def runICA(subjectID): jumpAmplitudes = dict(grad=400e-12, mag=6e-12) subject = 'dh{:#02d}a'.format(subjectID) subjectPath = data_path + '/MEG_mc_hp004/' + subject + '/block' for block in ['1', '2']: outfilename = subjectPath + block + '_ica.fif' raw_fname = subjectPath + block + '.fif' if os.path.exists(raw_fname): raw = Raw(raw_fname, preload=True) raw.info['bads'] = badChanLibrary[subjectID][int(block)] MEG_channels = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False) ica = ICA(n_components=0.99, n_pca_components=64, max_pca_components=100, noise_cov=None, random_state=17259) # decompose sources for raw data ica.fit(raw, picks=MEG_channels, reject=jumpAmplitudes, tstep=1.0) # Save ICA results for diagnosis and reconstruction ica.save(outfilename) else: print(raw_fname + ' does not exist. Skipping ICA.')
def reject_ica(inst, reference, n_components=0.99, method="fastica", corr_thresh=0.9, random_state=None, plot=False): if isinstance(reference, str): reference = read_ica(reference) ica = ICA(n_components=n_components, method=method) ica.fit(inst) labels = list(reference.labels_.keys()) components = list(reference.labels_.values()) for component, label in zip(components, labels): corrmap([reference, ica], template=(0, component[0]), plot=plot, label=label, threshold=corr_thresh) exclude = [item for subl in list(ica.labels_.values()) for item in subl] ica.apply(inst, exclude=exclude) return inst, ica
def ICA_decompose(raw, method, decim, variance, npcas, maxpcas, reject, picks): #################### RUN ICA ica = ICA(n_components=variance, n_pca_components=npcas, max_pca_components=maxpcas, method=method, verbose=True) ica.fit(raw, decim=decim, reject=reject, picks=picks) ica.get_sources(raw) return ica
def ICA(self, mneObj, icCount=None, random_state=None): picks = self.createPicks(mneObj) reject = dict(eeg=300) if icCount is None: icCount = len(picks) ica = ICA(n_components=icCount, method="fastica", random_state=random_state) ica.fit(mneObj, picks=picks, reject=reject) return ica
def ICA_decompose(raw, method, decim, variance, npcas, maxpcas, reject, picks): r = np.random.RandomState(1234) # allow for reproducible results r.uniform(0, 10, 5) #################### RUN ICA ica = ICA(n_components=variance, n_pca_components=npcas, max_pca_components=maxpcas, method=method, verbose=True, random_state=r) ica.fit(raw, decim=decim, reject=reject, picks=picks) ica.get_sources(raw) return ica
def ica_convert2mne(unmixing, pca, info=None, method='fastica'): # create MNE-type of ICA object from mne.preprocessing.ica import ICA n_comp = unmixing.shape[1] if method == 'extended-infomax': ica_method = 'infomax' fit_params = dict(extended=True) else: ica_method = method fit_params = None ica = ICA(n_components=n_comp, method=ica_method, fit_params=fit_params) # add PCA object ica.pca = pca # PCA info to be used bei MNE-Python ica.pca_mean_ = pca.mean_ ica.pca_components_ = pca.components_ exp_var = pca.explained_variance_ ica.pca_explained_variance_ = exp_var ica.pca_explained_variance_ratio_ = pca.explained_variance_ratio_ # ICA info ica.n_components_ = n_comp ica.n_components = n_comp ica.components_ = unmixing # compatible with sklearn ica.unmixing_ = ica.components_ # as used by sklearn ica.mixing_ = pinv(ica.unmixing_) # as used by sklearn ica.unmixing_matrix_ = ica.unmixing_ / np.sqrt( exp_var[0:n_comp])[None, :] # as used by MNE-Python ica.mixing_matrix_ = pinv(ica.unmixing_matrix_) # as used by MNE-Python ica._ica_names = ['ICA%03d' % ii for ii in range(n_comp)] ica.fun = method if info: ica.info = info return ica
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') ############################################################################### # Setup ICA seed decompose data, then access and plot sources. # Instead of the actual number of components here we pass a float value # between 0 and 1 to select n_components based on the percentage of # variance explained by the PCA components. ica = ICA(n_components=0.90, n_pca_components=None, max_pca_components=None, random_state=0) # Also we decide to use all PCA components before mixing back to sensor space. # You can again use percentages (float) or set the total number of components # to be kept directly (int) which allows to control the amount of additional # denoising. ica.n_pca_components = 1.0 # decompose sources for raw data using each third sample. ica.decompose_raw(raw, picks=picks, decim=3) print(ica) # plot reasonable time window for inspection start_plot, stop_plot = 100., 103.
proj=False, picks=picks, baseline=baseline, preload=True, reject=reject) random_state = np.random.RandomState(42) ############################################################################### # Setup ICA seed decompose data, then access and plot sources. # for more background information visit the plot_ica_from_raw.py example # fit sources from epochs or from raw (both works for epochs) ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100, noise_cov=None, random_state=random_state) ica.decompose_epochs(epochs, decim=2) print(ica) # plot spatial sensitivities of a few ICA components title = 'Spatial patterns of ICA components (Magnetometers)' source_idx = range(35, 50) ica.plot_topomap(source_idx, ch_type='mag') plt.suptitle(title, fontsize=12) ############################################################################### # Automatically find ECG and EOG component using correlation coefficient.
def eeglab2mne(fname, montage='standard_1020', event_id=None, load_ica=False): """Get an EEGLAB dataset into a MNE Raw object. Parameters ---------- input_fname : str Path to the .set file. If the data is stored in a separate .fdt file, it is expected to be in the same folder as the .set file. montage : str | None | instance of montage Path or instance of montage containing electrode positions. If None, sensor locations are (0,0,0). See the documentation of :func:`mne.channels.read_montage` for more information. event_id : dict Map event id (integer) to string name of corresponding event. This allows to smoothly load EEGLAB event structure when overlapping events (e.g. boundaries) occur. load_ica : bool Default to False. Load ica matrices from eeglab structure if available and attempt to transfer them into the ica structure of MNE. Returns ------- raw : Instance of RawEEGLAB A Raw object containing EEGLAB .set data. ica : Instance of ICA If load_ica True Note ---- ICA matrices in ICA MNE object might not entirely capture the decomposition. To apply projections (i.e. remove some components from observed EEG data) it might be better to load directly the matrices and do it by hand, where: - icawinv = pinv(icaweights * icasphere) - ica_act = icaweights * icasphere * eegdata References ---------- .. [#] https://benediktehinger.de/blog/science/ica-weights-and-invweights/ .. [#] https://github.com/mne-tools/mne-python/pull/5114/files """ montage_mne = mne.channels.make_standard_montage(montage) try: raw = mne.io.read_raw_eeglab(input_fname=fname, preload=True) except NotImplementedError: print("Version 7.3 matlab file detected, will load 'by hand'") eeg, srateate, _, _, _, ch_names = _load_eeglab_data(fname) info = mne.create_info(ch_names=ch_names, sfreq=srateate, ch_types='eeg') raw = mne.io.RawArray(eeg.T, info) # set up montage: raw.set_montage(montage_mne) if load_ica: weights, winv, sphere = load_ica_matrices(fname) ica = ICA(n_components=winv.shape[1], max_pca_components=winv.shape[1]) ica.fit(raw, decim=2, start=1., stop=60.) ica.unmixing_matrix_ = weights.dot(sphere.dot(ica.pca_components_.T)) ica.mixing_matrix_ = np.linalg.pinv(ica.unmixing_matrix_) return raw, ica return raw