Skip to content

Commit 4e94e68

Browse files
committed
[ENH] added new permsamples input into behavioral_pls
1 parent ee5e3e0 commit 4e94e68

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

pyls/base.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,26 +627,31 @@ def permutation(self, X, Y, seed=None):
627627
# generate permuted indices (unless already provided)
628628
self.permsamp = self.inputs.get('permsamples')
629629
if self.permsamp is None:
630+
use_permind = self.inputs.get('permindices')
630631
self.permsamp = gen_permsamp(self.inputs.groups,
631632
self.inputs.n_cond,
632633
self.inputs.n_perm,
633634
seed=seed,
634635
verbose=self.inputs.verbose)
636+
else:
637+
use_permind = self.inputs.get('permindices')
638+
self.permsamp = self.permsamp if use_permind else \
639+
np.transpose(self.permsamp, (1, 2, 0))
635640

636641
# get permuted values (parallelizing as requested)
637642
gen = utils.trange(self.inputs.n_perm, verbose=self.inputs.verbose,
638643
desc='Running permutations')
639644
with utils.get_par_func(self.inputs.n_proc,
640645
self.__class__._single_perm) as (par, func):
641-
out = par(func(self, X=X, Y=Y, inds=self.permsamp[:, i],
642-
groups=self.dummy, original=self.res['y_weights'],
643-
seed=i)
646+
out = par(func(self, X=X, Y=Y, samples=self.permsamp[..., i],
647+
use_permind=use_permind, groups=self.dummy,
648+
original=self.res['y_weights'], seed=i)
644649
for i in gen)
645650
d_perm, ucorrs, vcorrs = [np.stack(o, axis=-1) for o in zip(*out)]
646651

647652
return d_perm, ucorrs, vcorrs
648653

649-
def _single_perm(self, X, Y, inds, groups=None, original=None, seed=None):
654+
def _single_perm(self, X, Y, samples, use_permind=True, groups=None, original=None, seed=None):
650655
"""
651656
Permutes `X` (w/o replacement) and recomputes SVD
652657
@@ -656,8 +661,10 @@ def _single_perm(self, X, Y, inds, groups=None, original=None, seed=None):
656661
Input data matrix, where `S` is observations and `B` is features
657662
Y : (S, T) array_like
658663
Input data matrix, where `S` is observations and `T` is features
659-
inds : (S,) array_like
660-
Permutation resampling array
664+
samples : (S,) or (S, T) array_like
665+
Permutation resampling array or pre-permuted Y matrix
666+
use_permind : bool
667+
Whether `samples` is a resampling array or pre-permuted array
661668
original : (J, L) array_like
662669
Right singular vector from original decomposition of `X` and `Y`.
663670
Used to perform Procrustes rotation on permuted singular values,
@@ -679,7 +686,10 @@ def _single_perm(self, X, Y, inds, groups=None, original=None, seed=None):
679686
"""
680687

681688
# calculate SVD of permuted matrices
682-
Xp, Yp = self.make_permutation(X, Y, inds)
689+
if use_permind:
690+
Xp, Yp = self.make_permutation(X, Y, samples)
691+
else:
692+
Xp, Yp = X, samples
683693
U, d, V = self.svd(Xp, Yp, groups=groups, seed=seed)
684694

685695
# optionally get rotated/rescaled singular values

pyls/structures.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@
109109
"""),
110110
resamples=dedent("""\
111111
permsamples : array_like, optional
112-
Re-sampling array to be used during permutation test (if n_perm > 0).
112+
Resampled array to be used during permutation testing.
113113
If not specified a set of unique permutations will be generated.
114114
Default: None
115+
permindices : Boolean, optional
116+
Re-sampling array to be used during permutation test (if n_perm > 0).
117+
If not specified a set of unique permutations will be generated.
118+
Whether permsamples is an array to permute indices or a pre-permuted
119+
array. Useful when permuting with methods like BrainSMASH or Eigenstrapping.
120+
Default: True
115121
bootsamples : array_like, optional
116122
Resampling array to be used during bootstrap resampling (if n_boot >
117123
0). If not specified a set of unique bootstraps will be generated.
@@ -142,7 +148,7 @@ class PLSInputs(ResDict):
142148
'X', 'Y', 'groups', 'n_cond', 'n_perm', 'n_boot', 'n_split',
143149
'test_split', 'test_size', 'mean_centering', 'covariance', 'rotate',
144150
'ci', 'seed', 'verbose', 'n_proc', 'bootsamples', 'permsamples',
145-
'method', 'n_components', 'aggfunc'
151+
'method', 'n_components', 'aggfunc', 'permindices'
146152
]
147153

148154
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)