Skip to content

Commit

Permalink
Merge pull request #1076 from levje/levje/filter_by_length_fix
Browse files Browse the repository at this point in the history
[FIX]: Return rejected streamlines with empty sft
  • Loading branch information
arnaudbore authored Dec 16, 2024
2 parents a87ad30 + 1c31cc8 commit 87b5a30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 2 additions & 5 deletions scilpy/tractograms/streamline_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,16 @@ def filter_streamlines_by_length(sft, min_length=0., max_length=np.inf,
valid_length_ids = np.logical_and(lengths >= min_length,
lengths <= max_length)
filtered_sft = sft[valid_length_ids]

if return_rejected:
rejected_sft = sft[~valid_length_ids]
else:
valid_length_ids = []
valid_length_ids = np.array([], dtype=bool)
filtered_sft = sft

# Return to original space
sft.to_space(orig_space)
filtered_sft.to_space(orig_space)

if return_rejected:
rejected_sft.to_space(orig_space)
rejected_sft = sft[~valid_length_ids]
return filtered_sft, valid_length_ids, rejected_sft
else:
return filtered_sft, valid_length_ids
Expand Down
12 changes: 12 additions & 0 deletions scilpy/tractograms/tests/test_streamline_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
from dipy.io.streamline import load_tractogram
from dipy.tracking.streamlinespeed import length
from dipy.io.stateful_tractogram import StatefulTractogram

from scilpy import SCILPY_HOME
from scilpy.io.fetcher import fetch_data, get_testing_files_dict
Expand Down Expand Up @@ -174,6 +175,17 @@ def test_filter_streamlines_by_length():
# Test that streamlines shorter than 100 and longer than 120 were removed.
assert np.all(lengths >= min_length) and np.all(lengths <= max_length)

# === 4. Return rejected streamlines with empty sft ===
empty_sft = short_sft[[]] # Empty sft from short_sft (chosen arbitrarily)
filtered_sft, _, rejected = \
filter_streamlines_by_length(empty_sft, min_length=min_length,
max_length=max_length,
return_rejected=True)
assert isinstance(filtered_sft, StatefulTractogram)
assert isinstance(rejected, StatefulTractogram)
assert len(filtered_sft) == 0
assert len(rejected) == 0


def test_filter_streamlines_by_total_length_per_dim():
long_sft = load_tractogram(in_long_sft, in_ref)
Expand Down

0 comments on commit 87b5a30

Please sign in to comment.