From 398772a155739f1173fa69f96244db5f1735792f Mon Sep 17 00:00:00 2001 From: Adam Tyson Date: Mon, 26 Jun 2023 15:53:26 +0100 Subject: [PATCH] Warn user if data segmented in a different coordinate space (#107) * Warn user if data segmented in a different coordinate space * Add draft (failing) test * Add simpler test * rename test file * Add warnings about different coordinate spaces * Update tests/conftest.py Co-authored-by: Alessandro Felder --------- Co-authored-by: Alessandro Felder --- .../segmentation_panels/regions.py | 45 +++++++++++++++---- .../segmentation_panels/tracks.py | 2 + tests/conftest.py | 17 +++++++ .../test_integration/test_gui/test_gui.py | 15 ------- .../test_segmentation_panels/__init__.py | 0 .../test_segmentation_panels/test_regions.py | 17 +++++++ 6 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 tests/tests/test_unit/test_segmentation_panels/__init__.py create mode 100644 tests/tests/test_unit/test_segmentation_panels/test_regions.py diff --git a/brainreg_segment/segmentation_panels/regions.py b/brainreg_segment/segmentation_panels/regions.py index dd2d26f..9a60072 100644 --- a/brainreg_segment/segmentation_panels/regions.py +++ b/brainreg_segment/segmentation_panels/regions.py @@ -196,6 +196,8 @@ def run_region_analysis(self, override=False): choice = display_warning( self.parent, "About to analyse regions", + "Please ensure the regions were segmented in the same " + "reference space as the currently open image. " "Existing files will be will be deleted. Proceed?", ) else: @@ -203,18 +205,43 @@ def run_region_analysis(self, override=False): if choice: print("Running region analysis") - worker = region_analysis( + + if check_segmentation_in_correct_space( self.parent.label_layers, self.parent.annotations_layer.data, - self.parent.atlas, - self.parent.hemispheres_data, - self.parent.paths.regions_directory, - output_csv_file=self.parent.paths.region_summary_csv, - volumes=self.calculate_volumes_checkbox.isChecked(), - summarise=self.summarise_volumes_checkbox.isChecked(), - ) - worker.start() + ): + worker = region_analysis( + self.parent.label_layers, + self.parent.annotations_layer.data, + self.parent.atlas, + self.parent.hemispheres_data, + self.parent.paths.regions_directory, + output_csv_file=self.parent.paths.region_summary_csv, + volumes=self.calculate_volumes_checkbox.isChecked(), + summarise=self.summarise_volumes_checkbox.isChecked(), + ) + worker.start() + else: + display_incorrect_space_warning(self.parent) + return else: print("Preventing analysis as user chose 'Cancel'") else: print("No regions found") + + +def check_segmentation_in_correct_space(label_layers, annotations_layer_image): + for label_layer in label_layers: + if label_layer.data.shape != annotations_layer_image.shape: + return False + return True + + +def display_incorrect_space_warning(widget): + display_info( + widget, + "Incorrect coordinate space", + "One or more of the segmented images are not of the same size " + "as the registered image. Please ensure you segmented your " + "data in the same coordinate space as you wish to analyse it in. ", + ) diff --git a/brainreg_segment/segmentation_panels/tracks.py b/brainreg_segment/segmentation_panels/tracks.py index 49c9d8b..f3983e5 100644 --- a/brainreg_segment/segmentation_panels/tracks.py +++ b/brainreg_segment/segmentation_panels/tracks.py @@ -286,6 +286,8 @@ def run_track_analysis(self, override=False): choice = display_warning( self.parent, "About to analyse tracks", + "Please ensure the tracks were defined in the same " + "reference space as the currently open image. " "Existing files will be will be deleted. Proceed?", ) else: diff --git a/tests/conftest.py b/tests/conftest.py index b12739a..6f5ba77 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,26 @@ +from pathlib import Path + import pytest from bg_atlasapi import BrainGlobeAtlas +from brainreg_segment.segment import SegmentationWidget + atlas_name = "allen_mouse_50um" +brainreg_dir = Path.cwd() / "tests" / "data" / "brainreg_output" @pytest.fixture def allen_mouse_50um_atlas(): return BrainGlobeAtlas(atlas_name) + + +@pytest.fixture +def segmentation_widget(make_napari_viewer): + """ + Create a viewer, add the segmentation widget, and return the widget. + The viewer can be accessed using ``widget.viewer``. + """ + viewer = make_napari_viewer() + widget = SegmentationWidget(viewer) + viewer.window.add_dock_widget(widget) + return widget diff --git a/tests/tests/test_integration/test_gui/test_gui.py b/tests/tests/test_integration/test_gui/test_gui.py index 9b0338a..23b2841 100644 --- a/tests/tests/test_integration/test_gui/test_gui.py +++ b/tests/tests/test_integration/test_gui/test_gui.py @@ -4,27 +4,12 @@ import numpy as np import pandas as pd -import pytest - -from brainreg_segment.segment import SegmentationWidget brainreg_dir = Path.cwd() / "tests" / "data" / "brainreg_output" ATLAS_NAME = "example_mouse_100um" -@pytest.fixture -def segmentation_widget(make_napari_viewer): - """ - Create a viewer, add the curation widget, and return the widget. - The viewer can be accessed using ``widget.viewer``. - """ - viewer = make_napari_viewer() - widget = SegmentationWidget(viewer) - viewer.window.add_dock_widget(widget) - return widget - - def test_load_sample_space(segmentation_widget): segmentation_widget.standard_space = False segmentation_widget.plugin = "brainglobe-napari-io.brainreg_read_dir" diff --git a/tests/tests/test_unit/test_segmentation_panels/__init__.py b/tests/tests/test_unit/test_segmentation_panels/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests/test_unit/test_segmentation_panels/test_regions.py b/tests/tests/test_unit/test_segmentation_panels/test_regions.py new file mode 100644 index 0000000..0e66fdc --- /dev/null +++ b/tests/tests/test_unit/test_segmentation_panels/test_regions.py @@ -0,0 +1,17 @@ +import numpy as np +from napari.layers import Labels + +from brainreg_segment.segmentation_panels.regions import ( + check_segmentation_in_correct_space, +) + + +def test_check_segmentation_in_correct_space(): + labels_layer_10 = Labels(np.zeros((10, 10, 10), dtype=int)) + labels_layer_15 = Labels(np.zeros((15, 15, 15), dtype=int)) + assert check_segmentation_in_correct_space( + [labels_layer_10], labels_layer_10.data + ) + assert not check_segmentation_in_correct_space( + [labels_layer_10], labels_layer_15.data + )