def objecttree_get_all_skeletons(request, project_id=None, node_id=None): """ Retrieve all skeleton ids for a given node in the object tree """ g = get_annotation_graph( project_id ) potential_skeletons = nx.bfs_tree(g, int(node_id)).nodes() result = [] for node_id in potential_skeletons: if g.node[node_id]['class'] == 'skeleton': result.append( node_id ) json_return = json.dumps({'skeletons': result}, sort_keys=True, indent=4) return HttpResponse(json_return, mimetype='text/json')
def convert_annotations_to_networkx(request, project_id=None): g = get_annotation_graph( project_id ) data = json_graph.node_link_data(g) json_return = json.dumps(data, sort_keys=True, indent=4) return HttpResponse(json_return, mimetype='text/json')
def get_skeleton_as_dataarray(project_id=None, skeleton_id=None): # retrieve all treenodes for a given skeleton if skeleton_id is None: qs = Treenode.objects.filter( project=project_id).order_by('id') else: qs = Treenode.objects.filter( skeleton=skeleton_id, project=project_id).order_by('id') treenode_count = qs.count() treenode_xyz = np.zeros( (treenode_count, 3), dtype = np.float32 ) treenode_parentid = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_id = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_radius = np.zeros( (treenode_count,), dtype = np.int32 ) treenode_confidence = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_userid = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_type = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_skeletonid = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_connectivity = np.zeros( (treenode_count, 2), dtype = np.uint32 ) treenode_connectivity_type = np.zeros( (treenode_count,), dtype = np.uint32 ) treenode_connectivity_skeletonid = np.zeros( (treenode_count,), dtype = np.uint32 ) row_count = 0 parents = 0 for i,tn in enumerate(qs): treenode_xyz[i,0] = tn.location.x treenode_xyz[i,1] = tn.location.y treenode_xyz[i,2] = tn.location.z treenode_id[i] = tn.id treenode_radius[i] = tn.radius treenode_confidence[i] = tn.confidence treenode_userid[i] = tn.user_id treenode_skeletonid[i] = tn.skeleton_id if not tn.parent_id is None: treenode_parentid[i] = tn.parent_id treenode_type[i] = VerticesTypeSkeletonNode['id'] # only here save it and increment the row_count treenode_connectivity_type[row_count] = ConnectivityNeurite['id'] treenode_connectivity_skeletonid[row_count] = treenode_skeletonid[i] treenode_connectivity[row_count,0] = treenode_id[i] treenode_connectivity[row_count,1] = treenode_parentid[i] row_count += 1 else: treenode_type[i] = VerticesTypeSkeletonRootNode['id'] parents+=1 # correct for too many rows because of empty root relationship treenode_connectivity = treenode_connectivity[:-parents,:] treenode_connectivity_type = treenode_connectivity_type[:-parents] treenode_connectivity_skeletonid = treenode_connectivity_skeletonid[:-parents] if skeleton_id is None: qs_tc = TreenodeConnector.objects.filter( project=project_id, relation__relation_name__endswith = 'synaptic_to', ).select_related('treenode', 'connector', 'relation') else: qs_tc = TreenodeConnector.objects.filter( project=project_id, skeleton=skeleton_id, relation__relation_name__endswith = 'synaptic_to', ).select_related('connector', 'relation__relation_name') treenode_connector_connectivity=[]; treenode_connector_connectivity_type=[] cn_type=[]; cn_xyz=[]; cn_id=[]; cn_confidence=[]; cn_userid=[]; cn_radius=[]; cn_skeletonid=[] cn_skeletonid_connector=[] # because skeletons with a single treenode might have no connectivity # (no parent and no synaptic connection), but we still want to recover their skeleton id, we need # to store the skeletonid as a property on the vertices too, with default value 0 for connectors found_synapse=False for tc in qs_tc: if tc.relation.relation_name == 'presynaptic_to': treenode_connector_connectivity_type.append( ConnectivityPresynaptic['id'] ) found_synapse=True elif tc.relation.relation_name == 'postsynaptic_to': treenode_connector_connectivity_type.append( ConnectivityPostsynaptic['id'] ) found_synapse=True else: print >> std.err, "non-synaptic relation found: ", tc.relation.relation_name continue treenode_connector_connectivity.append( [tc.treenode_id,tc.connector_id] ) # !!! # also need other connector node information cn_xyz.append( [tc.connector.location.x, tc.connector.location.y, tc.connector.location.z] ) cn_id.append( tc.connector_id ) cn_confidence.append( tc.connector.confidence ) cn_userid.append( tc.connector.user_id ) cn_radius.append( 0 ) # default because no radius for connector cn_skeletonid_connector.append( 0 ) # default skeleton id for connector cn_type.append( VerticesTypeConnectorNode['id'] ) cn_skeletonid.append( tc.skeleton_id ) data = {'vert':{},'conn':{}} # check if we have synaptic connectivity at all if found_synapse: data['vert'] = { 'id': np.hstack((treenode_id.T, np.array(cn_id, dtype=np.uint32))), 'location': np.vstack((treenode_xyz, np.array(cn_xyz, dtype=np.uint32))), 'type': np.hstack((treenode_type, np.array(cn_type, dtype=np.uint32).ravel() )), 'confidence': np.hstack((treenode_confidence, np.array(cn_confidence, dtype=np.uint32).ravel() )), 'userid': np.hstack((treenode_userid, np.array(cn_userid, dtype=np.uint32).ravel() )), 'radius': np.hstack((treenode_radius, np.array(cn_radius, dtype=np.int32).ravel() )), 'skeletonid': np.hstack((treenode_skeletonid, np.array(cn_skeletonid_connector, dtype=np.int32).ravel() )) } # print np.array(cn_skeletonid, dtype=np.uint32) data['conn'] = { 'id': np.vstack((treenode_connectivity, np.array(treenode_connector_connectivity, dtype=np.uint32)) ), 'type': np.hstack((treenode_connectivity_type, np.array(treenode_connector_connectivity_type, dtype=np.uint32).ravel() )), 'skeletonid': np.hstack((treenode_connectivity_skeletonid.T, np.array(cn_skeletonid, dtype=np.uint32).ravel() ) ) } else: data['vert'] = { 'id': treenode_id, 'location': treenode_xyz, 'type': treenode_type, 'confidence': treenode_confidence, 'userid': treenode_userid, 'radius': treenode_radius, 'skeletonid': treenode_skeletonid } data['conn'] = { 'id': treenode_connectivity, 'type': treenode_connectivity_type, 'skeletonid': treenode_connectivity_skeletonid } # no connprop type # add metadata field with mapping from skeleton id to names of the hierarchy g = get_annotation_graph( project_id ) skeletonmap={} if skeleton_id is None: allskeletonids = [nid for nid,di in g.nodes_iter(data=True) if di['class']=='skeleton'] else: allskeletonids = [skeleton_id] rid = [nid for nid,di in g.nodes_iter(data=True) if di['class']=='root'] maxiter = 10 for id in allskeletonids: outstr = '' iterat = 0 currid = [id] while currid[0] != rid[0] and iterat < maxiter: currid = g.predecessors(currid[0]) outstr = g.node[currid[0]]['name']+'|'+outstr iterat+=1 skeletonmap[id] = outstr.rstrip('|') data['meta'] = skeletonmap return data