Skip to content

Commit 6789354

Browse files
committed
TEST: Add tests for __init__ and to_filename
1 parent 70502e4 commit 6789354

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

nibabel/tests/test_analyze.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,20 @@ def test_affine_44(self):
764764
with pytest.raises(ValueError):
765765
IC(data, np.diag([2, 3, 4]))
766766

767+
def test_dtype_init_arg(self):
768+
# data_dtype can be set by argument in absence of header
769+
img_klass = self.image_class
770+
arr = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
771+
aff = np.eye(4)
772+
for dtype in self.supported_np_types:
773+
img = img_klass(arr, aff, dtype=dtype)
774+
assert img.get_data_dtype() == dtype
775+
# It can also override the header dtype
776+
hdr = img.header
777+
for dtype in self.supported_np_types:
778+
img = img_klass(arr, aff, hdr, dtype=dtype)
779+
assert img.get_data_dtype() == dtype
780+
767781
def test_offset_to_zero(self):
768782
# Check offset is always set to zero when creating images
769783
img_klass = self.image_class
@@ -878,6 +892,21 @@ def test_no_finite_values(self):
878892
img_back = self.image_class.from_file_map(fm)
879893
assert_array_equal(img_back.dataobj, 0)
880894

895+
def test_dtype_to_filename_arg(self):
896+
# data_dtype can be set by argument in absence of header
897+
img_klass = self.image_class
898+
arr = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
899+
aff = np.eye(4)
900+
img = img_klass(arr, aff)
901+
fname = 'test' + img_klass.files_types[0][1]
902+
with InTemporaryDirectory():
903+
for dtype in self.supported_np_types:
904+
img.to_filename(fname, dtype=dtype)
905+
new_img = img_klass.from_filename(fname)
906+
assert new_img.get_data_dtype() == dtype
907+
# data_type is reset after write
908+
assert img.get_data_dtype() == np.int16
909+
881910

882911
def test_unsupported():
883912
# analyze does not support uint32

nibabel/tests/test_spm99analyze.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ class TestSpm99AnalyzeImage(test_analyze.TestAnalyzeImage, ImageScalingMixin):
414414
test_header_updating = needs_scipy(test_analyze.TestAnalyzeImage.test_header_updating)
415415
test_offset_to_zero = needs_scipy(test_analyze.TestAnalyzeImage.test_offset_to_zero)
416416
test_big_offset_exts = needs_scipy(test_analyze.TestAnalyzeImage.test_big_offset_exts)
417+
test_dtype_to_filename_arg = needs_scipy(test_analyze.TestAnalyzeImage.test_dtype_to_filename_arg)
417418
test_header_scaling = needs_scipy(ImageScalingMixin.test_header_scaling)
418419
test_int_int_scaling = needs_scipy(ImageScalingMixin.test_int_int_scaling)
419420
test_write_scaling = needs_scipy(ImageScalingMixin.test_write_scaling)

0 commit comments

Comments
 (0)