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 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
# use 60s of data raw_filt.crop(0, 60) raw.crop(0, 60) raw_unfilt = raw.copy() picks = mne.pick_types(raw.info, meg=True, exclude='bads') ica_mne = ICA_mne(method='fastica', n_components=60, random_state=42, max_pca_components=None, max_iter=5000, verbose=False) # fit ica object from mne to filtered data ica_mne.fit(raw_filt, picks=picks, reject=reject, verbose=True) # save mean and standard deviation of filtered MEG channels for the standard mne routine pca_mean_filt_mne = ica_mne.pca_mean_.copy() pca_pre_whitener_filt_mne = ica_mne._pre_whitener.copy( ) # this is the standard deviation of MEG channels ica_jumeg = ICA_jumeg(method='fastica', n_components=60, random_state=42, max_pca_components=None, max_iter=5000, verbose=False) # fit ica object from jumeg to filtered data ica_jumeg.fit(raw_filt, picks=picks, reject=reject, verbose=True)