-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #573 from AllenCell/feature/thresholding-live_images
Feature/thresholding live images
- Loading branch information
Showing
29 changed files
with
1,445 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
54 changes: 54 additions & 0 deletions
54
src/allencell_ml_segmenter/_tests/thresholding/test_thresholding_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
|
||
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, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def thresholding_model() -> ThresholdingModel: | ||
model = ThresholdingModel() | ||
return model | ||
|
||
|
||
def test_set_thresholding_value_dispatches_event(thresholding_model): | ||
fake_subscriber: FakeSubscriber = FakeSubscriber() | ||
thresholding_model.subscribe( | ||
Event.ACTION_THRESHOLDING_VALUE_CHANGED, | ||
fake_subscriber, | ||
fake_subscriber.handle, | ||
) | ||
|
||
thresholding_model.set_thresholding_value(2) | ||
|
||
assert fake_subscriber.was_handled(Event.ACTION_THRESHOLDING_VALUE_CHANGED) | ||
|
||
|
||
def test_set_autothresholding_enabled_dispatches_event(thresholding_model): | ||
fake_subscriber: FakeSubscriber = FakeSubscriber() | ||
thresholding_model.subscribe( | ||
Event.ACTION_THRESHOLDING_AUTOTHRESHOLDING_SELECTED, | ||
fake_subscriber, | ||
fake_subscriber.handle, | ||
) | ||
|
||
thresholding_model.set_autothresholding_enabled(True) | ||
|
||
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, | ||
) | ||
|
||
thresholding_model.dispatch_save_thresholded_images() | ||
|
||
assert fake_subscriber.was_handled(Event.ACTION_SAVE_THRESHOLDING_IMAGES) |
101 changes: 101 additions & 0 deletions
101
src/allencell_ml_segmenter/_tests/thresholding/test_thresholding_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import pytest | ||
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.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.core.task_executor import SynchroTaskExecutor | ||
from allencell_ml_segmenter._tests.fakes.fake_viewer import FakeViewer | ||
|
||
|
||
@pytest.fixture | ||
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): | ||
# ARRANGE | ||
thresholding_model: ThresholdingModel = ThresholdingModel() | ||
viewer: FakeViewer = FakeViewer() | ||
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 | ||
thresholding_model.set_thresholding_value(50) | ||
|
||
# Verify a segmentation layer is added | ||
assert "[threshold] test_layer" in viewer.threshold_inserted | ||
seg_data = viewer.threshold_inserted["[threshold] test_layer"] | ||
assert np.array_equal(seg_data, (test_image > 50).astype(int)) | ||
|
||
# check if existing thresholds get updated | ||
thresholding_model.set_thresholding_value(100) | ||
assert len(viewer.get_layers()) == 1 | ||
seg_data = viewer.threshold_inserted["[threshold] test_layer"] | ||
assert np.array_equal(seg_data, (test_image > 100).astype(int)) | ||
|
||
|
||
def test_on_threshold_changed_non_prediction(test_image): | ||
""" | ||
Test that the thresholding service does not add a threshold layer for a layer that is not a probability map | ||
""" | ||
# ARRANGE | ||
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(), | ||
) | ||
# Only the [seg] layers below should produce a threshold layer | ||
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") | ||
viewer.add_image(test_image, name="[seg] test_layer 2") | ||
viewer.add_image(test_image, name="donotthreshold") | ||
|
||
# ACT set a threshold to trigger | ||
thresholding_model.set_thresholding_value(50) | ||
|
||
# Verify a threshold layer is added for each seg layer | ||
assert "[threshold] [seg] test_layer 1" in viewer.threshold_inserted | ||
seg_data = viewer.threshold_inserted["[threshold] [seg] test_layer 1"] | ||
assert np.array_equal(seg_data, (test_image > 50).astype(int)) | ||
assert "[threshold] [seg] test_layer 2" in viewer.threshold_inserted | ||
seg_data = viewer.threshold_inserted["[threshold] [seg] test_layer 2"] | ||
assert np.array_equal(seg_data, (test_image > 50).astype(int)) | ||
# verify that raw layers do not get thresholded | ||
assert len(viewer.threshold_inserted) == 2 | ||
|
||
# verify existing threshold layers get updated correctly | ||
thresholding_model.set_thresholding_value(100) | ||
# Verify a threshold layer is added for each seg layer | ||
assert "[threshold] [seg] test_layer 1" in viewer.threshold_inserted | ||
seg_data = viewer.threshold_inserted["[threshold] [seg] test_layer 1"] | ||
assert np.array_equal(seg_data, (test_image > 100).astype(int)) | ||
assert "[threshold] [seg] test_layer 2" in viewer.threshold_inserted | ||
seg_data = viewer.threshold_inserted["[threshold] [seg] test_layer 2"] | ||
assert np.array_equal(seg_data, (test_image > 100).astype(int)) | ||
# verify that raw layers do not get thresholded | ||
assert len(viewer.threshold_inserted) == 2 |
Oops, something went wrong.