Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor(geometry): resolve Ruff SIM108 (#5606)

Alexd2383ffef5d4
diff --git a/kornia/geometry/boxes.py b/kornia/geometry/boxes.py
index 77761ffc0..103b44801 100644
--- a/kornia/geometry/boxes.py
+++ b/kornia/geometry/boxes.py
@@ -425,10 +425,7 @@ class Boxes:
Returns:
:class:`Boxes` containing the updated coordinates.
"""
- if inplace:
- _data = self._data
- else:
- _data = self._data.clone()
+ _data = self._data if inplace else self._data.clone()
if isinstance(values, Boxes):
_data.index_put_(indices, values.data)
@@ -542,10 +539,7 @@ class Boxes:
"`Boxes.clamp` accepts `topleft` and `botright` as `(B, 2)` torch.Tensor bounds; "
f"got topleft={type(topleft).__name__} and botright={type(botright).__name__}."
)
- if inplace:
- _data = self._data
- else:
- _data = self._data.clone()
+ _data = self._data if inplace else self._data.clone()
# Broadcast the per-image bounds rather than materialising them at the data's shape: the
# masked assignment this replaces needed a bound tensor of exactly the mask's shape, which
# is what tied it to the batched (B, N, 4) indexing. ``torch.where`` on the same comparison
@@ -611,10 +605,7 @@ class Boxes:
out-of-range boxes replaced by zero coordinates.
"""
area = self.compute_area()
- if inplace:
- _data = self._data
- else:
- _data = self._data.clone()
+ _data = self._data if inplace else self._data.clone()
if min_area is not None:
_data[area < min_area] = 0.0
if max_area is not None:
@@ -884,10 +875,7 @@ class Boxes:
# GPU Hotpath (vectorized)
# -----------------
out_shape: Tuple[int, ...]
- if is_batched:
- out_shape = (self.shape[0], self.shape[1], height, width)
- else:
- out_shape = (self.shape[0], height, width)
+ out_shape = (self.shape[0], self.shape[1], height, width) if is_batched else (self.shape[0], height, width)
xyxy = clipped_boxes_xyxy.view(-1, 4).round().long()
diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.py
index 91f4dd48a..e4c50d55d 100644
--- a/kornia/geometry/homography.py
+++ b/kornia/geometry/homography.py
@@ -452,13 +452,10 @@ def _refine_homography_lm(
tangent = (eye9 - 2 * v[:, :, None] * v[:, None, :])[:, :, :8] # (K, 9, 8)
stacked = torch.cat([h[:, :, None], tangent], 2).mT.reshape(K, 27, 3) # rows of H, then of each direction
P = (stacked @ x1.T).reshape(K, 9, 3, -1) # (K, 9, 3, N): P = H x1, then its directional derivatives
- if mask is None:
- iz = 1.0 / P[:, 0, 2]
- else:
- # A zero mask excludes the correspondence. Guarding its projective divisor keeps the residual and
- # Jacobian of a finite row finite, so the zero weight below removes it exactly; masking the whole
- # derivative stack as well costs the compiled CUDA program about 10% of a homography call.
- iz = 1.0 / torch.where(mask != 0, P[:, 0, 2], torch.ones_like(P[:, 0, 2]))
+ # When a mask is given, a zero mask excludes the correspondence. Guarding its projective divisor keeps the
+ # residual and Jacobian of a finite row finite, so the zero weight below removes it exactly; masking the whole
+ # derivative stack as well costs the compiled CUDA program about 10% of a homography call.
+ iz = 1.0 / (P[:, 0, 2] if mask is None else torch.where(mask != 0, P[:, 0, 2], torch.ones_like(P[:, 0, 2])))
uv = P[:, 0, :2] * iz[:, None] # (K, 2, N)
r = (uv - x2.T).flatten(1) # (K, 2N): u residuals, then v residuals
J = ((P[:, 1:, :2] - uv[:, None] * P[:, 1:, 2:3]) * iz[:, None, None]).flatten(2) # (K, 8, 2N)
diff --git a/kornia/geometry/keypoints.py b/kornia/geometry/keypoints.py
index cc954ea4e..b3d381530 100644
--- a/kornia/geometry/keypoints.py
+++ b/kornia/geometry/keypoints.py
@@ -145,10 +145,7 @@ class Keypoints:
Returns:
:class:`Keypoints` object containing the updated coordinates.
"""
- if inplace:
- _data = self._data
- else:
- _data = self._data.clone()
+ _data = self._data if inplace else self._data.clone()
if isinstance(values, Keypoints):
_data.index_put_(indices, values.data)
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 2329c4f28..5869c7f45 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -976,10 +976,7 @@ class Affine(nn.Module):
else:
sx, sy = self.shear[..., 0], self.shear[..., 1]
- if self.center is None:
- center = _compute_tensor_center(input).expand(input.size()[0], -1)
- else:
- center = self.center
+ center = _compute_tensor_center(input).expand(input.size()[0], -1) if self.center is None else self.center
matrix = get_affine_matrix2d(self.translation, center, self.scale_factor, -self.angle, sx=sx, sy=sy)
return affine(input, matrix[..., :2, :3], self.mode, self.padding_mode, self.align_corners)
diff --git a/tests/geometry/liegroup/test_se2.py b/tests/geometry/liegroup/test_se2.py
index fe8afaefe..003251dcd 100644
--- a/tests/geometry/liegroup/test_se2.py
+++ b/tests/geometry/liegroup/test_se2.py
@@ -56,10 +56,7 @@ class TestSe2(BaseTester):
assert s.exp(theta).so2.z.shape == input_shape
assert s.exp(theta).t.shape == t_input_shape
assert s.log().shape == (*input_shape, 3)
- if not any(input_shape):
- expected_hat_shape = (3, 3)
- else:
- expected_hat_shape = (input_shape[0], 3, 3)
+ expected_hat_shape = (input_shape[0], 3, 3) if any(input_shape) else (3, 3)
assert s.hat(theta).shape == expected_hat_shape
assert s.inverse().so2.z.shape == input_shape
assert s.inverse().t.shape == t_input_shape
@@ -163,10 +160,7 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
def test_mul_vector(self, device, dtype, batch_size):
s1 = Se2.identity(batch_size, device, dtype)
- if batch_size is None:
- shape = ()
- else:
- shape = (batch_size,)
+ shape = () if batch_size is None else (batch_size,)
s2 = Se2(So2.identity(batch_size, device, dtype), Vector2.random(shape, device, dtype))
s1_pose_s2 = s1 * s2
s2_pose_s2 = s2 * s2.inverse()
diff --git a/tests/geometry/liegroup/test_se3.py b/tests/geometry/liegroup/test_se3.py
index 4e23a6342..bdb4efab7 100644
--- a/tests/geometry/liegroup/test_se3.py
+++ b/tests/geometry/liegroup/test_se3.py
@@ -36,10 +36,7 @@ class TestSe3(BaseTester):
def _make_rand_se3d_vec(self, device, dtype, batch_size) -> Se3:
q = Quaternion.random(batch_size, device, dtype)
- if batch_size is None:
- shape = ()
- else:
- shape = (batch_size,)
+ shape = () if batch_size is None else (batch_size,)
t = Vector3.random(shape, device, dtype)
return Se3(So3(q), t)
@@ -169,10 +166,7 @@ class TestSe3(BaseTester):
def test_mul_vector(self, device, dtype, batch_size):
world_pose_s1: Se3 = self._make_rand_se3d(device, dtype, batch_size)
world_pose_s2: Se3 = self._make_rand_se3d_vec(device, dtype, batch_size)
- if batch_size is None:
- shape = ()
- else:
- shape = (batch_size,)
+ shape = () if batch_size is None else (batch_size,)
pt_in_world = Vector3.random(shape, device, dtype)
s1_pose_s2: Se3 = world_pose_s1.inverse() * world_pose_s2
pt_in_s1 = world_pose_s1.inverse() * pt_in_world
diff --git a/tests/geometry/liegroup/test_so2.py b/tests/geometry/liegroup/test_so2.py
index 20a2768c0..da5af1751 100644
--- a/tests/geometry/liegroup/test_so2.py
+++ b/tests/geometry/liegroup/test_so2.py
@@ -51,10 +51,7 @@ class TestSo2(BaseTester):
assert (s * s).z.shape == input_shape
assert s.exp(theta).z.shape == input_shape
assert s.log().shape == input_shape
- if not any(input_shape):
- expected_hat_shape = (2, 2)
- else:
- expected_hat_shape = (input_shape[0], 2, 2)
+ expected_hat_shape = (input_shape[0], 2, 2) if any(input_shape) else (2, 2)
assert s.hat(theta).shape == expected_hat_shape
assert s.inverse().z.shape == input_shape
@@ -151,10 +148,7 @@ class TestSo2(BaseTester):
@pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
def test_mul_vector(self, device, dtype, batch_size):
s1 = So2.identity(batch_size, device, dtype)
- if batch_size is None:
- shape = ()
- else:
- shape = (batch_size,)
+ shape = () if batch_size is None else (batch_size,)
t1 = Vector2.random(shape, device, dtype)
t2 = Vector2.random(shape, device, dtype)
self.assert_close((s1 * t1), t1)
diff --git a/tests/geometry/liegroup/test_so3.py b/tests/geometry/liegroup/test_so3.py
index 838117b40..ac22af468 100644
--- a/tests/geometry/liegroup/test_so3.py
+++ b/tests/geometry/liegroup/test_so3.py
@@ -309,10 +309,7 @@ class TestSo3(BaseTester):
def test_mul_vec(self, device, dtype, batch_size):
q1 = Quaternion.identity(batch_size, device, dtype)
q2 = Quaternion.random(batch_size, device, dtype)
- if batch_size is None:
- shape = ()
- else:
- shape = (batch_size,)
+ shape = () if batch_size is None else (batch_size,)
t = Vector3.random(shape, device, dtype)
s1 = So3(q1)
s2 = So3(q2)