示例#1
0
def test_3d_functions(renderer):
    """Test figure management functions."""
    fig = renderer.create_3d_figure((300, 300))
    # Mayavi actually needs something in the display to set the title
    wrap_renderer = renderer._Renderer(fig=fig)
    wrap_renderer.sphere(np.array([0., 0., 0.]), 'w', 1.)
    renderer._check_3d_figure(fig)
    renderer._set_3d_view(figure=fig, azimuth=None, elevation=None,
                          focalpoint=(0., 0., 0.), distance=None)
    renderer._set_3d_title(figure=fig, title='foo')
    renderer._take_3d_screenshot(figure=fig)
    renderer._close_all()
def test_3d_backend(renderer):
    """Test default plot."""
    # set data
    win_size = (600, 600)
    win_color = 'black'

    tet_size = 1.0
    tet_x = np.array([0, tet_size, 0, 0])
    tet_y = np.array([0, 0, tet_size, 0])
    tet_z = np.array([0, 0, 0, tet_size])
    tet_indices = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
    tet_color = 'white'

    sph_center = np.column_stack((tet_x, tet_y, tet_z))
    sph_color = 'red'
    sph_scale = tet_size / 3.0

    ct_scalars = np.array([0.0, 0.0, 0.0, 1.0])
    ct_levels = [0.2, 0.4, 0.6, 0.8]
    ct_surface = {"rr": sph_center, "tris": tet_indices}

    qv_mode = "arrow"
    qv_color = 'blue'
    qv_scale = tet_size / 2.0
    qv_center = np.array([
        np.mean((sph_center[va, :], sph_center[vb, :], sph_center[vc, :]),
                axis=0) for (va, vb, vc) in tet_indices
    ])
    center = np.mean(qv_center, axis=0)
    qv_dir = qv_center - center
    qv_scale_mode = 'scalar'
    qv_scalars = np.linspace(1.0, 2.0, 4)

    txt_x = 0.0
    txt_y = 0.0
    txt_text = "renderer"
    txt_size = 14

    cam_distance = 5 * tet_size

    # init scene
    rend = renderer._Renderer(size=win_size, bgcolor=win_color)
    rend.set_interactive()

    # use mesh
    rend.mesh(x=tet_x,
              y=tet_y,
              z=tet_z,
              triangles=tet_indices,
              color=tet_color)

    # use contour
    rend.contour(surface=ct_surface,
                 scalars=ct_scalars,
                 contours=ct_levels,
                 kind='line')
    rend.contour(surface=ct_surface,
                 scalars=ct_scalars,
                 contours=ct_levels,
                 kind='tube')

    # use sphere
    rend.sphere(center=sph_center,
                color=sph_color,
                scale=sph_scale,
                radius=1.0)

    # use quiver3d
    rend.quiver3d(x=qv_center[:, 0],
                  y=qv_center[:, 1],
                  z=qv_center[:, 2],
                  u=qv_dir[:, 0],
                  v=qv_dir[:, 1],
                  w=qv_dir[:, 2],
                  color=qv_color,
                  scale=qv_scale,
                  scale_mode=qv_scale_mode,
                  scalars=qv_scalars,
                  mode=qv_mode)

    # use tube
    rend.tube(origin=np.array([[0, 0, 0]]), destination=np.array([[0, 1, 0]]))
    tube = rend.tube(origin=np.array([[1, 0, 0]]),
                     destination=np.array([[1, 1, 0]]),
                     scalars=np.array([[1.0, 1.0]]))

    # scalar bar
    rend.scalarbar(source=tube, title="Scalar Bar")

    # use text
    rend.text2d(x_window=txt_x,
                y_window=txt_y,
                text=txt_text,
                size=txt_size,
                justification='right')
    rend.text3d(x=0, y=0, z=0, text=txt_text, scale=1.0)
    rend.set_camera(azimuth=180.0,
                    elevation=90.0,
                    distance=cam_distance,
                    focalpoint=center)
    rend.show()
示例#3
0
def test_plot_alignment(tmpdir, backends_3d):
    """Test plotting of -trans.fif files and MEG sensor layouts."""
    from mne.viz.backends.renderer import _Renderer
    backend_name = get_3d_backend()
    # generate fiducials file for testing
    tempdir = str(tmpdir)
    fiducials_path = op.join(tempdir, 'fiducials.fif')
    fid = [{'coord_frame': 5, 'ident': 1, 'kind': 1,
            'r': [-0.08061612, -0.02908875, -0.04131077]},
           {'coord_frame': 5, 'ident': 2, 'kind': 1,
            'r': [0.00146763, 0.08506715, -0.03483611]},
           {'coord_frame': 5, 'ident': 3, 'kind': 1,
            'r': [0.08436285, -0.02850276, -0.04127743]}]
    write_dig(fiducials_path, fid, 5)

    if backend_name == 'mayavi':
        mlab = _import_mlab()
    evoked = read_evokeds(evoked_fname)[0]
    sample_src = read_source_spaces(src_fname)
    bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True,
                       preload=False).info
    infos = dict(
        Neuromag=evoked.info,
        CTF=read_raw_ctf(ctf_fname).info,
        BTi=bti,
        KIT=read_raw_kit(sqd_fname).info,
    )
    for system, info in infos.items():
        meg = ['helmet', 'sensors']
        if system == 'KIT':
            meg.append('ref')
        fig = plot_alignment(info, trans_fname, subject='sample',
                             subjects_dir=subjects_dir, meg=meg)
        renderer = _Renderer(fig=fig)
        renderer.close()
    # KIT ref sensor coil def is defined
    if backend_name == 'mayavi':
        mlab.close(all=True)
    info = infos['Neuromag']
    pytest.raises(TypeError, plot_alignment, 'foo', trans_fname,
                  subject='sample', subjects_dir=subjects_dir)
    pytest.raises(OSError, plot_alignment, info, trans_fname,
                  subject='sample', subjects_dir=subjects_dir, src='foo')
    pytest.raises(ValueError, plot_alignment, info, trans_fname,
                  subject='fsaverage', subjects_dir=subjects_dir,
                  src=sample_src)
    sample_src.plot(subjects_dir=subjects_dir, head=True, skull=True,
                    brain='white')
    if backend_name == 'mayavi':
        mlab.close(all=True)
    # no-head version
    if backend_name == 'mayavi':
        mlab.close(all=True)
    # all coord frames
    pytest.raises(ValueError, plot_alignment, info)
    plot_alignment(info, surfaces=[])
    for coord_frame in ('meg', 'head', 'mri'):
        fig = plot_alignment(info, meg=['helmet', 'sensors'], dig=True,
                             coord_frame=coord_frame, trans=trans_fname,
                             subject='sample', mri_fiducials=fiducials_path,
                             subjects_dir=subjects_dir, src=src_fname)
        renderer = _Renderer(fig=fig)
        renderer.close()
    # EEG only with strange options
    evoked_eeg_ecog_seeg = evoked.copy().pick_types(meg=False, eeg=True)
    evoked_eeg_ecog_seeg.info['projs'] = []  # "remove" avg proj
    evoked_eeg_ecog_seeg.set_channel_types({'EEG 001': 'ecog',
                                            'EEG 002': 'seeg'})
    with pytest.warns(RuntimeWarning, match='Cannot plot MEG'):
        plot_alignment(evoked_eeg_ecog_seeg.info, subject='sample',
                       trans=trans_fname, subjects_dir=subjects_dir,
                       surfaces=['white', 'outer_skin', 'outer_skull'],
                       meg=['helmet', 'sensors'],
                       eeg=['original', 'projected'], ecog=True, seeg=True)
    if backend_name == 'mayavi':
        mlab.close(all=True)

    sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto')
    bem_sol = read_bem_solution(op.join(subjects_dir, 'sample', 'bem',
                                        'sample-1280-1280-1280-bem-sol.fif'))
    bem_surfs = read_bem_surfaces(op.join(subjects_dir, 'sample', 'bem',
                                          'sample-1280-1280-1280-bem.fif'))
    sample_src[0]['coord_frame'] = 4  # hack for coverage
    plot_alignment(info, subject='sample', eeg='projected',
                   meg='helmet', bem=sphere, dig=True,
                   surfaces=['brain', 'inner_skull', 'outer_skull',
                             'outer_skin'])
    plot_alignment(info, trans_fname, subject='sample', meg='helmet',
                   subjects_dir=subjects_dir, eeg='projected', bem=sphere,
                   surfaces=['head', 'brain'], src=sample_src)
    assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI
               for surf in bem_sol['surfs'])
    plot_alignment(info, trans_fname, subject='sample', meg=[],
                   subjects_dir=subjects_dir, bem=bem_sol, eeg=True,
                   surfaces=['head', 'inflated', 'outer_skull', 'inner_skull'])
    assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI
               for surf in bem_sol['surfs'])
    plot_alignment(info, trans_fname, subject='sample',
                   meg=True, subjects_dir=subjects_dir,
                   surfaces=['head', 'inner_skull'], bem=bem_surfs)
    sphere = make_sphere_model('auto', 'auto', evoked.info)
    src = setup_volume_source_space(sphere=sphere)
    plot_alignment(info, eeg='projected', meg='helmet', bem=sphere,
                   src=src, dig=True, surfaces=['brain', 'inner_skull',
                                                'outer_skull', 'outer_skin'])
    sphere = make_sphere_model('auto', None, evoked.info)  # one layer
    # no info is permitted
    fig = plot_alignment(trans=trans_fname, subject='sample', meg=False,
                         coord_frame='mri', subjects_dir=subjects_dir,
                         surfaces=['brain'], bem=sphere, show_axes=True)
    if backend_name == 'mayavi':
        import mayavi  # noqa: F401 analysis:ignore
        assert isinstance(fig, mayavi.core.scene.Scene)

    # 3D coil with no defined draw (ConvexHull)
    info_cube = pick_info(info, [0])
    info['dig'] = None
    info_cube['chs'][0]['coil_type'] = 9999
    with pytest.raises(RuntimeError, match='coil definition not found'):
        plot_alignment(info_cube, meg='sensors', surfaces=())
    coil_def_fname = op.join(tempdir, 'temp')
    with open(coil_def_fname, 'w') as fid:
        fid.write(coil_3d)
    with use_coil_def(coil_def_fname):
        plot_alignment(info_cube, meg='sensors', surfaces=(), dig=True)

    # one layer bem with skull surfaces:
    pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir,
                  surfaces=['brain', 'head', 'inner_skull'], bem=sphere)
    # wrong eeg value:
    pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir, eeg='foo')
    # wrong meg value:
    pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir, meg='bar')
    # multiple brain surfaces:
    pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir,
                  surfaces=['white', 'pial'])
    pytest.raises(TypeError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir,
                  surfaces=[1])
    pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname,
                  subject='sample', subjects_dir=subjects_dir,
                  surfaces=['foo'])
    if backend_name == 'mayavi':
        mlab.close(all=True)
示例#4
0
def test_3d_backend(backends_3d):
    """Test default plot."""
    from mne.viz.backends.renderer import _Renderer

    # set data
    win_size = (600, 600)
    win_color = (0, 0, 0)

    tet_size = 1.0
    tet_x = np.array([0, tet_size, 0, 0])
    tet_y = np.array([0, 0, tet_size, 0])
    tet_z = np.array([0, 0, 0, tet_size])
    tet_indices = np.array([[0, 1, 2],
                            [0, 1, 3],
                            [0, 2, 3],
                            [1, 2, 3]])
    tet_color = (1, 1, 1)

    sph_center = np.column_stack((tet_x, tet_y, tet_z))
    sph_color = (1, 0, 0)
    sph_scale = tet_size / 3.0

    ct_scalars = np.array([0.0, 0.0, 0.0, 1.0])
    ct_levels = [0.2, 0.4, 0.6, 0.8]
    ct_surface = {
        "rr": sph_center,
        "tris": tet_indices
    }

    qv_mode = "arrow"
    qv_color = (0, 0, 1)
    qv_scale = tet_size / 2.0
    qv_center = np.array([np.mean((sph_center[va, :],
                                   sph_center[vb, :],
                                   sph_center[vc, :]), axis=0)
                         for (va, vb, vc) in tet_indices])
    center = np.mean(qv_center, axis=0)
    qv_dir = qv_center - center
    qv_scale_mode = 'scalar'
    qv_scalars = np.linspace(1.0, 2.0, 4)

    txt_x = 0.0
    txt_y = 0.0
    txt_text = "renderer"
    txt_width = 1.0

    cam_distance = 5 * tet_size

    # init scene
    renderer = _Renderer(size=win_size, bgcolor=win_color)
    renderer.set_interactive()

    # use mesh
    renderer.mesh(x=tet_x, y=tet_y, z=tet_z,
                  triangles=tet_indices,
                  color=tet_color)

    # use contour
    renderer.contour(surface=ct_surface, scalars=ct_scalars,
                     contours=ct_levels)

    # use sphere
    renderer.sphere(center=sph_center, color=sph_color,
                    scale=sph_scale)

    # use quiver3d
    renderer.quiver3d(x=qv_center[:, 0],
                      y=qv_center[:, 1],
                      z=qv_center[:, 2],
                      u=qv_dir[:, 0],
                      v=qv_dir[:, 1],
                      w=qv_dir[:, 2],
                      color=qv_color,
                      scale=qv_scale,
                      scale_mode=qv_scale_mode,
                      scalars=qv_scalars,
                      mode=qv_mode)

    # use text
    renderer.text(x=txt_x, y=txt_y, text=txt_text, width=txt_width)
    renderer.set_camera(azimuth=180.0, elevation=90.0,
                        distance=cam_distance,
                        focalpoint=center)
    renderer.show()
示例#5
0
def test_plot_alignment(tmpdir, backends_3d):
    """Test plotting of -trans.fif files and MEG sensor layouts."""
    from mne.viz.backends.renderer import _Renderer
    backend_name = get_3d_backend()
    # generate fiducials file for testing
    tempdir = str(tmpdir)
    fiducials_path = op.join(tempdir, 'fiducials.fif')
    fid = [{
        'coord_frame': 5,
        'ident': 1,
        'kind': 1,
        'r': [-0.08061612, -0.02908875, -0.04131077]
    }, {
        'coord_frame': 5,
        'ident': 2,
        'kind': 1,
        'r': [0.00146763, 0.08506715, -0.03483611]
    }, {
        'coord_frame': 5,
        'ident': 3,
        'kind': 1,
        'r': [0.08436285, -0.02850276, -0.04127743]
    }]
    write_dig(fiducials_path, fid, 5)

    if backend_name == 'mayavi':
        mlab = _import_mlab()
    evoked = read_evokeds(evoked_fname)[0]
    sample_src = read_source_spaces(src_fname)
    bti = read_raw_bti(pdf_fname,
                       config_fname,
                       hs_fname,
                       convert=True,
                       preload=False).info
    infos = dict(
        Neuromag=evoked.info,
        CTF=read_raw_ctf(ctf_fname).info,
        BTi=bti,
        KIT=read_raw_kit(sqd_fname).info,
    )
    for system, info in infos.items():
        meg = ['helmet', 'sensors']
        if system == 'KIT':
            meg.append('ref')
        fig = plot_alignment(info,
                             trans_fname,
                             subject='sample',
                             subjects_dir=subjects_dir,
                             meg=meg)
        renderer = _Renderer(fig=fig)
        renderer.close()
    # KIT ref sensor coil def is defined
    if backend_name == 'mayavi':
        mlab.close(all=True)
    info = infos['Neuromag']
    pytest.raises(TypeError,
                  plot_alignment,
                  'foo',
                  trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir)
    pytest.raises(OSError,
                  plot_alignment,
                  info,
                  trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  src='foo')
    pytest.raises(ValueError,
                  plot_alignment,
                  info,
                  trans_fname,
                  subject='fsaverage',
                  subjects_dir=subjects_dir,
                  src=sample_src)
    sample_src.plot(subjects_dir=subjects_dir,
                    head=True,
                    skull=True,
                    brain='white')
    if backend_name == 'mayavi':
        mlab.close(all=True)
    # no-head version
    if backend_name == 'mayavi':
        mlab.close(all=True)
    # all coord frames
    pytest.raises(ValueError, plot_alignment, info)
    plot_alignment(info, surfaces=[])
    for coord_frame in ('meg', 'head', 'mri'):
        fig = plot_alignment(info,
                             meg=['helmet', 'sensors'],
                             dig=True,
                             coord_frame=coord_frame,
                             trans=trans_fname,
                             subject='sample',
                             mri_fiducials=fiducials_path,
                             subjects_dir=subjects_dir,
                             src=src_fname)
        renderer = _Renderer(fig=fig)
        renderer.close()
    # EEG only with strange options
    evoked_eeg_ecog_seeg = evoked.copy().pick_types(meg=False, eeg=True)
    evoked_eeg_ecog_seeg.info['projs'] = []  # "remove" avg proj
    evoked_eeg_ecog_seeg.set_channel_types({
        'EEG 001': 'ecog',
        'EEG 002': 'seeg'
    })
    with pytest.warns(RuntimeWarning, match='Cannot plot MEG'):
        plot_alignment(evoked_eeg_ecog_seeg.info,
                       subject='sample',
                       trans=trans_fname,
                       subjects_dir=subjects_dir,
                       surfaces=['white', 'outer_skin', 'outer_skull'],
                       meg=['helmet', 'sensors'],
                       eeg=['original', 'projected'],
                       ecog=True,
                       seeg=True)
    if backend_name == 'mayavi':
        mlab.close(all=True)

    sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto')
    bem_sol = read_bem_solution(
        op.join(subjects_dir, 'sample', 'bem',
                'sample-1280-1280-1280-bem-sol.fif'))
    bem_surfs = read_bem_surfaces(
        op.join(subjects_dir, 'sample', 'bem',
                'sample-1280-1280-1280-bem.fif'))
    sample_src[0]['coord_frame'] = 4  # hack for coverage
    plot_alignment(
        info,
        subject='sample',
        eeg='projected',
        meg='helmet',
        bem=sphere,
        dig=True,
        surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin'])
    plot_alignment(info,
                   trans_fname,
                   subject='sample',
                   meg='helmet',
                   subjects_dir=subjects_dir,
                   eeg='projected',
                   bem=sphere,
                   surfaces=['head', 'brain'],
                   src=sample_src)
    assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI
               for surf in bem_sol['surfs'])
    plot_alignment(info,
                   trans_fname,
                   subject='sample',
                   meg=[],
                   subjects_dir=subjects_dir,
                   bem=bem_sol,
                   eeg=True,
                   surfaces=['head', 'inflated', 'outer_skull', 'inner_skull'])
    assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI
               for surf in bem_sol['surfs'])
    plot_alignment(info,
                   trans_fname,
                   subject='sample',
                   meg=True,
                   subjects_dir=subjects_dir,
                   surfaces=['head', 'inner_skull'],
                   bem=bem_surfs)
    sphere = make_sphere_model('auto', 'auto', evoked.info)
    src = setup_volume_source_space(sphere=sphere)
    plot_alignment(
        info,
        eeg='projected',
        meg='helmet',
        bem=sphere,
        src=src,
        dig=True,
        surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin'])
    sphere = make_sphere_model('auto', None, evoked.info)  # one layer
    # no info is permitted
    fig = plot_alignment(trans=trans_fname,
                         subject='sample',
                         meg=False,
                         coord_frame='mri',
                         subjects_dir=subjects_dir,
                         surfaces=['brain'],
                         bem=sphere,
                         show_axes=True)
    if backend_name == 'mayavi':
        import mayavi  # noqa: F401 analysis:ignore
        assert isinstance(fig, mayavi.core.scene.Scene)

    # 3D coil with no defined draw (ConvexHull)
    info_cube = pick_info(info, [0])
    info['dig'] = None
    info_cube['chs'][0]['coil_type'] = 9999
    with pytest.raises(RuntimeError, match='coil definition not found'):
        plot_alignment(info_cube, meg='sensors', surfaces=())
    coil_def_fname = op.join(tempdir, 'temp')
    with open(coil_def_fname, 'w') as fid:
        fid.write(coil_3d)
    with use_coil_def(coil_def_fname):
        plot_alignment(info_cube, meg='sensors', surfaces=(), dig=True)

    # one layer bem with skull surfaces:
    pytest.raises(ValueError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  surfaces=['brain', 'head', 'inner_skull'],
                  bem=sphere)
    # wrong eeg value:
    pytest.raises(ValueError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  eeg='foo')
    # wrong meg value:
    pytest.raises(ValueError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  meg='bar')
    # multiple brain surfaces:
    pytest.raises(ValueError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  surfaces=['white', 'pial'])
    pytest.raises(TypeError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  surfaces=[1])
    pytest.raises(ValueError,
                  plot_alignment,
                  info=info,
                  trans=trans_fname,
                  subject='sample',
                  subjects_dir=subjects_dir,
                  surfaces=['foo'])
    if backend_name == 'mayavi':
        mlab.close(all=True)
示例#6
0
def test_3d_backend(renderer):
    """Test default plot."""
    # set data
    win_size = (600, 600)
    win_color = (0, 0, 0)

    tet_size = 1.0
    tet_x = np.array([0, tet_size, 0, 0])
    tet_y = np.array([0, 0, tet_size, 0])
    tet_z = np.array([0, 0, 0, tet_size])
    tet_indices = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
    tet_color = (1, 1, 1)

    sph_center = np.column_stack((tet_x, tet_y, tet_z))
    sph_color = (1, 0, 0)
    sph_scale = tet_size / 3.0

    ct_scalars = np.array([0.0, 0.0, 0.0, 1.0])
    ct_levels = [0.2, 0.4, 0.6, 0.8]
    ct_surface = {"rr": sph_center, "tris": tet_indices}

    qv_mode = "arrow"
    qv_color = (0, 0, 1)
    qv_scale = tet_size / 2.0
    qv_center = np.array([
        np.mean((sph_center[va, :], sph_center[vb, :], sph_center[vc, :]),
                axis=0) for (va, vb, vc) in tet_indices
    ])
    center = np.mean(qv_center, axis=0)
    qv_dir = qv_center - center
    qv_scale_mode = 'scalar'
    qv_scalars = np.linspace(1.0, 2.0, 4)

    txt_x = 0.0
    txt_y = 0.0
    txt_text = "renderer"
    txt_width = 1.0

    cam_distance = 5 * tet_size

    # init scene
    rend = renderer._Renderer(size=win_size, bgcolor=win_color)
    rend.set_interactive()

    # use mesh
    rend.mesh(x=tet_x,
              y=tet_y,
              z=tet_z,
              triangles=tet_indices,
              color=tet_color)

    # use contour
    rend.contour(surface=ct_surface, scalars=ct_scalars, contours=ct_levels)

    # use sphere
    rend.sphere(center=sph_center, color=sph_color, scale=sph_scale)

    # use quiver3d
    rend.quiver3d(x=qv_center[:, 0],
                  y=qv_center[:, 1],
                  z=qv_center[:, 2],
                  u=qv_dir[:, 0],
                  v=qv_dir[:, 1],
                  w=qv_dir[:, 2],
                  color=qv_color,
                  scale=qv_scale,
                  scale_mode=qv_scale_mode,
                  scalars=qv_scalars,
                  mode=qv_mode)

    # use tube
    rend.tube(origin=np.array([[0, 0, 0]]), destination=np.array([[0, 1, 0]]))
    rend.tube(origin=np.array([[1, 0, 0]]),
              destination=np.array([[1, 1, 0]]),
              scalars=np.array([[1.0, 1.0]]))

    # use text
    rend.text2d(x=txt_x, y=txt_y, text=txt_text, width=txt_width)
    if renderer.get_3d_backend() == "pyvista":
        with pytest.raises(NotImplementedError):
            rend.text3d(x=0, y=0, z=0, text=txt_text, scale=1.0)
    else:
        rend.text3d(x=0, y=0, z=0, text=txt_text, scale=1.0)
    rend.set_camera(azimuth=180.0,
                    elevation=90.0,
                    distance=cam_distance,
                    focalpoint=center)
    rend.show()
示例#7
0
def test_3d_backend():
    """Test 3d backend degenerate scenarios and default plot."""
    pytest.raises(ValueError, set_3d_backend, "unknown_backend")
    pytest.raises(TypeError, set_3d_backend, 1)

    assert get_3d_backend() == "mayavi"

    # smoke test
    set_3d_backend('mayavi')
    set_3d_backend('mayavi')

    # set data
    win_size = (600, 600)
    win_color = (0, 0, 0)

    tet_size = 1.0
    tet_x = np.array([0, tet_size, 0, 0])
    tet_y = np.array([0, 0, tet_size, 0])
    tet_z = np.array([0, 0, 0, tet_size])
    tet_indices = np.array([[0, 1, 2],
                            [0, 1, 3],
                            [0, 2, 3],
                            [1, 2, 3]])
    tet_color = (1, 1, 1)

    sph_center = np.column_stack((tet_x, tet_y, tet_z))
    sph_color = (1, 0, 0)
    sph_scale = tet_size / 3.0

    qv_mode = "arrow"
    qv_color = (0, 0, 1)
    qv_scale = tet_size / 2.0
    qv_center = np.array([np.mean((sph_center[va, :],
                                   sph_center[vb, :],
                                   sph_center[vc, :]), axis=0)
                         for (va, vb, vc) in tet_indices])
    center = np.mean(qv_center, axis=0)
    qv_dir = qv_center - center

    txt_x = 0.0
    txt_y = 0.0
    txt_text = "renderer"
    txt_width = 1.0

    cam_distance = 5 * tet_size

    # init scene
    renderer = _Renderer(size=win_size, bgcolor=win_color)
    renderer.set_interactive()

    # use mesh
    renderer.mesh(x=tet_x, y=tet_y, z=tet_z,
                  triangles=tet_indices,
                  color=tet_color)

    # use sphere
    renderer.sphere(center=sph_center, color=sph_color,
                    scale=sph_scale)

    # use quiver3d
    renderer.quiver3d(x=qv_center[:, 0],
                      y=qv_center[:, 1],
                      z=qv_center[:, 2],
                      u=qv_dir[:, 0],
                      v=qv_dir[:, 1],
                      w=qv_dir[:, 2],
                      color=qv_color,
                      scale=qv_scale,
                      mode=qv_mode)

    # use text
    renderer.text(x=txt_x, y=txt_y, text=txt_text, width=txt_width)
    renderer.set_camera(azimuth=180.0, elevation=90.0, distance=cam_distance,
                        focalpoint=center)
    renderer.show()
示例#8
0
def test_3d_backend(backends_3d):
    """Test default plot."""
    from mne.viz.backends.renderer import _Renderer

    # set data
    win_size = (600, 600)
    win_color = (0, 0, 0)

    tet_size = 1.0
    tet_x = np.array([0, tet_size, 0, 0])
    tet_y = np.array([0, 0, tet_size, 0])
    tet_z = np.array([0, 0, 0, tet_size])
    tet_indices = np.array([[0, 1, 2],
                            [0, 1, 3],
                            [0, 2, 3],
                            [1, 2, 3]])
    tet_color = (1, 1, 1)

    sph_center = np.column_stack((tet_x, tet_y, tet_z))
    sph_color = (1, 0, 0)
    sph_scale = tet_size / 3.0

    ct_scalars = np.array([0.0, 0.0, 0.0, 1.0])
    ct_levels = [0.2, 0.4, 0.6, 0.8]
    ct_surface = {
        "rr": sph_center,
        "tris": tet_indices
    }

    qv_mode = "arrow"
    qv_color = (0, 0, 1)
    qv_scale = tet_size / 2.0
    qv_center = np.array([np.mean((sph_center[va, :],
                                   sph_center[vb, :],
                                   sph_center[vc, :]), axis=0)
                         for (va, vb, vc) in tet_indices])
    center = np.mean(qv_center, axis=0)
    qv_dir = qv_center - center
    qv_scale_mode = 'scalar'
    qv_scalars = np.linspace(1.0, 2.0, 4)

    txt_x = 0.0
    txt_y = 0.0
    txt_text = "renderer"
    txt_width = 1.0

    cam_distance = 5 * tet_size

    # init scene
    renderer = _Renderer(size=win_size, bgcolor=win_color)
    renderer.set_interactive()

    # use mesh
    renderer.mesh(x=tet_x, y=tet_y, z=tet_z,
                  triangles=tet_indices,
                  color=tet_color)

    # use contour
    renderer.contour(surface=ct_surface, scalars=ct_scalars,
                     contours=ct_levels)

    # use sphere
    renderer.sphere(center=sph_center, color=sph_color,
                    scale=sph_scale)

    # use quiver3d
    renderer.quiver3d(x=qv_center[:, 0],
                      y=qv_center[:, 1],
                      z=qv_center[:, 2],
                      u=qv_dir[:, 0],
                      v=qv_dir[:, 1],
                      w=qv_dir[:, 2],
                      color=qv_color,
                      scale=qv_scale,
                      scale_mode=qv_scale_mode,
                      scalars=qv_scalars,
                      mode=qv_mode)

    # use text
    renderer.text(x=txt_x, y=txt_y, text=txt_text, width=txt_width)
    renderer.set_camera(azimuth=180.0, elevation=90.0,
                        distance=cam_distance,
                        focalpoint=center)
    renderer.show()