def _fast_map_meg_channels(inst, pick_from, pick_to, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.forward._lead_dots import _do_self_dots, _do_cross_dots from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG def _compute_dots(info, mode='fast'): """Compute all-to-all dots. """ templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots _compute_fast_dots = mem.cache(_compute_dots) info = inst.info.copy() info['bads'] = [] # if bads is different, hash will be different info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') self_dots, cross_dots = _compute_fast_dots(info, mode=mode) cross_dots = cross_dots[pick_to, :][:, pick_from] self_dots = self_dots[pick_from, :][:, pick_from] ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) return fmd['data']
def _fast_map_meg_channels(info, pick_from, pick_to, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.forward._lead_dots import _do_self_dots, _do_cross_dots from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') def _compute_dots(info, mode='fast'): """Compute all-to-all dots.""" templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots _compute_fast_dots = mem.cache(_compute_dots, verbose=0) info['bads'] = [] # if bads is different, hash will be different info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. self_dots, cross_dots = _compute_fast_dots( info, mode=mode) cross_dots = cross_dots[pick_to, :][:, pick_from] self_dots = self_dots[pick_from, :][:, pick_from] ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']
def map_meg_loocv_channels(inst, pick_from, pick_to, self_dots=None, cross_dots=None, mode='fast'): from mne.io.pick import pick_info from mne.forward._lead_dots import _do_self_dots, _do_cross_dots from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.bem import _check_origin info_from = pick_info(inst.info, pick_from, copy=True) info_to = pick_info(inst.info, pick_to, copy=True) # no need to apply trans because both from and to coils are in device # coordinates templates = _read_coil_defs(verbose=False) coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) coils_to = _create_meg_coils(info_to['chs'], 'normal', info_to['dev_head_t'], templates) miss = 1e-4 # Smoothing criterion for MEG int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') my_origin = _check_origin((0., 0., 0.04), info_from) if self_dots is None: self_dots = _do_self_dots(int_rad, False, coils_from, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) if cross_dots is None: cross_dots = _do_cross_dots(int_rad, False, coils_from, coils_to, my_origin, 'meg', lut_fun, n_fact).T ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) return fmd['data'], self_dots, cross_dots
def _compute_dots(info, mode='fast'): """Compute all-to-all dots.""" templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots
def _compute_dots(info, mode='fast'): """Compute all-to-all dots. """ templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots
def test_utils(): """Test utils.""" event_id = {'Visual/Left': 3} tmin, tmax = -0.2, 0.5 events = mne.find_events(raw) picks = mne.pick_channels(raw.info['ch_names'], ['MEG 2443', 'MEG 2442', 'MEG 2441']) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=None, preload=True) this_epoch = epochs.copy() assert this_epoch.info['bads'] == ['MEG 2443'] epochs_clean = clean_by_interp(this_epoch) assert this_epoch.info['bads'] == ['MEG 2443'] assert_array_equal(this_epoch.get_data(), epochs.get_data()) pytest.raises(AssertionError, assert_array_equal, epochs_clean.get_data(), this_epoch.get_data()) picks_meg = mne.pick_types(evoked.info, meg='grad', eeg=False, exclude=[]) picks_eeg = mne.pick_types(evoked.info, meg=False, eeg=True, exclude=[]) picks_bad_meg = mne.pick_channels(evoked.ch_names, include=['MEG 2443']) picks_bad_eeg = mne.pick_channels(evoked.ch_names, include=['EEG 053']) evoked_orig = evoked.copy() for picks, picks_bad in zip([picks_meg, picks_eeg], [picks_bad_meg, picks_bad_eeg]): evoked_autoreject = interpolate_bads(evoked, picks=picks, reset_bads=False) evoked.interpolate_bads(reset_bads=False) assert_array_equal(evoked.data[picks_bad], evoked_autoreject.data[picks_bad]) pytest.raises(AssertionError, assert_array_equal, evoked_orig.data[picks_bad], evoked.data[picks_bad]) # test that autoreject EEG interpolation code behaves the same as MNE evoked_ar = evoked_orig.copy() evoked_mne = evoked_orig.copy() origin = _check_origin('auto', evoked_ar.info) _interpolate_bads_eeg(evoked_ar, picks=None) mne.channels.interpolation._interpolate_bads_eeg(evoked_mne, origin=origin) assert_array_almost_equal(evoked_ar.data, evoked_mne.data)
def _compute_dots(info, mode='fast'): """Compute all-to-all dots.""" from mne.forward._field_interpolation import _setup_dots from mne.forward._lead_dots import _do_self_dots, _do_cross_dots from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.bem import _check_origin templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots
def _fast_map_meg_channels(info, pick_from, pick_to, dots=None, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _patch_setup_dots( mode, info_from, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. if dots is None: dots = self_dots, cross_dots = _compute_dots(info, mode=mode) else: self_dots, cross_dots = dots self_dots, cross_dots = _pick_dots(dots, pick_from, pick_to) ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']
def _fast_map_meg_channels(info, pick_from, pick_to, dots=None, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. if dots is None: dots = self_dots, cross_dots = _compute_dots(info, mode=mode) else: self_dots, cross_dots = dots self_dots, cross_dots = _pick_dots(dots, pick_from, pick_to) ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']