def test_output_h5_file_already_exists(self): with h5py.File(image_path.replace('.png', '.h5')) as _: pass with self.assertRaises(FileExistsError): translator = ImageTranslator() _ = translator.translate(image_path)
def test_path_not_str(self): with self.assertRaises(TypeError): translator = ImageTranslator() _ = translator.translate(np.arange(4))
def test_path_does_not_exist(self): with self.assertRaises(FileNotFoundError): translator = ImageTranslator() _ = translator.translate('no_such_file.png')
def test_invalid_interp(self): with self.assertRaises(ValueError): translator = ImageTranslator() _ = translator.translate(image_path, bin_factor=2, interp_func='dsdsdsd')
def test_invalid_h5_path(self): with self.assertRaises(TypeError): translator = ImageTranslator() _ = translator.translate(image_path, h5_path=np.arange(4))
def test_invalid_dtype(self): with self.assertRaises(TypeError): translator = ImageTranslator() _ = translator.translate(image_path, bin_factor=['dfrdd', True])
def test_float_parms(self): with self.assertRaises(TypeError): translator = ImageTranslator() _ = translator.translate(image_path, bin_factor=1.34)
def test_neg_parms(self): with self.assertRaises(ValueError): translator = ImageTranslator() _ = translator.translate(image_path, bin_factor=-2)
def test_too_many_dims(self): with self.assertRaises(ValueError): translator = ImageTranslator() _ = translator.translate(image_path, bin_factor=(1, 2, 3))
def main_translate(self, **kwargs): h5_path = kwargs.pop('h5_path', image_path.replace('.png', '.h5')) delete_existing_file(h5_path) input_image = rand_image.copy() usize, vsize = input_image.shape[:2] translator = ImageTranslator() h5_path = translator.translate(image_path, **kwargs) image_parms = dict() if 'bin_factor' in kwargs.keys(): bin_factor = kwargs.pop('bin_factor') if bin_factor is None: _ = kwargs.pop('interp_func', None) else: if isinstance(bin_factor, int): bin_factor = (bin_factor, bin_factor) interp_func = kwargs.pop('interp_func', Image.BICUBIC) image_parms.update({ 'image_binning_size': np.array(bin_factor), 'image_PIL_resample_mode': interp_func }) img_obj = Image.fromarray(input_image) img_obj = img_obj.convert(mode="L") img_obj = img_obj.resize( (int(vsize / bin_factor[1]), int(usize / bin_factor[0])), resample=interp_func) input_image = np.asarray(img_obj) image_parms.update({'normalized': False}) input_image = input_image.copy() if 'normalize' in kwargs.keys(): normalize = kwargs.pop('normalize') if normalize: input_image -= np.min(input_image) input_image = input_image / np.float32(np.max(input_image)) image_parms.update({'normalized': True}) image_parms.update({ 'image_min': np.min(input_image), 'image_max': np.max(input_image) }) with h5py.File(h5_path, mode='r') as h5_f: self.basic_file_validation(h5_f) h5_meas_grp = h5_f['Measurement_000'] h5_chan_grp = h5_meas_grp['Channel_000'] usid_main = USIDataset(h5_chan_grp['Raw_Data']) # check the attributes under this group for key, expected_val in image_parms.items(): self.assertTrue( np.all( hdf_utils.get_attr(h5_meas_grp, key) == expected_val)) one_d_image = input_image.T.reshape(-1, 1) self.assertTrue(np.allclose(one_d_image, usid_main[()])) # self.assertTrue(np.allclose(rand_image, np.reshape(usid_main[()], rand_image.shape))) pos_data = np.vstack((np.tile(np.arange(input_image.shape[0]), input_image.shape[1]), np.repeat(np.arange(input_image.shape[1]), input_image.shape[0]))).T validate_aux_dset_pair(self, h5_chan_grp, usid_main.h5_pos_inds, usid_main.h5_pos_vals, ['Y', 'X'], ['a.u.', 'a.u.'], pos_data, h5_main=usid_main, is_spectral=False) delete_existing_file(h5_path)