MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / test_write_2d

Method test_write_2d

tests/data/test_nifti_rw.py:151–173  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

149 os.remove(test_image)
150
151 def test_write_2d(self):
152 with tempfile.TemporaryDirectory() as out_dir:
153 image_name = os.path.join(out_dir, "test.nii.gz")
154 for p in TEST_NDARRAYS:
155 img = p(np.arange(6).reshape((2, 3)))
156 writer_obj = NibabelWriter()
157 writer_obj.set_data_array(img, channel_dim=None)
158 writer_obj.set_metadata({"affine": np.diag([1, 1, 1]), "original_affine": np.diag([1.4, 1, 1])})
159 writer_obj.write(image_name, verbose=True)
160 out = nib.load(image_name)
161 np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [3.0, 4, 5]], atol=1e-4, rtol=1e-4)
162 np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]), atol=1e-4, rtol=1e-4)
163
164 image_name = os.path.join(out_dir, "test1.nii.gz")
165 img = np.arange(5).reshape((1, 5))
166 writer_obj.set_data_array(img, channel_dim=None)
167 writer_obj.set_metadata(
168 {"affine": np.diag([1, 1, 1, 3, 3]), "original_affine": np.diag([1.4, 2.0, 1, 3, 5])}
169 )
170 writer_obj.write(image_name, verbose=True)
171 out = nib.load(image_name)
172 np.testing.assert_allclose(out.get_fdata(), [[0, 2, 4]], atol=1e-4, rtol=1e-4)
173 np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 1, 1]), atol=1e-4, rtol=1e-4)
174
175 def test_write_3d(self):
176 with tempfile.TemporaryDirectory() as out_dir:

Callers

nothing calls this directly

Calls 5

set_data_arrayMethod · 0.95
set_metadataMethod · 0.95
writeMethod · 0.95
NibabelWriterClass · 0.90
loadMethod · 0.80

Tested by

no test coverage detected