From 76c491b34fad036ddcc88379e3bec99d68cb90b7 Mon Sep 17 00:00:00 2001 From: Mike Gevaert Date: Wed, 15 May 2024 15:47:19 +0200 Subject: [PATCH] Fix nans in `compute_region_volumes` * hat tip to Pranav Rai [rai-pranav] for finding and suggesting a fix --- atlas_densities/densities/utils.py | 2 +- tests/densities/test_utils.py | 28 ++++++++++------------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/atlas_densities/densities/utils.py b/atlas_densities/densities/utils.py index a210ade..2251e04 100644 --- a/atlas_densities/densities/utils.py +++ b/atlas_densities/densities/utils.py @@ -530,7 +530,7 @@ def compute_region_volumes( ) ids, counts = np.unique(annotation, return_counts=True) - result["id_volume"] = pd.Series(counts * voxel_volume, index=ids) + result["id_volume"].update(pd.Series(counts * voxel_volume, index=ids)) volumes = [] for id_ in hierarchy_info.index: diff --git a/tests/densities/test_utils.py b/tests/densities/test_utils.py index a370327..2cb6f1d 100644 --- a/tests/densities/test_utils.py +++ b/tests/densities/test_utils.py @@ -372,28 +372,20 @@ def test_get_hierarchy(region_map): ) -@pytest.fixture -def annotation(): - return np.array([[[920, 10710, 10710], [10709, 10708, 976], [10708, 10710, 10709]]]) - - -@pytest.fixture -def volumes(voxel_volume=2): +def test_compute_region_volumes(): + voxel_volume = 2.0 + annotation = np.array([[[920, 10710, 10710], [10709, 10708, 976], [10708, 10710, 10709]]]) hierarchy_info = get_hierarchy_info() - return pd.DataFrame( + hierarchy_info.loc[42] = ("VolumeLess", set()) + expected = pd.DataFrame( { "brain_region": hierarchy_info["brain_region"], - "id_volume": voxel_volume * np.array([1.0, 1.0, 2.0, 2.0, 3.0], dtype=float), - "volume": voxel_volume * np.array([9.0, 8.0, 2.0, 2.0, 3.0], dtype=float), + "id_volume": voxel_volume * np.array([1.0, 1.0, 2.0, 2.0, 3.0, 0.0], dtype=float), + "volume": voxel_volume * np.array([9.0, 8.0, 2.0, 2.0, 3.0, 0.0], dtype=float), }, index=hierarchy_info.index, ) - - -def test_compute_region_volumes(volumes, annotation): - pdt.assert_frame_equal( - volumes, # expected - tested.compute_region_volumes( - annotation, voxel_volume=2.0, hierarchy_info=get_hierarchy_info() - ), + res = tested.compute_region_volumes( + annotation, voxel_volume=voxel_volume, hierarchy_info=hierarchy_info ) + pdt.assert_frame_equal(expected, res)