Skip to content

Commit eca94e5

Browse files
author
Ben Cipollini
committed
Simplify is_image test.
1 parent e81ac28 commit eca94e5

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

nibabel/tests/test_image_types.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ def test_sniff_and_guessed_image_type(img_klasses=all_image_classes):
9393
For each, we expect:
9494
* When the file matches the expected class, things should
9595
either work, or fail if we're doing bad stuff.
96-
* When the file is a mismatch, it should either
97-
* Fail to be loaded if the image type is unrelated to the expected class
98-
* Load or fail in a consistent manner, if there is a relationship between
99-
the image class and expected image class.
96+
* When the file is a mismatch, the functions should not throw.
10097
"""
10198

10299
def test_image_class(img_path, expected_img_klass):
@@ -116,24 +113,19 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success, msg):
116113
is_img, new_sniff = img_klass.is_image(img_path, sniff)
117114

118115
if expect_success:
116+
# Check that the sniff returned is appropriate.
119117
new_msg = '%s returned sniff==None (%s)' % (img_klass.__name__, msg)
120118
expected_sizeof_hdr = getattr(img_klass.header_class, 'sizeof_hdr', 0)
121119
current_sizeof_hdr = len(new_sniff) if new_sniff is not None else 0
122120
assert_true(current_sizeof_hdr >= expected_sizeof_hdr, new_msg)
123121

124-
# Build a message to the user.
125-
new_msg = '%s (%s) image is%s a %s image.' % (
126-
basename(img_path),
127-
msg,
128-
'' if is_img else ' not',
129-
img_klass.__name__)
130-
131-
if expect_success is None:
132-
assert_true(True, new_msg) # No expectation, pass if no Exception
133-
# elif is_img != expect_success:
134-
# print('Failed! %s' % new_msg)
135-
else:
136-
assert_equal(is_img, expect_success, new_msg)
122+
# Check that the image type was recognized.
123+
new_msg = '%s (%s) image is%s a %s image.' % (
124+
basename(img_path),
125+
msg,
126+
'' if is_img else ' not',
127+
img_klass.__name__)
128+
assert_true(is_img, new_msg)
137129

138130
if sniff_mode == 'vanilla':
139131
return new_sniff
@@ -152,23 +144,20 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success, msg):
152144

153145
for klass in img_klasses:
154146
if klass == expected_img_klass:
155-
expect_success = (sniff_mode not in ['bad_sniff'] or
147+
# Class will load unless you pass a bad sniff,
148+
# the header actually uses the sniff, and the
149+
# sniff check is actually something meaningful
150+
# (we're looking at you, Minc1Header...)
151+
expect_success = (sniff_mode != 'bad_sniff' or
156152
sizeof_hdr == 0 or
157153
klass == Minc1Image) # special case...
158-
elif (issubclass(klass, expected_img_klass) or
159-
issubclass(expected_img_klass, klass)):
160-
expect_success = None # Images are related; can't be sure.
161154
else:
162-
# Usually, if the two images are unrelated, they
163-
# won't be able to be loaded. But here's a
164-
# list of manually confirmed special cases
165-
expect_success = ((expected_img_klass == Nifti1Pair and klass == Spm99AnalyzeImage) or
166-
(expected_img_klass == Nifti2Pair and klass == Spm99AnalyzeImage))
167-
168-
msg = '%s / %s / %s' % (expected_img_klass.__name__, sniff_mode, str(expect_success))
169-
print(msg)
155+
expect_success = False # Not sure the relationships
156+
170157
# Reuse the sniff... but it will only change for some
171158
# sniff_mode values.
159+
msg = '%s/ %s/ %s' % (expected_img_klass.__name__, sniff_mode,
160+
str(expect_success))
172161
sniff = check_img(img_path, klass, sniff_mode=sniff_mode,
173162
sniff=sniff, expect_success=expect_success,
174163
msg=msg)
@@ -182,7 +171,7 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success, msg):
182171
('small.mnc', Minc2Image),
183172
('test.mgz', MGHImage),
184173
('analyze.hdr', Spm2AnalyzeImage)]:
185-
print('Testing: %s %s' % (img_filename, image_klass.__name__))
174+
# print('Testing: %s %s' % (img_filename, image_klass.__name__))
186175
test_image_class(pjoin(DATA_PATH, img_filename), image_klass)
187176

188177

0 commit comments

Comments
 (0)