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 _setup_args(info): """Helper to test_as_meg_type_evoked.""" coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t']) int_rad, noise, lut_fun, n_fact = _setup_dots('fast', coils, 'meg') my_origin = np.array([0., 0., 0.04]) args_dict = dict(intrad=int_rad, volume=False, coils1=coils, r0=my_origin, ch_type='meg', lut=lut_fun, n_fact=n_fact) return args_dict
def _setup_args(info): """Helper to test_as_meg_type_evoked.""" coils = _create_meg_coils(info["chs"], "normal", info["dev_head_t"]) my_origin, int_rad, noise, lut_fun, n_fact = _setup_dots("fast", coils, "meg") args_dict = dict( intrad=int_rad, volume=False, coils1=coils, r0=my_origin, ch_type="meg", lut=lut_fun, n_fact=n_fact ) return args_dict
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 _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_magnetic_dipole(): """Test basic magnetic dipole forward calculation.""" trans = Transform('mri', 'head') info = read_info(fname_raw) picks = pick_types(info, meg=True, eeg=False, exclude=[]) info = pick_info(info, picks[:12]) coils = _create_meg_coils(info['chs'], 'normal', trans) # magnetic dipole at device origin r0 = np.array([0., 13., -6.]) for ch, coil in zip(info['chs'], coils): rr = (ch['loc'][:3] + r0) / 2. far_fwd = _magnetic_dipole_field_vec(r0[np.newaxis, :], [coil]) near_fwd = _magnetic_dipole_field_vec(rr[np.newaxis, :], [coil]) ratio = 8. if ch['ch_name'][-1] == '1' else 16. # grad vs mag assert_allclose(np.median(near_fwd / far_fwd), ratio, atol=1e-1)
def test_magnetic_dipole(): """Test basic magnetic dipole forward calculation """ trans = Transform("mri", "head", np.eye(4)) info = read_info(fname_raw) picks = pick_types(info, meg=True, eeg=False, exclude=[]) info = pick_info(info, picks[:12]) coils = _create_meg_coils(info["chs"], "normal", trans) # magnetic dipole at device origin r0 = np.array([0.0, 13.0, -6.0]) for ch, coil in zip(info["chs"], coils): rr = (ch["loc"][:3] + r0) / 2.0 far_fwd = _magnetic_dipole_field_vec(r0[np.newaxis, :], [coil]) near_fwd = _magnetic_dipole_field_vec(rr[np.newaxis, :], [coil]) ratio = 8.0 if ch["ch_name"][-1] == "1" else 16.0 # grad vs mag assert_allclose(np.median(near_fwd / far_fwd), ratio, atol=1e-1)
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']
def test_magnetic_dipole(): """Test basic magnetic dipole forward calculation.""" info = read_info(fname_raw) picks = pick_types(info, meg=True, eeg=False, exclude=[]) info = pick_info(info, picks[:12]) coils = _create_meg_coils(info['chs'], 'normal', None) # magnetic dipole far (meters!) from device origin r0 = np.array([0., 13., -6.]) for ch, coil in zip(info['chs'], coils): rr = (ch['loc'][:3] + r0) / 2. # get halfway closer far_fwd = _magnetic_dipole_field_vec(r0[np.newaxis, :], [coil]) near_fwd = _magnetic_dipole_field_vec(rr[np.newaxis, :], [coil]) ratio = 8. if ch['ch_name'][-1] == '1' else 16. # grad vs mag assert_allclose(np.median(near_fwd / far_fwd), ratio, atol=1e-1) # degenerate case r0 = coils[0]['rmag'][[0]] with pytest.raises(RuntimeError, match='Coil too close'): _magnetic_dipole_field_vec(r0, coils[:1]) with pytest.warns(RuntimeWarning, match='Coil too close'): fwd = _magnetic_dipole_field_vec(r0, coils[:1], too_close='warning') assert not np.isfinite(fwd).any() with np.errstate(invalid='ignore'): fwd = _magnetic_dipole_field_vec(r0, coils[:1], too_close='info') assert not np.isfinite(fwd).any()