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

Fix: Raise clear errors for empty angles #65

Merged
merged 3 commits into from
May 4, 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
9 changes: 9 additions & 0 deletions neurots/extract_input/from_TMD.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import tmd

from neurots.utils import NeuroTSError


def persistent_homology_angles(
pop, threshold=2, neurite_type="basal_dendrite", feature="radial_distances"
Expand All @@ -35,10 +37,17 @@ def persistent_homology_angles(
ph_ang = [
tmd.methods.get_ph_angles(tree, feature=feature) for tree in getattr(pop, neurite_type)
]
if not ph_ang:
raise NeuroTSError(f"The given population does contain any tree of {neurite_type} type.")

# Keep only the trees whose number of terminations is above the threshold
# Saves the list of persistence diagrams for the selected neurite_type
phs = [p for p in ph_ang if len(p) > threshold]
if not phs:
raise NeuroTSError(
"The given threshold excluded all bars of the persistence diagram, please use a "
"lower threshold value."
)
min_bar_length = min(min(tmd.analysis.get_lengths(ph)) for ph in phs)

return {"persistence_diagram": phs, "min_bar_length": min_bar_length}
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

reqs = [
"jsonschema>=3.0.1",
"matplotlib>=3.2.1",
"morphio>=3.3.3,<4.0",
"matplotlib>=3.4",
"morphio>=3.3.4,<4.0",
"neurom>=3.0,<4.0",
"numpy>=1.22.0",
"scipy>=1.6",
"tmd>=2.2.0",
"diameter-synthesis>=0.5.2",
"tmd>=2.3.0",
"diameter-synthesis>=0.5.4",
]

doc_reqs = [
Expand All @@ -40,7 +40,6 @@
]

test_reqs = [
"diameter-synthesis>=0.5.0",
"dictdiffer>=0.5",
"mock>=3",
"morph-tool>=2.9",
Expand Down
22 changes: 22 additions & 0 deletions tests/test_extract_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,28 @@ def test_path_distances(self, filename):
assert_equal(distr["basal_dendrite"]["filtration_metric"], "path_distances")
validator.validate_neuron_distribs(distr)

def test_threshold_too_small(self):
with pytest.raises(
NeuroTSError,
match=(
"The given threshold excluded all bars of the persistence diagram, please use a "
"lower threshold value."
),
):
extract_input.distributions(
os.path.join(_PATH, "diam_simple.swc"),
feature="path_distances",
neurite_types=["basal_dendrite"],
)

def test_missing_neurite_type(self):
with pytest.raises(NeuroTSError):
extract_input.distributions(
os.path.join(_PATH, "diam_simple.swc"),
feature="path_distances",
neurite_types=["basal_dendrite", "apical_dendrite"],
)

def test_trunk_length(self, filename):
distr = extract_input.distributions(filename, feature="trunk_length")
assert "persistence_diagram" not in distr["basal_dendrite"]
Expand Down