diff --git a/brainglobe_napari_io/workflows/__init__.py b/brainglobe_napari_io/brainmapper/__init__.py similarity index 100% rename from brainglobe_napari_io/workflows/__init__.py rename to brainglobe_napari_io/brainmapper/__init__.py diff --git a/brainglobe_napari_io/workflows/wholebrain_cell_reader_dir.py b/brainglobe_napari_io/brainmapper/brainmapper_reader_dir.py similarity index 87% rename from brainglobe_napari_io/workflows/wholebrain_cell_reader_dir.py rename to brainglobe_napari_io/brainmapper/brainmapper_reader_dir.py index 8aa4605..d452882 100644 --- a/brainglobe_napari_io/workflows/wholebrain_cell_reader_dir.py +++ b/brainglobe_napari_io/brainmapper/brainmapper_reader_dir.py @@ -1,7 +1,7 @@ import json import os from pathlib import Path -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Dict, List, Optional, Tuple, Union import bg_space as bgs from napari.types import LayerDataTuple @@ -14,9 +14,9 @@ PathOrPaths = Union[List[os.PathLike], os.PathLike] -def is_wholebrain_cell_dir(path: os.PathLike) -> bool: +def is_brainmapper_dir(path: os.PathLike) -> bool: """ - Determines whether a path is to a BrainGlobe workflows whole brain + Determines whether a path is to a BrainGlobe brainmapper whole brain cell detection (previously cellfinder) output directory. """ path = os.path.abspath(path) @@ -24,12 +24,15 @@ def is_wholebrain_cell_dir(path: os.PathLike) -> bool: filelist = os.listdir(path) else: return False - if "cellfinder.json" in filelist: + if "brainmapper.json" in filelist: + return True + # for backwards compatibility + elif "cellfinder.json" in filelist: return True return False -def wholebrain_cell_read_dir(path: PathOrPaths) -> Optional[Callable]: +def brainmapper_read_dir(path: PathOrPaths) -> Optional[Callable]: """A basic implementation of the napari_get_reader hook specification. Parameters @@ -43,12 +46,23 @@ def wholebrain_cell_read_dir(path: PathOrPaths) -> Optional[Callable]: If the path is a recognized format, return a function that accepts the same path or list of paths, and returns a list of layer data tuples. """ - if isinstance(path, str) and is_wholebrain_cell_dir(path): + if isinstance(path, str) and is_brainmapper_dir(path): return reader_function else: return None +def get_metadata(directory: Path) -> Dict: + try: + with open(directory / "brainmapper.json") as json_file: + metadata = json.load(json_file) + # for backwards compatibility + except FileNotFoundError: + with open(directory / "cellfinder.json") as json_file: + metadata = json.load(json_file) + return metadata + + def reader_function( path: os.PathLike, point_size: int = 15, @@ -78,10 +92,9 @@ def reader_function( layer_type=="image" if not provided """ - print("Loading whole brain cell detection directory") + print("Loading brainmapper directory") path = Path(os.path.abspath(path)) - with open(path / "cellfinder.json") as json_file: - metadata = json.load(json_file) + metadata = get_metadata(path) layers: List[LayerDataTuple] = [] diff --git a/brainglobe_napari_io/napari.yaml b/brainglobe_napari_io/napari.yaml index 3ec292e..780bb21 100644 --- a/brainglobe_napari_io/napari.yaml +++ b/brainglobe_napari_io/napari.yaml @@ -10,9 +10,9 @@ contributions: title: Brainreg Read Directory Atlas Space python_name: brainglobe_napari_io.brainreg.reader_dir_atlas_space:brainreg_read_dir_atlas_space - - id: brainglobe-napari-io.workflows_wholebrain_cell_read_dir - title: Workflows Whole Brain Cell Detection Read Directory - python_name: brainglobe_napari_io.workflows.wholebrain_cell_reader_dir:wholebrain_cell_read_dir + - id: brainglobe-napari-io.brainmapper_read_dir + title: Brainmapper Read Directory + python_name: brainglobe_napari_io.brainmapper.brainmapper_reader_dir:brainmapper_read_dir - id: brainglobe-napari-io.cellfinder_read_xml title: Cellfinder Read XML @@ -34,7 +34,7 @@ contributions: - '*.tiff' accepts_directories: true - - command: brainglobe-napari-io.workflows_wholebrain_cell_read_dir + - command: brainglobe-napari-io.brainmapper_read_dir filename_patterns: - '*.tif' accepts_directories: true diff --git a/tests/data/brainmapper_output/brainmapper.json b/tests/data/brainmapper_output/brainmapper.json new file mode 100755 index 0000000..56d92d0 --- /dev/null +++ b/tests/data/brainmapper_output/brainmapper.json @@ -0,0 +1 @@ +{"signal_planes_paths": ["2"], "background_planes_path": ["4"], "output_dir": "output", "signal_ch_ids": null, "background_ch_id": null, "registration_config": "cellfinder.conf.custom", "voxel_sizes": ["5", "2.31", "2.31"], "network_voxel_sizes": [5, 1, 1], "no_detection": false, "no_classification": false, "no_register": false, "no_analyse": false, "no_figures": false, "start_plane": 0, "end_plane": -1, "save_planes": false, "outlier_keep": false, "artifact_keep": false, "max_cluster_size": 100000, "soma_diameter": 16, "ball_xy_size": 6, "ball_z_size": 15, "ball_overlap_fraction": 0.6, "log_sigma_size": 0.2, "n_sds_above_mean_thresh": 10, "soma_spread_factor": 1.4, "trained_model": "model.h5", "model_weights": null, "network_depth": "50", "batch_size": 64, "cube_width": 50, "cube_height": 50, "cube_depth": 20, "save_empty_cubes": false, "n_free_cpus": 8, "max_ram": null, "save_csv": false, "debug": false, "sort_input_file": false, "no_models": false, "model": "resnet50_tv", "heatmap_smooth": 100, "mask_figures": true, "install_path": ".cellfinder", "download_path": "tmp", "no_amend_config": false, "atlas": "allen_mouse_100um", "orientation": "prs", "backend": "niftyreg", "affine_n_steps": 6, "affine_use_n_steps": 5, "freeform_n_steps": 6, "freeform_use_n_steps": 4, "bending_energy_weight": 0.95, "grid_spacing": -10, "smoothing_sigma_reference": -1.0, "smoothing_sigma_floating": -1.0, "histogram_n_bins_floating": 128, "histogram_n_bins_reference": 128, "paths": {"output_dir": "output_dir", "registration_output_folder": "output_dir/brainreg_output", "metadata_path": "output/brainmapper.json", "registration_metadata_path": "output_dir/brainreg_output/brainreg.json"}} diff --git a/tests/data/wholebrain_cell_output/points/cell_classification.xml b/tests/data/brainmapper_output/points/cell_classification.xml similarity index 100% rename from tests/data/wholebrain_cell_output/points/cell_classification.xml rename to tests/data/brainmapper_output/points/cell_classification.xml diff --git a/tests/data/wholebrain_cell_output/registration/boundaries.tiff b/tests/data/brainmapper_output/registration/boundaries.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/boundaries.tiff rename to tests/data/brainmapper_output/registration/boundaries.tiff diff --git a/tests/data/wholebrain_cell_output/registration/brainreg.json b/tests/data/brainmapper_output/registration/brainreg.json similarity index 100% rename from tests/data/wholebrain_cell_output/registration/brainreg.json rename to tests/data/brainmapper_output/registration/brainreg.json diff --git a/tests/data/wholebrain_cell_output/registration/deformation_field_0.tiff b/tests/data/brainmapper_output/registration/deformation_field_0.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/deformation_field_0.tiff rename to tests/data/brainmapper_output/registration/deformation_field_0.tiff diff --git a/tests/data/wholebrain_cell_output/registration/deformation_field_1.tiff b/tests/data/brainmapper_output/registration/deformation_field_1.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/deformation_field_1.tiff rename to tests/data/brainmapper_output/registration/deformation_field_1.tiff diff --git a/tests/data/wholebrain_cell_output/registration/deformation_field_2.tiff b/tests/data/brainmapper_output/registration/deformation_field_2.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/deformation_field_2.tiff rename to tests/data/brainmapper_output/registration/deformation_field_2.tiff diff --git a/tests/data/wholebrain_cell_output/registration/downsampled.tiff b/tests/data/brainmapper_output/registration/downsampled.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/downsampled.tiff rename to tests/data/brainmapper_output/registration/downsampled.tiff diff --git a/tests/data/wholebrain_cell_output/registration/downsampled_brain.tiff b/tests/data/brainmapper_output/registration/downsampled_brain.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/downsampled_brain.tiff rename to tests/data/brainmapper_output/registration/downsampled_brain.tiff diff --git a/tests/data/wholebrain_cell_output/registration/downsampled_standard.tiff b/tests/data/brainmapper_output/registration/downsampled_standard.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/downsampled_standard.tiff rename to tests/data/brainmapper_output/registration/downsampled_standard.tiff diff --git a/tests/data/wholebrain_cell_output/registration/downsampled_standard_brain.tiff b/tests/data/brainmapper_output/registration/downsampled_standard_brain.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/downsampled_standard_brain.tiff rename to tests/data/brainmapper_output/registration/downsampled_standard_brain.tiff diff --git a/tests/data/wholebrain_cell_output/registration/registered_atlas.tiff b/tests/data/brainmapper_output/registration/registered_atlas.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/registered_atlas.tiff rename to tests/data/brainmapper_output/registration/registered_atlas.tiff diff --git a/tests/data/wholebrain_cell_output/registration/registered_hemispheres.tiff b/tests/data/brainmapper_output/registration/registered_hemispheres.tiff similarity index 100% rename from tests/data/wholebrain_cell_output/registration/registered_hemispheres.tiff rename to tests/data/brainmapper_output/registration/registered_hemispheres.tiff diff --git a/tests/data/wholebrain_cell_output/registration/volumes.csv b/tests/data/brainmapper_output/registration/volumes.csv similarity index 100% rename from tests/data/wholebrain_cell_output/registration/volumes.csv rename to tests/data/brainmapper_output/registration/volumes.csv diff --git a/tests/data/wholebrain_cell_output/cellfinder.json b/tests/data/cellfinder_output/cellfinder.json similarity index 100% rename from tests/data/wholebrain_cell_output/cellfinder.json rename to tests/data/cellfinder_output/cellfinder.json diff --git a/tests/tests/test_integration/test_brainmapper_reader.py b/tests/tests/test_integration/test_brainmapper_reader.py new file mode 100644 index 0000000..0150716 --- /dev/null +++ b/tests/tests/test_integration/test_brainmapper_reader.py @@ -0,0 +1,90 @@ +import json +import pathlib + +import pytest + +from brainglobe_napari_io.brainmapper import brainmapper_reader_dir + +brainmapper_dir = ( + pathlib.Path(__file__).parent.parent.parent / "data" / "brainmapper_output" +) +registration_dir = brainmapper_dir / "registration" + +cellfinder_dir = ( + pathlib.Path(__file__).parent.parent.parent / "data" / "cellfinder_output" +) +DOWNSAMPLED_IMAGE_SIZE = (135, 108, 77) + + +@pytest.fixture +def metadata(): + with open(brainmapper_dir / "brainmapper.json") as json_file: + metadata = json.load(json_file) + return metadata + + +def test_is_brainmapper_dir(): + assert brainmapper_reader_dir.is_brainmapper_dir(brainmapper_dir) + assert not brainmapper_reader_dir.is_brainmapper_dir( + brainmapper_dir.parent + ) + assert not brainmapper_reader_dir.is_brainmapper_dir(__file__) + + +def test_is_cellfinder_dir(): + assert brainmapper_reader_dir.is_brainmapper_dir(cellfinder_dir) + + +def test_brainmapper_read_dir(): + assert ( + brainmapper_reader_dir.brainmapper_read_dir(str(brainmapper_dir)) + == brainmapper_reader_dir.reader_function + ) + assert brainmapper_reader_dir.brainmapper_read_dir(brainmapper_dir) is None + assert ( + brainmapper_reader_dir.brainmapper_read_dir( + str(brainmapper_dir.parent) + ) + is None + ) + + +def check_metadata(metadata): + assert metadata["atlas"] == "allen_mouse_100um" + assert metadata["orientation"] == "prs" + assert metadata["soma_diameter"] == 16 + + +def test_get_metadata(): + check_metadata(brainmapper_reader_dir.get_metadata(brainmapper_dir)) + check_metadata(brainmapper_reader_dir.get_metadata(cellfinder_dir)) + + +def test_load_brainmapper_dir(): + layers = brainmapper_reader_dir.reader_function(brainmapper_dir) + assert len(layers) == 4 + + layer_names = ["allen_mouse_100um", "Boundaries", "Non cells", "Cells"] + layer_types = ["labels", "image", "points", "points"] + layer_shapes = [ + DOWNSAMPLED_IMAGE_SIZE, + DOWNSAMPLED_IMAGE_SIZE, + (22, 3), + (103, 3), + ] + for idx, layer in enumerate(layers): + assert layer[1]["name"] == layer_names[idx] + assert layer[2] == layer_types[idx] + assert layer[0].shape == layer_shapes[idx] + + +def test_load_registration(metadata): + layers = brainmapper_reader_dir.load_registration( + [], registration_dir, metadata + ) + assert len(layers) == 2 + assert layers[0][0].shape == DOWNSAMPLED_IMAGE_SIZE + assert layers[1][0].shape == DOWNSAMPLED_IMAGE_SIZE + + assert layers[0][1]["name"] == "allen_mouse_100um" + assert layers[1][1]["name"] == "Boundaries" diff --git a/tests/tests/test_integration/test_brainreg_reader.py b/tests/tests/test_integration/test_brainreg_reader.py index 027ab48..595fa8e 100644 --- a/tests/tests/test_integration/test_brainreg_reader.py +++ b/tests/tests/test_integration/test_brainreg_reader.py @@ -5,7 +5,7 @@ brainreg_dir = ( pathlib.Path(__file__).parent.parent.parent / "data" - / "wholebrain_cell_output" + / "brainmapper_output" / "registration" ) diff --git a/tests/tests/test_integration/test_brainreg_reader_atlas_space.py b/tests/tests/test_integration/test_brainreg_reader_atlas_space.py index eae241d..dfde6de 100644 --- a/tests/tests/test_integration/test_brainreg_reader_atlas_space.py +++ b/tests/tests/test_integration/test_brainreg_reader_atlas_space.py @@ -5,7 +5,7 @@ brainreg_dir = ( pathlib.Path(__file__).parent.parent.parent / "data" - / "wholebrain_cell_output" + / "brainmapper_output" / "registration" ) diff --git a/tests/tests/test_integration/test_wholebrain_cell_reader.py b/tests/tests/test_integration/test_wholebrain_cell_reader.py deleted file mode 100644 index 4cd6084..0000000 --- a/tests/tests/test_integration/test_wholebrain_cell_reader.py +++ /dev/null @@ -1,83 +0,0 @@ -import json -import pathlib - -import pytest - -from brainglobe_napari_io.workflows import wholebrain_cell_reader_dir - -wholebrain_cell_dir = ( - pathlib.Path(__file__).parent.parent.parent - / "data" - / "wholebrain_cell_output" -) -registration_dir = wholebrain_cell_dir / "registration" - -DOWNSAMPLED_IMAGE_SIZE = (135, 108, 77) - - -@pytest.fixture -def metadata(): - with open(wholebrain_cell_dir / "cellfinder.json") as json_file: - metadata = json.load(json_file) - return metadata - - -def test_is_wholebrain_cell_dir(): - assert wholebrain_cell_reader_dir.is_wholebrain_cell_dir( - wholebrain_cell_dir - ) - assert not wholebrain_cell_reader_dir.is_wholebrain_cell_dir( - wholebrain_cell_dir.parent - ) - assert not wholebrain_cell_reader_dir.is_wholebrain_cell_dir(__file__) - - -def test_wholebrain_cell_read_dir(): - assert ( - wholebrain_cell_reader_dir.wholebrain_cell_read_dir( - str(wholebrain_cell_dir) - ) - == wholebrain_cell_reader_dir.reader_function - ) - assert ( - wholebrain_cell_reader_dir.wholebrain_cell_read_dir( - wholebrain_cell_dir - ) - is None - ) - assert ( - wholebrain_cell_reader_dir.wholebrain_cell_read_dir( - str(wholebrain_cell_dir.parent) - ) - is None - ) - - -def test_load_wholebrain_cell_dir(): - layers = wholebrain_cell_reader_dir.reader_function(wholebrain_cell_dir) - assert len(layers) == 4 - - layer_names = ["allen_mouse_100um", "Boundaries", "Non cells", "Cells"] - layer_types = ["labels", "image", "points", "points"] - layer_shapes = [ - DOWNSAMPLED_IMAGE_SIZE, - DOWNSAMPLED_IMAGE_SIZE, - (22, 3), - (103, 3), - ] - for idx, layer in enumerate(layers): - assert layer[1]["name"] == layer_names[idx] - assert layer[2] == layer_types[idx] - assert layer[0].shape == layer_shapes[idx] - - -def test_load_registration(metadata): - layers = wholebrain_cell_reader_dir.load_registration( - [], registration_dir, metadata - ) - assert len(layers) == 2 - assert layers[0][0].shape == DOWNSAMPLED_IMAGE_SIZE - assert layers[1][0].shape == DOWNSAMPLED_IMAGE_SIZE - - assert layers[0][1]["name"] == "allen_mouse_100um" - assert layers[1][1]["name"] == "Boundaries" diff --git a/tests/tests/test_unit/test_brainreg_utils.py b/tests/tests/test_unit/test_brainreg_utils.py index 96756c6..1554b41 100644 --- a/tests/tests/test_unit/test_brainreg_utils.py +++ b/tests/tests/test_unit/test_brainreg_utils.py @@ -5,7 +5,7 @@ brainreg_dir = ( pathlib.Path(__file__).parent.parent.parent / "data" - / "wholebrain_cell_output" + / "brainmapper_output" / "registration" )