def volume(self): niter = iterators.PreOrderIter(self._root) volume1 = [] for tn in niter: if tn.is_regular() and tn.parent.is_regular(): volume1.append(tn._volume(tn.parent)) return ([sum(volume1), max(volume1), min(volume1)])
def Bif_tilit_local(self): p_distance=0.0 c_distance=0.0 #pdb.set_trace() if self.ntype()==1: return 0 else: if len(self.children)==0: return 0 else: pnode=self.parent niter = iterators.PreOrderIter(self)#self is a node for tn in niter: cnode=tn.children[0] break p_distance=self.distance(pnode) c_distance=self.distance(cnode) t_distance=pnode.distance(cnode) # print(p_distance,c_distance,t_distance) if c_distance*p_distance!=0: Bif_tilit_local=math.acos((np.square(p_distance)+np.square(c_distance)-np.square(t_distance))/(2*p_distance*c_distance)) return Bif_tilit_local else: return 0
def surface(self): niter = iterators.PreOrderIter(self._root) surface1 = [] for tn in niter: if tn.is_regular() and tn.parent.is_regular(): surface1.append(tn._surface(tn.parent)) return ([sum(surface1), max(surface1), min(surface1)])
def Bif_tilit_remote(self): p_distance = 0.0 c_distance = 0.0 cd_storage = [] if self.ntype() == 1: return 0 else: if not self.children: return 0 else: pnode = self.parent niter = iterators.PreOrderIter(self) #self is a node for tn in niter: # pdb.set_trace() if len(tn.children) > 1: cnode = tn.parent c_distance = self.distance(cnode) #find the first tip p_distance = self.distance(pnode) # c_distance=max(cd_storage) #t_distance=pnode.distance(self.node_from_id(cd_storage.index(c_distance))) t_distance = pnode.distance(cnode) if c_distance * p_distance != 0: Bif_tilit_remote = math.acos( (np.square(p_distance) + np.square(c_distance) - np.square(t_distance)) / (2 * p_distance * c_distance)) break else: return 0 else: Bif_tilit_remote = 0 return Bif_tilit_remote
def length(self): niter = iterators.PreOrderIter(self._root) result = 0 for tn in niter: result += tn.parent_distance() return result
def save(self, path): with open(path, 'w') as fp: niter = iterators.PreOrderIter(self._root) for tn in niter: if tn.is_regular(): fp.write('%s %d\n' % (tn.to_swc_str(), tn.get_parent_id())) fp.close()
def Bif_ampl_local(self): p_distance = 0.0 c_distance = 0.0 count = 0 #self is a node if len(self.children) < 2: return 0 else: niter = iterators.PreOrderIter(self) for tn in niter: pnode = tn.parent if pnode == self: count = count + 1 p2_distance = tn.distance(pnode) if count != 2: p1_distance = p2_distance firstchild = tn else: secondchild = tn break t_distance = secondchild.distance(firstchild) #pdb.set_trace() Bif_ampl_local = math.acos( (np.square(p1_distance) + np.square(p2_distance) - np.square(t_distance)) / (2 * p1_distance * p2_distance)) return Bif_ampl_local
def Bif_ampl_remote(self): p_distance = 0.0 c_distance = 0.0 count = 0 # if self.ntype()==1: # return 0 if self.ntype() == 1: return 0 else: pnode = self.parent while len(pnode.children) < 2: pnode = pnode.parent if pnode.ntype() == 1: return 0 break #pdb.set_trace() for pc in pnode.children: niter = iterators.PreOrderIter(pc) for tn in niter: if len(tn.children) != 1: count = count + 1 p2_distance = tn.distance(pnode) if count != 2: p1_distance = p2_distance firstchild = tn else: secondchild = tn break t_distance = secondchild.distance(firstchild) #pdb.set_trace() Bif_ampl_remote = math.acos( (np.square(p1_distance) + np.square(p2_distance) - np.square(t_distance)) / (2 * p1_distance * p2_distance)) return Bif_ampl_remote
def children(self): children_list = list() niter = iterators.PreOrderIter(self) #self is a node for tn in niter: if regular: if tn.is_regular(): children_list.append(tn) #else: return tuple(children_list)
def tip_count(self, regular=True): count = 0 niter = iterators.PreOrderIter(self._root) for tn in niter: if regular: if tn.is_regular(): if len(tn.children) == 0: count += 1 return count
def stem_count(self, regular=True): count = 0 niter = iterators.PreOrderIter(self) for tn in niter: if regular: if tn.is_regular(): if tn._type == 1: count += len(tn.children) if tn.parent._type == 1: count -= 1 return count
def child_count(self): count = 0 niter = iterators.PreOrderIter(self) #self is a node for tn in niter: if regular: if tn.is_regular(): count += 1 else: count += 1 return count
def tip_count(self, regular = True): count = 0 niter = iterators.PreOrderIter(self._root) for tn in niter: if regular: if tn.is_regular(): if not tn.children: count += 1 # print ("Node %d is a tip" %tn ) # print(count) return count
def node_count(self, regular=True): count = 0 niter = iterators.PreOrderIter(self._root) for tn in niter: if regular: if tn.is_regular(): count += 1 else: count += 1 return count
def ave_diameter(self, regular=True): total = self.node_count() count = 0.0 niter = iterators.PreOrderIter(self._root) for tn in niter: if regular: if tn.is_regular(): count += tn._radius else: count += tn._radius return count * 2 / total
def avrg_diameter(self, regular=True): #for node sum_diameter = 0.0 avrg_diameter = 0.0 if len(self.children) == 0: return 0 else: niter = iterators.PreOrderIter(self) #self is a node for tn in niter: if regular: if tn.is_regular(): sum_diameter = sum_diameter + 2 * tn.radius() avrg_dmt = sum_diameter / len(self.children) return avrg_dmt
def depth(self, atype=None): xmax = xmin = 0.0 niter = iterators.PreOrderIter(self._root) for tn in niter: if atype == None: xmax = max(xmax, tn._pos[2]) xmin = min(xmin, tn._pos[2]) else: if tn._type == atype: xmax = max(xmax, tn._pos[2]) xmin = min(xmin, tn._pos[2]) print(xmax, xmin) result = xmax - xmin return result
def diameter_rate(self, regular=True): #for node sum_rate = 0.0 sum_radius = 0.0 pradius = self.radius() if len(self.children) == 0: return 0 else: niter = iterators.PreOrderIter(self) #self is a node for tn in niter: if regular: if tn.is_regular(): sum_radius = sum_radius + tn.radius() sum_rate = sum_radius / pradius return sum_rate / (len(self.children))
def delete_minpath(self, persent): assert persent < 1.0 maxdist = self.pathdistance()[1] mindist = self.pathdistance()[2] count = 0 while mindist / maxdist < persent: niter = iterators.PreOrderIter(self._root) for tn in niter: if tn.is_regular() and len( tn.children) == 0 and compute_pathdist(tn) == mindist: tn.cutoff() count += 1 maxdist = self.pathdistance()[1] mindist = self.pathdistance()[2] self.cutcount = count print('Compelete!Delete %d nodes in all' % (count))
def pathdistance(self, disttype='normal'): #compute the total/maximun/minimum/average of path distance pathdist = 0 allpathdist = [] alltif = [] niter = iterators.PreOrderIter(self._root) for tn in niter: if tn.is_regular(): if len(tn.children) ==0 : alltif.append(tn) for tif in alltif: if tif._type != 1: if disttype == 'normal': pathdist = compute_pathdist(tif) if disttype == 'euc': pathdist = compute_eucdist(tif) allpathdist.append(pathdist) return([sum(allpathdist),max(allpathdist),min(allpathdist),sum(allpathdist)/len(allpathdist)])
def HillmanThreshold(self,regular=True): count=0 if len(self.children)<=1: return 0 else: niter = iterators.PreOrderIter(self)#self is a node #pdb.set_trace() for tn in niter: if regular: if tn.is_regular(): pnode=tn.parent if pnode==self: count=count+1 if count!=2: firstchild=tn else: secondchild=tn break hillman=0.5*self.radius()+0.25*firstchild.radius()+0.25*secondchild.radius() return hillman
def scale(self, sx, sy, sz, adjusting_radius=True): niter = iterators.PreOrderIter(self._root) for tn in niter: tn.scale(sx, sy, sz, adjusting_radius)
def DC_ego(input_adj, comm_top, comm_below, depth, directed_flag, C_max, nworker, verbose, ordered, output_filename): global node_number if not ordered: input_adj = fix_adj(input_adj) layer_index = 0 ####################################################################### ################### Form networkx graph ######################### ####################################################################### if directed_flag: G = GEN.edgelist2networkxG_directed(input_adj) else: G = GEN.edgelist2networkxG_undirected(input_adj) if C_max < 1: C_max = int(C_max * len(G.nodes())) print 'Network size = ' + str(len(G.nodes())) + ' nodes' print 'Max community resolution = ' + str(C_max) + ' nodes' child_index = 1 layer0 = Node('node0') layer_index = 0 dict_node2member['node' + str(node_number)] = G.nodes() parent_name = 'node' + str(node_number) start = timeit.default_timer() ################################################################### ########### Call hierarchy recursively ################ ################################################################### node_number_now = hierarchy(G, comm_top, layer_index, child_index, layer0, input_adj, comm_below, depth, C_max, directed_flag, nworker, verbose) stop = timeit.default_timer() print 'Time_DC_egoten= ' print(stop - start) ################################################################### ################### CLEAN UP ######################## ################################################################### for item in glob.glob(input_adj + '*.tns'): os.remove(item) for item in glob.glob(input_adj + '*.mat'): os.remove(item) for item in glob.glob(input_adj + '*CPD.txt'): os.remove(item) ######################################################################### ####### Write lit of nodes in detected communities ########### ######################################################################### list_of_leaves = [ node.name for node in iterators.PreOrderIter(layer0) if node.is_leaf ] print 'Total number of detected communities = ' + str(len(list_of_leaves)) f = open(output_filename, 'w') f.close() with open(output_filename, 'a') as f: for leaf in list_of_leaves: str_commnumber = str(leaf) f.write('#community' + str_commnumber[4:] + '\n ') for node in dict_node2member[ leaf]: # node refers to tree node and members are the members in this community (node) f.write(str(node) + ' ') f.write('\n') f.close()
def node_from_id(self, nid): niter = iterators.PreOrderIter(self._root) for tn in niter: if tn.get_id() == nid: return tn return None