Skip to content

Commit

Permalink
Warn user if data segmented in a different coordinate space (#107)
Browse files Browse the repository at this point in the history
* 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 <alessandrofelder@users.noreply.github.com>

---------

Co-authored-by: Alessandro Felder <alessandrofelder@users.noreply.github.com>
  • Loading branch information
adamltyson and alessandrofelder committed Jun 26, 2023
1 parent b6d0202 commit 398772a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
45 changes: 36 additions & 9 deletions brainreg_segment/segmentation_panels/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,52 @@ 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:
choice = True # for debugging

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. ",
)
2 changes: 2 additions & 0 deletions brainreg_segment/segmentation_panels/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions tests/tests/test_integration/test_gui/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions tests/tests/test_unit/test_segmentation_panels/test_regions.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 398772a

Please sign in to comment.