Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Optional raise in kornia check functions (#2375)

Alex Brooksaa3a78758737
diff --git a/kornia/core/check.py b/kornia/core/check.py
index b19746bae..728d51eb9 100644
--- a/kornia/core/check.py
+++ b/kornia/core/check.py
@@ -26,7 +26,7 @@ __all__ = [
# TODO: add somehow type check, or enforce to do it before
-def KORNIA_CHECK_SHAPE(x: Tensor, shape: list[str]) -> None:
+def KORNIA_CHECK_SHAPE(x: Tensor, shape: list[str], raises: bool = True) -> bool:
"""Check whether a tensor has a specified shape.
The shape can be specified with a implicit or explicit list of strings.
@@ -35,16 +35,19 @@ def KORNIA_CHECK_SHAPE(x: Tensor, shape: list[str]) -> None:
Args:
x: the tensor to evaluate.
shape: a list with strings with the expected shape.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- Exception: if the input tensor is has not the expected shape.
+ Exception: if the input tensor is has not the expected shape and raises is True.
Example:
>>> x = torch.rand(2, 3, 4, 4)
- >>> KORNIA_CHECK_SHAPE(x, ["B","C", "H", "W"]) # implicit
+ >>> KORNIA_CHECK_SHAPE(x, ["B", "C", "H", "W"]) # implicit
+ True
>>> x = torch.rand(2, 3, 4, 4)
- >>> KORNIA_CHECK_SHAPE(x, ["2","3", "H", "W"]) # explicit
+ >>> KORNIA_CHECK_SHAPE(x, ["2", "3", "H", "W"]) # explicit
+ True
"""
if '*' == shape[0]:
shape_to_check = shape[1:]
@@ -57,7 +60,10 @@ def KORNIA_CHECK_SHAPE(x: Tensor, shape: list[str]) -> None:
x_shape_to_check = x.shape
if len(x_shape_to_check) != len(shape_to_check):
- raise TypeError(f"{x} shape must be [{shape}]. Got {x.shape}")
+ if raises:
+ raise TypeError(f"{x} shape must be [{shape}]. Got {x.shape}")
+ else:
+ return False
for i in range(len(x_shape_to_check)):
# The voodoo below is because torchscript does not like
@@ -67,25 +73,34 @@ def KORNIA_CHECK_SHAPE(x: Tensor, shape: list[str]) -> None:
continue
dim = int(dim_)
if x_shape_to_check[i] != dim:
- raise TypeError(f"{x} shape must be [{shape}]. Got {x.shape}")
+ if raises:
+ raise TypeError(f"{x} shape must be [{shape}]. Got {x.shape}")
+ else:
+ return False
+ return True
-def KORNIA_CHECK(condition: bool, msg: str | None = None) -> None:
+def KORNIA_CHECK(condition: bool, msg: str | None = None, raises: bool = True) -> bool:
"""Check any arbitrary boolean condition.
Args:
condition: the condition to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- Exception: if the confition is met.
+ Exception: if the condition is met and raises is True.
Example:
>>> x = torch.rand(2, 3, 3)
>>> KORNIA_CHECK(x.shape[-2:] == (3, 3), "Invalid homography")
+ True
"""
if not condition:
- raise Exception(f"{condition} not true.\n{msg}")
+ if raises:
+ raise Exception(f"{condition} not true.\n{msg}")
+ return False
+ return True
def KORNIA_UNWRAP(maybe_obj: object, typ: Any) -> Any:
@@ -103,16 +118,17 @@ T = TypeVar('T', bound=type)
# TODO: fix mypy typeguard issue
-def KORNIA_CHECK_TYPE(x: object, typ: T | tuple[T, ...], msg: str | None = None) -> TypeGuard[T]:
+def KORNIA_CHECK_TYPE(x: object, typ: T | tuple[T, ...], msg: str | None = None, raises: bool = True) -> TypeGuard[T]:
"""Check the type of an aribratry variable.
Args:
x: any input variable.
typ: the expected type of the variable.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the input variable does not match with the expected.
+ TypeException: if the input variable does not match with the expected and raises is True.
Example:
>>> KORNIA_CHECK_TYPE("foo", str, "Invalid string")
@@ -120,20 +136,22 @@ def KORNIA_CHECK_TYPE(x: object, typ: T | tuple[T, ...], msg: str | None = None)
"""
# TODO: Move to use typeguard here dropping support for JIT
if not isinstance(x, typ):
- raise TypeError(f"Invalid type: {type(x)}.\n{msg}")
-
+ if raises:
+ raise TypeError(f"Invalid type: {type(x)}.\n{msg}")
+ return False
return True
-def KORNIA_CHECK_IS_TENSOR(x: object, msg: str | None = None) -> TypeGuard[Tensor]:
+def KORNIA_CHECK_IS_TENSOR(x: object, msg: str | None = None, raises: bool = True) -> TypeGuard[Tensor]:
"""Check the input variable is a Tensor.
Args:
x: any input variable.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the input variable does not match with the expected.
+ TypeException: if the input variable does not match with the expected and raises is True.
Example:
>>> x = torch.rand(2, 3, 3)
@@ -142,80 +160,101 @@ def KORNIA_CHECK_IS_TENSOR(x: object, msg: str | None = None) -> TypeGuard[Tenso
"""
# TODO: Move to use typeguard here dropping support for JIT
if not isinstance(x, Tensor):
- raise TypeError(f"Not a Tensor type. Got: {type(x)}.\n{msg}")
-
+ if raises:
+ raise TypeError(f"Not a Tensor type. Got: {type(x)}.\n{msg}")
+ return False
return True
-def KORNIA_CHECK_IS_LIST_OF_TENSOR(x: Sequence[object] | None) -> TypeGuard[list[Tensor]]:
+def KORNIA_CHECK_IS_LIST_OF_TENSOR(x: Sequence[object] | None, raises: bool = True) -> TypeGuard[list[Tensor]]:
"""Check the input variable is a List of Tensors.
Args:
x: Any sequence of objects
+ raises: bool indicating whether an exception should be raised upon failure.
+
+ Raises:
+ TypeException: if the input variable does not match with the expected and raises is True.
Return:
True if the input is a list of Tensors, otherwise return False.
Example:
>>> x = torch.rand(2, 3, 3)
- >>> KORNIA_CHECK_IS_LIST_OF_TENSOR(x)
+ >>> KORNIA_CHECK_IS_LIST_OF_TENSOR(x, raises=False)
False
>>> KORNIA_CHECK_IS_LIST_OF_TENSOR([x])
True
"""
- return isinstance(x, list) and all(isinstance(d, Tensor) for d in x)
+ are_tensors = isinstance(x, list) and all(isinstance(d, Tensor) for d in x)
+ if not are_tensors:
+ if raises:
+ raise TypeError(f"Provided container of type {type(x)} is not a list of tensors")
+ return False
+ return True
-def KORNIA_CHECK_SAME_DEVICE(x: Tensor, y: Tensor) -> None:
+def KORNIA_CHECK_SAME_DEVICE(x: Tensor, y: Tensor, raises: bool = True) -> bool:
"""Check whether two tensor in the same device.
Args:
x: first tensor to evaluate.
y: sencod tensor to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the two tensors are not in the same device.
+ TypeException: if the two tensors are not in the same device and raises is True.
Example:
>>> x1 = torch.rand(2, 3, 3)
>>> x2 = torch.rand(1, 3, 1)
>>> KORNIA_CHECK_SAME_DEVICE(x1, x2)
+ True
"""
if x.device != y.device:
- raise TypeError(f"Not same device for tensors. Got: {x.device} and {y.device}")
+ if raises:
+ raise TypeError(f"Not same device for tensors. Got: {x.device} and {y.device}")
+ return False
+ return True
-def KORNIA_CHECK_SAME_DEVICES(tensors: list[Tensor], msg: str | None = None) -> None:
+def KORNIA_CHECK_SAME_DEVICES(tensors: list[Tensor], msg: str | None = None, raises: bool = True) -> bool:
"""Check whether a list provided tensors live in the same device.
Args:
x: a list of tensors.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- Exception: if all the tensors are not in the same device.
+ Exception: if all the tensors are not in the same device and raises is True.
Example:
>>> x1 = torch.rand(2, 3, 3)
>>> x2 = torch.rand(1, 3, 1)
>>> KORNIA_CHECK_SAME_DEVICES([x1, x2], "Tensors not in the same device")
+ True
"""
- KORNIA_CHECK(isinstance(tensors, list) and len(tensors) >= 1, "Expected a list with at least one element")
+ KORNIA_CHECK(isinstance(tensors, list) and len(tensors) >= 1, "Expected a list with at least one element", raises)
if not all(tensors[0].device == x.device for x in tensors):
- raise Exception(f"Not same device for tensors. Got: {[x.device for x in tensors]}.\n{msg}")
+ if raises:
+ raise Exception(f"Not same device for tensors. Got: {[x.device for x in tensors]}.\n{msg}")
+ return False
+ return True
-def KORNIA_CHECK_SAME_SHAPE(x: Tensor, y: Tensor) -> bool:
+def KORNIA_CHECK_SAME_SHAPE(x: Tensor, y: Tensor, raises: bool = True) -> bool:
"""Check whether two tensor have the same shape.
Args:
x: first tensor to evaluate.
y: sencod tensor to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the two tensors have not the same shape.
+ TypeException: if the two tensors have not the same shape and raises is True.
Example:
>>> x1 = torch.rand(2, 3, 3)
@@ -224,20 +263,22 @@ def KORNIA_CHECK_SAME_SHAPE(x: Tensor, y: Tensor) -> bool:
True
"""
if x.shape != y.shape:
- raise TypeError(f"Not same shape for tensors. Got: {x.shape} and {y.shape}")
-
+ if raises:
+ raise TypeError(f"Not same shape for tensors. Got: {x.shape} and {y.shape}")
+ return False
return True
-def KORNIA_CHECK_IS_COLOR(x: Tensor, msg: str | None = None) -> bool:
+def KORNIA_CHECK_IS_COLOR(x: Tensor, msg: str | None = None, raises: bool = True) -> bool:
"""Check whether an image tensor is a color images.
Args:
x: image tensor to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if all the input tensor has not a shape :math:`(3,H,W)`.
+ TypeException: if all the input tensor has not a shape :math:`(3,H,W)` and raises is True.
Example:
>>> img = torch.rand(2, 3, 4, 4)
@@ -245,19 +286,22 @@ def KORNIA_CHECK_IS_COLOR(x: Tensor, msg: str | None = None) -> bool:
True
"""
if len(x.shape) < 3 or x.shape[-3] != 3:
- raise TypeError(f"Not a color tensor. Got: {type(x)}.\n{msg}")
+ if raises:
+ raise TypeError(f"Not a color tensor. Got: {type(x)}.\n{msg}")
+ return False
return True
-def KORNIA_CHECK_IS_GRAY(x: Tensor, msg: str | None = None) -> bool:
+def KORNIA_CHECK_IS_GRAY(x: Tensor, msg: str | None = None, raises: bool = True) -> bool:
"""Check whether an image tensor is grayscale.
Args:
x: image tensor to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the tensor has not a shape :math:`(1,H,W)` or :math:`(H,W)`.
+ TypeException: if the tensor has not a shape :math:`(1,H,W)` or :math:`(H,W)` and raises is True.
Example:
>>> img = torch.rand(2, 1, 4, 4)
@@ -265,19 +309,22 @@ def KORNIA_CHECK_IS_GRAY(x: Tensor, msg: str | None = None) -> bool:
True
"""
if len(x.shape) < 2 or (len(x.shape) >= 3 and x.shape[-3] != 1):
- raise TypeError(f"Not a gray tensor. Got: {type(x)}.\n{msg}")
+ if raises:
+ raise TypeError(f"Not a gray tensor. Got: {type(x)}.\n{msg}")
+ return False
return True
-def KORNIA_CHECK_IS_COLOR_OR_GRAY(x: Tensor, msg: str | None = None) -> bool:
+def KORNIA_CHECK_IS_COLOR_OR_GRAY(x: Tensor, msg: str | None = None, raises: bool = True) -> bool:
"""Check whether an image tensor is grayscale or color.
Args:
x: image tensor to evaluate.
msg: message to show in the exception.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the tensor has not a shape :math:`(1,H,W)` or :math:`(3,H,W)`.
+ TypeException: if the tensor has not a shape :math:`(1,H,W)` or :math:`(3,H,W)` and raises is True.
Example:
>>> img = torch.rand(2, 3, 4, 4)
@@ -285,20 +332,23 @@ def KORNIA_CHECK_IS_COLOR_OR_GRAY(x: Tensor, msg: str | None = None) -> bool:
True
"""
if len(x.shape) < 3 or x.shape[-3] not in [1, 3]:
- raise TypeError(f"Not a color or gray tensor. Got: {type(x)}.\n{msg}")
+ if raises:
+ raise TypeError(f"Not a color or gray tensor. Got: {type(x)}.\n{msg}")
+ return False
return True
-def KORNIA_CHECK_DM_DESC(desc1: Tensor, desc2: Tensor, dm: Tensor) -> bool:
+def KORNIA_CHECK_DM_DESC(desc1: Tensor, desc2: Tensor, dm: Tensor, raises: bool = True) -> bool:
"""Check whether the provided descriptors match with a distance matrix.
Args:
desc1: first descriptor tensor to evaluate.
desc2: second descriptor tensor to evaluate.
dm: distance matrix tensor to evaluate.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- TypeException: if the descriptors shape do not match with the distance matrix.
+ TypeException: if the descriptors shape do not match with the distance matrix and raises is True.
Example:
>>> desc1 = torch.rand(4)
@@ -308,24 +358,28 @@ def KORNIA_CHECK_DM_DESC(desc1: Tensor, desc2: Tensor, dm: Tensor) -> bool:
True
"""
if not ((dm.size(0) == desc1.size(0)) and (dm.size(1) == desc2.size(0))):
- raise TypeError(
- f"distance matrix shape {dm.shape} is not onsistent with descriptors shape: desc1 {desc1.shape} "
- f"desc2 {desc2.shape}"
- )
+ if raises:
+ raise TypeError(
+ f"distance matrix shape {dm.shape} is not onsistent with descriptors shape: desc1 {desc1.shape} "
+ f"desc2 {desc2.shape}"
+ )
+ return False
return True
-def KORNIA_CHECK_LAF(laf: Tensor) -> None:
+def KORNIA_CHECK_LAF(laf: Tensor, raises: bool = True) -> bool:
"""Check whether a Local Affine Frame (laf) has a valid shape.
Args:
laf: local affine frame tensor to evaluate.
+ raises: bool indicating whether an exception should be raised upon failure.
Raises:
- Exception: if the input laf does not have a shape :math:`(B,N,2,3)`.
+ Exception: if the input laf does not have a shape :math:`(B,N,2,3)` and raises is True.
Example:
>>> lafs = torch.rand(2, 10, 2, 3)
>>> KORNIA_CHECK_LAF(lafs)
+ True
"""
- KORNIA_CHECK_SHAPE(laf, ["B", "N", "2", "3"])
+ return KORNIA_CHECK_SHAPE(laf, ["B", "N", "2", "3"], raises)
diff --git a/test/core/test_check.py b/test/core/test_check.py
index 0d983cfd6..e96229e4a 100644
--- a/test/core/test_check.py
+++ b/test/core/test_check.py
@@ -20,12 +20,15 @@ from kornia.core.check import (
class TestCheck:
def test_valid(self):
- KORNIA_CHECK(True, "This is a test")
+ assert KORNIA_CHECK(True, "This is a test") is True
def test_invalid(self):
with pytest.raises(Exception):
KORNIA_CHECK(False, "This is a test")
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK(False, "This should not raise", raises=False) is False
+
class TestCheckShape:
@pytest.mark.parametrize(
@@ -38,7 +41,7 @@ class TestCheckShape:
],
)
def test_valid(self, data, shape):
- KORNIA_CHECK_SHAPE(data, shape)
+ assert KORNIA_CHECK_SHAPE(data, shape) is True
@pytest.mark.parametrize(
"data,shape",
@@ -53,12 +56,15 @@ class TestCheckShape:
with pytest.raises(Exception):
KORNIA_CHECK_SHAPE(data, shape)
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["1", "H", "W"], raises=False) is False
+
class TestCheckSameShape:
def test_valid(self):
- KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 3))
- KORNIA_CHECK_SAME_SHAPE(torch.rand(1, 2, 3), torch.rand(1, 2, 3))
- KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3, 3), torch.rand(2, 3, 3))
+ assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 3)) is True
+ assert KORNIA_CHECK_SAME_SHAPE(torch.rand(1, 2, 3), torch.rand(1, 2, 3)) is True
+ assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3, 3), torch.rand(2, 3, 3)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -68,13 +74,16 @@ class TestCheckSameShape:
with pytest.raises(Exception):
KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 3, 3))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 2, 3), raises=False) is False
+
class TestCheckType:
def test_valid(self):
- KORNIA_CHECK_TYPE("hello", str)
- KORNIA_CHECK_TYPE(23, int)
- KORNIA_CHECK_TYPE(torch.rand(1), torch.Tensor)
- KORNIA_CHECK_TYPE(torch.rand(1), (int, torch.Tensor))
+ assert KORNIA_CHECK_TYPE("hello", str) is True
+ assert KORNIA_CHECK_TYPE(23, int) is True
+ assert KORNIA_CHECK_TYPE(torch.rand(1), torch.Tensor) is True
+ assert KORNIA_CHECK_TYPE(torch.rand(1), (int, torch.Tensor)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -84,52 +93,74 @@ class TestCheckType:
with pytest.raises(Exception):
KORNIA_CHECK_TYPE(23, (float, str))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_TYPE("world", int, raises=False) is False
+
class TestCheckIsTensor:
def test_valid(self):
- KORNIA_CHECK_IS_TENSOR(torch.rand(1))
+ assert KORNIA_CHECK_IS_TENSOR(torch.rand(1)) is True
def test_invalid(self):
with pytest.raises(Exception):
KORNIA_CHECK_IS_TENSOR([1, 2, 3])
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_IS_TENSOR([1, 2, 3], raises=False) is False
+
class TestCheckIsListOfTensor:
def test_valid(self):
- assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), torch.rand(1), torch.rand(1)])
+ assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), torch.rand(1), torch.rand(1)]) is True
def test_invalid(self):
with pytest.raises(Exception):
- assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), [2, 3], torch.rand(1)])
+ KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), [2, 3], torch.rand(1)])
with pytest.raises(Exception):
- assert KORNIA_CHECK_IS_LIST_OF_TENSOR([1, 2, 3])
+ KORNIA_CHECK_IS_LIST_OF_TENSOR([1, 2, 3])
+
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), [2, 3], torch.rand(1)], raises=False) is False
class TestCheckSameDevice:
def test_valid(self, device):
- KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device=device), torch.rand(1, device=device))
+ assert KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device=device), torch.rand(1, device=device)) is True
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
def test_invalid(self):
with pytest.raises(Exception):
KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device="cpu"), torch.rand(1, device="cuda"))
+ @pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
+ def test_invalid_raises_false(self):
+ assert (
+ KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device="cpu"), torch.rand(1, device="cuda", raises=False)) is False
+ )
+
class TestCheckSameDevices:
def test_valid(self, device):
- KORNIA_CHECK_SAME_DEVICES([torch.rand(1, device=device), torch.rand(1, device=device)])
+ assert KORNIA_CHECK_SAME_DEVICES([torch.rand(1, device=device), torch.rand(1, device=device)]) is True
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
def test_invalid(self):
with pytest.raises(Exception):
KORNIA_CHECK_SAME_DEVICES([torch.rand(1, device="cpu"), torch.rand(1, device="cuda")])
+ @pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
+ def test_invalid_raises_false(self):
+ assert (
+ KORNIA_CHECK_SAME_DEVICES([torch.rand(1, device="cpu"), torch.rand(1, device="cuda")], raises=False)
+ is False
+ )
+
class TestCheckIsColor:
def test_valid(self):
- KORNIA_CHECK_IS_COLOR(torch.rand(3, 4, 4))
- KORNIA_CHECK_IS_COLOR(torch.rand(1, 3, 4, 4))
- KORNIA_CHECK_IS_COLOR(torch.rand(2, 3, 4, 4))
+ assert KORNIA_CHECK_IS_COLOR(torch.rand(3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR(torch.rand(1, 3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR(torch.rand(2, 3, 4, 4)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -141,12 +172,15 @@ class TestCheckIsColor:
with pytest.raises(Exception):
KORNIA_CHECK_IS_COLOR(torch.rand(1, 3, 4, 4, 4))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_IS_COLOR(torch.rand(1, 4, 4), raises=False) is False
+
class TestCheckIsGray:
def test_valid(self):
- KORNIA_CHECK_IS_GRAY(torch.rand(1, 4, 4))
- KORNIA_CHECK_IS_GRAY(torch.rand(2, 1, 4, 4))
- KORNIA_CHECK_IS_GRAY(torch.rand(3, 1, 4, 4))
+ assert KORNIA_CHECK_IS_GRAY(torch.rand(1, 4, 4)) is True
+ assert KORNIA_CHECK_IS_GRAY(torch.rand(2, 1, 4, 4)) is True
+ assert KORNIA_CHECK_IS_GRAY(torch.rand(3, 1, 4, 4)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -158,15 +192,18 @@ class TestCheckIsGray:
with pytest.raises(Exception):
KORNIA_CHECK_IS_GRAY(torch.rand(1, 3, 4, 4, 4))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_IS_GRAY(torch.rand(1, 3, 4, 4, 4), raises=False) is False
+
class TestCheckIsColorOrGray:
def test_valid(self):
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 4, 4))
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 3, 4, 4))
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 3, 4, 4))
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4))
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 1, 4, 4))
- KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 1, 4, 4))
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 1, 4, 4)) is True
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 1, 4, 4)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -174,10 +211,13 @@ class TestCheckIsColorOrGray:
with pytest.raises(Exception):
KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 3, 4, 4, 4))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4, 4), raises=False) is False
+
class TestCheckDmDesc:
def test_valid(self):
- KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 8))
+ assert KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 8)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -189,10 +229,13 @@ class TestCheckDmDesc:
with pytest.raises(Exception):
KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 3, 8))
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 7), raises=False) is False
+
class TestCheckLaf:
def test_valid(self):
- KORNIA_CHECK_LAF(torch.rand(4, 2, 2, 3))
+ assert KORNIA_CHECK_LAF(torch.rand(4, 2, 2, 3)) is True
def test_invalid(self):
with pytest.raises(Exception):
@@ -203,3 +246,6 @@ class TestCheckLaf:
KORNIA_CHECK_LAF(torch.rand(4, 2, 2, 2))
with pytest.raises(Exception):
KORNIA_CHECK_LAF(torch.rand(4, 2, 3, 3, 3))
+
+ def test_invalid_raises_false(self):
+ assert KORNIA_CHECK_LAF(torch.rand(4, 2, 2), raises=False) is False