def test_vec_stc(): """Test vector source estimate.""" nn = np.array([ [1, 0, 0], [0, 1, 0], [0, 0, 1], [np.sqrt(1 / 3.)] * 3 ]) src = [dict(nn=nn[:2]), dict(nn=nn[2:])] verts = [np.array([0, 1]), np.array([0, 1])] data = np.array([ [1, 0, 0], [0, 2, 0], [3, 0, 0], [1, 1, 1], ])[:, :, np.newaxis] stc = VectorSourceEstimate(data, verts, 0, 1, 'foo') # Magnitude of the vectors assert_array_equal(stc.magnitude().data[:, 0], [1, 2, 3, np.sqrt(3)]) # Vector components projected onto the vertex normals normal = stc.normal(src) assert_array_equal(normal.data[:, 0], [1, 2, 0, np.sqrt(3)])
def test_plot_vec_source_estimates(): """Test plotting of vector source estimates.""" sample_src = read_source_spaces(src_fname) vertices = [s['vertno'] for s in sample_src] n_verts = sum(len(v) for v in vertices) n_time = 5 data = np.random.RandomState(0).rand(n_verts, 3, n_time) stc = VectorSourceEstimate(data, vertices, 1, 1) with warnings.catch_warnings(record=True): warnings.simplefilter('always') stc.plot('sample', subjects_dir=subjects_dir)
def test_plot_vec_source_estimates(): """Test plotting of vector source estimates.""" sample_src = read_source_spaces(src_fname) vertices = [s['vertno'] for s in sample_src] n_verts = sum(len(v) for v in vertices) n_time = 5 data = np.random.RandomState(0).rand(n_verts, 3, n_time) stc = VectorSourceEstimate(data, vertices, 1, 1) stc.plot('sample', subjects_dir=subjects_dir) with pytest.raises(ValueError, match='use "pos_lims"'): stc.plot('sample', subjects_dir=subjects_dir, clim=dict(pos_lims=[1, 2, 3]))
def test_plot_vector_source_estimates(renderer_interactive): """Test plotting of vector source estimates.""" sample_src = read_source_spaces(src_fname) vertices = [s['vertno'] for s in sample_src] n_verts = sum(len(v) for v in vertices) n_time = 5 data = np.random.RandomState(0).rand(n_verts, 3, n_time) stc = VectorSourceEstimate(data, vertices, 1, 1) brain = stc.plot('sample', subjects_dir=subjects_dir, hemi='both', smoothing_steps=1, verbose='error') brain.close() del brain with pytest.raises(ValueError, match='use "pos_lims"'): stc.plot('sample', subjects_dir=subjects_dir, clim=dict(pos_lims=[1, 2, 3])) with pytest.raises(ValueError, match='cannot be used'): stc.plot('sample', subjects_dir=subjects_dir, show_traces=True, time_viewer=False)
def test_apply_forward(): """Test projection of source space data to sensor space.""" start = 0 stop = 5 n_times = stop - start - 1 sfreq = 10.0 t_start = 0.123 fwd = read_forward_solution(fname_meeg) fwd = convert_forward_solution(fwd, surf_ori=True, force_fixed=True, use_cps=True) fwd = pick_types_forward(fwd, meg=True) assert isinstance(fwd, Forward) vertno = [fwd['src'][0]['vertno'], fwd['src'][1]['vertno']] stc_data = np.ones((len(vertno[0]) + len(vertno[1]), n_times)) stc = SourceEstimate(stc_data, vertno, tmin=t_start, tstep=1.0 / sfreq) gain_sum = np.sum(fwd['sol']['data'], axis=1) # Evoked evoked = read_evokeds(fname_evoked, condition=0) evoked.pick_types(meg=True) with pytest.warns(RuntimeWarning, match='only .* positive values'): evoked = apply_forward(fwd, stc, evoked.info, start=start, stop=stop) data = evoked.data times = evoked.times # do some tests assert_array_almost_equal(evoked.info['sfreq'], sfreq) assert_array_almost_equal(np.sum(data, axis=1), n_times * gain_sum) assert_array_almost_equal(times[0], t_start) assert_array_almost_equal(times[-1], t_start + (n_times - 1) / sfreq) # vector stc_vec = VectorSourceEstimate( fwd['source_nn'][:, :, np.newaxis] * stc.data[:, np.newaxis], stc.vertices, stc.tmin, stc.tstep) with pytest.warns(RuntimeWarning, match='very large'): evoked_2 = apply_forward(fwd, stc_vec, evoked.info) assert np.abs(evoked_2.data).mean() > 1e-5 assert_allclose(evoked.data, evoked_2.data, atol=1e-10) # Raw with pytest.warns(RuntimeWarning, match='only .* positive values'): raw_proj = apply_forward_raw(fwd, stc, evoked.info, start=start, stop=stop) data, times = raw_proj[:, :] # do some tests assert_array_almost_equal(raw_proj.info['sfreq'], sfreq) assert_array_almost_equal(np.sum(data, axis=1), n_times * gain_sum) atol = 1. / sfreq assert_allclose(raw_proj.first_samp / sfreq, t_start, atol=atol) assert_allclose(raw_proj.last_samp / sfreq, t_start + (n_times - 1) / sfreq, atol=atol)
def test_stc_methods(): """Test stc methods lh_data, rh_data, bin(), resample().""" stc_ = read_source_estimate(fname_stc) # Make a vector version of the above source estimate x = stc_.data[:, np.newaxis, :] yz = np.zeros((x.shape[0], 2, x.shape[2])) vec_stc_ = VectorSourceEstimate(np.concatenate((x, yz), 1), stc_.vertices, stc_.tmin, stc_.tstep, stc_.subject) for stc in [stc_, vec_stc_]: # lh_data / rh_data assert_array_equal(stc.lh_data, stc.data[:len(stc.lh_vertno)]) assert_array_equal(stc.rh_data, stc.data[len(stc.lh_vertno):]) # bin binned = stc.bin(.12) a = np.mean(stc.data[..., :np.searchsorted(stc.times, .12)], axis=-1) assert_array_equal(a, binned.data[..., 0]) stc = read_source_estimate(fname_stc) stc.subject = 'sample' label_lh = read_labels_from_annot('sample', 'aparc', 'lh', subjects_dir=subjects_dir)[0] label_rh = read_labels_from_annot('sample', 'aparc', 'rh', subjects_dir=subjects_dir)[0] label_both = label_lh + label_rh for label in (label_lh, label_rh, label_both): assert (isinstance(stc.shape, tuple) and len(stc.shape) == 2) stc_label = stc.in_label(label) if label.hemi != 'both': if label.hemi == 'lh': verts = stc_label.vertices[0] else: # label.hemi == 'rh': verts = stc_label.vertices[1] n_vertices_used = len(label.get_vertices_used(verts)) assert_equal(len(stc_label.data), n_vertices_used) stc_lh = stc.in_label(label_lh) pytest.raises(ValueError, stc_lh.in_label, label_rh) label_lh.subject = 'foo' pytest.raises(RuntimeError, stc.in_label, label_lh) stc_new = deepcopy(stc) o_sfreq = 1.0 / stc.tstep # note that using no padding for this STC reduces edge ringing... stc_new.resample(2 * o_sfreq, npad=0, n_jobs=2) assert (stc_new.data.shape[1] == 2 * stc.data.shape[1]) assert (stc_new.tstep == stc.tstep / 2) stc_new.resample(o_sfreq, npad=0) assert (stc_new.data.shape[1] == stc.data.shape[1]) assert (stc_new.tstep == stc.tstep) assert_array_almost_equal(stc_new.data, stc.data, 5)
def test_plot_vector_source_estimates(): """Test plotting of vector source estimates.""" sample_src = read_source_spaces(src_fname) vertices = [s['vertno'] for s in sample_src] n_verts = sum(len(v) for v in vertices) n_time = 5 data = np.random.RandomState(0).rand(n_verts, 3, n_time) stc = VectorSourceEstimate(data, vertices, 1, 1) brain = stc.plot('sample', subjects_dir=subjects_dir) brain.close() del brain gc.collect() with pytest.raises(ValueError, match='use "pos_lims"'): stc.plot('sample', subjects_dir=subjects_dir, clim=dict(pos_lims=[1, 2, 3])) gc.collect() brain = stc.plot('sample', subjects_dir=subjects_dir, hemi='both') brain.close() del brain gc.collect()
def test_expand(): """Test stc expansion.""" stc_ = read_source_estimate(fname_stc, 'sample') vec_stc_ = VectorSourceEstimate(np.zeros((stc_.data.shape[0], 3, stc_.data.shape[1])), stc_.vertices, stc_.tmin, stc_.tstep, stc_.subject) for stc in [stc_, vec_stc_]: assert ('sample' in repr(stc)) labels_lh = read_labels_from_annot('sample', 'aparc', 'lh', subjects_dir=subjects_dir) new_label = labels_lh[0] + labels_lh[1] stc_limited = stc.in_label(new_label) stc_new = stc_limited.copy() stc_new.data.fill(0) for label in labels_lh[:2]: stc_new += stc.in_label(label).expand(stc_limited.vertices) pytest.raises(TypeError, stc_new.expand, stc_limited.vertices[0]) pytest.raises(ValueError, stc_new.expand, [stc_limited.vertices[0]]) # make sure we can't add unless vertno agree pytest.raises(ValueError, stc.__add__, stc.in_label(labels_lh[0]))
def test_plot_vec_source_estimates(): """Test plotting of vector source estimates.""" sample_src = read_source_spaces(src_fname) vertices = [s['vertno'] for s in sample_src] n_verts = sum(len(v) for v in vertices) n_time = 5 data = np.random.RandomState(0).rand(n_verts, 3, n_time) stc = VectorSourceEstimate(data, vertices, 1, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') stc.plot('sample', subjects_dir=subjects_dir) assert len(w) == 0 # not using deprecated params with pytest.raises(ValueError, match='use "pos_lims"'): stc.plot('sample', subjects_dir=subjects_dir, clim=dict(pos_lims=[1, 2, 3])) assert len(w) == 0
def _fake_vec_stc(n_time=10): verts = [np.arange(10), np.arange(90)] return VectorSourceEstimate(np.random.rand(100, 3, n_time), verts, 0, 1e-1, 'foo')