def test_nd_h5_illegal(self): with h5py.File(file_path, mode='r') as h5_f: with self.assertRaises(TypeError): _ = dtype_utils.flatten_compound_to_real(h5_f['real']) with self.assertRaises(TypeError): _ = dtype_utils.flatten_compound_to_real(h5_f['complex'])
def test_illegal(self): num_elems = (2, 3) r_vals = np.random.random(size=num_elems) with self.assertRaises(TypeError): _ = dtype_utils.flatten_compound_to_real(r_vals) with self.assertRaises(TypeError): _ = dtype_utils.flatten_compound_to_real(14)
def _get_guess_chunk(self): """ Read the next chunk of the Guess to use for fitting Parameters ---------- verbose : Boolean (optional) Whether or not to print debugging statements """ if self._start_pos < self.max_pos: self._current_sho_spec_slice = slice(self.sho_spec_inds_per_forc * self._current_forc, self.sho_spec_inds_per_forc * (self._current_forc + 1)) self._end_pos = int(min(self.h5_projected_loops.shape[0], self._start_pos + self.max_pos)) self.data = self.h5_projected_loops[self._start_pos:self._end_pos, self._current_sho_spec_slice] elif self._current_forc < self._num_forcs - 1: # Resest for next FORC self._current_forc += 1 self._current_sho_spec_slice = slice(self.sho_spec_inds_per_forc * self._current_forc, self.sho_spec_inds_per_forc * (self._current_forc + 1)) self._current_met_spec_slice = slice(self.metrics_spec_inds_per_forc * self._current_forc, self.metrics_spec_inds_per_forc * (self._current_forc + 1)) self._get_dc_offset() self._start_pos = 0 self._end_pos = int(min(self.h5_projected_loops.shape[0], self._start_pos + self.max_pos)) self.data = self.h5_projected_loops[self._start_pos:self._end_pos, self._current_sho_spec_slice] else: self.data = None guess = self.h5_guess[self._start_pos:self._end_pos, self._current_met_spec_slice].reshape([-1, 1]) self.guess = flatten_compound_to_real(guess)[:, :-1]
def extract_loop_parameters(h5_loop_fit, nuc_threshold=0.03): """ Method to extract a set of physical loop parameters from a dataset of fit parameters Parameters ---------- h5_loop_fit : h5py.Dataset Dataset of loop fit parameters nuc_threshold : float Nucleation threshold to use in calculation physical parameters Returns ------- h5_loop_parm : h5py.Dataset Dataset of physical parameters """ dset_name = h5_loop_fit.name.split('/')[-1] + '_Loop_Parameters' h5_loop_parameters = create_empty_dataset(h5_loop_fit, dtype=switching32, dset_name=dset_name, new_attrs={'nuc_threshold': nuc_threshold}) loop_coef_vec = flatten_compound_to_real(np.reshape(h5_loop_fit, [-1, 1])) switching_coef_vec = calc_switching_coef_vec(loop_coef_vec, nuc_threshold) h5_loop_parameters[:, :] = switching_coef_vec.reshape(h5_loop_fit.shape) h5_loop_fit.file.flush() return h5_loop_parameters
def test_compound_to_real_nd_h5_legal(self): with h5py.File(file_path, mode='r') as h5_f: h5_comp = h5_f['compound'] actual = dtype_utils.flatten_compound_to_real(h5_comp) expected = np.concatenate( [h5_comp['r'], h5_comp['g'], h5_comp['b']], axis=len(h5_comp.shape) - 1) self.assertTrue(np.allclose(actual, expected))
def base_nd_h5_legal(self, lazy): with h5py.File(file_path, mode='r') as h5_f: h5_comp = h5_f['compound'] actual = dtype_utils.flatten_compound_to_real(h5_comp, lazy=lazy) if lazy: self.assertIsInstance(actual, da.core.Array) actual = actual.compute() expected = np.concatenate([h5_comp['r'], h5_comp['g'], h5_comp['b']], axis=len(h5_comp.shape) - 1) self.assertTrue(np.allclose(actual, expected))
def test_1d(self): num_elems = 5 structured_array = np.zeros(shape=num_elems, dtype=struc_dtype) structured_array['r'] = r_vals = np.random.random(size=num_elems) structured_array['g'] = g_vals = np.random.randint(0, high=1024, size=num_elems) structured_array['b'] = b_vals = np.random.random(size=num_elems) expected = np.concatenate((r_vals, g_vals, b_vals)) actual = dtype_utils.flatten_compound_to_real(structured_array) self.assertTrue(np.allclose(actual, expected))
def base_nd(self, lazy_in, lazy): num_elems = (5, 7, 2, 3) structured_array = np.zeros(shape=num_elems, dtype=struc_dtype) structured_array['r'] = r_vals = np.random.random(size=num_elems) structured_array['g'] = g_vals = np.random.randint(0, high=1024, size=num_elems) structured_array['b'] = b_vals = np.random.random(size=num_elems) if lazy_in: structured_array = da.from_array(structured_array, chunks=structured_array.shape) expected = np.concatenate((r_vals, g_vals, b_vals), axis=len(num_elems) - 1) actual = dtype_utils.flatten_compound_to_real(structured_array, lazy=lazy) if lazy: self.assertIsInstance(actual, da.core.Array) actual = actual.compute() self.assertTrue(np.allclose(actual, expected))