Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix_nan_id_counts
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed May 15, 2024
2 parents 16bdf61 + 6a91d1a commit 45f6c75
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.update(pd.Series(counts * voxel_volume, index=ids).rename("id_volume"))

This comment has been minimized.

Copy link
@rai-pranav

rai-pranav May 21, 2024

Collaborator

I had added the rename method, because removing it will be give you the following warning -

FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.

  result["id_volume"].update(pd.Series(counts * voxel_volume, index=ids))

I think it better to keep the rename method.

This comment has been minimized.

Copy link
@mgeplf

mgeplf Jun 20, 2024

Author Collaborator

Sorry for the wait; I had done an update quelling all warnings in #82

I just merged those changes into this branch, and there aren't any warnings.

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 45f6c75

Please sign in to comment.