From 8d5543024633498a1541fbb2b2919bc31a6ce1d2 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 22 May 2023 18:49:27 +0200 Subject: [PATCH] binning added to points --- src/nmreval/data/points.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/nmreval/data/points.py b/src/nmreval/data/points.py index 3282627..1444aaf 100644 --- a/src/nmreval/data/points.py +++ b/src/nmreval/data/points.py @@ -488,7 +488,7 @@ class Points: x = self._x if y is None: y = self._y - if y_err is not None: + if y_err is None: y_err = self._y_err self._x, self._y, self._y_err, self.mask = self._prepare_xy(x, y, y_err) @@ -541,6 +541,31 @@ class Points: return self + def binning(self, decimals=2): + copy = self.copy() + + upper_lim = np.round(self.x[-1], decimals=decimals) + lower_lim = np.round(self.x[0], decimals=decimals) + + tens = 10**decimals + offset = 0.5 / tens + + 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) + + isnan = n != 0 + + n = n[isnan] + sum_y = sum_y[isnan] + sum_yerr_2 = sum_yerr_2[isnan] + xaxis = (xbins[:-1] + offset)[isnan] + + copy.set_data(xaxis, sum_y/n, y_err=np.sqrt(sum_yerr_2/n)) + + return copy + def shift(self, points: int) -> PointLike: """ Shift indexes of y values.