data += np.dot(raw_segment, raw_segment.T)
        n_samples += raw_segment.shape[1]
    mu /= n_samples
    data -= n_samples * mu[:, None] * mu[None, :]
    data /= (n_samples - 1.0)
    ch_names = [raw.info['ch_names'][k] for k in picks]
    data_cov = mne.Covariance(None)
    data_cov.update(kind=mne.fiff.FIFF.FIFFV_MNE_NOISE_COV, diag=False, 
        dim=len(data), names=ch_names, data=data, 
        projs=cp.deepcopy(raw.info['projs']), bads=raw.info['bads'], 
        nfree=n_samples, eig=None, eigvec=None)

    noise_cov = mne.compute_raw_data_covariance(er_raw)
    # note that MNE reads CTF data as magnetometers!
    noise_cov = mne.cov.regularize(noise_cov, raw.info, mag=noise_reg)
    events = fg.get_good_events(markers[subj], time, window_length)

    epochs = mne.Epochs(raw, events, None, 0, window_length, preload=True, baseline=None, detrend=0, picks=picks)
    stcs = mne.beamformer.lcmv_epochs(epochs, forward, noise_cov.as_diag(), data_cov, reg=data_reg, pick_ori='max-power')

    labels = [label.morph('fsaverage',subj) for label in net_labels]
    for label in labels:
        label_ts = [stc.in_label(label) for stc in stcs]
        con, freqs, times, n_epochs, n_tapers = mne.connectivity.spectral_connectivity(label_ts, method=method, mode='multitaper', sfreq=raw.info['sfreq'], fmin=[1,4,8,13,30], fmax=[4,8,13,30,50], faverage=True, n_jobs=1, mt_adaptive=False)
        il = np.tril_indices(label_ts[0].shape[0], k=-1)
        avg_con = []
        for c in con:
            band_avg = []
            for b in range(5):
                data = c[:,:,b]
                band_avg.append(np.mean(data[il]))
Example #2
0
    data_cov = mne.Covariance(None)
    data_cov.update(kind=mne.fiff.FIFF.FIFFV_MNE_NOISE_COV,
                    diag=False,
                    dim=len(data),
                    names=ch_names,
                    data=data,
                    projs=cp.deepcopy(raw.info['projs']),
                    bads=raw.info['bads'],
                    nfree=n_samples,
                    eig=None,
                    eigvec=None)

    noise_cov = mne.compute_raw_data_covariance(er_raw)
    # note that MNE reads CTF data as magnetometers!
    noise_cov = mne.cov.regularize(noise_cov, raw.info, mag=noise_reg)
    events = fg.get_good_events(markers[subj], time, window_length)

    epochs = mne.Epochs(raw,
                        events,
                        None,
                        0,
                        window_length,
                        preload=True,
                        baseline=None,
                        detrend=0,
                        picks=picks)
    stcs = mne.beamformer.lcmv_epochs(epochs,
                                      forward,
                                      noise_cov.as_diag(),
                                      data_cov,
                                      reg=data_reg,
no_chl_subjs = ['GYURGRWX', 'INSJTGTJ', 'BPPPDMXT', 'ILUVLFGJ',
                'ZKNUZRLN', 'JILJXPMM', 'KXVGYZMK', 'ZILYTPUL',
                'WHPYSIQL', 'CMAOTMDG', 'AQBNNQMR', 'LDPZODZI',
                'FHPHIIQJ', 'EQMSSLGP', 'JZFEBOBR']
# don't use these subjects regardless of what's in the data, based on observations during the scan
bad_subjs = ['XKSKEASF', 'RELHQPFH', 'HUKEPSWW']

markers = fgs.read_marker_files()
num_clean_epochs = {}
chl_mrks = {}
for subj, mrks in markers.iteritems():
    # note that here we look at the version of FIFF without HP, because the filter screws up the position channels!
    fid = env.data + '/MEG_data/fifs/' + subj + '_rest_LP100_CP3_DS300_raw.fif'
    raw = mne.fiff.Raw(fid, preload=True)
    _, times = raw[:, :]
    if subj not in no_chl_subjs:
        print 'Checking CHL...'
        chl_mrks[subj] = fgs.read_chl_events(raw, times)
        mrks = mrks + chl_mrks[subj]
    else:
        chl_mrks[subj] = []

    events = fgs.get_good_events(mrks, times, seg_len)
    if len(events) == 0 or subj in bad_subjs:
        num_clean_epochs[subj] = 0
    else:
        epochs = fgs.crop_clean_epochs(raw, events, seg_len=seg_len)
        num_clean_epochs[subj] = epochs.get_data().shape[0]

np.savez(env.results + 'num_clean_epochs_chl.5_lp58_hp.5_visual', num_clean_epochs=num_clean_epochs, markers=markers, chl_mrks=chl_mrks, seg_len=seg_len)
Example #4
0
subjs = np.load(res_dir + 'group_' + freq_band + '_aligned.npz')['subjs']
sensor_data = {}
weights = {}
for subj in subjs:
    raw = mne.io.Raw(data_dir + subj + '_rest_LP100_CP3_DS300_raw.fif',
                     preload=True,
                     compensation=3)
    picks = mne.pick_channels_regexp(raw.info['ch_names'], 'M..-*')
    band = [int(i) for i in freq_band.split('-')]
    raw.filter(l_freq=band[0], h_freq=band[1], picks=picks)
    weights[subj] = np.load(weights_dir + subj + '_' + freq_band +
                            '_weights.npz')['weights']
    data, time = raw[picks, :]
    raw.apply_hilbert(picks, envelope=False)
    markers = np.load(markers_fname)[()]
    events = fg.get_good_events(markers[subj], time, nwindow)
    epochs = mne.Epochs(raw,
                        events,
                        None,
                        0,
                        nwindow,
                        preload=True,
                        baseline=None,
                        detrend=0,
                        picks=picks)
    epoch_data = epochs.get_data()
    epoch_data = epoch_data.swapaxes(1, 0)
    nchans, nreps, npoints = epoch_data.shape
    sensor_data[subj] = epoch_data.reshape([nchans, nreps * npoints])

aligned_ICs = np.load(res_dir + 'group_' + freq_band +
freq_band = '8-13'
res_dir = home + '/tmp/'  # /data/results/graicar/meg/'

subjs = np.load(res_dir + 'group_' + freq_band + '_aligned.npz')['subjs']
sensor_data = {}
weights = {}
for subj in subjs:
    raw = mne.io.Raw(data_dir + subj + '_rest_LP100_CP3_DS300_raw.fif', preload=True, compensation=3)
    picks = mne.pick_channels_regexp(raw.info['ch_names'], 'M..-*')
    band = [int(i) for i in freq_band.split('-')]
    raw.filter(l_freq=band[0], h_freq=band[1], picks=picks)
    weights[subj] = np.load(weights_dir + subj + '_' + freq_band + '_weights.npz')['weights']
    data, time = raw[picks, :]
    raw.apply_hilbert(picks, envelope=False)
    markers = np.load(markers_fname)[()]
    events = fg.get_good_events(markers[subj], time, nwindow)
    epochs = mne.Epochs(raw, events, None, 0, nwindow, preload=True, baseline=None, detrend=0, picks=picks)
    epoch_data = epochs.get_data()
    epoch_data = epoch_data.swapaxes(1, 0)
    nchans, nreps, npoints = epoch_data.shape
    sensor_data[subj] = epoch_data.reshape([nchans, nreps*npoints])

aligned_ICs = np.load(res_dir + 'group_' + freq_band + '_aligned.npz')['aligned_ICs']
rep_mats = np.load(res_dir + 'group_' + freq_band + '_aligned.npz')['rep_mats']

nvoxels = 10 # weights.values()[0].shape[0]
ncomps = 5 #len(aligned_ICs)
corr_ICs = np.empty([nvoxels, ncomps])
corr_ICs[:] = np.nan
# for each aligned IC
for i in range(ncomps):