Skip to content

Commit

Permalink
mypy, lint, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yrkim98 committed Nov 27, 2024
1 parent 682c105 commit 324e0cd
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 154 deletions.
19 changes: 16 additions & 3 deletions src/allencell_ml_segmenter/_tests/fakes/fake_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,20 @@ def _on_layers_change(self):
fn(FakeNapariEvent())

def get_seg_layers(self, layer_list: list[Layer]) -> list[Layer]:
return [layer for layer in self._image_layers.values() if layer.name.startswith("[seg]")]

def insert_segmentation(self, layer_name: str, img: np.ndarray, seg_layers: bool = False) -> None:
return [
layer
for layer in self._image_layers.values()
if layer.name.startswith("[seg]")
]

def insert_segmentation(
self, layer_name: str, img: np.ndarray, seg_layers: bool = False
) -> None:
self.segmentation_inserted[f"[threshold] {layer_name}"] = img

def get_layers_nonthreshold(self) -> list[Layer]:
return [
layer
for layer in self._image_layers.values()
if not layer.name.startswith("[threshold]")
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from allencell_ml_segmenter._tests.fakes.fake_subscriber import FakeSubscriber
from allencell_ml_segmenter.core.event import Event
from allencell_ml_segmenter.thresholding.thresholding_model import ThresholdingModel
from allencell_ml_segmenter.thresholding.thresholding_model import (
ThresholdingModel,
)


@pytest.fixture
Expand All @@ -16,7 +18,7 @@ def test_set_thresholding_value_dispatches_event(thresholding_model):
thresholding_model.subscribe(
Event.ACTION_THRESHOLDING_VALUE_CHANGED,
fake_subscriber,
fake_subscriber.handle
fake_subscriber.handle,
)

thresholding_model.set_thresholding_value(2)
Expand All @@ -29,19 +31,22 @@ def test_set_autothresholding_enabled_dispatches_event(thresholding_model):
thresholding_model.subscribe(
Event.ACTION_THRESHOLDING_AUTOTHRESHOLDING_SELECTED,
fake_subscriber,
fake_subscriber.handle
fake_subscriber.handle,
)

thresholding_model.set_autothresholding_enabled(True)

assert fake_subscriber.was_handled(Event.ACTION_THRESHOLDING_AUTOTHRESHOLDING_SELECTED)
assert fake_subscriber.was_handled(
Event.ACTION_THRESHOLDING_AUTOTHRESHOLDING_SELECTED
)


def test_dispatch_save_thresholded_images(thresholding_model):
fake_subscriber: FakeSubscriber = FakeSubscriber()
thresholding_model.subscribe(
Event.ACTION_SAVE_THRESHOLDING_IMAGES,
fake_subscriber,
fake_subscriber.handle
fake_subscriber.handle,
)

thresholding_model.dispatch_save_thresholded_images()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import numpy as np

from allencell_ml_segmenter.core.file_input_model import FileInputModel
from allencell_ml_segmenter._tests.fakes.fake_experiments_model import FakeExperimentsModel
from allencell_ml_segmenter._tests.fakes.fake_experiments_model import (
FakeExperimentsModel,
)
from allencell_ml_segmenter.main.main_model import MainModel
from allencell_ml_segmenter.thresholding.thresholding_model import ThresholdingModel
from allencell_ml_segmenter.thresholding.thresholding_service import ThresholdingService
from allencell_ml_segmenter.thresholding.thresholding_model import (
ThresholdingModel,
)
from allencell_ml_segmenter.thresholding.thresholding_service import (
ThresholdingService,
)
from allencell_ml_segmenter.core.task_executor import SynchroTaskExecutor
from allencell_ml_segmenter._tests.fakes.fake_viewer import FakeViewer

Expand All @@ -15,12 +21,18 @@ def test_image():
"""Create a small test image for thresholding."""
return np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])


def test_on_threshold_changed_non_prediction(test_image):
thresholding_model: ThresholdingModel = ThresholdingModel()
viewer: FakeViewer = FakeViewer()
thresholding_service: ThresholdingService = ThresholdingService(thresholding_model, FakeExperimentsModel(),
FileInputModel(), MainModel(), viewer,
task_executor=SynchroTaskExecutor.global_instance())
thresholding_service: ThresholdingService = ThresholdingService(
thresholding_model,
FakeExperimentsModel(),
FileInputModel(),
MainModel(),
viewer,
task_executor=SynchroTaskExecutor.global_instance(),
)
viewer.add_image(test_image, name="test_layer")

# ACT set a threshold to trigger
Expand All @@ -37,14 +49,20 @@ def test_on_threshold_changed_non_prediction(test_image):
seg_data = viewer.segmentation_inserted["[threshold] test_layer"]
assert np.array_equal(seg_data, (test_image > 100).astype(int))


def test_on_threshold_changed_non_prediction(test_image):
thresholding_model: ThresholdingModel = ThresholdingModel()
viewer: FakeViewer = FakeViewer()
main_model: MainModel = MainModel()
main_model.set_predictions_in_viewer(True)
thresholding_service: ThresholdingService = ThresholdingService(thresholding_model, FakeExperimentsModel(),
FileInputModel(), main_model, viewer,
task_executor=SynchroTaskExecutor.global_instance())
thresholding_service: ThresholdingService = ThresholdingService(
thresholding_model,
FakeExperimentsModel(),
FileInputModel(),
main_model,
viewer,
task_executor=SynchroTaskExecutor.global_instance(),
)
viewer.add_image(test_image, name="[raw] test_layer 1")
viewer.add_image(test_image, name="[seg] test_layer 1")
viewer.add_image(test_image, name="[raw] test_layer 2")
Expand Down
Loading

0 comments on commit 324e0cd

Please sign in to comment.