def get_explained(subject_no, n_comp, n_bads): subject = subjects[subject_no] raw_fname = root_dir + '%s/passive_raw.fif' % subject room_fname = root_dir + '%s/emptyroom_%s.fif' % (subject, subject) raw = mne.io.read_raw_fif(raw_fname, preload=True) raw_room = mne.io.read_raw_fif(room_fname, preload=True) raw.filter(0.5, 70) raw_room.filter(1, 70) mne.channels.fix_mag_coil_types(raw.info) mne.channels.fix_mag_coil_types(raw_room.info) picks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False, stim=False, exclude='bads') events = mne.find_events(raw) tmin, tmax = -0.1, 0.3 expes = list(event_id.keys()) expe = expes[2] # reject = dict(grad=4000e-13, mag=1e-12, eog=150e-6) reject = None # Fit smica freqs = np.linspace(1, 70, 41) smica = ICA(n_components=n_comp, freqs=freqs, rng=0).fit(raw, picks, tol=1e-9, verbose=100, n_it_min=3000, em_it=3000, n_it_lbfgs=3000) ica = ICA_mne(n_comp).fit(raw, picks) X_room = raw_room.get_data(picks=picks) X_room /= np.linalg.norm(X_room) Ss = smica.compute_sources(X_room) As = smica.A del smica Ai = ica.get_components() Si = np.linalg.pinv(Ai).dot(X_room) def greedy_comp(X_room, A, S, n_comp=5): bads = [] vals = [] Xc = X_room.copy() idxs = np.arange(S.shape[0]) for i in range(n_comp): print(i) diffs = [np.linalg.norm(Xc - a[:, None] * s) for a, s in zip(A.T[idxs], S[idxs])] j = idxs[np.argmin(diffs)] bads.append(j) Xc -= A[:, j][:, None] * S[j] vals.append(np.linalg.norm(Xc)) idxs = np.delete(idxs, np.where(idxs == j)) return bads, vals bads, vals = greedy_comp(X_room, As, Ss, n_bads) badi, vali = greedy_comp(X_room, Ai, Si, n_bads) np.save('results/expl_%d_%d_%d.npy' % (subject_no, n_comp, n_bads), np.array((vals, vali))) return vals, vali
def compute_decomposition(algo_name, subject, n_components, decomp_num, n_decomp): runs = [6, 10, 14] # motor imagery: hands vs feet filename = '/home/pierre/work/smica/dataset/%s.set' % SUBJECTS[subject] epochs = mne.io.read_epochs_eeglab(filename, verbose='CRITICAL') epochs.set_channel_types({'LEYE': 'eog', 'REYE': 'eog'}) epochs.filter(2, 60) picks = mne.pick_types(epochs.info, meg=False, eeg=True, eog=False, stim=False, exclude='bads') n_bins = 40 freqs = np.linspace(2, 60, n_bins + 1) if algo_name == 'smica': algorithm = ICA(n_components=n_components, freqs=freqs, rng=0) algo_args = dict(em_it=100000, tol=1e-8, n_it_min=100000) mixing = standard_mixing(epochs, picks, algorithm, algo_args) if algo_name == 'jdiag': algorithm = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) algo_args = dict(max_iter=1000, tol=1e-10) mixing = standard_mixing(epochs, picks, algorithm, algo_args) if algo_name == 'sobi': algorithm = SOBI_mne(1000, n_components=n_components, freqs=freqs, rng=0) algo_args = dict() mixing = standard_mixing(epochs, picks, algorithm, algo_args) if algo_name == 'infomax': jdiag = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) jdiag.fit(epochs, picks, max_iter=10) sources = jdiag.compute_sources(method='pinv') K, W, _ = picard(sources, max_iter=1000, ortho=False) picard_mix = np.linalg.pinv(np.dot(W, K)) mixing = np.dot(jdiag.A, picard_mix) if algo_name in ['pinv_infomax', 'wiener_infomax']: smica = ICA(n_components=n_components, freqs=freqs, rng=0) algo_args = dict(em_it=100000, tol=1e-8, n_it_min=100000) smica.fit(epochs, picks, corr=False, **algo_args) method = { 'pinv_infomax': 'pinv', 'wiener_infomax': 'wiener' }[algo_name] sources = smica.compute_sources(method=method) K, W, _ = picard(sources, max_iter=1000, ortho=False) picard_mix = np.linalg.pinv(np.dot(W, K)) mixing = np.dot(smica.A, picard_mix) gof, _, _ = dipolarity(mixing, epochs, picks) print(decomp_num, n_decomp) return gof
epochs_data = epochs.get_data() l_list = [] for event_id in [dict(hands=2), dict(feet=3)]: epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) n_components = 20 freqs = np.linspace(7, 30, 31) ica = ICA(n_components=n_components, freqs=freqs, rng=0) ica.fit(epochs, max_iter=5000, verbose=100) l_list.append([ica.compute_loss(x) for x in epochs_data]) plt.figure() likelihoods = np.array(l_list) for i, label in enumerate(['hand', 'feet']): plt.scatter(*(likelihoods[j, labels == i] for j in [0, 1]), label=label) plt.xlabel('negative loglik of model hand') plt.ylabel('negative loglik of model feet') l_min = np.min(likelihoods) l_max = np.max(likelihoods) t = np.linspace(l_min, l_max) plt.title(subject) plt.plot(t, t, color='k', label='y=x') plt.legend()
def process_one(dipole_no, amp, n_components, crop_idx, crop_len=None): raw_fname = "%sAmp%d_Dip%d_IASoff.fif" % (get_root_dir(), amp, dipole_no) raw = read_raw_fif(raw_fname) raw.load_data() raw.crop(5, None) if crop_len is not None: raw.crop(crop_idx * crop_len, (crop_idx + 1) * crop_len) raw.fix_mag_coil_types() events = find_events(raw, "SYS201") picks = mne.pick_types(raw.info, meg="mag", eeg=False, eog=False, stim=False, exclude="bads") n_bins = 40 freqs = np.linspace(1, 70, n_bins + 1) smica = ICA(n_components, freqs, rng=0).fit( raw, picks=picks, em_it=50000, n_it_lbfgs=200, tol=1e-8, verbose=100, n_it_min=10000, ) ica = ICA_mne(n_components, method="picard").fit(raw.copy().filter(1, 70), picks=picks) jdiag = JDIAG_mne(n_components, freqs, rng=0).fit(raw, picks=picks) tmin, tmax = -0.1, 0.1 event_id = [dipole_no] # pows = np.linalg.norm(smica.A, axis=0) * # np.linalg.norm(smica.powers, axis=0) ** .5 raw_max = raw.copy().filter(1, 40) raw_max = maxwell_filter(raw_max, origin=(0.0, 0.0, 0.0)) epochs = mne.Epochs( raw_max, events, event_id, tmin, tmax, baseline=(None, -0.01), preload=True, picks=picks, ) cov = mne.compute_covariance(epochs, tmax=0) e1 = get_error(epochs, cov) e_2 = get_error_A(smica.A, cov, epochs.info) good = np.argmin(e_2[0]) e2 = [e_2[0][good], e_2[1][good]] e_3 = get_error_A(jdiag.A, cov, epochs.info) good = np.argmin(e_3[0]) e3 = [e_3[0][good], e_3[1][good]] e_4 = get_error_A(ica.get_components(), cov, epochs.info) good = np.argmin(e_4[0]) e4 = [e_4[0][good], e_4[1][good]] crop_str = crop_len if crop_len is not None else 0 np.save( "results/phantom_%d_%d_%d_%d_%d.npy" % (dipole_no, amp, n_components, crop_idx, crop_str), np.array([e1, e2, e3, e4]), ) print(e1, e2, e3, e4) return e1, e2, e3, e4
raw.info, meg="mag", eeg=False, eog=False, stim=False, exclude="bads" ) raw.set_eeg_reference("average", projection=True) # set EEG average reference events = mne.find_events(raw) tmin, tmax = -0.2, 0.5 event_id = { "Auditory/Left": 1, "Auditory/Right": 2, "Visual/Left": 3, "Visual/Right": 4, } reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) # Fit smica freqs = np.linspace(1, 70, 41) smica = ICA(n_components=20, freqs=freqs, rng=0).fit(raw, picks) colors = ["k", "darkred", "forestgreen", "royalblue", "orange"] # Plot the clusters # smica.plot_extended(sort=False) bad_sources = [17, 18, 19] X_filtered = smica.filter( raw._data[picks], bad_sources=bad_sources, method="wiener" ) raw_filtered = raw.copy() raw_filtered._data[picks] = X_filtered
import mne from mne.datasets import sample data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif' raw = mne.io.read_raw_fif(raw_fname, preload=True) picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False, stim=False, exclude='bads') n_components = 59 n_bins = 40 freqs = np.linspace(1, 60, n_bins + 1) loss_list = [] for n_component in np.arange(1, n_components): print(n_component) smica = ICA(n_components=n_component, freqs=freqs, rng=0) smica.fit(raw, picks=picks, verbose=2000, tol=1e-7, em_it=10000) loss_list.append(smica.compute_loss()) plt.plot(np.arange(1, n_components), loss_list - np.min(loss_list)) plt.yscale('log') plt.xlabel('Number of sources') plt.ylabel('Negative log likelihood') plt.show()
# fetch data raw = mne.io.read_raw_fif(raw_fname, preload=True) picks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False, stim=False, exclude='bads') # Compute ICA on raw: chose the frequency decomposition. Here, uniform between # 2 - 35 Hz. # jdiag = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) jdiag.fit(raw, picks=picks, verbose=True, tol=1e-9, max_iter=1000) smica = ICA(n_components=n_components, freqs=freqs, rng=0) smica.fit(raw, picks=picks, verbose=100, tol=1e-10, em_it=100000) # Plot powers # noise_sources = [6, 8, 9] # muscle_source = [7] # f, ax = plt.subplots(figsize=(4, 2)) # plot_powers(smica.powers, noise_sources, muscle_source, ax, 'smica') # plt.show() # # # # # sobi = SOBI_mne(100, n_components, freqs, rng=0) # sobi.fit(raw, picks=picks) raw.filter(2, 70) ica = ICA_mne(n_components=n_components, method='fastica', random_state=0)
sources = set(range(n_s)) free = set(range(n_s)) while sources: i, j = np.unravel_index(np.argmax(corr, axis=None), corr.shape) corr[i, j] = -1 if i in sources and j in free: sources.remove(i) free.remove(j) order[i] = j return order n_bins = 40 freqs = np.linspace(1, 70, n_bins + 1) n_comp = 40 smica_room = ICA(10, freqs=freqs, rng=0).fit(raw_room, picks) jdiag_room = JDIAG_mne(10, freqs=freqs, rng=0).fit(raw_room, picks=picks) smica = ICA(n_comp, freqs=freqs, rng=0).fit(raw, picks=picks) to_plot_smica = [37, 38, 39, 20, 4, 3, 19, 8, 12, 2] raw.filter(1, 70) jdiag = JDIAG_mne(n_comp, freqs=freqs, rng=0).fit(raw, picks=picks) to_plot_jdiag = [2, 1, 0, 13, 34, 3, 20, 30, 5, 10] sobi = SOBI_mne(100, n_comp, freqs).fit(raw, picks=picks) to_plot_sobi = [2, 0, 8, 12, 20, 31, 3, 17, 9, 10] ifmx_ = ICA_mne(n_comp, random_state=0).fit(raw, picks=picks) ifmx = transfer_to_ica(raw, picks, freqs, ifmx_.get_sources(raw).get_data(), ifmx_.get_components()) to_plot_ifmx = [1, 2, 0, 35, 33, 19, 3, 32, 25, 9] plot_args = dict(number=True, t_min=2, t_max=4)
data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/ernoise_raw.fif' raw = mne.io.read_raw_fif(raw_fname, preload=True) picks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False, stim=False, exclude='bads') # Compute ICA on raw: chose the frequency decomposition. Here, uniform between # 2 - 35 Hz. n_bins = 40 n_components = 10 freqs = np.linspace(1, 70, n_bins + 1) smica = ICA(n_components=n_components, freqs=freqs, rng=0) smica.fit(raw, picks=picks, verbose=100, tol=1e-15, em_it=100000) jdiag = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) jdiag.fit(raw, picks=picks, verbose=True, tol=1e-7, max_iter=10000) raw.filter(1, 70) sobi = SOBI_mne(p=2000, n_components=n_components, freqs=freqs, rng=0) sobi.fit(raw, picks=picks, verbose=True, tol=1e-7, max_iter=2000) raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif' raw = mne.io.read_raw_fif(raw_fname, preload=True) picks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False, stim=False, exclude='bads')
eeg=True, eog=False, stim=False, exclude='bads') n_bins = 40 n_components = 20 freqs = np.linspace(1, 60, n_bins + 1) # jdiag = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) jdiag.fit(raw, picks=picks, verbose=True, tol=1e-9, max_iter=1000) # jdiag2 = JDIAG_mne(n_components=n_components, freqs=freqs, rng=0) # jdiag2.fit(raw, pca='other', picks=picks, verbose=True, tol=1e-9, max_iter=1000) smica = ICA(n_components=n_components, freqs=freqs, rng=0) smica.fit(raw, picks=picks, verbose=100, tol=1e-10, em_it=100000, corr=True) # Plot powers # noise_sources = [6, 8, 9] # muscle_source = [7] # f, ax = plt.subplots(figsize=(4, 2)) # plot_powers(smica.powers, noise_sources, muscle_source, ax, 'smica') # plt.show() # # # # raw.filter(2, 70) ica = ICA_mne(n_components=n_components, method='fastica', random_state=0) ica.fit(raw, picks=picks)