def CfcAmpSwap(xfP, xfA, CfcModel, n_perm=200, matricial=True): """Swap phase/amplitude trials, (Bahramisharif, 2013) [xfP] = (nPha, npts, ntrials) [xfA] = (nAmp, npts, ntrials) """ # Get sizes : nPha, timeL, nbTrials = xfP.shape nAmp = xfA.shape[0] Suro = np.zeros((nbTrials, nAmp, nPha, n_perm)) if matricial: # Swap trials phase/amplitude : ampSh, _ = perm_swap(xfA, xfA, axis=2, n_perm=n_perm) # Get pac : iteract = product(range(nbTrials), range(n_perm)) for tr, pe in iteract: Suro[tr, :, :, pe] = CfcModel(xfP[:, :, tr], np.matrix(ampSh[pe, :, :, tr])) else: for iamp in range(nAmp): ampSh, _ = perm_swap(xfA[iamp, ...], xfA[iamp, ...], axis=1, n_perm=n_perm) # Get pac : iteract = product(range(nbTrials), range(n_perm)) for tr, pe in iteract: Suro[tr, iamp, :, pe] = CfcModel(xfP[:, :, tr], np.matrix(ampSh[pe, :, tr])) return Suro
def CfcTrialSwap(xfP, xfA, CfcModel, n_perm=200, matricial=True): """Swap phase/amplitude trials (Tort, 2010) [xfP] = (nPha, npts, ntrials) [xfA] = (nAmp, npts, ntrials) """ # Get sizes : nPha, timeL, nbTrials = xfP.shape nAmp = xfA.shape[0] Suro = np.zeros((nbTrials, nAmp, nPha, n_perm)) if matricial: # Swap trials phase/amplitude : phamp = np.concatenate((xfP, xfA)) phampSh1, phampSh2 = perm_swap(phamp, phamp, axis=2, n_perm=n_perm) phaSh, ampSh = phampSh1[:, 0:nPha, ...], phampSh2[:, nPha::, ...] del phamp, phampSh1, phampSh2 # Get pac : iteract = product(range(nbTrials), range(n_perm)) for tr, pe in iteract: Suro[tr, :, :, pe] = CfcModel(np.matrix(phaSh[pe, :, :, tr]), ampSh[pe, :, :, tr]) else: # Swap trials phase/amplitude : phampiter = product(range(nPha), range(nAmp)) for ipha, iamp in phampiter: # Concatenate selected pha/amp : phamp = np.concatenate((xfP[[ipha], ...], xfA[[iamp], ...])) # Swap: phampSh1, phampSh2 = perm_swap(phamp, phamp, axis=2, n_perm=n_perm) phaSh, ampSh = phampSh1[:, 0, ...], phampSh2[:, 1, ...] # Get pac : iteract = product(range(nbTrials), range(n_perm)) for tr, pe in iteract: Suro[tr, iamp, ipha, pe] = CfcModel(np.matrix(phaSh[pe, :, tr]), ampSh[pe, :, tr]) return Suro
def _evalstat(self, x, bsl): """Statistical evaluation of features [x] = [xn] = (nFce, npts, nTrials) """ # Unpack variables: statmeth = self._statmeth n_perm = self._n_perm tail = self._2t maxst = self._mxst # Mean Frequencies : x, _ = binArray(x, self._fSplitIndex, axis=0) # Get the baseline and set to same shape of x: xFm = np.mean(x[:, bsl[0]:bsl[1], :], 1) # Mean time : if self._window is not None: x, _ = binArray(x, self._window, axis=1) # Repeat baseline: baseline = np.tile(xFm[:, np.newaxis, :], [1, x.shape[1], 1]) # Get shape of x: nf, npts, nt = x.shape pvalues = np.ones((nf, npts)) # Switch between methods: # -> Permutations # Loops on time and matrix for frequency (avoid RAM usage but increase speed) if statmeth == 'permutation': # Get metric: fcn = perm_metric(self._metric) # Apply metric to x and baseline: xN = fcn(x, baseline).mean(axis=2) # For each time points: for pts in range(npts): # Randomly swap x // baseline : perm = perm_swap(x[:, pts, :], baseline[:, pts, :], n_perm=n_perm, axis=1, rndstate=0 + pts)[0] # Normalize permutations by baline: perm = fcn(perm, baseline[:, pts, :]).mean(2) # Maximum stat (correct through frequencies): if maxst: perm = maxstat(perm, axis=1) # Get pvalues : pvalues[:, pts] = perm_2pvalue(xN[:, pts], perm, n_perm, tail=tail) # -> Wilcoxon // Kruskal-Wallis: else: # Get the method: if statmeth == 'wilcoxon': def fcn(a, b): return wilcoxon(a, b)[1] elif statmeth == 'kruskal': def fcn(a, b): return kruskal(a, b)[1] # Apply: ite = product(range(nf), range(npts)) for k, i in ite: pvalues[k, i] = fcn(x[k, i, :], xFm[k, :]) return pvalues
def _evalstat(self, x, bsl): """Statistical evaluation of features [x] = [xn] = (nFce, npts, nTrials) """ # Unpack variables: statmeth = self._statmeth n_perm = self._n_perm tail = self._2t maxst = self._mxst # Mean Frequencies : x, _ = binArray(x, self._fSplitIndex, axis=0) # Get the baseline and set to same shape of x: xFm = np.mean(x[:, bsl[0]:bsl[1], :], 1) # Mean time : if self._window is not None: x, _ = binArray(x, self._window, axis=1) # Repeat baseline: baseline = np.tile(xFm[:, np.newaxis, :], [1, x.shape[1], 1]) # Get shape of x: nf, npts, nt = x.shape pvalues = np.ones((nf, npts)) # Switch between methods: # -> Permutations # Loops on time and matrix for frequency (avoid RAM usage but increase speed) if statmeth == 'permutation': # Get metric: fcn = perm_metric(self._metric) # Apply metric to x and baseline: xN = fcn(x, baseline).mean(axis=2) # For each time points: for pts in range(npts): # Randomly swap x // baseline : perm = perm_swap(x[:, pts, :], baseline[:, pts, :], n_perm=n_perm, axis=1, rndstate=0+pts)[0] # Normalize permutations by baline: perm = fcn(perm, baseline[:, pts, :]).mean(2) # Maximum stat (correct through frequencies): if maxst: perm = maxstat(perm, axis=1) # Get pvalues : pvalues[:, pts] = perm_2pvalue(xN[:, pts], perm, n_perm, tail=tail) # -> Wilcoxon // Kruskal-Wallis: else: # Get the method: if statmeth == 'wilcoxon': def fcn(a, b): return wilcoxon(a, b)[1] elif statmeth == 'kruskal': def fcn(a, b): return kruskal(a, b)[1] # Apply: ite = product(range(nf), range(npts)) for k, i in ite: pvalues[k, i] = fcn(x[k, i, :], xFm[k, :]) return pvalues