Skip to content

Commit

Permalink
Fix for missing output directory for previewing; add make() method for
Browse files Browse the repository at this point in the history
models
  • Loading branch information
multimeric committed Nov 3, 2023
1 parent 6cf492f commit 69500a7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
10 changes: 10 additions & 0 deletions core/lls_core/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ def copy_validate(self, **kwargs) -> Self:
"""
updated = self.copy(**kwargs)
return updated.validate(updated.dict())

@classmethod
def make(cls, validate: bool = True, **kwargs: Any):
"""
Creates an instance of this class, with validation either enabled or disabled
"""
if validate:
return cls(**kwargs)
else:
return cls.construct(**kwargs)
17 changes: 12 additions & 5 deletions plugin/napari_lattice/dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def _check_validity(self) -> bool:
except:
return False

def _make_model(self) -> LatticeData:
def _make_model(self, validate: bool = True) -> LatticeData:
from rich import print
from sys import stdout

deskew_args = self.LlszMenu.WidgetContainer.deskew_fields._get_kwargs()
output_args = self.LlszMenu.WidgetContainer.output_fields._make_model()
params = LatticeData(
output_args = self.LlszMenu.WidgetContainer.output_fields._make_model(validate=False)
args = dict(
input_image=deskew_args["data"],
angle=deskew_args["angle"],
channel_range=output_args.channel_range,
Expand All @@ -56,6 +56,7 @@ def _make_model(self) -> LatticeData:
deconvolution=self.LlszMenu.WidgetContainer.deconv_fields._make_model(),
crop=self.LlszMenu.WidgetContainer.cropping_fields._make_model()
)
params = LatticeData.make(validate=validate, **args)
# Log the lattice
print(params, file=stdout)
return params
Expand Down Expand Up @@ -108,12 +109,18 @@ def __post_init__(self):
call_button="Preview"
)
@set_design(text="Preview")
def preview(self, header:str, time: int, channel: int):
def preview(self, header: str, time: int, channel: int):
from pathlib import Path

# We only need to process one time point for the preview,
# so we made a copy using a subset of the times
lattice = self._make_model().copy_validate(update=dict(
lattice = self._make_model(validate=False).copy_validate(update=dict(
time_range = range(time, time+1),
channel_range = range(time, time+1),
# Patch in a placeholder for the save dir because previewing doesn't use it
# TODO: use a more elegant solution such as making the "saveable" lattice
# a child class which more validations
save_dir = Path.home()
))

for slice in lattice.process().slices:
Expand Down
6 changes: 4 additions & 2 deletions plugin/napari_lattice/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,10 @@ class OutputFields(NapariFieldGroup):
)
errors = field(Label).with_options(label="Errors")

def _make_model(self) -> OutputParams:
return OutputParams(
def _make_model(self, validate: bool = True) -> OutputParams:
return OutputParams.make(
validate=validate,

channel_range=range(self.channel_range.value[0], self.channel_range.value[1]),
time_range=range(self.time_range.value[0], self.time_range.value[1]),
save_dir=self.save_path.value,
Expand Down
10 changes: 9 additions & 1 deletion plugin/tests/test_dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ def test_dock_widget(make_napari_viewer: Callable[[], Viewer], image_data: AICSI
fields.img_layer.value = list(viewer.layers)
fields.dimension_order.value = image_data.dims.order
fields.pixel_sizes_source.value = PixelSizeSource.Manual

# Test previewing
tester = FunctionGuiTester(ui.preview)
tester.call("", 0, 0)

# Add the save path which shouldn't be needed for previewing
ui.LlszMenu.WidgetContainer.output_fields.save_path.value = tmpdir
# fields.pixel_sizes.value = image_data.physical_pixel_sizes

# Test saving
tester = FunctionGuiTester(ui.save)
tester.call()


def test_check_buildable():
ui = LLSZWidget()
set_debug(ui)
Expand Down

0 comments on commit 69500a7

Please sign in to comment.