def validate_dtype(self, imaker, params): # data / storage dtype img = imaker() # Need to rename this one assert img.get_data_dtype().type == params['dtype'] # dtype survives round trip if self.has_scaling and self.can_save: with np.errstate(invalid='ignore'): rt_img = bytesio_round_trip(img) assert rt_img.get_data_dtype().type == params['dtype'] # Setting to a different dtype img.set_data_dtype(np.float32) # assumed supported for all formats assert img.get_data_dtype().type == np.float32 # dtype survives round trip if self.can_save: rt_img = bytesio_round_trip(img) assert rt_img.get_data_dtype().type == np.float32
def validate_filenames(self, imaker, params): # Validate the filename, file_map interface if not self.can_save: raise unittest.SkipTest img = imaker() img.set_data_dtype(np.float32) # to avoid rounding in load / save # Make sure the object does not have a file_map img.file_map = None # The bytesio_round_trip helper tests bytesio load / save via file_map rt_img = bytesio_round_trip(img) assert_array_equal(img.shape, rt_img.shape) assert_almost_equal(img.get_fdata(), rt_img.get_fdata()) assert_almost_equal(np.asanyarray(img.dataobj), np.asanyarray(rt_img.dataobj)) # Give the image a file map klass = type(img) rt_img.file_map = bytesio_filemap(klass) # This object can now be saved and loaded from its own file_map rt_img.to_file_map() rt_rt_img = klass.from_file_map(rt_img.file_map) assert_almost_equal(img.get_fdata(), rt_rt_img.get_fdata()) assert_almost_equal(np.asanyarray(img.dataobj), np.asanyarray(rt_img.dataobj)) # get_ / set_ filename fname = 'an_image' + self.standard_extension for path in (fname, pathlib.Path(fname)): img.set_filename(path) assert img.get_filename() == str(path) assert img.file_map['image'].filename == str(path) # to_ / from_ filename fname = 'another_image' + self.standard_extension for path in (fname, pathlib.Path(fname)): with InTemporaryDirectory(): # Validate that saving or loading a file doesn't use deprecated methods internally with clear_and_catch_warnings() as w: warnings.filterwarnings('error', category=DeprecationWarning, module=r"nibabel.*") img.to_filename(path) rt_img = img.__class__.from_filename(path) assert_array_equal(img.shape, rt_img.shape) assert_almost_equal(img.get_fdata(), rt_img.get_fdata()) assert_almost_equal(np.asanyarray(img.dataobj), np.asanyarray(rt_img.dataobj)) del rt_img # to allow windows to delete the directory
def prox_imaker(): img = self.image_maker(arr, aff, hdr) rt_img = bytesio_round_trip(img) return self.image_maker(rt_img.dataobj, aff, rt_img.header)