From 16ccc3eb3d35425205364358c48ba8f3d6fd977a Mon Sep 17 00:00:00 2001 From: dominik Date: Mon, 4 Jul 2022 20:54:11 +0200 Subject: [PATCH] less buggy than before in dsc and t1 --- nmreval/gui_qt/io/dscreader.py | 117 +++++++++++++++++---------------- nmreval/gui_qt/nmr/t1widget.py | 2 +- nmreval/io/dsc.py | 10 +-- nmreval/version.py | 2 +- 4 files changed, 68 insertions(+), 63 deletions(-) diff --git a/nmreval/gui_qt/io/dscreader.py b/nmreval/gui_qt/io/dscreader.py index 42a4444..c272cfe 100644 --- a/nmreval/gui_qt/io/dscreader.py +++ b/nmreval/gui_qt/io/dscreader.py @@ -1,5 +1,6 @@ +from __future__ import annotations + from pathlib import Path -from typing import Union import numpy as np from pyqtgraph import PlotDataItem @@ -14,7 +15,7 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): data_read = QtCore.pyqtSignal(list) file_ext = ['.txt', '.dsc'] - def __init__(self, sample= None, empty=None, reference=None, parent=None): + def __init__(self, sample=None, empty=None, reference=None, parent=None): super().__init__(parent=parent) self.setupUi(self) @@ -62,7 +63,13 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): for r in reference: self.add_reference(ref=r) - def add_sample(self, fname: Union[Path, str]): + def __call__(self, fname: Path | str): + self.clear_plots() + self.add_sample(fname) + + return self + + def add_sample(self, fname: Path | str): self.sample = self.calibrator.set_measurement(fname, mode='sample') self.fname = self.sample.fname @@ -95,13 +102,13 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): @QtCore.pyqtSlot(name='on_delempty_button_clicked') def remove_empty(self): - self.empty_label.setText('Empty measurement') + self.empty_label.setText('No empty measurement') self.calibrator.empty = None self.update_plots() @QtCore.pyqtSlot(name='on_ref_add_pushButton_clicked') - def add_reference(self, ref=None): + def add_reference(self, ref: str | Path = None): if ref is None: ref, _ = QtWidgets.QFileDialog.getOpenFileName(directory=str(self.fname.parent)) @@ -127,9 +134,6 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): self.calibrator.remove_reference(self.reference_tableWidget.item(idx, 0).data(QtCore.Qt.UserRole)) self.reference_tableWidget.removeRow(idx) - - print(self.calibrator.reference) - self.update_plots() @QtCore.pyqtSlot(QtWidgets.QListWidgetItem, name='on_step_listWidget_itemChanged') @@ -153,16 +157,15 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): self.sample_idx = None self.clear_plots() - @QtCore.pyqtSlot(QtWidgets.QAbstractButton, name='on_buttonGroup_buttonClicked') - def update_plots(self, _=None): + def get_data(self, ): if self.sample_idx is None: return + rate = self.current_run[0] slope_type = {self.none_radioButton: None, self.isotherm_radioButton: 'iso', self.slope_radioButton: 'curve'}[self.buttonGroup.checkedButton()] - rate = self.current_run[0] try: raw_sample, drift_value, sample_data, empty_data, slope = self.calibrator.get_data(self.sample_idx, slope=slope_type) @@ -170,21 +173,38 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): _msg = QtWidgets.QMessageBox.warning(self, 'No rate found', e.args[0]) return - self.raw_sample.setData(x=raw_sample[0], y=raw_sample[1]) - self.drift_sample.setData(x=drift_value[0]/60., y=drift_value[1]) - self.slope_graph.setData(x=slope[0]/60., y=slope[1]) - - if empty_data is not None: - self.empty_sample.setData(x=empty_data[0], y=empty_data[1]) - self.calibrator.ref_list = [] for row in range(self.reference_tableWidget.rowCount()): self.calibrator.ref_list.append(self.reference_tableWidget.cellWidget(row, 1).get_reference()) - self.calib_graph.clear() - calib_x, calib_y, regions = self.calibrator.get_calibration(rate) + drift_value[0] /= 60. + slope[0] /= 60. + + if calib_x is not None: + sample_data[0] *= calib_x[0] + sample_data[0] += calib_x[1] + + if self.cp_checkBox.isChecked(): + sample_data[1] *= calib_y + + return sample_data, raw_sample, empty_data, drift_value, slope, calib_x, calib_y, regions + + @QtCore.pyqtSlot(QtWidgets.QAbstractButton, name='on_buttonGroup_buttonClicked') + @QtCore.pyqtSlot(int, name='on_cp_checkBox_stateChanged') + def update_plots(self, _=None): + sample_data, raw_sample, empty_data, drift_value, slope, calib_x, calib_y, regions = self.get_data() + + self.raw_sample.setData(x=raw_sample[0], y=raw_sample[1]) + self.drift_sample.setData(x=drift_value[0], y=drift_value[1]) + self.slope_graph.setData(x=slope[0], y=slope[1]) + + if empty_data is not None: + self.empty_sample.setData(x=empty_data[0], y=empty_data[1]) + + self.calib_graph.clear() + if calib_x is not None: for ref_zoom, onset, grad_points in regions: ref_plot = PlotDataItem(x=ref_zoom[0], y=ref_zoom[1]) @@ -200,14 +220,7 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): self.calib_graph.addItem(PlotDataItem(x=[ref_zoom[0, 0], ref_zoom[0, -1]], y=[0, 0], pen={'color': 'r', 'dash': (2, 5)})) - else: - calib_x = [1, 0] - calib_y = 1 - - if self.cp_checkBox.isChecked(): - self.baseline_sample.setData(x=calib_x[0]*sample_data[0]+calib_x[1], y=calib_y*sample_data[1]) - else: - self.baseline_sample.setData(x=calib_x[0]*sample_data[0]+calib_x[1], y=sample_data[1]) + self.baseline_sample.setData(x=sample_data[0], y=sample_data[1]) def clear_plots(self): for plot in [self.raw_sample, self.baseline_sample, self.empty_sample, self.drift_sample, self.slope_graph]: @@ -215,38 +228,30 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): self.calib_graph.clear() - def accept(self): - run_list = [] - for i in range(self.step_listWidget.count()): - if self.step_listWidget.item(i).checkState() == QtCore.Qt.Checked: - run_list.append(i) + def export_data(self, filesave=False, close_after=True): + try: + sample_data = self.get_data()[0] + except TypeError: + return - if self.baseline_groupBox.isChecked(): - empty = self.empty - slope = {self.none_radioButton: None, - self.isotherm_radioButton: 'iso', - self.slope_radioButton: 'heat'}[self.buttonGroup.checkedButton()] + rate, mode = self.current_run + new_val = Points(sample_data[0], sample_data[1], value=rate, name=f'{self.fname.stem} {rate} ({mode})') + + if filesave: + new_val.savetxt(self.fname.with_name(f'{self.fname.stem} {rate}K-min {mode}.dat'.replace(' ', '_'))) else: - empty = None - slope = None + self.data_read.emit([new_val]) - if self.reference_groupBox.isChecked(): - calibrations = [] - for j, ref in enumerate(self.references): - calibrations.append((ref, self.reference_tableWidget.cellWidget(j, 1).get_reference())) + if close_after: + super().accept() + + @QtCore.pyqtSlot(QtWidgets.QAbstractButton, name='on_buttonBox_clicked') + def button_clicked(self, bttn: QtWidgets.QAbstractButton): + bttn_value = self.buttonBox.standardButton(bttn) + if bttn_value in (self.buttonBox.Ok, self.buttonBox.Apply, self.buttonBox.Save): + self.export_data(filesave=bttn_value==self.buttonBox.Save, close_after=bttn_value==self.buttonBox.Ok) else: - calibrations = None - - new_vals = [] - - for run in run_list: - mode, rate, _, _ = self.sample.steps[run] - xy = self.sample.curve(rate, mode, empty=empty, slope=slope, calibration=calibrations) - new_vals.append(Points(xy[0], xy[1], value=rate, name=f'{self.fname.stem} {rate} ({mode})')) - - self.data_read.emit(new_vals) - - super().accept() + super().close() class ReferenceComboBox(QtWidgets.QComboBox): diff --git a/nmreval/gui_qt/nmr/t1widget.py b/nmreval/gui_qt/nmr/t1widget.py index e2b8edf..f615080 100644 --- a/nmreval/gui_qt/nmr/t1widget.py +++ b/nmreval/gui_qt/nmr/t1widget.py @@ -163,7 +163,7 @@ class QT1Widget(QtWidgets.QDialog, Ui_t1dialog): if self.coupling[idx].choice is not None: for (name, kw_name, opts) in self.coupling[idx].choice: widgt = SelectionWidget(name, kw_name, opts) - widgt.comboBox.currentIndexChanged.connect(self.update_coupling) + widgt.comboBox.currentIndexChanged.connect(self.update_coupling_parameter) self.verticalLayout_4.addWidget(widgt) self.cp_parameter.append(widgt) diff --git a/nmreval/io/dsc.py b/nmreval/io/dsc.py index 28ff16b..2017340 100644 --- a/nmreval/io/dsc.py +++ b/nmreval/io/dsc.py @@ -1,7 +1,7 @@ +from __future__ import annotations from pathlib import Path import re from collections import namedtuple -from typing import Union import numpy as np try: @@ -19,7 +19,7 @@ class DSCSample: DSC_RUN_RE = re.compile(r'(\d+)\) DSC') DSC_END_RE = re.compile(r'DSC8[50]00 MANUAL TUNE') - def __init__(self, fname: Union[str, Path]): + def __init__(self, fname: str | Path): fname = Path(fname) if fname.exists(): self.fname = fname @@ -32,7 +32,7 @@ class DSCSample: self.read_file(fname) - def read_file(self, fname: Union[str, Path]): + def read_file(self, fname: str | Path): fname = Path(fname) # file contains weird deg C character in stupiod ISO encoding @@ -144,7 +144,7 @@ class DSCCalibrator: self.ref_list = [] def set_measurement(self, - fname: Union[str, Path, DSCSample], mode: str = 'sample', + fname: str | Path | DSCSample, mode: str = 'sample', reference: ReferenceValue = Cyclohexane): if mode not in ['sample', 'empty', 'reference']: raise ValueError(f'Unknown mode {mode}, not "sample", "empty", "reference"') @@ -164,7 +164,7 @@ class DSCCalibrator: return fname - def remove_reference(self, fname: Union[str, Path]): + def remove_reference(self, fname: str | Path): fname = Path(fname) i = None for i, ref in enumerate(self.reference): diff --git a/nmreval/version.py b/nmreval/version.py index bd87b23..05c7787 100644 --- a/nmreval/version.py +++ b/nmreval/version.py @@ -1,3 +1,3 @@ # coding=utf-8 -__version__ = '0.1' +__version__ = '0.1.1' __releasename__ = 'Snobilikat'