def test_csd_save(): """Test saving and loading a CrossSpectralDensity.""" csd = _make_csd() tempdir = _TempDir() fname = op.join(tempdir, 'csd.h5') csd.save(fname) csd2 = read_csd(fname) assert_array_equal(csd._data, csd2._data) assert csd.tmin == csd2.tmin assert csd.tmax == csd2.tmax assert csd.ch_names == csd2.ch_names assert csd.frequencies == csd2.frequencies assert csd._is_sum == csd2._is_sum
"{dir}nc_{meg}_from-fs_ico4_exp-r-fwd.fif".format(dir=meg_dir, meg=meg)) # convert the forward model to one that defines two orthogonal dipoles at each source, that are tangential to a sphere fwd_tan = conpy.forward_to_tangential(fwd_r) # get pairs for connectivity calculation pairs = np.load("{}NEMO_ico4_connectivity_pairs.npy".format(meg_dir)) # pairs are defined in fsaverage space, map them to the source space of the current subject fsaverage_to_subj = conpy.utils.get_morph_src_mapping( fs_src, fwd_tan['src'], indices=True, subjects_dir=mri_dir)[0] pairs = [[fsaverage_to_subj[v] for v in pairs[0]], [fsaverage_to_subj[v] for v in pairs[1]]] for cond in exp_conds: print("Calculations for Condition: ", cond) for freq, vals in freqs.items(): print("Calculations for Frequency: ", freq) csd = read_csd("{dir}nc_{meg}-csd_{cond}_{freq}.h5".format( dir=meg_dir, meg=meg, cond=cond, freq=freq)) csd = csd.mean() csd = pick_channels_csd(csd, fwd_tan['info']['ch_names']) con = conpy.dics_connectivity(vertex_pairs=pairs, fwd=fwd_tan, data_csd=csd, reg=0.05, n_jobs=8) con.save("{dir}nc_{meg}_{cond}_{freq}-connectivity.h5".format( dir=meg_dir, meg=meg, cond=cond, freq=freq)) # for the baseline conditions: print("Running Baseline Conditions") fwd_r = mne.read_forward_solution( "{dir}nc_{meg}_from-fs_ico4_bas-r-fwd.fif".format(dir=meg_dir, meg=meg))
from matplotlib import pyplot as plt import mne from mne.time_frequency import read_csd, pick_channels_csd from config import fname, subjects, freq_bands info = mne.io.read_info(fname.epo(subject=subjects[0])) grads = [info['ch_names'][ch] for ch in mne.pick_types(info, meg='grad')] csd = read_csd(fname.csd(subject=subjects[0], condition='face')) csd = pick_channels_csd(csd, grads) csd = csd.mean([f[0] for f in freq_bands], [f[1] for f in freq_bands]) # Plot theta, alpha, low beta csd[:3].plot(info, n_cols=3, show=False) plt.savefig('../paper/figures/csd1.pdf', bbox_inches='tight') # Plot high beta 1, high beta 2 and low gamma csd[3:].plot(info, n_cols=3, show=False) plt.savefig('../paper/figures/csd2.pdf', bbox_inches='tight')
# Read the forward model fwd = mne.read_forward_solution(fname.fwd(subject=subject)) # Read the info structure info = mne.io.read_info(fname.epo(subject=subject)) # Compute source power for all frequency bands and all conditions fmin = [f[0] for f in freq_bands] fmax = [f[1] for f in freq_bands] csds = dict() for condition in conditions + ['baseline']: print('Reading CSD matrix for condition:', condition) # Read the CSD matrix csds[condition] = read_csd(fname.csd(condition=condition, subject=subject)) # Average the CSDs of both conditions csd = csds['face'].copy() csd._data += csds['scrambled']._data csd._data /= 2 # Compute the DICS beamformer using the CSDs from both conditions, for all # frequency bands. filters = make_dics(info=info, forward=fwd, csd=csd.mean(fmin, fmax), reg=reg, pick_ori='max-power') stcs = dict() for condition in conditions + ['baseline']: print('Computing power for condition:', condition) stcs[condition], _ = apply_dics_csd(csds[condition].mean(fmin, fmax),