Skip to content

Commit b3a645a

Browse files
authored
Merge pull request #82 from rmarkello/ref/freespins
[REF] Modifies freesurfer spins
2 parents c8e09c0 + c55a77a commit b3a645a

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

netneurotools/freesurfer.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ def _geodesic_parcel_centroid(vertices, faces, inds):
230230
# get the edges in our graph
231231
keep = faces[np.sum(np.isin(faces, inds), axis=1) > 1]
232232
edges = np.row_stack([list(itertools.combinations(f, 2)) for f in keep])
233-
edges = np.row_stack([edges, np.fliplr(edges)])
234233
weights = np.linalg.norm(np.diff(vertices[edges], axis=1), axis=-1)
235234

236235
# construct our sparse matrix on which to calculate shortest paths
@@ -397,6 +396,7 @@ def vertices_to_parcels(data, *, lhannot, rhannot, drop=None):
397396

398397
# store parcellated data
399398
reduced[n_parc:n_parc + len(names) - len(inds), idx] = currdata
399+
400400
start = end
401401
n_parc += len(names) - len(inds)
402402

@@ -435,7 +435,7 @@ def _get_fsaverage_coords(version='fsaverage', surface='sphere'):
435435

436436

437437
def _get_fsaverage_spins(version='fsaverage', spins=None, n_rotate=1000,
438-
seed=None, verbose=False, return_cost=False):
438+
**kwargs):
439439
"""
440440
Generates spatial permutation resamples for fsaverage `version`
441441
@@ -453,14 +453,12 @@ def _get_fsaverage_spins(version='fsaverage', spins=None, n_rotate=1000,
453453
None
454454
n_rotate : int, optional
455455
Number of rotations to generate. Default: 1000
456-
seed : {int, np.random.RandomState instance, None}, optional
457-
Seed for random number generation. Default: None
458-
verbose : bool, optional
459-
Whether to print occasional status messages. Default: False
460456
return_cost : bool, optional
461457
Whether to return cost array (specified as Euclidean distance) for each
462458
coordinate for each rotation. Currently this option is not supported if
463459
pre-computed `spins` are provided. Default: True
460+
kwargs : key-value pairs
461+
Keyword arguments passed to `netneurotools.stats.gen_spinsamples`
464462
465463
Returns
466464
--------
@@ -470,10 +468,10 @@ def _get_fsaverage_spins(version='fsaverage', spins=None, n_rotate=1000,
470468

471469
if spins is None:
472470
coords, hemiid = _get_fsaverage_coords(version, 'sphere')
473-
spins, cost = gen_spinsamples(coords, hemiid, n_rotate=n_rotate,
474-
seed=seed, verbose=verbose)
475-
if return_cost:
476-
return spins, cost
471+
spins = gen_spinsamples(coords, hemiid, n_rotate=n_rotate,
472+
**kwargs)
473+
if kwargs.get('return_cost'):
474+
return spins
477475

478476
spins = np.asarray(spins, dtype='int32')
479477
if spins.shape[-1] != n_rotate:
@@ -482,15 +480,12 @@ def _get_fsaverage_spins(version='fsaverage', spins=None, n_rotate=1000,
482480
'Ignoring specified `n_rotate` parameter and using '
483481
'all provided `spins`.')
484482
n_rotate = spins.shape[-1]
485-
if return_cost:
486-
raise ValueError('Cannot `return_cost` when `spins` are provided.')
487483

488484
return spins, None
489485

490486

491487
def spin_data(data, *, lhannot, rhannot, version='fsaverage', n_rotate=1000,
492-
spins=None, drop=None, seed=None, verbose=False,
493-
return_cost=False):
488+
spins=None, drop=None, verbose=False, **kwargs):
494489
"""
495490
Projects parcellated `data` to surface, rotates, and re-parcellates
496491
@@ -524,14 +519,10 @@ def spin_data(data, *, lhannot, rhannot, version='fsaverage', n_rotate=1000,
524519
will be inserted in place of the these regions in the returned data. If
525520
not specified, parcels defined in `netneurotools.freesurfer.FSIGNORE`
526521
are assumed to not be present. Default: None
527-
seed : {int, np.random.RandomState instance, None}, optional
528-
Seed for random number generation. Default: None
529522
verbose : bool, optional
530523
Whether to print occasional status messages. Default: False
531-
return_cost : bool, optional
532-
Whether to return cost array (specified as Euclidean distance) for each
533-
coordinate for each rotation. Currently this option is not supported if
534-
pre-computed `spins` are provided. Default: True
524+
kwargs : key-value pairs
525+
Keyword arguments passed to `netneurotools.stats.gen_spinsamples`
535526
536527
Returns
537528
-------
@@ -553,8 +544,7 @@ def spin_data(data, *, lhannot, rhannot, version='fsaverage', n_rotate=1000,
553544
# get spins + cost (if requested)
554545
spins, cost = _get_fsaverage_spins(version=version, spins=spins,
555546
n_rotate=n_rotate,
556-
seed=seed, verbose=verbose,
557-
return_cost=return_cost)
547+
verbose=verbose, **kwargs)
558548
if len(vertices) != len(spins):
559549
raise ValueError('Provided annotation files have a different '
560550
'number of vertices than the specified fsaverage '
@@ -574,15 +564,14 @@ def spin_data(data, *, lhannot, rhannot, version='fsaverage', n_rotate=1000,
574564
if verbose:
575565
print(' ' * len(msg) + '\b' * len(msg), end='', flush=True)
576566

577-
if return_cost:
567+
if kwargs.get('return_cost'):
578568
return spun, cost
579569

580570
return spun
581571

582572

583573
def spin_parcels(*, lhannot, rhannot, version='fsaverage', n_rotate=1000,
584-
spins=None, drop=None, seed=None, verbose=False,
585-
return_cost=False):
574+
spins=None, drop=None, verbose=False, **kwargs):
586575
"""
587576
Rotates parcels in `{lh,rh}annot` and re-assigns based on maximum overlap
588577
@@ -617,6 +606,8 @@ def spin_parcels(*, lhannot, rhannot, version='fsaverage', n_rotate=1000,
617606
return_cost : bool, optional
618607
Whether to return cost array (specified as Euclidean distance) for each
619608
coordinate for each rotation. Default: True
609+
kwargs : key-value pairs
610+
Keyword arguments passed to `netneurotools.stats.gen_spinsamples`
620611
621612
Returns
622613
-------
@@ -662,9 +653,8 @@ def overlap(vals):
662653

663654
# get spins + cost (if requested)
664655
spins, cost = _get_fsaverage_spins(version=version, spins=spins,
665-
n_rotate=n_rotate,
666-
seed=seed, verbose=verbose,
667-
return_cost=return_cost)
656+
n_rotate=n_rotate, verbose=verbose,
657+
**kwargs)
668658
if len(vertices) != len(spins):
669659
raise ValueError('Provided annotation files have a different '
670660
'number of vertices than the specified fsaverage '
@@ -681,7 +671,7 @@ def overlap(vals):
681671
regions[:, n] = labeled_comprehension(vertices[spins[:, n]], vertices,
682672
labels, overlap, int, -1)[mask]
683673

684-
if return_cost:
674+
if kwargs.get('return_cost'):
685675
return regions, cost
686676

687677
return regions

0 commit comments

Comments
 (0)