示例#1
0
    def test_surrogate(self):
        np.random.seed(31415)
        x, var0 = self.generate_data()

        result = cs.surrogate_connectivity('PDC', x, VAR(2), 4, repeats=100)
        self.assertEqual(result.shape, (100, 2, 2, 4))

        structure = np.mean(np.mean(result, axis=3), axis=0)
        self.assertTrue(np.all(np.abs(structure - np.eye(2)) < 0.05))
示例#2
0
    def test_surrogate(self):
        np.random.seed(31415)
        x, var0 = self.generate_data()

        result = cs.surrogate_connectivity('PDC', x, VAR(2), nfft=4,
                                           repeats=100)
        self.assertEqual(result.shape, (100, 2, 2, 4))

        structure = np.mean(np.mean(result, axis=3), axis=0)
        self.assertTrue(np.all(np.abs(structure-np.eye(2)) < 0.05))
thr_cons, whit_min, whit_max = 0.8, 1., 3.
is_white, consistency, is_stable = do_mvar_evaluation(label_ts, morder,
                                                      whit_max, whit_min,
                                                      thr_cons)
print('model_order, whiteness, consistency, stability: %d, %s, %f, %s\n' %
      (morder, str(is_white), consistency, str(is_stable)))

# compute the Granger Partial Directed Coherence values
print('computing GPDC connectivity...')

mvar = scot.var.VAR(morder)
# result : array, shape (`repeats`, n_channels, n_channels, nfft)
surr = scs.surrogate_connectivity(gcmethod,
                                  label_ts,
                                  mvar,
                                  nfft=nfft,
                                  n_jobs=njobs,
                                  repeats=n_surr)

mvar.fit(label_ts)
# mvar coefficients (n_channels, n_channels * model_order)
# mvar covariance matrix (n_channels, n_channels)
# result : array, shape (n_channels, n_channels, `nfft`)
cau = connectivity(gcmethod, mvar.coef, mvar.rescov, nfft=nfft)

# get the band averaged, thresholded connectivity matrix
caus, max_cons, max_surrs = prepare_causality_matrix(
    cau,
    surr,
    freqs,
    nfft=nfft,
示例#4
0
def causal_analysis(fn_norm, method='GPDC', morder=None, repeats=1000,
                    msave=True, per=99.99,
                    sfreq=678,
                    freqs=[(4, 8), (8, 12), (12, 18), (18, 30), (30, 40)]):
    '''
    Calculate causality matrices of real data and surrogates. And calculate
    the significant causal matrix for each frequency band.
    Parameters
    ----------
    fnnorm: string
        The file name of model order estimation.
    morder: int
        The optimized model order.
    method: string
        causality measures.
    repeats: int
        Shuffling times for surrogates.
    msave: bool
        Save the causal matrix of the whole frequency domain or not.
    per: float or int
        Percentile of the surrogates.
    sfreq: float
        The sampling rate.
    freqs: list
        The list of interest frequency bands.
    '''
    import scot.connectivity_statistics as scs
    from scot.connectivity import connectivity
    import scot
    path_list = get_files_from_list(fn_norm)
    # loop across all filenames
    for fnnorm in path_list:
        cau_path = os.path.split(fnnorm)[0]
        name = os.path.basename(fnnorm)
        condition = name.split('_')[0]
        sig_path = cau_path + '/sig_cau_%d/' % morder
        set_directory(sig_path)
        fncau = fnnorm[:fnnorm.rfind('.npy')] + ',morder%d,cau.npy' % morder
        fnsurr = fnnorm[:fnnorm.rfind('.npy')] + ',morder%d,surrcau.npy' % morder
        X = np.load(fnnorm)
        X = X.transpose(2, 0, 1)
        mvar = scot.var.VAR(morder)
        surr = scs.surrogate_connectivity(method, X, mvar,
                                          repeats=repeats)
        mvar.fit(X)
        cau = connectivity(method, mvar.coef, mvar.rescov)
        if msave:
            np.save(fncau, cau)
            np.save(fnsurr, surr)
        nfft = cau.shape[-1]
        delta_F = sfreq / float(2 * nfft)
        sig_freqs = []
        nfreq = len(freqs)
        #surr_bands = []
        #cau_bands = []
        for ifreq in range(nfreq):
            print 'Frequency index used..', ifreq
            fmin, fmax = int(freqs[ifreq][0] / delta_F), int(freqs[ifreq][1] /
                                                             delta_F)
            con_band = np.mean(cau[:, :, fmin:fmax + 1], axis=-1)
            np.fill_diagonal(con_band, 0)
            surr_band = np.mean(surr[:, :, :, fmin:fmax + 1], axis=-1)
            r, s, _ = surr_band.shape
            for i in xrange(r):
                ts = surr_band[i]
                np.fill_diagonal(ts, 0)
            #surr_bands.append(surr_band)
            #cau_bands.append(con_band)
            con_b = con_band.flatten()
            con_b = con_b[con_b > 0]
            surr_b = surr_band.reshape(r, s * s)
            surr_b = surr_b[surr_b > 0]
            thr = np.percentile(surr_band, per)
            print 'max surrogates %.4f' % thr
            con_band[con_band < thr] = 0
            con_band[con_band >= thr] = 1
            histout = sig_path + '%s,%d-%d,distribution.png'\
                % (condition, freqs[ifreq][0], freqs[ifreq][1])
            throut = sig_path + '%s,%d-%d,threshold.png'\
                % (condition, freqs[ifreq][0], freqs[ifreq][1])
            _plot_hist(con_b, surr_b, histout)
            # _plot_thr(con_b, thr, surr_band.max(), alpha, throut)
            _plot_thr(con_b, thr, surr_band.max(), per, throut)
            # con_band[con_band < z_thre] = 0
            # con_band[con_band >= z_thre] = 1
            sig_freqs.append(con_band)

        sig_freqs = np.array(sig_freqs)
        print 'Saving computed arrays..'
        np.save(sig_path + '%s_sig_con_band.npy' % condition, sig_freqs)
        #cau_bands = np.array(cau_bands)
        #np.save(fncau, cau_bands)
        #surr_bands = np.array(surr_bands)
        #np.save(fnsurr, surr_bands)

    return