Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor(augmentation): resolve Ruff SIM108 (#5607)

Alexcba3c5b3b0a4
diff --git a/kornia/augmentation/_2d/mix/base.py b/kornia/augmentation/_2d/mix/base.py
index 0236d7164..39bd49931 100644
--- a/kornia/augmentation/_2d/mix/base.py
+++ b/kornia/augmentation/_2d/mix/base.py
@@ -267,10 +267,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):
data_keys: Optional[List[Union[str, int, DataKey]]] = None,
) -> Union[torch.Tensor, List[torch.Tensor]]:
keys: List[DataKey]
- if data_keys is None:
- keys = self.data_keys
- else:
- keys = [DataKey.get(inp) for inp in data_keys]
+ keys = self.data_keys if data_keys is None else [DataKey.get(inp) for inp in data_keys]
for key in keys:
self._validate_data_key(key)
diff --git a/kornia/augmentation/_2d/mix/transplantation.py b/kornia/augmentation/_2d/mix/transplantation.py
index 67f75e835..1b4ba951f 100644
--- a/kornia/augmentation/_2d/mix/transplantation.py
+++ b/kornia/augmentation/_2d/mix/transplantation.py
@@ -389,10 +389,7 @@ class RandomTransplantation(MixAugmentationBaseV2):
**kwargs: dict[str, Any],
) -> torch.Tensor | list[torch.Tensor]:
keys: list[DataKey]
- if data_keys is None:
- keys = self.data_keys
- else:
- keys = [DataKey.get(inp) for inp in data_keys]
+ keys = self.data_keys if data_keys is None else [DataKey.get(inp) for inp in data_keys]
if params is None:
self._check_mask_given(input, keys)
diff --git a/kornia/augmentation/auto/rand_augment/rand_augment.py b/kornia/augmentation/auto/rand_augment/rand_augment.py
index 6101c30dc..8996315b3 100644
--- a/kornia/augmentation/auto/rand_augment/rand_augment.py
+++ b/kornia/augmentation/auto/rand_augment/rand_augment.py
@@ -100,10 +100,7 @@ class RandAugment(PolicyAugmentBase):
if m <= 0 or m >= 30:
raise ValueError(f"Expect `m` in (0, 30). Got {m}.")
- if policy is None:
- _policy = default_policy
- else:
- _policy = policy
+ _policy = default_policy if policy is None else policy
# `rand_selector` samples without replacement, so more operations than there are
# sub-policies cannot be honoured, and `n <= 0` silently applies nothing at all.
diff --git a/kornia/augmentation/auto/trivial_augment/trivial_augment.py b/kornia/augmentation/auto/trivial_augment/trivial_augment.py
index cbfa03256..a674bee43 100644
--- a/kornia/augmentation/auto/trivial_augment/trivial_augment.py
+++ b/kornia/augmentation/auto/trivial_augment/trivial_augment.py
@@ -83,10 +83,7 @@ class TrivialAugment(PolicyAugmentBase):
def __init__(
self, policy: Optional[List[SUBPOLICY_CONFIG]] = None, transformation_matrix_mode: str = "silent"
) -> None:
- if policy is None:
- _policy = default_policy
- else:
- _policy = policy
+ _policy = default_policy if policy is None else policy
super().__init__(_policy, transformation_matrix_mode=transformation_matrix_mode)
selection_weights = torch.tensor([1.0 / len(self)] * len(self))
diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.py
index eadac2525..7625481f0 100644
--- a/kornia/augmentation/utils/helpers.py
+++ b/kornia/augmentation/utils/helpers.py
@@ -189,10 +189,7 @@ def _infer_batch_shape(input: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tens
Input may be either (torch.Tensor,) or (torch.Tensor, transform_matrix)
"""
- if isinstance(input, tuple):
- tensor_var = _transform_input(input[0])
- else:
- tensor_var = _transform_input(input)
+ tensor_var = _transform_input(input[0]) if isinstance(input, tuple) else _transform_input(input)
return tensor_var.shape
@@ -201,10 +198,7 @@ def _infer_batch_shape3d(input: Union[torch.Tensor, Tuple[torch.Tensor, torch.Te
Input may be either (torch.Tensor,) or (torch.Tensor, transform_matrix)
"""
- if isinstance(input, tuple):
- tensor_var = _transform_input3d(input[0])
- else:
- tensor_var = _transform_input3d(input)
+ tensor_var = _transform_input3d(input[0]) if isinstance(input, tuple) else _transform_input3d(input)
return tensor_var.shape
diff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.py
index 9cc28a533..b3277071e 100644
--- a/tests/augmentation/container/test_augmentation_sequential.py
+++ b/tests/augmentation/container/test_augmentation_sequential.py
@@ -1271,10 +1271,7 @@ class TestConventionAugmentationSequential(BaseTester):
)
blocks = ((yy // 2) + (xx // 2)) % 2
- if mask_dtype == torch.bool:
- mask = blocks.bool()
- else:
- mask = (2 + blocks).to(mask_dtype)
+ mask = blocks.bool() if mask_dtype == torch.bool else (2 + blocks).to(mask_dtype)
mask = mask[None, None]
image = torch.rand(1, 3, 12, 16, device=device, dtype=dtype)
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index ef5b98f09..b627c9dc8 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -1415,10 +1415,7 @@ class TestColorJiggle(BaseTester):
@pytest.mark.parametrize("layout", ["channels_last", "transposed"])
def test_dynamo_fixed_order_noncontiguous(self, layout):
image = torch.rand(2, 3, 8, 10)
- if layout == "channels_last":
- image = image.to(memory_format=torch.channels_last)
- else:
- image = image.transpose(-1, -2)
+ image = image.to(memory_format=torch.channels_last) if layout == "channels_last" else image.transpose(-1, -2)
image.requires_grad_()
op = ColorJiggle(0.2, 0.2, 0.2, 0.1, p=1.0, order=(2, 3, 1, 0))
diff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.py
index c085796c8..cc69401e6 100644
--- a/tests/augmentation/test_auto_operation.py
+++ b/tests/augmentation/test_auto_operation.py
@@ -151,10 +151,7 @@ class TestRandAugment(BaseTester):
@pytest.mark.parametrize("policy", [None, [[("translate_y", -0.5, 0.5)]]])
def test_smoke(self, policy):
- if policy is None:
- n = len(randaug_config)
- else:
- n = 1
+ n = len(randaug_config) if policy is None else 1
aug = RandAugment(n=n, m=15, policy=policy)
in_tensor = torch.rand(10, 3, 50, 50, requires_grad=True)
aug(in_tensor)
diff --git a/tests/augmentation/test_random_generator.py b/tests/augmentation/test_random_generator.py
index a04929fd8..586cb28e3 100644
--- a/tests/augmentation/test_random_generator.py
+++ b/tests/augmentation/test_random_generator.py
@@ -2016,12 +2016,9 @@ class TestHalfPrecisionPositionSamplers:
generator.set_rng_device_and_dtype(device, half_dtype)
torch.manual_seed(0)
params = generator(batch_shape)
- if isinstance(generator, PatchMixGenerator):
- # (B, 2) patch starts; the far corner of a size-4 patch is 3 pixels on.
- last_covered = params["patch_coords"] + 3
- else:
- # (B, 4, 2) or (B, 8, 3) crop corners in pixel coordinates.
- last_covered = params["src"]
+ # PatchMix gives (B, 2) patch starts, and the far corner of a size-4 patch is 3 pixels on; the other
+ # generators give (B, 4, 2) or (B, 8, 3) crop corners in pixel coordinates.
+ last_covered = params["patch_coords"] + 3 if isinstance(generator, PatchMixGenerator) else params["src"]
assert int(last_covered.min()) >= 0
assert int(last_covered.max()) < 8