def test_make_morph_maps(): """Test reading and creating morph maps """ # make a new fake subjects_dir tempdir = _TempDir() for subject in ('sample', 'sample_ds', 'fsaverage_ds'): os.mkdir(op.join(tempdir, subject)) os.mkdir(op.join(tempdir, subject, 'surf')) for hemi in ['lh', 'rh']: args = [subject, 'surf', hemi + '.sphere.reg'] copyfile(op.join(subjects_dir, *args), op.join(tempdir, *args)) # this should trigger the creation of morph-maps dir and create the map mmap = read_morph_map('fsaverage_ds', 'sample_ds', tempdir) mmap2 = read_morph_map('fsaverage_ds', 'sample_ds', subjects_dir) assert_equal(len(mmap), len(mmap2)) for m1, m2 in zip(mmap, mmap2): # deal with sparse matrix stuff diff = (m1 - m2).data assert_allclose(diff, np.zeros_like(diff), atol=1e-3, rtol=0) # This will also trigger creation, but it's trivial mmap = read_morph_map('sample', 'sample', subjects_dir=tempdir) for mm in mmap: assert_true((mm - sparse.eye(mm.shape[0], mm.shape[0])).sum() == 0)
def test_make_morph_maps(): """Test reading and creating morph maps.""" # make a new fake subjects_dir tempdir = _TempDir() for subject in ('sample', 'sample_ds', 'fsaverage_ds'): os.mkdir(op.join(tempdir, subject)) os.mkdir(op.join(tempdir, subject, 'surf')) regs = ('reg', 'left_right') if subject == 'fsaverage_ds' else ('reg',) for hemi in ['lh', 'rh']: for reg in regs: args = [subject, 'surf', hemi + '.sphere.' + reg] copyfile(op.join(subjects_dir, *args), op.join(tempdir, *args)) for subject_from, subject_to, xhemi in ( ('fsaverage_ds', 'sample_ds', False), ('fsaverage_ds', 'fsaverage_ds', True)): # trigger the creation of morph-maps dir and create the map with warnings.catch_warnings(record=True): mmap = read_morph_map(subject_from, subject_to, tempdir, xhemi=xhemi) mmap2 = read_morph_map(subject_from, subject_to, subjects_dir, xhemi=xhemi) assert_equal(len(mmap), len(mmap2)) for m1, m2 in zip(mmap, mmap2): # deal with sparse matrix stuff diff = (m1 - m2).data assert_allclose(diff, np.zeros_like(diff), atol=1e-3, rtol=0) # This will also trigger creation, but it's trivial with warnings.catch_warnings(record=True): mmap = read_morph_map('sample', 'sample', subjects_dir=tempdir) for mm in mmap: assert (mm - sparse.eye(mm.shape[0], mm.shape[0])).sum() == 0
def test_make_morph_maps(): """Test reading and creating morph maps.""" # make a new fake subjects_dir tempdir = _TempDir() for subject in ('sample', 'sample_ds', 'fsaverage_ds'): os.mkdir(op.join(tempdir, subject)) os.mkdir(op.join(tempdir, subject, 'surf')) regs = ('reg', 'left_right') if subject == 'fsaverage_ds' else ('reg',) for hemi in ['lh', 'rh']: for reg in regs: args = [subject, 'surf', hemi + '.sphere.' + reg] copyfile(op.join(subjects_dir, *args), op.join(tempdir, *args)) for subject_from, subject_to, xhemi in ( ('fsaverage_ds', 'sample_ds', False), ('fsaverage_ds', 'fsaverage_ds', True)): # trigger the creation of morph-maps dir and create the map with warnings.catch_warnings(record=True): mmap = read_morph_map(subject_from, subject_to, tempdir, xhemi=xhemi) mmap2 = read_morph_map(subject_from, subject_to, subjects_dir, xhemi=xhemi) assert_equal(len(mmap), len(mmap2)) for m1, m2 in zip(mmap, mmap2): # deal with sparse matrix stuff diff = (m1 - m2).data assert_allclose(diff, np.zeros_like(diff), atol=1e-3, rtol=0) # This will also trigger creation, but it's trivial with warnings.catch_warnings(record=True): mmap = read_morph_map('sample', 'sample', subjects_dir=tempdir) for mm in mmap: assert_true((mm - sparse.eye(mm.shape[0], mm.shape[0])).sum() == 0)
def test_make_morph_maps(): """Test reading and creating morph maps """ mmap = read_morph_map('fsaverage', 'sample', subjects_dir=subjects_dir) mmap2 = _make_morph_map('fsaverage', 'sample', subjects_dir=subjects_dir) assert_equal(len(mmap), len(mmap2)) for m1, m2 in zip(mmap, mmap2): # deal with sparse matrix stuff diff = (m1 - m2).data assert_allclose(diff, np.zeros_like(diff), atol=1e-3, rtol=0)
def mne_anatomy(subject, subjects_dir, overwrite=False): import warnings # Checks that watershed hasn't already been run for fname in [ 'fiducials.fif', 'head.fif', 'head-dense.fif', 'head-medium.fif', 'head-sparse.fif', 'inner_skull.surf', 'oct-6-src.fif' ]: fname = op.join(subjects_dir, subject, 'bem', subject + '-' + fname) if (not overwrite) and op.exists(fname): raise IOError('%s already exists. Set overwrite=True.' % fname) return # Create BEM surfaces make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # Copy files outside watershed folder in case of bad manipulation for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from_file = op.join(subjects_dir, subject, 'bem', 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(subjects_dir, subject, 'bem', '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) # Update file try: os.symlink(from_file, to_file) except OSError as e: # if disk is not NTFS, symoblic link isn't possible if e.strerror == 'Operation not permitted': from shutil import copyfile copyfile(from_file, to_file) # Make scalp surfaces make_scalp_surfaces(subjects_dir, subject, force='store_true', overwrite='store_true', verbose=None) # Setup source space src_fname = op.join(subjects_dir, subject, 'bem', subject + 'oct-6-src.fif') if not op.isfile(src_fname): from mne import setup_source_space setup_source_space(subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # Prepare BEM model bem_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem.fif') bem_sol_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem-sol.fif') if not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) surfs = make_bem_model(subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) # Make morphs to fsaverage if has it try: read_morph_map(subject, 'fsaverage', subjects_dir=subjects_dir) except IOError as e: if 'No such file or directory' in e.strerror: warnings.warn(e.strerror)
def mne_anatomy(subject, subjects_dir, overwrite=False): import warnings # Checks that watershed hasn't already been run for fname in ['fiducials.fif', 'head.fif', 'head-dense.fif', 'head-medium.fif', 'head-sparse.fif', 'inner_skull.surf', 'oct-6-src.fif']: fname = op.join(subjects_dir, subject, 'bem', subject + '-' + fname) if (not overwrite) and op.exists(fname): raise IOError('%s already exists. Set overwrite=True.' % fname) return # Create BEM surfaces make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # Copy files outside watershed folder in case of bad manipulation for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from_file = op.join(subjects_dir, subject, 'bem', 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(subjects_dir, subject, 'bem', '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) # Update file try: os.symlink(from_file, to_file) except OSError as e: # if disk is not NTFS, symoblic link isn't possible if e.strerror == 'Operation not permitted': from shutil import copyfile copyfile(from_file, to_file) # Make scalp surfaces make_scalp_surfaces(subjects_dir, subject, force='store_true', overwrite='store_true', verbose=None) # Setup source space src_fname = op.join(subjects_dir, subject, 'bem', subject + 'oct-6-src.fif') if not op.isfile(src_fname): from mne import setup_source_space setup_source_space(subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # Prepare BEM model bem_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem.fif') bem_sol_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem-sol.fif') if not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) surfs = make_bem_model(subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) # Make morphs to fsaverage if has it try: read_morph_map(subject, 'fsaverage', subjects_dir=subjects_dir) except IOError as e: if 'No such file or directory' in e.strerror: warnings.warn(e.strerror)