From 233cdd9f80fb717c4e18869cd6e1a45aa0e81cc1 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Wed, 10 May 2023 17:09:11 +0200 Subject: [PATCH 1/3] add gnuplot to requirements for plots of FC reading --- AppImageBuilder.yml | 3 ++- src/nmreval/io/fcbatchreader.py | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/AppImageBuilder.yml b/AppImageBuilder.yml index 80b4572..fb8b178 100644 --- a/AppImageBuilder.yml +++ b/AppImageBuilder.yml @@ -41,6 +41,7 @@ AppDir: # - zsync # - hicolor-icon-theme - libatlas3-base + - gnuplot-nox - python3.9-minimal - python3-numpy - python3-scipy @@ -70,7 +71,7 @@ AppDir: - usr/share/doc/*/README.* - usr/share/doc/*/changelog.* - usr/share/doc/*/NEWS.* - - usr/share/doc/*/TODO.}* + - usr/share/doc/*/TODO.* runtime: # if needed, apparently replaces hardcoded location with APPDIR location # path_mappings: diff --git a/src/nmreval/io/fcbatchreader.py b/src/nmreval/io/fcbatchreader.py index b3168cf..f221bfe 100644 --- a/src/nmreval/io/fcbatchreader.py +++ b/src/nmreval/io/fcbatchreader.py @@ -1,6 +1,8 @@ from __future__ import annotations import pathlib +import subprocess +import tempfile # import matplotlib.pyplot as plt from scipy.optimize import curve_fit @@ -178,6 +180,7 @@ class FCReader: fit_path.mkdir(parents=True, exist_ok=True) if save_fig: + data_path = fname_no_ext.joinpath('data') image_path = fname_no_ext.joinpath('png') image_path.mkdir(parents=True, exist_ok=True) @@ -209,7 +212,7 @@ class FCReader: except KeyError: self.f_params[k] = [new_entry] - if save_fits or save_fig: + if True: # save_fits or save_fig: xplot = np.geomspace(v.x[0], v.x[-1], num=10*len(v.x)) yplot = FCReader.kww(xplot, *p0) save_name = f'{filename.stem}_{k:011.2f}'.replace('.', 'p') + '.dat' @@ -218,6 +221,26 @@ class FCReader: 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)])) + img_file = image_path.joinpath(save_name).with_suffix(".png") + + gnuplot_args = [ + 'gnuplot', + '-e', + 'set terminal png', + f'set output "{img_file}"', + f'set title "f = {k:.4g} Hz\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})"', + 'set xlabel "t / s"', + 'set logscale x', + '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', + ] + + print(gnuplot_args) + + subprocess.Popen(gnuplot_args) + # if save_fig: # fig, ax = plt.subplots() # ax.set_xlabel('t / s') From 6e9dd4d45f8055dc9b55898e425ba375089d33b6 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Fri, 19 May 2023 11:33:02 +0200 Subject: [PATCH 2/3] use gnuplot as lightweight plotter --- src/nmreval/io/fcbatchreader.py | 52 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/nmreval/io/fcbatchreader.py b/src/nmreval/io/fcbatchreader.py index f221bfe..9bb09fb 100644 --- a/src/nmreval/io/fcbatchreader.py +++ b/src/nmreval/io/fcbatchreader.py @@ -11,6 +11,7 @@ import numpy as np from nmreval.data.points import Points from nmreval.io.asciireader import AsciiReader from nmreval.io.hdfreader import HdfReader +from nmreval.lib.logger import logger from nmreval.utils.utils import get_temperature, roundrobin @@ -56,7 +57,7 @@ class FCReader: _temp = self._read_from_dir(filename) else: - raise TypeError + raise TypeError(f'{filename} is of unknown type') if not _temp: raise OSError(-666, f'No magnetization found for {filename.name}.', filename.name) @@ -212,7 +213,7 @@ class FCReader: except KeyError: self.f_params[k] = [new_entry] - if True: # save_fits or save_fig: + if True: # save_fits or save_fig: xplot = np.geomspace(v.x[0], v.x[-1], num=10*len(v.x)) yplot = FCReader.kww(xplot, *p0) save_name = f'{filename.stem}_{k:011.2f}'.replace('.', 'p') + '.dat' @@ -224,33 +225,30 @@ class FCReader: img_file = image_path.joinpath(save_name).with_suffix(".png") gnuplot_args = [ - 'gnuplot', - '-e', - 'set terminal png', - f'set output "{img_file}"', - f'set title "f = {k:.4g} Hz\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})"', - 'set xlabel "t / s"', - 'set logscale x', - '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', + 'set terminal png;', + f'set output "{img_file}";', + f'set title "f = {k:.4g} Hz\\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})";', + 'set xlabel "t / s";', + 'set logscale x;', + '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;', ] - print(gnuplot_args) - - subprocess.Popen(gnuplot_args) - - # if save_fig: - # fig, ax = plt.subplots() - # ax.set_xlabel('t / s') - # ax.set_ylabel('M') - # 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.semilogx(v.x, v.y, 'o') - # ax.semilogx(xplot, yplot, '-') - # fig.savefig(image_path.joinpath(save_name).with_suffix('.png')) - # plt.close(fig) + 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) params = np.asanyarray(params) From 3dcd44c3eefc7d2ed911622f31a235e86e3a0db0 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Fri, 19 May 2023 11:37:24 +0200 Subject: [PATCH 3/3] restore optionality to figure saving --- src/nmreval/io/fcbatchreader.py | 53 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/nmreval/io/fcbatchreader.py b/src/nmreval/io/fcbatchreader.py index 9bb09fb..f67a661 100644 --- a/src/nmreval/io/fcbatchreader.py +++ b/src/nmreval/io/fcbatchreader.py @@ -213,7 +213,7 @@ class FCReader: except KeyError: self.f_params[k] = [new_entry] - if True: # save_fits or save_fig: + if save_fits or save_fig: xplot = np.geomspace(v.x[0], v.x[-1], num=10*len(v.x)) yplot = FCReader.kww(xplot, *p0) save_name = f'{filename.stem}_{k:011.2f}'.replace('.', 'p') + '.dat' @@ -222,33 +222,34 @@ class FCReader: 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)])) - img_file = image_path.joinpath(save_name).with_suffix(".png") + if save_fig: + img_file = image_path.joinpath(save_name).with_suffix(".png") - gnuplot_args = [ - 'set terminal png;', - f'set output "{img_file}";', - f'set title "f = {k:.4g} Hz\\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})";', - 'set xlabel "t / s";', - 'set logscale x;', - '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;', - ] + gnuplot_args = [ + 'set terminal png;', + f'set output "{img_file}";', + f'set title "f = {k:.4g} Hz\\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})";', + 'set xlabel "t / s";', + 'set logscale x;', + '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) + 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) params = np.asanyarray(params)