Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn user if data segmented in a different coordinate space #107

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions brainreg_segment/segmentation_panels/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,52 @@
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

Check warning on line 213 in brainreg_segment/segmentation_panels/regions.py

View check run for this annotation

Codecov / codecov/patch

brainreg_segment/segmentation_panels/regions.py#L212-L213

Added lines #L212 - L213 were not covered by tests
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(

Check warning on line 228 in brainreg_segment/segmentation_panels/regions.py

View check run for this annotation

Codecov / codecov/patch

brainreg_segment/segmentation_panels/regions.py#L228

Added line #L228 was not covered by tests
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 @@ -264,6 +264,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 curation widget, and return the widget.
adamltyson marked this conversation as resolved.
Show resolved Hide resolved
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
)
Loading