Streamlines manipulation
Here, we discuss scripts allowing modifying the streamlines themselves, for instance by resampling the number of points on each streamline (scil_tractogram_resample_nb_points, scil_tractogram_compress), or smoothing the streamlines’ trajectories (scil_tractogram_smooth).
See page Tractogram manipulations: logical operations, resampling, filtering for scripts modifying the tractograms as a whole.
Preparing data for this tutorial
Example of data for this tutorial can be downloaded using instructions here: Getting ready for tutorials. Let’s use a tractogram available in the tutorial data. In the last section, you will also need a mask, and two regions of interest (ROIs), that we will create from labels: wmparc.
# Let's use a tractogram available in the data:
# tractogram1=$in_dir/sub-01/sub-01__cst_L_part2.trk not compatible...
tractogram1=$in_dir/sub-01/sub-01__cst_L.trk
labels=$in_dir/sub-01/sub-01__wmparc.nii.gz
mask=$in_dir/sub-01/sub-01__small_mask_wm.nii.gz
# Current wmparc is in float. Should be in int. Let's convert.
scil_volume_math convert $in_dir/sub-01/sub-01__wmparc.nii.gz \
--data_type int16 -f $in_dir/sub-01/sub-01__wmparc.nii.gz
To look at your data in a viewer, you may use the subject’s T1 volume:
t1=$in_folder/sub-01/sub-01__t1.nii.gz
Tip
You may download the complete bash script to run the whole tutorial in one step ⭳ here.
Resampling the number of points in each streamline
A streamline is a list of points (coordinates), connected by segments. The more you have points, the more precise and smooth your streamline will be, but if you have too much, your data will become heavy on disk. 20 is usually a good number of points. Atlernatively, you could set the length of each segment, using for example a step size of 0.5mm, with --step_size 0.5.
scil_tractogram_resample_nb_points $tractogram1 resampled_20pts.trk \
--nb_pts_per_streamline 20
This term “resampling streamlines” should not be counfounded with “resampling tractograms”, which means changing the number of streamlines in the tractogram.
Compressing streamlines
An alternative is to use DIPY’s compression. This uses the minimal number of points required to have good quality. In regions where the streamline is straight, only a few points are required. For streamlines with high curvature, more points are required.
scil_tractogram_compress $tractogram1 compressed.trk -v
Cutting streamlines
It is possible to simply cut streamlines that are too long (ex, cut the parts that are going out of a given mask) using the following command lines. This will only remove the points (segments) that are extra, it will not remove the entire streamline. This would be called filtering of streamlines, and you can learn more about in page_tractogram_filtering.
1. Cutting the parts that are out of the mask
The option --min_length will ensure that final streamlines after cutting are not too short (we chose 20mm).
scil_tractogram_cut_streamlines $tractogram1 cut_streamlines.trk \
--mask $mask --min_length 20
2. Cutting the parts that are not inside two ROIs
This will find the segments of streamlines that go from one region of interest (ROI) to the other, and will cut the points that are going past these ROIs.
scil_tractogram_cut_streamlines $tractogram1 cut_streamlines2.trk \
--labels $labels --label_ids 16 2024