def _evalstat(x, bsl, meth, n_perm, metric, maxstat, tail): """Statistical evaluation of features [x] = [xn] = (nFce, npts, nTrials) [bsl] = (nFce, nTrials) """ # Get shape of xF : nf, npts, nt = x.shape pvalues = np.ones((nf, npts)) # Permutations : if meth == 'permutation': perm = perm_swaparray(a, b, n_perm=200, axis=-1, rndstate=0) from brainpipe.xPOO.stats import permutation # Pre-define permutations : pObj = permutation(n_perm) perm = np.zeros((n_perm, nf, npts)) # For each permutation : for p in range(n_perm): # Get 1D iterations : ite = product(range(nf), range(npts)) permT = np.random.permutation(2*nt) for f, pts in ite: bs, xs = bsl[f, :], x[f, pts, :] # Reshape data : subX = np.vstack((bsl[f, :], x[f, pts, :])).reshape(2*nt,) # Shuffle data : subX = subX[permT].reshape(nt, 2) # Normalize data : subX = normalize(subX[:, 0], subX[:, 1], norm=norm) # Get mean of data : perm[p, f, pts] = np.mean(subX) # Get final pvalues : pvalues = pObj.perm2p(np.mean(xn, 2), perm, tail=tail, maxstat=maxstat) # Wilcoxon test : elif meth == 'wilcoxon': from scipy.stats import wilcoxon # Get iterations : ite = product(range(nf), range(npts)) # Compute wilcoxon : for k, i in ite: _, pvalues[k, i] = wilcoxon(x[k, i, :], bsl[k, :]) # Kruskal-Wallis : elif meth == 'kruskal': from scipy.stats import kruskal # Get iterations : ite = product(range(nf), range(npts)) # Compute Kruskal-Wallis : for k, i in ite: _, pvalues[k, i] = kruskal(x[k, i, :], bsl[k, :]) return pvalues
def _evalstat(x, bsl, meth, n_perm, metric, maxstat, tail): """Statistical evaluation of features [x] = [xn] = (nFce, npts, nTrials) [bsl] = (nFce, nTrials) """ # Get shape of xF : nf, npts, nt = x.shape pvalues = np.ones((nf, npts)) # Permutations : if meth == 'permutation': perm = perm_swaparray(a, b, n_perm=200, axis=-1, rndstate=0) from brainpipe.xPOO.stats import permutation # Pre-define permutations : pObj = permutation(n_perm) perm = np.zeros((n_perm, nf, npts)) # For each permutation : for p in range(n_perm): # Get 1D iterations : ite = product(range(nf), range(npts)) permT = np.random.permutation(2 * nt) for f, pts in ite: bs, xs = bsl[f, :], x[f, pts, :] # Reshape data : subX = np.vstack((bsl[f, :], x[f, pts, :])).reshape(2 * nt, ) # Shuffle data : subX = subX[permT].reshape(nt, 2) # Normalize data : subX = normalize(subX[:, 0], subX[:, 1], norm=norm) # Get mean of data : perm[p, f, pts] = np.mean(subX) # Get final pvalues : pvalues = pObj.perm2p(np.mean(xn, 2), perm, tail=tail, maxstat=maxstat) # Wilcoxon test : elif meth == 'wilcoxon': from scipy.stats import wilcoxon # Get iterations : ite = product(range(nf), range(npts)) # Compute wilcoxon : for k, i in ite: _, pvalues[k, i] = wilcoxon(x[k, i, :], bsl[k, :]) # Kruskal-Wallis : elif meth == 'kruskal': from scipy.stats import kruskal # Get iterations : ite = product(range(nf), range(npts)) # Compute Kruskal-Wallis : for k, i in ite: _, pvalues[k, i] = kruskal(x[k, i, :], bsl[k, :]) return pvalues
def _get(x, self): """Sub get function. Get the spectral info of x. """ # Unpack args : bsl, norm, n_perm = self._baseline, self._norm, self._n_perm maxstat, tail, metric = self._mxst, self._2t, self._metric statmeth = self._statmeth # Get the filter properties and apply: fMeth = self._fobj.get(self._sf, self._fSplit, self._npts) xF = self._fobj.apply(x, fMeth) nf, npts, nt = xF.shape # Mean through trials: if self._meanT: xF = np.mean(xF, 2) xF = xF[..., np.newaxis] # Normalize power : if norm is not 0: xFm = np.mean(xF[:, bsl[0]:bsl[1], :], 1) baseline = np.tile(xFm[:, np.newaxis, :], [1, xF.shape[1], 1]) xFn = normalize(xF, baseline, norm=norm) del xFm, baseline else: baseline = np.zeros(xF.shape) xFn = xF # Mean Frequencies : xFn, _ = binArray(xFn, self._fSplitIndex, axis=0) # Mean time : if self._window is not None: xFn, xvec = binArray(xFn, self._window, axis=1) # Statistical evaluation : if (norm is not 0) and (self._statmeth is not 'none'): pvalues = _evalstat(xF, xFm, statmeth, n_perm, metric, maxstat, tail) else: pvalues = np.matrix([0]) return xFn # , pvalues
def get(self, x, cue): """Get power phase locked Args: x: array Data of shape (npt, ntrials) cue: integer Cue to align time-frequency maps. Returns: xpow, xpha, xsig: repectively realigned power, phase and filtered signal """ # Find cue according to define time vector self._cue = cue xvec = self._powObj.xvec cue = np.abs(np.array(xvec)-cue).argmin() self._cueIdx = cue # Extact power, phase and filtered signal: xpow = np.squeeze(self._powObj.get(x)[0]) xpha = np.squeeze(self._phaObj.get(x)[0]) xsig = np.squeeze(self._sigObj.get(x)[0]) # Re-align: xpha_s, xpow_s, xsig_s = np.empty_like( xpha), np.empty_like(xpow), np.empty_like(xsig) nTrials = xsig.shape[1] for k in range(nTrials): # Get shifting: move = self._PeakDetection(xsig[:, k], cue) # Apply shifting: xpha_s[:, k] = self._ShiftSignal(np.matrix(xpha[:, k]), move) xsig_s[:, k] = self._ShiftSignal(np.matrix(xsig[:, k]), move) xpow_s[:, :, k] = self._ShiftSignal(xpow[:, :, k], move) xpow_s = np.mean(xpow_s, 2) # Normalize mean power: if self._normBck is not 0: bsl = self._baseline xFm = np.mean(xpow_s[:, bsl[0]:bsl[1]], 1) baseline = np.tile(xFm[:, np.newaxis], [1, xpow_s.shape[1]]) xpow_s = normalize(xpow_s, baseline, norm=self._normBck) return xpow_s, xpha_s, xsig_s
def _get(x, self): """Sub get function. Get the spectral info of x. """ # Unpack args : bsl, norm = self._baseline, self._norm n_perm, statmeth = self._n_perm, self._statmeth # Get the filter properties and apply: fMeth = self._fobj.get(self._sf, self._fSplit, self._npts) xF = self._fobj.apply(x, fMeth) nf, npts, nt = xF.shape # Statistical evaluation : if (n_perm is not 0) and (bsl is not None) and (statmeth is not None): pvalues = _evalstat(self, xF, bsl) else: pvalues = None # Mean through trials: if self._meanT: xF = np.mean(xF[..., np.newaxis], 2) # Normalize power : if (norm is not None) and (bsl is not None): xFm = np.mean(xF[:, bsl[0]:bsl[1], :], 1) baseline = np.tile(xFm[:, np.newaxis, :], [1, xF.shape[1], 1]) xF = normalize(xF, baseline, norm=norm) # Mean Frequencies : xF, _ = binArray(xF, self._fSplitIndex, axis=0) # Mean time : if self._window is not None: xF, _ = binArray(xF, self._window, axis=1) return xF, pvalues