From afdfeae42b1d22387c342886c1a6fcef4ab47577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Mon, 2 Dec 2024 21:56:10 +0100 Subject: [PATCH] make sure 2D navigation spread data are displayed using triangulation if possible --- src/pymodaq_gui/plotting/data_viewers/base.py | 2 +- .../plotting/data_viewers/viewerND.py | 51 +++++++++++++++++-- .../data_viewers_test/viewerND_test.py | 36 +++++++++++++ 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/pymodaq_gui/plotting/data_viewers/base.py b/src/pymodaq_gui/plotting/data_viewers/base.py index adbf239b..1c1c75d0 100644 --- a/src/pymodaq_gui/plotting/data_viewers/base.py +++ b/src/pymodaq_gui/plotting/data_viewers/base.py @@ -273,7 +273,7 @@ def activate_roi(self, activate=True): raise NotImplementedError def setVisible(self, show=True): - """convenience method to show or hide the paretn widget""" + """convenience method to show or hide the parent widget""" self.parent.setVisible(show) @property diff --git a/src/pymodaq_gui/plotting/data_viewers/viewerND.py b/src/pymodaq_gui/plotting/data_viewers/viewerND.py index a7cd40cd..ba4dac09 100644 --- a/src/pymodaq_gui/plotting/data_viewers/viewerND.py +++ b/src/pymodaq_gui/plotting/data_viewers/viewerND.py @@ -399,7 +399,9 @@ def update_nav_data(self, x, y, width=None, height=None): self._navigator1D.show_data(nav_data) elif len(nav_axes) == 2: try: - Triangulation(np.array([axis.get_data() for axis in nav_data.get_nav_axes()])) + self._navigator2D.setVisible(True) + self._navigator1D.setVisible(False) + #Triangulation(np.array([axis.get_data() for axis in nav_data.get_nav_axes()])) self._navigator2D.show_data(nav_data) except QhullError as e: self.triangulation = False @@ -683,9 +685,10 @@ def update_widget_visibility(self, data: DataRaw = None, self.axes_viewer.setVisible(len(nav_indexes) > 2 or ( len(nav_indexes) == 2 and not data.check_axes_linear())) elif data.distribution.name == DataDistribution.spread: - self.navigator2D.setVisible(len(nav_indexes) == 2 and self.data_displayer.triangulation) - self.navigator1D.setVisible(len(nav_indexes) == 1 or len(nav_indexes) > 2 or - len(nav_indexes) == 2 and + n_nav_axes = len(data.get_nav_axes()) + self.navigator2D.setVisible(n_nav_axes == 2 and self.data_displayer.triangulation) + self.navigator1D.setVisible(n_nav_axes == 1 or n_nav_axes > 2 or + n_nav_axes == 2 and not self.data_displayer.triangulation) else: raise ValueError(f'Unknown distribution: {data.distribution.name}') @@ -801,6 +804,44 @@ def main(): sys.exit(app.exec_()) +def spread_main(): + from pymodaq_data.data import DataRaw, Axis + app = QtWidgets.QApplication(sys.argv) + widget = QtWidgets.QWidget() + prog = ViewerND(widget) + + param = np.linspace(1, 10, 10) + + # generating Npts of spread 2D data + N = 100 + + x_axis_array = np.random.randint(-20, 50, size=N) + y_axis_array = np.random.randint(20, 40, size=N) + + x_axis = Axis('xaxis', 'm', data=x_axis_array, index=0, spread_order=0) + y_axis = Axis('yaxis', 'm', data=y_axis_array, index=0, spread_order=1) + param_axis = Axis('Param', 'eV', data=param, index=1) + + data_spread_1x2 = [] + for i in param: + data_list = [] + for ind in range(N): + data_list.append(mutils.gauss2D(x_axis.get_data()[ind], 10 + i, 15, + y_axis.get_data()[ind], 30 - i, 5)) + data_array = np.squeeze(np.array(data_list)) + data_spread_1x2.append(data_array) + data_spread_1x2 = np.array(data_spread_1x2).T + + data_2D_1x2_spread = DataRaw('data2DSpread', data=[data_spread_1x2], + axes=[x_axis, y_axis, param_axis], + distribution='spread', + nav_indexes=(0,)) + + widget.show() + prog.show_data(data_2D_1x2_spread) + sys.exit(app.exec_()) + + if __name__ == '__main__': - main() + spread_main() diff --git a/tests/plotting_test/data_viewers_test/viewerND_test.py b/tests/plotting_test/data_viewers_test/viewerND_test.py index 8a9ef4a5..5c628267 100644 --- a/tests/plotting_test/data_viewers_test/viewerND_test.py +++ b/tests/plotting_test/data_viewers_test/viewerND_test.py @@ -5,6 +5,7 @@ from pymodaq_gui.plotting.data_viewers.viewerND import ViewerND from pymodaq_data import data as data_mod +from pymodaq_utils import math_utils as mutils @pytest.fixture @@ -36,3 +37,38 @@ def test_linear_spread(self, init_viewernd): viewer.show_data(data_spread) pass + + def test_2D_spread_1D_signal(self, init_viewernd): + viewer = init_viewernd + + # Parameter + param = np.linspace(1, 10, 10) + + # generating Npts of spread 2D data + N = 200 + + x_axis_array = np.random.randint(-20, 50, size=N) + y_axis_array = np.random.randint(20, 40, size=N) + + x_axis = data_mod.Axis('xaxis', 'm', data=x_axis_array, index=0, spread_order=0) + y_axis = data_mod.Axis('yaxis', 'm', data=y_axis_array, index=0, spread_order=1) + param_axis = data_mod.Axis('Param', 'eV', data=param, index=1) + + data_spread_1x2 = [] + for i in param : + data_list = [] + for ind in range(N): + data_list.append(mutils.gauss2D(x_axis.get_data()[ind], 10+i, 15, + y_axis.get_data()[ind], 30-i, 5)) + data_array = np.squeeze(np.array(data_list)) + data_spread_1x2.append(data_array) + data_spread_1x2 = np.array(data_spread_1x2).T + + data_2D_1x2_spread = data_mod.DataRaw('data2DSpread', data=[data_spread_1x2], + axes=[x_axis, y_axis, param_axis], + distribution='spread', + nav_indexes=(0,)) + + data_2D_1x2_spread.plot('qt') + QtWidgets.QApplication.processEvents() + pass