def nifti_rw(self, test_data, reader, writer, dtype, resample=True): test_data = test_data.astype(dtype) ndim = len(test_data.shape) - 1 for p in TEST_NDARRAYS: output_ext = ".nii.gz" filepath = f"testfile_{ndim}d" saver = SaveImage(output_dir=self.test_dir, output_ext=output_ext, resample=resample, separate_folder=False, writer=writer) saver( p(test_data), { "filename_or_obj": f"{filepath}.png", "affine": np.eye(4), "original_affine": np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), }, ) saved_path = os.path.join(self.test_dir, filepath + "_trans" + output_ext) self.assertTrue(os.path.exists(saved_path)) loader = LoadImage(reader=reader, squeeze_non_spatial_dims=True) data, meta = loader(saved_path) if meta["original_channel_dim"] == -1: _test_data = moveaxis(test_data, 0, -1) else: _test_data = test_data[0] if resample: _test_data = moveaxis(_test_data, 0, 1) assert_allclose(data, _test_data)
def png_rw(self, test_data, reader, writer, dtype, resample=True): test_data = test_data.astype(dtype) ndim = len(test_data.shape) - 1 for p in TEST_NDARRAYS: output_ext = ".png" filepath = f"testfile_{ndim}d" saver = SaveImage(output_dir=self.test_dir, output_ext=output_ext, resample=resample, separate_folder=False, writer=writer) saver(p(test_data), { "filename_or_obj": f"{filepath}.png", "spatial_shape": (6, 8) }) saved_path = os.path.join(self.test_dir, filepath + "_trans" + output_ext) self.assertTrue(os.path.exists(saved_path)) loader = LoadImage(reader=reader) data, meta = loader(saved_path) if meta["original_channel_dim"] == -1: _test_data = moveaxis(test_data, 0, -1) else: _test_data = test_data[0] assert_allclose(data, _test_data)