Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pandas.df.append with safe_concat #259

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions cellfinder/analyse/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tifffile
from bg_atlasapi import BrainGlobeAtlas
from brainglobe_utils.general.system import ensure_directory_exists
from brainglobe_utils.pandas.misc import sanitise_df
from brainglobe_utils.pandas.misc import safe_pandas_concat, sanitise_df

Check warning on line 19 in cellfinder/analyse/analyse.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/analyse/analyse.py#L19

Added line #L19 was not covered by tests

from cellfinder.export.export import export_points

Expand Down Expand Up @@ -155,13 +155,16 @@
]
)
if n_points:
point_numbers = point_numbers.append(
{
"structure_name": structure,
"hemisphere": hemisphere,
"cell_count": n_points,
},
ignore_index=True,
point_numbers = safe_pandas_concat(

Check warning on line 158 in cellfinder/analyse/analyse.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/analyse/analyse.py#L158

Added line #L158 was not covered by tests
point_numbers,
pd.DataFrame(
data=[[structure, hemisphere, n_points]],
columns=[
"structure_name",
"hemisphere",
"cell_count",
],
),
)
sorted_point_numbers = point_numbers.sort_values(
by=["cell_count"], ascending=False
Expand Down
663 changes: 663 additions & 0 deletions tests/data/analyse/region_totals.csv

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions tests/data/analyse/region_totals_regression_pandas1_5_3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure_name,left_cell_count,right_cell_count,total_cells,left_volume_mm3,right_volume_mm3,total_volume_mm3,left_cells_per_mm3,right_cells_per_mm3
Anterodorsal nucleus,1.0,0.0,1.0,0.083,0.075,0.158,12.048192771084336,0.0
"Paraventricular hypothalamic nucleus, descending division",0.0,1.0,1.0,0.075,0.065,0.14,0.0,15.384615384615383
Lateral terminal nucleus of the accessory optic tract,0.0,0.0,0.0,0.01,0.009,0.019,0.0,0.0
Interstitial nucleus of Cajal,0.0,0.0,0.0,0.049,0.047,0.096,0.0,0.0
5 changes: 5 additions & 0 deletions tests/data/analyse/volumes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure_name,left_volume_mm3,right_volume_mm3,total_volume_mm3
"Paraventricular hypothalamic nucleus, descending division",0.075,0.065,0.14
Anterodorsal nucleus,0.083,0.075,0.158
Lateral terminal nucleus of the accessory optic tract,0.01,0.009000000000000001,0.019000000000000003
Interstitial nucleus of Cajal,0.049,0.047,0.096
54 changes: 54 additions & 0 deletions tests/tests/test_unit/test_analyse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import filecmp
import os
from pathlib import Path

import pytest

from cellfinder.analyse.analyse import Point, get_region_totals


@pytest.fixture
def points():
p1 = Point(
raw_coordinate=[1.0, 1.0, 1.0],
atlas_coordinate=[1.0, 1.0, 1.0],
structure="Paraventricular hypothalamic nucleus, descending division",
structure_id=56,
hemisphere="right",
)
p2 = Point(
raw_coordinate=[2.0, 2.0, 2.0],
atlas_coordinate=[2.0, 2.0, 2.0],
structure="Anterodorsal nucleus",
structure_id=57,
hemisphere="left",
)
return [p1, p2]


@pytest.fixture
def structures_with_points():
return [
"Paraventricular hypothalamic nucleus, descending division",
"Anterodorsal nucleus",
]


def test_get_region_totals(tmp_path, points, structures_with_points):
"""Regression test for get_region_totals for pandas 1.5.3 -> 2.1.3+.
pd.Dataframe.append was deprecated and remove in this time."""
OUTPUT_DATA_LOC = (
Path(os.path.dirname(os.path.abspath(__file__))) / "../../data/analyse"
).resolve()

volumes_path = OUTPUT_DATA_LOC / "volumes.csv"
expected_output = (
OUTPUT_DATA_LOC / "region_totals_regression_pandas1_5_3.csv"
)

output_path = Path(tmp_path / "tmp_region_totals.csv")
get_region_totals(
points, structures_with_points, volumes_path, output_path
)
assert output_path.exists()
assert filecmp.cmp(output_path, expected_output)
4 changes: 2 additions & 2 deletions tests/tests/test_unit/test_tools/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

def test_get_subdirectories():
subdirs = system.get_subdirectories(data_dir)
assert len(subdirs) == 9
assert len(subdirs) == 10
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
assert Path(data_dir / "cells") in subdirs
assert Path(data_dir / "brain") in subdirs

subdir_names = system.get_subdirectories(data_dir, names_only=True)
assert len(subdir_names) == 9
assert len(subdir_names) == 10
assert "cells" in subdir_names
assert "brain" in subdir_names

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INPUT_COREDEV =
true: coredev

[testenv]
commands = pytest -v --cov=./ --cov-report=xml
commands = pytest {toxinidir} -v --cov=./ --cov-report=xml
deps =
pytest-cov
pytest
Expand Down