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

Method test_write_5d

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

Source from the content-addressed store, hash-verified

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:
225 image_name = os.path.join(out_dir, "test.nii.gz")
226 for p in TEST_NDARRAYS:
227 img = p(np.arange(12).reshape((1, 1, 3, 2, 2)))
228 writer_obj = NibabelWriter()
229 writer_obj.set_data_array(img, channel_dim=-1, squeeze_end_dims=False, spatial_ndim=None)
230 writer_obj.set_metadata({"affine": np.diag([1, 1, 1, 1]), "original_affine": np.diag([1.4, 1, 1, 1])})
231 writer_obj.write(image_name, verbose=True)
232 out = nib.load(image_name)
233 np.testing.assert_allclose(
234 out.get_fdata(),
235 np.array([[[[[0.0, 1.0], [2.0, 3.0]], [[4.0, 5.0], [6.0, 7.0]], [[8.0, 9.0], [10.0, 11.0]]]]]),
236 atol=1e-4,
237 rtol=1e-4,
238 )
239 np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]), atol=1e-4, rtol=1e-4)
240
241 image_name = os.path.join(out_dir, "test1.nii.gz")
242 img = p(np.arange(10).reshape((1, 1, 5, 1, 2)))
243 writer_obj.set_data_array(img, channel_dim=-1, squeeze_end_dims=False, spatial_ndim=None)
244 writer_obj.set_metadata({"affine": np.diag([1, 1, 1, 3]), "original_affine": np.diag([1.4, 2.0, 2, 3])})
245 writer_obj.write(image_name, verbose=True)
246 out = nib.load(image_name)
247 np.testing.assert_allclose(
248 out.get_fdata(), np.array([[[[[0.0, 2.0]], [[4.0, 5.0]], [[7.0, 9.0]]]]]), atol=1e-4, rtol=1e-4
249 )
250 np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]), atol=1e-4, rtol=1e-4)
251
252
253if __name__ == "__main__":

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected