def test_figure_io(self): vpl.close() self.assertIs(vpl.gcf(False), None) vpl.auto_figure(False) self.assertIs(vpl.gcf(), None) with self.assertRaises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() self.assertIs(vpl.gcf(), None) del fig vpl.auto_figure(True) fig = vpl.gcf() self.assertTrue(fig is not None) self.assertIs(fig, vpl.gcf()) vpl.close() self.assertIs(vpl.gcf(False), None) vpl.scf(fig) self.assertIs(fig, vpl.gcf()) vpl.close() fig = vpl.figure() self.assertIs(fig, vpl.gcf()) vpl.close()
def test_figure_io(): vpl.close() assert vpl.gcf(False) is None vpl.auto_figure(False) assert vpl.gcf() is None with pytest.raises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() assert vpl.gcf() is None del fig vpl.auto_figure(True) fig = vpl.gcf() assert fig is not None assert fig is vpl.gcf() vpl.close() assert vpl.gcf(False) is None vpl.scf(fig) assert fig is vpl.gcf() vpl.close() fig = vpl.figure() assert fig is vpl.gcf() vpl.close()
def test_conversions(): vpl.quick_test_plot() arr = vpl.screenshot_fig() vpl.close() image_data = vpl.image_io.vtkimagedata_from_array(arr) arr2 = vpl.image_io.vtkimagedata_to_array(image_data) assert np.array_equal(arr, arr2)
def test_trim_image(): fig = vpl.figure() # Shouldn't do anything if the figure is empty. assert vpl.screenshot_fig(trim_pad_width=.05).shape[:2] == fig.render_size vpl.quick_test_plot() arr = vpl.screenshot_fig() vpl.close() trimmed = vpl.image_io.trim_image(arr, fig.background_color, 10) background_color = np.asarray(fig.background_color) * 255 # Check that no non-background coloured pixels have been lost. assert (arr != background_color).any(-1).sum() \ == (trimmed != background_color).any(-1).sum() return trimmed
def test_save(self): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) # I can't get python2 to cooperate with unicode here. # The os functions just don't like them. if sys.version[0] == "3": path = Path.cwd() / u"ҢघԝઌƔࢳܢˀા\\Հએࡓ\u061cཪЈतயଯ\u0886.png" try: os.mkdir(str(path.parent)) vpl.save_fig(path) self.assertTrue(path.exists()) os.remove(str(path)) finally: if path.parent.exists(): os.rmdir(str(path.parent)) else: path = Path.cwd() / "image.png" vpl.save_fig(path) os.remove(str(path)) array = vpl.screenshot_fig(2) self.assertEqual(array.shape, tuple(i * 2 for i in vpl.gcf().render_size) + (3, )) plt.imshow(array) plt.show() shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3))) vpl.screenshot_fig(pixels=shape).shape # The following will fail depending on VTK version # self.assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() fig = vpl.figure() for plot in plots: fig += plot vpl.show()
def test_save(): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) path = TEST_DIR / "name.png" if path.exists(): os.remove(str(path)) vpl.save_fig(path) assert path.exists() array = vpl.screenshot_fig(magnification=2) assert array.shape == tuple(i * 2 for i in vpl.gcf().render_size) + (3, ) shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3))) vpl.screenshot_fig(pixels=shape).shape # The following will fail depending on VTK version # .assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() return array
def test_qfigure(): vpl.QtFigure._abc_assert_no_abstract_methods() self = vpl.QtFigure("a Qt widget figure") assert self is vpl.gcf() direction = np.array([1, 0, 0]) vpl.quiver(np.array([0, 0, 0]), direction) vpl.view(camera_direction=direction) vpl.reset_camera() self.show(block=False) self.close() self.showMaximized(block=not VTKPLOTLIB_WINDOWLESS_TEST) out = vpl.screenshot_fig(fig=self) vpl.close(fig=self) globals().update(locals()) return out
def test_qfigure2(): fig = vpl.QtFigure2("a QWidget figure") fig.setWindowTitle(fig.window_name) assert fig is vpl.gcf() plot = vpl.scatter(np.arange(9).reshape((3, 3)).T)[0] vpl.quick_test_plot() fig.add_all() fig.show(block=False) fig.qapp.processEvents() for i in fig.view_buttons.buttons: i.released.emit() fig.qapp.processEvents() time.sleep(.1) if not VTKPLOTLIB_WINDOWLESS_TEST: fig.screenshot_button.released.emit() fig.show_plot_table_button.released.emit() fig.show(block=False) for plot in fig.plot_table.rows: fig.plot_table.rows[plot].text.released.emit() fig.qapp.processEvents() assert not plot.visible assert np.allclose(vpl.screenshot_fig(fig=fig), np.array(255) * fig.background_color, atol=1.) for plot in fig.plot_table.rows: fig.plot_table.rows[plot].text.released.emit() fig.qapp.processEvents() assert plot.visible fig.plot_table.close()
def get_projections(file, camera_directions=CAMERA_DIRS): """ Get projection views in given camera directions from an STL file. Returns: Numpy array of shape (PROJ_SHAPE, len(camera_directions)). """ views = [] for dir in camera_directions[::-1]: mesh = Mesh.from_file(file) vpl.mesh_plot(mesh, color="black") r = vpl.view(camera_position=(0, 0, 0), camera_direction=dir) vpl.reset_camera() vpl.zoom_to_contents(padding=1) # Upscale first so downscale is more accurate arr = vpl.screenshot_fig(magnification=10, off_screen=True) # Change 3-channel RGB image to single channel binary matrix arr = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY) arr[arr == 0] = 1 arr[arr == 218] = 0 arr = cv2.resize(arr, dsize=PROJ_SHAPE, interpolation=cv2.INTER_LINEAR) vpl.close() views.append(arr) views = np.array(views).reshape( (PROJ_SHAPE[0], PROJ_SHAPE[1], len(camera_directions))) return views