Added hdf reader, but there still seems to be a memory leakage bug.

This commit is contained in:
jupfi 2024-02-08 21:57:59 +01:00
parent 57d9274199
commit 62c8863a4e
5 changed files with 187 additions and 2 deletions

2
extern/limedriver vendored

@ -1 +1 @@
Subproject commit 3d1c7bdf2ce95fb69f5a89b195a0f6e18496f97f
Subproject commit f9d86bd4dea17250d9b0267936fb99d35a4ad1fd

View file

@ -17,7 +17,10 @@ classifiers = [
requires = [
"setuptools",
"wheel",
"Cython"
"Cython",
"h5py",
"numpy",
'matplotlib',
]
build-backend = "setuptools.build_meta"

View file

@ -1 +1,2 @@
import limedriver.binding
import limedriver.hdf_reader

View file

@ -0,0 +1,149 @@
import h5py
import numpy as np
import matplotlib.pyplot as plt
# class for accessing data of stored HDF5 file this is from the limr program by andrin doll
class HDF():
def __init__(self, filename = ''):
# check first for the filename provided
if filename != '':
self.HDFsrc = filename
else:
self.HDFsrc = ''
# get data
self.__get_data()
# just an alias for __init__ that does load a specific file
def load(self, filename = ''):
self.__init__(filename)
# gets the data of the file
def __get_data(self):
if (self.HDFsrc == '') | (self.HDFsrc == []):
# initialize all as empty
self.tdy = []
self.tdx = []
self.attrs = []
self.parsoutp = {}
self.parvar = {}
else:
f = h5py.File(self.HDFsrc, 'r')
print("opened file")
HDFkeys = list(f.keys())
for ii, HDFkey in enumerate(HDFkeys):
if ii == 0:
# initialize data array
dsize = f[HDFkey].shape
inddim = dsize[0]
self.tdy = np.zeros((int(dsize[1]/2), int(dsize[0] * len(HDFkeys))),dtype=np.complex_)
print("initialize")
# initialize the output objects
self.attrs = [dynclass() for jj in range(len(HDFkeys))]
print("get the attribute keys")
# get the attribute keys
self.parsoutp = {}
ii_oupargs = 0
for item in f[HDFkey].attrs.items():
print(item)
itemname = item[0][5:]
itemarg = item[0][1:4]
if not ('///' in itemarg):
self.parsoutp[itemarg] = [ item[1], itemname]
else:
self.parsoutp['//'+str(ii_oupargs)] = [ item[1], itemname]
ii_oupargs+=1
print("end of loop")
print("look for vars")
# look for eventual parvar lists
self.parvar = {}
for item in f.attrs.items():
self.parvar[item[0]] = item[1]
# Get the data
data_raw = np.array(f[HDFkey])
try:
self.tdy[:,ii*inddim:(ii+1)*inddim] = np.transpose(np.float_(data_raw[:,::2])) + 1j*np.transpose(np.float_(data_raw[:,1::2]))
except:
pass
# Get the arguments
ii_oupargs = 0
for item in f[HDFkey].attrs.items():
itemname = item[0][5:]
itemarg = item[0][1:4]
if not ('///' in itemarg):
setattr(self.attrs[ii], itemarg, item[1])
else:
setattr(self.attrs[ii], '//'+str(ii_oupargs), item[1])
ii_oupargs+=1
f.close()
srate_MHz = getattr(self.attrs[0], 'sra')*1e-6
self.tdx = 1/srate_MHz*np.arange(self.tdy.shape[0])
# get an argument by matching the text description
def attr_by_txt(self, pattern):
for key in sorted(self.parsoutp):
if pattern in self.parsoutp[key][1]: # pattern match
attr = getattr(self.attrs[0], key)
try:
ouparr = np.zeros( ( len(attr), len(self.attrs)), attr.dtype)
except:
ouparr = np.zeros( ( 1, len(self.attrs)), attr.dtype)
for ii in np.arange(len(self.attrs)):
ouparr[:,ii] = getattr(self.attrs[ii], key)
return np.transpose(ouparr)
print('Problem obtaining the attribute from the description using the pattern ' + pattern + '!')
print('Valid descriptions are: ')
self.print_params()
# get an argument by key
def attr_by_key(self, key):
if key in dir(self.attrs[0]):
attr = getattr(self.attrs[0], key)
try:
ouparr = np.zeros( ( len(attr), len(self.attrs)), attr.dtype)
except:
ouparr = np.zeros( ( 1, len(self.attrs)), attr.dtype)
for ii in np.arange(len(self.attrs)):
ouparr[:,ii] = getattr(self.attrs[ii], key)
return np.transpose(ouparr)
print('Problem obtaining the attribute from key ' + key + '!')
print('Valid keys are: ')
self.print_params()
# print the arguments
def print_params(self, ouponly = False):
for key in sorted(self.parsoutp):
val = getattr(self.attrs[0], key)
if not('//' in key): # input argument?
if ouponly: continue;
print('{:<5}: {:>50} {:<25}'.format(key, val, self.parsoutp[key][1]))
def plot_dta(self):
plt.plot(self.tdx, self.tdy.real)
plt.xlabel('$t$ [$\mu$s]')
plt.ylabel('$y$ [Counts]')
plt.show()
# empty class to store dynamic attributes, basically for the attributes in HDF keys
class dynclass:
pass

View file

@ -8,6 +8,8 @@ from libc.string cimport memcpy, strcpy
from libcpp.string cimport string
import pathlib
cdef extern from "limedriver.h":
cdef struct LimeConfig_t:
float srate
@ -149,8 +151,33 @@ cdef class PyLimeConfig:
def __dealloc__(self):
if self._config is not NULL:
free(self._config.p_frq)
free(self._config.p_dur)
free(self._config.p_dur_smp)
free(self._config.p_offs)
free(self._config.p_amp)
free(self._config.p_frq_smp)
free(self._config.p_pha)
free(self._config.p_phacyc_N)
free(self._config.p_phacyc_lev)
free(self._config.am_frq)
free(self._config.am_pha)
free(self._config.am_depth)
free(self._config.am_mode)
free(self._config.am_frq_smp)
free(self._config.fm_frq)
free(self._config.fm_pha)
free(self._config.fm_width)
free(self._config.fm_mode)
free(self._config.fm_frq_smp)
free(self._config.p_c0_en)
free(self._config.p_c1_en)
free(self._config.p_c2_en)
free(self._config.p_c3_en)
free(self._config)
@property
def srate(self):
return self._config.srate
@ -802,3 +829,8 @@ cdef class PyLimeConfig:
def run(self):
return run_experiment_from_LimeCfg(self._config[0])
def get_path(self):
path = self.save_path + self.file_stamp + '_' + self.file_pattern + '.h5'
path = pathlib.Path(path).absolute()
return path