Skip to content

Commit

Permalink
add negative tests for new validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Jan 28, 2025
1 parent b9574ec commit 18f8d2b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions brainglobe_atlasapi/atlas_generation/validate_atlases.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def validate_annotation_symmetry(atlas: BrainGlobeAtlas):
label_5_right_of_centre = central_leftright_axis_annotations[centre[2] - 5]
assert (
label_5_left_of_centre == label_5_right_of_centre
), "Annotation labels are asymmetric"
), "Annotation labels are asymmetric."
return True


Expand All @@ -242,7 +242,7 @@ def validate_atlas_name(atlas: BrainGlobeAtlas):
"""
assert (
atlas.atlas_name == atlas.atlas_name.lower()
), "Atlas name cannot contain capitals."
), f"Atlas name {atlas.atlas_name} cannot contain capitals."
return True


Expand Down
58 changes: 52 additions & 6 deletions tests/atlasgen/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
catch_missing_structures,
get_all_validation_functions,
validate_additional_references,
validate_annotation_symmetry,
validate_atlas_files,
validate_atlas_name,
validate_image_dimensions,
validate_mesh_matches_image_extents,
validate_reference_image_pixels,
)
from brainglobe_atlasapi.config import get_brainglobe_dir
from brainglobe_atlasapi.core import AdditionalRefDict


@pytest.fixture
def valid_atlas():
def atlas():
"""A fixture providing a small atlas for testing.
Tests assume this atlas is valid"""
return BrainGlobeAtlas("kim_dev_mouse_e11-5_mri-adc_31.5um")
Expand Down Expand Up @@ -121,20 +124,20 @@ def atlas_with_reference_matching_additional_reference():
os.remove(additional_reference_name)


def test_valid_atlas_passes_all_validations(valid_atlas):
def test_valid_atlas_passes_all_validations(atlas):
"""
Check all our validation functions return True
for a valid atlas
"""
validation_functions = get_all_validation_functions()
for validation_function in validation_functions:
assert validation_function(
valid_atlas
atlas
), f"Function {validation_function.__name__} fails on valid atlas."


def test_validate_mesh_matches_image_extents_negative(mocker, valid_atlas):
flipped_annotation_image = np.transpose(valid_atlas.annotation)
def test_validate_mesh_matches_image_extents_negative(mocker, atlas):
flipped_annotation_image = np.transpose(atlas.annotation)
mocker.patch(
"brainglobe_atlasapi.BrainGlobeAtlas.annotation",
new_callable=mocker.PropertyMock,
Expand All @@ -143,7 +146,7 @@ def test_validate_mesh_matches_image_extents_negative(mocker, valid_atlas):
with pytest.raises(
AssertionError, match="differ by more than 10 times pixel size"
):
validate_mesh_matches_image_extents(valid_atlas)
validate_mesh_matches_image_extents(atlas)


def test_invalid_atlas_path(atlas_with_bad_reference_file):
Expand Down Expand Up @@ -225,3 +228,46 @@ def test_atlas_additional_reference_same(
validate_additional_references(
atlas_with_reference_matching_additional_reference
)


def test_badly_scaled_reference_fails(mocker, atlas):
"""Checks that an atlas with only ones as reference image fails"""
invalid_reference = np.ones_like(atlas.reference)
mocker.patch(
"brainglobe_atlasapi.BrainGlobeAtlas.reference",
new_callable=mocker.PropertyMock,
return_value=invalid_reference,
)
with pytest.raises(
AssertionError,
match=r"Reference image is likely wrongly rescaled to.",
):
validate_reference_image_pixels(atlas)


def test_asymmetrical_annotation_fails(mocker, atlas):
"""Checks that an atlas with LR annotation mismatch fails"""
asymmetrical_lr_labels = atlas.annotation.copy()
midsagittal_index = asymmetrical_lr_labels.shape[2] // 2
asymmetrical_lr_labels[:, :, midsagittal_index:] += 1

mocker.patch(
"brainglobe_atlasapi.BrainGlobeAtlas.annotation",
new_callable=mocker.PropertyMock,
return_value=asymmetrical_lr_labels,
)
with pytest.raises(
AssertionError,
match=r"Annotation labels are asymmetric",
):
validate_annotation_symmetry(atlas)


def test_upper_case_name_fails(atlas):
"""Checks that an atlas with capital letters in name fails"""
atlas.atlas_name = atlas.atlas_name.upper()
with pytest.raises(
AssertionError,
match=r"cannot contain capitals.",
):
validate_atlas_name(atlas)

0 comments on commit 18f8d2b

Please sign in to comment.