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.