From 0b355cb73191d990887a99849fb9202a6b608ecf Mon Sep 17 00:00:00 2001 From: Adam Tyson Date: Tue, 19 Dec 2023 16:23:17 +0000 Subject: [PATCH] Add whole brain cell detection & registration tests --- .../wholebrain_cell_output/cellfinder.json | 1 + .../points/cell_classification.xml | 640 ++++++++++++++++++ .../registration}/boundaries.tiff | Bin .../registration}/brainreg.json | 0 .../registration}/deformation_field_0.tiff | Bin .../registration}/deformation_field_1.tiff | Bin .../registration}/deformation_field_2.tiff | Bin .../registration}/downsampled.tiff | Bin .../registration}/downsampled_brain.tiff | Bin .../registration}/downsampled_standard.tiff | Bin .../downsampled_standard_brain.tiff | Bin .../registration}/registered_atlas.tiff | Bin .../registration}/registered_hemispheres.tiff | Bin .../registration}/volumes.csv | 0 .../test_integration/test_brainreg_reader.py | 27 +- .../test_brainreg_reader_atlas_space.py | 33 +- .../test_wholebrain_cell_reader.py | 50 ++ tests/tests/test_unit/test_brainreg_utils.py | 13 +- 18 files changed, 748 insertions(+), 16 deletions(-) create mode 100755 tests/data/wholebrain_cell_output/cellfinder.json create mode 100644 tests/data/wholebrain_cell_output/points/cell_classification.xml rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/boundaries.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/brainreg.json (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/deformation_field_0.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/deformation_field_1.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/deformation_field_2.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/downsampled.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/downsampled_brain.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/downsampled_standard.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/downsampled_standard_brain.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/registered_atlas.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/registered_hemispheres.tiff (100%) rename tests/data/{brainreg_output => wholebrain_cell_output/registration}/volumes.csv (100%) create mode 100644 tests/tests/test_integration/test_wholebrain_cell_reader.py diff --git a/tests/data/wholebrain_cell_output/cellfinder.json b/tests/data/wholebrain_cell_output/cellfinder.json new file mode 100755 index 0000000..5c6e671 --- /dev/null +++ b/tests/data/wholebrain_cell_output/cellfinder.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/cellfinder.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/wholebrain_cell_output/points/cell_classification.xml new file mode 100644 index 0000000..efdc23b --- /dev/null +++ b/tests/data/wholebrain_cell_output/points/cell_classification.xml @@ -0,0 +1,640 @@ + + + + placeholder.tif + + + 1 + + 1 + + 3422 + 2089 + 11 + + + 3407 + 2580 + 11 + + + 1229 + 2570 + 11 + + + 1265 + 1055 + 11 + + + 942 + 1133 + 11 + + + 901 + 1235 + 11 + + + 2771 + 1245 + 11 + + + 2194 + 1252 + 11 + + + 1351 + 1259 + 11 + + + 1715 + 1324 + 11 + + + 2842 + 1341 + 11 + + + 1533 + 1372 + 11 + + + 1336 + 2135 + 2540 + + + 1336 + 3066 + 2540 + + + 1280 + 3221 + 2540 + + + 1556 + 2034 + 2540 + + + 1121 + 2114 + 2540 + + + 2028 + 2137 + 2540 + + + 1923 + 2386 + 2540 + + + 2057 + 2731 + 2540 + + + 1860 + 2739 + 2540 + + + 1399 + 3301 + 2540 + + + + 2 + + 1674 + 2441 + 20 + + + 2194 + 2810 + 120 + + + 1634 + 1891 + 122 + + + 3363 + 2528 + 183 + + + 2158 + 2763 + 187 + + + 1661 + 3150 + 200 + + + 2393 + 1830 + 224 + + + 1236 + 2647 + 238 + + + 1833 + 2733 + 314 + + + 2012 + 1735 + 315 + + + 2520 + 3135 + 318 + + + 2479 + 2402 + 333 + + + 977 + 3615 + 339 + + + 985 + 3616 + 339 + + + 1323 + 3061 + 368 + + + 616 + 1843 + 388 + + + 1252 + 3608 + 408 + + + 1242 + 3608 + 411 + + + 1343 + 3908 + 419 + + + 1273 + 3639 + 419 + + + 1421 + 3977 + 417 + + + 1361 + 3927 + 417 + + + 1358 + 3930 + 421 + + + 1417 + 3980 + 421 + + + 1211 + 3800 + 422 + + + 1263 + 3814 + 422 + + + 1330 + 3749 + 425 + + + 1348 + 3871 + 427 + + + 1385 + 3982 + 431 + + + 3360 + 2122 + 434 + + + 1548 + 4018 + 433 + + + 1049 + 3573 + 435 + + + 1385 + 3908 + 435 + + + 1437 + 3966 + 435 + + + 1468 + 3960 + 436 + + + 2081 + 4073 + 439 + + + 1524 + 3970 + 441 + + + 1415 + 4013 + 441 + + + 1354 + 3769 + 438 + + + 1418 + 4012 + 438 + + + 3367 + 2125 + 438 + + + 1478 + 3891 + 440 + + + 2127 + 4025 + 442 + + + 1244 + 3898 + 443 + + + 1478 + 4015 + 446 + + + 1393 + 3717 + 446 + + + 1468 + 3950 + 446 + + + 1468 + 3948 + 447 + + + 1239 + 3488 + 445 + + + 1486 + 3986 + 448 + + + 1417 + 3697 + 449 + + + 1382 + 3941 + 449 + + + 1433 + 3807 + 451 + + + 1416 + 3887 + 451 + + + 1379 + 3944 + 452 + + + 1460 + 3894 + 453 + + + 1461 + 3793 + 453 + + + 1520 + 3957 + 455 + + + 998 + 3647 + 455 + + + 1417 + 3708 + 460 + + + 1468 + 3944 + 459 + + + 1432 + 3803 + 457 + + + 1526 + 4033 + 456 + + + 1040 + 3718 + 458 + + + 1438 + 3688 + 458 + + + 1432 + 3802 + 458 + + + 1458 + 3884 + 464 + + + 1581 + 4079 + 464 + + + 1408 + 3735 + 465 + + + 1442 + 3686 + 465 + + + 1043 + 3745 + 465 + + + 1435 + 3853 + 465 + + + 1435 + 3682 + 465 + + + 1134 + 3455 + 466 + + + 1742 + 4005 + 467 + + + 1005 + 3697 + 467 + + + 1395 + 3693 + 467 + + + 2269 + 4185 + 467 + + + 1392 + 3695 + 470 + + + 1579 + 3983 + 469 + + + 1572 + 3974 + 473 + + + 1123 + 3423 + 471 + + + 2243 + 3863 + 474 + + + 1420 + 3713 + 475 + + + 1198 + 3691 + 475 + + + 1530 + 3751 + 475 + + + 2518 + 4054 + 477 + + + 1406 + 3754 + 479 + + + 1109 + 3806 + 479 + + + 1154 + 3637 + 482 + + + 1438 + 3725 + 482 + + + 1584 + 3983 + 485 + + + 636 + 2486 + 484 + + + 1346 + 3539 + 486 + + + 670 + 2735 + 486 + + + 1593 + 4069 + 488 + + + 1484 + 3983 + 489 + + + 897 + 3542 + 490 + + + 1505 + 3676 + 490 + + + 1437 + 3635 + 492 + + + 904 + 1256 + 493 + + + 887 + 3337 + 493 + + + 1996 + 3220 + 2529 + + + + diff --git a/tests/data/brainreg_output/boundaries.tiff b/tests/data/wholebrain_cell_output/registration/boundaries.tiff similarity index 100% rename from tests/data/brainreg_output/boundaries.tiff rename to tests/data/wholebrain_cell_output/registration/boundaries.tiff diff --git a/tests/data/brainreg_output/brainreg.json b/tests/data/wholebrain_cell_output/registration/brainreg.json similarity index 100% rename from tests/data/brainreg_output/brainreg.json rename to tests/data/wholebrain_cell_output/registration/brainreg.json diff --git a/tests/data/brainreg_output/deformation_field_0.tiff b/tests/data/wholebrain_cell_output/registration/deformation_field_0.tiff similarity index 100% rename from tests/data/brainreg_output/deformation_field_0.tiff rename to tests/data/wholebrain_cell_output/registration/deformation_field_0.tiff diff --git a/tests/data/brainreg_output/deformation_field_1.tiff b/tests/data/wholebrain_cell_output/registration/deformation_field_1.tiff similarity index 100% rename from tests/data/brainreg_output/deformation_field_1.tiff rename to tests/data/wholebrain_cell_output/registration/deformation_field_1.tiff diff --git a/tests/data/brainreg_output/deformation_field_2.tiff b/tests/data/wholebrain_cell_output/registration/deformation_field_2.tiff similarity index 100% rename from tests/data/brainreg_output/deformation_field_2.tiff rename to tests/data/wholebrain_cell_output/registration/deformation_field_2.tiff diff --git a/tests/data/brainreg_output/downsampled.tiff b/tests/data/wholebrain_cell_output/registration/downsampled.tiff similarity index 100% rename from tests/data/brainreg_output/downsampled.tiff rename to tests/data/wholebrain_cell_output/registration/downsampled.tiff diff --git a/tests/data/brainreg_output/downsampled_brain.tiff b/tests/data/wholebrain_cell_output/registration/downsampled_brain.tiff similarity index 100% rename from tests/data/brainreg_output/downsampled_brain.tiff rename to tests/data/wholebrain_cell_output/registration/downsampled_brain.tiff diff --git a/tests/data/brainreg_output/downsampled_standard.tiff b/tests/data/wholebrain_cell_output/registration/downsampled_standard.tiff similarity index 100% rename from tests/data/brainreg_output/downsampled_standard.tiff rename to tests/data/wholebrain_cell_output/registration/downsampled_standard.tiff diff --git a/tests/data/brainreg_output/downsampled_standard_brain.tiff b/tests/data/wholebrain_cell_output/registration/downsampled_standard_brain.tiff similarity index 100% rename from tests/data/brainreg_output/downsampled_standard_brain.tiff rename to tests/data/wholebrain_cell_output/registration/downsampled_standard_brain.tiff diff --git a/tests/data/brainreg_output/registered_atlas.tiff b/tests/data/wholebrain_cell_output/registration/registered_atlas.tiff similarity index 100% rename from tests/data/brainreg_output/registered_atlas.tiff rename to tests/data/wholebrain_cell_output/registration/registered_atlas.tiff diff --git a/tests/data/brainreg_output/registered_hemispheres.tiff b/tests/data/wholebrain_cell_output/registration/registered_hemispheres.tiff similarity index 100% rename from tests/data/brainreg_output/registered_hemispheres.tiff rename to tests/data/wholebrain_cell_output/registration/registered_hemispheres.tiff diff --git a/tests/data/brainreg_output/volumes.csv b/tests/data/wholebrain_cell_output/registration/volumes.csv similarity index 100% rename from tests/data/brainreg_output/volumes.csv rename to tests/data/wholebrain_cell_output/registration/volumes.csv diff --git a/tests/tests/test_integration/test_brainreg_reader.py b/tests/tests/test_integration/test_brainreg_reader.py index 6a4dce1..027ab48 100644 --- a/tests/tests/test_integration/test_brainreg_reader.py +++ b/tests/tests/test_integration/test_brainreg_reader.py @@ -1,24 +1,37 @@ import pathlib - from brainglobe_napari_io.brainreg import reader_dir -brainreg_dir = pathlib.Path(__file__).parent.parent.parent / "data" / "brainreg_output" +brainreg_dir = ( + pathlib.Path(__file__).parent.parent.parent + / "data" + / "wholebrain_cell_output" + / "registration" +) + def test_brainreg_read_dir(): - assert reader_dir.brainreg_read_dir(str(brainreg_dir)) == reader_dir.reader_function + assert ( + reader_dir.brainreg_read_dir(str(brainreg_dir)) + == reader_dir.reader_function + ) assert reader_dir.brainreg_read_dir(brainreg_dir) is None assert reader_dir.brainreg_read_dir(str(brainreg_dir.parent)) is None + def test_load_brainreg_dir(): layers = reader_dir.reader_function(brainreg_dir) assert len(layers) == 5 - layer_names = ["brain (downsampled)", "Registered image", "Hemispheres", - "allen_mouse_100um", "Boundaries"] + layer_names = [ + "brain (downsampled)", + "Registered image", + "Hemispheres", + "allen_mouse_100um", + "Boundaries", + ] - layer_types = ["image", "image", "labels", - "labels", "image"] + layer_types = ["image", "image", "labels", "labels", "image"] for idx, layer in enumerate(layers): assert layer[1]["name"] == layer_names[idx] assert layer[2] == layer_types[idx] 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 5481460..eae241d 100644 --- a/tests/tests/test_integration/test_brainreg_reader_atlas_space.py +++ b/tests/tests/test_integration/test_brainreg_reader_atlas_space.py @@ -1,20 +1,41 @@ import pathlib - from brainglobe_napari_io.brainreg import reader_dir_atlas_space -brainreg_dir = pathlib.Path(__file__).parent.parent.parent / "data" / "brainreg_output" +brainreg_dir = ( + pathlib.Path(__file__).parent.parent.parent + / "data" + / "wholebrain_cell_output" + / "registration" +) + def test_brainreg_read_dir_atlas_space(): - assert reader_dir_atlas_space.brainreg_read_dir_atlas_space(str(brainreg_dir)) == reader_dir_atlas_space.reader_function - assert reader_dir_atlas_space.brainreg_read_dir_atlas_space(brainreg_dir) is None - assert reader_dir_atlas_space.brainreg_read_dir_atlas_space(str(brainreg_dir.parent)) is None + assert ( + reader_dir_atlas_space.brainreg_read_dir_atlas_space(str(brainreg_dir)) + == reader_dir_atlas_space.reader_function + ) + assert ( + reader_dir_atlas_space.brainreg_read_dir_atlas_space(brainreg_dir) + is None + ) + assert ( + reader_dir_atlas_space.brainreg_read_dir_atlas_space( + str(brainreg_dir.parent) + ) + is None + ) + def test_load_brainreg_dir(): layers = reader_dir_atlas_space.reader_function(brainreg_dir) assert len(layers) == 3 - layer_names = ["brain (downsampled)", "Registered image", "allen_mouse_100um"] + layer_names = [ + "brain (downsampled)", + "Registered image", + "allen_mouse_100um", + ] layer_types = ["image", "image", "labels"] for idx, layer in enumerate(layers): diff --git a/tests/tests/test_integration/test_wholebrain_cell_reader.py b/tests/tests/test_integration/test_wholebrain_cell_reader.py new file mode 100644 index 0000000..9745dab --- /dev/null +++ b/tests/tests/test_integration/test_wholebrain_cell_reader.py @@ -0,0 +1,50 @@ +import pathlib +import pytest +import json +from brainglobe_napari_io.workflows import wholebrain_cell_reader_dir +from brainglobe_napari_io.brainreg.reader_dir import ( + reader_function as brainreg_reader, +) +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 b9a960f..96756c6 100644 --- a/tests/tests/test_unit/test_brainreg_utils.py +++ b/tests/tests/test_unit/test_brainreg_utils.py @@ -1,15 +1,22 @@ import pathlib -brainreg_dir = pathlib.Path(__file__).parent.parent.parent / "data" / "brainreg_output" - from brainglobe_napari_io.brainreg import utils +brainreg_dir = ( + pathlib.Path(__file__).parent.parent.parent + / "data" + / "wholebrain_cell_output" + / "registration" +) + + def test_is_brainreg(): assert utils.is_brainreg_dir(brainreg_dir) assert not utils.is_brainreg_dir(__file__) assert not utils.is_brainreg_dir(brainreg_dir.parent) + def test_load_additional_downsampled_channels(): layers = utils.load_additional_downsampled_channels(brainreg_dir, []) assert len(layers) == 1 - assert layers[0][1]["name"] == "brain (downsampled)" \ No newline at end of file + assert layers[0][1]["name"] == "brain (downsampled)"