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

add tests for transforming to atlas space function #38

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dev = [
"pyqt5",
"pytest-cov",
"pytest-qt",
"pytest-mock",
"pytest",
"qtpy",
"ruff",
Expand Down
85 changes: 85 additions & 0 deletions tests/tests/test_brainreg/test_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from pathlib import Path

import numpy as np
from bg_atlasapi import BrainGlobeAtlas

from brainglobe_utils.brainreg.transform import (
transform_points_from_downsampled_to_atlas_space,
)


def test_transform_points_from_downsampled_to_atlas_space(mocker):
"""
Test case for transforming points from downsampled space to atlas space.
This is a basic test case that covers a deformation field that maps
everything to [1, 1, 1].

Args:
mocker: The mocker object used to patch the reading of deformation
field tiffs.

Returns:
None
"""
mock_deformation_field = np.ones(
(132, 80, 114)
) # shape of allen 100 reference, has unit mm
mocker.patch(
"brainglobe_utils.brainreg.transform.tifffile.imread",
side_effect=lambda x: mock_deformation_field,
)
downsampled_points = np.array([[1, 1, 1], [2, 2, 2]])
transformed_points, points_out_of_bounds = (
transform_points_from_downsampled_to_atlas_space(
downsampled_points=downsampled_points,
atlas=BrainGlobeAtlas("allen_mouse_100um"),
deformation_field_paths=[
Path.home() / "dummy_x_deformation.tif",
Path.home() / "dummy_y_deformation.tif",
Path.home() / "dummy_z_deformation.tif",
],
)
)
# because we mock the deformation field as all ones,
# all coordinates should be mapped to [1,1,1]*1mm/100um = [10,10,10]
assert np.all(transformed_points == np.ones_like(transformed_points) * 10)
assert not points_out_of_bounds, str(points_out_of_bounds)


def test_transform_points_from_downsampled_to_atlas_space_out_of_bounds(
mocker,
):
"""
Test case for transforming points from downsampled space to atlas space
when points are out of bounds.
Points are out of bounds when they cause an index error on the deformation
field.

Args:
mocker: The mocker object to patch the reading of deformation
field tiffs

Returns:
None
"""
mock_deformation_field = np.ones((4, 4, 4))
mocker.patch(
"brainglobe_utils.brainreg.transform.tifffile.imread",
side_effect=lambda x: mock_deformation_field,
)
downsampled_points = np.array([[5, 5, 5]])
transformed_points, points_out_of_bounds = (
transform_points_from_downsampled_to_atlas_space(
downsampled_points=downsampled_points,
atlas=BrainGlobeAtlas("allen_mouse_100um"),
deformation_field_paths=[
Path.home() / "dummy_x_deformation.tif",
Path.home() / "dummy_y_deformation.tif",
Path.home() / "dummy_z_deformation.tif",
],
)
)

assert len(transformed_points) == 0
assert len(points_out_of_bounds) == 1
assert points_out_of_bounds[0] == [5, 5, 5]
Loading