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

Method test_write_4d

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

Source from the content-addressed store, hash-verified

197 np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]), atol=1e-4, rtol=1e-4)
198
199 def test_write_4d(self):
200 with tempfile.TemporaryDirectory() as out_dir:
201 image_name = os.path.join(out_dir, "test.nii.gz")
202 for p in TEST_NDARRAYS:
203 img = p(np.arange(6).reshape((1, 1, 3, 2)))
204 writer_obj = NibabelWriter()
205 writer_obj.set_data_array(img, channel_dim=-1)
206 writer_obj.set_metadata({"affine": np.diag([1.4, 1, 1, 1]), "original_affine": np.diag([1, 1.4, 1, 1])})
207 writer_obj.write(image_name, verbose=True)
208 out = nib.load(image_name)
209 np.testing.assert_allclose(out.get_fdata(), [[[[0, 1], [2, 3], [4, 5]]]], atol=1e-4, rtol=1e-4)
210 np.testing.assert_allclose(out.affine, np.diag([1, 1.4, 1, 1]), atol=1e-4, rtol=1e-4)
211
212 image_name = os.path.join(out_dir, "test1.nii.gz")
213 img = p(np.arange(5).reshape((1, 1, 5, 1)))
214 writer_obj.set_data_array(img, channel_dim=-1, squeeze_end_dims=False)
215 writer_obj.set_metadata(
216 {"affine": np.diag([1, 1, 1, 3, 3]), "original_affine": np.diag([1.4, 2.0, 2, 3, 5])}
217 )
218 writer_obj.write(image_name, verbose=True)
219 out = nib.load(image_name)
220 np.testing.assert_allclose(out.get_fdata(), [[[[0], [2], [4]]]], atol=1e-4, rtol=1e-4)
221 np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]), atol=1e-4, rtol=1e-4)
222
223 def test_write_5d(self):
224 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