def get(self, xpha, xamp, n_perm=200, n_jobs=-1): """Get the erpac mesure between an xpha and xamp signals. Args: xpha: array Signal for phase. The shape of xpha should be : (n_electrodes x n_pts x n_trials) xamp: array Signal for amplitude. The shape of xamp should be : (n_electrodes x n_pts x n_trials) Kargs: n_perm: integer, optional, [def: 200] Number of permutations for normalizing the cfc mesure. n_jobs: integer, optional, [def: -1] Control the number of jobs for parallel computing. Use 1, 2, .. depending of your number or cores. -1 for all the cores. If the same signal is used (example : xpha=x and xamp=x), this mean the program compute a local erpac. Returns: xerpac: array The erpac mesure of size : (n_amplitude x n_phase x n_electrodes x n_windows) pvalue: array The associated p-values of size : (n_amplitude x n_phase x n_electrodes x n_windows) """ # Check and get methods: xpha, xamp = _cfcCheck(xpha, xamp, self._npts) npha, namp = self._nPha, self._nAmp phaMeth = self._pha.get(self._sf, self._pha.f, self._npts) ampMeth = self._amp.get(self._sf, self._amp.f, self._npts) # Extract phase and amplitude: nelec, npts, ntrials = xpha.shape xp, xa = cfcparafilt(xpha, xamp, n_jobs, self) # Window: if not (self._window == [(0, npts)]): xp = binArray(xp, self._window, axis=2)[0] xa = binArray(xa, self._window, axis=2)[0] npts = xp.shape[2] # Extract ERPAC and surrogates: iteract = product(range(nelec), range(npha), range(namp)) xerpac = np.zeros((nelec, npha, namp, npts)) pval = np.empty_like(xerpac) for e, p, a in iteract: xerpac[e, p, a, :], pval[e, p, a, :] = _erpac(xp[e, p, ...], xa[e, a, ...], n_perm, n_jobs) return xerpac, pval
def __init__(self, sf, npts, kind, f, baseline, norm, method, window, width, step, split, time, meanT, **kwargs): # Check the type of f: if (len(f) == 4) and isinstance(f[0], (int, float)): f = binarize(f[0], f[1], f[2], f[3], kind='list') # Manage time and frequencies: self._window, self.xvec = _manageWindow(npts, window=window, width=width, step=step, time=time) self.f, self._fSplit, self._fSplitIndex = _manageFrequencies( f, split=split) # Get variables : self._baseline = baseline self._norm = norm self._width = width self._step = step self._split = split self._nf = len(self.f) self._sf = sf self._npts = npts self.yvec = [round((k[0] + k[1]) / 2) for k in self.f] self._kind = kind self._fobj = fextract(method, kind, **kwargs) self._meanT = meanT if (self._window is not None) and (time is not None): self.xvec = binArray(time, self._window)[0] self.xvec = list(self.xvec)
def __init__(self, pha_f, pha_kind, pha_meth, pha_cycle, amp_f, amp_kind, amp_meth, amp_cycle, sf, npts, window, width, step, time, **kwargs): # Define windows and frequency : self._pha = fextract(kind=pha_kind, method=pha_meth, cycle=pha_cycle, **kwargs) self._amp = fextract(kind=amp_kind, method=amp_meth, cycle=amp_cycle, **kwargs) self._window, xvec = _manageWindow(npts, window=window, width=width, step=step, time=time) self._pha.f, _, _ = _manageFrequencies(pha_f, split=None) self._amp.f, _, _ = _manageFrequencies(amp_f, split=None) if time is None: time = np.arange(npts) if self._window is None: self._window = [(0, npts)] self.time = np.array(self._window).mean() # self.xvec = [0, npts] else: self.time = binArray(time, self._window)[0] # Get variables : self._width = width self._step = step self._nPha = len(self._pha.f) self._nAmp = len(self._amp.f) self._sf = sf self._npts = npts self._nwin = len(self._window) self.pha = [np.mean(k) for k in self._pha.f] self.amp = [np.mean(k) for k in self._amp.f]
def __init__(self, sf, npts, kind, f, baseline, norm, method, window, width, step, split, time, meanT, **kwargs): # Check the type of f: if (len(f) == 4) and isinstance(f[0], (int, float)): f = binarize(f[0], f[1], f[2], f[3], kind='list') # Manage time and frequencies: self._window, self.xvec = _manageWindow( npts, window=window, width=width, step=step, time=time) self.f, self._fSplit, self._fSplitIndex = _manageFrequencies( f, split=split) # Get variables : self._baseline = baseline self._norm = norm self._width = width self._step = step self._split = split self._nf = len(self.f) self._sf = sf self._npts = npts self.yvec = [round((k[0]+k[1])/2) for k in self.f] self._kind = kind self._fobj = fextract(method, kind, **kwargs) self._meanT = meanT if (self._window is not None) and (time is not None): self.xvec = binArray(time, self._window)[0] self.xvec = list(self.xvec)
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(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
def _phase(x, self): """Sub-phase function """ # Get the filter properties and apply: fMeth = self._fobj.get(self._sf, self._fSplit, self._npts) xF = self._fobj.apply(x, fMeth) # Mean time : if self._window is not None: xF, _ = binArray(xF, self._window, axis=1) nf, npts, nt = xF.shape # Get p-value: if self._getstat: pvalues = np.zeros((nf, npts), dtype=float) for f in range(nf): for k in range(npts): pvalues[f, k] = circ_rtest(xF[f, k, :])[0] else: pvalues = None return xF, 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
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