def test_brain_add_label(renderer): """Test adding data in _Brain instance.""" from mne.label import read_label brain = _Brain(subject_id, hemi='lh', size=500, surf=surf, subjects_dir=subjects_dir) label = read_label(fname_label) brain.add_label(label, scalar_thresh=0.) brain.close() brain = _Brain(subject_id, hemi='split', size=500, surf=surf, subjects_dir=subjects_dir) brain.add_label(fname_label) brain.close()
def test_brain_init(renderer): """Test initialization of the _Brain instance.""" hemi = 'both' with pytest.raises(ValueError, match='size'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, size=0.5) with pytest.raises(TypeError, match='figure'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, figure='foo') with pytest.raises(ValueError, match='interaction'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, interaction=0) with pytest.raises(KeyError): _Brain(subject_id=subject_id, hemi='foo', surf=surf) _Brain(subject_id, hemi, surf, size=(300, 300), subjects_dir=subjects_dir)
def _create_testing_brain(hemi, surf='inflated'): sample_src = read_source_spaces(src_fname) # dense version vertices = [s['vertno'] for s in sample_src] n_time = 5 n_verts = sum(len(v) for v in vertices) stc_data = np.zeros((n_verts * n_time)) stc_size = stc_data.size stc_data[(np.random.rand(stc_size // 20) * stc_size).astype(int)] = \ np.random.RandomState(0).rand(stc_data.size // 20) stc_data.shape = (n_verts, n_time) stc = SourceEstimate(stc_data, vertices, 1, 1) fmin = stc.data.min() fmax = stc.data.max() brain_data = _Brain(subject_id, hemi, surf, size=300, subjects_dir=subjects_dir) hemi_list = ['lh', 'rh'] if hemi in ['both', 'split'] else [hemi] for hemi_str in hemi_list: hemi_idx = 0 if hemi_str == 'lh' else 1 data = getattr(stc, hemi_str + '_data') vertices = stc.vertices[hemi_idx] brain_data.add_data(data, fmin=fmin, hemi=hemi_str, fmax=fmax, colormap='hot', vertices=vertices, colorbar=True) return brain_data
def test_brain_screenshot(renderer): """Test screenshot of a _Brain instance.""" brain = _Brain(subject_id, hemi='both', size=600, surf=surf, subjects_dir=subjects_dir) img = brain.screenshot(mode='rgb') assert(img.shape == (600, 600, 3)) brain.close()
def test_brain_add_foci(renderer): """Test adding foci in _Brain instance.""" brain = _Brain(subject_id, hemi='lh', size=500, surf=surf, subjects_dir=subjects_dir) brain.add_foci([0], coords_as_verts=True, hemi='lh', color='blue') brain.close()
def test_brain_timeviewer(renderer): """Test _TimeViewer primitives.""" if renderer.get_3d_backend() == "mayavi": pytest.skip() # Skip PySurfer.TimeViewer else: # Disable testing to allow interactive window renderer.MNE_3D_BACKEND_TESTING = False sample_src = read_source_spaces(src_fname) # dense version vertices = [s['vertno'] for s in sample_src] n_time = 5 n_verts = sum(len(v) for v in vertices) stc_data = np.zeros((n_verts * n_time)) stc_size = stc_data.size stc_data[(np.random.rand(stc_size // 20) * stc_size).astype(int)] = \ np.random.RandomState(0).rand(stc_data.size // 20) stc_data.shape = (n_verts, n_time) stc = SourceEstimate(stc_data, vertices, 1, 1) fmin = stc.data.min() fmax = stc.data.max() brain_data = _Brain(subject_id, 'split', surf, size=300, subjects_dir=subjects_dir) for hemi in ['lh', 'rh']: hemi_idx = 0 if hemi == 'lh' else 1 data = getattr(stc, hemi + '_data') vertices = stc.vertices[hemi_idx] brain_data.add_data(data, fmin=fmin, hemi=hemi, fmax=fmax, colormap='hot', vertices=vertices, colorbar=True) time_viewer = _TimeViewer(brain_data) time_viewer.time_call(value=0) time_viewer.orientation_call(value='lat', update_widget=True) time_viewer.orientation_call(value='medial', update_widget=True) time_viewer.smoothing_call(value=1) time_viewer.fmin_call(value=12.0) time_viewer.fmax_call(value=4.0) time_viewer.fmid_call(value=6.0) time_viewer.fmid_call(value=4.0) time_viewer.fscale_call(value=1.1) time_viewer.toggle_interface() time_viewer.playback_speed_call(value=0.1) time_viewer.toggle_playback() time_viewer.apply_auto_scaling() time_viewer.restore_user_scaling() link_viewer = _LinkViewer([brain_data]) link_viewer.set_time_point(value=0) link_viewer.set_playback_speed(value=0.1) link_viewer.toggle_playback()
def test_brain_add_data(renderer): """Test adding data in _Brain instance.""" stc = read_source_estimate(fname_stc) hemi = 'lh' hemi_data = stc.data[:len(stc.vertices[0]), 10] hemi_vertices = stc.vertices[0] fmin = stc.data.min() fmax = stc.data.max() brain_data = _Brain(subject_id, hemi, surf, size=300, subjects_dir=subjects_dir) with pytest.raises(ValueError, match='thresh'): brain_data.add_data(hemi_data, thresh=-1) with pytest.raises(ValueError, match='remove_existing'): brain_data.add_data(hemi_data, remove_existing=-1) with pytest.raises(ValueError, match='time_label_size'): brain_data.add_data(hemi_data, time_label_size=-1) with pytest.raises(ValueError, match='scale_factor'): brain_data.add_data(hemi_data, scale_factor=-1) with pytest.raises(ValueError, match='vector_alpha'): brain_data.add_data(hemi_data, vector_alpha=-1) with pytest.raises(ValueError): brain_data.add_data(array=np.array([0, 1, 2])) with pytest.raises(ValueError): brain_data.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, vertices=None) brain_data.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, colormap='hot', vertices=hemi_vertices, colorbar=False, time=None) brain_data.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, colormap='hot', vertices=hemi_vertices, initial_time=0., colorbar=True, time=None)
def test_brain_add_text(renderer): """Test adding text in _Brain instance.""" brain = _Brain(subject_id, hemi='lh', size=250, surf=surf, subjects_dir=subjects_dir) brain.add_text(x=0, y=0, text='foo')
def test_brain_init(renderer): """Test initialization of the _Brain instance.""" hemi = 'both' with pytest.raises(ValueError, match='size'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, size=0.5) with pytest.raises(TypeError, match='figure'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, figure='foo') with pytest.raises(ValueError, match='interaction'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, interaction=0) with pytest.raises(KeyError): _Brain(subject_id=subject_id, hemi='foo', surf=surf) brain = _Brain(subject_id, hemi, surf, size=(300, 300), subjects_dir=subjects_dir) brain.show_view(view=dict(azimuth=180., elevation=90.)) brain.close()
def test_brain_init(renderer): """Test initialization of the _Brain instance.""" backend_name = renderer.get_3d_backend() hemi = 'both' with pytest.raises(ValueError, match='hemi'): _Brain(subject_id=subject_id, hemi="split", surf=surf) with pytest.raises(ValueError, match='figure'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, figure=0) with pytest.raises(ValueError, match='interaction'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, interaction=0) with pytest.raises(KeyError): _Brain(subject_id=subject_id, hemi="foo", surf=surf) brain = _Brain(subject_id, hemi, surf, subjects_dir=subjects_dir) if backend_name != 'mayavi': brain.show()
def test_brain_add_annotation(renderer): """Test adding data in _Brain instance.""" annots = ['aparc', 'PALS_B12_Lobes'] borders = [True, 2] alphas = [1, 0.5] brain = _Brain(subject_id='fsaverage', hemi='lh', size=500, surf='inflated', subjects_dir=subjects_dir) for a, b, p in zip(annots, borders, alphas): brain.add_annotation(a, b, p) brain.close()
def test_brain_init(renderer): """Test initialization of the _Brain instance.""" hemi = 'both' with pytest.raises(ValueError, match='"size" parameter must be'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, size=[1, 2, 3]) with pytest.raises(TypeError, match='figure'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, figure='foo') with pytest.raises(ValueError, match='interaction'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, interaction=0) with pytest.raises(KeyError): _Brain(subject_id=subject_id, hemi='foo', surf=surf) for cortex in ['classic', 'low_contrast', 'high_contrast', 'bone']: brain = _Brain(subject_id, hemi, surf, size=(300, 300), subjects_dir=subjects_dir, title='test', cortex=cortex) brain.show_view(view=dict(azimuth=180., elevation=90.)) brain.close()
def test_brain_add_data(renderer): """Test adding data in _Brain instance.""" backend_name = renderer.get_3d_backend() act_data = path.join(data_path, 'MEG/sample/sample_audvis-meg-eeg') stc = read_source_estimate(act_data) hemi = 'lh' hemi_data = stc.data[:len(stc.vertices[0]), 10] hemi_vertices = stc.vertices[0] fmin = stc.data.min() fmax = stc.data.max() brain_data = _Brain(subject_id, hemi, surf, size=300, subjects_dir=subjects_dir) with pytest.raises(ValueError): brain_data.add_data(array=np.array([0, 1, 2])) with pytest.raises(ValueError): brain_data.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, vertices=None) brain_data.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, colormap='hot', vertices=hemi_vertices, colorbar=False) if backend_name != 'mayavi': brain_data.show()
def test_brain(renderer): """Test initialization of the _Brain instance.""" from mne.label import read_label hemi = 'lh' surf = 'inflated' cortex = 'low_contrast' title = 'test' size = (300, 300) with pytest.raises(ValueError, match='"size" parameter must be'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, size=[1, 2, 3]) with pytest.raises(TypeError, match='figure'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, figure='foo') with pytest.raises(ValueError, match='interaction'): _Brain(subject_id=subject_id, hemi=hemi, surf=surf, interaction=0) with pytest.raises(KeyError): _Brain(subject_id=subject_id, hemi='foo', surf=surf) brain = _Brain(subject_id, hemi=hemi, surf=surf, size=size, subjects_dir=subjects_dir, title=title, cortex=cortex) # add_data stc = read_source_estimate(fname_stc) fmin = stc.data.min() fmax = stc.data.max() for h in brain._hemis: if h == 'lh': hi = 0 else: hi = 1 hemi_data = stc.data[:len(stc.vertices[hi]), 10] hemi_vertices = stc.vertices[hi] with pytest.raises(TypeError, match='scale_factor'): brain.add_data(hemi_data, hemi=h, scale_factor='foo') with pytest.raises(TypeError, match='vector_alpha'): brain.add_data(hemi_data, hemi=h, vector_alpha='foo') with pytest.raises(ValueError, match='thresh'): brain.add_data(hemi_data, hemi=h, thresh=-1) with pytest.raises(ValueError, match='remove_existing'): brain.add_data(hemi_data, hemi=h, remove_existing=-1) with pytest.raises(ValueError, match='time_label_size'): brain.add_data(hemi_data, hemi=h, time_label_size=-1) with pytest.raises(ValueError, match='is positive'): brain.add_data(hemi_data, hemi=h, smoothing_steps=-1) with pytest.raises(TypeError, match='int or NoneType'): brain.add_data(hemi_data, hemi=h, smoothing_steps='foo') with pytest.raises(ValueError): brain.add_data(array=np.array([0, 1, 2]), hemi=h) with pytest.raises(ValueError): brain.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, vertices=None) brain.add_data(hemi_data, fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices, smoothing_steps='nearest', colorbar=False, time=None) brain.add_data(hemi_data, fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices, smoothing_steps=1, initial_time=0., colorbar=False, time=None) # add label label = read_label(fname_label) brain.add_label(label, scalar_thresh=0.) brain.remove_labels() # add foci brain.add_foci([0], coords_as_verts=True, hemi=hemi, color='blue') # add text brain.add_text(x=0, y=0, text='foo') # screenshot brain.show_view(view=dict(azimuth=180., elevation=90.)) img = brain.screenshot(mode='rgb') assert_allclose(img.shape, (size[0], size[1], 3), atol=70) # XXX undo once size is fixed # add annotation annots = ['aparc', 'PALS_B12_Lobes'] borders = [True, 2] alphas = [1, 0.5] brain = _Brain(subject_id='fsaverage', hemi=hemi, size=size, surf='inflated', subjects_dir=subjects_dir) for a, b, p in zip(annots, borders, alphas): brain.add_annotation(a, b, p) brain.close()
def test_brain_init(renderer, tmpdir, pixel_ratio): """Test initialization of the _Brain instance.""" from mne.label import read_label hemi = 'lh' surf = 'inflated' cortex = 'low_contrast' title = 'test' size = (300, 300) kwargs = dict(subject_id=subject_id, subjects_dir=subjects_dir) with pytest.raises(ValueError, match='"size" parameter must be'): _Brain(hemi=hemi, surf=surf, size=[1, 2, 3], **kwargs) with pytest.raises(TypeError, match='figure'): _Brain(hemi=hemi, surf=surf, figure='foo', **kwargs) with pytest.raises(TypeError, match='interaction'): _Brain(hemi=hemi, surf=surf, interaction=0, **kwargs) with pytest.raises(ValueError, match='interaction'): _Brain(hemi=hemi, surf=surf, interaction='foo', **kwargs) with pytest.raises(KeyError): _Brain(hemi='foo', surf=surf, **kwargs) brain = _Brain(hemi=hemi, surf=surf, size=size, title=title, cortex=cortex, units='m', **kwargs) assert brain.interaction == 'trackball' # add_data stc = read_source_estimate(fname_stc) fmin = stc.data.min() fmax = stc.data.max() for h in brain._hemis: if h == 'lh': hi = 0 else: hi = 1 hemi_data = stc.data[:len(stc.vertices[hi]), 10] hemi_vertices = stc.vertices[hi] with pytest.raises(TypeError, match='scale_factor'): brain.add_data(hemi_data, hemi=h, scale_factor='foo') with pytest.raises(TypeError, match='vector_alpha'): brain.add_data(hemi_data, hemi=h, vector_alpha='foo') with pytest.raises(ValueError, match='thresh'): brain.add_data(hemi_data, hemi=h, thresh=-1) with pytest.raises(ValueError, match='remove_existing'): brain.add_data(hemi_data, hemi=h, remove_existing=-1) with pytest.raises(ValueError, match='time_label_size'): brain.add_data(hemi_data, hemi=h, time_label_size=-1, vertices=hemi_vertices) with pytest.raises(ValueError, match='is positive'): brain.add_data(hemi_data, hemi=h, smoothing_steps=-1, vertices=hemi_vertices) with pytest.raises(TypeError, match='int or NoneType'): brain.add_data(hemi_data, hemi=h, smoothing_steps='foo') with pytest.raises(ValueError, match='dimension mismatch'): brain.add_data(array=np.array([0, 1, 2]), hemi=h, vertices=hemi_vertices) with pytest.raises(ValueError, match='vertices parameter must not be'): brain.add_data(hemi_data, fmin=fmin, hemi=hemi, fmax=fmax, vertices=None) with pytest.raises(ValueError, match='has shape'): brain.add_data(hemi_data[:, np.newaxis], fmin=fmin, hemi=hemi, fmax=fmax, vertices=None, time=[0, 1]) brain.add_data(hemi_data, fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices, smoothing_steps='nearest', colorbar=(0, 0), time=None) assert brain.data['lh']['array'] is hemi_data assert brain.views == ['lateral'] assert brain.hemis == ('lh', ) brain.add_data(hemi_data[:, np.newaxis], fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices, smoothing_steps=1, initial_time=0., colorbar=False, time=[0]) brain.set_time_point(0) # should hit _safe_interp1d with pytest.raises(ValueError, match='consistent with'): brain.add_data(hemi_data[:, np.newaxis], fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices, smoothing_steps='nearest', colorbar=False, time=[1]) with pytest.raises(ValueError, match='different from'): brain.add_data(hemi_data[:, np.newaxis][:, [0, 0]], fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices) with pytest.raises(ValueError, match='need shape'): brain.add_data(hemi_data[:, np.newaxis], time=[0, 1], fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices) with pytest.raises(ValueError, match='If array has 3'): brain.add_data(hemi_data[:, np.newaxis, np.newaxis], fmin=fmin, hemi=h, fmax=fmax, colormap='hot', vertices=hemi_vertices) # add label label = read_label(fname_label) brain.add_label(label, scalar_thresh=0.) brain.remove_labels() brain.add_label(fname_label) brain.add_label('V1', borders=True) brain.remove_labels() brain.remove_labels() # add foci brain.add_foci([0], coords_as_verts=True, hemi=hemi, color='blue') # add text brain.add_text(x=0, y=0, text='foo') # add annotation annots = [ 'aparc', path.join(subjects_dir, 'fsaverage', 'label', 'lh.PALS_B12_Lobes.annot') ] borders = [True, 2] alphas = [1, 0.5] colors = [None, 'r'] brain = _Brain(subject_id='fsaverage', hemi=hemi, size=size, surf='inflated', subjects_dir=subjects_dir) for a, b, p, color in zip(annots, borders, alphas, colors): brain.add_annotation(a, b, p, color=color) brain.show_view(dict(focalpoint=(1e-5, 1e-5, 1e-5)), roll=1, distance=500) # image and screenshot fname = path.join(str(tmpdir), 'test.png') assert not path.isfile(fname) brain.save_image(fname) assert path.isfile(fname) brain.show_view(view=dict(azimuth=180., elevation=90.)) img = brain.screenshot(mode='rgb') if renderer._get_3d_backend() == 'mayavi': pixel_ratio = 1. # no HiDPI when using the testing backend want_size = np.array([size[0] * pixel_ratio, size[1] * pixel_ratio, 3]) assert_allclose(img.shape, want_size) brain.close()