Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix error when loading SequenceMeta as dict #310

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/napari_micromanager/_gui_objects/_mda_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did the linter remove this? or did you?

)
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):
tlambert03 marked this conversation as resolved.
Show resolved Hide resolved
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)
31 changes: 30 additions & 1 deletion tests/test_multid_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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"
Loading