generalize binning

This commit is contained in:
Dominik Demuth 2023-06-19 20:03:21 +02:00
parent 84d588cf80
commit 354d5cbc99
6 changed files with 20 additions and 16 deletions

View File

@ -480,9 +480,9 @@ class ExperimentContainer(QtCore.QObject):
return new_data return new_data
def binning(self, digits: int): def binning(self, digits: float):
new_data = self.copy(full=True) 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 return new_data

View File

@ -67,9 +67,9 @@ class TgCalculator(QtWidgets.QDialog, Ui_Dialog):
self.dsc_plot.removeItem(val) self.dsc_plot.removeItem(val)
self.graphicsView_2.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[0].setData(x=[], y=[])
plt[1] = None self._hodge[key] = (plt[0], None)
self._dsc = {} self._dsc = {}
self._plots = {} self._plots = {}

View File

@ -702,7 +702,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
dialog = BinningWindow(self) dialog = BinningWindow(self)
res = dialog.exec() res = dialog.exec()
if res: if res:
digits = dialog.spinbox.value() digits = float(dialog.spinbox.text())
self.management.binning(digits) self.management.binning(digits)
@QtCore.pyqtSlot(name='on_actionDerivation_triggered') @QtCore.pyqtSlot(name='on_actionDerivation_triggered')

View File

@ -766,7 +766,7 @@ class UpperManagement(QtCore.QObject):
self.newData.emit(new_key, dest_graph) self.newData.emit(new_key, dest_graph)
def binning(self, digits: int): def binning(self, digits: float):
_active = self.graphs[self.current_graph].active _active = self.graphs[self.current_graph].active
new_data = [] new_data = []
for sid in _active: for sid in _active:

View File

@ -1,4 +1,4 @@
from ..Qt import QtWidgets from ..Qt import QtWidgets, QtGui
class BinningWindow(QtWidgets.QDialog): class BinningWindow(QtWidgets.QDialog):
@ -8,9 +8,9 @@ class BinningWindow(QtWidgets.QDialog):
layout = QtWidgets.QFormLayout() layout = QtWidgets.QFormLayout()
self.label = QtWidgets.QLabel('Digits (negative values position left of decimal point)') self.label = QtWidgets.QLabel('Digits (negative values position left of decimal point)')
self.spinbox = QtWidgets.QSpinBox() self.spinbox = QtWidgets.QLineEdit()
self.spinbox.setMinimum(-10) self.spinbox.setValidator(QtGui.QDoubleValidator())
self.spinbox.setMaximum(10) self.spinbox.setText('1')
layout.addRow(self.label, self.spinbox) layout.addRow(self.label, self.spinbox)
self.dialogbox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) self.dialogbox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import copy import copy
from math import log10
from numbers import Number, Real from numbers import Number, Real
from pathlib import Path from pathlib import Path
from typing import Any, TypeVar from typing import Any, TypeVar
@ -541,16 +542,19 @@ class Points:
return self 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() copy = self.copy()
upper_lim = np.round(self.x[-1], decimals=decimals) upper_lim = (self.x[-1]//value + 1) * value
lower_lim = np.round(self.x[0], decimals=decimals) lower_lim = (self.x[0]//value) * value
tens = 10**decimals offset = value / 2
offset = 0.5 / tens
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) n, _ = np.histogram(copy.x, bins=xbins)
sum_y, _ = np.histogram(copy.x, bins=xbins, weights=copy.y) 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) sum_yerr_2, _ = np.histogram(copy.x, bins=xbins, weights=copy.y_err**2)