From 2cf94af2c4f357c5d784276de24b690844655f40 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 28 Dec 2023 10:24:34 +0000 Subject: [PATCH] fix point selection for values with all the same x (#190) bugfix for issue #189; closes #189 Co-authored-by: Dominik Demuth Reviewed-on: https://gitea.pkm.physik.tu-darmstadt.de/IPKM/nmreval/pulls/190 --- src/gui_qt/main/management.py | 6 ++++-- src/nmreval/data/points.py | 33 ++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/gui_qt/main/management.py b/src/gui_qt/main/management.py index 69cb626..4b8b25c 100644 --- a/src/gui_qt/main/management.py +++ b/src/gui_qt/main/management.py @@ -742,8 +742,10 @@ class UpperManagement(QtCore.QObject): new_datasets[data_i.group] = [], [] new_x_axis, _temp = new_datasets[data_i.group] - new_x_axis.append(data_i.value) - _temp.append(data_i.points(params)) + pts = data_i.points(params) + if pts: + new_x_axis.append(data_i.value) + _temp.append(pts) key_list = [] for label, (new_x_axis, _temp) in new_datasets.items(): diff --git a/src/nmreval/data/points.py b/src/nmreval/data/points.py index a9d8cba..e4895f4 100644 --- a/src/nmreval/data/points.py +++ b/src/nmreval/data/points.py @@ -319,39 +319,46 @@ class Points: if pts is None: pts = [] + _tmp_x = self._x[self.mask] + _tmp_y = self._y[self.mask] if idx is not None: - for x in idx: - if isinstance(x, tuple): - x_idx = np.argmin(np.abs(self._x[self.mask] - (x[0]+x[1])/2)) - left_b = np.argmin(np.abs(self._x[self.mask] - x[0])) - right_b = np.argmin(np.abs(self._x[self.mask] - x[1])) + for idx_i in idx: + if isinstance(idx_i, tuple): + in_region = np.where((_tmp_x - idx_i[0] > 0) & (idx_i[1] - _tmp_x > 0))[0] + if len(in_region) > 0: + x_idx = in_region[in_region.size//2] + left_b = in_region[0] + right_b = in_region[-1] + 1 + else: + continue + else: - x_idx = np.argmin(np.abs(self._x[self.mask]-x)) + x_idx = np.argmin(np.abs(_tmp_x-idx_i)) left_b = int(max(0, x_idx - avg_range[0])) right_b = int(min(len(self), x_idx + avg_range[1] + 1)) if left_b < right_b: - pts.append([self._x[x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) + pts.append([_tmp_x[x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) else: - pts.append([self._x[x_idx], self._y[x_idx], self._y_err[x_idx]]) + pts.append([_tmp_x[x_idx], _tmp_y[x_idx], self._y_err[x_idx]]) if special is not None: if special not in ['max', 'min', 'absmax', 'absmin']: raise ValueError('Parameter "special" must be "max", "min", "absmax", "absmin".') if special == 'max': - x_idx = np.argmax(self._y.real[self.mask]) + x_idx = np.argmax(_tmp_y.real) elif special == 'min': - x_idx = np.argmax(self._y.real[self.mask]) + x_idx = np.argmax(_tmp_y.real) elif special == 'absmax': - x_idx = np.argmax(np.abs(self._y[self.mask])) + x_idx = np.argmax(np.abs(_tmp_y)) else: - x_idx = np.argmin(np.abs(self._y[self.mask])) + x_idx = np.argmin(np.abs(_tmp_y)) left_b = int(max(0, x_idx - avg_range[0])) right_b = int(min(len(self), x_idx + avg_range[1] + 1)) - pts.append([self._x[self.mask][x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) + pts.append([_tmp_x[x_idx], *self._average(avg_mode, x_idx, left_b, right_b)]) return pts