newer FC scripts are working, now older ones fail

This commit is contained in:
Dominik Demuth 2023-04-12 20:42:33 +02:00
parent 3793f67951
commit 29518b9ea0
3 changed files with 106 additions and 175 deletions

View File

@ -42,6 +42,7 @@ AppDir:
- python3.9-minimal - python3.9-minimal
- python3-numpy - python3-numpy
- python3-scipy - python3-scipy
- gnuplot-nox
- python3-bsddb3 - python3-bsddb3
- python3-h5py - python3-h5py
- python3-pyqt5 - python3-pyqt5
@ -57,7 +58,6 @@ AppDir:
- qtbase5-dev-tools - qtbase5-dev-tools
- qtchooser - qtchooser
- pyqt5-dev-tools - pyqt5-dev-tools
- qtchooser
- libavahi-client3 - libavahi-client3
- libavahi-common-data - libavahi-common-data
- libavahi-common3 - libavahi-common3

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import pathlib import pathlib
from matplotlib import pyplot as plt
# import matplotlib.pyplot as plt # import matplotlib.pyplot as plt
from scipy.optimize import curve_fit from scipy.optimize import curve_fit
import numpy as np import numpy as np
@ -14,6 +15,7 @@ from nmreval.utils.utils import get_temperature, roundrobin
class FCReader: class FCReader:
def __init__(self, fname: (list | str | pathlib.Path)): def __init__(self, fname: (list | str | pathlib.Path)):
print('Hello')
if isinstance(fname, (str, pathlib.Path)): if isinstance(fname, (str, pathlib.Path)):
self.fnames = [fname] self.fnames = [fname]
else: else:
@ -25,8 +27,6 @@ class FCReader:
self.t_params = {} self.t_params = {}
self.f_params = {} self.f_params = {}
self._scipt_type = 'old'
def __call__(self, fname: (list | str | pathlib.Path)): def __call__(self, fname: (list | str | pathlib.Path)):
if isinstance(fname, (str, pathlib.Path)): if isinstance(fname, (str, pathlib.Path)):
self.fnames = [fname] self.fnames = [fname]
@ -77,7 +77,14 @@ class FCReader:
def _read_from_hdf(self, filename: (str | pathlib.Path)) -> dict: def _read_from_hdf(self, filename: (str | pathlib.Path)) -> dict:
_temp = {} _temp = {}
reader = HdfReader(filename) reader = HdfReader(filename)
mag_directory = 'mag' if self._scipt_type == 'old' else 'Magnetization'
mag_directory = 'Magnetization'
for m in ['Magnetization', 'mag']:
try:
mag_directory = m
except KeyError:
continue
for mag in reader.get_selected(mag_directory, dtype='points'): for mag in reader.get_selected(mag_directory, dtype='points'):
_temp[mag.value] = mag _temp[mag.value] = mag
@ -97,21 +104,15 @@ class FCReader:
return _temp return _temp
@staticmethod def _read_signals(self, filename, region: tuple = None) -> dict:
def _read_signals(filename, region: tuple = None) -> dict:
reader = HdfReader(filename) reader = HdfReader(filename)
start = 0
stop = 30e-5
# This is one set with attributes to find default start:stop values # This is one set with attributes to find default start:stop values (with some
try:
p = reader.parameters('/ABS_ACC_FID') p = reader.parameters('/ABS_ACC_FID')
start = p['start'] start = p.get('stainte') or p.get('start') or 0
stop = p['stop'] stop = p.get('stointe') or p.get('stop') or 30e-5
except:
pass
if region is None: if region == (None, None) or region is None:
region = (start, stop) region = (start, stop)
if region[0] is None: if region[0] is None:
@ -119,15 +120,35 @@ class FCReader:
if region[1] is None: if region[1] is None:
region = (region[0], stop) region = (region[0], stop)
sig = reader.get_selected('/data/B=*/ACC_ABS_FID*', dtype='signal')
_temp = {} _temp = {}
for s in sig: bevo_groups = reader['data'].children
pts = s.points([region]) for grp in bevo_groups.values():
b = s.group for k in grp.data():
if b not in _temp: print(k)
_temp[b] = [] if str(k.parent) != 'ACC_ABS_FID_sig (group)':
continue
_temp[b].append([s.value, *[pp[1] for pp in pts]]) print(k.path, k, k.parent, k.parameter['bevo'], k.parameter['tevo'])
bevo = k.parameter['bevo']
tevo = k.parameter['tevo']
s = reader.make_signal(k, flag='fid', value='tevo', group='bevo')
pts = s.points([region])
if bevo not in _temp:
_temp[bevo] = []
_temp[bevo].append((tevo, *[pp[1] for pp in pts]))
# sig = reader.get_selected('/data/B=*/ACC_ABS_FID*', dtype='signal')
# _temp = {}
# for s in sig:
# pts = s.points([region])
# b = s.group
# if b not in _temp:
# _temp[b] = []
#
# _temp[b].append([s.value, *[pp[1] for pp in pts]])
for b, m in sorted(_temp.items()): for b, m in sorted(_temp.items()):
m = np.array(m) m = np.array(m)
@ -148,9 +169,9 @@ class FCReader:
fit_path = fname_no_ext.joinpath('fit') fit_path = fname_no_ext.joinpath('fit')
fit_path.mkdir(parents=True, exist_ok=True) fit_path.mkdir(parents=True, exist_ok=True)
# if save_fig: if save_fig:
# image_path = fname_no_ext.joinpath('png') image_path = fname_no_ext.joinpath('png')
# image_path.mkdir(parents=True, exist_ok=True) image_path.mkdir(parents=True, exist_ok=True)
header = 'm0\tt1\tbeta\toff\n' header = 'm0\tt1\tbeta\toff\n'
@ -189,16 +210,16 @@ class FCReader:
np.savetxt(fit_path.joinpath(save_name), np.c_[xplot, yplot], np.savetxt(fit_path.joinpath(save_name), np.c_[xplot, yplot],
header=header+'\t'.join([f'{p}+/-{err}' for p, err in zip(p0, perr)])) header=header+'\t'.join([f'{p}+/-{err}' for p, err in zip(p0, perr)]))
# if save_fig: if save_fig:
# fig, ax = plt.subplots() fig, ax = plt.subplots()
# ax.set_xlabel('t / s') ax.set_xlabel('t / s')
# ax.set_ylabel('M') ax.set_ylabel('M')
# axheader = f'T1: {p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})' axheader = f'T1: {p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})'
# ax.set_title(f'f = {k:.4g} Hz\n{axheader}') ax.set_title(f'f = {k:.4g} Hz\n{axheader}')
# ax.semilogx(v.x, v.y, 'o') ax.semilogx(v.x, v.y, 'o')
# ax.semilogx(xplot, yplot, '-') ax.semilogx(xplot, yplot, '-')
# fig.savefig(image_path.joinpath(save_name).with_suffix('.png')) fig.savefig(image_path.joinpath(save_name).with_suffix('.png'))
# plt.close(fig) plt.close(fig)
freqs = np.asanyarray(freqs) freqs = np.asanyarray(freqs)
params = np.asanyarray(params) params = np.asanyarray(params)
@ -290,45 +311,45 @@ class FCReader:
path = pathlib.Path(path) path = pathlib.Path(path)
path.mkdir(parents=True, exist_ok=True) path.mkdir(parents=True, exist_ok=True)
# fig_mag, ax_mag = plt.subplots() fig_mag, ax_mag = plt.subplots()
# fig_t1, ax_t1 = plt.subplots() fig_t1, ax_t1 = plt.subplots()
# fig_beta, ax_beta = plt.subplots() fig_beta, ax_beta = plt.subplots()
#
# if kind == 'temp': if kind == 'temp':
# _params = self.t_params _params = self.t_params
# else: else:
# _params = self.f_params _params = self.f_params
#
# save_path = path.joinpath(kind) save_path = path.joinpath(kind)
# if not save_path.exists(): if not save_path.exists():
# save_path.mkdir(parents=True) save_path.mkdir(parents=True)
#
# for key, par in _params.items(): for key, par in _params.items():
# pl, = ax_mag.plot(par[:, 0], par[:, 1], 'o', label=key) pl, = ax_mag.plot(par[:, 0], par[:, 1], 'o', label=key)
# ax_mag.plot(par[:, 0], par[:, 3], 's', color=pl.get_color()) ax_mag.plot(par[:, 0], par[:, 3], 's', color=pl.get_color())
# ax_t1.plot(par[:, 0], par[:, 5], 'o', label=key) ax_t1.plot(par[:, 0], par[:, 5], 'o', label=key)
# ax_beta.plot(par[:, 0], par[:, 7], 'o', label=key) ax_beta.plot(par[:, 0], par[:, 7], 'o', label=key)
#
# for a in [ax_mag, ax_t1, ax_beta]: for a in [ax_mag, ax_t1, ax_beta]:
# if kind == 'freq': if kind == 'freq':
# a.legend(loc='upper left', bbox_to_anchor=(1, 1), ncol=2) a.legend(loc='upper left', bbox_to_anchor=(1, 1), ncol=2)
# a.set_xlabel('T / K') a.set_xlabel('T / K')
# else: else:
# a.set_xscale('log') a.set_xscale('log')
# a.legend(loc='upper left', bbox_to_anchor=(1, 1)) a.legend(loc='upper left', bbox_to_anchor=(1, 1))
# a.set_xlabel('f / Hz') a.set_xlabel('f / Hz')
#
# ax_t1.set_yscale('log') ax_t1.set_yscale('log')
# ax_t1.set_ylabel('T1 / s') ax_t1.set_ylabel('T1 / s')
# ax_beta.set_ylabel('beta') ax_beta.set_ylabel('beta')
# ax_mag.set_ylabel('M0 (squares), Offset (circles)') ax_mag.set_ylabel('M0 (squares), Offset (circles)')
#
# fig_beta.savefig(path.joinpath(f'beta_{kind}.png'), bbox_inches="tight") fig_beta.savefig(path.joinpath(f'beta_{kind}.png'), bbox_inches="tight")
# fig_mag.savefig(path.joinpath(f'mag_{kind}.png'), bbox_inches="tight") fig_mag.savefig(path.joinpath(f'mag_{kind}.png'), bbox_inches="tight")
# fig_t1.savefig(path.joinpath(f't1_{kind}.png'), bbox_inches="tight") fig_t1.savefig(path.joinpath(f't1_{kind}.png'), bbox_inches="tight")
# plt.close(fig_mag) plt.close(fig_mag)
# plt.close(fig_beta) plt.close(fig_beta)
# plt.close(fig_t1) plt.close(fig_t1)
def get_parameter(self, parameter='all', kind='freq', path=None, write=True, plot=True): def get_parameter(self, parameter='all', kind='freq', path=None, write=True, plot=True):
param_list = [] param_list = []
@ -391,99 +412,10 @@ class FCReader:
return m0 * np.exp(-(x/t1)**beta) + off return m0 * np.exp(-(x/t1)**beta) + off
class FCFileReader:
def __init__(self, fname: str):
self.filename = pathlib.Path(fname)
self._is_dir = self.filename.is_dir()
self._script_type = None
self.reader = None
if not self._is_dir:
self.reader = HdfReader(self.filename)
if 'mag' in self.reader:
self._script_type = 'old'
elif 'Magnetization' in self.reader:
self._script_type = '2022_09'
self._temperature = get_temperature(self.filename.stem)
def load_magnetization(self, region=None):
if self._is_dir:
_temp = self._read_from_dir()
else:
if region is None:
_temp = self._read_from_hdf()
else:
_temp = self._read_signals(region)
if not _temp:
raise OSError(-666, f'No magnetization found for {self.filename.name}.', self.filename.name)
def _read_from_hdf(self) -> dict:
_temp = {}
mag_directory = 'mag' if self._script_type == 'old' else 'Magnetization'
for mag in self.reader.get_selected(mag_directory, dtype='points'):
_temp[mag.value] = mag
return _temp
def _read_from_dir(self) -> dict:
fname_no_ext = self.filename.with_suffix('')
data_path = fname_no_ext / 'data'
_temp = {}
for mag in data_path.glob('*.dat'):
d = AsciiReader(mag).export()
for v in d:
_temp[v.value] = v
break
return _temp
def _read_signals(self, region: tuple = None) -> dict:
start = 0
stop = 30e-5
# This is one set with attributes to find default start:stop values
try:
p = self.reader.parameters('/ABS_ACC_FID')
start = p['start']
stop = p['stop']
except:
pass
if region is None:
region = (start, stop)
if region[0] is None:
region = (start, region[1])
if region[1] is None:
region = (region[0], stop)
sig = self.reader.get_selected('/data/B=*/ACC_ABS_FID*', dtype='signal')
_temp = {}
for s in sig:
pts = s.points([region])
b = s.group
if b not in _temp:
_temp[b] = []
_temp[b].append([s.value, *[pp[1] for pp in pts]])
for b, m in sorted(_temp.items()):
m = np.array(m)
_temp[b] = Points(x=m[:, 0], y=m[:, 1], value=b, name=f'B={b}').sort()
return _temp
if __name__ == '__main__': if __name__ == '__main__':
from pprint import pprint test = FCReader('/autohome/dominik/nmreval/testdata/fc_test/314K_2015-09-03_1605.h5')
test = FCReader('/autohome/dominik/nmreval/testdata/fc_test/2022-09-15_1344_new_script.h5') test.load_magnetization(region=(None, None))
# test = FCReader('/autohome/dominik/nmreval/testdata/fc_test/314K_2015-09-03_1605.h5')
test.load_magnetization() # test = FCReader('/autohome/dominik/nmreval/testdata/fc_test/2023-03-16_1423.h5')
test.load_magnetization(region=(0, 10e-6)) # test.load_magnetization(region=(None, None))
# pprint(test.data) print(test.data)

View File

@ -27,7 +27,7 @@ class HdfNode:
'num_signals', 'num_pts', 'num_grp', 'num_signals', 'num_pts', 'num_grp',
'title_parameter', 'parameter'] 'title_parameter', 'parameter']
def __init__(self, name, ref, parent): def __init__(self, name: str, ref, parent: HdfNode | None):
self.name = name self.name = name
self.type = 'group' self.type = 'group'
self.reference = ref self.reference = ref
@ -59,7 +59,6 @@ class HdfNode:
return key in self.children return key in self.children
def clear(self): def clear(self):
self.name = '' self.name = ''
self.type = 'group' self.type = 'group'
@ -281,16 +280,16 @@ class HdfReader(HdfNode):
for child in val.data(dtype=dtype): for child in val.data(dtype=dtype):
try: try:
if child.type == 'points': if child.type == 'points':
ret_val.append(self._make_point(child)) ret_val.append(self.make_point(child))
elif child.type == 'signal': elif child.type == 'signal':
ret_val.append(self._make_signal(child, flag=flag, value=value, group=group)) ret_val.append(self.make_signal(child, flag=flag, value=value, group=group))
except IOError: except IOError:
print('something went wrong for ' + child.name) print('something went wrong for ' + child.name)
continue continue
return ret_val return ret_val
def _make_point(self, node): def make_point(self, node):
data = self.file[node.reference] data = self.file[node.reference]
val = None val = None
@ -300,7 +299,7 @@ class HdfReader(HdfNode):
return Points(x=data['x'], y=data['y'], yerr=data['y_err'], name=node.name, value=val) return Points(x=data['x'], y=data['y'], yerr=data['y_err'], name=node.name, value=val)
def _make_signal(self, node, flag: str = 'fid', value: str = None, group: str = None): def make_signal(self, node, flag: str = 'fid', value: str = None, group: str = None):
if value is None: if value is None:
value = self._get_parameter_values(node, node.parameter) value = self._get_parameter_values(node, node.parameter)
else: else: