Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): say "Got" instead of "Gotcha", and name a bad behaviour as such (#5411)

Dipak Chaudhari649191b40b9c
diff --git a/changelog.d/5411.fixed.md b/changelog.d/5411.fixed.md
new file mode 100644
index 000000000..d8d6fa95d
--- /dev/null
+++ b/changelog.d/5411.fixed.md
@@ -0,0 +1,3 @@
+Error messages that read "Gotcha <value>" now read "Got <value>", across `kornia.filters`, `kornia.augmentation`,
+`kornia.feature`, `kornia.enhance` and `kornia.constants`, and `filter2d` names a bad `behaviour` as an invalid
+behaviour mode instead of an invalid padding mode. (#5411)
diff --git a/kornia/augmentation/_2d/geometric/resize.py b/kornia/augmentation/_2d/geometric/resize.py
index 716b887d4..b59ac3662 100644
--- a/kornia/augmentation/_2d/geometric/resize.py
+++ b/kornia/augmentation/_2d/geometric/resize.py
@@ -139,7 +139,7 @@ class Resize(GeometricAugmentationBase2D):
size: Optional[Tuple[int, int]] = None,
) -> torch.Tensor:
if not isinstance(size, tuple):
- raise TypeError(f"Expected the size be a tuple. Gotcha {type(size)}")
+ raise TypeError(f"Expected the size be a tuple. Got {type(size)}")
if not isinstance(transform, torch.Tensor):
raise TypeError(f"Expected the `transform` be a torch.Tensor. Got {type(transform)}.")
diff --git a/kornia/augmentation/_2d/geometric/resized_crop.py b/kornia/augmentation/_2d/geometric/resized_crop.py
index 28b1ec054..42a43a4b4 100644
--- a/kornia/augmentation/_2d/geometric/resized_crop.py
+++ b/kornia/augmentation/_2d/geometric/resized_crop.py
@@ -201,7 +201,7 @@ class RandomResizedCrop(GeometricAugmentationBase2D):
f"`inverse` is only applicable for resample cropping mode. Got {flags['cropping_mode']}."
)
if not isinstance(size, tuple):
- raise TypeError(f"Expected the size be a tuple. Gotcha {type(size)}")
+ raise TypeError(f"Expected the size be a tuple. Got {type(size)}")
if not isinstance(transform, torch.Tensor):
raise TypeError(f"Expected the `transform` be a torch.Tensor. Got {type(transform)}.")
diff --git a/kornia/augmentation/_2d/intensity/planckian_jitter.py b/kornia/augmentation/_2d/intensity/planckian_jitter.py
index f68dba754..6308ae8f6 100644
--- a/kornia/augmentation/_2d/intensity/planckian_jitter.py
+++ b/kornia/augmentation/_2d/intensity/planckian_jitter.py
@@ -85,7 +85,7 @@ def get_planckian_coeffs(mode: str) -> torch.Tensor:
]
)
else:
- raise RuntimeError(f"Unexpected mode. Gotcha {mode}")
+ raise RuntimeError(f"Unexpected mode. Got {mode}")
return torch.stack((coefs[:, 0] / coefs[:, 1], coefs[:, 2] / coefs[:, 1]), 1)
diff --git a/kornia/augmentation/_2d/mix/mosaic.py b/kornia/augmentation/_2d/mix/mosaic.py
index 2db8ac00e..2d1c306c5 100644
--- a/kornia/augmentation/_2d/mix/mosaic.py
+++ b/kornia/augmentation/_2d/mix/mosaic.py
@@ -240,7 +240,7 @@ class RandomMosaic(MixAugmentationBaseV2):
flags = self.flags if flags is None else flags
if flags["cropping_mode"] == "resample": # uses bilinear interpolation to crop
if not isinstance(transform, torch.Tensor):
- raise TypeError(f"Expected the transform to be a torch.Tensor. Gotcha {type(transform)}")
+ raise TypeError(f"Expected the transform to be a torch.Tensor. Got {type(transform)}")
# Fit the arg to F.F.pad
if flags["padding_mode"] == "constant":
diff --git a/kornia/augmentation/_3d/geometric/affine.py b/kornia/augmentation/_3d/geometric/affine.py
index 1cb6b7a8c..a7e0fc17b 100644
--- a/kornia/augmentation/_3d/geometric/affine.py
+++ b/kornia/augmentation/_3d/geometric/affine.py
@@ -188,7 +188,7 @@ class RandomAffine3D(GeometricAugmentationBase3D):
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
if not isinstance(transform, torch.Tensor):
- raise TypeError(f"Expected the transform to be a torch.Tensor. Gotcha {type(transform)}")
+ raise TypeError(f"Expected the transform to be a torch.Tensor. Got {type(transform)}")
return warp_affine3d(
input,
diff --git a/kornia/augmentation/_3d/geometric/crop.py b/kornia/augmentation/_3d/geometric/crop.py
index 008e78d91..f2734f35b 100644
--- a/kornia/augmentation/_3d/geometric/crop.py
+++ b/kornia/augmentation/_3d/geometric/crop.py
@@ -193,7 +193,7 @@ class RandomCrop3D(GeometricAugmentationBase3D):
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
if not isinstance(transform, torch.Tensor):
- raise TypeError(f"Expected the transform to be a torch.Tensor. Gotcha {type(transform)}")
+ raise TypeError(f"Expected the transform to be a torch.Tensor. Got {type(transform)}")
# Resampling starts from the padded volume; the public matrix starts from the original input.
transform = transform.clone()
diff --git a/kornia/augmentation/_3d/geometric/perspective.py b/kornia/augmentation/_3d/geometric/perspective.py
index 5c4692d9e..15c7f46aa 100644
--- a/kornia/augmentation/_3d/geometric/perspective.py
+++ b/kornia/augmentation/_3d/geometric/perspective.py
@@ -137,7 +137,7 @@ class RandomPerspective3D(GeometricAugmentationBase3D):
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
if not isinstance(transform, torch.Tensor):
- raise TypeError(f"Expected the transform to be a torch.Tensor. Gotcha {type(transform)}")
+ raise TypeError(f"Expected the transform to be a torch.Tensor. Got {type(transform)}")
return warp_perspective3d(
input,
diff --git a/kornia/augmentation/_3d/geometric/rotation.py b/kornia/augmentation/_3d/geometric/rotation.py
index 1d2412fe4..898c21225 100644
--- a/kornia/augmentation/_3d/geometric/rotation.py
+++ b/kornia/augmentation/_3d/geometric/rotation.py
@@ -147,6 +147,6 @@ class RandomRotation3D(GeometricAugmentationBase3D):
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
if not isinstance(transform, torch.Tensor):
- raise TypeError(f"Expected the transform to be a torch.Tensor. Gotcha {type(transform)}")
+ raise TypeError(f"Expected the transform to be a torch.Tensor. Got {type(transform)}")
return affine3d(input, transform[..., :3, :4], flags["resample"].name.lower(), "zeros", flags["align_corners"])
diff --git a/kornia/augmentation/container/image.py b/kornia/augmentation/container/image.py
index f7213137a..d646e6719 100644
--- a/kornia/augmentation/container/image.py
+++ b/kornia/augmentation/container/image.py
@@ -207,7 +207,7 @@ class ImageSequential(ImageSequentialBase, ImageModuleForSequentialMixIn):
if isinstance(self.random_apply, tuple):
num_samples = int(torch.randint(*self.random_apply, (1,)).item())
else:
- raise TypeError(f"random apply should be a tuple. Gotcha {type(self.random_apply)}")
+ raise TypeError(f"random apply should be a tuple. Got {type(self.random_apply)}")
multinomial_weights = self.random_apply_weights.clone()
# Mix augmentation can only be applied once per forward
diff --git a/kornia/augmentation/container/ops.py b/kornia/augmentation/container/ops.py
index 0cc81b4b6..cc11bc10a 100644
--- a/kornia/augmentation/container/ops.py
+++ b/kornia/augmentation/container/ops.py
@@ -57,7 +57,7 @@ class SequentialOpsInterface(Generic[T], metaclass=ABCMeta):
if isinstance(param, ParamItem) and isinstance(param.data, dict):
_params = param.data
else:
- raise TypeError(f"Expected param (ParamItem.data) be a dictionary. Gotcha {param}.")
+ raise TypeError(f"Expected param (ParamItem.data) be a dictionary. Got {param}.")
return _params
@classmethod
@@ -76,7 +76,7 @@ class SequentialOpsInterface(Generic[T], metaclass=ABCMeta):
if isinstance(param, ParamItem) and isinstance(param.data, list):
_params = param.data
else:
- raise TypeError(f"Expected param (ParamItem.data) be a list. Gotcha {param}.")
+ raise TypeError(f"Expected param (ParamItem.data) be a list. Got {param}.")
return _params
@classmethod
diff --git a/kornia/constants.py b/kornia/constants.py
index 6348b4af1..13d114f55 100644
--- a/kornia/constants.py
+++ b/kornia/constants.py
@@ -59,7 +59,7 @@ def _get(cls: Type[_T], value: TKEnum[_T]) -> _T:
return value
raise TypeError(
- f"The `.get` method from `{cls}` expects a value with type `str`, `int` or `{cls}`. Gotcha {type(value)}"
+ f"The `.get` method from `{cls}` expects a value with type `str`, `int` or `{cls}`. Got {type(value)}"
)
diff --git a/kornia/enhance/zca.py b/kornia/enhance/zca.py
index b9b08935f..bc448c4cf 100644
--- a/kornia/enhance/zca.py
+++ b/kornia/enhance/zca.py
@@ -218,7 +218,7 @@ class ZCAWhitening(nn.Module):
raise RuntimeError("Did not compute inverse ZCA. Please set compute_inv to True")
if self.transform_inv is None:
- raise TypeError("The transform inverse should be a torch.Tensor. Gotcha None.")
+ raise TypeError("The transform inverse should be a torch.Tensor. Got None.")
mean_inv: torch.Tensor = -self.mean_vector.mm(self.transform_matrix)
diff --git a/kornia/feature/loftr/loftr.py b/kornia/feature/loftr/loftr.py
index b5b807564..aa69f8662 100644
--- a/kornia/feature/loftr/loftr.py
+++ b/kornia/feature/loftr/loftr.py
@@ -212,7 +212,7 @@ class LoFTR(nn.Module):
if isinstance(_d, torch.Tensor):
out[v] = _d
else:
- raise TypeError(f"Expected torch.Tensor for item `{k}`. Gotcha {type(_d)}")
+ raise TypeError(f"Expected torch.Tensor for item `{k}`. Got {type(_d)}")
return out
def load_state_dict(self, state_dict: dict[str, Any], *args: Any, **kwargs: Any) -> Any: # type: ignore[override]
diff --git a/kornia/feature/scale_space_detector.py b/kornia/feature/scale_space_detector.py
index 83251d33a..40495ac5e 100644
--- a/kornia/feature/scale_space_detector.py
+++ b/kornia/feature/scale_space_detector.py
@@ -581,7 +581,7 @@ class ScaleSpaceDetector(nn.Module):
else:
raise TypeError(
"Expected the scale pyramid module to have `n_levels` as a torch.Tensor or int."
- f"Gotcha {type(self.scale_pyr.n_levels)}"
+ f"Got {type(self.scale_pyr.n_levels)}"
)
rotmat = torch.eye(2, dtype=dtype, device=dev).view(1, 1, 2, 2)
# Read the live module once per forward, so a refiner swapped in after construction, or a
diff --git a/kornia/feature/sold2/sold2_detector.py b/kornia/feature/sold2/sold2_detector.py
index 616d57014..77d0fdda8 100644
--- a/kornia/feature/sold2/sold2_detector.py
+++ b/kornia/feature/sold2/sold2_detector.py
@@ -412,7 +412,7 @@ class LineSegmentDetectionModule:
if not isinstance(self.junction_refine_cfg, JunctionRefineCfg):
raise TypeError(
"Expected to have dataclass of type JunctionRefineCfg for junction."
- f"Gotcha {type(self.junction_refine_cfg)}"
+ f"Got {type(self.junction_refine_cfg)}"
)
num_perturbs = self.junction_refine_cfg.num_perturbs
perturb_interval = self.junction_refine_cfg.perturb_interval
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index 6b20a8bc9..ba0604dd4 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -154,7 +154,7 @@ def filter2d(
)
KORNIA_CHECK(
str(behaviour).lower() in _VALID_BEHAVIOUR,
- f"Invalid padding mode, {behaviour}. Expected one of {_VALID_BEHAVIOUR}",
+ f"Invalid behaviour mode, {behaviour}. Expected one of {_VALID_BEHAVIOUR}",
)
# the checks are case-insensitive, so dispatch on the lower-case spelling as well
border_type, padding, behaviour = str(border_type).lower(), str(padding).lower(), str(behaviour).lower()
@@ -336,12 +336,12 @@ def filter3d(
KORNIA_CHECK(
str(border_type).lower() in _VALID_BORDERS,
- f"Invalid border, gotcha {border_type}. Expected one of {_VALID_BORDERS}",
+ f"Invalid border, got {border_type}. Expected one of {_VALID_BORDERS}",
)
KORNIA_CHECK(
str(behaviour).lower() in _VALID_BEHAVIOUR,
- f"Invalid behaviour mode, gotcha {behaviour}. Expected one of {_VALID_BEHAVIOUR}",
+ f"Invalid behaviour mode, got {behaviour}. Expected one of {_VALID_BEHAVIOUR}",
)
# the checks are case-insensitive, so dispatch on the lower-case spelling as well
border_type, behaviour = str(border_type).lower(), str(behaviour).lower()
diff --git a/kornia/filters/kernels.py b/kornia/filters/kernels.py
index fab618203..151eab78b 100644
--- a/kornia/filters/kernels.py
+++ b/kornia/filters/kernels.py
@@ -33,7 +33,7 @@ def _check_kernel_size(kernel_size: tuple[int, ...] | int, min_value: int = 0, a
for size in kernel_size:
KORNIA_CHECK(
isinstance(size, int) and (((size % 2 == 1) or allow_even) and size > min_value),
- f"Kernel size must be an {fmt} integer bigger than {min_value}. Gotcha {size} on {kernel_size}",
+ f"Kernel size must be an {fmt} integer bigger than {min_value}. Got {size} on {kernel_size}",
)
diff --git a/kornia/filters/kernels_geometry.py b/kornia/filters/kernels_geometry.py
index 3b217882f..e49ea986d 100644
--- a/kornia/filters/kernels_geometry.py
+++ b/kornia/filters/kernels_geometry.py
@@ -116,7 +116,7 @@ def get_motion_kernel2d(
kernel = F.pad(k[:, None], [0, 0, kernel_size // 2, kernel_size // 2, 0, 0])
expected_shape = torch.Size([direction.size(0), *kernel_tuple])
- KORNIA_CHECK(kernel.shape == expected_shape, f"Kernel shape should be {expected_shape}. Gotcha {kernel.shape}")
+ KORNIA_CHECK(kernel.shape == expected_shape, f"Kernel shape should be {expected_shape}. Got {kernel.shape}")
kernel = kernel[:, None, ...]
# rotate (counterclockwise) kernel by given angle
@@ -228,7 +228,7 @@ def get_motion_kernel3d(
)
expected_shape = torch.Size([direction.size(0), *kernel_tuple])
- KORNIA_CHECK(kernel.shape == expected_shape, f"Kernel shape should be {expected_shape}. Gotcha {kernel.shape}")
+ KORNIA_CHECK(kernel.shape == expected_shape, f"Kernel shape should be {expected_shape}. Got {kernel.shape}")
kernel = kernel[:, None, ...]
# rotate (counterclockwise) kernel by given angle
diff --git a/tests/filters/test_filters.py b/tests/filters/test_filters.py
index aa779801b..79795b2b5 100644
--- a/tests/filters/test_filters.py
+++ b/tests/filters/test_filters.py
@@ -167,6 +167,11 @@ class TestFilter2D(BaseTester):
filter2d(data, k, padding="a")
assert "Invalid padding mode, a. Ex" in str(errinfo)
+ # a bad behaviour is named as such, not as a bad padding mode (#5157)
+ with pytest.raises(Exception) as errinfo:
+ filter2d(data, k, behaviour="a")
+ assert "Invalid behaviour mode, a. Ex" in str(errinfo)
+
@pytest.mark.parametrize("padding", ["same", "valid"])
def test_mean_filter(self, padding, device, dtype):
kernel = torch.ones(1, 3, 3, device=device, dtype=dtype)
@@ -497,7 +502,11 @@ class TestFilter3D(BaseTester):
with pytest.raises(Exception) as errinfo:
filter3d(data, k, border_type="a")
- assert "Invalid border, gotcha a. Ex" in str(errinfo)
+ assert "Invalid border, got a. Ex" in str(errinfo)
+
+ with pytest.raises(Exception) as errinfo:
+ filter3d(data, k, behaviour="a")
+ assert "Invalid behaviour mode, got a. Ex" in str(errinfo)
def test_mean_filter(self, device, dtype):
kernel = torch.ones(1, 3, 3, 3, device=device, dtype=dtype)
@@ -2339,6 +2348,21 @@ class TestConventionsKernels(BaseTester):
self.assert_close(actual, expected)
assert get_gaussian_kernel2d((5, 5), (1, 1), device=device, dtype=dtype).shape == (1, 5, 5)
+ @pytest.mark.parametrize(
+ ("build", "message"),
+ [
+ (lambda: get_gaussian_kernel1d(4, 1.0), "Got 4 on (4,)"),
+ (lambda: get_motion_kernel2d(1, 0.0), "Got 1 on (1,)"),
+ ],
+ ids=["kernel_size", "motion_kernel_size"],
+ )
+ def test_a_bad_kernel_size_message_reads_got(self, build, message):
+ """The size checks said "Gotcha" for "Got" (#5157)."""
+ with pytest.raises(BaseError) as errinfo:
+ build()
+ assert message in str(errinfo.value)
+ assert "Gotcha" not in str(errinfo.value)
+
@pytest.mark.parametrize("case", ["box_int32", "gaussian_uint8", "laplacian_uint8", "gradient3d_int32"])
def test_wart_kernel_builders_truncate_or_wrap_in_an_integer_dtype_5155(self, case, device):
"""An integer dtype truncates fractional taps to 0, and uint8 wraps the negative ones (#5155)."""