コード例 #1
0
ファイル: spt.py プロジェクト: albayenes/SPT_priors
def find_ROI_nodes(graph,atlas,roi_label,use_wm=[False,False]):
	""" Computes the nodes set for each ROI.
	    
	    Parameters
 		----------
 		graph: RCSP Graph (mandatory)
	    	Graph in which to perform the tractography.
	    atlas: numpy float array (mandatory)
	    	3-D array holding the labels for the ROIs in voxel coordinates.
	    label: [int,int] (mandatory)
			Labels of the two endpoint ROIs.
	    use_wm: [wm,wm] where wm is boolean
	    	Whether to use WM voxels (nodes) of the ROIs. (Default: [False,False])
	    
	    Returns
		-----------
		roi1_nodes: [node1,node2,...]
			List of node IDs for first ROI.
		roi2_nodes: [node1,node2,...]
			List of node IDs for second ROI.
	"""
	
	n = graph.no_of_nodes() #number of nodes in the graph
	
	#select endpoint ROI voxels
	roi1_nodes = [] #source region
	roi2_nodes = [] #target region
	for node in range(0,n):
		tissue = graph.node(node).properties['tissue'][0]
		#coordinates
		x = int(graph.node(node).properties['position'][0])
		y = int(graph.node(node).properties['position'][1])
		z = int(graph.node(node).properties['position'][2])
		if  tissue == float(tissue2int('GM')):
			if atlas[x,y,z] == roi_label[0]:
				roi1_nodes.append(node)
			if atlas[x,y,z] == roi_label[1]:
				roi2_nodes.append(node)
		elif tissue == float(tissue2int('WM')):
			if atlas[x,y,z] == roi_label[0] and use_wm[0]:
				roi1_nodes.append(node)
			if atlas[x,y,z] == roi_label[1] and use_wm[1]:
				roi2_nodes.append(node)
	
	#make sure to compute shortest paths for smaller region
	if len(roi1_nodes) > len(roi2_nodes):
		temp = roi1_nodes
		roi1_nodes = roi2_nodes
		roi2_nodes = temp
	
	return roi1_nodes, roi2_nodes
コード例 #2
0
ファイル: brain_graph.py プロジェクト: albayenes/SPT_priors
def convert_graph(graph,prior=None,mask=None,atlas=None):
	""" Convert the brain graph from python to C format, optionally including WM probabilities and ROI labels.\n\
	    Note: edge weights of the given graph are assumed to not contain any prior information!
	    
	    Parameters
 		----------
 		graph: Graph in python or binary format. (mandatory)
	    prior: numpy array
	    	Holds the prior probability information in a float 3D-array.
	    mask: numpy array
	    	Holds binary inclusion/waypoint prior information in a float 3D-array.
	    atlas: numpy array
			Holds a region label for each voxel in a float 3D-array.
	    
	    Returns
		-----------
		new_graph: Graph in binary format.
	"""	
		
	#map from current node IDs to [0, number of nodes) interval
	id_to_idx = {}
    #collect nodes
	nodes = []
	#current IDs of the nodes
	if type(graph) is dict: nodelist = graph['nodes'].keys() #python format
	elif type(graph) is Graph: nodelist = range(0,graph.no_of_nodes()) #binary format
	for i, node_id in enumerate(nodelist):
		id_to_idx[node_id] = i
		#python format
		if type(graph) is dict:
			node = graph['nodes'][node_id]
			#node coordinates
			x = node['x']
			y = node['y']
			z = node['z']
			tissue = tissue2int(node['tissue'])
			cur_roi = node['region']
		#binary format
		elif type(graph) is Graph:
			node = graph.node(node_id)
			#node coordinates
			x = int(node.properties['position'][0])
			y = int(node.properties['position'][1])
			z = int(node.properties['position'][2])
			tissue = node.properties['tissue'][0]
			cur_roi = int(node.properties['region'][0])
			
		#ROI label information
		if atlas is not None: roi = atlas[x,y,z] #overwrite current label with new label
		else: roi = cur_roi #keep current label
		#prior information
		if prior is not None: #new prior
			prob = prior[x,y,z]
			#set probability for GM and WM voxels low but above 0 to avoid unconnected nodes
			if prob == 0. and (tissue == 2 or tissue == 3): prob = 10**-10
		else: prob = 1
		#apply binary mask to prior
		if mask is not None: prob *= mask[x,y,z]
		#set the properties for current node
		properties = {
            'position': Property([ float(x), float(y), float(z) ]),
			'tissue': Property(tissue),
			'region': Property(float(roi)),
			'prior': Property(float(prob))
        	}
		nodes.append(Node(i, properties))
    #collect edges 
	edges = []
	for source_id in nodelist:
		sid = id_to_idx[source_id] #new ID of node
		edge_list = [] #edges of current node
		#neighbor nodes
		if type(graph) is dict: target_list = graph['W'][source_id].keys() #python format
		elif type(graph) is Graph: target_list = [e.node for e in graph.edges(source_id)] #binary format
		for target_id in target_list:
			tid = id_to_idx[target_id] #new ID of neighbor node
			#edge weight
			if type(graph) is dict: ew = graph['W'][source_id][target_id] #python format
			elif type(graph) is Graph: ew = graph.edge(source_id,target_id).weight #binary format
			if ((prior is not None) or (mask is not None)): #edges need to be updated if prior changed or mask was applied
				prob1 = nodes[sid].properties['prior'][0]
				prob2 = nodes[tid].properties['prior'][0]
				if (prob1>0 and prob2>0): #not removed by mask
					#include prior of endnodes in the edge weight
					if prior is not None: ew -= math.log(math.sqrt(prob1) * math.sqrt(prob2))
					edge_list.append(Edge(tid, ew)) #keep old edge weight
			else: edge_list.append(Edge(tid, ew)) #keep old edge weight
		edges.append(edge_list)  #add node's edges to list of edges
	
	#create new graph
	new_graph = Graph(nodes, edges)
	return new_graph