Skip to content

Commit

Permalink
Merge pull request #1090 from levje/levje/rejected_streamlines_arg
Browse files Browse the repository at this point in the history
FIX: Filter streamlines by length, use the "save rejection" argument.
  • Loading branch information
arnaudbore authored Dec 16, 2024
2 parents 7302a36 + 2dce9cb commit ce00b24
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
18 changes: 14 additions & 4 deletions scripts/scil_tractogram_filter_by_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def _build_arg_parser():
help='Do not write file if there is no streamline.')
p.add_argument('--display_counts', action='store_true',
help='Print streamline count before and after filtering')
p.add_argument('--save_rejected', action='store_true',
help='Save rejected streamlines to output tractogram.')
p.add_argument('--out_rejected',
help='If specified, save rejected streamlines to this '
'file.')
add_json_args(p)
add_reference_arg(p)
add_verbose_arg(p)
Expand All @@ -75,8 +76,17 @@ def main():
sft = load_tractogram_with_reference(parser, args, args.in_tractogram)

# Processing
new_sft, _, outliers_sft = filter_streamlines_by_length(
sft, args.minL, args.maxL, return_rejected=True)
return_rejected = args.out_rejected is not None

# Returns (new_sft, _, [rejected_sft]) in filtered_result
filtered_result = filter_streamlines_by_length(
sft, args.minL, args.maxL, return_rejected=return_rejected)

new_sft = filtered_result[0]

if return_rejected:
rejected_sft = filtered_result[2]
save_tractogram(rejected_sft, args.out_rejected, args.no_empty)

if args.display_counts:
sc_bf = len(sft.streamlines)
Expand Down
47 changes: 47 additions & 0 deletions scripts/tests/test_tractogram_filter_by_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from scilpy import SCILPY_HOME
from scilpy.io.fetcher import fetch_data, get_testing_files_dict
from scilpy.io.streamlines import load_tractogram

# If they already exist, this only takes 5 seconds (check md5sum)
fetch_data(get_testing_files_dict(), keys=['filtering.zip'])
Expand All @@ -20,9 +21,55 @@ def test_help_option(script_runner):

def test_execution_filtering(script_runner, monkeypatch):
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
# Effectively, this doesn't filter anything, since bundle_4.trk has
# all streamlines with lengths of 130mm. This is just to test the
# script execution.
in_bundle = os.path.join(SCILPY_HOME, 'filtering',
'bundle_4.trk')
ret = script_runner.run('scil_tractogram_filter_by_length.py',
in_bundle, 'bundle_4_filtered.trk',
'--minL', '125', '--maxL', '130')

sft = load_tractogram('bundle_4_filtered.trk', 'same')
assert len(sft) == 52

assert ret.success


def test_rejected_filtering(script_runner, monkeypatch):
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
in_bundle = os.path.join(SCILPY_HOME, 'filtering',
'bundle_all_1mm.trk')
ret = script_runner.run('scil_tractogram_filter_by_length.py',
in_bundle, 'bundle_all_1mm_filtered.trk',
'--minL', '125', '--maxL', '130',
'--out_rejected', 'bundle_all_1mm_rejected.trk')
assert ret.success
assert os.path.exists('bundle_all_1mm_rejected.trk')
assert os.path.exists('bundle_all_1mm_rejected.trk')

sft = load_tractogram('bundle_all_1mm_filtered.trk', 'same')
rejected_sft = load_tractogram('bundle_all_1mm_rejected.trk', 'same')

assert len(sft) == 266
assert len(rejected_sft) == 2824


def test_rejected_filtering_no_rejection(script_runner, monkeypatch):
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
in_bundle = os.path.join(SCILPY_HOME, 'filtering',
'bundle_4.trk')
ret = script_runner.run('scil_tractogram_filter_by_length.py',
in_bundle, 'bundle_4_filtered_no_rejection.trk',
'--minL', '125', '--maxL', '130',
'--out_rejected', 'bundle_4_rejected.trk')
assert ret.success

# File should be created even though there are no rejected streamlines
assert os.path.exists('bundle_4_rejected.trk')

sft = load_tractogram('bundle_4_filtered_no_rejection.trk', 'same')
rejected_sft = load_tractogram('bundle_4_rejected.trk', 'same')

assert len(sft) == 52
assert len(rejected_sft) == 0

0 comments on commit ce00b24

Please sign in to comment.