diff --git a/brainglobe_registration/registration_widget.py b/brainglobe_registration/registration_widget.py index a98f9cb..cc46da2 100644 --- a/brainglobe_registration/registration_widget.py +++ b/brainglobe_registration/registration_widget.py @@ -9,14 +9,16 @@ """ from pathlib import Path +from typing import Optional import dask.array as da +import napari.layers import numpy as np +import numpy.typing as npt from bg_atlasapi import BrainGlobeAtlas from bg_atlasapi.list_atlases import get_downloaded_atlases from brainglobe_utils.qtpy.collapsible_widget import CollapsibleWidgetContainer from dask_image.ndinterp import affine_transform as dask_affine_transform -from scipy.ndimage.interpolation import affine_transform from napari.qt.threading import thread_worker from napari.utils.notifications import show_error from napari.viewer import Viewer @@ -54,11 +56,11 @@ def __init__(self, napari_viewer: Viewer): self.setContentsMargins(10, 10, 10, 10) self._viewer = napari_viewer - self._atlas: BrainGlobeAtlas = None - self._atlas_data_layer = None - self._atlas_annotations_layer = None - self._moving_image = None - self._moving_image_data_backup = None + self._atlas: Optional[BrainGlobeAtlas] = None + self._atlas_data_layer: Optional[napari.layers.Image] = None + self._atlas_annotations_layer: Optional[napari.layers.Labels] = None + self._moving_image: Optional[napari.layers.Image] = None + self._moving_image_data_backup: Optional[npt.NDArray] = None self.transform_params: dict[str, dict] = { "rigid": {}, @@ -196,11 +198,19 @@ def _on_atlas_dropdown_index_changed(self, index): self._atlas = BrainGlobeAtlas(atlas_name) atlas_dask_array = da.from_array( self._atlas.reference, - chunks=(1, self._atlas.reference.shape[1], self._atlas.reference.shape[2]), + chunks=( + 1, + self._atlas.reference.shape[1], + self._atlas.reference.shape[2], + ), ) dask_annotations = da.from_array( self._atlas.annotation, - chunks=(1, self._atlas.annotation.shape[1], self._atlas.annotation.shape[2]), + chunks=( + 1, + self._atlas.annotation.shape[1], + self._atlas.annotation.shape[2], + ), ) self._atlas_data_layer = self._viewer.add_image( @@ -229,7 +239,8 @@ def _on_sample_dropdown_index_changed(self, index): def _on_adjust_moving_image(self, x: int, y: int, rotate: float): if not self._moving_image: show_error( - "No moving image selected. Please select a moving image before adjusting" + "No moving image selected. " + "Please select a moving image before adjusting" ) return adjust_napari_image_layer(self._moving_image, x, y, rotate) @@ -237,7 +248,8 @@ def _on_adjust_moving_image(self, x: int, y: int, rotate: float): def _on_adjust_moving_image_reset_button_click(self): if not self._moving_image: show_error( - "No moving image selected. Please select a moving image before adjusting" + "No moving image selected. " + "Please select a moving image before adjusting" ) return adjust_napari_image_layer(self._moving_image, 0, 0, 0) @@ -388,7 +400,11 @@ def _on_scale_moving_image(self, x: float, y: float): ) def _on_adjust_atlas_rotation(self, pitch: float, yaw: float, roll: float): - if not self._atlas: + if not ( + self._atlas + and self._atlas_data_layer + and self._atlas_annotations_layer + ): show_error( "No atlas selected. Please select an atlas before rotating" ) diff --git a/brainglobe_registration/utils/utils.py b/brainglobe_registration/utils/utils.py index 7c6db59..2bb9891 100644 --- a/brainglobe_registration/utils/utils.py +++ b/brainglobe_registration/utils/utils.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import List, Tuple, Dict +from typing import Dict, List, Tuple import napari import numpy as np diff --git a/tests/test_registration_widget.py b/tests/test_registration_widget.py index d46a0bb..e713305 100644 --- a/tests/test_registration_widget.py +++ b/tests/test_registration_widget.py @@ -1,11 +1,7 @@ import pytest -from pytransform3d.rotations import active_matrix_from_angle -import numpy as np from brainglobe_registration.registration_widget import RegistrationWidget -from brainglobe_registration.utils.utils import calculate_rotated_bounding_box - @pytest.fixture() def registration_widget(make_napari_viewer_with_images): @@ -153,11 +149,10 @@ def test_scale_moving_image( "pitch, yaw, roll, expected_shape", [ (0, 0, 0, (132, 80, 114)), - (45, 0, 0, (150, 150, 114)), + (45, 0, 0, (150, 150, 114)), (0, 45, 0, (174, 80, 174)), (0, 0, 45, (132, 137, 137)), ], - ) def test_on_adjust_atlas_rotation( registration_widget_with_example_atlas, @@ -169,18 +164,14 @@ def test_on_adjust_atlas_rotation( reg_widget = registration_widget_with_example_atlas atlas_shape = reg_widget._atlas.reference.shape - reg_widget._on_adjust_atlas_rotation( - pitch, yaw, roll - ) + reg_widget._on_adjust_atlas_rotation(pitch, yaw, roll) assert reg_widget._atlas_data_layer.data.shape == expected_shape assert reg_widget._atlas_annotations_layer.data.shape == expected_shape assert reg_widget._atlas.reference.shape == atlas_shape -def test_on_adjust_atlas_rotation_no_atlas( - registration_widget, mocker -): +def test_on_adjust_atlas_rotation_no_atlas(registration_widget, mocker): mocked_show_error = mocker.patch( "brainglobe_registration.registration_widget.show_error" ) @@ -190,9 +181,7 @@ def test_on_adjust_atlas_rotation_no_atlas( ) -def test_on_atlas_reset( - registration_widget_with_example_atlas -): +def test_on_atlas_reset(registration_widget_with_example_atlas): reg_widget = registration_widget_with_example_atlas atlas_shape = reg_widget._atlas.reference.shape reg_widget._on_adjust_atlas_rotation(10, 10, 10) @@ -204,9 +193,7 @@ def test_on_atlas_reset( assert reg_widget._atlas_annotations_layer.data.shape == atlas_shape -def test_on_atlas_reset_no_atlas( - registration_widget, mocker -): +def test_on_atlas_reset_no_atlas(registration_widget, mocker): mocked_show_error = mocker.patch( "brainglobe_registration.registration_widget.show_error" ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 35dab9e..89068e7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -7,10 +7,10 @@ from brainglobe_registration.utils.utils import ( adjust_napari_image_layer, + calculate_rotated_bounding_box, find_layer_index, get_image_layer_names, open_parameter_file, - calculate_rotated_bounding_box, ) @@ -101,7 +101,7 @@ def test_get_image_layer_names(make_napari_viewer_with_images): [ (0, 0, (50, 100, 200)), (0, 90, (50, 200, 100)), - (0, 180, (50, 100, 200)), + (0, 180, (50, 100, 200)), (0, 45, (50, 212, 212)), (1, 0, (50, 100, 200)), (1, 90, (200, 100, 50)), @@ -111,12 +111,14 @@ def test_get_image_layer_names(make_napari_viewer_with_images): (2, 90, (100, 50, 200)), (2, 180, (50, 100, 200)), (2, 45, (106, 106, 200)), - ] + ], ) def test_calculate_rotated_bounding_box(basis, rotation, expected_bounds): image_shape = (50, 100, 200) rotation_matrix = np.eye(4) - rotation_matrix[:-1, :-1] = active_matrix_from_angle(basis, np.deg2rad(rotation)) + rotation_matrix[:-1, :-1] = active_matrix_from_angle( + basis, np.deg2rad(rotation) + ) result_shape = calculate_rotated_bounding_box(image_shape, rotation_matrix)