def h5load(filename, verbose): ''' h5load(filename, verbose) input: - filename (as string) - names of variables and values to be loaded (as string) return: - dictionary of saved data notice: use with files saved with accpy.dataio.save ''' if filename[-5:] != '.hdf5': filename = ''.join([filename, '.hdf5']) fid = h5pyFile(filename, 'r') data = {} if verbose: print('\n==========================================================') print('Beginning to load from %s ...' % filename) print('\n----------------------------------------------------------') for key in fid: data[key] = fid[key].value print('Loading values from {0:} {1:} ... '.format( key, type(data[key]))) print('\n----------------------------------------------------------') print('... finished loading from %s !' % filename) print('\n==========================================================') else: for key in fid: data[key] = fid[key].value fid.close() return data
def confsave(filename, listofvars, listofvals): # working with two lists as dictionarys do not accept numpy arrays hdf5_fid = h5pyFile(filename, 'w') hdf5_fid.create_dataset('listofvars', data=listofvars) for var, val in zip(listofvars, listofvals): hdf5_fid.create_dataset(var, data=val) hdf5_fid.close()
def save(filename, showinfo, **namesandvariables): ''' save(filename,variables(e.g. a=a, b=b, ...)) input: - desired filename as string - names = values of variables to be saved return: - saves data to "datetime_filename.hdf5 in working directory" notice: accepted datatypes: - ''' timstamp = strftime('%Y%m%d%H%M%S') filename = ''.join([timstamp, '_', filename, '.hdf5']) hdf5_fid = h5pyFile(filename, 'w') if showinfo: print('\n==========================================================') print('Beginning to save to %s ...' % filename) print('\n----------------------------------------------------------') for key, value in namesandvariables.iteritems(): print('Saving values in %s ... ' % key) hdf5_fid.create_dataset(key, data=value) print('\n----------------------------------------------------------') print('... finished saving to %s !' % filename) print('\n==========================================================') else: for key, value in namesandvariables.iteritems(): hdf5_fid.create_dataset(key, data=value) hdf5_fid.close() return filename
def load(filename, showinfo, *varnames): ''' load(filename,variables(e.g. 'a', 'b', ...)) input: - desired filename (as string) - names of variables and values to be loaded (as string) return: - wanted variables are loaded from filename notice: use with files saved with mypy.save - ''' if filename[-5:] != '.hdf5': filename = ''.join([filename, '.hdf5']) fid = h5pyFile(filename, 'r') data = [] if showinfo: print('\n==========================================================') print('Beginning to load from %s ...' % filename) print('\n----------------------------------------------------------') for arg in varnames: print('Loading values from %s ...' % arg) data.append(fid[arg].value) print('\n----------------------------------------------------------') print('... finished loading from %s !' % filename) print('\n==========================================================') else: for arg in varnames: data.append(fid[arg].value) fid.close() return data
def get_fcx2( self, sessions = 'all', path = 'fcx-2/' ): if sessions == 'all': sessions = [f's{no}' for no in range(1, 11)] sessions = [f'{self.root}{path}{s}' for s in sessions] tmp = [] for s in sessions: for p in os.listdir(s): if 'data_primary' in p: tmp.append(f'{s}/{p}') sessions = tmp del tmp out = {} for s in sessions: with h5pyFile(s, 'r') as fp: data = np.array(fp['gdat_clean_filt']) # Errors 1: empty channels data = data[:, (data == 0).all(0) == False] # Errors 2: Sessions that are padded with zeroes to extreme lengths # (Note: == 0 comparisson works, since no non-trash-value is close enough to 0 to return true) data = data[data != 0].reshape((-1, data.shape[1])) sess = s.split('/')[-2] out[sess] = { 'data': data, 'fs': 500 if ('8' in sess or '9' in sess) else 1000 } # Construct spectrogram functions for sess, obj in out.items(): def spect_for_sess( channel, nperseg = 100, noverlap = 75, nfft = 2 ** 12, f_lo = 10, f_hi = 100, window = 'boxcar', normalize = True ): return self.spectrogram_base( obj['data'].T[channel].flatten(), obj['fs'], nperseg, noverlap, nfft, f_lo, f_hi, window = 'boxcar', normalize = True ) obj['spectrogram'] = spect_for_sess return out
def confload(filename): fid = h5pyFile(filename, 'r') listofvars = list(fid['listofvars'].value) listofvals = [] for var in listofvars: listofvals.append(fid[var].value) fid.close() return listofvars, listofvals
def h5save(filename, verbose, **namesandvariables): ''' save dataset to hdf5 format input: - desired filename as string - names = values of variables to be saved return: - saves data to "timestamp_filename.hdf5 in working directory" - complete filename is returned usage: 1. recommended datadict = {'a' : 2, 'b' : 'foo', 'c' : 1.337, 'd' : [1, 2, 'c']} h5save(filename, True. **datadict) 2. alternative a=2, b='foo', c=1.337, d=[1, 2, 'c'] h5save(filename, True. a=a, b=b, c=c, d=d) accepted datatypes: - int -> numpy.int64 - str -> str - float -> numpy.float64 - list -> numpy.ndarray of: - np.string__ if >0 string - np.float64 if >0 float - np.int64 if only ints ''' timstamp = strftime('%Y%m%d%H%M%S') filename = ''.join([timstamp, '_', filename, '.hdf5']) hdf5_fid = h5pyFile(filename, 'w') if verbose: print('\n==========================================================') print('Beginning to save to %s ...' % filename) print('\n----------------------------------------------------------') for key, value in namesandvariables.iteritems(): if verbose: print('Saving values in %s ... ' % key) hdf5_fid.create_dataset(key, data=value) if verbose: print('\n----------------------------------------------------------') print('... finished saving to %s !' % filename) print('\n==========================================================') hdf5_fid.close() return filename