From 354d5cbc994ac0ce1de39729fcb96552aa8003fc Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 19 Jun 2023 20:03:21 +0200 Subject: [PATCH] generalize binning --- src/gui_qt/data/container.py | 4 ++-- src/gui_qt/dsc/glass_dialog.py | 4 ++-- src/gui_qt/main/mainwindow.py | 2 +- src/gui_qt/main/management.py | 2 +- src/gui_qt/math/binning.py | 8 ++++---- src/nmreval/data/points.py | 16 ++++++++++------ 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/gui_qt/data/container.py b/src/gui_qt/data/container.py index ec4563f..12375ff 100644 --- a/src/gui_qt/data/container.py +++ b/src/gui_qt/data/container.py @@ -480,9 +480,9 @@ class ExperimentContainer(QtCore.QObject): return new_data - def binning(self, digits: int): + def binning(self, digits: float): new_data = self.copy(full=True) - new_data.data = self._data.binning(decimals=digits) + new_data.data = self._data.binning(value=digits) return new_data diff --git a/src/gui_qt/dsc/glass_dialog.py b/src/gui_qt/dsc/glass_dialog.py index f35119e..7dd342b 100644 --- a/src/gui_qt/dsc/glass_dialog.py +++ b/src/gui_qt/dsc/glass_dialog.py @@ -67,9 +67,9 @@ class TgCalculator(QtWidgets.QDialog, Ui_Dialog): self.dsc_plot.removeItem(val) self.graphicsView_2.removeItem(val) - for plt in self._hodge.values(): + for key, plt in self._hodge.items(): plt[0].setData(x=[], y=[]) - plt[1] = None + self._hodge[key] = (plt[0], None) self._dsc = {} self._plots = {} diff --git a/src/gui_qt/main/mainwindow.py b/src/gui_qt/main/mainwindow.py index 502f2a6..5453cff 100644 --- a/src/gui_qt/main/mainwindow.py +++ b/src/gui_qt/main/mainwindow.py @@ -702,7 +702,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): dialog = BinningWindow(self) res = dialog.exec() if res: - digits = dialog.spinbox.value() + digits = float(dialog.spinbox.text()) self.management.binning(digits) @QtCore.pyqtSlot(name='on_actionDerivation_triggered') diff --git a/src/gui_qt/main/management.py b/src/gui_qt/main/management.py index 27d7d75..b47732e 100644 --- a/src/gui_qt/main/management.py +++ b/src/gui_qt/main/management.py @@ -766,7 +766,7 @@ class UpperManagement(QtCore.QObject): self.newData.emit(new_key, dest_graph) - def binning(self, digits: int): + def binning(self, digits: float): _active = self.graphs[self.current_graph].active new_data = [] for sid in _active: diff --git a/src/gui_qt/math/binning.py b/src/gui_qt/math/binning.py index 5bafd16..cc38f87 100644 --- a/src/gui_qt/math/binning.py +++ b/src/gui_qt/math/binning.py @@ -1,4 +1,4 @@ -from ..Qt import QtWidgets +from ..Qt import QtWidgets, QtGui class BinningWindow(QtWidgets.QDialog): @@ -8,9 +8,9 @@ class BinningWindow(QtWidgets.QDialog): layout = QtWidgets.QFormLayout() self.label = QtWidgets.QLabel('Digits (negative values position left of decimal point)') - self.spinbox = QtWidgets.QSpinBox() - self.spinbox.setMinimum(-10) - self.spinbox.setMaximum(10) + self.spinbox = QtWidgets.QLineEdit() + self.spinbox.setValidator(QtGui.QDoubleValidator()) + self.spinbox.setText('1') layout.addRow(self.label, self.spinbox) self.dialogbox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) diff --git a/src/nmreval/data/points.py b/src/nmreval/data/points.py index 1444aaf..8744ad7 100644 --- a/src/nmreval/data/points.py +++ b/src/nmreval/data/points.py @@ -1,6 +1,7 @@ from __future__ import annotations import copy +from math import log10 from numbers import Number, Real from pathlib import Path from typing import Any, TypeVar @@ -541,16 +542,19 @@ class Points: return self - def binning(self, decimals=2): + def binning(self, value: float): + if value <= 0: + raise ValueError('value must be a positive number') + copy = self.copy() - upper_lim = np.round(self.x[-1], decimals=decimals) - lower_lim = np.round(self.x[0], decimals=decimals) + upper_lim = (self.x[-1]//value + 1) * value + lower_lim = (self.x[0]//value) * value - tens = 10**decimals - offset = 0.5 / tens + offset = value / 2 + + xbins = np.linspace(lower_lim - offset, upper_lim + offset, num=int((upper_lim-lower_lim)/value + 2)) - xbins = np.linspace(lower_lim - offset, upper_lim + offset, num=int(tens * (upper_lim-lower_lim)+2)) n, _ = np.histogram(copy.x, bins=xbins) sum_y, _ = np.histogram(copy.x, bins=xbins, weights=copy.y) sum_yerr_2, _ = np.histogram(copy.x, bins=xbins, weights=copy.y_err**2)