def xcorr(self): """The cross-correlation between every pairwise combination time-series in the object. Uses np.correlation('full'). Returns ------- TimeSeries: the time-dependent cross-correlation, with zero-lag at time=0""" tseries_length = self.input.data.shape[0] t_points = self.input.data.shape[-1] xcorr = np.zeros((tseries_length, tseries_length, t_points*2-1)) for i in xrange(tseries_length): for j in xrange(i,tseries_length): xcorr[i][j] = tsu.xcorr(self.input.data[i],self.input.data[j]) idx = tsu.tril_indices(tseries_length,-1) xcorr[idx[0],idx[1],...] = xcorr[idx[1],idx[0],...] return ts.TimeSeries(xcorr, sampling_interval=self.input.sampling_interval, t0=-self.input.sampling_interval*t_points)
def xcorr_norm(self): """The cross-correlation between every pairwise combination time-series in the object, where the zero lag correlation is normalized to be equal to the correlation coefficient between the time-series Returns ------- TimeSeries: the time-dependent cross-correlation, with zero-lag at time=0""" tseries_length = self.input.data.shape[0] t_points = self.input.data.shape[-1] xcorr = np.zeros((tseries_length, tseries_length, t_points*2-1)) for i in xrange(tseries_length): for j in xrange(i,tseries_length): xcorr[i,j] = tsu.xcorr(self.input.data[i],self.input.data[j]) xcorr[i,j] /= (xcorr[i,j,t_points]) xcorr[i,j] *= self.output[i,j] idx = tsu.tril_indices(tseries_length,-1) xcorr[idx[0],idx[1],...] = xcorr[idx[1],idx[0],...] return ts.TimeSeries(xcorr, sampling_interval=self.input.sampling_interval, t0=-self.input.sampling_interval*t_points)
def coherence_partial(self): """The partial coherence between data[i] and data[j], given data[k], as a function of frequency band""" tseries_length = self.input.data.shape[0] spectrum_length = self.spectrum.shape[-1] p_coherence=np.zeros((tseries_length, tseries_length, tseries_length, spectrum_length)) for i in xrange(tseries_length): for j in xrange(tseries_length): for k in xrange(tseries_length): if j==k or i==k: pass else: p_coherence[i][j][k]=tsa.coherence_partial_calculate( self.spectrum[i][j], self.spectrum[i][i], self.spectrum[j][j], self.spectrum[i][k], self.spectrum[j][k], self.spectrum[k][k]) idx = tsu.tril_indices(tseries_length,-1) p_coherence[idx[0],idx[1],...] = p_coherence[idx[1],idx[0],...].conj() return p_coherence
def coherence(self): """ The coherence between the different channels in the input TimeSeries object """ #XXX Calculate this from the standard output, instead of recalculating #the coherence: tseries_length = self.input.data.shape[0] spectrum_length = self.spectrum.shape[-1] coherence=np.zeros((tseries_length, tseries_length, spectrum_length)) for i in xrange(tseries_length): for j in xrange(i,tseries_length): coherence[i][j] = tsa.coherence_calculate(self.spectrum[i][j], self.spectrum[i][i], self.spectrum[j][j]) idx = tsu.tril_indices(tseries_length,-1) coherence[idx[0],idx[1],...] = coherence[idx[1],idx[0],...].conj() return coherence
def coherence(self): tseries_length = self.input.data.shape[0] spectrum_length = self.spectrum.shape[-1] coherence=np.zeros((tseries_length, tseries_length, spectrum_length)) for i in xrange(tseries_length): for j in xrange(i,tseries_length): coherence[i][j] = tsa.coherence_calculate(self.spectrum[i][j], self.spectrum[i][i], self.spectrum[j][j]) idx = tsu.tril_indices(tseries_length,-1) coherence[idx[0],idx[1],...] = coherence[idx[1],idx[0],...].conj() return coherence
def output(self): """The standard output for this kind of analyzer is the coherency """ data = self.input.data tseries_length = data.shape[0] spectrum_length = self.spectrum.shape[-1] coherency=np.zeros((tseries_length, tseries_length, spectrum_length),dtype=complex) for i in xrange(tseries_length): for j in xrange(i,tseries_length): coherency[i][j] = tsa.coherency_calculate(self.spectrum[i][j], self.spectrum[i][i], self.spectrum[j][j]) idx = tsu.tril_indices(tseries_length,-1) coherency[idx[0],idx[1],...] = coherency[idx[1],idx[0],...].conj() return coherency
tspectra[i], tspectra[i], (w[i], w[i]), sides='onesided' ).real syy = alg.mtm_cross_spectrum( tspectra[j], tspectra[j], (w[i], w[j]), sides='onesided' ).real psd_mat[0,i,j] = sxx psd_mat[1,i,j] = syy coh_mat[i,j] = np.abs(sxy)**2 coh_mat[i,j] /= (sxx * syy) csd_mat[i,j] = sxy if i != j: coh_var[i,j] = utils.jackknifed_coh_variance( tspectra[i], tspectra[j], weights=(w[i], w[j]), last_freq=L ) upper_idc = utils.triu_indices(nseq, k=1) lower_idc = utils.tril_indices(nseq, k=-1) coh_mat[upper_idc] = coh_mat[lower_idc] coh_var[upper_idc] = coh_var[lower_idc] # convert this measure with the normalizing function coh_mat_xform = utils.normalize_coherence(coh_mat, 2*K-2) t025_limit = coh_mat_xform + dist.t.ppf(.025, K-1)*np.sqrt(coh_var) t975_limit = coh_mat_xform + dist.t.ppf(.975, K-1)*np.sqrt(coh_var) utils.normal_coherence_to_unit(t025_limit, 2*K-2, t025_limit) utils.normal_coherence_to_unit(t975_limit, 2*K-2, t975_limit) if L < n_samples: freqs = np.linspace(0, 1/(2*TR), L)