From 2cdf4330af5751d2c94e2fdc5f84aacdb650da94 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Sat, 3 Feb 2024 12:24:51 +0100 Subject: [PATCH 1/2] check relative path; remove possible cause of #231 --- src/gui_qt/io/dscreader.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/gui_qt/io/dscreader.py b/src/gui_qt/io/dscreader.py index b3b0768..a9b83a0 100644 --- a/src/gui_qt/io/dscreader.py +++ b/src/gui_qt/io/dscreader.py @@ -78,14 +78,22 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): for opts in self.sample.steps: item = QtWidgets.QListWidgetItem() - item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable) - item.setCheckState(QtCore.Qt.Unchecked) + item.setFlags( + QtCore.Qt.ItemFlag.ItemIsEnabled | + QtCore.Qt.ItemFlag.ItemIsSelectable | + QtCore.Qt.ItemFlag.ItemIsUserCheckable + ) + item.setCheckState(QtCore.Qt.CheckState.Unchecked) if opts[0] == 'i': - item.setFlags(QtCore.Qt.NoItemFlags) + item.setFlags(QtCore.Qt.ItemFlag.NoItemFlags) item.setText(f'{opts[1]:.2f} K for {opts[2] / 60:.0f} min') else: - item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable) + item.setFlags( + QtCore.Qt.ItemFlag.ItemIsEnabled | + QtCore.Qt.ItemFlag.ItemIsSelectable | + QtCore.Qt.ItemFlag.ItemIsUserCheckable + ) item.setText(f'{opts[2]:.2f} K to {opts[3]:.2f} K with {opts[1]} K/min') self.step_listWidget.addItem(item) @@ -97,7 +105,12 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): if empty: self.empty = self.calibrator.set_measurement(empty, mode='empty') - self.empty_label.setText('~/' + str(self.empty.fname.relative_to(Path.home()))) + + # avoid ValueError breaking data update + if self.empty.fname.is_relative_to(Path.home()): + self.empty_label.setText('~/' + str(self.empty.fname.relative_to(Path.home()))) + else: + self.empty_label.setText(str(self.empty.fname)) self.update_plots() @@ -118,8 +131,8 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): self.references.append(ref) item = QtWidgets.QTableWidgetItem(str(ref.fname.name)) - item.setData(QtCore.Qt.UserRole, ref.fname) - item.setFlags(QtCore.Qt.ItemIsEnabled) + item.setData(QtCore.Qt.ItemDataRole.UserRole, ref.fname) + item.setFlags(QtCore.Qt.ItemFlag.ItemIsEnabled) rowcnt = self.reference_tableWidget.rowCount() self.reference_tableWidget.setRowCount(rowcnt+1) @@ -132,7 +145,7 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): @QtCore.pyqtSlot(name='on_ref_remove_pushButton_clicked') def remove_reference(self): idx = self.reference_tableWidget.currentRow() - self.calibrator.remove_reference(self.reference_tableWidget.item(idx, 0).data(QtCore.Qt.UserRole)) + self.calibrator.remove_reference(self.reference_tableWidget.item(idx, 0).data(QtCore.Qt.ItemDataRole.UserRole)) self.reference_tableWidget.removeRow(idx) self.update_plots() @@ -145,10 +158,10 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): for row in range(self.step_listWidget.count()): if idx == row: continue - self.step_listWidget.item(row).setCheckState(QtCore.Qt.Unchecked) + self.step_listWidget.item(row).setCheckState(QtCore.Qt.CheckState.Unchecked) self.step_listWidget.blockSignals(False) - if item.checkState() == QtCore.Qt.Checked: + if item.checkState() == QtCore.Qt.CheckState.Checked: mode, rate, _, _ = self.sample.steps[idx] self.current_run = (rate, mode) self.sample_idx = idx @@ -217,6 +230,8 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): if empty_data is not None: self.empty_sample.setData(x=empty_data[0], y=empty_data[1]) + else: + self.empty_sample.setData(x=[], y=[]) self.calib_graph.clear() -- 2.39.5 From e0a93e025a612afc9592234d22539e8da9373f41 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Tue, 6 Feb 2024 18:21:51 +0100 Subject: [PATCH 2/2] changed interpolation of empty dsc; closes #231 --- src/gui_qt/dsc/glass_dialog.py | 28 ++++++++++++++-------------- src/gui_qt/io/dscreader.py | 9 +++++++-- src/nmreval/io/dsc.py | 30 ++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/gui_qt/dsc/glass_dialog.py b/src/gui_qt/dsc/glass_dialog.py index f815b7a..b7afe09 100644 --- a/src/gui_qt/dsc/glass_dialog.py +++ b/src/gui_qt/dsc/glass_dialog.py @@ -136,8 +136,8 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): max_x = max(max_x, data.x.max()) item = QtWidgets.QListWidgetItem(name) - item.setCheckState(QtCore.Qt.Checked) - item.setData(QtCore.Qt.UserRole, key) + item.setCheckState(QtCore.Qt.CheckState.Checked) + item.setData(QtCore.Qt.ItemDataRole.UserRole, key) item.setForeground(mkBrush(c.rgb())) self.listWidget.addItem(item) @@ -191,10 +191,10 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): for idx in range(self.listWidget.count()): item = self.listWidget.item(idx) - if item.checkState() == QtCore.Qt.Unchecked: + if item.checkState() == QtCore.Qt.CheckState.Unchecked: continue - key = item.data(QtCore.Qt.UserRole) + key = item.data(QtCore.Qt.ItemDataRole.UserRole) plot = self._plots[key] data, _ = self._dsc[key] @@ -214,7 +214,7 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): item = self.listWidget.item(idx) tree_item = QtWidgets.QTreeWidgetItem([item.text()]) - values = self._tg_value.get(item.data(QtCore.Qt.UserRole)) + values = self._tg_value.get(item.data(QtCore.Qt.ItemDataRole.UserRole)) if values is not None: for name, pos in values.items(): @@ -223,7 +223,7 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): self.tg_tree.addTopLevelItem(tree_item) - key = item.data(QtCore.Qt.UserRole) + key = item.data(QtCore.Qt.ItemDataRole.UserRole) plot = self._plots[key] data, _ = self._dsc[key] @@ -251,7 +251,7 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): @QtCore.pyqtSlot(QtWidgets.QListWidgetItem) def change_visibility(self, item: QtWidgets.QListWidgetItem): is_checked = bool(item.checkState()) - plot = self._plots[item.data(QtCore.Qt.UserRole)] + plot = self._plots[item.data(QtCore.Qt.ItemDataRole.UserRole)] for val in plot: val.setVisible(is_checked) @@ -275,10 +275,10 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): self.tnmh_tree.clear() for idx in range(self.listWidget.count()): item = self.listWidget.item(idx) - if item.checkState() == QtCore.Qt.Unchecked: + if item.checkState() == QtCore.Qt.CheckState.Unchecked: continue - key = item.data(QtCore.Qt.UserRole) + key = item.data(QtCore.Qt.ItemDataRole.UserRole) data = self.get_fictive(key, baselines) @@ -292,7 +292,7 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): item = self.listWidget.item(idx) tree_item = QtWidgets.QTreeWidgetItem([item.text()]) - values = self._fit.get(item.data(QtCore.Qt.UserRole)) + values = self._fit.get(item.data(QtCore.Qt.ItemDataRole.UserRole)) if values is not None: child_item = QtWidgets.QTreeWidgetItem([values.parameter_string()]) @@ -305,10 +305,10 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): ret_dic = {} for idx in range(self.listWidget.count()): item = self.listWidget.item(idx) - if item.checkState() == QtCore.Qt.Unchecked: + if item.checkState() == QtCore.Qt.CheckState.Unchecked: continue - key = item.data(QtCore.Qt.UserRole) + key = item.data(QtCore.Qt.ItemDataRole.UserRole) cp = None if self.fictive_export_check.isChecked(): @@ -332,10 +332,10 @@ class TgCalculator(QtWidgets.QWizard, Ui_DSCEvalDialog): m = [] for idx in range(self.listWidget.count()): item = self.listWidget.item(idx) - if item.checkState() == QtCore.Qt.Unchecked: + if item.checkState() == QtCore.Qt.CheckState.Unchecked: continue - key = item.data(QtCore.Qt.UserRole) + key = item.data(QtCore.Qt.ItemDataRole.UserRole) data, _ = self._dsc[key] try: tg_value = self._tg_value[key][tg_type][0] diff --git a/src/gui_qt/io/dscreader.py b/src/gui_qt/io/dscreader.py index a9b83a0..1ebda46 100644 --- a/src/gui_qt/io/dscreader.py +++ b/src/gui_qt/io/dscreader.py @@ -264,11 +264,16 @@ class QDSCReader(QtWidgets.QDialog, Ui_Dialog): except TypeError: return + if self.cp_checkBox.isChecked() and self.references: + y_label = 'cp' + else: + y_label = 'q' + rate, mode = self.current_run - new_val = DSC(sample_data[0], sample_data[1], value=rate, name=f'{self.fname.stem} {rate} ({mode})') + new_val = DSC(sample_data[0], sample_data[1], value=rate, name=f'{self.fname.stem} {rate}K-min ({mode}, {y_label})') if filesave: - new_val.savetxt(self.fname.with_name(f'{self.fname.stem} {rate}K-min {mode}.dat'.replace(' ', '_'))) + new_val.savetxt(self.fname.with_name(f'{self.fname.stem}_{rate}K-min_{y_label}{mode}.dat'.replace(' ', '_'))) close_after = False else: self.data_read.emit([new_val]) diff --git a/src/nmreval/io/dsc.py b/src/nmreval/io/dsc.py index b531bd4..4aaf606 100644 --- a/src/nmreval/io/dsc.py +++ b/src/nmreval/io/dsc.py @@ -11,7 +11,8 @@ try: from scipy.integrate import simpson except ImportError: from scipy.integrate import simps as simpson -from scipy.interpolate import interp1d +from scipy.interpolate import CubicSpline + ReferenceValue = namedtuple('Reference', ['name', 'transitions']) Cyclohexane = ReferenceValue('Cyclohexane', [(-87.06+273.15, 79.58), (6.54+273.15, None)]) @@ -38,7 +39,7 @@ class DSCSample: def read_file(self, fname: str | Path) -> None: fname = Path(fname) - # file contains weird deg C character in stupiod ISO encoding + # file contains weird deg C character in stupid ISO encoding with fname.open('r', encoding='iso-8859-15') as f: ii = 1 for line in f: @@ -144,9 +145,12 @@ class DSCCalibrator: self.reference = [] self.ref_list = [] - def set_measurement(self, - fname: str | Path | DSCSample, mode: str = 'sample', - reference: ReferenceValue = Cyclohexane): + def set_measurement( + self: DSCCalibrator, + 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"') if mode == 'reference' and not isinstance(reference, ReferenceValue): @@ -266,7 +270,12 @@ class DSCCalibrator: return sol - def get_data(self, idx: int, slope: str = 'iso', limits: tuple[float, float] = None): + def get_data( + self: DSCCalibrator, + idx: int, + slope: str = 'iso', + limits: tuple[float, float] = None + ) -> tuple[np.ndarray, np.ndarray, np.ndarray,np.ndarray | None, np.ndarray]: if self.sample.steps[idx][0] == 'i': raise ValueError('baseline correction is not implemented for isotherms') @@ -292,7 +301,7 @@ class DSCCalibrator: empty_y = empty_data[1] if self.sample.length(idx) != self.empty.length(idx_empty): with np.errstate(all='ignore'): - empty_y = interp1d(empty_data[2]-empty_data[2, 0], empty_data[1], fill_value='extrapolate')(sample_data[2, 0]) + empty_y = CubicSpline(empty_data[2]-empty_data[2, 0], empty_data[1], extrapolate=True)(sample_data[2] - sample_data[2, 0]) sample_data[1] -= empty_y drift_value = sample_data.copy()[(2, 1), :] @@ -346,9 +355,10 @@ class DSCCalibrator: offset = region[0, 0] sample_data[1] -= m * (sample_data[2] - region[1, 0]) + offset - line = np.array([[sample_data[2, 0], sample_data[2, -1]], - [m * (sample_data[2, 0] - region[1, 0]) + offset, - m * (sample_data[2, -1] - region[1, 0]) + offset]]) + line = np.array([ + [sample_data[2, 0], sample_data[2, -1]], + [m * (sample_data[2, 0] - region[1, 0]) + offset, m * (sample_data[2, -1] - region[1, 0]) + offset] + ]) else: line = np.array([[sample_data[2, 0], sample_data[2, -1]], [0, 0]]) -- 2.39.5