From 3626cfc7ea7b1c18d080a0896cc66b0e8d873e7d Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Tue, 30 Jan 2024 17:01:13 +0000 Subject: [PATCH] ensure sorted sets before averaging in pick points (#226) should finally fix #189 --- .gitea/ISSUE_TEMPLATE/BUG_TEMPLATE.yml | 2 +- src/nmreval/data/points.py | 39 +++++++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/.gitea/ISSUE_TEMPLATE/BUG_TEMPLATE.yml b/.gitea/ISSUE_TEMPLATE/BUG_TEMPLATE.yml index c81f466..f7dc7d4 100644 --- a/.gitea/ISSUE_TEMPLATE/BUG_TEMPLATE.yml +++ b/.gitea/ISSUE_TEMPLATE/BUG_TEMPLATE.yml @@ -17,7 +17,7 @@ body: description: For which version have you observed this behavior? placeholder: You find the program version in "Help/About" validations: - required: true + required: false - type: textarea attributes: label: Expected behavior diff --git a/src/nmreval/data/points.py b/src/nmreval/data/points.py index b394efa..8332084 100644 --- a/src/nmreval/data/points.py +++ b/src/nmreval/data/points.py @@ -320,7 +320,11 @@ class Points: pts = [] _tmp_x = self._x[self.mask] - _tmp_y = self._y[self.mask] + x_order = np.argsort(_tmp_x) + _tmp_x = _tmp_x[x_order] + _tmp_y = self._y[self.mask][x_order] + _tmp_yerr = self._y_err[self.mask][x_order] + if idx is not None: for idx_i in idx: if isinstance(idx_i, tuple): @@ -338,7 +342,7 @@ class Points: right_b = int(min(len(self), x_idx + avg_range[1] + 1)) if left_b < right_b: - pts.append([_tmp_x[x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) + pts.append([_tmp_x[x_idx], *self._average(_tmp_x, _tmp_y, _tmp_yerr, avg_mode, x_idx, left_b, right_b)]) else: pts.append([_tmp_x[x_idx], _tmp_y[x_idx], self._y_err[x_idx]]) @@ -358,28 +362,37 @@ class Points: left_b = int(max(0, x_idx - avg_range[0])) right_b = int(min(len(self), x_idx + avg_range[1] + 1)) - pts.append([_tmp_x[x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) + pts.append([_tmp_x[x_idx], *self._average(_tmp_x, _tmp_y, _tmp_yerr, avg_mode, x_idx, left_b, right_b)]) return pts - def _average(self, mode: str, idx, left: int, right: int) -> tuple[float, float]: + @staticmethod + def _average( + x: np.ndarray, + y: np.ndarray, + y_err: np.ndarray, + mode: str, + idx: int, + left: int, + right: int, + ) -> tuple[float, float]: if mode == 'mean': - y_mean = np.mean(self._y[self.mask][left:right].real) - y_err = np.linalg.norm(self._y_err[self.mask][left:right]) / (right - left) + y_mean = np.mean(y[left:right].real) + y_err_mean = np.linalg.norm(y_err[left:right]) / (right - left) elif mode == 'sum': - y_mean = np.sum(self._y[self.mask][left:right].real) - y_err = np.linalg.norm(self._y_err[self.mask][left:right]) + y_mean = np.sum(y[left:right].real) + y_err_mean = np.linalg.norm(y_err[left:right]) elif mode == 'integral': - y_mean = simpson(self._y[self.mask][left:right].real, x=self._x[left:right]) - y_err = np.linalg.norm(cumulative_trapezoid(self._y_err[self.mask][left:right].real, x=self._x[left:right])) + y_mean = simpson(y[left:right].real, x=x[left:right]) + y_err_mean = np.linalg.norm(cumulative_trapezoid(y_err[left:right].real, x=x[left:right])) else: - y_mean = self._y[self.mask][idx].real - y_err = self._y_err[self.mask][idx] + y_mean = y[idx].real + y_err_mean = y_err[idx] - return y_mean, y_err + return y_mean, y_err_mean def concatenate(self, other): """