-
Notifications
You must be signed in to change notification settings - Fork 34
/
segmentation.py
842 lines (754 loc) · 34.1 KB
/
segmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import os.path as op
import logging
import numpy as np
from scipy.spatial.distance import cdist
from scipy.stats import zscore
import nibabel as nib
from templateflow import api as tflow
from tqdm.auto import tqdm
import dipy.data as dpd
import dipy.tracking.streamline as dts
import dipy.tracking.streamlinespeed as dps
from dipy.segment.bundles import RecoBundles
from dipy.align.streamlinear import whole_brain_slr
from dipy.stats.analysis import gaussian_weights
import dipy.core.gradients as dpg
from dipy.io.stateful_tractogram import StatefulTractogram, Space
import AFQ.registration as reg
import AFQ.utils.models as ut
import AFQ.utils.volume as auv
import AFQ.data as afd
__all__ = ["Segmentation"]
def _resample_tg(tg, n_points):
# reformat for dipy's set_number_of_points
if isinstance(tg, np.ndarray):
if len(tg.shape) > 2:
streamlines = tg.tolist()
streamlines = [np.asarray(item) for item in streamlines]
else:
streamlines = tg.streamlines
return dps.set_number_of_points(streamlines, n_points)
class Segmentation:
def __init__(self,
nb_points=False,
seg_algo='AFQ',
progressive=True,
greater_than=50,
rm_small_clusters=50,
model_clust_thr=40,
reduction_thr=40,
refine=False,
pruning_thr=6,
b0_threshold=0,
prob_threshold=0,
rng=None,
return_idx=False,
filter_by_endpoints=True,
dist_to_aal=4,
save_intermediates=None):
"""
Segment streamlines into bundles.
Parameters
----------
nb_points : int, boolean
Resample streamlines to nb_points number of points.
If False, no resampling is done. Default: False
seg_algo : string
Algorithm for segmentation (case-insensitive):
'AFQ': Segment streamlines into bundles,
based on inclusion/exclusion ROIs.
'Reco': Segment streamlines using the RecoBundles algorithm
[Garyfallidis2017].
Default: 'AFQ'
rm_small_clusters : int
Using RecoBundles Algorithm.
Remove clusters that have less than this value
during whole brain SLR.
Default: 50
progressive : boolean, optional
Using RecoBundles Algorithm.
Whether or not to use progressive technique
during whole brain SLR.
Default: True.
greater_than : int, optional
Using RecoBundles Algorithm.
Keep streamlines that have length greater than this value
during whole brain SLR.
Default: 50.
b0_theshold : float.
Using AFQ Algorithm.
All b-values with values less than or equal to `bo_threshold` are
considered as b0s i.e. without diffusion weighting.
Default: 0.
prob_threshold : float.
Using AFQ Algorithm.
Initial cleaning of fiber groups is done using probability maps
from [Hua2008]_. Here, we choose an average probability that
needs to be exceeded for an individual streamline to be retained.
Default: 0.
rng : RandomState
If None, creates RandomState. Used in RecoBundles Algorithm.
Default: None.
return_idx : bool
Whether to return the indices in the original streamlines as part
of the output of segmentation.
filter_by_endpoints: bool
Whether to filter the bundles based on their endpoints relative
to regions defined in the AAL atlas. Applies only to the waypoint
approach (XXX for now). Default: True.
dist_to_aal : float
If filter_by_endpoints is True, this is the distance from the
endpoints to the AAL atlas ROIs that is required.
save_intermediates : str, optional
The full path to a folder into which intermediate products
are saved. Default: None, means no saving of intermediates.
References
----------
.. [Hua2008] Hua K, Zhang J, Wakana S, Jiang H, Li X, et al. (2008)
Tract probability maps in stereotaxic spaces: analyses of white
matter anatomy and tract-specific quantification. Neuroimage 39:
336-347
"""
self.logger = logging.getLogger('AFQ.Segmentation')
self.nb_points = nb_points
if rng is None:
self.rng = np.random.RandomState()
else:
self.rng = rng
self.seg_algo = seg_algo.lower()
self.prob_threshold = prob_threshold
self.b0_threshold = b0_threshold
self.progressive = progressive
self.greater_than = greater_than
self.rm_small_clusters = rm_small_clusters
self.model_clust_thr = model_clust_thr
self.reduction_thr = reduction_thr
self.refine = refine
self.pruning_thr = pruning_thr
self.return_idx = return_idx
self.filter_by_endpoints = filter_by_endpoints
self.dist_to_aal = dist_to_aal
self.save_intermediates = save_intermediates
def segment(self, bundle_dict, tg, fdata=None, fbval=None,
fbvec=None, mapping=None, reg_prealign=None,
reg_template=None, b0_threshold=0, img_affine=None):
"""
Segment streamlines into bundles based on either waypoint ROIs
[Yeatman2012]_ or RecoBundles [Garyfallidis2017]_.
Parameters
----------
bundle_dict: dict
Meta-data for the segmentation. The format is something like::
{'name': {'ROIs':[img1, img2],
'rules':[True, True]},
'prob_map': img3,
'cross_midline': False}
tg : StatefulTractogram
Bundles to segment
fdata, fbval, fbvec : str
Full path to data, bvals, bvecs
mapping : DiffeomorphicMap object, str or nib.Nifti1Image, optional.
A mapping between DWI space and a template. If None, mapping will
be registered from data used in prepare_img. Default: None.
reg_prealign : array, optional.
The linear transformation to be applied to align input images to
the reference space before warping under the deformation field.
Default: None.
reg_template : str or nib.Nifti1Image, optional.
Template to use for registration. Default: MNI T2.
img_affine : array, optional.
The spatial transformation from the measurement to the scanner
space.
References
----------
.. [Yeatman2012] Yeatman, Jason D., Robert F. Dougherty, Nathaniel J.
Myall, Brian A. Wandell, and Heidi M. Feldman. 2012. "Tract Profiles of
White Matter Properties: Automating Fiber-Tract Quantification"
PloS One 7 (11): e49790.
.. [Garyfallidis17] Garyfallidis et al. Recognition of white matter
bundles using local and global streamline-based registration and
clustering, Neuroimage, 2017.
"""
if img_affine is not None:
if (mapping is None
or fdata is not None
or fbval is not None
or fbvec is not None):
self.logger.error(
"Provide either the full path to data, bvals, bvecs,"
+ "or provide the affine of the image and the mapping")
self.logger.info("Preparing Segmentation Parameters")
self.img_affine = img_affine
self.prepare_img(fdata, fbval, fbvec)
self.logger.info("Preprocessing Streamlines")
self.tg = tg
# If resampling over-write the sft:
if self.nb_points:
self.tg = StatefulTractogram(
dps.set_number_of_points(self.tg.streamlines, self.nb_points),
self.tg, self.tg.space)
self.prepare_map(mapping, reg_prealign, reg_template)
self.bundle_dict = bundle_dict
self.cross_streamlines()
if self.seg_algo == "afq":
return self.segment_afq()
elif self.seg_algo == "reco":
return self.segment_reco()
def prepare_img(self, fdata, fbval, fbvec):
"""
Prepare image data from DWI data.
Parameters
----------
fdata, fbval, fbvec : str
Full path to data, bvals, bvecs
"""
if self.img_affine is None:
self.img, _, _, _ = \
ut.prepare_data(fdata, fbval, fbvec,
b0_threshold=self.b0_threshold)
self.img_affine = self.img.affine
self.fdata = fdata
self.fbval = fbval
self.fbvec = fbvec
def prepare_map(self, mapping=None, reg_prealign=None, reg_template=None):
"""
Set mapping between DWI space and a template.
Parameters
----------
mapping : DiffeomorphicMap object, str or nib.Nifti1Image, optional.
A mapping between DWI space and a template.
If None, mapping will be registered from data used in prepare_img.
Default: None.
reg_template : str or nib.Nifti1Image, optional.
Template to use for registration (defaults to the MNI T2)
Default: None.
reg_prealign : array, optional.
The linear transformation to be applied to align input images to
the reference space before warping under the deformation field.
Default: None.
"""
if reg_template is None:
reg_template = afd.read_mni_template()
self.reg_template = reg_template
if mapping is None:
gtab = dpg.gradient_table(self.fbval, self.fbvec)
self.mapping = reg.syn_register_dwi(self.fdata, gtab,
template=reg_template)[1]
elif isinstance(mapping, str) or isinstance(mapping, nib.Nifti1Image):
if reg_prealign is None:
reg_prealign = np.eye(4)
if self.img is None:
self.img, _, _, _ = \
ut.prepare_data(self.fdata,
self.fbval,
self.fbvec,
b0_threshold=self.b0_threshold)
self.mapping = reg.read_mapping(
mapping,
self.img,
reg_template,
prealign=np.linalg.inv(reg_prealign))
else:
self.mapping = mapping
def cross_streamlines(self, tg=None, template=None, low_coord=10):
"""
Classify the streamlines by whether they cross the midline.
Creates a crosses attribute which is an array of booleans. Each boolean
corresponds to a streamline, and is whether or not that streamline
crosses the midline.
Parameters
----------
tg : StatefulTractogram class instance.
template : nibabel.Nifti1Image class instance
An affine transformation into a template space.
"""
if tg is None:
tg = self.tg
if template is None:
template_affine = self.img_affine
else:
template_affine = template.affine
# What is the x,y,z coordinate of 0,0,0 in the template space?
zero_coord = np.dot(np.linalg.inv(template_affine),
np.array([0, 0, 0, 1]))
self.crosses = np.zeros(len(tg), dtype=bool)
# already_split = 0
for sl_idx, sl in enumerate(tg.streamlines):
if np.any(sl[:, 0] > zero_coord[0]) and \
np.any(sl[:, 0] < zero_coord[0]):
self.crosses[sl_idx] = True
else:
self.crosses[sl_idx] = False
def _get_bundle_info(self, bundle_idx, bundle):
"""
Get fiber probabilites and ROIs for a given bundle.
"""
rules = self.bundle_dict[bundle]['rules']
include_rois = []
exclude_rois = []
for rule_idx, rule in enumerate(rules):
roi = self.bundle_dict[bundle]['ROIs'][rule_idx]
if not isinstance(roi, np.ndarray):
roi = roi.get_fdata()
warped_roi = auv.patch_up_roi(self.mapping.transform_inverse(
roi.astype(np.float32), interpolation='linear'))
if rule:
# include ROI:
include_rois.append(np.array(np.where(warped_roi)).T)
else:
# Exclude ROI:
exclude_rois.append(np.array(np.where(warped_roi)).T)
# For debugging purposes, we can save the variable as it is:
if self.save_intermediates is not None:
nib.save(
nib.Nifti1Image(warped_roi.astype(np.float32),
self.img_affine),
op.join(self.save_intermediates,
'warpedROI_',
bundle,
'as_used.nii.gz'))
# The probability map if doesn't exist is all ones with the same
# shape as the ROIs:
prob_map = self.bundle_dict[bundle].get(
'prob_map', np.ones(roi.shape))
if not isinstance(prob_map, np.ndarray):
prob_map = prob_map.get_fdata()
warped_prob_map = \
self.mapping.transform_inverse(prob_map,
interpolation='nearest')
return warped_prob_map, include_rois, exclude_rois
def _check_sl_with_inclusion(self, sl, include_rois, tol):
"""
Helper function to check that a streamline is close to a list of
inclusion ROIS.
"""
dist = []
for roi in include_rois:
# Use squared Euclidean distance, because it's faster:
dist.append(cdist(sl, roi, 'sqeuclidean'))
if np.min(dist[-1]) > tol:
# Too far from one of them:
return False, []
# Apparently you checked all the ROIs and it was close to all of them
return True, dist
def _check_sl_with_exclusion(self, sl, exclude_rois, tol):
""" Helper function to check that a streamline is not too close to a
list of exclusion ROIs.
"""
for roi in exclude_rois:
# Use squared Euclidean distance, because it's faster:
if np.min(cdist(sl, roi, 'sqeuclidean')) < tol:
return False
# Either there are no exclusion ROIs, or you are not close to any:
return True
def _return_empty(self, bundle):
"""
Helper function for segment_afq, to return an empty dict under
some conditions.
"""
if self.return_idx:
self.fiber_groups[bundle] = {}
self.fiber_groups[bundle]['sl'] = StatefulTractogram(
[], self.img, Space.VOX)
self.fiber_groups[bundle]['idx'] = np.array([])
else:
self.fiber_groups[bundle] = StatefulTractogram(
[], self.img, Space.VOX)
def segment_afq(self, tg=None):
"""
Assign streamlines to bundles using the waypoint ROI approach
Parameters
----------
tg : StatefulTractogram class instance
"""
if tg is None:
tg = self.tg
else:
self.tg = tg
self.tg.to_vox()
# For expedience, we approximate each streamline as a 100 point curve.
# This is only used in extracting the values from the probability map,
# so will not affect measurement of distance from the waypoint ROIs
fgarray = np.array(_resample_tg(tg, 100))
n_streamlines = fgarray.shape[0]
streamlines_in_bundles = np.zeros(
(n_streamlines, len(self.bundle_dict)))
min_dist_coords = np.zeros(
(n_streamlines, len(self.bundle_dict), 2), dtype=int)
self.fiber_groups = {}
if self.return_idx:
out_idx = np.arange(n_streamlines, dtype=int)
if self.filter_by_endpoints:
aal_atlas = afd.read_aal_atlas(self.reg_template)
aal_atlas = aal_atlas['atlas'].get_fdata()
# We need to calculate the size of a voxel, so we can transform
# from mm to voxel units:
R = self.img_affine[0:3, 0:3]
vox_dim = np.mean(np.diag(np.linalg.cholesky(R.T.dot(R))))
dist_to_aal = self.dist_to_aal / vox_dim
self.logger.info("Assigning Streamlines to Bundles")
# Tolerance is set to the square of the distance to the corner
# because we are using the squared Euclidean distance in calls to
# `cdist` to make those calls faster.
tol = dts.dist_to_corner(self.img_affine)**2
for bundle_idx, bundle in enumerate(self.bundle_dict):
self.logger.info(f"Finding Streamlines for {bundle}")
warped_prob_map, include_roi, exclude_roi = \
self._get_bundle_info(bundle_idx, bundle)
if self.save_intermediates is not None:
nib.save(
nib.Nifti1Image(warped_prob_map.astype(np.float32),
self.img_affine),
op.join(self.save_intermediates,
'warpedprobmap',
bundle,
'as_used.nii.gz'))
fiber_probabilities = dts.values_from_volume(
warped_prob_map,
fgarray, np.eye(4))
fiber_probabilities = np.mean(fiber_probabilities, -1)
idx_above_prob = np.where(
fiber_probabilities > self.prob_threshold)
self.logger.info((f"{len(idx_above_prob[0])} streamlines exceed"
" the probability threshold."))
crosses_midline = self.bundle_dict[bundle]['cross_midline']
for sl_idx in tqdm(idx_above_prob[0]):
sl = tg.streamlines[sl_idx]
if fiber_probabilities[sl_idx] > self.prob_threshold:
if crosses_midline is not None:
if self.crosses[sl_idx]:
# This means that the streamline does
# cross the midline:
if crosses_midline:
# This is what we want, keep going
pass
else:
# This is not what we want,
# skip to next streamline
continue
is_close, dist = \
self._check_sl_with_inclusion(sl,
include_roi,
tol)
if is_close:
is_far = \
self._check_sl_with_exclusion(sl,
exclude_roi,
tol)
if is_far:
min_dist_coords[sl_idx, bundle_idx, 0] =\
np.argmin(dist[0], 0)[0]
min_dist_coords[sl_idx, bundle_idx, 1] =\
np.argmin(dist[1], 0)[0]
streamlines_in_bundles[sl_idx, bundle_idx] =\
fiber_probabilities[sl_idx]
self.logger.info(
(f"{np.sum(streamlines_in_bundles[:, bundle_idx] > 0)} "
"streamlines selected with waypoint ROIs"))
# Eliminate any fibers not selected using the waypoint ROIs:
possible_fibers = np.sum(streamlines_in_bundles, -1) > 0
tg = StatefulTractogram(tg.streamlines[possible_fibers],
self.img,
Space.VOX)
if self.return_idx:
out_idx = out_idx[possible_fibers]
streamlines_in_bundles = streamlines_in_bundles[possible_fibers]
min_dist_coords = min_dist_coords[possible_fibers]
bundle_choice = np.argmax(streamlines_in_bundles, -1)
# We do another round through, so that we can orient all the
# streamlines within a bundle in the same orientation with respect to
# the ROIs. This order is ARBITRARY but CONSISTENT (going from ROI0
# to ROI1).
self.logger.info("Re-orienting streamlines to consistent directions")
for bundle_idx, bundle in enumerate(self.bundle_dict):
self.logger.info(f"Processing {bundle}")
select_idx = np.where(bundle_choice == bundle_idx)
if len(select_idx[0]) == 0:
# There's nothing here, set and move to the next bundle:
self._return_empty(bundle)
continue
# Use a list here, because ArraySequence doesn't support item
# assignment:
select_sl = list(tg.streamlines[select_idx])
# Sub-sample min_dist_coords:
min_dist_coords_bundle = min_dist_coords[select_idx]
for idx in range(len(select_sl)):
min0 = min_dist_coords_bundle[idx, bundle_idx, 0]
min1 = min_dist_coords_bundle[idx, bundle_idx, 1]
if min0 > min1:
select_sl[idx] = select_sl[idx][::-1]
# Set this to StatefulTractogram object for filtering/output:
select_sl = StatefulTractogram(select_sl, self.img, Space.VOX)
if self.filter_by_endpoints:
self.logger.info("Filtering by endpoints")
# Create binary masks and warp these into subject's DWI space:
aal_targets = afd.bundles_to_aal([bundle], atlas=aal_atlas)[0]
aal_idx = []
for targ in aal_targets:
if targ is not None:
aal_roi = np.zeros(aal_atlas.shape[:3])
aal_roi[targ[:, 0], targ[:, 1], targ[:, 2]] = 1
warped_roi = self.mapping.transform_inverse(
aal_roi,
interpolation='nearest')
aal_idx.append(np.array(np.where(warped_roi > 0)).T)
else:
aal_idx.append(None)
self.logger.info("Before filtering "
f"{len(select_sl)} streamlines")
new_select_sl = clean_by_endpoints(select_sl.streamlines,
aal_idx[0],
aal_idx[1],
tol=dist_to_aal,
return_idx=self.return_idx)
# Generate immediately:
new_select_sl = list(new_select_sl)
# We need to check this again:
if len(new_select_sl) == 0:
# There's nothing here, set and move to the next bundle:
self._return_empty(bundle)
continue
if self.return_idx:
temp_select_sl = []
temp_select_idx = np.empty(len(new_select_sl), int)
for ii, ss in enumerate(new_select_sl):
temp_select_sl.append(ss[0])
temp_select_idx[ii] = ss[1]
select_idx = select_idx[0][temp_select_idx]
new_select_sl = temp_select_sl
select_sl = StatefulTractogram(new_select_sl,
self.img,
Space.RASMM)
self.logger.info("After filtering "
f"{len(select_sl)} streamlines")
if self.return_idx:
self.fiber_groups[bundle] = {}
self.fiber_groups[bundle]['sl'] = select_sl
self.fiber_groups[bundle]['idx'] = out_idx[select_idx]
else:
self.fiber_groups[bundle] = select_sl
return self.fiber_groups
def segment_reco(self, tg=None):
"""
Segment streamlines using the RecoBundles algorithm [Garyfallidis2017]
Parameters
----------
tg : StatefulTractogram class instance
A whole-brain tractogram to be segmented.
Returns
-------
fiber_groups : dict
Keys are names of the bundles, values are Streamline objects.
The streamlines in each object have all been oriented to have the
same orientation (using `dts.orient_by_streamline`).
"""
if tg is None:
tg = self.tg
else:
self.tg = tg
fiber_groups = {}
self.logger.info("Registering Whole-brain with SLR")
# We start with whole-brain SLR:
atlas = self.bundle_dict['whole_brain']
moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(
atlas, self.tg.streamlines, x0='affine', verbose=False,
progressive=self.progressive,
greater_than=self.greater_than,
rm_small_clusters=self.rm_small_clusters,
rng=self.rng)
# We generate our instance of RB with the moved streamlines:
self.logger.info("Extracting Bundles")
rb = RecoBundles(moved, verbose=False, rng=self.rng)
# Next we'll iterate over bundles, registering each one:
bundle_list = list(self.bundle_dict.keys())
bundle_list.remove('whole_brain')
self.logger.info("Assigning Streamlines to Bundles")
for bundle in bundle_list:
model_sl = self.bundle_dict[bundle]['sl']
_, rec_labels = rb.recognize(model_bundle=model_sl,
model_clust_thr=self.model_clust_thr,
reduction_thr=self.reduction_thr,
reduction_distance='mdf',
slr=True,
slr_metric='asymmetric',
pruning_distance='mdf')
# Use the streamlines in the original space:
recognized_sl = tg.streamlines[rec_labels]
if self.refine:
_, rec_labels = rb.refine(model_sl, recognized_sl,
self.model_clust_thr,
reduction_thr=self.reduction_thr,
pruning_thr=self.pruning_thr)
recognized_sl = tg.streamlines[rec_labels]
standard_sl = self.bundle_dict[bundle]['centroid']
oriented_sl = dts.orient_by_streamline(recognized_sl, standard_sl)
if self.return_idx:
fiber_groups[bundle] = {}
fiber_groups[bundle]['idx'] = rec_labels
fiber_groups[bundle]['sl'] = StatefulTractogram(oriented_sl,
self.img,
Space.RASMM)
else:
fiber_groups[bundle] = StatefulTractogram(oriented_sl,
self.img,
Space.RASMM)
self.fiber_groups = fiber_groups
return fiber_groups
def clean_bundle(tg, n_points=100, clean_rounds=5, distance_threshold=5,
length_threshold=4, min_sl=20, stat='mean',
return_idx=False):
"""
Clean a segmented fiber group based on the Mahalnobis distance of
each streamline
Parameters
----------
tg : StatefulTractogram class instance
A whole-brain tractogram to be segmented.
clean_rounds : int, optional.
Number of rounds of cleaning based on the Mahalanobis distance from
the mean of extracted bundles. Default: 5
distance_threshold : float, optional.
Threshold of cleaning based on the Mahalanobis distance (the units are
standard deviations). Default: 5.
length_threshold: float, optional
Threshold for cleaning based on length (in standard deviations). Length
of any streamline should not be *more* than this number of stdevs from
the mean length.
min_sl : int, optional.
Number of streamlines in a bundle under which we will
not bother with cleaning outliers. Default: 20.
stat : callable or str, optional.
The statistic of each node relative to which the Mahalanobis is
calculated. Default: `np.mean` (but can also use median, etc.)
return_idx : bool
Whether to return indices in the original streamlines.
Default: False.
Returns
-------
A nibabel.Streamlines class instance containing only the streamlines
that have a Mahalanobis distance smaller than `clean_threshold` from
the mean of each one of the nodes.
"""
# Convert string to callable, if that's what you got.
if isinstance(stat, str):
stat = getattr(np, stat)
# We don't even bother if there aren't enough streamlines:
if len(tg.streamlines) < min_sl:
if return_idx:
return tg, np.arange(len(tg.streamlines))
else:
return tg
# Resample once up-front:
fgarray = _resample_tg(tg, n_points)
# Keep this around, so you can use it for indexing at the very end:
idx = np.arange(len(fgarray))
# This calculates the Mahalanobis for each streamline/node:
w = gaussian_weights(fgarray, return_mahalnobis=True, stat=stat)
lengths = np.array([sl.shape[0] for sl in tg.streamlines])
# We'll only do this for clean_rounds
rounds_elapsed = 0
while ((np.any(w > distance_threshold)
or np.any(zscore(lengths) > length_threshold))
and rounds_elapsed < clean_rounds
and len(tg.streamlines) > min_sl):
# Select the fibers that have Mahalanobis smaller than the
# threshold for all their nodes:
idx_dist = np.where(np.all(w < distance_threshold, axis=-1))[0]
idx_len = np.where(zscore(lengths) < length_threshold)[0]
idx_belong = np.intersect1d(idx_dist, idx_len)
if len(idx_belong) < min_sl:
# need to sort and return exactly min_sl:
idx_belong = np.argsort(np.sum(w, axis=-1))[:min_sl]
idx = idx[idx_belong.astype(int)]
# Update by selection:
fgarray = fgarray[idx_belong.astype(int)]
lengths = lengths[idx_belong.astype(int)]
# Repeat:
w = gaussian_weights(fgarray, return_mahalnobis=True)
rounds_elapsed += 1
# Select based on the variable that was keeping track of things for us:
out = StatefulTractogram(tg.streamlines[idx], tg, Space.VOX)
if return_idx:
return out, idx
else:
return out
def clean_by_endpoints(streamlines, targets0, targets1, tol=None, atlas=None,
return_idx=False):
"""
Clean a collection of streamlines based on their two endpoints
Filters down to only include items that have their starting points close to
the targets0 and ending points close to targets1
Parameters
----------
streamlines : sequence of 3XN_i arrays The collection of streamlines to
filter down to.
targets0, target1: sequences or Nx3 arrays or None.
The targets. Numerical values in the atlas array for targets for the
first and last node in each streamline respectively, or NX3 arrays with
each row containing the indices for these locations in the atlas.
If provided a None, this means no restriction on that end.
tol : float, optional A distance tolerance (in units that the coordinates
of the streamlines are represented in). Default: 0, which means that
the endpoint is exactly in the coordinate of the target ROI.
atlas : 3D array or Nifti1Image class instance with a 3D array, optional.
Contains numerical values for ROIs. Default: if not provided, assume
that targets0 and targets1 are both arrays of indices, and this
information is not needed.
Yields
-------
Generator of the filtered collection
"""
if tol is None:
tol = 0
# We square the tolerance, because below we are using the squared Euclidean
# distance which is slightly faster:
tol = tol ** 2
# Check whether it's already in the right format:
sp_is_idx = (targets0 is None
or(isinstance(targets0, np.ndarray)
and targets0.shape[1] == 3))
ep_is_idx = (targets1 is None
or (isinstance(targets1, np.ndarray)
and targets1.shape[1] == 3))
if atlas is None and not (ep_is_idx and sp_is_idx):
raise ValueError()
if sp_is_idx:
idxes0 = targets0
else:
# Otherwise, we'll need to derive it:
startpoint_roi = np.zeros(atlas.shape, dtype=bool)
for targ in targets0:
startpoint_roi[atlas == targ] = 1
idxes0 = np.array(np.where(startpoint_roi)).T
if ep_is_idx:
idxes1 = targets1
else:
endpoint_roi = np.zeros(atlas.shape, dtype=bool)
for targ in targets1:
endpoint_roi[atlas == targ] = 1
idxes1 = np.array(np.where(endpoint_roi)).T
for ii, sl in enumerate(streamlines):
if targets0 is None:
# Nothing to check
dist0ok = True
else:
dist0ok = False
dist0 = np.min(cdist(np.array([sl[0]]), idxes0, 'sqeuclidean'))
if dist0 <= tol:
dist0ok = True
# Only proceed if conditions for one side are fulfilled:
if dist0ok:
if targets1 is None:
# Nothing to check on this end:
if return_idx:
yield sl, ii
else:
yield sl
else:
dist2 = np.min(cdist(np.array([sl[-1]]), idxes1,
'sqeuclidean'))
if dist2 <= tol:
if return_idx:
yield sl, ii
else:
yield sl