Skip to content

Commit

Permalink
Fixed pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorTatarnikov committed Apr 23, 2024
1 parent 4cbd6be commit 503f57e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
38 changes: 27 additions & 11 deletions brainglobe_registration/registration_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -229,15 +239,17 @@ 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)

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)
Expand Down Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion brainglobe_registration/utils/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 5 additions & 18 deletions tests/test_registration_widget.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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"
)
Expand All @@ -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)
Expand All @@ -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"
)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -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)),
Expand All @@ -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)

Expand Down

0 comments on commit 503f57e

Please sign in to comment.