From 13028d816f4489414cf9a5377d3061288e51e0de Mon Sep 17 00:00:00 2001 From: fdrgsp Date: Thu, 1 Feb 2024 21:03:09 -0500 Subject: [PATCH 1/3] test: add test for SequenceMeta fix --- tests/test_multid_widget.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/test_multid_widget.py b/tests/test_multid_widget.py index 64672977..d2c0e9b1 100644 --- a/tests/test_multid_widget.py +++ b/tests/test_multid_widget.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING +import pytest from napari_micromanager._gui_objects._mda_widget import MultiDWidget from napari_micromanager._mda_meta import SEQUENCE_META_KEY, SequenceMeta from pymmcore_plus.mda import MDAEngine @@ -73,7 +74,12 @@ def test_saving_mda( expected_shape = [x for x in (*mda.shape, 500, 512) if x > 1] multiC = len(mda.channels) > 1 - splitC = mda.metadata[SEQUENCE_META_KEY].split_channels + + meta = mda.metadata[SEQUENCE_META_KEY] + if isinstance(meta, dict): + meta = SequenceMeta(**meta) + + splitC = meta.split_channels if multiC and splitC: expected_shape.pop(mda.used_axes.find("c")) nfiles = len(list((tmp_path / f"{meta.file_name}_000").iterdir())) @@ -104,3 +110,26 @@ def test_script_initiated_mda(main_window: MainWindow, qtbot: QtBot): viewer_layer_names = [layer.name for layer in viewer.layers] assert layer_name in viewer_layer_names assert sequence.shape == viewer.layers[layer_name].data.shape[:-2] + + +meta = [ + {SEQUENCE_META_KEY: SequenceMeta(mode="mda")}, + {SEQUENCE_META_KEY: {"mode": "mda"}}, +] + + +@pytest.mark.parametrize("meta", meta) +def test_mda_set_value(qtbot: QtBot, meta: dict[str, str]): + wdg = MultiDWidget() + qtbot.addWidget(wdg) + + sequence = MDASequence( + channels=[{"config": "Cy5", "exposure": 1}], + time_plan={"interval": 0.1, "loops": 2}, + axis_order="pcz", + stage_positions=[(222, 1, 1)], + metadata=meta, + ) + wdg.setValue(sequence) + + assert wdg.value().metadata[SEQUENCE_META_KEY].mode == "mda" From 12ce21dbed05664e867d9c7170dc82f21ac7f765 Mon Sep 17 00:00:00 2001 From: fdrgsp Date: Thu, 1 Feb 2024 21:03:42 -0500 Subject: [PATCH 2/3] fix: fix MDA setValue --- src/napari_micromanager/_gui_objects/_mda_widget.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/napari_micromanager/_gui_objects/_mda_widget.py b/src/napari_micromanager/_gui_objects/_mda_widget.py index abd34780..2f5f8b4c 100644 --- a/src/napari_micromanager/_gui_objects/_mda_widget.py +++ b/src/napari_micromanager/_gui_objects/_mda_widget.py @@ -49,21 +49,20 @@ def value(self) -> MDASequence: split_channels=bool(split), save_dir=widget_meta.get("save_dir", ""), file_name=widget_meta.get("save_name", ""), - should_save=bool("save_dir" in widget_meta), + should_save="save_dir" in widget_meta, ) return sequence # type: ignore[no-any-return] def setValue(self, value: MDASequence) -> None: """Set the current value of the widget.""" if nmm_meta := value.metadata.get(SEQUENCE_META_KEY): - if not isinstance(nmm_meta, SequenceMeta): # pragma: no cover - raise TypeError(f"Expected {SequenceMeta}, got {type(nmm_meta)}") + if isinstance(nmm_meta, dict): + nmm_meta = SequenceMeta(**nmm_meta) # update pymmcore_widgets metadata if SequenceMeta are provided widgets_meta = value.metadata.setdefault(MMCORE_WIDGETS_META, {}) widgets_meta.setdefault("save_dir", nmm_meta.save_dir) widgets_meta.setdefault("save_name", nmm_meta.file_name) - # set split_channels checkbox self.checkBox_split_channels.setChecked(bool(nmm_meta.split_channels)) super().setValue(value) From 8b203b41a12cf61f643dcc52e0c99abf0a0dc345 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Fri, 2 Feb 2024 13:27:43 -0500 Subject: [PATCH 3/3] simplify --- .../_gui_objects/_mda_widget.py | 5 ++- tests/test_multid_widget.py | 31 +------------------ 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/src/napari_micromanager/_gui_objects/_mda_widget.py b/src/napari_micromanager/_gui_objects/_mda_widget.py index 2f5f8b4c..60b12bbd 100644 --- a/src/napari_micromanager/_gui_objects/_mda_widget.py +++ b/src/napari_micromanager/_gui_objects/_mda_widget.py @@ -49,7 +49,7 @@ def value(self) -> MDASequence: split_channels=bool(split), save_dir=widget_meta.get("save_dir", ""), file_name=widget_meta.get("save_name", ""), - should_save="save_dir" in widget_meta, + should_save=bool("save_dir" in widget_meta), ) return sequence # type: ignore[no-any-return] @@ -58,11 +58,14 @@ def setValue(self, value: MDASequence) -> None: if nmm_meta := value.metadata.get(SEQUENCE_META_KEY): if isinstance(nmm_meta, dict): nmm_meta = SequenceMeta(**nmm_meta) + if not isinstance(nmm_meta, SequenceMeta): # pragma: no cover + raise TypeError(f"Expected {SequenceMeta}, got {type(nmm_meta)}") # update pymmcore_widgets metadata if SequenceMeta are provided widgets_meta = value.metadata.setdefault(MMCORE_WIDGETS_META, {}) widgets_meta.setdefault("save_dir", nmm_meta.save_dir) widgets_meta.setdefault("save_name", nmm_meta.file_name) + # set split_channels checkbox self.checkBox_split_channels.setChecked(bool(nmm_meta.split_channels)) super().setValue(value) diff --git a/tests/test_multid_widget.py b/tests/test_multid_widget.py index 35277c2f..00f10bb8 100644 --- a/tests/test_multid_widget.py +++ b/tests/test_multid_widget.py @@ -2,7 +2,6 @@ from typing import TYPE_CHECKING -import pytest from napari_micromanager._gui_objects._mda_widget import MultiDWidget from napari_micromanager._mda_meta import SEQUENCE_META_KEY, SequenceMeta from pymmcore_plus.mda import MDAEngine @@ -76,12 +75,7 @@ def test_saving_mda( expected_shape = [x for x in (*mda.shape, 500, 512) if x > 1] multiC = len(mda.channels) > 1 - - meta = mda.metadata[SEQUENCE_META_KEY] - if isinstance(meta, dict): - meta = SequenceMeta(**meta) - - splitC = meta.split_channels + splitC = mda.metadata[SEQUENCE_META_KEY].split_channels if multiC and splitC: expected_shape.pop(mda.used_axes.find("c")) nfiles = len(list((tmp_path / f"{meta.file_name}_000").iterdir())) @@ -112,26 +106,3 @@ def test_script_initiated_mda(main_window: MainWindow, qtbot: QtBot): viewer_layer_names = [layer.name for layer in viewer.layers] assert layer_name in viewer_layer_names assert sequence.shape == viewer.layers[layer_name].data.shape[:-2] - - -meta = [ - {SEQUENCE_META_KEY: SequenceMeta(mode="mda")}, - {SEQUENCE_META_KEY: {"mode": "mda"}}, -] - - -@pytest.mark.parametrize("meta", meta) -def test_mda_set_value(qtbot: QtBot, meta: dict[str, str]): - wdg = MultiDWidget() - qtbot.addWidget(wdg) - - sequence = MDASequence( - channels=[{"config": "Cy5", "exposure": 1}], - time_plan={"interval": 0.1, "loops": 2}, - axis_order="pcz", - stage_positions=[(222, 1, 1)], - metadata=meta, - ) - wdg.setValue(sequence) - - assert wdg.value().metadata[SEQUENCE_META_KEY].mode == "mda"