From 48678a7f57da4fca0df3f8d13952be5ae2ff9ba0 Mon Sep 17 00:00:00 2001 From: Jeremi Levesque Date: Thu, 12 Dec 2024 09:11:40 -0500 Subject: [PATCH 1/3] fix: return rejected with empty sft --- scilpy/tractograms/streamline_operations.py | 8 ++------ .../tractograms/tests/test_streamline_operations.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/scilpy/tractograms/streamline_operations.py b/scilpy/tractograms/streamline_operations.py index 5249b80a8..2e392ee02 100644 --- a/scilpy/tractograms/streamline_operations.py +++ b/scilpy/tractograms/streamline_operations.py @@ -408,19 +408,15 @@ 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 diff --git a/scilpy/tractograms/tests/test_streamline_operations.py b/scilpy/tractograms/tests/test_streamline_operations.py index ccddcc972..da22eb514 100644 --- a/scilpy/tractograms/tests/test_streamline_operations.py +++ b/scilpy/tractograms/tests/test_streamline_operations.py @@ -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 @@ -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) From 296106897e3f2dd75d034fd11277f8f3cb71bf2e Mon Sep 17 00:00:00 2001 From: Jeremi Levesque Date: Thu, 12 Dec 2024 09:18:10 -0500 Subject: [PATCH 2/3] fix: pep8 --- scilpy/tractograms/tests/test_streamline_operations.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scilpy/tractograms/tests/test_streamline_operations.py b/scilpy/tractograms/tests/test_streamline_operations.py index da22eb514..d381da976 100644 --- a/scilpy/tractograms/tests/test_streamline_operations.py +++ b/scilpy/tractograms/tests/test_streamline_operations.py @@ -176,11 +176,11 @@ def test_filter_streamlines_by_length(): 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) + 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 From 1c31cc8f679f7b3ef8f1276a25fa39d20a4cc3b7 Mon Sep 17 00:00:00 2001 From: Jeremi Levesque Date: Thu, 12 Dec 2024 10:36:18 -0500 Subject: [PATCH 3/3] fix: restore missing line --- scilpy/tractograms/streamline_operations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scilpy/tractograms/streamline_operations.py b/scilpy/tractograms/streamline_operations.py index 2e392ee02..6e244af92 100644 --- a/scilpy/tractograms/streamline_operations.py +++ b/scilpy/tractograms/streamline_operations.py @@ -413,6 +413,7 @@ def filter_streamlines_by_length(sft, min_length=0., max_length=np.inf, filtered_sft = sft # Return to original space + sft.to_space(orig_space) filtered_sft.to_space(orig_space) if return_rejected: