ensure sorted sets before averaging in pick points #226

Merged
dominik merged 1 commits from 189-wrong-mean into master 2024-01-30 17:01:13 +00:00
2 changed files with 27 additions and 14 deletions
Showing only changes of commit 40bee320e6 - Show all commits

View File

@ -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

View File

@ -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):
"""