Skip to content

Commit

Permalink
Fix nans in compute_region_volumes (#80)
Browse files Browse the repository at this point in the history
* hat tip to Pranav Rai [rai-pranav] for finding and suggesting a fix
  • Loading branch information
mgeplf authored May 15, 2024
1 parent 924b6c6 commit 6a91d1a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion atlas_densities/densities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 10 additions & 18 deletions tests/densities/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6a91d1a

Please sign in to comment.