def test_morph_data(): """Test morphing of data """ subject_from = 'sample' subject_to = 'fsaverage' fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis-meg') stc_from = read_source_estimate(fname, subject='sample') fname = op.join(data_path, 'MEG', 'sample', 'fsaverage_audvis-meg') stc_to = read_source_estimate(fname) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3) # indexing silliness here due to mne_make_movie's indexing oddities assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertno, vertices_to, smooth=12) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3) assert_true(stc_to5.data.shape[0] == 163842 + 163842)
def test_morph_data(): """Test morphing of data """ subject_from = 'sample' subject_to = 'fsaverage' fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis-meg') stc_from = read_source_estimate(fname) stc_from.crop(0.09, 0.1) # for faster computation # After running this: # stc_from.save('%s_audvis-meg-cropped' % subject_from) # this was run from a command line: # mne_make_movie --stcin sample_audvis-meg-cropped-lh.stc # --subject sample --morph fsaverage --smooth 12 --morphgrade 3 # --stc fsaverage_audvis-meg-cropped # XXX These files should eventually be moved to the sample dataset and # removed from mne/fiff/tests/data/ fname = op.join(op.dirname(__file__), '..', 'fiff', 'tests', 'data', 'fsaverage_audvis-meg-cropped') stc_to = read_source_estimate(fname) stc_to1 = morph_data(subject_from, subject_to, stc_from, grade=3, smooth=12, buffer_size=1000) stc_to1.save('%s_audvis-meg' % subject_to) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=3, smooth=12, buffer_size=3) # indexing silliness here due to mne_make_movie's indexing oddities assert_array_almost_equal(stc_to.data, stc_to1.data[:, 0][:, None], 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) # make sure precomputed morph matrices work vertices_to = grade_to_vertices(subject_to, grade=3) morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertno, vertices_to, smooth=12) stc_to3 = morph_data_precomputed(subject_from, subject_to, stc_from, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # test two types of morphing: # 1) make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3) assert_true(stc_to5.data.shape[0] == 163842 + 163842) # 2) make sure we can specify vertices vertices_to = [np.arange(10242), np.arange(10242)] stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3) stc_to4 = morph_data(subject_from, subject_to, stc_from, grade=5, smooth=12, buffer_size=3) assert_array_almost_equal(stc_to3.data, stc_to4.data)
def test_morph_data(): """Test morphing of data """ subject_from = 'sample' subject_to = 'fsaverage' fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis-meg') stc_from = read_source_estimate(fname, subject='sample') fname = op.join(data_path, 'MEG', 'sample', 'fsaverage_audvis-meg') stc_to = read_source_estimate(fname) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertno, vertices_to, smooth=12, subjects_dir=subjects_dir) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_true(stc_to5.data.shape[0] == 163842 + 163842) # test morphing to the same subject stc_to6 = stc_from.morph(subject_from, grade=stc_from.vertno, smooth=1, subjects_dir=subjects_dir) mask = np.ones(stc_from.data.shape[0], dtype=np.bool) # XXX: there is a bug somewhere that causes a difference at 2 vertices.. mask[6799] = False mask[6800] = False assert_array_almost_equal(stc_from.data[mask], stc_to6.data[mask], 5)
def test_morph_data(): """Test morphing of data """ subject_from = 'sample' subject_to = 'fsaverage' fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis-meg') stc_from = read_source_estimate(fname, subject='sample') fname = op.join(data_path, 'MEG', 'sample', 'fsaverage_audvis-meg') stc_to = read_source_estimate(fname) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3, subjects_dir=subjects_dir) # indexing silliness here due to mne_make_movie's indexing oddities assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertno, vertices_to, smooth=12, subjects_dir=subjects_dir) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_true(stc_to5.data.shape[0] == 163842 + 163842)
def test_morph_data(): """Test morphing of data """ tempdir = _TempDir() subject_from = 'sample' subject_to = 'fsaverage' stc_from = read_source_estimate(fname_smorph, subject='sample') stc_to = read_source_estimate(fname_fmorph) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3, subjects_dir=subjects_dir) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertices, vertices_to, smooth=12, subjects_dir=subjects_dir) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_true(stc_to5.data.shape[0] == 163842 + 163842) # Morph sparse data # Make a sparse stc stc_from.vertices[0] = stc_from.vertices[0][[100, 500]] stc_from.vertices[1] = stc_from.vertices[1][[200]] stc_from._data = stc_from._data[:3] assert_raises(RuntimeError, stc_from.morph, subject_to, sparse=True, grade=5, subjects_dir=subjects_dir) stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep) stc_from.vertices[0] = np.array([], dtype=np.int64) stc_from._data = stc_from._data[:1] stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep)
def test_morph_data(): """Test morphing of data.""" tempdir = _TempDir() subject_from = 'sample' subject_to = 'fsaverage' stc_from = read_source_estimate(fname_smorph, subject='sample') stc_to = read_source_estimate(fname_fmorph) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation assert_array_equal(stc_to.time_as_index([0.09, 0.1], use_rounding=True), [0, len(stc_to.times) - 1]) pytest.raises(ValueError, stc_from.morph, subject_to, grade=3, smooth=-1, subjects_dir=subjects_dir) stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # Morphing to a density that is too high should raise an informative error # (here we need to push to grade=6, but for some subjects even grade=5 # will break) pytest.raises(ValueError, stc_to1.morph, subject_from, grade=6, subjects_dir=subjects_dir) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3, subjects_dir=subjects_dir) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3, subjects_dir=subjects_dir) # make sure we get a warning about # of steps with pytest.warns(RuntimeWarning, match='consider increasing'): morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=1, buffer_size=3, subjects_dir=subjects_dir) assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertices, vertices_to, smooth=12, subjects_dir=subjects_dir) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) pytest.raises(ValueError, stc_from.morph_precomputed, subject_to, vertices_to, 'foo') pytest.raises(ValueError, stc_from.morph_precomputed, subject_to, [vertices_to[0]], morph_mat) pytest.raises(ValueError, stc_from.morph_precomputed, subject_to, [vertices_to[0][:-1], vertices_to[1]], morph_mat) pytest.raises(ValueError, stc_from.morph_precomputed, subject_to, vertices_to, morph_mat, subject_from='foo') # steps warning with pytest.warns(RuntimeWarning, match='steps'): compute_morph_matrix(subject_from, subject_to, stc_from.vertices, vertices_to, smooth=1, subjects_dir=subjects_dir) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert (np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert (stc_to5.data.shape[0] == 163842 + 163842) # Morph sparse data # Make a sparse stc stc_from.vertices[0] = stc_from.vertices[0][[100, 500]] stc_from.vertices[1] = stc_from.vertices[1][[200]] stc_from._data = stc_from._data[:3] pytest.raises(RuntimeError, stc_from.morph, subject_to, sparse=True, grade=5, subjects_dir=subjects_dir) stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep) stc_from.vertices[0] = np.array([], dtype=np.int64) stc_from._data = stc_from._data[:1] stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep) # Morph vector data stc_vec = _real_vec_stc() # Ignore warnings about number of steps stc_vec_to1 = stc_vec.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_vec_to2 = stc_vec.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_vec_to1.data, stc_vec_to2.data)
def test_morph_data(): """Test morphing of data """ tempdir = _TempDir() subject_from = 'sample' subject_to = 'fsaverage' stc_from = read_source_estimate(fname_smorph, subject='sample') stc_to = read_source_estimate(fname_fmorph) # make sure we can specify grade stc_from.crop(0.09, 0.1) # for faster computation stc_to.crop(0.09, 0.1) # for faster computation assert_array_equal(stc_to.time_as_index([0.09, 0.1], use_rounding=True), [0, len(stc_to.times) - 1]) assert_raises(ValueError, stc_from.morph, subject_to, grade=3, smooth=-1, subjects_dir=subjects_dir) stc_to1 = stc_from.morph(subject_to, grade=3, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) stc_to1.save(op.join(tempdir, '%s_audvis-meg' % subject_to)) # Morphing to a density that is too high should raise an informative error # (here we need to push to grade=6, but for some subjects even grade=5 # will break) assert_raises(ValueError, stc_to1.morph, subject_from, grade=6, subjects_dir=subjects_dir) # make sure we can specify vertices vertices_to = grade_to_vertices(subject_to, grade=3, subjects_dir=subjects_dir) stc_to2 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=1000, subjects_dir=subjects_dir) # make sure we can use different buffer_size stc_to3 = morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=12, buffer_size=3, subjects_dir=subjects_dir) # make sure we get a warning about # of steps with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') morph_data(subject_from, subject_to, stc_from, grade=vertices_to, smooth=1, buffer_size=3, subjects_dir=subjects_dir) assert_equal(len(w), 2) assert_array_almost_equal(stc_to.data, stc_to1.data, 5) assert_array_almost_equal(stc_to1.data, stc_to2.data) assert_array_almost_equal(stc_to1.data, stc_to3.data) # make sure precomputed morph matrices work morph_mat = compute_morph_matrix(subject_from, subject_to, stc_from.vertices, vertices_to, smooth=12, subjects_dir=subjects_dir) stc_to3 = stc_from.morph_precomputed(subject_to, vertices_to, morph_mat) assert_array_almost_equal(stc_to1.data, stc_to3.data) assert_raises(ValueError, stc_from.morph_precomputed, subject_to, vertices_to, 'foo') assert_raises(ValueError, stc_from.morph_precomputed, subject_to, [vertices_to[0]], morph_mat) assert_raises(ValueError, stc_from.morph_precomputed, subject_to, [vertices_to[0][:-1], vertices_to[1]], morph_mat) assert_raises(ValueError, stc_from.morph_precomputed, subject_to, vertices_to, morph_mat, subject_from='foo') # steps warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') compute_morph_matrix(subject_from, subject_to, stc_from.vertices, vertices_to, smooth=1, subjects_dir=subjects_dir) assert_equal(len(w), 2) mean_from = stc_from.data.mean(axis=0) mean_to = stc_to1.data.mean(axis=0) assert_true(np.corrcoef(mean_to, mean_from).min() > 0.999) # make sure we can fill by morphing stc_to5 = morph_data(subject_from, subject_to, stc_from, grade=None, smooth=12, buffer_size=3, subjects_dir=subjects_dir) assert_true(stc_to5.data.shape[0] == 163842 + 163842) # Morph sparse data # Make a sparse stc stc_from.vertices[0] = stc_from.vertices[0][[100, 500]] stc_from.vertices[1] = stc_from.vertices[1][[200]] stc_from._data = stc_from._data[:3] assert_raises(RuntimeError, stc_from.morph, subject_to, sparse=True, grade=5, subjects_dir=subjects_dir) stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep) stc_from.vertices[0] = np.array([], dtype=np.int64) stc_from._data = stc_from._data[:1] stc_to_sparse = stc_from.morph(subject_to, grade=None, sparse=True, subjects_dir=subjects_dir) assert_array_almost_equal(np.sort(stc_from.data.sum(axis=1)), np.sort(stc_to_sparse.data.sum(axis=1))) assert_equal(len(stc_from.rh_vertno), len(stc_to_sparse.rh_vertno)) assert_equal(len(stc_from.lh_vertno), len(stc_to_sparse.lh_vertno)) assert_equal(stc_to_sparse.subject, subject_to) assert_equal(stc_from.tmin, stc_from.tmin) assert_equal(stc_from.tstep, stc_from.tstep)