def test_swmr_mode_consistency(self): fname = self.mktemp() fid = File(fname, 'w', libver='latest') g = fid.create_group('foo') assert fid.swmr_mode == g.file.swmr_mode == False fid.swmr_mode = True # This setter should affect both fid and group member file attribute assert fid.swmr_mode == g.file.swmr_mode == True fid.close()
def test_file_mode_generalizes(self): fname = self.mktemp() fid = File(fname, 'w', libver='latest') g = fid.create_group('foo') # fid and group member file attribute should have the same mode assert fid.mode == g.file.mode == 'r+' fid.swmr_mode = True # fid and group member file attribute should still be 'r+' # even though file intent has changed assert fid.mode == g.file.mode == 'r+' fid.close()
def write_data_to_file(datadict: DataDict, f: h5py.File, groupname: str = 'data', append_mode: AppendMode = AppendMode.new, swmr_mode: bool = True) -> None: if groupname not in f: raise RuntimeError('Group does not exist, initialize file first.') grp = f[groupname] # if we want to use swmr, we need to make sure that we're not # creating any more objects (see hdf5 docs). allexist = True for k, v in datadict.data_items(): if k not in grp: allexist = False # add top-level meta data. for k, v in datadict.meta_items(clean_keys=False): set_attr(grp, k, v) f.flush() if allexist and swmr_mode and not f.swmr_mode: f.swmr_mode = True for k, v in datadict.data_items(): data = v['values'] shp = data.shape nrows = shp[0] # create new dataset, add axes and unit metadata if k not in grp: maxshp = tuple([None] + list(shp[1:])) ds = grp.create_dataset(k, maxshape=maxshp, data=data) # add meta data add_cur_time_attr(ds) if v.get('axes', []) != []: set_attr(ds, 'axes', v['axes']) if v.get('unit', "") != "": set_attr(ds, 'unit', v['unit']) for kk, vv in datadict.meta_items(k, clean_keys=False): set_attr(ds, kk, vv) ds.flush() # if the dataset already exits, append data according to # chosen append mode. else: ds = grp[k] dslen = ds.shape[0] if append_mode == AppendMode.new: newshp = tuple([nrows] + list(shp[1:])) ds.resize(newshp) ds[dslen:] = data[dslen:] elif append_mode == AppendMode.all: newshp = tuple([dslen + nrows] + list(shp[1:])) ds.resize(newshp) ds[dslen:] = data[:] ds.flush() f.flush()