def test_pos_and_spec_sliced_list(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) h5_pos_inds = usi_main.h5_pos_inds h5_spec_inds = usi_main.h5_spec_inds actual_pos, actual_spec = usi_main._get_pos_spec_slices({ 'X': [1, 2, 4], 'Bias': slice(1, 7, 3) }) # we want every fifth position starting from 3 positions = [] for col_ind in [1, 2, 4]: positions += np.argwhere(h5_pos_inds[h5_pos_inds.attrs['X']] == col_ind)[:, 0].tolist() specs = [] for bias_ind in range(1, 7, 3): specs += np.argwhere(h5_spec_inds[h5_spec_inds.attrs['Bias']] == bias_ind)[:, 1].tolist() expected_pos = np.expand_dims(positions, axis=1) expected_spec = np.expand_dims(specs, axis=1) expected_pos.sort(axis=0) expected_spec.sort(axis=0) self.assertTrue(np.allclose(expected_spec, actual_spec)) self.assertTrue(np.allclose(expected_pos, actual_pos))
def test_empty_dict(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) actual_pos, actual_spec = usi_main._get_pos_spec_slices({}) self.assertTrue( np.allclose(np.expand_dims(np.arange(14), axis=1), actual_spec)) self.assertTrue( np.allclose(np.expand_dims(np.arange(15), axis=1), actual_pos))
def test_both_pos_removed(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) actual_pos, actual_spec = usi_main._get_pos_spec_slices({'X': 3, 'Y': 1}) # we want every fifth position starting from 3 expected_pos = np.expand_dims([1 * 5 + 3], axis=1) expected_spec = np.expand_dims(np.arange(14), axis=1) self.assertTrue(np.allclose(expected_spec, actual_spec)) self.assertTrue(np.allclose(expected_pos, actual_pos))
def test_get_pos_spec_slices_non_existent_dim(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: pycro_main = USIDataset(h5_f['/Raw_Measurement/source_main']) with self.assertRaises(KeyError): _ = pycro_main._get_pos_spec_slices({ 'blah': 4, 'X': 3, 'Y': 1 })
def test_one_pos_dim_removed(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) # orig_pos = np.vstack([np.tile(np.arange(5), 3), np.repeat(np.arange(3), 5)]).T # orig_spec = np.vstack([np.tile(np.arange(7), 2), np.repeat(np.arange(2), 7)]) actual_pos, actual_spec = usi_main._get_pos_spec_slices({'X': 3}) # we want every fifth position starting from 3 expected_pos = np.expand_dims(np.arange(3, 15, 5), axis=1) expected_spec = np.expand_dims(np.arange(14), axis=1) self.assertTrue(np.allclose(expected_spec, actual_spec)) self.assertTrue(np.allclose(expected_pos, actual_pos))
def test_two_pos_dim_sliced_list(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) actual_pos, actual_spec = usi_main._get_pos_spec_slices({'X': [1, 2, 4], 'Y': 1}) # we want every fifth position starting from 3 positions = [] for row_ind in range(1, 2): for col_ind in [1, 2, 4]: positions.append(5 * row_ind + col_ind) expected_pos = np.expand_dims(positions, axis=1) expected_spec = np.expand_dims(np.arange(14), axis=1) self.assertTrue(np.allclose(expected_spec, actual_spec)) self.assertTrue(np.allclose(expected_pos, actual_pos))
def test_out_of_bounds(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) with self.assertRaises(IndexError): _ = usi_main._get_pos_spec_slices({'X': 15, 'Y': 1})
def test_negative_index(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) with self.assertRaises(ValueError): _ = usi_main._get_pos_spec_slices({'X': -4, 'Y': 1})
def test_incorrect_type(self): with h5py.File(test_h5_file_path, mode='r') as h5_f: usi_main = USIDataset(h5_f['/Raw_Measurement/source_main']) with self.assertRaises(TypeError): _ = usi_main._get_pos_spec_slices({'X': 'fdfd', 'Y': 1})