def test_colors(interactive=False): peak_dirs, peak_vals, peak_affine = generate_peaks() valid_mask = np.abs(peak_dirs).max(axis=(-2, -1)) > 0 indices = np.nonzero(valid_mask) scene = window.Scene() colors = [[1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [1, 1, 0]] peak_actor = PeakActor(peak_dirs, indices, values=peak_vals, affine=peak_affine, colors=colors) scene.add(peak_actor) scene.azimuth(30) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 4)
def test_basic_geometry_actor(interactive=False): centers = np.array([[4, 0, 0], [0, 4, 0], [0, 0, 0]]) colors = np.array([[1, 0, 0, 0.4], [0, 1, 0, 0.8], [0, 0, 1, 0.5]]) directions = np.array([[1, 1, 0]]) scale_list = [1, 2, (1, 1, 1), [3, 2, 1], np.array([1, 2, 3]), np.array([[1, 2, 3], [1, 3, 2], [3, 1, 2]])] actor_list = [[actor.cube, {}], [actor.box, {}], [actor.square, {}], [actor.rectangle, {}], [actor.frustum, {}], [actor.octagonalprism, {}], [actor.pentagonalprism, {}], [actor.triangularprism, {}], [actor.rhombicuboctahedron, {}]] for act_func, extra_args in actor_list: for scale in scale_list: scene = window.Scene() g_actor = act_func(centers=centers, colors=colors, directions=directions, scales=scale, **extra_args) scene.add(g_actor) if interactive: window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) msg = 'Failed with {}, scale={}'.format(act_func.__name__, scale) npt.assert_equal(report.objects, 3, err_msg=msg)
def plot_each_shell(ms, plot_sym_vecs=True, use_sphere=True, same_color=False, rad=0.025, opacity=1.0, ofile=None, ores=(300, 300)): """ Plot each shell Parameters ---------- ms: list of numpy.ndarray bvecs for each bval plot_sym_vecs: boolean Plot symmetrical vectors use_sphere: boolean rendering of the sphere same_color: boolean use same color for all shell rad: float radius of each point opacity: float opacity for the shells ofile: str output filename ores: tuple resolution of the output png Return ------ """ if len(ms) > 10: vtkcolors = fury.colormap.distinguishable_colormap(nb_colors=len(ms)) if use_sphere: sphere = get_sphere('symmetric724') shape = (1, 1, 1, sphere.vertices.shape[0]) fid, fname = mkstemp(suffix='_odf_slicer.mmap') odfs = np.memmap(fname, dtype=np.float64, mode='w+', shape=shape) odfs[:] = 1 odfs[..., 0] = 1 affine = np.eye(4) for i, shell in enumerate(ms): if same_color: i = 0 ren = window.Renderer() ren.SetBackground(1, 1, 1) if use_sphere: sphere_actor = actor.odf_slicer(odfs, affine, sphere=sphere, colormap='winter', scale=1.0, opacity=opacity) ren.add(sphere_actor) pts_actor = actor.point(shell, vtkcolors[i], point_radius=rad) ren.add(pts_actor) if plot_sym_vecs: pts_actor = actor.point(-shell, vtkcolors[i], point_radius=rad) ren.add(pts_actor) window.show(ren) if ofile: window.snapshot(ren, fname=ofile + '_shell_' + str(i) + '.png', size=ores)
def main(): parser = _build_args_parser() args = parser.parse_args() assert_inputs_exist(parser, [args.tractogram]) assert_outputs_exist(parser, args, [], [args.save]) tracts_format = detect_format(args.tractogram) if tracts_format is not TrkFile: raise ValueError("Invalid input streamline file format " + "(must be trk): {0}".format(args.tractogram_filename)) # Load files and data trk = TrkFile.load(args.tractogram) tractogram = trk.tractogram streamlines = tractogram.streamlines if 'seeds' not in tractogram.data_per_streamline: parser.error('Tractogram does not contain seeds') seeds = tractogram.data_per_streamline['seeds'] # Make display objects streamlines_actor = actor.line(streamlines) points = actor.dots(seeds, color=(1., 1., 1.)) # Add display objects to canvas r = window.Renderer() r.add(streamlines_actor) r.add(points) # Show and record if needed if args.save is not None: window.record(r, out_path=args.save, size=(1000, 1000)) window.show(r)
def test_lookup_colormap(interactive=False): peak_dirs, peak_vals, peak_affine = generate_peaks() valid_mask = np.abs(peak_dirs).max(axis=(-2, -1)) > 0 indices = np.nonzero(valid_mask) scene = window.Scene() colors = [.0, .1, .2, .5, .8, .9, 1] hue = (0, 1) # Red to blue saturation = (0, 1) # White to full saturation lut_cmap = actor.colormap_lookup_table(hue_range=hue, saturation_range=saturation) peak_actor = PeakActor(peak_dirs, indices, values=peak_vals, affine=peak_affine, colors=colors, lookup_colormap=lut_cmap) scene.add(peak_actor) scene.azimuth(30) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 4)
def test_replace_shader_in_actor(interactive=False): scene = window.Scene() test_actor = generate_points() scene.add(test_actor) if interactive: window.show(scene) ss = window.snapshot(scene, size=(200, 200)) actual = ss[40, 140, :] npt.assert_array_equal(actual, [0, 0, 0]) actual = ss[140, 40, :] npt.assert_array_equal(actual, [0, 0, 0]) actual = ss[40, 40, :] npt.assert_array_equal(actual, [0, 0, 0]) scene.clear() replace_shader_in_actor(test_actor, 'geometry', geometry_code) scene.add(test_actor) if interactive: window.show(scene) ss = window.snapshot(scene, size=(200, 200)) actual = ss[40, 140, :] npt.assert_array_equal(actual, [255, 0, 0]) actual = ss[140, 40, :] npt.assert_array_equal(actual, [0, 255, 0]) actual = ss[40, 40, :] npt.assert_array_equal(actual, [0, 0, 255])
def test_dots(interactive=False): points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0]]) dots_actor = actor.dots(points, color=(0, 255, 0)) scene = window.Scene() scene.add(dots_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene, reset_camera=False) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1) extent = scene.GetActors().GetLastActor().GetBounds() npt.assert_equal(extent, (0.0, 1.0, 0.0, 1.0, 0.0, 0.0)) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=(0, 255, 0)) npt.assert_equal(report.objects, 3) # Test one point points = np.array([0, 0, 0]) dot_actor = actor.dots(points, color=(0, 0, 255)) scene.clear() scene.add(dot_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=(0, 0, 255)) npt.assert_equal(report.objects, 1)
def main(): parser = _build_arg_parser() args = parser.parse_args() assert_inputs_exist(parser, [args.tractogram]) assert_outputs_exist(parser, args, [], [args.save]) tracts_format = detect_format(args.tractogram) if tracts_format is not TrkFile: raise ValueError("Invalid input streamline file format " + "(must be trk): {0}".format(args.tractogram_filename)) # Load files and data. TRKs can have 'same' as reference tractogram = load_tractogram(args.tractogram, 'same') # Streamlines are saved in RASMM but seeds are saved in VOX # This might produce weird behavior with non-iso tractogram.to_vox() streamlines = tractogram.streamlines if 'seeds' not in tractogram.data_per_streamline: parser.error('Tractogram does not contain seeds') seeds = tractogram.data_per_streamline['seeds'] # Make display objects streamlines_actor = actor.line(streamlines) points = actor.dots(seeds, color=(1., 1., 1.)) # Add display objects to canvas s = window.Scene() s.add(streamlines_actor) s.add(points) # Show and record if needed if args.save is not None: window.record(s, out_path=args.save, size=(1000, 1000)) window.show(s)
def test_shader_to_actor(interactive=False): cube = generate_cube_with_effect() scene = window.Scene() scene.add(cube) if interactive: scene.add(actor.axes()) window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) # test errors npt.assert_raises(ValueError, shader_to_actor, cube, "error", vertex_impl) npt.assert_raises(ValueError, shader_to_actor, cube, "geometry", vertex_impl) npt.assert_raises(ValueError, shader_to_actor, cube, "vertex", vertex_impl, block="error") npt.assert_raises(ValueError, replace_shader_in_actor, cube, "error", vertex_impl)
def test_stick(interactive=False): molecule = mol.Molecule() mol.add_atom(molecule, 6, 0, 0, 0) mol.add_atom(molecule, 6, 2, 0, 0) # Test errors for inadequate bonding data npt.assert_raises(ValueError, mol.stick, molecule) mol.add_bond(molecule, 0, 1, 1) colormodes = ['discrete', 'single'] bond_thickness = [0.1, 0.12] table = mol.PTable() colors = np.array([[table.atom_color(6)], [[150/255, 150/255, 150/255], [50/255, 50/255, 50/255]]], dtype=object) scene = window.Scene() for i, colormode in enumerate(colormodes): test_actor = mol.stick(molecule, colormode, bond_thickness[i]) scene.add(test_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors[i]) npt.assert_equal(report.objects, 1) scene.clear() # Testing warnings npt.assert_warns(UserWarning, mol.stick, molecule, 'multiple')
def test_vertices_from_actor(interactive=False): expected = np.array([[1.5, -0.5, 0.], [1.5, 0.5, 0], [2.5, 0.5, 0], [2.5, -0.5, 0], [-1, 1, 0], [-1, 3, 0], [1, 3, 0], [1, 1, 0], [-0.5, -0.5, 0], [-0.5, 0.5, 0], [0.5, 0.5, 0], [0.5, -0.5, 0]]) centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]]) colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) scales = [1, 2, 1] verts, faces = fp.prim_square() res = fp.repeat_primitive(verts, faces, centers=centers, colors=colors, scales=scales) big_verts = res[0] big_faces = res[1] big_colors = res[2] actr = get_actor_from_primitive(big_verts, big_faces, big_colors) actr.GetProperty().BackfaceCullingOff() if interactive: scene = window.Scene() scene.add(actor.axes()) scene.add(actr) window.show(scene) res_vertices = vertices_from_actor(actr) npt.assert_array_almost_equal(expected, res_vertices)
def test_billboard_actor(interactive=False): scene = window.Scene() scene.background((1, 1, 1)) centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]]) colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) scale = [1, 2, 1] fake_sphere = \ """ float len = length(point); float radius = 1.; if(len > radius) {discard;} vec3 normalizedPoint = normalize(vec3(point.xy, sqrt(1. - len))); vec3 direction = normalize(vec3(1., 1., 1.)); float df = max(0, dot(direction, normalizedPoint)); float sf = pow(df, 24); fragOutput0 = vec4(max(df * color, sf * vec3(1)), 1); """ billboard_actor = actor.billboard(centers, colors=colors.astype(np.uint8), scale=scale, fs_impl=fake_sphere) scene.add(billboard_actor) scene.add(actor.axes()) if interactive: window.show(scene)
def test_billboard_actor(interactive=False): scene = window.Scene() scene.background((1, 1, 1)) centers = np.array([[0, 0, 0], [5, -5, 5], [-7, 7, -7], [10, 10, 10], [10.5, 11.5, 11.5], [12, -12, -12], [-17, 17, 17], [-22, -22, 22]]) colors = np.array([[1, 1, 0], [0, 0, 0], [1, 0, 1], [0, 0, 1], [1, 1, 1], [1, 0, 0], [0, 1, 0], [0, 1, 1]]) scales = [6, .4, 1.2, 1, .2, .7, 3, 2] fake_sphere = \ """ float len = length(point); float radius = 1.; if(len > radius) {discard;} vec3 normalizedPoint = normalize(vec3(point.xy, sqrt(1. - len))); vec3 direction = normalize(vec3(1., 1., 1.)); float df_1 = max(0, dot(direction, normalizedPoint)); float sf_1 = pow(df_1, 24); fragOutput0 = vec4(max(df_1 * color, sf_1 * vec3(1)), 1); """ billboard_actor = actor.billboard(centers, colors=colors, scales=scales, fs_impl=fake_sphere) scene.add(billboard_actor) scene.add(actor.axes()) if interactive: window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 8)
def test_marker_actor(interactive=False): scene = window.Scene() scene.background((1, 1, 1)) centers_3do = np.array([[4, 0, 0], [4, 4, 0], [4, 8, 0]]) markers_2d = ['o', 's', 'd', '^', 'p', 'h', 's6', 'x', '+'] center_markers_2d = np.array( [[0, i*2, 0] for i in range(len(markers_2d))]) fake_spheres = actor.markers( centers_3do, colors=(0, 1, 0), scales=1, marker='3d' ) markers_2d = actor.markers( center_markers_2d, colors=(0, 1, 0), scales=1, marker=markers_2d ) scene.add(fake_spheres) scene.add(markers_2d) if interactive: window.show(scene) arr = window.snapshot(scene) colors = np.array([[0, 1, 0] for i in range(12)]) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 12)
def test_sphere_cpk(interactive=False): atomic_numbers, atom_coords = get_default_molecular_info() molecule = mol.Molecule(atomic_numbers, atom_coords) table = mol.PTable() colormodes = ['discrete', 'single'] colors = np.array([[table.atom_color(1), table.atom_color(6)], [[150/255, 250/255, 150/255]]], dtype=object) scene = window.Scene() for i, colormode in enumerate(colormodes): test_actor = mol.sphere_cpk(molecule, colormode) scene.add(test_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors[i]) npt.assert_equal(report.objects, 1) scene.clear() # Testing warnings npt.assert_warns(UserWarning, mol.sphere_cpk, molecule, 'multiple')
def test_spheres(interactive=False): xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 25], [200, 0, 0, 50]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.99]]) scene = window.Scene() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3]) scene.add(sphere_actor) if interactive: window.show(scene, order_transparent=True) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 3) # test with an unique color for all centers scene.clear() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=np.array([1, 0, 0]), radii=xyzr[:, 3]) scene.add(sphere_actor) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=(1, 0, 0)) npt.assert_equal(report.colors_found, [True])
def test_contour_from_label(interactive=False): # Render volumne scene = window.Scene() data = np.zeros((50, 50, 50)) data[5:15, 1:10, 25] = 1. data[25:35, 1:10, 25] = 2. data[40:49, 1:10, 25] = 3. color = np.array([[255, 0, 0, 0.6], [0, 255, 0, 0.5], [0, 0, 255, 1.0]]) surface = actor.contour_from_label(data, color=color) scene.add(surface) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test Errors with npt.assert_raises(ValueError): actor.contour_from_label(data, color=np.array([1, 2, 3])) actor.contour_from_label(np.ones(50)) # Test binarization scene2 = window.Scene() data2 = np.zeros((50, 50, 50)) data2[20:30, 25, 25] = 1. data2[25, 20:30, 25] = 2. color2 = np.array([[255, 0, 255], [255, 255, 0]]) surface2 = actor.contour_from_label(data2, color=color2) scene2.add(surface2) scene2.reset_camera() scene2.reset_clipping_range() if interactive: window.show(scene2) arr = window.snapshot(scene, 'test_surface.png', offscreen=True, order_transparent=False) arr2 = window.snapshot(scene2, 'test_surface2.png', offscreen=True, order_transparent=True) report = window.analyze_snapshot(arr, colors=[(255, 0, 0), (0, 255, 0), (0, 0, 255)], find_objects=True) report2 = window.analyze_snapshot(arr2, find_objects=True) npt.assert_equal(report.objects, 3) npt.assert_equal(report2.objects, 1) actor.contour_from_label(data)
def show_atlas_target_graph(atlas, target, out_path, interactive=True): ren = window.Scene() ren.SetBackground(1, 1, 1) ren.add(actor.line(atlas, colors=(1, 0, 1))) # Magenta ren.add(actor.line(target, colors=(1, 1, 0))) # Yellow #window.record(ren, out_path=out_path, size=(600, 600)) if interactive: window.show(ren)
def test_polydata_polygon(interactive=False): # Create a cube my_triangles = np.array([[0, 6, 4], [0, 2, 6], [0, 3, 2], [0, 1, 3], [2, 7, 6], [2, 3, 7], [4, 6, 7], [4, 7, 5], [0, 4, 5], [0, 5, 1], [1, 5, 7], [1, 7, 3]], dtype='i8') my_vertices = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) colors = my_vertices * 255 my_polydata = vtk.vtkPolyData() utils.set_polydata_vertices(my_polydata, my_vertices) utils.set_polydata_triangles(my_polydata, my_triangles) npt.assert_equal(len(my_vertices), my_polydata.GetNumberOfPoints()) npt.assert_equal(len(my_triangles), my_polydata.GetNumberOfCells()) npt.assert_equal(utils.get_polydata_normals(my_polydata), None) res_triangles = utils.get_polydata_triangles(my_polydata) res_vertices = utils.get_polydata_vertices(my_polydata) npt.assert_array_equal(my_vertices, res_vertices) npt.assert_array_equal(my_triangles, res_triangles) utils.set_polydata_colors(my_polydata, colors) npt.assert_equal(utils.get_polydata_colors(my_polydata), colors) utils.update_polydata_normals(my_polydata) normals = utils.get_polydata_normals(my_polydata) npt.assert_equal(len(normals), len(my_vertices)) mapper = utils.get_polymapper_from_polydata(my_polydata) actor1 = utils.get_actor_from_polymapper(mapper) actor2 = utils.get_actor_from_polydata(my_polydata) scene = window.Scene() for actor in [actor1, actor2]: scene.add(actor) if interactive: window.show(scene) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1)
def test_peak_slicer(interactive=False): _peak_dirs = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype='f4') # peak_dirs.shape = (1, 1, 1) + peak_dirs.shape peak_dirs = np.zeros((11, 11, 11, 3, 3)) peak_values = np.random.rand(11, 11, 11, 3) peak_dirs[:, :, :] = _peak_dirs scene = window.Scene() peak_actor = actor.peak_slicer(peak_dirs) scene.add(peak_actor) scene.add(actor.axes((11, 11, 11))) if interactive: window.show(scene) scene.clear() scene.add(peak_actor) scene.add(actor.axes((11, 11, 11))) for k in range(11): peak_actor.display_extent(0, 10, 0, 10, k, k) for j in range(11): peak_actor.display_extent(0, 10, j, j, 0, 10) for i in range(11): peak_actor.display(i, None, None) scene.rm_all() peak_actor = actor.peak_slicer( peak_dirs, peak_values, mask=None, affine=np.diag([3, 2, 1, 1]), colors=None, opacity=0.8, linewidth=3, lod=True, lod_points=10 ** 4, lod_points_size=3) scene.add(peak_actor) scene.add(actor.axes((11, 11, 11))) if interactive: window.show(scene) report = window.analyze_scene(scene) ex = ['vtkLODActor', 'vtkOpenGLActor'] npt.assert_equal(report.actors_classnames, ex) # 6d data data_6d = (255 * np.random.rand(5, 5, 5, 5, 5, 5)) npt.assert_raises(ValueError, actor.peak_slicer, data_6d, data_6d)
def test_geometry_actor(interactive=False): xyz = np.array([[0, 0, 0], [50, 0, 0], [100, 0, 0]]) dirs = np.array([[0, 1, 0], [1, 0, 0], [0, 0.5, 0.5]]) actor_list = [[actor.cone, { 'directions': dirs, 'resolution': 8 }], [actor.arrow, { 'directions': dirs, 'resolution': 9 }], [actor.box, { 'directions': dirs, 'size': (1, 3, 2) }], [actor.cube, { 'directions': dirs }], [actor.cylinder, { 'directions': dirs }]] scene = window.Scene() for act_func, extra_args in actor_list: colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [1, 1, 0, 1]]) heights = np.array([5, 7, 10]) geom_actor = act_func(centers=xyz, heights=heights, colors=colors[:], **extra_args) scene.add(geom_actor) if interactive: window.show(scene, order_transparent=True) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 3) colors = np.array([1.0, 1.0, 1.0, 1.0]) heights = 10 scene.clear() geom_actor = act_func(centers=xyz[:, :3], heights=10, colors=colors[:], **extra_args) scene.add(geom_actor) if interactive: window.show(scene, order_transparent=True) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=[colors]) npt.assert_equal(report.objects, 3) scene.clear()
def test_labels(interactive=False): text_actor = actor.label("Hello") scene = window.Scene() scene.add(text_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene, reset_camera=False) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1)
def test_ribbon(interactive=False): scene = window.Scene() # Testing if helices and sheets are rendered properly atom_coords = np.array( [[31.726, 105.084, 71.456], [31.477, 105.680, 70.156], [32.599, 106.655, 69.845], [32.634, 107.264, 68.776], [30.135, 106.407, 70.163], [29.053, 105.662, 70.913], [28.118, 106.591, 71.657], [28.461, 107.741, 71.938], [26.928, 106.097, 71.983], [33.507, 106.802, 70.804], [34.635, 107.689, 70.622], [35.687, 107.018, 69.765], [36.530, 107.689, 69.174], [35.631, 105.690, 69.688], [36.594, 104.921, 68.903], [36.061, 104.498, 67.534], [36.601, 103.580, 66.916], [37.047, 103.645, 69.660], [35.907, 102.828, 69.957], [37.751, 104.014, 70.958]]) elements = np.array( [7, 6, 6, 8, 6, 6, 6, 8, 7, 7, 6, 6, 8, 7, 6, 6, 8, 6, 8, 6]) atom_names = np.array([ 'N', 'CA', 'C', 'O', 'CB', 'CG', 'CD', 'OE1', 'NE2', 'N', 'CA', 'C', 'O', 'N', 'CA', 'C', 'O', 'CB', 'OG1', 'OG2' ]) model = np.ones(20) chain = np.ones(20) * 65 residue_seq = np.ones(20) residue_seq[9:13] = 2 residue_seq[13:] = 3 residue_seq[6] = 4 is_hetatm = np.zeros(20, dtype=bool) secondary_structure = np.array([[65, 1, 65, 3]]) colors = np.array([[240 / 255, 0, 128 / 255], [1, 1, 0]]) for i, color in enumerate(colors): if i: helix = [] sheet = secondary_structure else: helix = secondary_structure sheet = [] molecule = mol.Molecule(elements, atom_coords, atom_names, model, residue_seq, chain, sheet, helix, is_hetatm) test_actor = mol.ribbon(molecule) scene.set_camera((28, 113, 74), (34, 106, 70), (-0.37, 0.29, -0.88)) scene.add(test_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=[color]) npt.assert_equal(report.objects, 1) scene.clear()
def test_vertices_from_actor(interactive=False): expected = np.array([[1.5, -0.5, 0.], [1.5, 0.5, 0], [2.5, 0.5, 0], [2.5, -0.5, 0], [-1, 1, 0], [-1, 3, 0], [1, 3, 0], [1, 1, 0], [-0.5, -0.5, 0], [-0.5, 0.5, 0], [0.5, 0.5, 0], [0.5, -0.5, 0]]) centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]]) colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) scales = [1, 2, 1] verts, faces = fp.prim_square() res = fp.repeat_primitive(verts, faces, centers=centers, colors=colors, scales=scales) big_verts = res[0] big_faces = res[1] big_colors = res[2] actr = get_actor_from_primitive(big_verts, big_faces, big_colors) actr.GetProperty().BackfaceCullingOff() if interactive: scene = window.Scene() scene.add(actor.axes()) scene.add(actr) window.show(scene) res_vertices = vertices_from_actor(actr) res_vertices_vtk = vertices_from_actor(actr, as_vtk=True) npt.assert_array_almost_equal(expected, res_vertices) npt.assert_equal(isinstance(res_vertices_vtk, vtk.vtkDoubleArray), True) # test colors_from_actor: l_colors = utils.colors_from_actor(actr) l_colors_vtk = utils.colors_from_actor(actr, as_vtk=True) l_colors_none = utils.colors_from_actor(actr, array_name='col') npt.assert_equal(l_colors_none, None) npt.assert_equal(isinstance(l_colors_vtk, vtk.vtkUnsignedCharArray), True) npt.assert_equal(np.unique(l_colors, axis=0).shape, colors.shape) l_array = utils.array_from_actor(actr, 'colors') l_array_vtk = utils.array_from_actor(actr, 'colors', as_vtk=True) l_array_none = utils.array_from_actor(actr, 'col') npt.assert_array_equal(l_array, l_colors) npt.assert_equal(l_array_none, None) npt.assert_equal(isinstance(l_array_vtk, vtk.vtkUnsignedCharArray), True)
def test_peak_slicer(interactive=False): _peak_dirs = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype='f4') # peak_dirs.shape = (1, 1, 1) + peak_dirs.shape peak_dirs = np.zeros((11, 11, 11, 3, 3)) peak_values = np.random.rand(11, 11, 11, 3) peak_dirs[:, :, :] = _peak_dirs renderer = window.Renderer() peak_actor = actor.peak_slicer(peak_dirs) renderer.add(peak_actor) renderer.add(actor.axes((11, 11, 11))) if interactive: window.show(renderer) renderer.clear() renderer.add(peak_actor) renderer.add(actor.axes((11, 11, 11))) for k in range(11): peak_actor.display_extent(0, 10, 0, 10, k, k) for j in range(11): peak_actor.display_extent(0, 10, j, j, 0, 10) for i in range(11): peak_actor.display(i, None, None) renderer.rm_all() peak_actor = actor.peak_slicer( peak_dirs, peak_values, mask=None, affine=np.diag([3, 2, 1, 1]), colors=None, opacity=1, linewidth=3, lod=True, lod_points=10 ** 4, lod_points_size=3) renderer.add(peak_actor) renderer.add(actor.axes((11, 11, 11))) if interactive: window.show(renderer) report = window.analyze_renderer(renderer) ex = ['vtkLODActor', 'vtkOpenGLActor', 'vtkOpenGLActor', 'vtkOpenGLActor'] npt.assert_equal(report.actors_classnames, ex)
def test_labels(interactive=False): npt.assert_warns(DeprecationWarning, actor.label, "FURY Rocks") text_actor = actor.vector_text("FURY Rocks") scene = window.Scene() scene.add(text_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene, reset_camera=False) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1)
def test_labels(interactive=False): text_actor = actor.label("Hello") renderer = window.Renderer() renderer.add(text_actor) renderer.reset_camera() renderer.reset_clipping_range() if interactive: window.show(renderer, reset_camera=False) npt.assert_equal(renderer.GetActors().GetNumberOfItems(), 1)
def my_visu(sf, sphere, rot=True, norm=True, scale=True, title="Modeling"): ren = window.renderer(background=window.colors.white) sf_actor = actor.odf_slicer(sf, sphere=sphere, colormap='jet', scale=0.4, norm=norm, radial_scale=scale) if rot: sf_actor.RotateX(90) ren.add(sf_actor) window.show(ren, title=title, size=WINDOW_SIZE) ren.rm(sf_actor) window.rm_all(ren)
def test_spheres(interactive=False): xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 25], [200, 0, 0, 50]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.99]]) renderer = window.Renderer() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3]) renderer.add(sphere_actor) if interactive: window.show(renderer, order_transparent=True) arr = window.snapshot(renderer) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 3)
def test_contour_from_roi(interactive=False): # Render volume scene = window.Scene() data = np.zeros((50, 50, 50)) data[20:30, 25, 25] = 1. data[25, 20:30, 25] = 1. affine = np.eye(4) surface = actor.contour_from_roi(data, affine, color=np.array([1, 0, 1]), opacity=.5) scene.add(surface) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test Errors npt.assert_raises(ValueError, actor.contour_from_roi, np.ones(50)) # Test binarization scene2 = window.Scene() data2 = np.zeros((50, 50, 50)) data2[20:30, 25, 25] = 1. data2[35:40, 25, 25] = 1. affine = np.eye(4) surface2 = actor.contour_from_roi(data2, affine, color=np.array([0, 1, 1]), opacity=.5) scene2.add(surface2) scene2.reset_camera() scene2.reset_clipping_range() if interactive: window.show(scene2) arr = window.snapshot(scene, 'test_surface.png', offscreen=True) arr2 = window.snapshot(scene2, 'test_surface2.png', offscreen=True) report = window.analyze_snapshot(arr, find_objects=True) report2 = window.analyze_snapshot(arr2, find_objects=True) npt.assert_equal(report.objects, 1) npt.assert_equal(report2.objects, 2)