def plotsynapses(i): synapse1_loc = [ spines_df['spine 1 synapse'][i][0] * 4, spines_df['spine 1 synapse'][i][1] * 4, spines_df['spine 1 synapse'][i][2] * 40 ] synapse2_loc = [ spines_df['spine 2 synapse'][i][0] * 4, spines_df['spine 2 synapse'][i][1] * 4, spines_df['spine 2 synapse'][i][2] * 40 ] synapse1_actor = trimesh_vtk.point_cloud_actor([synapse1_loc], size=100, color=(0, 0, 1), opacity=1) synapse2_actor = trimesh_vtk.point_cloud_actor([synapse2_loc], size=100, color=(1, 0, 0), opacity=1) return synapse1_loc, synapse2_loc, synapse1_actor, synapse2_actor
def viz_3d_network(positions, edgelist): """ Makes an interactive, 3d visualization of a spatial network. Parameters ---------- positions: n_nodes x 3 The 3d coordinates of each node in the network. edgelist: n_edges x 2 List of the edges. If edges are labeled by node names (as opposed to node indexes) then positions must be a pd.DataFrame indexed by the node names. """ # if nodes are named then convert node names to indexes if isinstance(positions, pd.DataFrame) and \ isinstance(edgelist, pd.DataFrame): # convert edges to node index format i.e. (idx_0, idx_9) -> (0, 9) rootid2index = {idx: i for i, idx in enumerate(positions.index)} to_idxer = np.vectorize(lambda x: rootid2index[x]) edgelist = to_idxer(edgelist) positions = np.array(positions) edgelist = np.array(edgelist) sk = skeleton.Skeleton(positions, edgelist) edge_actor = trimesh_vtk.skeleton_actor(sk, color=(0, 0, 0), opacity=.7, line_width=1) vert_actor = trimesh_vtk.point_cloud_actor(positions, size=3000, color=(1, 0, 0)) trimesh_vtk.render_actors([edge_actor, vert_actor])
def test_point_cloud(full_cell_mesh, full_cell_synapses, full_cell_soma_pt, tmp_path): mesh_actor = trimesh_vtk.mesh_actor(full_cell_mesh) camera = trimesh_vtk.oriented_camera(full_cell_soma_pt, backoff=300) sizes = full_cell_synapses['sizes'] # size points by size, fixed color syn_actor = trimesh_vtk.point_cloud_actor(full_cell_synapses['positions'], size=sizes, color=(1, 0, 0)) eval_actor_image([mesh_actor, syn_actor], 'full_cell_with_synapes_size_scaled.png', tmp_path, camera=camera) # color points by size, mapping sizes syn_actor = trimesh_vtk.point_cloud_actor(full_cell_synapses['positions'], size=500, color=np.clip(sizes, 0, 1000)) eval_actor_image([mesh_actor, syn_actor], 'full_cell_synapes_colored_size.png', tmp_path, camera=camera) # color and size points syn_actor = trimesh_vtk.point_cloud_actor(full_cell_synapses['positions'], size=sizes, color=np.clip(sizes, 0, 1000)) eval_actor_image([mesh_actor, syn_actor], 'full_cell_synapes_colored_and_size.png', tmp_path, camera=camera) # random colors x = np.linspace(0, 1.0, len(sizes)) rand_colors = np.hstack([ x[:, np.newaxis], np.abs(x - .5)[:, np.newaxis], (1 - x)[:, np.newaxis] ]) syn_actor = trimesh_vtk.point_cloud_actor(full_cell_synapses['positions'], size=500, color=rand_colors) eval_actor_image([mesh_actor, syn_actor], 'full_cell_synapes_random_colors.png', tmp_path, camera=camera) # random colors uint8 rand_colors_uint8 = np.uint8(rand_colors * 255) syn_actor = trimesh_vtk.point_cloud_actor(full_cell_synapses['positions'], size=500, color=rand_colors_uint8) eval_actor_image([mesh_actor, syn_actor], 'full_cell_synapes_random_colors_uint8.png', tmp_path, camera=camera) # test failure modes with pytest.raises(ValueError) as e: syn_actor = trimesh_vtk.point_cloud_actor( full_cell_synapses['positions'], size=np.random.rand(10, 10), color=(1, 0, 0)) with pytest.raises(ValueError) as e: syn_actor = trimesh_vtk.point_cloud_actor( full_cell_synapses['positions'], size=300, color=np.random.rand(len(x), 2))
color=(1, 0, 0), opacity=0.2) # Find synapse location and make a point cloud actor for the synapse of each mesh synapse1_loc = [ spines_df['spine 1 synapse'][i][0] * 4, spines_df['spine 1 synapse'][i][1] * 4, spines_df['spine 1 synapse'][i][2] * 40 ] synapse2_loc = [ spines_df['spine 2 synapse'][i][0] * 4, spines_df['spine 2 synapse'][i][1] * 4, spines_df['spine 2 synapse'][i][2] * 40 ] synapse1_actor = trimesh_vtk.point_cloud_actor([synapse1_loc], size=100, color=(0, 0, 1), opacity=1) synapse2_actor = trimesh_vtk.point_cloud_actor([synapse2_loc], size=100, color=(1, 0, 0), opacity=1) # Skeletonize and create a skeleton actor for each mesh skeleton1 = meshparty.skeletonize.skeletonize_mesh(spinemesh1_mp, compute_radius=False, cc_vertex_thresh=0) skeleton1_actor = meshparty.trimesh_vtk.skeleton_actor(skeleton1, color=(0, 0, 1), opacity=1) skeleton2 = meshparty.skeletonize.skeletonize_mesh(spinemesh2_mp, compute_radius=False,
biggest_synapses = post_synapse_df.sort_values(by=["size"], ascending=False).head(10) print(biggest_synapses) # Visualize. mm = trimesh_io.MeshMeta(disk_cache_path="test/test_files") mesh = mm.mesh( filename= "/data/dynamic_brain_workshop/electron_microscopy/2019/meshes/%d.h5" % cellid) mesh_poly = trimesh_vtk.trimesh_to_vtk(mesh.vertices, mesh.faces, None) plt_actor = vtkplotter.Actor(mesh_poly) syn_pts = np.vstack( biggest_synapses["ctr_pt_position"].values) * voxel_resolution syn_sizes = biggest_synapses["size"] syn_actors = trimesh_vtk.point_cloud_actor(syn_pts, size=syn_sizes.values) vtkplotter.embedWindow(backend="k3d") vp = vtkplotter.Plotter(bg="b") myactor = vtkplotter.Actor(plt_actor, c="r") myactor.GetMapper().Update() mysynactor = vtkplotter.Actor(syn_actors, c="b") mysynactor.GetMapper().Update() vp += mysynactor vp.show() # Find the axon initial segment (AIS) of a pyramidal cell. mesh_folder = "/data/dynamic_brain_workshop/electron_microscopy/2019/meshes/" # Query the AIS bounds table and find the AIS points with the pyramidal cell that # have the functional ID of 1. nrn_ind = 1
def doubleheaded(): # Find opening and point on skeleton closest to opening for each mesh spine1broken = trimesh.repair.broken_faces(spinemesh1_mp) spine1openings = spinemesh1_mp.submesh([spine1broken], append=True) spine1openings = meshparty.trimesh_io.Mesh( vertices=spine1openings.vertices, faces=spine1openings.faces) print(spine1openings) if spine1openings.body_count == 1: path_opening1_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh1_mp, skeleton1, spine1openings.vertices[0]) opening_ind1_a = np.where( skeleton1.vertices == spinemesh1_mp.vertices[path_opening1_a[-1]]) opening_ind1_a = opening_ind1_a[0][0] opening_ind1_b = -1 elif spine1openings.body_count == 2: mask1 = mesh_filters.filter_largest_component(spine1openings) spine1openings_a = spine1openings.apply_mask(mask1) spine1openings_b_vertices = [] for i in spine1openings.vertices: if i not in spine1openings_a.vertices: spine1openings_b_vertices.append(i.tolist()) path_opening1_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh1_mp, skeleton1, spine1openings.vertices[0]) path_opening1_b = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh1_mp, skeleton1, spine1openings_b_vertices[0]) opening_ind1_a = np.where( skeleton1.vertices == spinemesh1_mp.vertices[path_opening1_a[-1]]) opening_ind1_a = opening_ind1_a[0][0] opening_ind1_b = np.where( skeleton1.vertices == spinemesh1_mp.vertices[path_opening1_b[-1]]) opening_ind1_b = opening_ind1_b[0][0] else: mask1 = mesh_filters.filter_largest_component(spine1openings) spine1openings = spine1openings.apply_mask(mask1) path_opening1_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh1_mp, skeleton1, spine1openings.vertices[0]) opening_ind1_a = np.where( skeleton1.vertices == spinemesh1_mp.vertices[path_opening1_a[-1]]) opening_ind1_a = opening_ind1_a[0][0] opening_ind1_b = -1 spine2broken = trimesh.repair.broken_faces(spinemesh2_mp) spine2openings = spinemesh2_mp.submesh([spine2broken], append=True) spine2openings = meshparty.trimesh_io.Mesh( vertices=spine2openings.vertices, faces=spine2openings.faces) print(spine2openings) if spine2openings.body_count == 1: path_opening2_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh2_mp, skeleton2, spine2openings.vertices[0]) opening_ind2_a = np.where( skeleton2.vertices == spinemesh2_mp.vertices[path_opening2_a[-1]]) opening_ind2_a = opening_ind2_a[0][0] opening_ind2_b = -1 elif spine2openings.body_count == 2: mask2 = mesh_filters.filter_largest_component(spine2openings) spine2openings_a = spine2openings.apply_mask(mask2) spine2openings_b_vertices = [] for i in spine2openings.vertices: if i not in spine2openings_a.vertices: spine2openings_b_vertices.append(i.tolist()) path_opening2_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh2_mp, skeleton2, spine2openings.vertices[0]) path_opening2_b = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh2_mp, skeleton2, spine2openings_b_vertices[0]) opening_ind2_a = np.where( skeleton2.vertices == spinemesh2_mp.vertices[path_opening2_a[-1]]) opening_ind2_a = opening_ind2_a[0][0] opening_ind2_b = np.where( skeleton2.vertices == spinemesh2_mp.vertices[path_opening2_b[-1]]) opening_ind2_b = opening_ind2_b[0][0] else: mask2 = mesh_filters.filter_largest_component(spine2openings) spine2openings = spine2openings.apply_mask(mask2) path_opening2_a = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh2_mp, skeleton2, spine2openings.vertices[0]) opening_ind2_a = np.where( skeleton2.vertices == spinemesh2_mp.vertices[path_opening2_a[-1]]) opening_ind2_a = opening_ind2_a[0][0] opening_ind2_b = -1 # Create a path from the synapse to the opening for each mesh/synapse # The path consists of two parts: (1) a path from the synapse to the nearest point on the skeleton, and # (2) a path along the skeleton to the opening path1_1 = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh1_mp, skeleton1, synapse1_loc) meshverts1 = spinemesh1_mp.vertices.tolist() skelverts1 = skeleton1.vertices.tolist() inds1_a = path1_1 inds1_b = path1_1[1:] index = np.where(skeleton1.vertices == spinemesh1_mp.vertices[path1_1[-1]]) index = index[0][0] if opening_ind1_b == -1: opening_ind1 = opening_ind1_a skeleton1.reroot(opening_ind1_a) path1_2 = skeleton1.path_to_root(index) for i in path1_2[1:]: mesh_ind = np.where( spinemesh1_mp.vertices == skeleton1.vertices[i]) mesh_ind = mesh_ind[0][0] inds1_a.append(mesh_ind) inds1_b.append(mesh_ind) inds1_b = np.append(inds1_b, inds1_b[-1]) line1 = meshparty.trimesh_vtk.linked_point_actor(np.array(meshverts1), np.array(meshverts1), inds_a=inds1_a, inds_b=inds1_b, opacity=1) else: skeleton1_a = skeleton1 skeleton1_a.reroot(opening_ind1_a) path1_2a = skeleton1_a.path_to_root(index) skeleton1_b = skeleton1 skeleton1_b.reroot(opening_ind1_b) path1_2b = skeleton1_b.path_to_root(index) if len(path1_2a) > len(path1_2b) and len(path1_2b) > 1: path1_2 = path1_2b opening_ind1 = opening_ind1_b else: path1_2 = path1_2a opening_ind1 = opening_ind1_a for i in path1_2[1:]: mesh_ind = np.where( spinemesh1_mp.vertices == skeleton1.vertices[i]) mesh_ind = mesh_ind[0][0] inds1_a.append(mesh_ind) inds1_b.append(mesh_ind) inds1_b = np.append(inds1_b, inds1_b[-1]) line1 = meshparty.trimesh_vtk.linked_point_actor(np.array(meshverts1), np.array(meshverts1), inds_a=inds1_a, inds_b=inds1_b, opacity=1) path2_1 = meshparty.mesh_skel_utils.point_to_skel_meshpath( spinemesh2_mp, skeleton2, synapse2_loc) meshverts2 = spinemesh2_mp.vertices.tolist() skelverts2 = skeleton2.vertices.tolist() inds2_a = path2_1 inds2_b = path2_1[1:] index = np.where(skeleton2.vertices == spinemesh2_mp.vertices[path2_1[-1]]) index = index[0][0] if opening_ind2_b == -1: opening_ind2 = opening_ind2_a skeleton2.reroot(opening_ind2_a) path2_2 = skeleton2.path_to_root(index) for i in path2_2[1:]: mesh_ind = np.where( spinemesh2_mp.vertices == skeleton2.vertices[i]) mesh_ind = mesh_ind[0][0] inds2_a.append(mesh_ind) inds2_b.append(mesh_ind) inds2_b = np.append(inds2_b, inds2_b[-1]) line2 = meshparty.trimesh_vtk.linked_point_actor(np.array(meshverts2), np.array(meshverts2), inds_a=inds2_a, inds_b=inds2_b, opacity=1) else: skeleton2_a = skeleton2 skeleton2_a.reroot(opening_ind2_a) path2_2a = skeleton2_a.path_to_root(index) skeleton2_b = skeleton2 skeleton2_b.reroot(opening_ind2_b) path2_2b = skeleton2_b.path_to_root(index) if len(path2_2a) > len(path2_2b) and len(path2_2b) > 1: path2_2 = path2_2b opening_ind2 = opening_ind2_b else: path2_2 = path2_2a opening_ind2 = opening_ind2_a for i in path2_2[1:]: mesh_ind = np.where( spinemesh2_mp.vertices == skeleton2.vertices[i]) mesh_ind = mesh_ind[0][0] inds2_a.append(mesh_ind) inds2_b.append(mesh_ind) inds2_b = np.append(inds2_b, inds2_b[-1]) line2 = meshparty.trimesh_vtk.linked_point_actor(np.array(meshverts2), np.array(meshverts2), inds_a=inds2_a, inds_b=inds2_b, opacity=1) end_actor1 = trimesh_vtk.point_cloud_actor([skelverts1[opening_ind1]], size=100, color=(0.7, 0.9, 1), opacity=1) end_actor2 = trimesh_vtk.point_cloud_actor([skelverts2[opening_ind2]], size=100, color=(0.9, 0.7, 0.6), opacity=1) # Compare the two lines line1points = [] verts_ind1 = 0 for i in meshverts1: if verts_ind1 in inds1_a: line1points.append(i) verts_ind1 += 1 line2points = [] verts_ind2 = 0 for i in meshverts2: if verts_ind2 in inds2_a: line2points.append(i) verts_ind2 += 1 class Linemesh: def __init__(self, vertices): self.vertices = vertices line1mesh = Linemesh(line1points) #cloud = trimesh.PointCloud(line1points) #line1mesh = cloud.convex_hull compare = meshparty.mesh_filters.filter_spatial_distance_from_points( line1mesh, line2points, 200) print(compare) numTrue = 0 for i in compare: if i == True: numTrue += 1 if numTrue > len(compare) / 2: return False else: return True
color=(1, 0, 0), opacity=0.2) # Find synapse location and make a point cloud actor for the synapse of each mesh synapse1_loc = [ spines_df['spine 1 synapse'][i][0] * 4, spines_df['spine 1 synapse'][i][1] * 4, spines_df['spine 1 synapse'][i][2] * 40 ] synapse2_loc = [ spines_df['spine 2 synapse'][i][0] * 4, spines_df['spine 2 synapse'][i][1] * 4, spines_df['spine 2 synapse'][i][2] * 40 ] synapse1_actor = trimesh_vtk.point_cloud_actor([synapse1_loc], size=100, color=(0, 0, 1), opacity=1) synapse2_actor = trimesh_vtk.point_cloud_actor([synapse2_loc], size=100, color=(1, 0, 0), opacity=1) # Find opening for each mesh spine1broken = trimesh.repair.broken_faces(spinemesh1_mp) spine1openings = spinemesh1_mp.submesh([spine1broken], append=True) spine1openings = meshparty.trimesh_io.Mesh(vertices=spine1openings.vertices, faces=spine1openings.faces) print(spine1openings.body_count) if spine1openings.body_count == 1: print("PSS 1: spine") elif spine1openings.body_count > 3: