Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test: narrow pytest.raises blocks for Ruff PT012 (#5057)

Daniele Lorued5cb3745a38
diff --git a/tests/augmentation/container/test_video_sequential.py b/tests/augmentation/container/test_video_sequential.py
index 276da0dc8..4be7b341d 100644
--- a/tests/augmentation/container/test_video_sequential.py
+++ b/tests/augmentation/container/test_video_sequential.py
@@ -36,8 +36,8 @@ class TestVideoSequential:
@pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
def test_exception(self, shape, data_format, device, dtype):
aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1), data_format=data_format, same_on_frame=True)
+ img = torch.randn(*shape, device=device, dtype=dtype)
with pytest.raises(AssertionError):
- img = torch.randn(*shape, device=device, dtype=dtype)
aug_list(img)
@pytest.mark.parametrize(
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index eeae2485d..42674082c 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -6005,8 +6005,7 @@ class TestRandomSnow(BaseTester):
exception_test_data = self._get_exception_test_data(device, dtype)
for err_msg, snow_coef, brght_coef, input_data in exception_test_data:
with pytest.raises(Exception) as errinfo:
- aug = RandomSnow(p=1.0, snow_coefficient=snow_coef, brightness=brght_coef)
- aug(input_data)
+ RandomSnow(p=1.0, snow_coefficient=snow_coef, brightness=brght_coef)(input_data)
assert err_msg in str(errinfo)
@@ -6158,8 +6157,7 @@ class TestRandomRain(BaseTester):
exception_test_data = self._get_exception_test_data(device, dtype)
for err_msg, drop_height, drop_width, input_data in exception_test_data:
with pytest.raises(Exception) as errinfo:
- aug = RandomRain(p=1.0, drop_height=drop_height, drop_width=drop_width)
- aug(input_data)
+ RandomRain(p=1.0, drop_height=drop_height, drop_width=drop_width)(input_data)
assert err_msg in str(errinfo)
diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.py
index 55e93f687..d6d0030b0 100644
--- a/tests/augmentation/test_augmentation_mix.py
+++ b/tests/augmentation/test_augmentation_mix.py
@@ -1148,25 +1148,25 @@ class TestRandomTransplantation(BaseTester):
with pytest.raises(Exception, match="excluded_labels must be a 1-dimensional"):
RandomTransplantation(p=1.0, excluded_labels=torch.tensor([[0, 1]], device=device, dtype=dtype))
+ f = RandomTransplantation(p=1.0)
with pytest.raises(Exception, match=r"Length of keys.*does not match number of inputs"):
- f = RandomTransplantation(p=1.0)
f(image, mask, data_keys=["input", "mask", "mask"])
+ params_copy = copy.deepcopy(params)
+ params_copy["selected_labels"] = torch.tensor([[0, 1]], device=device, dtype=dtype)
+ del params_copy["selection"]
with pytest.raises(Exception, match=r"selected_labels must be a 1-dimensional torch\.tensor"):
- params_copy = copy.deepcopy(params)
- params_copy["selected_labels"] = torch.tensor([[0, 1]], device=device, dtype=dtype)
- del params_copy["selection"]
f(image, mask, params=params_copy)
+ params_copy = copy.deepcopy(params)
+ params_copy["selected_labels"] = torch.tensor([0, 1], device=device, dtype=dtype)
+ del params_copy["selection"]
with pytest.raises(Exception, match="There cannot be more selected labels"):
- params_copy = copy.deepcopy(params)
- params_copy["selected_labels"] = torch.tensor([0, 1], device=device, dtype=dtype)
- del params_copy["selection"]
f(image, mask, params=params_copy)
with pytest.raises(Exception, match="Every image input must have one additional dimension"):
f(image.unsqueeze(dim=-1), mask)
+ image = torch.rand(1, 3, 2, 5, device=device, dtype=torch.float64)
with pytest.raises(Exception, match="The dimensions of the input image and segmentation mask must match"):
- image = torch.rand(1, 3, 2, 5, device=device, dtype=torch.float64)
f(image, mask)
diff --git a/tests/augmentation/test_random_generator.py b/tests/augmentation/test_random_generator.py
index a8de0a34b..9b856df27 100644
--- a/tests/augmentation/test_random_generator.py
+++ b/tests/augmentation/test_random_generator.py
@@ -1515,10 +1515,9 @@ class TestRandomPosterizeGen(RandomGeneratorBaseTests):
)
def test_invalid_param_combinations(self, bits, device, dtype):
with pytest.raises(Exception):
- if isinstance(bits, Tensor):
- PosterizeGenerator(bits.to(device=device, dtype=dtype))(torch.Size([3]))
- else:
- PosterizeGenerator(bits)(torch.Size([3]))
+ PosterizeGenerator(bits.to(device=device, dtype=dtype) if isinstance(bits, Tensor) else bits)(
+ torch.Size([3])
+ )
def test_random_gen(self, device, dtype):
torch.manual_seed(9)
diff --git a/tests/augmentation/test_random_generator_3d.py b/tests/augmentation/test_random_generator_3d.py
index 3fa51004b..7f9e3a89a 100644
--- a/tests/augmentation/test_random_generator_3d.py
+++ b/tests/augmentation/test_random_generator_3d.py
@@ -68,8 +68,9 @@ class TestRandomPerspectiveGen3D(RandomGeneratorBaseTests):
)
def test_invalid_param_combinations(self, depth, height, width, distortion_scale, device, dtype):
with pytest.raises(Exception):
- param_gen = PerspectiveGenerator3D(distortion_scale=distortion_scale.to(device=device, dtype=dtype))
- param_gen(batch_shape=torch.Size((2, depth, height, width)))
+ PerspectiveGenerator3D(distortion_scale=distortion_scale.to(device=device, dtype=dtype))(
+ batch_shape=torch.Size((2, depth, height, width))
+ )
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
@@ -254,8 +255,9 @@ class TestRandomAffineGen3D(RandomGeneratorBaseTests):
shear.to(dtype=dtype, device=device)
with pytest.raises(Exception):
- param_gen = AffineGenerator3D(degrees=degrees, translate=translate, scale=scale, shears=shear)
- param_gen(batch_shape=torch.Size((2, depth, height, width)))
+ AffineGenerator3D(degrees=degrees, translate=translate, scale=scale, shears=shear)(
+ batch_shape=torch.Size((2, depth, height, width))
+ )
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
@@ -352,8 +354,7 @@ class TestRandomRotationGen3D(RandomGeneratorBaseTests):
)
def test_invalid_param_combinations(self, degrees, device, dtype):
with pytest.raises(Exception):
- param_gen = RotationGenerator3D(degrees=degrees.to(device=device, dtype=dtype))
- param_gen(torch.Size((2,)))
+ RotationGenerator3D(degrees=degrees.to(device=device, dtype=dtype))(torch.Size((2,)))
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
@@ -412,11 +413,10 @@ class TestRandomCropGen3D(RandomGeneratorBaseTests):
)
def test_invalid_param_combinations(self, input_size, size, resize_to, device, dtype):
with pytest.raises(Exception):
- param_gen = CropGenerator3D(
+ CropGenerator3D(
size=size.to(device=device, dtype=dtype) if isinstance(size, torch.Tensor) else size,
resize_to=resize_to,
- )
- param_gen(batch_shape=torch.Size((2, 1, *input_size)))
+ )(batch_shape=torch.Size((2, 1, *input_size)))
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
@@ -645,13 +645,11 @@ class TestRandomMotionBlur3D(RandomGeneratorBaseTests):
)
def test_invalid_param_combinations(self, kernel_size, angle, direction, device, dtype):
with pytest.raises(Exception):
- param_gen = MotionBlurGenerator3D(
+ MotionBlurGenerator3D(
kernel_size=kernel_size,
angle=angle.to(device=device, dtype=dtype),
direction=direction.to(device=device, dtype=dtype),
- )
-
- param_gen(batch_shape=torch.Size((2,)))
+ )(batch_shape=torch.Size((2,)))
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
diff --git a/tests/contrib/test_connected_component.py b/tests/contrib/test_connected_component.py
index 5ec8c560f..c829d9340 100644
--- a/tests/contrib/test_connected_component.py
+++ b/tests/contrib/test_connected_component.py
@@ -50,9 +50,9 @@ class TestConnectedComponents(BaseTester):
assert kornia.contrib.connected_components(img, 0)
assert "Input num_iterations must be a positive integer." in str(errinf)
+ img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError) as errinf:
- img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- assert kornia.contrib.connected_components(img, 2)
+ kornia.contrib.connected_components(img, 2)
assert "Input image shape must be (*,1,H,W). Got:" in str(errinf)
def test_value(self, device, dtype):
diff --git a/tests/contrib/test_conv_distance_transformer.py b/tests/contrib/test_conv_distance_transformer.py
index d6b716de6..b5509a11e 100755
--- a/tests/contrib/test_conv_distance_transformer.py
+++ b/tests/contrib/test_conv_distance_transformer.py
@@ -62,8 +62,7 @@ class TestConvDistanceTransform(BaseTester):
# Non-odd kernel size -> BaseError from KORNIA_CHECK
with pytest.raises(BaseError) as excinfo:
- ConvDT = kornia.contrib.DistanceTransform(6)
- ConvDT.forward(sample2d)
+ kornia.contrib.DistanceTransform(6).forward(sample2d)
assert "kernel_size must be an odd integer >= 3" in str(excinfo.value)
with pytest.raises(BaseError):
diff --git a/tests/contrib/test_kmeans.py b/tests/contrib/test_kmeans.py
index 88188a165..e38faea76 100644
--- a/tests/contrib/test_kmeans.py
+++ b/tests/contrib/test_kmeans.py
@@ -72,27 +72,27 @@ class TestKMeans(BaseTester):
assert "num_clusters can't be 0" in str(errinfo.value)
# case: cluster centers is not a 2D tensor
+ starting_centers = torch.rand((2, 3, 5), device=device, dtype=dtype)
with pytest.raises(ShapeError) as errinfo:
- starting_centers = torch.rand((2, 3, 5), device=device, dtype=dtype)
- kmeans = kornia.contrib.KMeans(None, starting_centers, 1e-3, 100, 0)
+ kornia.contrib.KMeans(None, starting_centers, 1e-3, 100, 0)
assert "Shape dimension mismatch" in str(errinfo.value)
# case: input data is not a 2D tensor
+ kmeans = kornia.contrib.KMeans(3, None, 1e-3, 100, 0)
with pytest.raises(ShapeError) as errinfo:
- kmeans = kornia.contrib.KMeans(3, None, 1e-3, 100, 0)
kmeans.fit(torch.rand((1000, 5, 60), dtype=dtype, device=device))
assert "Shape dimension mismatch" in str(errinfo.value) or "Expected shape" in str(errinfo.value)
# case: column dimensions of cluster centers and data to be predicted do not match
+ kmeans = kornia.contrib.KMeans(3, None, 1e-3, 100, 0)
+ kmeans.fit(torch.rand((1000, 5), dtype=dtype))
with pytest.raises(Exception) as errinfo:
- kmeans = kornia.contrib.KMeans(3, None, 1e-3, 100, 0)
- kmeans.fit(torch.rand((1000, 5), dtype=dtype))
kmeans.predict(torch.rand((10, 7), dtype=dtype))
assert "7 != 5" in str(errinfo)
# case: num_clusters does not match the number of rows in an explicit cluster_centers
+ starting_centers = torch.rand((5, 2), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- starting_centers = torch.rand((5, 2), device=device, dtype=dtype)
kornia.contrib.KMeans(3, starting_centers, 1e-3, 100, 0)
assert "cluster_centers has 5 rows but num_clusters=3" in str(errinfo.value)
diff --git a/tests/core/test_helpers.py b/tests/core/test_helpers.py
index 92ab27d31..b7df02332 100644
--- a/tests/core/test_helpers.py
+++ b/tests/core/test_helpers.py
@@ -101,9 +101,9 @@ class TestInverseCast:
assert_close(op(x), op_jit(x))
def test_not_invertible(self, device, dtype):
+ x = torch.tensor([[0.0, 0.0], [0.0, 0.0]], device=device, dtype=dtype)
with pytest.raises(RuntimeError):
- x = torch.tensor([[0.0, 0.0], [0.0, 0.0]], device=device, dtype=dtype)
- _ = _torch_inverse_cast(x)
+ _torch_inverse_cast(x)
@pytest.mark.parametrize("n", [2, 3, 4])
def test_closed_form_matches_linalg_inv(self, device, dtype, n):
diff --git a/tests/enhance/test_integral.py b/tests/enhance/test_integral.py
index 5c91b95fa..cfac51cbf 100644
--- a/tests/enhance/test_integral.py
+++ b/tests/enhance/test_integral.py
@@ -38,11 +38,9 @@ class TestIntegralTensor(BaseTester):
def test_exception(self, device, dtype):
tensor = torch.rand(1, 1, 4, 4, device=device, dtype=dtype)
with pytest.raises(Exception):
- dim = (0, 1, 2, 3, 4)
- integral_tensor(tensor, dim)
+ integral_tensor(tensor, (0, 1, 2, 3, 4))
with pytest.raises(Exception):
- dim = (4, 5)
- integral_tensor(tensor, dim)
+ integral_tensor(tensor, (4, 5))
with pytest.raises(Exception) as errinfo:
integral_tensor(tensor, ())
assert "dim must be a non-empty tuple." in str(errinfo)
diff --git a/tests/enhance/test_jpeg.py b/tests/enhance/test_jpeg.py
index 825d85b79..80609dfd3 100644
--- a/tests/enhance/test_jpeg.py
+++ b/tests/enhance/test_jpeg.py
@@ -143,37 +143,37 @@ class TestDiffJPEG(BaseTester):
def test_exception(self, device, dtype) -> None:
"""Test exceptions (non-tensor input, wrong JPEG quality shape, wrong img shape, and wrong QT shape.)"""
+ B = 2
+ jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
with pytest.raises(TypeError) as errinfo:
- B = 2
- jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(1904.0, jpeg_quality)
assert "Input input type is not a torch.Tensor" in str(errinfo.value)
from kornia.core.exceptions import TypeCheckError
+ B, H, W = 2, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
with pytest.raises(TypeCheckError) as errinfo:
- B, H, W = 2, 32, 32
- img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, None)
assert "Type mismatch: expected Tensor" in str(errinfo.value)
from kornia.core.exceptions import BaseError
+ B, H, W = 2, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B, 3, 2, 1), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 2, 32, 32
- img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B, 3, 2, 1), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
assert "Shape dimension mismatch" in str(errinfo.value) or "Expected shape" in str(errinfo.value)
from kornia.core.exceptions import BaseError
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 7, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 4, 32, 32
- img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
- qt_y = torch.randint(low=1, high=255, size=(B, 7, 8), device=device, dtype=dtype)
- qt_c = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
assert (
"Shape dimension mismatch" in str(errinfo.value)
@@ -183,12 +183,12 @@ class TestDiffJPEG(BaseTester):
from kornia.core.exceptions import BaseError
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 7), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 4, 32, 32
- img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B,), device=device, dtype=dtype)
- qt_y = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
- qt_c = torch.randint(low=1, high=255, size=(B, 8, 7), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
assert (
"Shape dimension mismatch" in str(errinfo.value)
@@ -198,34 +198,34 @@ class TestDiffJPEG(BaseTester):
from kornia.core.exceptions import BaseError
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B * B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B * 2, 8, 8), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 4, 32, 32
- img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B * B,), device=device, dtype=dtype)
- qt_y = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
- qt_c = torch.randint(low=1, high=255, size=(B * 2, 8, 8), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
assert "Batch dimensions do not match" in str(errinfo.value)
from kornia.core.exceptions import BaseError
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B * B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B * 2, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 4, 32, 32
- img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B * B,), device=device, dtype=dtype)
- qt_y = torch.randint(low=1, high=255, size=(B * 2, 8, 8), device=device, dtype=dtype)
- qt_c = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
assert "Batch dimensions do not match" in str(errinfo.value)
from kornia.core.exceptions import BaseError
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=1, high=100, size=(B * 2,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
with pytest.raises(BaseError) as errinfo:
- B, H, W = 4, 32, 32
- img = torch.rand(B, B, 3, H, W, device=device, dtype=dtype)
- jpeg_quality = torch.randint(low=1, high=100, size=(B * 2,), device=device, dtype=dtype)
- qt_y = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
- qt_c = torch.randint(low=1, high=255, size=(B * B, 8, 8), device=device, dtype=dtype)
kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
assert "Batch dimensions do not match" in str(errinfo.value)
diff --git a/tests/enhance/test_zca.py b/tests/enhance/test_zca.py
index bcd9b4bed..1d6c3e85e 100644
--- a/tests/enhance/test_zca.py
+++ b/tests/enhance/test_zca.py
@@ -127,17 +127,15 @@ class TestZCA(BaseTester):
self.gradcheck(zca, (data,))
def test_not_fitted(self, device, dtype):
+ data = torch.rand(10, 2, device=device, dtype=dtype)
+ zca = kornia.enhance.ZCAWhitening()
with pytest.raises(RuntimeError):
- data = torch.rand(10, 2, device=device, dtype=dtype)
-
- zca = kornia.enhance.ZCAWhitening()
zca(data)
def test_not_fitted_inv(self, device, dtype):
+ data = torch.rand(10, 2, device=device, dtype=dtype)
+ zca = kornia.enhance.ZCAWhitening()
with pytest.raises(RuntimeError):
- data = torch.rand(10, 2, device=device, dtype=dtype)
-
- zca = kornia.enhance.ZCAWhitening()
zca.inverse_transform(data)
def test_jit(self, device, dtype):
diff --git a/tests/geometry/calibration/test_undistort.py b/tests/geometry/calibration/test_undistort.py
index e62b5fe85..45e042be4 100644
--- a/tests/geometry/calibration/test_undistort.py
+++ b/tests/geometry/calibration/test_undistort.py
@@ -459,34 +459,34 @@ class TestUndistortImage(BaseTester):
self.assert_close(actual, expected)
def test_exception(self, device, dtype):
+ im = torch.rand(5, 5, device=device, dtype=dtype)
+ K = torch.rand(3, 3, device=device, dtype=dtype)
+ distCoeff = torch.rand(4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- im = torch.rand(5, 5, device=device, dtype=dtype)
- K = torch.rand(3, 3, device=device, dtype=dtype)
- distCoeff = torch.rand(4, device=device, dtype=dtype)
undistort_image(im, K, distCoeff)
+ im = torch.rand(3, 5, 5, device=device, dtype=dtype)
+ K = torch.rand(4, 4, device=device, dtype=dtype)
+ distCoeff = torch.rand(4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- im = torch.rand(3, 5, 5, device=device, dtype=dtype)
- K = torch.rand(4, 4, device=device, dtype=dtype)
- distCoeff = torch.rand(4, device=device, dtype=dtype)
undistort_image(im, K, distCoeff)
+ im = torch.rand(3, 5, 5, device=device, dtype=dtype)
+ K = torch.rand(3, 3, device=device, dtype=dtype)
+ distCoeff = torch.rand(6, device=device, dtype=dtype)
with pytest.raises(ValueError):
- im = torch.rand(3, 5, 5, device=device, dtype=dtype)
- K = torch.rand(3, 3, device=device, dtype=dtype)
- distCoeff = torch.rand(6, device=device, dtype=dtype)
undistort_image(im, K, distCoeff)
+ im = torch.randint(0, 256, (3, 5, 5), device=device, dtype=torch.uint8)
+ K = torch.rand(3, 3, device=device, dtype=dtype)
+ distCoeff = torch.rand(4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- im = torch.randint(0, 256, (3, 5, 5), device=device, dtype=torch.uint8)
- K = torch.rand(3, 3, device=device, dtype=dtype)
- distCoeff = torch.rand(4, device=device, dtype=dtype)
undistort_image(im, K, distCoeff)
+ im = torch.rand(1, 1, 3, 5, 5, device=device, dtype=dtype)
+ K = torch.rand(1, 3, 3, device=device, dtype=dtype)
+ distCoeff = torch.rand(1, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- im = torch.rand(1, 1, 3, 5, 5, device=device, dtype=dtype)
- K = torch.rand(1, 3, 3, device=device, dtype=dtype)
- distCoeff = torch.rand(1, 4, device=device, dtype=dtype)
undistort_image(im, K, distCoeff)
def test_opencv(self, device, dtype):
diff --git a/tests/geometry/camera/test_stereo.py b/tests/geometry/camera/test_stereo.py
index 14ede7483..c6e0be98d 100644
--- a/tests/geometry/camera/test_stereo.py
+++ b/tests/geometry/camera/test_stereo.py
@@ -209,10 +209,11 @@ class TestStereoCamera(BaseTester):
disparity = torch.ones(disparity_shape, device=device, dtype=dtype)
with pytest.raises(StereoException) as exc_info:
- if entrypoint == "method":
+ (
camera.reproject_disparity_to_3D(disparity)
- else:
- reproject_disparity_to_3D(disparity, camera.Q)
+ if entrypoint == "method"
+ else reproject_disparity_to_3D(disparity, camera.Q)
+ )
assert str(exc_info.value).splitlines()[0] == (
"Expected 'disparity_tensor' to have channels-last shape (B, H, W, 1) "
diff --git a/tests/geometry/liegroup/test_se2.py b/tests/geometry/liegroup/test_se2.py
index 9314373c5..bc1a85ea0 100644
--- a/tests/geometry/liegroup/test_se2.py
+++ b/tests/geometry/liegroup/test_se2.py
@@ -66,33 +66,34 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (1, 2, 5))
def test_exception(self, device, dtype, batch_size):
+ r = So2.random(batch_size)
+ t1 = torch.randn((batch_size, 1), dtype=dtype, device=device)
+ t2 = torch.randn((batch_size, 3), dtype=dtype, device=device)
with pytest.raises(ValueError):
- r = So2.random(batch_size)
- t1 = torch.randn((batch_size, 1), dtype=dtype, device=device)
- t2 = torch.randn((batch_size, 3), dtype=dtype, device=device)
Se2(r, t1)
+ with pytest.raises(ValueError):
Se2(r, t2)
+ theta = torch.rand((batch_size, 2), dtype=dtype, device=device)
with pytest.raises(ValueError):
- theta = torch.rand((batch_size, 2), dtype=dtype, device=device)
Se2.exp(theta)
+ v = torch.rand((batch_size, 2), dtype=dtype, device=device)
with pytest.raises(ValueError):
- v = torch.rand((batch_size, 2), dtype=dtype, device=device)
Se2.hat(v)
+ omega = torch.rand((4, 4), dtype=dtype, device=device)
with pytest.raises(ValueError):
- omega = torch.rand((4, 4), dtype=dtype, device=device)
Se2.vee(omega)
with pytest.raises(TypeError):
Se2.identity(1, device, dtype) * [1.0, 2.0, 1.0]
+ theta = torch.rand((batch_size, 2), dtype=dtype, device=device)
with pytest.raises(ValueError):
- theta = torch.rand((batch_size, 2), dtype=dtype, device=device)
Se2.hat(theta)
with pytest.raises(Exception):
Se2.identity(batch_size=0)
with pytest.raises(Exception):
Se2.random(batch_size=0)
+ x = torch.rand(5, dtype=dtype, device=device)
+ y = torch.rand(3, dtype=dtype, device=device)
with pytest.raises(Exception):
- x = torch.rand(5, dtype=dtype, device=device)
- y = torch.rand(3, dtype=dtype, device=device)
Se2.trans(x, y)
def test_gradcheck(self, device):
diff --git a/tests/geometry/liegroup/test_so2.py b/tests/geometry/liegroup/test_so2.py
index 2608344b8..4f4aad7d0 100644
--- a/tests/geometry/liegroup/test_so2.py
+++ b/tests/geometry/liegroup/test_so2.py
@@ -70,22 +70,22 @@ class TestSo2(BaseTester):
@pytest.mark.parametrize("batch_size", (1, 2, 5))
@pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))
def test_exception(self, batch_size, device, dtype, cdtype):
+ z = torch.randn(batch_size, 2, dtype=cdtype, device=device)
with pytest.raises(ValueError):
- z = torch.randn(batch_size, 2, dtype=cdtype, device=device)
assert So2(z)
with pytest.raises(TypeError):
assert So2.identity(1, device, dtype) * [1.0, 2.0, 1.0]
+ theta = torch.rand((2, 2), dtype=dtype, device=device)
with pytest.raises(ValueError):
- theta = torch.rand((2, 2), dtype=dtype, device=device)
assert So2.exp(theta)
+ theta = torch.rand((2, 2), dtype=dtype, device=device)
with pytest.raises(ValueError):
- theta = torch.rand((2, 2), dtype=dtype, device=device)
assert So2.hat(theta)
+ m = torch.rand((2, 2, 1), dtype=dtype, device=device)
with pytest.raises(ValueError):
- m = torch.rand((2, 2, 1), dtype=dtype, device=device)
assert So2.from_matrix(m)
+ m = torch.rand((2, 2, 1), dtype=dtype, device=device)
with pytest.raises(ValueError):
- m = torch.rand((2, 2, 1), dtype=dtype, device=device)
assert So2.from_matrix(m)
with pytest.raises(Exception):
assert So2.identity(batch_size=0)
diff --git a/tests/geometry/transform/test_affine.py b/tests/geometry/transform/test_affine.py
index dcc33c0e3..6c5dad7a0 100644
--- a/tests/geometry/transform/test_affine.py
+++ b/tests/geometry/transform/test_affine.py
@@ -714,9 +714,9 @@ class TestAffine2d(BaseTester):
kornia.geometry.transform.Affine()
def test_affine_batch_size_mismatch(self, device, dtype):
+ angle = torch.rand(1, device=device, dtype=dtype)
+ translation = torch.rand(2, 2, device=device, dtype=dtype)
with pytest.raises(RuntimeError):
- angle = torch.rand(1, device=device, dtype=dtype)
- translation = torch.rand(2, 2, device=device, dtype=dtype)
kornia.geometry.transform.Affine(angle, translation)
def test_affine_rotate(self, device, dtype):
diff --git a/tests/geometry/transform/test_elastic_transform.py b/tests/geometry/transform/test_elastic_transform.py
index 963641c82..a17559367 100644
--- a/tests/geometry/transform/test_elastic_transform.py
+++ b/tests/geometry/transform/test_elastic_transform.py
@@ -58,21 +58,21 @@ class TestElasticTransform(BaseTester):
elastic_transform2d(ex, 1)
assert "Type mismatch: expected Tensor" in str(errinfo.value)
+ img = torch.ones(1, 1, 1, device=device, dtype=dtype)
+ noise = torch.ones(1, 2, 1, 1, device=device, dtype=dtype)
with pytest.raises(ShapeError) as errinfo:
- img = torch.ones(1, 1, 1, device=device, dtype=dtype)
- noise = torch.ones(1, 2, 1, 1, device=device, dtype=dtype)
elastic_transform2d(img, noise)
assert "Shape dimension mismatch" in str(errinfo.value)
+ img = torch.ones(1, 1, 1, 1, device=device, dtype=dtype)
+ noise = torch.ones(2, 1, 1, device=device, dtype=dtype)
with pytest.raises(ShapeError) as errinfo:
- img = torch.ones(1, 1, 1, 1, device=device, dtype=dtype)
- noise = torch.ones(2, 1, 1, device=device, dtype=dtype)
elastic_transform2d(img, noise)
assert "Shape dimension mismatch" in str(errinfo.value)
+ img = torch.ones(1, 1, 1, 1, device=device, dtype=dtype)
+ noise = torch.ones(1, 3, 1, 1, device=device, dtype=dtype)
with pytest.raises(RuntimeError) as errinfo:
- img = torch.ones(1, 1, 1, 1, device=device, dtype=dtype)
- noise = torch.ones(1, 3, 1, 1, device=device, dtype=dtype)
elastic_transform2d(img, noise)
assert "The size of tensor a (2) must match the size of tensor b (3)" in str(errinfo.value)
diff --git a/tests/geometry/transform/test_imgwarp.py b/tests/geometry/transform/test_imgwarp.py
index 5c22cd8de..983ac28b6 100644
--- a/tests/geometry/transform/test_imgwarp.py
+++ b/tests/geometry/transform/test_imgwarp.py
@@ -519,12 +519,12 @@ class TestWarpAffine(BaseTester):
with pytest.raises(TypeError):
assert kornia.geometry.warp_affine(img, 0.0, size)
+ img = torch.rand(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- img = torch.rand(2, 3, 4, device=device, dtype=dtype)
assert kornia.geometry.warp_affine(img, aff, size)
+ aff = torch.eye(2, 2, device=device, dtype=dtype)[None]
with pytest.raises(ValueError):
- aff = torch.eye(2, 2, device=device, dtype=dtype)[None]
assert kornia.geometry.warp_affine(img, aff, size)
def test_translation(self, device, dtype):
@@ -689,12 +689,12 @@ class TestWarpPerspective(BaseTester):
with pytest.raises(TypeError):
assert kornia.geometry.warp_perspective(img, 0.0, size)
+ img = torch.rand(2, 3, 4, device=device, dtype=dtype)
with pytest.raises(ValueError):
- img = torch.rand(2, 3, 4, device=device, dtype=dtype)
assert kornia.geometry.warp_perspective(img, homo, size)
+ homo = torch.eye(2, 2, device=device, dtype=dtype)[None]
with pytest.raises(ValueError):
- homo = torch.eye(2, 2, device=device, dtype=dtype)[None]
assert kornia.geometry.warp_perspective(img, homo, size)
def test_translation(self, device, dtype):
diff --git a/tests/geometry/transform/test_thin_plate_spline.py b/tests/geometry/transform/test_thin_plate_spline.py
index 54c00dc99..4e1ae6245 100644
--- a/tests/geometry/transform/test_thin_plate_spline.py
+++ b/tests/geometry/transform/test_thin_plate_spline.py
@@ -61,12 +61,12 @@ class TestTransformParameters(BaseTester):
@pytest.mark.parametrize("batch_size", [1, 3])
def test_exception(self, batch_size, device, dtype):
+ src = torch.rand(batch_size, 5, 2).numpy()
with pytest.raises(TypeError):
- src = torch.rand(batch_size, 5, 2).numpy()
assert kornia.geometry.transform.get_tps_transform(src, src)
+ src = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- src = torch.rand(batch_size, 5)
assert kornia.geometry.transform.get_tps_transform(src, src)
def test_kernel_distance_values(self, device, dtype):
@@ -137,20 +137,20 @@ class TestWarpPoints(BaseTester):
with pytest.raises(TypeError):
assert kornia.geometry.transform.warp_points_tps(src, src, kernel, affine.numpy())
+ src_bad = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- src_bad = torch.rand(batch_size, 5)
assert kornia.geometry.transform.warp_points_tps(src_bad, src, kernel, affine)
+ src_bad = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- src_bad = torch.rand(batch_size, 5)
assert kornia.geometry.transform.warp_points_tps(src, src_bad, kernel, affine)
+ kernel_bad = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- kernel_bad = torch.rand(batch_size, 5)
assert kornia.geometry.transform.warp_points_tps(src, src, kernel_bad, affine)
+ affine_bad = torch.rand(batch_size, 3)
with pytest.raises(ValueError):
- affine_bad = torch.rand(batch_size, 3)
assert kornia.geometry.transform.warp_points_tps(src, src, kernel, affine_bad)
@pytest.mark.parametrize("batch_size", [1, 3])
@@ -225,20 +225,20 @@ class TestWarpImage(BaseTester):
with pytest.raises(TypeError):
assert kornia.geometry.transform.warp_image_tps(image, dst, kernel, affine.numpy())
+ image_bad = torch.rand(batch_size, 32, 32)
with pytest.raises(ValueError):
- image_bad = torch.rand(batch_size, 32, 32)
assert kornia.geometry.transform.warp_image_tps(image_bad, dst, kernel, affine)
+ dst_bad = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- dst_bad = torch.rand(batch_size, 5)
assert kornia.geometry.transform.warp_image_tps(image, dst_bad, kernel, affine)
+ kernel_bad = torch.rand(batch_size, 5)
with pytest.raises(ValueError):
- kernel_bad = torch.rand(batch_size, 5)
assert kornia.geometry.transform.warp_image_tps(image, dst, kernel_bad, affine)
+ affine_bad = torch.rand(batch_size, 3)
with pytest.raises(ValueError):
- affine_bad = torch.rand(batch_size, 3)
assert kornia.geometry.transform.warp_image_tps(image, dst, kernel, affine_bad)
@pytest.mark.parametrize("batch_size", [1, 3])