Merge branch 'read_fc'

This commit is contained in:
Dominik Demuth 2023-05-19 11:37:42 +02:00
commit 681b49a22f
2 changed files with 34 additions and 11 deletions

View File

@ -41,6 +41,7 @@ AppDir:
# - zsync # - zsync
# - hicolor-icon-theme # - hicolor-icon-theme
- libatlas3-base - libatlas3-base
- gnuplot-nox
- python3.9-minimal - python3.9-minimal
- python3-numpy - python3-numpy
- python3-scipy - python3-scipy

View File

@ -1,6 +1,8 @@
from __future__ import annotations from __future__ import annotations
import pathlib import pathlib
import subprocess
import tempfile
# import matplotlib.pyplot as plt # import matplotlib.pyplot as plt
from scipy.optimize import curve_fit from scipy.optimize import curve_fit
@ -9,6 +11,7 @@ import numpy as np
from nmreval.data.points import Points from nmreval.data.points import Points
from nmreval.io.asciireader import AsciiReader from nmreval.io.asciireader import AsciiReader
from nmreval.io.hdfreader import HdfReader from nmreval.io.hdfreader import HdfReader
from nmreval.lib.logger import logger
from nmreval.utils.utils import get_temperature, roundrobin from nmreval.utils.utils import get_temperature, roundrobin
@ -54,7 +57,7 @@ class FCReader:
_temp = self._read_from_dir(filename) _temp = self._read_from_dir(filename)
else: else:
raise TypeError raise TypeError(f'{filename} is of unknown type')
if not _temp: if not _temp:
raise OSError(-666, f'No magnetization found for {filename.name}.', filename.name) raise OSError(-666, f'No magnetization found for {filename.name}.', filename.name)
@ -178,6 +181,7 @@ class FCReader:
fit_path.mkdir(parents=True, exist_ok=True) fit_path.mkdir(parents=True, exist_ok=True)
if save_fig: if save_fig:
data_path = fname_no_ext.joinpath('data')
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)
@ -218,16 +222,34 @@ 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() img_file = image_path.joinpath(save_name).with_suffix(".png")
# ax.set_xlabel('t / s')
# ax.set_ylabel('M') gnuplot_args = [
# axheader = f'T1: {p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})' 'set terminal png;',
# ax.set_title(f'f = {k:.4g} Hz\n{axheader}') f'set output "{img_file}";',
# ax.semilogx(v.x, v.y, 'o') f'set title "f = {k:.4g} Hz\\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})";',
# ax.semilogx(xplot, yplot, '-') 'set xlabel "t / s";',
# fig.savefig(image_path.joinpath(save_name).with_suffix('.png')) 'set logscale x;',
# plt.close(fig) 'set format x "10^{{%L}}";',
'set ylabel "M";',
'set key off;',
f'plot "{data_path.joinpath(save_name)}" with points pointtype 5, "{fit_path.joinpath(save_name)}" with lines;',
]
try:
proc = subprocess.Popen(
['gnuplot', '-p'],
shell=True,
stdin=subprocess.PIPE,
encoding='utf8',
)
for args in gnuplot_args:
proc.stdin.write(args)
proc.stdin.write('quit\n')
proc.stdin.flush()
except Exception as e:
logger.error(f'saving image {save_name} failed', e)
freqs = np.asanyarray(freqs) freqs = np.asanyarray(freqs)
params = np.asanyarray(params) params = np.asanyarray(params)