Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor(enhance): resolve Ruff SIM108 (#5608)

Alexcda9cbb37487
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.py
index 2b6e36628..ed2f41f31 100644
--- a/kornia/enhance/adjust.py
+++ b/kornia/enhance/adjust.py
@@ -539,10 +539,7 @@ def adjust_contrast_with_mean_subtraction(image: torch.Tensor, factor: Union[flo
# KORNIA_CHECK(any(factor >= 0), "Contrast factor must be positive.")
- if image.shape[-3] == 3:
- img_mean = rgb_to_grayscale(image).mean((-2, -1), True)
- else:
- img_mean = image.mean((-3, -2, -1), True)
+ img_mean = rgb_to_grayscale(image).mean((-2, -1), True) if image.shape[-3] == 3 else image.mean((-3, -2, -1), True)
# Apply contrast factor subtracting the mean
img_adjust: torch.Tensor = image * factor + img_mean * (1 - factor)
@@ -708,11 +705,7 @@ def adjust_sigmoid(image: torch.Tensor, cutoff: float = 0.5, gain: float = 10, i
"""
KORNIA_CHECK_IS_TENSOR(image, "Expected shape (*, H, W)")
- if inv:
- img_adjust = 1 - 1 / (1 + (gain * (cutoff - image)).exp())
- else:
- img_adjust = 1 / (1 + (gain * (cutoff - image)).exp())
- return img_adjust
+ return 1 - 1 / (1 + (gain * (cutoff - image)).exp()) if inv else 1 / (1 + (gain * (cutoff - image)).exp())
def adjust_log(image: torch.Tensor, gain: float = 1, inv: bool = False, clip_output: bool = True) -> torch.Tensor:
@@ -745,10 +738,7 @@ def adjust_log(image: torch.Tensor, gain: float = 1, inv: bool = False, clip_out
"""
KORNIA_CHECK_IS_TENSOR(image, "Expected shape (*, H, W)")
- if inv:
- img_adjust = (2**image - 1) * gain
- else:
- img_adjust = (1 + image).log2() * gain
+ img_adjust = (2**image - 1) * gain if inv else (1 + image).log2() * gain
# truncate between pixel values
if clip_output:
@@ -1208,10 +1198,7 @@ def invert(image: torch.Tensor, max_val: Optional[torch.Tensor] = None) -> torch
if not isinstance(image, torch.Tensor):
raise AssertionError(f"Input is not a torch.Tensor. Got: {type(input)}")
- if max_val is None:
- _max_val = torch.tensor([1.0])
- else:
- _max_val = max_val
+ _max_val = torch.tensor([1.0]) if max_val is None else max_val
if not isinstance(_max_val, torch.Tensor):
raise AssertionError(f"max_val is not a torch.Tensor. Got: {type(_max_val)}")
diff --git a/kornia/enhance/zca.py b/kornia/enhance/zca.py
index bc448c4cf..7c29cb8d2 100644
--- a/kornia/enhance/zca.py
+++ b/kornia/enhance/zca.py
@@ -319,10 +319,7 @@ def zca_mean(
cov = inp_center_flat.t().mm(inp_center_flat)
- if unbiased:
- cov = cov / float(N - 1)
- else:
- cov = cov / float(N)
+ cov = cov / float(N - 1 if unbiased else N)
U, S, _ = _torch_svd_cast(cov)
diff --git a/tests/enhance/test_histogram.py b/tests/enhance/test_histogram.py
index 6279bd5ec..c5d03b217 100644
--- a/tests/enhance/test_histogram.py
+++ b/tests/enhance/test_histogram.py
@@ -95,10 +95,7 @@ class TestImageHistogram2d(BaseTester):
sample = torch.linspace(0, 255, 10, device=device, dtype=dtype)
sample_x, _ = torch.meshgrid(sample, sample, indexing="ij")
sample_x = sample_x.repeat(*size)
- if kernel == "gaussian":
- bandwidth = 2 * 0.4**2
- else:
- bandwidth = None
+ bandwidth = 2 * 0.4**2 if kernel == "gaussian" else None
hist, _ = TestImageHistogram2d.fcn(sample_x, 0.0, 255.0, 10, bandwidth=bandwidth, centers=sample, kernel=kernel)
ans = 10 * torch.ones_like(hist)
self.assert_close(ans, hist)
@@ -109,10 +106,7 @@ class TestImageHistogram2d(BaseTester):
sample = torch.linspace(0, 255, 10, device=device, dtype=dtype)
sample_x, _ = torch.meshgrid(sample, sample, indexing="ij")
sample_x = sample_x.repeat(*size)
- if kernel == "gaussian":
- bandwidth = 2 * 0.4**2
- else:
- bandwidth = None
+ bandwidth = 2 * 0.4**2 if kernel == "gaussian" else None
hist, pdf = TestImageHistogram2d.fcn(
sample_x, 0.0, 255.0, 10, bandwidth=bandwidth, centers=sample, kernel=kernel, return_pdf=True
)
diff --git a/tests/enhance/test_zca.py b/tests/enhance/test_zca.py
index edfac79c9..a733d90a6 100644
--- a/tests/enhance/test_zca.py
+++ b/tests/enhance/test_zca.py
@@ -81,10 +81,7 @@ class TestZCA(BaseTester):
def test_zca_unbiased(self, unbiased, device, dtype):
data = torch.tensor([[0, 1], [1, 0], [-1, 0], [0, -1]], device=device, dtype=dtype)
- if unbiased:
- unbiased_val = 1.5
- else:
- unbiased_val = 2.0
+ unbiased_val = 1.5 if unbiased else 2.0
expected = torch.sqrt(unbiased_val * torch.abs(data)) * torch.sign(data)
@@ -307,10 +304,7 @@ class TestZCA(BaseTester):
def test_zca_whiten_func_unbiased(self, unbiased, device, dtype):
data = torch.tensor([[0, 1], [1, 0], [-1, 0], [0, -1]], device=device, dtype=dtype)
- if unbiased:
- unbiased_val = 1.5
- else:
- unbiased_val = 2.0
+ unbiased_val = 1.5 if unbiased else 2.0
expected = torch.sqrt(unbiased_val * torch.abs(data)) * torch.sign(data)