def add_neuron(s, df, neuron_ids=[], show_ellipsoid_annotation=False): if len(neuron_ids) == 0: neuron_ids = list(np.unique(df.neuron_id)) for ii, neuron_id in enumerate(neuron_ids): color = "#%06x" % random.randint(0, 0xFFFFFF) pos_dic = {} df_neuron = df[df.neuron_id == neuron_id] edges = [] for index, row in df_neuron.iterrows(): node_id = row.id pos = np.array(row.position) pos_dic[node_id] = np.flip(pos) if row.parent_id: edges.append((node_id, row.parent_id)) v_nodes, u_nodes, connectors = [], [], [] print(f'Loaded {len(pos_dic)} nodes for neuron id {neuron_id}') for u, v in edges: if u in pos_dic and v in pos_dic: u_site = pos_dic[u] v_site = pos_dic[v] u_nodes.append( neuroglancer.EllipsoidAnnotation(center=u_site, radii=(30, 30, 30), id=next(ngid))) v_nodes.append( neuroglancer.EllipsoidAnnotation(center=v_site, radii=(30, 30, 30), id=next(ngid))) connectors.append( neuroglancer.LineAnnotation(point_a=u_site, point_b=v_site, id=next(ngid))) s.layers['neuronskeleton_{}'.format( neuron_id)] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color=color, annotations=connectors, ) if show_ellipsoid_annotation: s.layers['node_u_{}'.format( neuron_id)] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color=color, annotations=u_nodes, ) s.layers['node_v_{}'.format( neuron_id)] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color=color, annotations=v_nodes, )
def add_synapses(s, df, pre_neuron_id=None, post_neuron_id=None, color=None, name='', radius=30): if pre_neuron_id is not None: df = df[df.id_skel_pre == pre_neuron_id] if post_neuron_id is not None: df = df[df.id_skel_post == post_neuron_id] pre_sites, post_sites, connectors = [], [], [] print('Displaying {} of synapses'.format(len(df))) for index, syn in df.iterrows(): pre_site = np.flip(syn.location_pre) post_site = np.flip(syn.location_post) pre_sites.append( neuroglancer.EllipsoidAnnotation(center=pre_site, radii=(radius, radius, radius), id=next(ngid))) post_sites.append( neuroglancer.EllipsoidAnnotation(center=post_site, radii=(radius, radius, radius), id=next(ngid))) connectors.append( neuroglancer.LineAnnotation(point_a=pre_site, point_b=post_site, id=next(ngid))) s.layers['synlinks_{}'.format(name)] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color=color, annotations=connectors, )
def add_ellipsoid_annotations(viewer, centers, radii, layer_name, color=(1, 1, 1)): extant_layers = [l.name for l in viewer.state.layers] if layer_name not in extant_layers: add_annotation_layer(viewer, layer_name) print("Adding layer {}".format(layer_name)) with viewer.txn() as s: s.layers[layer_name].annotationColor = cl.to_hex(color) for ind, row in enumerate(centers): s.layers[layer_name].annotations.append( neuroglancer.EllipsoidAnnotation( center=np.array(row, dtype=np.float), radii=np.array(xyz1[ind, :], dtype=np.int), id=neuroglancer.random_token.make_random_token())) set_annotation_color(viewer, layer_name=layer_name, color=color) return viewer
def visualize_synapses_in_neuroglancer(s, synapses, score_thr=-1, radius=30, show_ellipsoid_annotation=False, name='', color='#00ff00'): pre_sites = [] post_sites = [] connectors = [] below_score = 0 neuro_id = 0 for syn in synapses: if syn.score is None: add_synapse = True else: add_synapse = syn.score > score_thr if not add_synapse: below_score += 1 else: pre_site = np.flip(syn.location_pre) post_site = np.flip(syn.location_post) description = f"id: {syn.id}, pre_seg: {syn.id_segm_pre}, post_seg: {syn.id_segm_post}, score: {syn.score}" pre_sites.append( neuroglancer.EllipsoidAnnotation(center=pre_site, radii=(radius, radius, radius), id=neuro_id + 1)) post_sites.append( neuroglancer.EllipsoidAnnotation(center=post_site, radii=(radius, radius, radius), id=neuro_id + 2)) connectors.append( neuroglancer.LineAnnotation(point_a=pre_site, point_b=post_site, id=neuro_id + 3, description=description)) neuro_id += 3 s.layers['connectors_{}'.format(name)] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color=color, annotations=connectors, ) if show_ellipsoid_annotation: s.layers['pre_sites'] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color='#00ff00', annotations=pre_sites, ) s.layers['post_sites'] = neuroglancer.AnnotationLayer( voxel_size=(1, 1, 1), filter_by_segmentation=False, annotation_color='#ff00ff', annotations=post_sites, ) print('filtered out {}/{} of synapses'.format(below_score, len(synapses))) print('displaying {} synapses'.format(len(post_sites))) return synapses
except KeyError: pass data_k["edge_connectors"] = edge_connectors if nodes: maxima = [] maxima_dict = {} if not selected_only: for node_id, node_data in nodes.items(): x = node_data[2] y = node_data[1] z = node_data[0] maxima_dict[node_id] = (x, y, z) maxima.append( neuroglancer.EllipsoidAnnotation(center=(x, y, z), radii=(tuple([10] * 3)), id=node_id, segments=None)) data_k["maxima"] = maxima else: for node_id in nodes_in_edges: x = nodes[node_id][2] y = nodes[node_id][1] z = nodes[node_id][0] maxima_dict[node_id] = (x, y, z) maxima.append( neuroglancer.EllipsoidAnnotation(center=(x, y, z), radii=(tuple([10] * 3)), id=node_id, segments=None)) data_k["maxima"] = maxima
print(array.roi) print(array.voxel_size) add_layer(s, array, dataset) if args.graphs: for f, graphs in zip(args.file, args.graphs): for graph in graphs: graph_annotations = [] ids = daisy.open_ds(f, graph + '-ids') loc = daisy.open_ds(f, graph + '-locations') for i, l in zip(ids.data, loc.data): graph_annotations.append( neuroglancer.EllipsoidAnnotation(center=l[::-1], radii=(5, 5, 5), id=i)) graph_layer = neuroglancer.AnnotationLayer( annotations=graph_annotations, voxel_size=(1, 1, 1)) with viewer.txn() as s: s.layers.append(name='graph', layer=graph_layer) url = str(viewer) print(url) if os.environ.get("DISPLAY") and not args.no_browser: webbrowser.open_new(url) print("Press ENTER to quit") input()
def vis_points_with_array(raw: np.ndarray, points: nx.DiGraph, voxel_size: np.ndarray): ngid = itertools.count(start=1) neuroglancer.set_server_bind_address("0.0.0.0") viewer = neuroglancer.Viewer() nodes = [] edges = [] for node_a, node_b in points.edges: a = points.nodes[node_a]["location"][::-1] b = points.nodes[node_b]["location"][::-1] pos_u = a pos_v = b nodes.append( neuroglancer.EllipsoidAnnotation(center=pos_u, radii=(3, 3, 3) / voxel_size, id=next(ngid))) edges.append( neuroglancer.LineAnnotation(point_a=pos_u, point_b=pos_v, id=next(ngid))) nodes.append( neuroglancer.EllipsoidAnnotation(center=pos_v, radii=(1, 1, 1) / voxel_size, id=next(ngid))) print(raw.shape) max_raw = np.max(raw) min_raw = np.min(raw) diff_raw = max_raw - min_raw try: raw = ((raw - min_raw) / float(diff_raw) * 255).astype("uint8") except Exception as e: print(min_raw, max_raw) raise e with viewer.txn() as s: s.layers["raw"] = neuroglancer.ImageLayer( source=neuroglancer.LocalVolume(data=raw.transpose([2, 1, 0]), voxel_size=voxel_size)) s.layers["edges"] = neuroglancer.AnnotationLayer( voxel_size=voxel_size, filter_by_segmentation=False, annotation_color="#add8e6", annotations=edges, ) s.layers["nodes"] = neuroglancer.AnnotationLayer( voxel_size=voxel_size, filter_by_segmentation=False, annotation_color="#ff00ff", annotations=nodes, ) position = np.array(raw.shape) // 2 s.navigation.position.voxelCoordinates = tuple(position) print(viewer) input("done?")
voxel_size_rounded = np.array((10, 3, 3)[::-1]) nodes = [] edges = [] print(len(neuron_graph.nodes)) for node_a, node_b in neuron_graph.edges: a = swc_to_voxel_coords(neuron_graph.nodes[node_a]["location"], origin, spacing) b = swc_to_voxel_coords(neuron_graph.nodes[node_b]["location"], origin, spacing) pos_u = a pos_v = b nodes.append( neuroglancer.EllipsoidAnnotation( center=pos_u, radii=(3, 3, 3) / voxel_size, id=next(ngid) ) ) edges.append( neuroglancer.LineAnnotation(point_a=pos_u, point_b=pos_v, id=next(ngid)) ) if len(nodes) > 10000: break nodes.append( neuroglancer.EllipsoidAnnotation( center=pos_v, radii=(1, 1, 1) / voxel_size, id=next(ngid) ) ) a = daisy.open_ds(str(n5_path.absolute()), "volume")