Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

simplify validation function #113

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
56 changes: 23 additions & 33 deletions bg_atlasgen/validate_atlases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
from bg_atlasapi.list_atlases import (
get_all_atlases_lastversions,
get_atlases_lastversions,
get_local_atlas_version,
)
from bg_atlasapi.update_atlases import update_atlas


def validate_atlas_files(atlas_path: Path):
def validate_atlas_files(atlas: BrainGlobeAtlas):

Check warning on line 18 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L18

Added line #L18 was not covered by tests
"""Checks if basic files exist in the atlas folder"""

atlas_path = (

Check warning on line 21 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L21

Added line #L21 was not covered by tests
Path(get_brainglobe_dir())
/ f"{atlas.atlas_name}_v{get_local_atlas_version(atlas.atlas_name)}"
)
assert atlas_path.is_dir(), f"Atlas path {atlas_path} not found"
expected_files = [
"annotation.tiff",
Expand Down Expand Up @@ -90,28 +95,31 @@
return True


def open_for_visual_check():
def open_for_visual_check(atlas: BrainGlobeAtlas):

Check warning on line 98 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L98

Added line #L98 was not covered by tests
# implement visual checks later
pass


def validate_checksum():
def validate_checksum(atlas: BrainGlobeAtlas):

Check warning on line 103 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L103

Added line #L103 was not covered by tests
# implement later
pass


def check_additional_references():
def check_additional_references(atlas: BrainGlobeAtlas):

Check warning on line 108 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L108

Added line #L108 was not covered by tests
# check additional references are different, but have same dimensions
pass


def validate_mesh_structure_pairs(atlas_name: str, atlas_path: Path):
# json_path = Path(atlas_path / "structures.json")
atlas = BrainGlobeAtlas(atlas_name)
def validate_mesh_structure_pairs(atlas: BrainGlobeAtlas):

Check warning on line 113 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L113

Added line #L113 was not covered by tests
"""Ensure mesh files (.obj) exist for each expected structure in the atlas."""
ids_from_bg_atlas_api = list(atlas.structures.keys())

Check warning on line 115 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L115

Added line #L115 was not covered by tests

atlas_path = (

Check warning on line 117 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L117

Added line #L117 was not covered by tests
Path(get_brainglobe_dir())
/ f"{atlas.atlas_name}_v{get_local_atlas_version(atlas.atlas_name)}"
)
obj_path = Path(atlas_path / "meshes")

ids_from_bg_atlas_api = list(atlas.structures.keys())
ids_from_mesh_files = [
int(Path(file).stem)
for file in os.listdir(obj_path)
Expand All @@ -135,7 +143,7 @@
)


def validate_atlas(atlas_name, version, all_validation_functions):
def validate_atlas(atlas_name, version, validation_functions):

Check warning on line 146 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L146

Added line #L146 was not covered by tests
"""Validates the latest version of a given atlas"""

print(atlas_name, version)
Expand All @@ -144,34 +152,16 @@
if not updated:
update_atlas(atlas_name)

validation_function_parameters = [
# validate_atlas_files(atlas_path: Path)
(Path(get_brainglobe_dir() / f"{atlas_name}_v{version}"),),
# validate_mesh_matches_image_extents(atlas: BrainGlobeAtlas)
(BrainGlobeAtlas(atlas_name),),
# open_for_visual_check()
(),
# validate_checksum()
(),
# check_additional_references()
(),
# validate_mesh_structure_pairs(atlas_name: str, atlas_path: Path):
(
atlas_name,
Path(get_brainglobe_dir() / f"{atlas_name}_v{version}"),
),
]

# list to store the errors of the failed validations
failed_validations = []
successful_validations = []
failed_validations = {atlas_name: []}
successful_validations = {atlas_name: []}

Check warning on line 157 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L156-L157

Added lines #L156 - L157 were not covered by tests

for i, validation_function in enumerate(all_validation_functions):
for i, validation_function in enumerate(validation_functions):

Check warning on line 159 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L159

Added line #L159 was not covered by tests
try:
validation_function(*validation_function_parameters[i])
successful_validations.append((atlas_name, validation_function))
validation_function(BrainGlobeAtlas(atlas_name))
successful_validations[atlas_name].append(validation_function)

Check warning on line 162 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L161-L162

Added lines #L161 - L162 were not covered by tests
except AssertionError as error:
failed_validations.append((atlas_name, validation_function, error))
failed_validations[atlas_name].append((validation_function, error))

Check warning on line 164 in bg_atlasgen/validate_atlases.py

View check run for this annotation

Codecov / codecov/patch

bg_atlasgen/validate_atlases.py#L164

Added line #L164 was not covered by tests

return successful_validations, failed_validations

Expand Down
41 changes: 29 additions & 12 deletions tests/test_unit/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pathlib import Path
import os

import numpy as np
import pytest
Expand All @@ -12,13 +12,33 @@
)


def test_validate_mesh_matches_image_extents():
atlas = BrainGlobeAtlas("allen_mouse_100um")
@pytest.fixture
def atlas():
"""A fixture providing a low-res Allen Mouse atlas for testing.
Tests assume this atlas is valid"""
return BrainGlobeAtlas("allen_mouse_100um")


@pytest.fixture
def atlas_with_bad_reference_file():
"""A fixture providing an invalid version of Allen Mouse atlas for testing.
The atlas will have a misnamed template file that won't be found by the API
This fixture also does the clean-up after the test has run
"""
good_name = get_brainglobe_dir() / "allen_mouse_100um_v1.2/reference.tiff"
bad_name = (
get_brainglobe_dir() / "allen_mouse_100um_v1.2/reference_bad.tiff"
)
os.rename(good_name, bad_name)
yield BrainGlobeAtlas("allen_mouse_100um")
os.rename(bad_name, good_name)


def test_validate_mesh_matches_image_extents(atlas):
assert validate_mesh_matches_image_extents(atlas)


def test_validate_mesh_matches_image_extents_negative(mocker):
atlas = BrainGlobeAtlas("allen_mouse_100um")
def test_validate_mesh_matches_image_extents_negative(mocker, atlas):
flipped_annotation_image = np.transpose(atlas.annotation)
mocker.patch(
"bg_atlasapi.BrainGlobeAtlas.annotation",
Expand All @@ -31,16 +51,13 @@ def test_validate_mesh_matches_image_extents_negative(mocker):
validate_mesh_matches_image_extents(atlas)


def test_valid_atlas_files():
_ = BrainGlobeAtlas("allen_mouse_100um")
atlas_path = Path(get_brainglobe_dir()) / "allen_mouse_100um_v1.2"
assert validate_atlas_files(atlas_path)
def test_valid_atlas_files(atlas):
assert validate_atlas_files(atlas)


def test_invalid_atlas_path():
atlas_path = Path.home()
def test_invalid_atlas_path(atlas_with_bad_reference_file):
with pytest.raises(AssertionError, match="Expected file not found"):
validate_atlas_files(atlas_path)
validate_atlas_files(atlas_with_bad_reference_file)


def test_assert_close():
Expand Down
Loading