def test_attempt_reg_ref_build_pos(self): file_path = 'test.h5' data_utils.delete_existing_file(file_path) with h5py.File(file_path, mode='w') as h5_f: h5_dset = h5_f.create_dataset('Indices', data=np.random.rand(5, 2)) dim_names = ['Bias', 'Cycle'] expected = { 'Bias': (slice(None), slice(0, 1)), 'Cycle': (slice(None), slice(1, 2)) } if sys.version_info.major == 3: with self.assertWarns(UserWarning): cleaned = reg_ref.attempt_reg_ref_build(h5_dset, dim_names) else: cleaned = reg_ref.attempt_reg_ref_build(h5_dset, dim_names) for key, value in expected.items(): self.assertEqual(value, cleaned[key]) os.remove(file_path)
def test_attempt_reg_ref_build_pos_too_few_dims(self): file_path = 'test.h5' data_utils.delete_existing_file(file_path) with h5py.File(file_path, mode='w') as h5_f: h5_dset = h5_f.create_dataset('Indices', data=np.random.rand(5, 2)) dim_names = ['Bias'] ret_val = reg_ref.attempt_reg_ref_build(h5_dset, dim_names) self.assertEqual(ret_val, dict()) os.remove(file_path)
def _write_dset_attributes(h5_dset, attrs, print_log=False): """ Writes attributes to a h5py dataset Parameters ---------- h5_dset : h5py.Dataset object h5py dataset to which the attributes will be written to. This function handles region references as well attrs : dict Dictionary containing the attributes as key-value pairs print_log : bool, optional. Default=False Whether or not to print debugging statements """ if not isinstance(attrs, dict): HDFwriter.__safe_abort(h5_dset.file) raise TypeError( 'attrs should be a dictionary but is instead of type ' '{}'.format(type(attrs))) if not isinstance(h5_dset, h5py.Dataset): raise TypeError( 'h5_dset should be a h5py Dataset object but is instead of type ' '{}. UNABLE to safely abort'.format(type(h5_dset))) # First, set aside the complicated attribute(s) attr_dict = attrs.copy() labels_dict = attr_dict.pop('labels', None) # Next, write the simple ones using a centralized function write_simple_attrs(h5_dset, attr_dict, obj_type='dataset', verbose=print_log) if labels_dict is None: if print_log: print('Finished writing all attributes of dataset') return if isinstance(labels_dict, (tuple, list)): # What if the labels dictionary is just a list of names? make a dictionary using the names # This is the most that can be done. labels_dict = attempt_reg_ref_build(h5_dset, labels_dict, verbose=print_log) if len(labels_dict) == 0: if print_log: warn('No region references to write') return # Now, handle the region references attribute: write_region_references(h5_dset, labels_dict, verbose=print_log)