def test_read_ica_eeglab(): """Test read_ica_eeglab function.""" fname = op.join(test_base_dir, "EEGLAB", "test_raw.set") fname_cleaned_matlab = op.join(test_base_dir, "EEGLAB", "test_raw.cleaned.set") raw = read_raw_eeglab(fname, preload=True) raw_eeg = _check_load_mat(fname, None) raw_cleaned_matlab = read_raw_eeglab(fname_cleaned_matlab, preload=True) mark_to_remove = ["manual"] comp_info = raw_eeg.marks["comp_info"] if len(comp_info["flags"].shape) > 1: ind_comp_to_drop = [np.where(flags)[0] for flags, label in zip(comp_info["flags"], comp_info["label"]) if label in mark_to_remove] ind_comp_to_drop = np.unique(np.concatenate(ind_comp_to_drop)) else: ind_comp_to_drop = np.where(comp_info["flags"])[0] ica = read_ica_eeglab(fname) _assert_ica_attributes(ica) raw_cleaned = ica.apply(raw.copy(), exclude=ind_comp_to_drop) assert_allclose(raw_cleaned_matlab.get_data(), raw_cleaned.get_data(), rtol=1e-05, atol=1e-08)
def add_bad_segment_annot(raw, file_name, mark_to_remove=("manual", )): raw_eeg = _check_load_mat(file_name, None) time_info = raw_eeg.marks["time_info"] if len(np.array(time_info["flags"]).shape) > 1: ind_time_to_drop = np.unique( np.concatenate([ np.where(flags)[0] for flags, label in zip(time_info["flags"], time_info["label"]) if label in mark_to_remove ])) else: ind_time_to_drop = np.where(time_info["flags"])[0] ind_starts = np.concatenate( [[ind_time_to_drop[0]], ind_time_to_drop[np.where(np.diff(ind_time_to_drop) > 1)[0] + 1]]) ind_ends = np.concatenate([ ind_time_to_drop[np.where(np.diff(ind_time_to_drop) > 1)[0]], [ind_time_to_drop[-1]] ]) durations = (ind_ends + 1 - ind_starts) / raw.info["sfreq"] onsets = ind_starts / raw.info["sfreq"] for onset, duration in zip(onsets, durations): raw.annotations.append(onset, duration, description="bad_lossless_qc")
def remove_rejected_ica_components(raw, file_name, inplace=True): raw_eeg = _check_load_mat(file_name, None) mark_to_remove = ["manual"] comp_info = raw_eeg.marks["comp_info"] if len(np.array(comp_info["flags"]).shape) > 1: ind_comp_to_drop = np.unique(np.concatenate([np.where(flags)[0] for flags, label in zip(comp_info["flags"], comp_info["label"]) if label in mark_to_remove])) else: ind_comp_to_drop = np.where(comp_info["flags"])[0] if inplace: read_ica_eeglab(file_name).apply(raw, exclude=ind_comp_to_drop) else: read_ica_eeglab(file_name).apply(raw.copy(), exclude=ind_comp_to_drop)
def mark_bad_channels(raw, file_name, mark_to_remove=("manual", "rank")): raw_eeg = _check_load_mat(file_name, None) info, _, _ = _get_info(raw_eeg) print("############ file_name", file_name, type(file_name)) print("############ raw_eeg", raw_eeg.keys(), type(raw_eeg)) chan_info = raw_eeg.marks["chan_info"] mat_chans = np.array(info["ch_names"]) assert (len(chan_info["flags"][0]) == len(mat_chans)) if len(np.array(chan_info["flags"]).shape) > 1: ind_chan_to_drop = np.unique(np.concatenate([np.where(flags)[0] for flags, label in zip(chan_info["flags"], chan_info["label"]) if label in mark_to_remove])) else: ind_chan_to_drop = np.where(chan_info["flags"])[0] bad_chan = [chan for chan in mat_chans[ind_chan_to_drop]] raw.info['bads'].extend(bad_chan)