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]: Return rejected streamlines with empty sft #1076

Merged
merged 3 commits into from
Dec 16, 2024
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
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]
levje marked this conversation as resolved.
Show resolved Hide resolved
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