def test_io_bem(tmpdir, ext): """Test reading and writing of bem surfaces and solutions.""" import h5py temp_bem = op.join(str(tmpdir), f'temp-bem.{ext}') # model with pytest.raises(ValueError, match='BEM data not found'): read_bem_surfaces(fname_raw) with pytest.raises(ValueError, match='surface with id 10'): read_bem_surfaces(fname_bem_3, s_id=10) surf = read_bem_surfaces(fname_bem_3, patch_stats=True) surf = read_bem_surfaces(fname_bem_3, patch_stats=False) write_bem_surfaces(temp_bem, surf[0]) with pytest.raises(IOError, match='exists'): write_bem_surfaces(temp_bem, surf[0]) write_bem_surfaces(temp_bem, surf[0], overwrite=True) if ext == 'h5': with h5py.File(temp_bem, 'r'): # make sure it's valid pass surf_read = read_bem_surfaces(temp_bem, patch_stats=False) _compare_bem_surfaces(surf, surf_read) # solution with pytest.raises(RuntimeError, match='No BEM solution found'): read_bem_solution(fname_bem_3) temp_sol = op.join(str(tmpdir), f'temp-sol.{ext}') sol = read_bem_solution(fname_bem_sol_3) assert 'BEM' in repr(sol) write_bem_solution(temp_sol, sol) sol_read = read_bem_solution(temp_sol) _compare_bem_solutions(sol, sol_read) sol = read_bem_solution(fname_bem_sol_1) with pytest.raises(RuntimeError, match='BEM does not have.*triangulation'): _bem_find_surface(sol, 3)
def _compute_depth(dip, fname_bem, fname_trans, subject, subjects_dir): """Compute dipole depth.""" trans = _get_trans(fname_trans)[0] bem = read_bem_solution(fname_bem) surf = _bem_find_surface(bem, 'inner_skull') points = surf['rr'] points = apply_trans(trans['trans'], points) depth = _compute_nearest(points, dip.pos, return_dists=True)[1][0] return np.ravel(depth)
def _compute_depth(dip, fname_bem, fname_trans, subject, subjects_dir): """Compute dipole depth.""" trans = _get_trans(fname_trans)[0] bem = read_bem_solution(fname_bem) surf = _bem_find_surface(bem, 'inner_skull') points = surf['rr'] points = apply_trans(trans['trans'], points) depth = _compute_nearest(points, dip.pos, return_dists=True)[1][0] return np.ravel(depth)
def _prepare_for_forward(src, mri_head_t, info, bem, mindist, n_jobs, perts, bem_extra='', trans='', info_extra='', meg=True, eeg=True, ignore_ref=False, verbose=None): """Prepare for forward computation.""" # Read the source locations logger.info('') # let's make a copy in case we modify something src = _ensure_src(src).copy() nsource = sum(s['nuse'] for s in src) if nsource == 0: raise RuntimeError('No sources are active in these source spaces. ' '"do_all" option should be used.') logger.info('Read %d source spaces a total of %d active source locations' % (len(src), nsource)) # Delete some keys to clean up the source space: for key in ['working_dir', 'command_line']: if key in src.info: del src.info[key] # Read the MRI -> head coordinate transformation logger.info('') _print_coord_trans(mri_head_t) # make a new dict with the relevant information arg_list = [ info_extra, trans, src, bem_extra, meg, eeg, mindist, n_jobs, verbose ] cmd = 'make_forward_solution(%s)' % (', '.join([str(a) for a in arg_list])) mri_id = dict(machid=np.zeros(2, np.int32), version=0, secs=0, usecs=0) info = Info(chs=info['chs'], comps=info['comps'], dev_head_t=info['dev_head_t'], mri_file=trans, mri_id=mri_id, meas_file=info_extra, meas_id=None, working_dir=os.getcwd(), command_line=cmd, bads=info['bads'], mri_head_t=mri_head_t) info._update_redundant() info._check_consistency() logger.info('') megcoils, compcoils, megnames, meg_info = [], [], [], [] eegels, eegnames = [], [] if meg and len(pick_types(info, ref_meg=False, exclude=[])) > 0: megcoils, compcoils, megnames, meg_info = \ _prep_meg_channels(info, perts, ignore_ref=ignore_ref) if eeg and len( pick_types(info, meg=False, eeg=True, ref_meg=False, exclude=[])) > 0: eegels, eegnames = _prep_eeg_channels(info) # Check that some channels were found if len(megcoils + eegels) == 0: raise RuntimeError('No MEG or EEG channels found.') # pick out final info info = pick_info( info, pick_types(info, meg=meg, eeg=eeg, ref_meg=False, exclude=[])) # Transform the source spaces into the appropriate coordinates # (will either be HEAD or MRI) for s in src: transform_surface_to(s, 'head', mri_head_t) logger.info('Source spaces are now in %s coordinates.' % _coord_frame_name(s['coord_frame'])) # Prepare the BEM model bem = _setup_bem(bem, bem_extra, len(eegnames), mri_head_t) # Circumvent numerical problems by excluding points too close to the skull if not bem['is_sphere']: inner_skull = _bem_find_surface(bem, 'inner_skull') _filter_source_spaces(inner_skull, mindist, mri_head_t, src, n_jobs) logger.info('') rr = np.concatenate([s['rr'][s['vertno']] for s in src]) if len(rr) < 1: raise RuntimeError('No points left in source space after excluding ' 'points close to inner skull.') # deal with free orientations: source_nn = np.tile(np.eye(3), (len(rr), 1)) update_kwargs = dict(nchan=len(info['ch_names']), nsource=len(rr), info=info, src=src, source_nn=source_nn, source_rr=rr, surf_ori=False, mri_head_t=mri_head_t) return megcoils, meg_info, compcoils, megnames, eegels, eegnames, rr, \ info, update_kwargs, bem