def test_save_figures(): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise nose.SkipTest('Mayavi not installed') mlab.options.offscreen = True examples_dir = tempfile.mkdtemp() gallery_conf = {'find_mayavi_figures': True} mlab.test_plot3d() plt.plot(1, 1) fname_template = os.path.join(examples_dir, 'image{0}.png') fig_list = sg.save_figures(fname_template, 0, gallery_conf) assert_equal(len(fig_list), 2) assert fig_list[0].endswith('image1.png') assert fig_list[1].endswith('image2.png') mlab.test_plot3d() plt.plot(1, 1) fig_list = sg.save_figures(fname_template, 2, gallery_conf) assert_equal(len(fig_list), 2) assert fig_list[0].endswith('image3.png') assert fig_list[1].endswith('image4.png') shutil.rmtree(examples_dir)
def test_save_figures(gallery_conf): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise pytest.skip('Mayavi not installed') mlab.options.offscreen = True gallery_conf.update(find_mayavi_figures=True) mlab.test_plot3d() plt.plot(1, 1) fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png') image_rst, fig_num = sg.save_figures(fname_template, 0, gallery_conf) assert fig_num == 2 assert '/image1.png' in image_rst assert '/image2.png' in image_rst mlab.test_plot3d() plt.plot(1, 1) image_rst, fig_num = sg.save_figures(fname_template, 2, gallery_conf) assert fig_num == 2 assert '/image2.png' not in image_rst assert '/image3.png' in image_rst assert '/image4.png' in image_rst shutil.rmtree(gallery_conf['gallery_dir'])
def test_save_figures(): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise nose.SkipTest('Mayavi not installed') mlab.options.offscreen = True examples_dir = tempfile.mkdtemp() gallery_conf = {'find_mayavi_figures': True} mlab.test_plot3d() plt.plot(1, 1) fname_template = os.path.join(examples_dir, 'image{0}.png') fig_list, _ = sg.save_figures(fname_template, 0, gallery_conf) assert_equal(len(fig_list), 2) assert fig_list[0].endswith('image1.png') assert fig_list[1].endswith('image2.png') mlab.test_plot3d() plt.plot(1, 1) fig_list, _ = sg.save_figures(fname_template, 2, gallery_conf) assert_equal(len(fig_list), 2) assert fig_list[0].endswith('image3.png') assert fig_list[1].endswith('image4.png') shutil.rmtree(examples_dir)
def mayavi_plt(): ml.test_plot3d() arr = ml.screenshot() ml.close() plt.imshow(arr) plt.axis('off') plt.show()
def test_save_mayavi_figures(gallery_conf, req_mpl, req_pil): """Test file naming when saving figures. Requires mayavi.""" Image = _get_image() try: from mayavi import mlab except ImportError: raise pytest.skip('Mayavi not installed') import matplotlib.pyplot as plt mlab.options.offscreen = True gallery_conf.update( image_scrapers=(matplotlib_scraper, mayavi_scraper)) fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png') image_path_iterator = ImagePathIterator(fname_template) block = ('',) * 3 block_vars = dict(image_path_iterator=image_path_iterator) plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Greens') mlab.test_plot3d() image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 2 assert '/image0.png' not in image_rst assert '/image1.png' in image_rst assert '/image2.png' in image_rst assert '/image3.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) assert os.path.isfile(fname_template.format(1)) assert os.path.isfile(fname_template.format(2)) assert not os.path.isfile(fname_template.format(0)) with Image.open(fname_template.format(1)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [247, 252, 245]).all() # plt first # Test next-value handling, plus image_scrapers modification gallery_conf.update(image_scrapers=(matplotlib_scraper,)) mlab.test_plot3d() plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Reds') image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 3 assert '/image1.png' not in image_rst assert '/image2.png' not in image_rst assert '/image3.png' in image_rst assert '/image4.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) for ii in range(3): assert os.path.isfile(fname_template.format(ii + 1)) assert not os.path.isfile(fname_template.format(4)) with Image.open(fname_template.format(3)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [255, 245, 240]).all()
def test_axes(self): s = mlab.test_plot3d() a = mlab.axes(s) assert_allclose( a.axes.ranges, [-1.5, 1.5, -1.5, 1.5, -0.5, 0.5], rtol=0, atol=0.1 )
def generate_spinning_brain_frames(trajectories, nframes_per_cycle=30, figsize=[800, 700]): ''' Plots spike amps versus the time Parameters ------------- trajectories: list of dicts list of dictionaries of trajectories fetched from ONE nframes_per_cycle: int, optional number of frames per cycle of spinning, default 30. figsize: list, [width, height], optional size of the images, default [800, 700] Return ------------ frames: list of numpy.darray image in rgb for each frame ''' fig = rendering.figure() for index, trj in enumerate(trajectories): if trj['coordinate_system'] == 'IBL-Allen': brain_atlas = ba_allen elif trj['coordinate_system'] == 'Needles-Allen': brain_atlas = ba_needles else: brain_atlas = ba_allen ins = atlas.Insertion.from_dict(trj, brain_atlas=brain_atlas) ins = atlas.Insertion.from_dict(trj, brain_atlas=ba_allen) mlapdv = brain_atlas.xyz2ccf(ins.xyz) if trj['provenance'] == 'Ephys aligned histology track': color = (0, 0, 1.) # Blue mlab.plot3d(mlapdv[:, 1], mlapdv[:, 2], mlapdv[:, 0], line_width=3, color=color, tube_radius=20) frames = [] for i in range(nframes_per_cycle): mlab.view(azimuth=0, elevation=0 - i * (360 / nframes_per_cycle)) mlab.roll(180) mlab.test_plot3d() f = mlab.gcf() f.scene._lift() frames.append(mlab.screenshot(mode='rgb', antialiased=True)) return frames
def test_udp(): """Simple test for the UDP server.""" from mayavi import mlab mlab.test_plot3d() serve_udp()
def test_tcp(): """Simple test for the TCP server.""" from mayavi import mlab mlab.test_plot3d() serve_tcp()
Plotting simple 3D graph with Mayavi ==================================== A simple example of the plot of a 3D graph with Mayavi in order to test the autonomy of the gallery. """ # Code source: Alex Gramfort # License: BSD 3 clause # This will show the mlab.test_mesh figure in the gallery # sphinx_gallery_thumbnail_number = 4 from mayavi import mlab mlab.test_plot3d() mlab.figure() mlab.test_contour3d() ##################################################################### # Note: this shows that inside a cell matplotlib figures are always # put before mayavi in the example HTML. In other words, the order of # plotting is not respected between matplotlib and mayavi figures mlab.figure() mlab.test_mesh() mlab.figure() mlab.test_flow()
from mayavi import mlab import vtk currfig = mlab.test_plot3d() mlab.orientation_axes() #mlab.draw() cam = currfig.scene.camera cam.zoom(1.5) #mlab.draw() mlab.show()
def test_save_mayavi_figures(gallery_conf): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise pytest.skip('Mayavi not installed') import matplotlib.pyplot as plt mlab.options.offscreen = True gallery_conf.update( image_scrapers=(matplotlib_scraper, mayavi_scraper)) fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png') image_path_iterator = ImagePathIterator(fname_template) block = ('',) * 3 block_vars = dict(image_path_iterator=image_path_iterator) plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Greens') mlab.test_plot3d() image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 2 assert '/image0.png' not in image_rst assert '/image1.png' in image_rst assert '/image2.png' in image_rst assert '/image3.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) assert os.path.isfile(fname_template.format(1)) assert os.path.isfile(fname_template.format(2)) assert not os.path.isfile(fname_template.format(0)) with Image.open(fname_template.format(1)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [247, 252, 245]).all() # plt first # Test next-value handling, plus image_scrapers modification gallery_conf.update(image_scrapers=(matplotlib_scraper,)) mlab.test_plot3d() plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Reds') image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 3 assert '/image1.png' not in image_rst assert '/image2.png' not in image_rst assert '/image3.png' in image_rst assert '/image4.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) for ii in range(3): assert os.path.isfile(fname_template.format(ii + 1)) assert not os.path.isfile(fname_template.format(4)) with Image.open(fname_template.format(3)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [255, 245, 240]).all() # custom finders gallery_conf.update(image_scrapers=[lambda x, y, z: '']) image_rst = save_figures(block, block_vars, gallery_conf) assert len(image_path_iterator) == 3 # degenerate gallery_conf.update(image_scrapers=['foo']) with pytest.raises(ValueError, match='Unknown image scraper'): _complete_gallery_conf( gallery_conf, gallery_conf['gallery_dir'], True, False) gallery_conf.update( image_scrapers=[lambda x, y, z: y['image_path_iterator'].next()]) with pytest.raises(RuntimeError, match='did not produce expected image'): save_figures(block, block_vars, gallery_conf) gallery_conf.update(image_scrapers=[lambda x, y, z: 1.]) with pytest.raises(TypeError, match='was not a string'): save_figures(block, block_vars, gallery_conf)
def test_save_mayavi_figures(gallery_conf): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise pytest.skip('Mayavi not installed') import matplotlib.pyplot as plt mlab.options.offscreen = True gallery_conf.update(image_scrapers=(matplotlib_scraper, mayavi_scraper)) fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png') image_path_iterator = ImagePathIterator(fname_template) block = ('', ) * 3 block_vars = dict(image_path_iterator=image_path_iterator) plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Greens') mlab.test_plot3d() image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 2 assert '/image0.png' not in image_rst assert '/image1.png' in image_rst assert '/image2.png' in image_rst assert '/image3.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) assert os.path.isfile(fname_template.format(1)) assert os.path.isfile(fname_template.format(2)) assert not os.path.isfile(fname_template.format(0)) with Image.open(fname_template.format(1)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [247, 252, 245]).all() # plt first # Test next-value handling, plus image_scrapers modification gallery_conf.update(image_scrapers=(matplotlib_scraper, )) mlab.test_plot3d() plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Reds') image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 3 assert '/image1.png' not in image_rst assert '/image2.png' not in image_rst assert '/image3.png' in image_rst assert '/image4.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) for ii in range(3): assert os.path.isfile(fname_template.format(ii + 1)) assert not os.path.isfile(fname_template.format(4)) with Image.open(fname_template.format(3)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [255, 245, 240]).all() # custom finders gallery_conf.update(image_scrapers=[lambda x, y, z: '']) image_rst = save_figures(block, block_vars, gallery_conf) assert len(image_path_iterator) == 3 # degenerate gallery_conf.update(image_scrapers=['foo']) with pytest.raises(ValueError, match='Unknown image scraper'): _complete_gallery_conf(gallery_conf, gallery_conf['gallery_dir'], True, False) gallery_conf.update( image_scrapers=[lambda x, y, z: y['image_path_iterator'].next()]) with pytest.raises(RuntimeError, match='did not produce expected image'): save_figures(block, block_vars, gallery_conf) gallery_conf.update(image_scrapers=[lambda x, y, z: 1.]) with pytest.raises(TypeError, match='was not a string'): save_figures(block, block_vars, gallery_conf)