Skip to content

Commit fb3bcfc

Browse files
committed
TEST improve error catching
1 parent 8855837 commit fb3bcfc

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

nibabel/arrayops.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import numpy as np
44

55
from .orientations import aff2axcodes
6-
7-
8-
support_np_type = (
9-
np.int8,
10-
np.int64,
11-
np.float16,
12-
np.float32,
13-
np.float64,
14-
np.complex128)
6+
# support_np_type = (
7+
# np.int8,
8+
# np.int64,
9+
# np.float16,
10+
# np.float32,
11+
# np.float64,
12+
# np.complex128)
1513

1614

1715
class OperableImage:
@@ -47,7 +45,7 @@ def _unop(self, *, op):
4745
op :
4846
Python operator.
4947
"""
50-
_type_check(self)
48+
# _type_check(self)
5149
if op.__name__ in ["pos", "neg", "abs"]:
5250
dataobj = op(np.asanyarray(self.dataobj))
5351
return self.__class__(dataobj, self.affine, self.header)
@@ -85,18 +83,19 @@ def __abs__(self):
8583

8684
def _input_validation(self, val):
8785
"""Check images orientation, affine, and shape muti-images operation."""
88-
_type_check(self)
86+
# _type_check(self)
8987
if isinstance(val, self.__class__):
90-
_type_check(val)
88+
# _type_check(val)
9189
# Check orientations are the same
9290
if aff2axcodes(self.affine) != aff2axcodes(val.affine):
9391
raise ValueError("Two images should have the same orientation")
9492
# Check affine
95-
if (self.affine != val.affine).all():
93+
if (self.affine != val.affine).any():
9694
raise ValueError("Two images should have the same affine.")
95+
9796
# Check shape.
9897
if self.shape[:3] != val.shape[:3]:
99-
raise ValueError("Two images should have the same shape except"
98+
raise ValueError("Two images should have the same shape except "
10099
"the time dimension.")
101100

102101
# if 4th dim exist in a image,
@@ -122,10 +121,10 @@ def _input_validation(self, val):
122121
return self_, val_
123122

124123

125-
def _type_check(*args):
126-
"""Ensure image contains correct nifti data type."""
127-
# Check types
128-
dtypes = [img.get_data_dtype().type for img in args]
129-
# check allowed dtype based on the operator
130-
if set(support_np_type).union(dtypes) == 0:
131-
raise ValueError("Image contains illegal datatype for Nifti1Image.")
124+
# def _type_check(*args):
125+
# """Ensure image contains correct nifti data type."""
126+
# # Check types
127+
# dtypes = [img.get_data_dtype().type for img in args]
128+
# # check allowed dtype based on the operator
129+
# if set(support_np_type).union(dtypes) == 0:
130+
# raise ValueError("Image contains illegal datatype for Nifti1Image.")

nibabel/tests/test_arrayops.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,44 @@ def test_binary_operations_4d():
5151
img1 = Nifti1Image(data1, np.eye(4))
5252
img2 = Nifti1Image(data2, np.eye(4))
5353
data2_ = np.reshape(data2, (5, 5, 2, 1))
54+
5455
output = img1 * img2
5556
assert_array_equal(output.dataobj, data1 * data2_)
5657

5758

5859
def test_unary_operations():
59-
data = np.random.rand(5, 5, 2)
60+
data = np.random.rand(5, 5, 2) - 0.5
6061
img = Nifti1Image(data, np.eye(4))
6162

63+
output = +img
64+
assert_array_equal(output.dataobj, +data)
65+
6266
output = -img
6367
assert_array_equal(output.dataobj, -data)
6468

6569
output = abs(img)
6670
assert_array_equal(output.dataobj, abs(data))
71+
72+
73+
def test_error_catching():
74+
data1 = np.random.rand(5, 5, 1)
75+
data2 = np.random.rand(5, 5, 2)
76+
img1 = Nifti1Image(data1, np.eye(4))
77+
img2 = Nifti1Image(data2, np.eye(4))
78+
with pytest.raises(ValueError, match=r'should have the same shape'):
79+
img1 + img2
80+
81+
data1 = np.random.rand(5, 5, 2)
82+
data2 = np.random.rand(5, 5, 2)
83+
img1 = Nifti1Image(data1, np.eye(4) * 2)
84+
img2 = Nifti1Image(data2, np.eye(4))
85+
with pytest.raises(ValueError, match=r'should have the same affine'):
86+
img1 + img2
87+
88+
data = np.random.rand(5, 5, 2)
89+
aff1 = [[0,1,0,10],[-1,0,0,20],[0,0,1,30],[0,0,0,1]]
90+
aff2 = np.eye(4)
91+
img1 = Nifti1Image(data, aff1)
92+
img2 = Nifti1Image(data, aff2)
93+
with pytest.raises(ValueError, match=r'should have the same orientation'):
94+
img1 + img2

0 commit comments

Comments
 (0)