예제 #1
0
파일: mlst.py 프로젝트: calebwang/mlst
def fast_approximate_solution_two(graph):
    """
    Given a graph, construct a solution greedily using approximation methods.
    Performs bad.
    """
    new_graph = nx.Graph()
    degrees = nx.degree_centrality(graph) 
    largest = argmax(degrees)
    new_graph.add_node(largest)
    while new_graph.number_of_edges() < graph.number_of_nodes() - 1:
        degrees = {n: count_uncovered_degree(graph, new_graph, n) for n in nx.nodes(graph)}
        neighbor_list = [nx.neighbors(graph, n) for n in new_graph.nodes()]
        neighbors = set()
        for lst in neighbor_list:
            neighbors = neighbors.union(lst)
        if not neighbors:
            break
        next_largest = argmax_in(degrees, neighbors, exceptions = new_graph.nodes())
        possible_edge_ends = [n for n in nx.neighbors(graph, next_largest) 
                              if graph.has_edge(n, next_largest) 
                              and n in new_graph.nodes()]
        new_graph.add_node(next_largest)
        edge_end = argmax_in(degrees, possible_edge_ends)
        new_graph.add_edge(edge_end, next_largest)

    return new_graph
def main():    
    universe = nx.read_graphml(sys.argv[1])
    
    beings = filter(lambda x: x[1]["type"] == "Being", universe.nodes(data=True))
    clients = filter(lambda x: x[1]["type"] == "client", universe.nodes(data=True))
    firm = filter(lambda x: x[1]["type"] == "firm", universe.nodes(data=True))        
    print len(beings)
    print len(clients)
    print len(firm)
    
    for b in beings:
        ns = nx.neighbors(universe,b[0])
        rep = ns[0]
        for n in ns[1:]:
            for nn in nx.neighbors(universe,n):
                universe.add_edge(rep,nn) #doesn't preserve directions or properties, yolo
            universe.remove_node(n)
        universe.remove_node(b[0])
        
    beings = filter(lambda x: x[1]["type"] == "Being", universe.nodes(data=True))
    clients = filter(lambda x: x[1]["type"] == "client", universe.nodes(data=True))
    firm = filter(lambda x: x[1]["type"] == "firm", universe.nodes(data=True))        
    print len(beings)
    print len(clients)
    print len(firm)
            
    nx.write_graphml(universe,"simplified-{}.graphml".format(int(time.time())))
예제 #3
0
    def chooseNodesByDistributionOld(self):
#        try:
        self.nodesToChange = [] #reset from previous turns 
        #choose first node based on distribution
        
        rand = random.random()
        cumProb = 0.0
        currIndex = 0
        currNode = self.controlledNodes[currIndex]
        cumProb = cumProb+currNode[1]
        while rand>cumProb:
            currIndex = currIndex+1
            currNode = self.controlledNodes[currIndex]
            cumProb = cumProb+currNode[1]
        chosenNode = copy.deepcopy(currNode[0])
        currNode = chosenNode
        self.nodesToChange.append(chosenNode)
        #get remaining nodes from neighbors
        for i in range(self.actionLimit-1): #TODO: consider randomizing number of nodes chosen; consider prioritizing just neighbors of first node
            newNode = random.sample(nx.neighbors(self.knownGraph, currNode),1)
            while ((len(newNode)==0)):
                newNode = random.sample(nx.neighbors(self.knownGraph, currNode),1)
                if newNode in self.nodesToChange:
                    print 'why?'
            self.nodesToChange.append(newNode[0])
            currNode = newNode[0]
        for i in range(len(self.nodesToChange)):
            for j in range(i+1,len(self.nodesToChange)):
                if self.nodesToChange[i] == self.nodesToChange[j]:
                    print 'problem'
       
        return self.nodesToChange    
예제 #4
0
파일: openpg.py 프로젝트: clemej/libopenpg
	def hinges(self):
		""" Return a list of all detected hinge nodes """
		ret = []
		# Consider a node a /possible/ hinge if it meets two criteria:
		# - it has a degree of at least 3
		# - it is in two or more visible faces
		for node in [x for x in self.graph.nodes_iter() 
				if len(nx.neighbors(self.graph, x)) > 3]:

			neighbors = nx.neighbors(self.graph, node)
			faces = set()
			#print node
			for n in neighbors:
				f1 = self.graph[node][n]['face']
				f2 = self.graph[n][node]['face']
				if f1.visible:
					faces.add(f1)
				if f2.visible:
					faces.add(f2)

			if len(faces) < 2:
				continue
			
			#print 'sending to examine_hings'
			result,on = self._examine_hinge(node)
			if len(result) > 3:
				#pprint.pprint(on)
				#pprint.pprint(result)
				ret.append(node)
		return ret 
예제 #5
0
def all_children_of(tree, A):
	""" returns all the children of A """
	front = []
	children = []
	nbrs = nx.neighbors(tree, A)
	
	for n in nbrs:		
		if n.i_time > A.i_time:
			front.append(n)
			children.append(n) 

	if len(front) < 1:
		return children
	
	while 1:
		new_front = []
		if len(front) < 1:
			return children
		for a in front:
			nbrs = nx.neighbors(tree, a)
			for n in nbrs:
				if n.i_time > a.i_time:
					new_front.append(n) 
					children.append(n)
		front = new_front
예제 #6
0
def ministro_ministro(G):
    """
    Cria um grafo de ministros conectados de acordo com a sobreposição de seu uso da legislação
    Construido a partir to grafo ministro_lei
    """
    GM = nx.Graph()
    for m in G:
        try:
            int(m)
        except ValueError:# Add only if node is a minister
            if m != "None":
                GM.add_node(m.decode('utf-8'))
#    Add edges
    for n in GM:
        for m in GM:
            if n == m: continue
            if GM.has_edge(n,m) or GM.has_edge(m,n): continue
            # Edge weight is the cardinality of the intersection each node neighbor set.
            w = len(set(nx.neighbors(G,n.encode('utf-8'))) & set(nx.neighbors(G,m.encode('utf-8')))) #encode again to allow for matches
            if w > 5:
                GM.add_edge(n,m,{'weight':w})
    # abreviate node names
    GMA = nx.Graph()
    GMA.add_weighted_edges_from([(o.replace('MIN.','').strip(),d.replace('MIN.','').strip(),di['weight']) for o,d,di in GM.edges_iter(data=True)])
    P.figure()
    nx.draw_spectral(GMA)
    nx.write_graphml(GMA,'ministro_ministro.graphml')
    nx.write_gml(GMA,'ministro_ministro.gml')
    nx.write_pajek(GMA,'ministro_ministro.pajek')
    nx.write_dot(GMA,'ministro_ministro.dot')
    return GMA
예제 #7
0
def cluster_graph( g, colors, current_color = 0 ):
    # Initialize colors
    degrees = []
    for n in g.nodes( ):
        degrees.append( (nx.degree(g, n ), n))

    nodeWithMaxDegree = sorted( degrees, reverse = True )[0][1]

    # Start with node with maximum degree
    current_color  += 1
    colors[ nodeWithMaxDegree ] = current_color

    numIteration = 0
    pN = nodeWithMaxDegree
    stack = [ nodeWithMaxDegree ]
    while stack:
        neighbours = nx.neighbors( g, stack.pop( ) )
        numIteration += 1
        for n in neighbours:
            if colors[n] > 0:
                continue
            # If most neightbours of n are as close to pN as the value current
            # iteration, then accept this node.
            potentialCandidates = filter(lambda x: colors[x] == 0, nx.neighbors( g, n ))
            # print( "Potential neighbours ", potentialCandidates )
            distance2pN = [ nx.shortest_path_length( g, x, pN) for x in potentialCandidates ]
            goodDist2pN = filter( lambda x: x <= numIteration, distance2pN )
            accepted = False
            if len( goodDist2pN ) > 0:
                accepted = True
                colors[ n ] = current_color 
                stack.append( n )
            # print n, ' -> ', distance2pN, ',', goodDist2pN, 'Accepted', accepted
        # print stack
    return g
예제 #8
0
def RW_Size(G,r = 1000,m=100):
    sampled = []
    now_node = random.choice(G.nodes())
    sampled.append(now_node)
    while True:
        next_node = random.choice(nx.neighbors(G,now_node))
        now_node = next_node
        sampled.append(now_node)
        if len(sampled) >= r:
            break
    print(1)
    lst = []
    for i in range(0,r-m):
        if i+m <= r-1:
            for j in range(i+m,r):
               # l1 = set(nx.neighbors(G,sampled[i]))
               # l2 = set(nx.neighbors(G,sampled[j]))
               # if len(list(l1 & l2)) >= 1:
                lst.append((sampled[i],sampled[j]))
                lst.append((sampled[j],sampled[i]))
    sumA = 0.0
    sumB = 0.0
    print(len(lst))
    for nodes in lst:
        sumA += float(nx.degree(G,nodes[0]))/nx.degree(G,nodes[1])
        l1 = set(nx.neighbors(G,nodes[0]))
        l2 = set(nx.neighbors(G,nodes[1]))
        count = len(list(l1&l2))
        sumB += count/(float(nx.degree(G,nodes[0]))*nx.degree(G,nodes[1]))
    return sumA/sumB
예제 #9
0
    def expand(seed_set):
        members = seed_set
        print 'seed:', members, nx.subgraph(data_graph, set(
            flatten(map(lambda mem: nx.neighbors(data_graph, mem), members))) | members).edges()
        is_change = True
        while is_change:
            to_check_neighbors = list(flatten(map(lambda mem: nx.neighbors(data_graph, mem), members)))
            random.shuffle(to_check_neighbors)
            print to_check_neighbors
            is_change = False
            # for neighbor in to_check_neighbors:
            for neighbor in to_check_neighbors:
                if fitness(members | {neighbor}) > fitness(members):
                    is_change = True
                    members.add(neighbor)
                    fitness(members, is_print=True)
                    print 'add neighbor:', neighbor, members, 'w_in:', w_in, 'w_all:', w_all
                    break

            for member in members:
                if fitness(members - {member}) > fitness(members):
                    is_change = True
                    members.remove(member)
                    fitness(members, is_print=True)
                    print 'remove member:', member, members, 'w_in', w_in, 'w_all:', w_all
                    break
        print set(members)
        print '\n----------------------------\n'
예제 #10
0
def find_sidechains(molecule_graph):
    # Identify chiral atoms
    atoms = molecule_graph.atoms
    chiral_centres = chir.get_chiral_sets(atoms)
    # Identify sidechains (Ca-Cb-X), apart from proline and glycine.
    sidechains = {}
    # Detection of sidechains requires the multiple bonds be present in the atom graph.
    chir.multi_bonds(atoms)
    for k, v in chiral_centres.items():
        carbons = [atom for atom in v if atom.element == "C"]
        amides = [
            carbon
            for carbon in carbons
            if any([type(nb) == chir.GhostAtom and nb.element == "O" for nb in nx.neighbors(atoms, carbon)])
            and any([nb.element == "N" or nb.element == "O" for nb in nx.neighbors(atoms, carbon)])
        ]
        nbs_n = [nb for nb in v if nb.element == "N"]
        if amides and nbs_n:
            amide_bond = (k, amides[0])
            n_bond = (k, nbs_n[0])
            h_bond = (k, [h for h in nx.neighbors(atoms, k) if h.element == "H"][0])
            # Now find sidechains by cutting the Ca-C, Ca-N and Ca-H bonds
            atoms.remove_edges_from([amide_bond, n_bond, h_bond])
            sidechain_atoms = [
                atom
                for atom in [comp for comp in nx.connected_components(atoms) if k in comp][0]
                if type(atom) != chir.GhostAtom
            ]
            atoms.add_edges_from([amide_bond, n_bond, h_bond])
            if not any([k in cycle for cycle in nx.cycle_basis(atoms.subgraph(sidechain_atoms))]):
                sidechains[k] = atoms.subgraph(sidechain_atoms)
    chir.remove_ghost_atoms(atoms)
    return sidechains
예제 #11
0
def m_projection(graph_orig, members, prods, full_graph):
    logging.info('Projecting the graph on members')

    graph = graph_orig.subgraph(graph_orig.nodes())
    #considering only favorable edges
    graph.remove_edges_from([e for e in graph.edges(data=True) if e[2]['starRating'] < 4])
    assert set(graph) == (set(members) | set(prods))

    mg = nx.Graph()
    mg.add_nodes_from(members)

    prods_len = float(len(prods))
    last_pctg = 0
    prod_names = dict()
    for p_i, p in enumerate(prods):
        # first check whether two favorable reviews within a WINDOW is significant enough (p-value < 0.5)
        ts = [e['date'] for e in full_graph[p].values()]
        # In order for gkde to work, there should be more than one point value
        if len(ts) >= MIN_TS_LEN and min(ts) < max(ts):
            gkde = gaussian_kde(ts)
            p_value, err = dblquad(lambda u, v: gkde(u)*gkde(v), min(ts) - WINDOW/2.0, max(ts) + WINDOW/2.0,
                                   lambda v: v - WINDOW/2.0, lambda v: v + WINDOW/2.0)
            if p_value - EPS >= SIGNF_LEVEL and err < EPS:
                continue
        for m1, m2 in itertools.combinations(nx.neighbors(graph, p), 2):
            # order m1,m2 so the key (m1,m2) for prod_names works regardless of edge direction
            if m1 > m2:
                m1, m2 = m2, m1
                #assert m1 in members and m2 in members
            if abs(graph[p][m1]['date'] - graph[p][m2]['date']) < WINDOW:
                if mg.has_edge(m1, m2):
                    c = mg[m1][m2]['weight']
                else:
                    c = 0
                    prod_names[(m1, m2)] = []
                prod_names[(m1, m2)].append(p)
                mg.add_edge(m1, m2, weight=c + 1)
        pctg = int(p_i/prods_len*100)
        if pctg % 10 == 0 and pctg > last_pctg:
            last_pctg = pctg
            logging.info('%d%% Done' % pctg)

    logging.debug('Normalizing edge weights: meet/max')
    for e in mg.edges():
        u, v = e
        if mg[u][v]['weight'] <= 1:
            mg.remove_edge(u, v)
            del prod_names[(min(u, v), max(u, v))]
        else:
            norm = max(len(nx.neighbors(graph, u)), len(nx.neighbors(graph, v)))
            mg.add_edge(u, v, weight=float(mg[u][v]['weight']) / norm, denom=norm)
    # remove isolated nodes
    degrees = mg.degree()
    mg.remove_nodes_from([n for n in mg if degrees[n] == 0])
    # adding original graph metadata on nodes
    for m in mg:
        mg.node[m] = graph_orig.node[m]
    logging.debug(r'Projected Nodes = %d, Projected Edges = %d' % (mg.order(), len(mg.edges())))

    return mg, prod_names
예제 #12
0
	def printroute(self):
		for src in self.nodes.iterkeys():
			print "[Neighors List] neighbor = %s" % (src)
			print nx.neighbors(self.graph, src)
			print "[Shortest Paths] - %s " % (src)
			for dst in self.nodes.iterkeys():
				if src != dst:
					print nx.shortest_path(self.graph, src, dst)
예제 #13
0
def mindeg_GSK(BG, variables_index=0, verbose=False):
    Vprime1 = [];
    Vprime2 = [];
    
    layer = nx.get_node_attributes(BG,'bipartite');
    var = [x for x in BG.nodes() if layer[x] == variables_index]
    fac = [x for x in BG.nodes() if layer[x] != variables_index]
    
    if verbose==True:
        print 'Initial variable nodes:', var;
        print 'Initial factor nodes:', fac;

    isolated_variables = [x for x in BG.nodes() if nx.degree(BG,x)==0 and layer[x]==variables_index];
    [var.remove(x) for x in isolated_variables]
    
    G = BG.copy();
    Vprime1.extend(isolated_variables);
    G.remove_nodes_from(isolated_variables)
    
    isolated_factors = [x for x in G.nodes() if nx.degree(BG,x)==0 and layer[x]!=variables_index];
    [fac.remove(x) for x in isolated_factors]
    G.remove_nodes_from(isolated_factors);

    while len(var)>0:
        if verbose==True:
            print '#var:',len(var),'#fac:', len(fac), '#nodes in depleted graph:', G.number_of_nodes(),'#original BG:',BG.number_of_nodes();

        pendant = return_mindeg_pendant(G,layer,variables_index);
        if len(pendant)==0:
            ## if not, choose randomly and do the game. 
            if verbose==True:
                print var
            m = G.number_of_nodes()*2;
            degs = G.degree();
            for e in G.edges():
                if degs[e[0]] + degs[e[1]] < m:
                    m = degs[e[0]] + degs[e[1]];
                    v = e;
            if e[0] in var:
                v = e[0];
            else:
                v = e[1];
            pendant = []
            pendant.append(v);
            pendant.extend(nx.neighbors(G,v));
            Vprime2.append(pendant[0]);
        else:
            Vprime1.append(pendant[0]);
        augmented_pendant = []
        augmented_pendant.extend(pendant);
        for n in pendant[1:]:
            augmented_pendant.extend(nx.neighbors(G,n));
        augmented_pendant = list(set(augmented_pendant));
        G.remove_nodes_from(augmented_pendant);        
        [var.remove(x) for x in augmented_pendant if x in var];
        [fac.remove(x) for x in augmented_pendant if x in fac];

    return Vprime1,Vprime2;
예제 #14
0
    def __repr__(self):

        st = str(self.rootid)+'\n'
        levels = nx.neighbors(self,self.rootid)
        st = st + '---'+'\n'
        for l in levels:
            nw = len(nx.neighbors(self,l))
            st = st + str(l)+' : '+ str(nw) + '\n'
        return(st)
예제 #15
0
def explore(u, pre):
    pre.append(u)
    
    if len(nx.neighbors(G,u)) == 0:
        Leafs[u]=set(pre)
        return

    for v in nx.neighbors(G, u):
        explore(v, pre[:])
예제 #16
0
    def __repr__(self):

        st = str(self.rootid) + "\n"
        levels = nx.neighbors(self, self.rootid)
        st = st + "---" + "\n"
        for l in levels:
            nw = len(nx.neighbors(self, l))
            st = st + str(l) + " : " + str(nw) + "\n"
        return st
예제 #17
0
def Welsh_Powell(g,nome):
    '''
    Função: Resolver o sudoku, é gerado um arquivo com a resolução
            a resolução também é mostrada no programa
    Parametros: g, grafo de incompatibilidade do sudoku
    Retorno: -
    '''
    #Todos os vértices tem o mesmo grau
    #Atribui uma lista de possíveis "cores" para cada vértice
    ci = []
    for no_atual in range(81):
        #verifica se o no não tem valor
        if g.node[no_atual] == 0:
            g.node[no_atual] = []
            #Lista de vértices não resolvidos
            ci.append(no_atual)
            #recupera o valor que os vizinhos possuem
            valores_vertice = [g.node[v] for v in nx.neighbors(g,no_atual)]
            #Coloca como possível valor do no os valores que não estão presentes em seus 
            #vizinhos
            for possivel_valor in range(1,10):
                if possivel_valor not in valores_vertice:
                    g.node[no_atual].append(possivel_valor)
    
    #Enquanto houver mais de um elemento por nó
    while len(ci):
        #Escolhe um no que não tem seu valor definido
        for v in ci:
            #Escolhe um no que tem só uma possibilidade
            if len(g.node[v]) == 1:
                no_retirado = g.node[v][0]
                g.node[v] = g.node[v][0]                
                ci.remove(v)
                #Remove o valor do no que tinha só uma possibilidade dos vizinhos
                for i in nx.neighbors(g,v):
                    if i in ci and no_retirado in g.node[i]:
                        g.node[i].remove(no_retirado)
    resposta = []        
    for i in range(81):
        resposta.append(g.node[i])
    gravar = int(raw_input('Para guardar a resposta em arquivo digite:\n1 - Sim\n2 - Não\n'))
    #Grafa a resposta em arquivo
    if gravar:
        arq_resposta = open(nome,"w")
        for i in range(81):
            if (i+1) % 9 == 0:
                arq_resposta.write(str(resposta[i])+'\n')
            else:
                arq_resposta.write(str(resposta[i])+" ")
        arq_resposta.close()
    print '\nResposta: '
    #Mostra a resposta no console
    for i in range(81):
        print g.node[i],
        if (i+1) % 9 == 0:
            print 
예제 #18
0
def mhrw_sampling(network, desired_order): 
    ''' MHRV algorithm '''
    
    # variables here
    g = nx.Graph()
    order = 0
    go_more = True
    
    # Try to get the first node that is not isolated 
    trycount = 0
    found_node = False

    while trycount < 10 and not found_node:
        v = random.choice(network.nodes())
        neighbors = nx.neighbors(network, v)
        if not neighbors: 
            trycount += 1
        else: 
            found_node = True

    # if didnt find the node, just return
    if not found_node:       
        return False, g, 0
    
    # now, with the first node, we start our graph
    g.add_node(v)

    while order < desired_order and go_more:
        
        # If don't find more neighboors, but almost have the order
        neighbors = nx.neighbors(network, v)
        if not neighbors:
            if order < 0.9*desired_order:
                return False, g, order
            else:
                return True, g, order

        # If find neighboors keep going
        for i in range(len(neighbors)): 
            g.add_edge(v, neighbors[i])   

        order_new = g.order()
        # If we are too big now (more than 30%), lets try again
        if order_new > 1.3*desired_order:
            return False, g, order

        # or If we reach the order we want 
        if order_new >= desired_order:
            go_more = False
         
        # If nothing happened, keep going...
        v = random.choice(neighbors) 
        order = order_new

    return True, g, order
예제 #19
0
def coefficient(node_i,node_j,G):
    '''
        paper : <<Defining and identifying communities in networks>>
    '''
    neighbors_i = set(nx.neighbors(G, node_i))
    neighbors_j = set(nx.neighbors(G, node_j))
    common = len(set(neighbors_i & neighbors_j))
    min_k = min(len(neighbors_i),len(neighbors_j))-1
    if(min_k == 0):
        return 1
    else:
        return common / min_k
예제 #20
0
    def run3(self,cs,ct,cutoff=1):

        ns = nx.neighbors(self.L.Gt,cs)
        nt = nx.neighbors(self.L.Gt,ct)
        print ns
        print nt
        path=[]
        for s in ns:
            for t in nt:
                p=nx.dijkstra_path(self.L.Gt,s,t)
                if not cs in p and not ct in p:
                    path.append(p)
        return path
예제 #21
0
def CC2(G,node):
    n_list = nx.neighbors(G,node)
    degree = len(n_list)
    if degree <= 1:
        return 0
    tri = 0
    count = 0
    for i in range(0,degree-1):
        n2_list = nx.neighbors(G,n_list[i])
        for j in range(i+1,degree):
            count += 1
            if n_list[j] in n2_list:
                tri += 1
    return float(tri)/count
예제 #22
0
def updateNeighbors(s2,s1,L1,L2,P1,P2,G):    
    L1.append(s1)
    L2.remove(s1)
    L2.append(s2)
    L1.remove(s2)
    
    
    if not hasOutsideNeighbor(s1,P1,G) :
        L1.remove(s1)

    if not hasOutsideNeighbor(s2,P2,G) :
        L2.remove(s2)
    
    N1 = nx.neighbors(G,s1)
    N2 = nx.neighbors(G,s2)

    for node1 in N1 :
        if node1 in P1:
            #print "Nodes",node1,"and",s1,"in the same partition"
            if hasOutsideNeighbor(node1,P1,G) :
                #print "Node", node1, "has a neighbor outside from",P1
                if node1 not in L1 :
                    L1.append(node1)
                    #print "L1 : ajout de", node1
            else :
                if node1 in L1 :
                    L1.remove(node1)
                #print "L1 : suppression de", node1
        else:
            #print "Nodes",node1,"and",s1,"not in the same partition"
            if not hasOutsideNeighbor(node1,P1,G) and node1 in L1:
                L1.remove(node1)
                #print "L1 : suppression de", node1
                
    for node2 in N2 : 
        if node2 in P2 :
            #print "Nodes",node2,"and",s2,"in the same partition"
            if hasOutsideNeighbor(node2,P2,G) :
                #print "Node", node2, "has a neighbor outside from",P2
                if node2 not in L2 :
                    L2.append(node2)
                    #print "L2 : ajout de", node2
            else :
                if node2 in L2 :
                    L2.remove(node2)
                #print "L2 : suppression de", node2
        else:
            #print "Nodes",node2,"and",s2,"not in the same partition"
            if not hasOutsideNeighbor(node2,P2,G) and node2 in L2:
                L2.remove(node2)
예제 #23
0
파일: mlst.py 프로젝트: calebwang/mlst
def approximate_solution(graph):
    """
    Given a graph, construct a solution greedily using approximation methods.
    """
    new_graph = nx.Graph()
    degrees = nx.degree_centrality(graph) 
    largest = argmax(degrees)
    new_graph.add_node(largest)
    while new_graph.number_of_edges() < graph.number_of_nodes() - 1:
        neighbor_list = [nx.neighbors(graph, n) for n in new_graph.nodes()]
        neighbors = set()
        for lst in neighbor_list:
            neighbors = neighbors.union(lst)
        if not neighbors:
            return

        next_largest = argmax_in(degrees, neighbors, exceptions = new_graph.nodes())
        next_largest_list = [n for n in neighbors if (n not in new_graph.nodes()) and degrees[n] == degrees[next_largest]]
        best_edge = None
        best_score = 0
        for n_largest in next_largest_list:

            possible_edge_ends = [n for n in nx.neighbors(graph, n_largest) 
                                  if n in new_graph.nodes()]
            new_graph.add_node(n_largest)

            edge_end = argmax_in(degrees, possible_edge_ends)
            
            new_graph.add_edge(edge_end, n_largest)
            best_subscore = count_leaves(new_graph)
            best_end = edge_end
            new_graph.remove_edge(edge_end, n_largest)

            for end in possible_edge_ends:
                new_graph.add_edge(end, n_largest)
                subscore = count_leaves(new_graph)
                if subscore > best_subscore:
                    best_end = end
                    best_subscore = subscore
                new_graph.remove_edge(end, n_largest)
                
                
            new_graph.remove_node(n_largest)
            if best_subscore > best_score:
                best_edge = (n_largest, best_end)
                best_score = best_subscore
        new_graph.add_edge(best_edge[0], best_edge[1])

    return new_graph
예제 #24
0
파일: mlst.py 프로젝트: calebwang/mlst
def basic_local_search(graph, solution):

    original = solution.copy()
    before = count_leaves(solution)
    best_solution = solution.copy()
    best_leaves = count_leaves(best_solution)
    for i in range(10):
        best = count_leaves(solution)
        candidates = set(get_vertices_with_degree(solution, 2))
        leaves = set(get_vertices_with_degree(solution, 1))
        leaf_neighbors = []
        for leaf in leaves:
            leaf_neighbors.extend(nx.neighbors(solution, leaf))
        leaf_neighbors = set(leaf_neighbors)
        vs = candidates.intersection(leaf_neighbors)
        for v in vs:
            leafs = [l for l in nx.neighbors(solution, v) if l in leaves]
            if leafs:
                leaf = leafs[0]
            else:
                break
            solution.remove_edge(v, leaf)
            neighbors = nx.neighbors(graph, leaf)
            for neighbor in neighbors:
                solution.add_edge(leaf, neighbor)
                new = count_leaves(solution)
                if new > best:
                    best = new
                else:
                    solution.remove_edge(leaf, neighbor)
            if not nx.is_connected(solution):
                solution.add_edge(v, leaf)
        if count_leaves(solution) < best_leaves:
            solution = best_solution.copy()
        elif count_leaves(solution) > best_leaves:
            best_solution = solution.copy()
            best_leaves = count_leaves(best_solution)

    after = count_leaves(solution)
    if before > after:
        solution = original.copy()
    if before != after:
        print 'before/after: ', before, after
    if before > after:
        raise Exception('you dun goofed')
    if not nx.is_connected(solution):
        raise Exception('you dun goofed')
    return solution
예제 #25
0
def triaddist(graph):
  t = {0:0, 1:0, 2:0, 3:0}
  for i in graph.nodes():
    for j in graph.nodes():
      for k in graph.nodes():
        x = 0
        if i in nx.neighbors(graph, j):
          x += 1
        if i in nx.neighbors(graph, k):
          x += 1
        if j in nx.neighbors(graph, k):
          x += 1
        t[x] += 1
  for q in t:
    t[q] /= 6
  return t
def test(rt):
 
 global g
 weight = nx.get_node_attributes(g,"weight")
 #print "from test :" , weight
 print rt
 weights=weight.values()
 node_words=weight.keys()
 
 parent_node = rt
 question_string = rt 
 neighbours_weights=dict()
 prev=""
 for i in range(20):
  neighbours_weights={}
  x=nx.neighbors(g,parent_node)
  print x
  print prev
  try :
   if i!=0:
     x.remove(prev)
   for node in x:
    neighbours_weights[node] = weight[node]
   prev=parent_node
   parent_node = max(neighbours_weights)
   print "Parent node is : ", parent_node
   question_string = question_string + " " + parent_node
   
  except Exception,e:
    print str(e)
예제 #27
0
 def findReachability(self, state, horizon):
     """ Finds a reachable set on the graph.
     
     Inputs: 
         state: starting point of reachability search
         horizon: number of edges away from state to be searched.
     
     Outputs: the set of all states that are reachable from state
              in exactly horizon transitions.
     """
     k = 0
     prevSet = set()
     prevSet.add(state)
     nextSet = set()
     while k < horizon:
         nextSet.clear()
         for node in prevSet:
             nextSet = nextSet.union(set(nx.neighbors(self.graphRepresentation, node)))
         if len(nextSet) == 1 and None in nextSet:
             return None
         if None in nextSet:
             nextSet.remove(None)
         prevSet = copy.deepcopy(nextSet)
         k += 1
     return nextSet
    def install_neighbours(self):
        """
        Install the neighbours information inside the Nodes classes.
        """
        # Initialize neighbours sets as empty sets:
        nodes_nei = [set() for _ in range(self.num_nodes)]

        # Build translation tables between graph nodes
        # and node numbers 1 .. self.num_nodes
        self.graph_to_vec = {}
        self.vec_to_graph = []
        
        for i,gnd in enumerate(self.graph.nodes_iter()):
            self.vec_to_graph.append(gnd)
            self.graph_to_vec[gnd] = i

        for i,nd in enumerate(self.nodes):
            # Sample a set of indices (Which represent a set of nodes).
            # Those nodes will be nd's neighbours:
            gneighbours = nx.neighbors(self.graph,self.vec_to_graph[i])
            nodes_nei[i].update(map(lambda gnei:\
                    self.graph_to_vec[gnei],gneighbours))

            # Make sure that i is not his own neighbour:
            assert i not in nodes_nei[i]

        for i,nd in enumerate(self.nodes):
            # Initialize a list of neighbours:
            nd.set_neighbours(map(self.make_knode,list(nodes_nei[i])))
def compressGraph(graph):
    """ compact the gateway nodes to one single node."""

    """ Each node in the graph with ID < 0 is a 0.0.0.0/0 HNA route (a gateway 
    to the Internet). We compact them in one single node for easier analysis."""

    newGraph = graph.copy()
    if 0 in newGraph.nodes():
        print "We use node id 0 for representing an abstract gateway node",\
        "please do not use ID 0 in the graph definition"
        sys.exit(1)
    negNodes = []
    negNeighs = []
    for node in newGraph.nodes():
        if node < 0:
            negNodes.append(node)
            for neighs in nx.neighbors(newGraph, node):
                negNeighs.append(neighs)
    if len(negNodes) == 0:
        print "no gateway found for the network. Gateways are expected to ",\
                "have an ID lower than zero"
    newGraph.add_node(0)
    for neigh in negNeighs:
        newGraph.add_edge(0, neigh, weight=1)
    for node in negNodes:
        newGraph.remove_node(node)

    return newGraph
예제 #30
0
    def sync_label_propagation():
        iter_num = 5
        for node in nx.nodes(g):
            g.node[node]['prev'] = node
            g.node[node]['next'] = None

        for i in xrange(iter_num):
            for node in nx.nodes(g):
                label_count_dict = {}

                for neighbor in nx.neighbors(g, node):
                    if g.node[neighbor]['prev'] not in label_count_dict:
                        label_count_dict[g.node[neighbor]['prev']] = 0
                    label_count_dict[g.node[neighbor]['prev']] += 1

                label_list_dict = reverse_dict(label_count_dict)
                tie_list = label_list_dict[max(list(label_list_dict))]
                # print label_count_dict
                # print tie_list
                g.node[node]['next'] = get_rand_element(tie_list)

            for node in nx.nodes(g):
                g.node[node]['prev'] = g.node[node]['next']

            print_result(g, 'prev')
def get_new(graph,road):
    """
    Gets a new network from the original synthetic distribution network by 
    swapping an edge with a minimum length reconnection. The created network is
    rejected if power flow is not satisfied.
    """
    prim_nodes = [n for n in graph if graph.nodes[n]['label']!='H']
    reg_nodes = [n for n in graph if graph.nodes[n]['label']=='R']
    prim_edges = [e for e in graph.edges() \
                  if graph[e[0]][e[1]]['label']!='S']
    sub = [n for n in graph if graph.nodes[n]['label']=='S'][0]
    
    # Reconstruct primary network to alter without modifying original
    new_graph = graph.__class__()
    new_graph.add_nodes_from(prim_nodes)
    new_graph.add_edges_from(prim_edges)
    
    # Get edgelist for random sampling and remove an edge
    edgelist = [e for e in prim_edges if graph.nodes[e[0]]['label']=='T' \
                and graph.nodes[e[1]]['label']=='T']
    rand_edge = edgelist[np.random.choice(range(len(edgelist)))]
    rem_geom = graph.edges[rand_edge]['geometry']
    new_graph.remove_edge(*rand_edge)
    
    # Analyze the two disconnected components
    target = rand_edge[1] if nx.has_path(new_graph,sub,rand_edge[0]) else rand_edge[0]
    comps = list(nx.connected_components(new_graph))
    if sub in list(comps[0]):
        not_connected_nodes = list(comps[1])
        connected_nodes = list(comps[0])
    else:
        not_connected_nodes = list(comps[0])
        connected_nodes = list(comps[1])
    
    # Create a dummy road network
    new_road = road.__class__()
    new_road.add_nodes_from(road.nodes)
    new_road.add_edges_from(road.edges)
    for e in new_road.edges:
        new_road.edges[e]['geometry'] = LineString([road.nodes[e[0]]['cord'],
                                                    road.nodes[e[1]]['cord']])
        new_road.edges[e]['length'] = Link(new_road.edges[e]['geometry']).geod_length
        
    # Delete the same edge/path from the dummy road network
    path = [rand_edge[0],rand_edge[1]]
    if len(rem_geom.coords)>2:
        path_coords = list(rem_geom.coords)[1:-1]
        for cord in path_coords:
            neighbor_dist = {n: geodist(cord,road.nodes[n]['cord']) \
                             for n in nx.neighbors(road,path[-2])}
            node_in_path = min(neighbor_dist,key=neighbor_dist.get)
            path.insert(-1,node_in_path)
    rem_road_edge = [(path[i],path[i+1]) for i,_ in enumerate(path[:-1])]
    for edge in rem_road_edge:
        new_road.remove_edge(*edge)
    
    # Get edgeset
    for n in not_connected_nodes:
        circle = Point(graph.nodes[n]['cord']).buffer(0.01)
        nearby_nodes = [m for m in connected_nodes \
                        if Point(graph.nodes[m]['cord']).within(circle)]
        
        
    
    sys.exit(0)
    
    # Get the set of edges between the disconnected components
    # edge_set = []
    # for e in new_road:
    #     if (e[0] in comps[0] and e[1] in comps[1]) or (e[0] in comps[1] and e[1] in comps[0]):
            
    
    # dict_node = {n:graph.nodes[n]['cord'] for n in connected_nodes \
    #              if n!=end_node}
    # center_node = graph.nodes[other_node]['cord']
    # near_node = find_nearest_node(center_node,dict_node)
    # new_edge = (near_node,other_node)
    # new_graph.add_edge(*new_edge)
    # create_network(new_graph,graph)
    
    # print("Checking power flow result...")
    powerflow(new_graph)
    voltage = [new_graph.nodes[n]['voltage'] for n in new_graph \
               if new_graph.nodes[n]['label']!='H']
    low_voltage_nodes = [v for v in voltage if v<=0.87]
    check = (len(low_voltage_nodes)/len(voltage))*100.0
    if check>5.0:
        print("Many nodes have low voltage. Percentage:",check)
        return graph,0
    else:
        print("Acceptable power flow results. Percentage:",check)
        return new_graph,1
예제 #32
0
파일: refex.py 프로젝트: shawnli/RolX
def inducer(graph, node):
    nebs = nx.neighbors(graph, node)
    sub_nodes = nebs + [node]
    sub_g = nx.subgraph(graph, sub_nodes)
    out_counts = np.sum(map(lambda x: len(nx.neighbors(graph,x)), sub_nodes))
    return sub_g, out_counts, nebs
예제 #33
0
    def detect(self):
        """detect the source with GSBA.
        Returns:
            @rtype:int
            the detected source
        """

        ''' 
        这是什么时候调用的?这是公式需要,对别的来说,就不是这个了。所有对于贪心是有两个

        先验的,一个是要谣言中心性,一个是其他。
        我们要加我们的就是加一个先验的,比如覆盖的操作,模仿其谣言定位方式,自己写一个
        然后作为先验,放在先验中。

        '''
        self.reset_centrality()
        self.prior_detector.set_data(self.data)
        self.prior_detector.detect()
        self.prior = nx.get_node_attributes(self.subgraph, 'centrality')

        # print('先验检测器是什么?')
        # print(self.prior)

        # epa带权重的东西
        self.reset_centrality()
        epa_weight_object = epa2.EPA_center_weight()
        epa_weight_object.set_data(self.data)
        epa_weight_object.detect()
        epa_weight_cnetralities = nx.get_node_attributes(self.subgraph, 'centrality')

        # 覆盖率中心
        self.reset_centrality()
        cc_object = cc.CoverageCenter()
        cc_object.set_data(self.data)
        cc_object.detect()
        coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality')




        self.reset_centrality()
        infected_nodes = set(self.subgraph.nodes())
        n = len(infected_nodes)
        epa_coverage={}
        for node in infected_nodes:
            epa_coverage[node] = decimal.Decimal(coverage_centralities[node]*epa_weight_cnetralities[node])
        nx.set_node_attributes(self.subgraph,'centrality',epa_coverage)
        result= self.sort_nodes_by_centrality()
        print('result')
        high_value =[x[0] for x in result]
        new_subgraph =nx.Graph()
        new_subgraph= self.data.graph.subgraph(high_value[0:40])
        new_infect_nodes = new_subgraph.nodes()


        self.reset_centrality()
        posterior = {}
        included = set()
        neighbours = set()
        weights = self.data.weights
        for v in new_infect_nodes:
            """find the approximate upper bound by greedy searching"""
            included.clear()
            neighbours.clear()
            included.add(v)
            neighbours.add(v)
            likelihood = 1
            w = {}  # effective propagation probabilities: node->w
            w_key_sorted = blist()
            w[v] = 1
            w_key_sorted.append(v)
            while len(included) < n and len(w_key_sorted)>1:
                # print('邻居用来计算所谓的neighbours')
                # print(neighbours)
                w_sum = sum([w[j] for j in neighbours])
                u = w_key_sorted.pop()  # pop out the last element from w_key_sorted with the largest w
                likelihood *= w[u] / w_sum
                # print('分母是?')
                # print(w_sum)
                # print('likelihood')
                # print(likelihood)
                included.add(u)
                neighbours.remove(u)
                new = nx.neighbors(new_subgraph, u)
                # print('new也就是在总图中的邻居')
                # print(new)
                for h in new:
                    # print('遍历到某个邻居')
                    # print(h)
                    if h in included:
                        continue
                    neighbours.add(h)
                    # compute w for h
                    w_h2u = weights[self.data.node2index[u], self.data.node2index[h]]
                    # w_h2u = weights[self.data.node2index[u]][self.data.node2index[h]]
                    if h in w.keys():
                        # print('------')
                        # print(w[h])
                        # print(w_h2u)
                        w[h] = 1 - (1 - w[h]) * (1 - w_h2u)
                        # print('w[h],,,,h在keys')
                        # print(w[h])
                    else:
                        # print('h不在keys')
                        w[h] = w_h2u
                        # print(w[h])

                    # print('w是什么')
                    # print(w)
                    # h_neighbor = nx.neighbors(self.data.graph, h)
                    # w_h = 1
                    # for be in included.intersection(h_neighbor):
                    #     w_h *= 1 - self.data.get_weight(h, be)
                    # w[h] = 1 - w_h
                    """insert h into w_key_sorted, ranking by w from small to large"""
                    if h in infected_nodes:
                        # print('开始排序了')
                        if h in w_key_sorted:
                            w_key_sorted.remove(h)  # remove the old w[h]
                        k = 0

                        while k < len(w_key_sorted):
                            if w[w_key_sorted[k]] > w[h]:
                                break
                            k += 1
                        # print(w_key_sorted)
                        w_key_sorted.insert(k, h)  # 安排降序加入,就是排列可能性加入,安排顺序插入进去
                        # print('w_key_sorted')
                        # print(w_key_sorted)

                        # w_key_sorted[k:k] = [h]
            # print('每次开始的是那个节点呢?')
            # print(v)
            # print('每一个的可能性是likehood')
            # print(likelihood)
            posterior[v] = (decimal.Decimal(self.prior[v]) * decimal.Decimal(likelihood) * self.prior[v])

        nx.set_node_attributes(self.subgraph, 'centrality', posterior)
        return self.sort_nodes_by_centrality()
예제 #34
0
def algo9(G):
    T = nx.Graph()
    n = len(G)
    B = nx.Graph()
    # Add nodes with the node attribute "bipartite"
    B.add_nodes_from(G.nodes)
    for node in G.nodes():
        tmp = n + node
        B.add_node(tmp)

    for a in G.nodes():
        for b in nx.neighbors(G, a):
            # print(G[a][b]['weight'])
            B.add_edge(a, n + b, weight=G[a][b]['weight'])

    weightOfNodes = nodesWeight(B, n)

    currentNode = weightOfNodes[0][0]
    T.add_node(currentNode)
    B_prime = B.copy()
    for node in nx.neighbors(B_prime, currentNode):
        B.remove_node(node)
    B.remove_node(currentNode)

    print(B.edges, 1)

    i = 1
    while len(set(B.nodes) & set(range(n, 2 * n))) != 0:
        candidate = set()
        for node in T.nodes():
            candidate = candidate | set(nx.neighbors(G, node))

        candidate -= T.nodes()
        # print(candidate, 'candidate')

        for i in weightOfNodes:
            if i[0] in list(candidate):
                currentNode = i[0]
                break

        # print(currentNode, 'currentNode')

        shortest = float('inf')
        shortestNode = None
        for j in T.nodes:
            if ((currentNode, j)
                    in G.edges) and (G[currentNode][j]['weight'] < shortest):
                shortest = G[currentNode][j]['weight']
                shortestNode = j
        # print(currentNode, shortestNode, G[currentNode][shortestNode]['weight'])
        T.add_edge(currentNode,
                   shortestNode,
                   weight=G[currentNode][shortestNode]['weight'])

        # currentNode = weightOfNodes[i][0]
        T.add_node(currentNode)
        B_prime = B.copy()
        for node in nx.neighbors(B_prime, currentNode):
            B.remove_node(node)
        B.remove_node(currentNode)

    return T
예제 #35
0
def khren2(G):

    result_s = {}
    result_d = {}
    passed_set = []

    list_neighbrs = {}

    for v in G.nodes:
        list_neighbrs.update({v: set(nx.neighbors(G, v))})

    for u in G.nodes:
        passed_set.append(u)
        for v in nx.non_neighbors(G, u):
            if not v in passed_set:
                cmn_nmbr = list_neighbrs[u] & list_neighbrs[v]
                # dist = nx.shortest_path_length(G,u,v)
                # if dist == 2:
                # cmn_nmbr = G.distance(u,v)
                if G.nodes[u]["ground_label"] == G.nodes[v]['ground_label']:
                    result_s.update({(u, v): cmn_nmbr})
                else:
                    result_d.update({(u, v): cmn_nmbr})

    # max_s = max(len(result_s.values()))
    max_s = len(max(result_s.values(), key=len))
    max_d = len(max(result_d.values(), key=len))
    print(max_s, max_d)

    potential = 0
    potential_set = set()
    result_s = {key: val for key, val in result_s.items() if len(val) > max_d}

    G_s = G.subgraph(list(G.nodes))
    edges = list(G_s.edges())
    G_s.remove_edges_from(edges)

    for (u, v) in result_s:
        G_s.add_edge(u, v)

    print(G_s.number_of_edges())
    print(nx.number_connected_components(G_s))
    fig, ax1 = plt.subplots(1, 1, sharey=True, figsize=(14, 7))
    pos = nx.spring_layout(G_s)
    nx.draw(G_s,
            pos,
            ax1,
            with_labels=False,
            node_color='black',
            node_size=20,
            width=1)
    plt.show()
    plt.close()
    # ground_colors = [G_s.nodes[node]["ground_label"] for node in self.nodes

    for pair in result_s:
        potential += 1
        potential_set = potential_set | result_s[pair]
    print("Potential pairs = %d" % potential)
    print("Potential vertices = %d" % len(potential_set))

    for (pair, vertex_list) in result_s.items():
        if len(vertex_list) == max_s:
            max_pair = pair
            break

    nx.set_node_attributes(G, {max_pair[0]: 0}, "label")
    nx.set_node_attributes(G, {max_pair[1]: 0}, "label")
    marked_nmbr = 0
    marked_vertices = {max_pair[0], max_pair[1]}
    passed_pairs = set()
    # print(result_s[max_pair])
    it = 0
    prev_len = 0

    while len(marked_vertices) < 500 and len(marked_vertices) > marked_nmbr:
        marked_nmbr = len(marked_vertices)
        marked_list = list(marked_vertices)
        marked_list.reverse()
        print(marked_list)
        for marked_pair in itertools.combinations(marked_list, 2):
            if marked_pair[0] > marked_pair[1]:
                rev_marked_pair = (marked_pair[1], marked_pair[0])
            else:
                rev_marked_pair = marked_pair
            if rev_marked_pair in result_s:
                for pair in result_s:
                    if len(result_s[pair]) > max_d:
                        if len(result_s[pair]
                               & result_s[rev_marked_pair]) > 0 or len(
                                   [pair[0]]):
                            pred_labels = dict(
                                zip(list(result_s[pair]),
                                    [0 for i in result_s[pair]]))
                            nx.set_node_attributes(G, pred_labels, "label")
                            marked_vertices.update(result_s[pair])
                            marked_vertices.update([pair[0], pair[1]])
                if len(marked_vertices) > prev_len:
                    print(len(marked_vertices))
                    prev_len = len(marked_vertices)
                if len(marked_vertices) >= 500:
                    break
        if len(marked_vertices) >= 500:
            break
        print("iteration = %d" % it)
        it += 1

    print(len(marked_vertices))
    one_pred = list(set(G.nodes) - marked_vertices)
    pred_labels = dict(zip(one_pred, [1 for i in one_pred]))
    nx.set_node_attributes(G, pred_labels, "label")
    prediction = [v for v in nx.get_node_attributes(G, 'label').values()]
    # print([G.nodes[node]['ground_label'] for node in G.nodes if node not in marked_vertices])
    # print([G.nodes[node]['label'] for node in G.nodes if node not in marked_vertices])
    print(max_1(accuracy_score(G.ground_labels, prediction)))

    print(it)

    return (result_s, result_d)
예제 #36
0
def get_orings(atoms, index, possible):
    '''
    atoms: ASE atoms object of the zeolite framework to be analyzed
    index: (integer) index of the atom that you want to classify
    possible: (list) of the types of rings known to be present in the zeolite
              framework you are studying. This information is available on IZA
              or in the collections module of this package.
    Returns: Class - The size of rings associated with the oxygen.
             paths - The actual atom indices that compose those rings.
    '''

    cell = atoms.get_cell_lengths_and_angles()[:3]
    repeat = []
    possible = possible * 2
    maxring = max(possible)
    for i, c in enumerate(cell):
        if c / 2 < maxring / 2 + 5:
            l = c
            re = 1
            while l / 2 < maxring / 2 + 5:
                re += 1
                l = c * re

            repeat.append(re)
        else:
            repeat.append(1)
    atoms2 = atoms.copy()
    atoms2 = atoms2.repeat(repeat)
    center = atoms2.get_center_of_mass()
    trans = center - atoms2.positions[index]
    atoms2.translate(trans)
    atoms2.wrap()

    cutoff = neighborlist.natural_cutoffs(atoms2, mult=1.05)
    nl = neighborlist.NeighborList(cutoffs=cutoff,
                                   self_interaction=False,
                                   bothways=True)
    nl.update(atoms2)
    matrix = nl.get_connectivity_matrix(sparse=False)
    m = matrix.copy()
    G = nx.from_numpy_matrix(matrix)

    neighbs = nx.neighbors(G, index)
    for n in neighbs:
        if atoms2[n].symbol != 'O':
            fe = [n]
    fe.append(index)

    G.remove_edge(fe[0], fe[1])
    tmpClass = []
    rings = []
    while len(tmpClass) < 6:
        try:
            path = nx.shortest_path(G, fe[0], fe[1])
        except:
            break
        length = len(path)
        if length in possible:
            tmpClass.append(int(len(path) / 2))
            rings.append(path)
            if length == 18:
                G.remove_edge(path[8], path[9])
            elif length < 16 and length > 6:
                G.remove_edge(path[3], path[4])
            elif length >= 16:
                G.remove_edge(path[int(len(path) / 2 - 1)],
                              path[int(len(path) / 2)])
            if length == 8:
                G.remove_node(path[4])
            if length == 6:
                G.remove_node(path[3])
        else:
            G.remove_edge(path[int(length / 2)], path[int(length / 2 + 1)])

    rings = remove_dups(rings)
    rings = remove_sec(rings)
    Class = []
    for r in rings:
        Class.append(int(len(r) / 2))
    paths = rings
    paths = [x for _, x in sorted(zip(Class, paths), reverse=True)]
    Class.sort(reverse=True)

    keepers = []
    for i in paths:
        for j in i:
            if j not in keepers:
                keepers.append(j)
    d = [atom.index for atom in atoms2 if atom.index not in keepers]
    del atoms2[d]

    return Class, paths, atoms2
예제 #37
0
def MI(graph_file):
    G = nx.read_edgelist(graph_file)
    node_num = nx.number_of_nodes(G)
    edge_num = nx.number_of_edges(G)
    print(node_num)
    print(edge_num)
    sim_dict = {}  # �洢���ƶȵ��ֵ�
    i = 0

    I_pConnect_dict = {}
    edges = nx.edges(G)
    ebunch = nx.non_edges(G)

    for u, v in edges:
        uDegree = nx.degree(G, u)
        vDegree = nx.degree(G, v)
        pConnect = 1 - (comb(
            (edge_num - uDegree), vDegree)) / (comb(edge_num, vDegree))
        I_pConnect = -math.log2(pConnect)
        I_pConnect_dict[(u, v)] = I_pConnect
        I_pConnect_dict[(v, u)] = I_pConnect
    for u, v in ebunch:
        uDegree = nx.degree(G, u)
        vDegree = nx.degree(G, v)
        pConnect = 1 - (comb(
            (edge_num - uDegree), vDegree)) / (comb(edge_num, vDegree))
        I_pConnect = -math.log2(pConnect)
        I_pConnect_dict[(u, v)] = I_pConnect
        I_pConnect_dict[(v, u)] = I_pConnect

    ebunchs = nx.non_edges(G)
    i = 0
    for u, v in ebunchs:

        pMutual_Information = 0
        I_pConnect = I_pConnect_dict[(u, v)]
        for z in nx.common_neighbors(G, u, v):
            neighbor_num = len(list(nx.neighbors(G, z)))
            neighbor_list = nx.neighbors(G, z)
            for m in range(len(neighbor_list)):
                for n in range(m + 1, len(neighbor_list)):
                    if m != n:
                        I_ppConnect = I_pConnect_dict[(neighbor_list[m],
                                                       neighbor_list[n])]
                        if nx.clustering(G, z) == 0:
                            pMutual_Information = pMutual_Information + (
                                2 /
                                (neighbor_num *
                                 (neighbor_num - 1))) * ((I_ppConnect) -
                                                         (-math.log2(0.0001)))
                        else:
                            pMutual_Information = pMutual_Information + (
                                2 / (neighbor_num * (neighbor_num - 1))) * (
                                    (I_ppConnect) -
                                    (-math.log2(nx.clustering(G, z))))
        sim_dict[(u, v)] = -(I_pConnect - pMutual_Information)
        i = i + 1


# =============================================================================
#         print(i)
#         print(str(u) + "," + str(v))
#         print (sim_dict[(u, v)])
# =============================================================================
    print(sim_dict)
    return sim_dict
예제 #38
0
 def neighbors(self,state):
     return nx.neighbors(self.graph,state)
예제 #39
0
파일: Demon.py 프로젝트: PanShi2016/QOCE
    def __overlapping_label_propagation(self,
                                        ego_minus_ego,
                                        ego,
                                        max_iteration=100):
        """

        :param max_iteration: number of desired iteration for the label propagation
        :param ego_minus_ego: ego network minus its center
        :param ego: ego network center
        """
        t = 0

        old_node_to_coms = {}

        while t < max_iteration:
            t += 1

            label_freq = {}
            node_to_coms = {}

            nodes = nx.nodes(ego_minus_ego)
            random.shuffle(nodes)

            count = -len(nodes)

            for n in nodes:
                n_neighbors = nx.neighbors(ego_minus_ego, n)

                if count == 0:
                    t += 1

                #compute the frequency of the labels
                for nn in n_neighbors:

                    communities_nn = [nn]

                    if nn in old_node_to_coms:
                        communities_nn = old_node_to_coms[nn]

                    for nn_c in communities_nn:
                        if nn_c in label_freq:
                            v = label_freq.get(nn_c)
                            #case of weighted graph
                            if self.weighted:
                                label_freq[nn_c] = v + ego_minus_ego.edge[nn][
                                    n]['weight']
                            else:
                                label_freq[nn_c] = v + 1
                        else:
                            #case of weighted graph
                            if self.weighted:
                                label_freq[nn_c] = ego_minus_ego.edge[nn][n][
                                    'weight']
                            else:
                                label_freq[nn_c] = 1

                #first run, random choosing of the communities among the neighbors labels
                if t == 1:
                    if not len(n_neighbors) == 0:
                        r_label = random.sample(label_freq.keys(), 1)
                        ego_minus_ego.node[n]['communities'] = r_label
                        old_node_to_coms[n] = r_label
                    count += 1
                    continue

                #choose the majority
                else:
                    labels = []
                    max_freq = -1

                    for l, c in label_freq.items():
                        if c > max_freq:
                            max_freq = c
                            labels = [l]
                        elif c == max_freq:
                            labels.append(l)

                    node_to_coms[n] = labels

                    if not n in old_node_to_coms or not set(
                            node_to_coms[n]) == set(old_node_to_coms[n]):
                        old_node_to_coms[n] = node_to_coms[n]
                        ego_minus_ego.node[n]['communities'] = labels

            t += 1

        #build the communities reintroducing the ego
        community_to_nodes = {}
        for n in nx.nodes(ego_minus_ego):
            if len(nx.neighbors(ego_minus_ego, n)) == 0:
                ego_minus_ego.node[n]['communities'] = [n]

            c_n = ego_minus_ego.node[n]['communities']

            for c in c_n:

                if c in community_to_nodes:
                    com = community_to_nodes.get(c)
                    com.append(n)
                else:
                    nodes = [n, ego]
                    community_to_nodes[c] = nodes

        return community_to_nodes
예제 #40
0
파일: mol.py 프로젝트: mstieffe/deepMap
    def aa_seq(self, order="dfs", train=True):
        mol_atoms_heavy = [a for a in self.atoms if a.type.mass >= 2.0]
        atom_seq_dict_heavy = {}
        atom_seq_dict_hydrogens = {}
        atom_predecessors_dict = {}
        cg_seq = self.cg_seq(order=order, train=train)
        for bead, predecessor_beads in cg_seq:
            bead_atoms = bead.atoms
            heavy_atoms = [a for a in bead_atoms if a.type.mass >= 2.0]
            hydrogens = [a for a in bead_atoms if a.type.mass < 2.0]
            predecessor_atoms = list(itertools.chain.from_iterable([b.atoms for b in set(predecessor_beads)]))
            predecessor_atoms_heavy = [a for a in predecessor_atoms if a.type.mass >= 2.0]
            predecessor_atoms_hydrogens = [a for a in predecessor_atoms if a.type.mass < 2.0]

            #find start atom
            psble_start_nodes = []
            n_heavy_neighbors = []
            for a in heavy_atoms:
                n_heavy_neighbors.append(len(list(nx.all_neighbors(self.G_heavy, a))))
                for n in nx.all_neighbors(self.G_heavy, a):
                    if n in predecessor_atoms_heavy:
                        psble_start_nodes.append(a)
            if psble_start_nodes:
                #start_atom = np.random.choice(psble_start_nodes)
                #weird bonds in cg sPS... therefore just take first one...
                start_atom = psble_start_nodes[0]
            else:
                start_atom = heavy_atoms[np.array(n_heavy_neighbors).argmin()]
            #else:
            #    start_atom = heavy_atoms[0]

            #sequence through atoms of bead
            if order == "bfs":
                edges = list(nx.bfs_edges(self.G.subgraph(heavy_atoms), start_atom))
                atom_seq = [start_atom] + [e[1] for e in edges]
            elif order == "random":
                atom_seq = [start_atom]
                pool = []
                for n in range(1, len(heavy_atoms)):
                    pool += list(nx.neighbors(self.G.subgraph(heavy_atoms), atom_seq[-1]))
                    pool = list(set(pool))
                    next = np.random.choice(pool)
                    while next in atom_seq:
                        next = np.random.choice(pool)
                    pool.remove(next)
                    atom_seq.append(next)
            else:
                atom_seq = list(nx.dfs_preorder_nodes(self.G.subgraph(heavy_atoms), start_atom))
            #hydrogens = self.hydrogens[:]
            np.random.shuffle(hydrogens)
            #atom_seq = atom_seq + hydrogens

            #atom_seq = []
            for n in range(0, len(atom_seq)):
                atom_predecessors_dict[atom_seq[n]] = predecessor_atoms_heavy + atom_seq[:n]
            for n in range(0, len(hydrogens)):
                atom_predecessors_dict[hydrogens[n]] = mol_atoms_heavy + predecessor_atoms_hydrogens + hydrogens[:n]

            atom_seq_dict_heavy[bead] = atom_seq
            atom_seq_dict_hydrogens[bead] = hydrogens


        return cg_seq, atom_seq_dict_heavy, atom_seq_dict_hydrogens, atom_predecessors_dict
예제 #41
0
def FindNeighbors(G, node):
    tmp = list(nx.neighbors(G, str(node)))
    return tmp
예제 #42
0
파일: rings_old.py 프로젝트: cwaitt/zse
def get_orings_new(atoms, index, possible):
    cell = atoms.get_cell_lengths_and_angles()[:3]
    repeat = []
    possible = possible * 2
    maxring = max(possible)
    for i, c in enumerate(cell):
        if c / 2 < maxring / 2 + 5:
            l = c
            re = 1
            while l / 2 < maxring / 2 + 5:
                re += 1
                l = c * re

            repeat.append(re)
        else:
            repeat.append(1)
    atoms2 = atoms.copy()
    atoms2 = atoms2.repeat(repeat)
    center = atoms2.get_center_of_mass()
    trans = center - atoms2.positions[index]
    atoms2.translate(trans)
    atoms2.wrap()
    atoms3 = atoms2.copy()
    cutoff = neighborlist.natural_cutoffs(atoms2, mult=1.05)
    nl = neighborlist.NeighborList(cutoffs=cutoff,
                                   self_interaction=False,
                                   bothways=True)
    nl.update(atoms2)
    matrix = nl.get_connectivity_matrix(sparse=False)
    m = matrix.copy()
    G = nx.from_numpy_matrix(matrix)

    neighbs = nx.neighbors(G, index)
    fe = []
    for n in neighbs:
        if atoms2[n].symbol != 'O':
            fe.append(n)
    fe.append(index)
    rings = []
    for path in nx.all_simple_paths(G, index, fe[0], cutoff=max(possible) - 1):
        rings.append(path)
    new_rings = []
    for r in rings:
        if len(r) in possible:
            new_rings.append(r)
    rings = new_rings

    delete = []
    for j, r in enumerate(rings):
        flag = False
        if len(r) >= 12:
            for i in range(1, len(r) - 3, 2):
                angle = atoms3.get_angle(r[i], r[i + 2], r[i + 4], mic=True)
                if angle < 100:
                    delete.append(j)
                    break
    new_rings = []
    for j, r in enumerate(rings):
        if j not in delete:
            new_rings.append(r)
    rings = new_rings

    rings = remove_sec(rings)
    rings = remove_dups(rings)
    Class = []
    for r in rings:
        Class.append(int(len(r) / 2))
    paths = rings
    paths = [x for _, x in sorted(zip(Class, paths), reverse=True)]
    Class.sort(reverse=True)

    keepers = []
    for i in paths:
        for j in i:
            if j not in keepers:
                keepers.append(j)
    d = [atom.index for atom in atoms2 if atom.index not in keepers]
    del atoms2[d]

    return Class, paths, atoms2
예제 #43
0
def generate_subgraph_biased_random_walk(id=0):
    path = '/network/rit/lab/ceashpc/share_data/GraphOpt/datasets/epinions'
    fn = 'graph.pkl'
    with open(os.path.join(path, fn), 'rb') as rfile:
        graph = pickle.load(rfile)

    print(graph.number_of_nodes())
    print(graph.number_of_edges())

    print(len([1 for (u, v) in graph.edges() if u in graph[v]]))

    undigraph = graph.to_undirected()  # directed graph to undirected graph
    print(undigraph.number_of_nodes())
    print(undigraph.number_of_edges())
    print(nx.is_connected(undigraph))

    path = '/network/rit/lab/ceashpc/share_data/GraphOpt/datasets/epinions/dcs_test'
    fn = '03EdgesC.txt'
    correlations = {}  # edge weight of conceptual network
    with open(os.path.join(path, fn)) as rfile:
        for line in rfile:
            terms = line.strip().split(' ')
            node_1, node_2, weight = int(terms[0]), int(terms[1]), float(
                terms[2])

            correlations[(node_1, node_2)] = weight

    num_nodes = undigraph.number_of_nodes()
    node_degree = np.zeros(num_nodes)
    for current_node in undigraph.nodes():
        sum_weight = 0.
        for node in nx.neighbors(undigraph, current_node):
            pair = tuple(sorted(list((node, current_node))))
            weight = correlations[pair] if pair in correlations else 0.
            sum_weight += weight

        node_degree[current_node] = sum_weight

    # random walk
    start_node = next_node = np.random.choice(range(num_nodes))
    subgraph = set()
    subgraph.add(start_node)
    restart = 0.1
    count = 1000
    print(start_node)
    # random walk on undirected physical graph, biased for nodes with higher degree on conceptual network
    while True:
        if len(subgraph) >= count:
            break

        neighbors = [node for node in nx.neighbors(undigraph, next_node)]

        neighbor_degree_dist = [node_degree[node] for node in neighbors]
        sum_prob = np.sum(neighbor_degree_dist)
        # note, when there are no neighbors for one node on conceptual network, its probabilities to other neighbor nodes are equal
        normalized_prob_dist = [
            prob / sum_prob if not sum_prob == 0. else 1. / len(neighbors)
            for prob in neighbor_degree_dist
        ]
        # if sum_prob == 0.:
        #     print(len(neighbors))
        #     print(neighbor_degree_dist)
        #     print(sum_prob)
        #     print(normalized_prob_dist)
        #     break
        # print(len(neighbor_degree_dist))
        # print(normalized_prob_dist)

        if np.random.uniform() > restart:
            next_node = np.random.choice(
                neighbors, p=normalized_prob_dist
            )  # biased for those nodes with high degree
        else:  # restart
            next_node = start_node

        subgraph.add(next_node)
        print(len(subgraph))

    if len(subgraph) < count:
        print('generation fails')
        return

    mean_1 = 5.
    mean_2 = 0.
    std = 1.
    attributes = np.zeros(graph.number_of_nodes())
    for node in graph.nodes():
        if node in subgraph:
            attributes[node] = np.random.normal(mean_1, std)
        else:
            attributes[node] = np.random.normal(mean_2, std)

    path = '/network/rit/lab/ceashpc/share_data/GraphOpt/datasets/epinions/syn'
    fn = 'attributes_biased_{}.pkl'.format(id)
    with open(os.path.join(path, fn), 'wb') as wfile:
        pickle.dump({'attributes': attributes, 'subgraph': subgraph}, wfile)
예제 #44
0
while len(list(set(D) - set(F))) != 0:
    min = 9999
    for i in set(D) - set(F):
        if degree[i] < min:
            u = i
            min = degree[i]
    uu = []
    uu.append(u)
    g = G.subgraph(set(D) - set(uu))
    n = len(list(nx.connected_components(g)))
    if n != 1:
        F = set(F) | set(uu)
    else:
        D = set(D) - set(uu)
        for s in set(D) & set(list(nx.neighbors(G, u))):
            degree[s] = degree[s] - 1
        if len(list(set(list(nx.neighbors(G, u))) & set(F))) == 0:
            max = 0
            for i in list(nx.neighbors(G, u)):
                if degree[i] > max:
                    w = i
                    max = degree[i]
            ww = []
            ww.append(w)
            F = set(F) | set(ww)
    print(D)
    print(F)
    print(set(D) - set(F))
    print('\n')
print(D)
예제 #45
0
def neighbors(G, v):
    return list(nx.neighbors(G, v))
예제 #46
0
from networkx.algorithms.community import label_propagation_communities
from networkx.algorithms.community import asyn_fluidc
from networkx.algorithms.community.quality import coverage
from networkx.algorithms.community.quality import performance
from networkx.algorithms.community.centrality import girvan_newman

# preparing data
g = nx.read_gml('datasets/dolphins.gml', label='id')
# g=nx.karate_club_graph()
# g=nx.fast_gnp_random_graph(8,0.7)
# g=nx.windmill_graph(8,4)
N = len(g)
W = np.zeros((N, N))
for i in g:
    print(i, end="-> ")
    for j in nx.neighbors(g, i):
        print(j, end=" ")
        W[i][j] = 1
        W[j][i] = 1
    print()

# networkx community detection

gmc = list(greedy_modularity_communities(g))
alc = list(asyn_lpa_communities(g))
lpac = list(label_propagation_communities(g))
asfl = list(asyn_fluidc(g, 3))

# inititalization
anchorList = set([])
U = {}
def greedyDP(G, i, k):  #doesn't consider subtrees
    #This is different since we are considering each node's weight in the graph to be the number of accepting nodes in a given cluster
    #i = number_of_nodes(G)
    #k = number of seeds
    storePayoff = [[0] * i for _ in range(k)]  #store payoff
    storeSeeds = [[[]] * i for _ in range(k)]  #store seeds at each stage
    tree = nx.bfs_tree(G, 1)
    for numSeeds in range(0, k):  #bottom up DP
        nodes = list(reversed(list(
            (nx.topological_sort(tree)
             ))))  #look at nodes in reverse topological order
        for j in range(0, i):
            if j == 0 and numSeeds == 0:  #first entry
                #breakpoint()
                storeSeeds[numSeeds][j] = [nodes[j]]
                nodeWeight = computeNegPayoff(G, nodes[j])
                storePayoff[numSeeds][j] = nodeWeight
                #print("first entry,", storePayoff)

            elif numSeeds == 0:  #if there is only one seed to consider, aka first row
                last = storePayoff[numSeeds][j - 1]
                nodeWeight = computeNegPayoff(G, nodes[j])
                if nodeWeight > last:
                    storePayoff[numSeeds][j] = nodeWeight
                    storeSeeds[numSeeds][j] = [nodes[j]]
                else:
                    storePayoff[numSeeds][j] = last
                    table = storeSeeds[numSeeds][j - 1]
                    table2 = table[:]
                    storeSeeds[numSeeds][j] = table2
                #print("num seeds 0",storePayoff)
            elif j == 0:  #we only consider first node, so its simple
                storePayoff[numSeeds][j] = storePayoff[numSeeds - 1][j]
                storeSeeds[numSeeds][j] = storeSeeds[numSeeds - 1][j][:]
            else:  #where DP comes in
                last = storePayoff[numSeeds - 1][j - 1]  #diagonal-up entry
                nextGuess = computeNegPayoff(G, nodes[j]) + last
                for lastNodes in storeSeeds[numSeeds - 1][
                        j - 1]:  #dont want to double count edges!
                    neighbors = nx.neighbors(G, lastNodes)
                    for neighbor in neighbors:
                        if neighbor == nodes[j]:
                            add = G.get_edge_data(
                                lastNodes, nodes[j]
                            )  #neighbor of new node is current node
                            add = add['weight']
                            nextGuess += add
                lastEntry = storePayoff[numSeeds][j - 1]  #left entry
                lastEntryUp = storePayoff[numSeeds - 1][j]
                tup = [(storeSeeds[numSeeds][j - 1], lastEntry),
                       (storeSeeds[numSeeds - 1][j], lastEntryUp),
                       (storeSeeds[numSeeds - 1][j - 1], nextGuess),
                       (storeSeeds[numSeeds - 1][j - 1], last)]
                tup.sort(key=lambda x: x[1])
                nextList = tup[-1][0][:]
                storeSeeds[numSeeds][j] = nextList
                storePayoff[numSeeds][j] = tup[-1][1]
                if tup[-1][0] == storeSeeds[numSeeds - 1][j - 1]:
                    storeSeeds[numSeeds][j].append(nodes[j])
    f = open("make_matrix.txt", "a")
    f.write("\n  regular DP payoff: " + str(storePayoff))
    f.write("\n with seeds: " + str(storeSeeds))
    maxVal = storePayoff[k - 1][i - 1]
    for j in range(0, k):
        if storePayoff[j][i - 1] > maxVal:
            maxVal = storePayoff[j][i - 1]
    return (maxVal, storeSeeds[j][i - 1])
예제 #48
0
import networkx as nx
import operator
import pandas as pd
import numpy as np

if __name__ == "__main__":
    G = nx.read_gpickle(
        "/home/kapxy/networkanalysis/OverflowNA/data/Users_comments_nanremoved_noselfloops.pkl"
    )
    print(len(G.nodes))
    centralities = nx.degree_centrality(G)
    max_centrality = max(centralities.items(), key=operator.itemgetter(1))[0]
    print(nx.degree(G, max_centrality))
    neighbors = list(nx.neighbors(G, max_centrality))
    neighbors_of_neighbors = {max_centrality: neighbors}

    for neighbor in neighbors:
        neighbors_of_neighbors[neighbor] = list(nx.neighbors(G, neighbor))
    #print(neighbors_of_neighbors)
    thonk = np.array([
        y for x in [[(k, target) for target in v]
                    for k, v in neighbors_of_neighbors.items()] for y in x
    ])
    print(thonk[14])
    df = pd.DataFrame(np.array(thonk), columns=["Source", "Target"])
    df.to_csv("data/two_step_neighbors_from_max_degree_centrality.csv",
              index=False)
    # nx.write_edgelist(max_conn, path="data/max_conn_edgelist.csv", delimiter=",", data=False)
예제 #49
0
def WMI(G):
    #G = nx.read_edgelist(graph_file)

    edges = nx.edges(G)
    nodes = nx.nodes(G)
    beta = -math.log2(0.0001)
    sim_dict = {}

    # 得到图中所有边的权值之和
    all_weight = 0
    for u, v in edges:
        all_weight = all_weight + G.get_edge_data(u, v)['weight']
    print(all_weight)

    # 计算图中不同‘点权值’的点之间相连的互信息
    nodes_Weight_dict = {}
    weight_list = []

    # 得到每个点的“点权值”
    for v in nodes:
        node_weight = 0
        v_neighbors = nx.neighbors(G, v)
        for u in v_neighbors:
            node_weight += G.get_edge_data(u, v)['weight']
        weight_list.append(node_weight)
        nodes_Weight_dict[v] = node_weight
    #print(weight_list)
    #print(nodes_Weight_dict)

    distinct_weight_list = list(set(weight_list))
    #print(distinct_weight_list)
    size = len(distinct_weight_list)
    #print(size)

    self_Connect_dict = {}
    #得到不同‘点权值’的点之间相连的互信息
    for x in range(size):
        w_x = distinct_weight_list[x]
        for y in range(x, size):
            w_y = distinct_weight_list[y]
            p0 = 1
            (w_n, w_m) = pair(w_x, w_y)
            a = all_weight + 1
            b = all_weight - w_m + 1
            for i in range(1, int(w_n + 1)):
                p0 *= (b - i) / (a - i)
            if p0 == 1:
                self_Connect_dict[(w_n, w_m)] = beta
                #self_Connect_dict[(w_m, w_n)] = beta
            else:
                self_Connect_dict[(w_n, w_m)] = -math.log2(1 - p0)
                #self_Connect_dict[(w_m, w_n)] = -math.log2(1 - p0)
            #print (str(w_n) + "," + str(w_m))
            #print (self_Connect_dict[(w_n, w_m)])
    #print(self_Connect_dict)
    self_Conditional_dict = {}
    for z in nodes:
        w_z = nodes_Weight_dict[z]
        if w_z > 1:
            alpha = 2 / (w_z * (w_z - 1))
            cc_z = nx.clustering(G, z)  #修改为加权聚类系数
            if cc_z == 0:
                log_c = beta
            else:
                log_c = -math.log2(cc_z)
            # end if
            s = 0
            neighbor_list = nx.neighbors(G, z)
            size = len(neighbor_list)
            for i in range(size):
                m = neighbor_list[i]
                for j in range(i + 1, size):
                    n = neighbor_list[j]
                    (k_x, k_y) = pair(nodes_Weight_dict[m],
                                      nodes_Weight_dict[n])
                    if i != j:
                        s += (self_Connect_dict[(k_x, k_y)] - log_c)
            self_Conditional_dict[z] = alpha * s
    print(self_Conditional_dict)
    sim_dict = {}  # 存储相似度的字典
    ebunch = nx.non_edges(G)

    for x, y in ebunch:
        s = 0
        (k_x, k_y) = pair(nodes_Weight_dict[x], nodes_Weight_dict[y])
        for z in nx.common_neighbors(G, x, y):
            s += self_Conditional_dict[z]
        sim_dict[(x, y)] = s - self_Connect_dict[(k_x, k_y)]
        # end if
    # end for
    #print(sim_dict)
    return sim_dict
예제 #50
0
def get_trings(atoms, index, code, validation='cross_distance', cutoff=3.15):
    '''
    Function to find all the rings asssociated with a T-site in a zeolite
    framework.

    INPUTS:
    atoms:      (ASE atoms object) the zeolite framework to be analyzed
                works best if you remove any adsorbates first
    index:      (integer) index of the atom that you want to classify
    code:       (str) IZA code for the zeolite you are using (i.e. 'CHA')
    validation: (str) Method in which to determin valid rings.
                cross_distance: uses cross ring Si-Si distances
                d2:             ensures each ring can't be decomposed into two
                                smaller rings
                sphere:         Checks that no non ring atoms are within some
                                cutoff radius of the center of mass of the
                                ring.
                                Cutoff input is required for this method
                sp:             Custum shortest path method, not very reliable
    cutoff:     (float) Value required for the sphere validation method

    OUTPUTS:
    ring_list:      (list) The size of rings associated with the oxygen.
    paths:      (2d array) The actual atom indices that compose found rings.
    ring_atoms: (ASE atoms object) all the rings found
    '''
    #get possible rings, and max rings size
    ring_sizes = get_ring_sizes(code) * 2
    max_ring = max(ring_sizes)

    # repeat the unit cell so it is large enough to capture the max ring size
    # also turn this new larger unit cell into a graph
    G, large_atoms, repeat = atoms_to_graph(atoms, index, max_ring)
    index = [atom.index for atom in large_atoms if atom.tag == index][0]

    # to find all the rings associated with a T site, we need all the rings
    # associated with each oxygen bound to that T site. We will use networkx
    # neighbors to find those oxygens
    import networkx as nx
    paths = []
    for n in nx.neighbors(G, index):
        paths = paths + get_paths(G, n, ring_sizes)

    # Since we found the rings for each oxygen attached to the T-site,
    # there will be duplicate rings. Let's remove those.
    paths = remove_dups(paths)

    # now we want to remove all the non ring paths, the method for determining
    # valid rings is designated with the validation input variable.
    if validation == 'sp':
        paths = sp(G, paths)
    if validation == 'd2':
        paths = d2(G, paths)
    if validation == 'sphere':
        if cutoff == None:
            print(
                'INPUT ERROR: Validation with geometry requires cutoff in Å, however, cutoff not set.'
            )
            return
        paths = sphere(large_atoms, paths, cutoff)
    if validation == 'cross_distance':
        paths = cross_distance(large_atoms, paths)

    # finally organize all outputs: list of ring sizes, atom indices that make
    # ring paths, and an atoms object that shows all those rings
    ring_list = [int(len(p) / 2) for p in paths]
    paths2 = [x for _, x in sorted(zip(ring_list, paths), reverse=True)]
    tmp_paths = [x for _, x in sorted(zip(ring_list, paths), reverse=True)]
    paths = []
    for p in tmp_paths:
        temp = []
        for i in p:
            temp.append(large_atoms[i].tag)
        paths.append(temp)
    ring_list.sort(reverse=True)

    ring_atoms = paths_to_atoms(large_atoms, paths2)

    return ring_list, paths, ring_atoms
예제 #51
0
    def main(self, filename):
        # #拿到图
        # subGraph=self.get_Graph('../Propagation_subgraph/many_methods/result/chouqu.txt')

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/3regular_tree1000.txt')
        initG = commons.get_networkByFile(filename)

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/4_regular_graph_3000_data.txt')

        # initG = commons.get_networkByFile('../../../data/email-Eu-core.txt')

        max_sub_graph = commons.judge_data(initG)
        # source_list = product_sourceList(max_sub_graph, 2)
        source_list = commons.product_sourceList(max_sub_graph, 2)
        # print('两个节点的距离', nx.shortest_path_length(max_sub_graph, source=source_list[0], target=source_list[1]))
        infectG = commons.propagation1(max_sub_graph, source_list)

        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        self.judge_data(subinfectG)

        # single_source = commons.revsitionAlgorithm_singlueSource(subinfectG)
        distance_iter = nx.shortest_path_length(subinfectG)
        everynode_distance = []
        for node, node_distance in distance_iter:
            # print(node_distance)
            sort_list = sorted(node_distance.items(),
                               key=lambda x: x[1],
                               reverse=True)
            # print('sort_list',sort_list)
            everynode_distance.append([node, sort_list[0][0], sort_list[0][1]])
        # print('everynode_idstance',everynode_distance)
        sort_every_distance = sorted(everynode_distance,
                                     key=lambda x: x[2],
                                     reverse=True)
        print('sort_every_distance', sort_every_distance)

        #从两个最远的点进行BFS直到找到单源的位置。

        # print(nx.shortest_path_length(infectG,source=single_source[0],target=sort_every_distance[0][0]))
        # print(nx.shortest_path_length(infectG, source=single_source[0], target=sort_every_distance[0][1]))
        #
        # print(nx.shortest_path_length(infectG, source=single_source[0], target=sort_every_distance[1][0]))
        # print(nx.shortest_path_length(infectG, source=single_source[0], target=sort_every_distance[1][1]))
        #
        # print(nx.shortest_path_length(infectG, source=single_source[0], target=sort_every_distance[2][0]))
        # print(nx.shortest_path_length(infectG, source=single_source[0], target=sort_every_distance[2][1]))

        # #根据最远得点,把我们的那个啥,分区,然后利用分区点进行单源定位。。
        node_twolist = [[], []]
        lengthA_dict = nx.single_source_bellman_ford_path_length(
            subinfectG, sort_every_distance[0][0], weight='weight')
        lengthB_dict = nx.single_source_bellman_ford_path_length(
            subinfectG, sort_every_distance[0][1], weight='weight')
        for node in list(subinfectG.nodes):
            if lengthA_dict[node] > lengthB_dict[node]:  # 这个点离b近一些。
                node_twolist[1].append(node)
            elif lengthA_dict[node] < lengthB_dict[node]:
                node_twolist[0].append(node)
            # else:
            #     node_twolist[0].append(node)
            #     node_twolist[1].append(node)
        print('len(node_twolist[0]', len(node_twolist[0]))
        print('len(node_twolist[1]', len(node_twolist[1]))
        # 边界点。
        bound_list = []
        for node_temp in list(infectG.nodes()):
            # print(node_temp)
            if infectG.node[node_temp]['SI'] == 2:
                neighbors_list = list(nx.neighbors(infectG, node_temp))
                neighbors_infect_list = [
                    x for x in neighbors_list if infectG.node[x]['SI'] == 2
                ]
                if len(neighbors_list) != 1 and len(
                        neighbors_infect_list) == 1:
                    # if  len(neighbors_infect_list) == 1:
                    bound_list.append(node_temp)

        print('boundelist', len(bound_list))

        print('len(kjlk)',
              len([x for x in bound_list if x in list(subinfectG.nodes())]))
        left = [x for x in bound_list if x in node_twolist[0]]
        right = [x for x in bound_list if x in node_twolist[1]]

        print('left', left)
        print('right', right)

        left_source = commons.revsitionAlgorithm_singlueSource_receive(
            subinfectG, left)
        right_source = commons.revsitionAlgorithm_singlueSource_receive(
            subinfectG, right)
        if set(left) < set(list(subinfectG.nodes())):
            print('left在感染点里面啊')
        if set(right) < set(list(subinfectG.nodes())):
            print('left在感染点里面啊')
        distance = commons.cal_distance(infectG,
                                        [left_source[0], right_source[0]],
                                        source_list)

        return distance
mapping = {node: str(id) for id, node in enumerate(g.nodes())}
g = nx.relabel_nodes(g, mapping=mapping)

num_of_nodes = g.number_of_nodes()

# perform random walks
L = 10000
N = 1

walks = []
for node in g.nodes():
    walk = [node]

    for l in range(1, L):
        prev = walk[-1]
        nb_list = list(nx.neighbors(g, prev))
        p = np.asarray([float(g.degree(nb)) for nb in nb_list])
        p = p / np.sum(p)
        next = np.random.choice(a=nb_list, size=1, p=p)[0]
        walk.append(next)

    walks.append(walk[1:])

# Degree frequency
expected_freq = np.zeros(shape=(num_of_nodes, ), dtype=np.float)
estimated_freq = np.zeros(shape=(num_of_nodes, ), dtype=np.float)

expected_freq = np.asarray(
    [float(nx.degree(g, str(node))) for node in range(num_of_nodes)])
expected_freq = expected_freq / np.sum(expected_freq)
예제 #53
0
 def test_neighbors(self):
     assert_equal(self.G.neighbors(1), nx.neighbors(self.G, 1))
     assert_equal(self.DG.neighbors(1), nx.neighbors(self.DG, 1))
예제 #54
0
 def get_actions_by_node(self, node_name):
     s_idx = self.get_index_by_name(node_name)
     ts_idx = list(nx.neighbors(self.g_acs, s_idx))
     # send valid actions in ACTION_LOOKUP
     return [self.get_edge_attr_acs_by_idx(s_idx, t_idx) for t_idx in ts_idx]
예제 #55
0
# shortest_paths = [list([-1]*len(temp)) for i in range(len(temp))]
#
# for i in range(len(temp)):
#     for j in range(len(temp)):
#         if nx.has_path(G,name_interactor_dict[temp[tabela_genov[i]]], name_interactor_dict[temp[tabela_genov[j]]]):
#             shortest_paths[i][j] = len(nx.shortest_path(G,name_interactor_dict[temp[tabela_genov[i]]], name_interactor_dict[temp[tabela_genov[j]]]))
#         else:
#             shortest_paths[i][j] = 9999
#         # print(len(nx.shortest_path(G,name_interactor_dict[temp[tabela_genov[i]]], name_interactor_dict[temp[tabela_genov[j]]])))


gene_neighbours = {}
for gene in tabela_genov:
    # print(gene, n_neighbor(G, name_interactor_dict[temp[gene]],2))
    gene_neighbours[name_interactor_dict[temp[gene]]] = nx.neighbors(G,name_interactor_dict[temp[gene]])

gene_neighbours_2 = defaultdict(list)
for gene in tabela_genov:
    # print(gene, n_neighbor(G, name_interactor_dict[temp[gene]],2))
    for gen1 in gene_neighbours[name_interactor_dict[temp[gene]]]:
        gene_neighbours_2[name_interactor_dict[temp[gene]]] += nx.neighbors(G,gen1)
    # gene_neighbours[name_interactor_dict[temp[gene]]] = nx.neighbors(G,name_interactor_dict[temp[gene]])
for gene in gene_neighbours_2:
    gene_neighbours_2[gene] = set(gene_neighbours_2[gene])


# print combinations of n genes, orderd by jaccard similarity
# for n in [3,4,5]:
#     comb_num_dict = {}
#     for c in combinations([key for key in temp if key  in simon],n):
예제 #56
0
 def get_all_states_by_node(self, node_name):
     s_idx = self.get_index_by_name(node_name)
     ts_idx = list(nx.neighbors(self.g_acs, s_idx))
     # send the whole 1st order subgraph (current_index, list_of_neighbor_index, list_of_action_nums)
     return s_idx, ts_idx, [self.get_edge_attr_acs_by_idx(s_idx, t_idx) for t_idx in ts_idx]
N = 99
Max_Num_Neighbor = 50
resolution = 0.5

G_all = nx.Graph()
G_all.add_edges_from(relation_train_positive.values)
G_all.add_edges_from(relation_test.values)

test_user = relation_test.user.values

uninteract_user_list = []
uninteract_item_list = []

for user in test_user:
    interact_item = nx.neighbors(G_all, user)
    un_interact_item = list(set(all_item) - set(interact_item))
    np.random.shuffle(un_interact_item)
    if len(un_interact_item) < N:
        un_interact_item = un_interact_item * (int(N / len(un_interact_item)) +
                                               1)
    un_interact_item_choose = un_interact_item[:N]
    uninteract_user_list += [user] * N
    uninteract_item_list += un_interact_item_choose

uninteract = pd.DataFrame(np.array(
    [uninteract_user_list, uninteract_item_list]).T,
                          columns=['user', 'item'])

G = nx.Graph()
G.add_edges_from(relation_train_positive.values)
예제 #58
0
 def test_neighbors(self):
     assert list(self.G.neighbors(1)) == list(nx.neighbors(self.G, 1))
     assert list(self.DG.neighbors(1)) == list(nx.neighbors(self.DG, 1))
def neighbors(G, v):
    return list(nx.neighbors(G, v))  # requires 'v'
예제 #60
0
파일: rings_old.py 프로젝트: cwaitt/zse
def test_orings(atoms, index, possible):
    cell = atoms.get_cell_lengths_and_angles()[:3]
    repeat = []
    possible = possible * 2
    maxring = max(possible)
    for i, c in enumerate(cell):
        if c / 2 < maxring / 2 + 5:
            l = c
            re = 1
            while l / 2 < maxring / 2 + 5:
                re += 1
                l = c * re

            repeat.append(re)
        else:
            repeat.append(1)
    atoms2 = atoms.copy()
    atoms2 = atoms2.repeat(repeat)
    center = atoms2.get_center_of_mass()
    trans = center - atoms2.positions[index]
    atoms2.translate(trans)
    atoms2.wrap()

    cutoff = neighborlist.natural_cutoffs(atoms2, mult=1.05)
    nl = neighborlist.NeighborList(cutoffs=cutoff,
                                   self_interaction=False,
                                   bothways=True)
    nl.update(atoms2)
    matrix = nl.get_connectivity_matrix(sparse=False)
    m = matrix.copy()
    G = nx.from_numpy_matrix(matrix)

    neighbs = nx.neighbors(G, index)
    fe = []
    for n in neighbs:
        if atoms2[n].symbol != 'O':
            fe.append(n)
    fe.append(index)

    tmpClass = []
    rings = []
    G2 = G.copy()
    G2.remove_edge(fe[0], fe[2])
    pr = sorted(possible)
    rings = []

    for q in pr:
        tmprings = []
        paths = nx.all_simple_paths(G2, fe[0], fe[2], cutoff=q - 1)
        for p in paths:
            tmprings.append(p)
        for r in tmprings:
            length = len(r)
            if length in possible:
                if ((len(r) / 2) % 2) == 0:
                    try:
                        G2.remove_node(r[int(length / 2 - 1)])
                    except:
                        2 + 2
                elif ((len(r) / 2) % 2) != 0:
                    try:
                        G2.remove_node(r[int(length / 2)])
                    except:
                        2 + 2

                rings.append(r)
    rings = remove_dups(rings)
    rings = remove_sec(rings)
    Class = []
    for r in rings:
        Class.append(int(len(r) / 2))
    paths = rings
    paths = [x for _, x in sorted(zip(Class, paths), reverse=True)]
    Class.sort(reverse=True)

    keepers = []
    for i in paths:
        for j in i:
            if j not in keepers:
                keepers.append(j)
    d = [atom.index for atom in atoms2 if atom.index not in keepers]
    del atoms2[d]

    return Class, paths, atoms2