Example #1
1
def copy_layout(from_fname, to_fname):
    if not from_fname[-4:]  =='.gml': from_name +='.gml'
    if not to_fname[-4:]    =='.gml': to_name   +='.gml'

    print 'reading A=', from_fname,'..',
    g1 =  NX.read_gml(from_fname)
    labels1 = NX.get_node_attributes(g1, 'label')
    n1 = set(labels1.values())
    print len(n1),'nodes'
    
    print 'reading B=', to_fname,'..',
    g2 =    NX.read_gml(to_fname)
    labels2 = NX.get_node_attributes(g2, 'label')
    n2 = set(labels2.values())
    print len(n2),'nodes'

    intersection = len(n2.intersection(n1))
    percent=100.*intersection/len(n2)
    print 'B.intersect(A)=',intersection,'(%.1f%%)'%percent

    print 'copying layout..',
    mapping = {}
    for L1 in labels1:
        for L2 in labels2:
            if labels1[L1]==labels2[L2]:
                mapping[L1] = L2
                break

    layout = NX.get_node_attributes(g1, 'graphics')
    attr = dict([  (  mapping[ID],  {'x':layout[ID]['x'],'y':layout[ID]['y']}  )   for ID in mapping])
    
    NX.set_node_attributes(g2, 'graphics', attr)
    NX.write_gml(g2, to_fname)
    print 'done.'
Example #2
0
def test_planned_path_smoothing():
    start_pos = [2650, 2650]
    goal_pos = [1900, 400]

    graph_path = plan_path(start_pos, goal_pos)

    path_pos = nx.get_node_attributes(graph_path, 'pos')

    #this relies on the fact that nodes are sorted properly
    od = OrderedDict(sorted(path_pos.items(), key=lambda t: t[0]))

    path_keys = od.keys()
    path_list = od.values()

    #finally, actual smoothing
    smoothed_path = smooth(path_list)

    printpaths(path_list, smoothed_path)
    
    smoothed_graph = deepcopy(graph_path)

    for i, k in enumerate(path_keys):
        smoothed_graph.node[k]['pos'] = smoothed_path[i]
    
    smoothed_pos = nx.get_node_attributes(smoothed_graph, 'pos')

    plot_map()
    
    nx.draw(smoothed_graph, smoothed_pos, node_size=5, edge_color='r')
    
    nx.draw(graph_path, path_pos, node_size=5, edge_color='b')
    #nx.draw_networkx_labels(graph_path, path_pos)

    plt.show()
Example #3
0
 def run_monte_carlo(self, pickle_fn=None, init=False):
     if init:
         self.results = []
     for nnodes in self.nnodes:
         print 'Running MC n='+repr(nnodes)
         embed_param = self.get_embed_param(nnodes)
         for mc in xrange(self.nmc):
             G = self.get_random_graph(nnodes)
             for epar in embed_param:
                 embed = epar['embed'] 
                 x = embed.embed(G)
                 x = embed.get_embedding(epar['dim'], epar['scale'])
                 
                 k_means = KMeans(init='k-means++', k=self.k, n_init=5)
                 pred = k_means.fit(x).labels_
                 epar['num_diff'][mc] = num_diff_w_perms(
                                             nx.get_node_attributes(G, 'block').values(), pred)
                 epar['rand_idx'][mc] = metrics.adjusted_rand_score(
                                             nx.get_node_attributes(G, 'block').values(), pred)
         [epar.pop('embed') for epar  in embed_param] # pop off the Embedding to save space
         self.results.extend(embed_param)
         if pickle_fn:
             pickle.dump(self, open(pickle_fn,'wb'))
             print 'Saved to '+pickle_fn
     return self.results
Example #4
0
    def loc_save(self,S):
        """
        save txt 
        node ID , True pos x , True pos y , est pos x , est pos y , timestamp


        Attributes:
        ----------
        
        S        : Simulation
                   Scipy.Simulation object

        """

        pos=nx.get_node_attributes(self,'p')
        pe=nx.get_node_attributes(self,'pe_alg')
        typ = nx.get_node_attributes(self,'type')
        if self.idx == 0:
            entete = 'NodeID, True Position x, True Position y, Est Position x, Est Position y, Timestamp\n'
            file=open(basename+'/' + pstruc['DIRNETSAVE'] +'/simulation.txt','write')
            file.write(entete)
            file.close()

        try:
            file=open(basename+'/' + pstruc['DIRNETSAVE'] +'/simulation.txt','a')
            for n in self.nodes():
                if typ[n] != 'ap':
                    data = n + ',' + str(pos[n][0]) + ',' + str(pos[n][1]) + ',' + str(pe[n][0][0]) + ',' + str(pe[n][0][1]) + ',' +pyu.timestamp(S.now()) +',\n'
                    file.write(data)
            file.close()
            self.idx = self.idx +1
        except:
            pass
def find_seq_changes(nodes, G, name):
	limit = len(nodes)
	errors =[]
	G1=G.copy()
	nodes_to_be_deleted=nodes[:]
	ranks = nx.get_node_attributes(G1,"weight_rank")
	ranks_keys = {y:x for x,y in ranks.iteritems()}
	for count in range(1,limit):
		weights_original = nx.get_node_attributes(G1, "weight_sum")
		
		G1, interface_nodes_new = delete_node_by_rank(nodes_to_be_deleted, G1, 1)
		nodes_to_be_deleted.remove(ranks_keys[1])
		
		G1 = find_ranks(interface_nodes_new, G1, find_weights, 'weight')
		weights_new = nx.get_node_attributes(G1, "weight_sum")
		
		error =0.0
		for node in weights_new:
			error += np.square(weights_original[node] - weights_new[node])
		G.node[ranks_keys[1]][name+"_sum"] = round(error/len(nodes), 6)
		errors.append((ranks_keys[1], round(error/len(nodes), 6)))
		ranks = nx.get_node_attributes(G1,"weight_rank")
		ranks_keys = {y:x for x,y in ranks.iteritems()}
	G.node[nodes_to_be_deleted[0]][name+"_sum"] = 0
	errors.append((nodes_to_be_deleted[0], 0))
	return sorted(errors, key=operator.itemgetter(1)), G
    def plot_graph(cls, G, filename=None, node_attribute_name='id', edge_attribute_name=None, 
                    colored_nodes=None, colored_edges=None, colored_path=None, **kwargs):
    #def plot_graph(self, G, out_file, **kwd):
        """plot graph"""
        plt.clf()

        # get the layout of G
        pos = nx.get_node_attributes(G, 'pos')
        if not pos:
            pos = nx.spring_layout(G)

        # get node attributes
        with_labels = False
        node_labels = None
        if node_attribute_name == 'id':
            with_labels = True
        elif node_attribute_name:
            node_labels = nx.get_node_attributes(G, node_attribute_name)

        # get edge attributes
        if not edge_attribute_name:
            edge_labels = nx.get_edge_attributes(G, edge_attribute_name)

        # colored nodes
        node_default_color = '0.75' # Gray shades 

        node_color = node_default_color
        if colored_nodes:
            node_color = ['r' if node in colored_nodes else node_default_color
                            for node in G.nodes()]

        # colored path
        if colored_path:
            nrof_nodes = len(colored_path)
            idx = 0
            colored_edges = list()
            while idx < nrof_nodes-1:
                colored_edges.append((colored_path[idx], colored_path[idx+1]))
                idx += 1

        # colored edges
        edge_default_color = 'k' # black
        edge_color = edge_default_color
        if colored_edges:
            set_colored_edges = {frozenset(t) for t in colored_edges}  # G.edges returns a list of 2-tuples

            edge_color = ['r' if frozenset([u, v]) in set_colored_edges else edge_default_color
                            for u, v in G.edges()]


        # draw 
        nx.draw(G, pos, with_labels=with_labels, node_color=node_color, edge_color=edge_color, **kwargs)
        if node_labels:
            nx.draw_networkx_labels(G, pos, labels=node_labels)
        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

        if filename:
            plt.savefig(filename, bbox_inches='tight', pad_inches=0)
        else:
            plt.show()
Example #7
0
    def _retrieve_skycoords(V):
        coords_l = []
        # Accessing the borders one by one. At this step, V_subgraphs contains a list of cycles
        # (i.e. one describing the external border of the MOC component and several describing the holes
        # found in the MOC component).
        V_subgraphs = nx.connected_component_subgraphs(V)
        for v in V_subgraphs:
            # Compute the MST for each cycle
            v = nx.convert_node_labels_to_integers(v)
            mst = nx.minimum_spanning_tree(v)
            # Get one end of the span tree by looping over its node and checking if the degree is one
            src = None
            for (node, deg) in mst.degree():
                if deg == 1:
                    src = node
                    break

            # Get the unordered lon and lat
            ra = np.asarray(list(nx.get_node_attributes(v, 'ra').values()))
            dec = np.asarray(list(nx.get_node_attributes(v, 'dec').values()))
            coords = np.vstack((ra, dec)).T
            # Get the ordering from the MST
            ordering = np.asarray(list(nx.dfs_preorder_nodes(mst, src)))
            # Order the coords
            coords = coords[ordering]
            # Get a skycoord containing N coordinates computed from the Nx2 `coords` array
            coords = SkyCoord(coords, unit="deg")
            coords_l.append(coords)

        return coords_l
Example #8
0
def process_graph(graph):
	for (n, data) in graph.nodes(data=True):
		data["orig_id"] = str(n)

	# get largest connected component
	sc = sorted(nx.connected_components(graph), key=len, reverse=True)
	lcc = graph.subgraph(sc[0])
	graph = lcc

	graph = nx.convert_node_labels_to_integers(graph)

	if hasattr(graph, 'pos'):
		pos = graph.pos
	else:
		x = nx.get_node_attributes(graph, 'x')
		y = nx.get_node_attributes(graph, 'y')
		if len(x) != 0 and len(y) != 0:
			pos = { n: [x[n], y[n]] for n in graph.nodes() }
		else:
			pos = nx.spring_layout(graph)

	#graph = nx.Graph(graph) # in case of di/multi graph
	graph.pos = pos

	if len(nx.get_edge_attributes(graph, 'weight')) == 0:
		for (u,v) in graph.edges():
			weight = 1
			graph[u][v]['weight'] = weight

	return graph
def clust(Graph):
    """
    Returns the graph that merges artificial loops into a
    single node. Detects the nodes included to the triangles
    and merges them. Uses the extern function merge_nodes.
    
    Parameters
    --------
    Graph : input graph with artificial loops
    
    Returns
    -------
    G : a graph without loops; triangles of neighboring nodes
    are replaced by a single node


    """
    G = Graph.copy()
    size = G.number_of_nodes()           
    for i in G.nodes():
        neigh = nx.get_node_attributes(G, 'neig')
        index = nx.get_node_attributes(G, 'index')
        if (i in G.nodes() and nx.triangles(G, i))>0:
            n = nx.all_neighbors(G,i)
            l = [i]
            for k in n:
                if ((neigh[k]>2) and 
                    (nx.get_edge_attributes(G, 'length')[min(i,k), max(i,k)]<2)):
                    l = np.append(l, k)
            merge_nodes(G,l,size+1,index = index[i], neig = neigh[i])
            size+=1
        if (i==G.number_of_nodes()):
            break
    G = nx.convert_node_labels_to_integers(G, first_label=1)
    return G
Example #10
0
    def _split_nodes(self, node_class_names):
        """
        Split nodes based on the attribute specified in self.node_class_attribute

        :param node_class_names: Values of node_class_attribute which will be included in the plot (in clockwise order)
        :type node_class_names: list
        :return: A dictionary whose keys are the node class attribute values, and values are lists of nodes belonging to that class
        :rtype: dict
        """
        node_attribute_dict = nx.get_node_attributes(self.network, self.node_class_attribute)
        if self.order_nodes_by != 'degree':
            valid_node_set = set(nx.get_node_attributes(self.network, self.order_nodes_by))
        else:
            valid_node_set = set(node_attribute_dict)

        if node_class_names is None:
            node_class_names = set(node_attribute_dict.values())
            if len(node_class_names) > 3:
                raise ValueError("Nodes should be in 3 or fewer classes based on their {} attribute.".format(
                    self.node_class_attribute))
            node_class_names = sorted(node_class_names)
        else:
            for_deletion = []
            for node in node_attribute_dict:
                if node_attribute_dict[node] not in node_class_names:
                    for_deletion.append(node)
            for fd in for_deletion:
                node_attribute_dict.pop(fd)

        split_nodes = OrderedDict([(node_class, []) for node_class in node_class_names])
        for node_name, node_class in node_attribute_dict.items():
            if node_name in valid_node_set:
                split_nodes[node_class].append(node_name)

        return split_nodes
Example #11
0
def test_weights_planning():
    plot_map()

    start_pos = [ 2650, 2650 ]

    L, c = grid_graph(start_pos, dim=10, width=1000)

    filename = os.path.join(root, 'flash', 'fft2', 'processed', 'map.png')

    img_data = imread(filename)

    custom_labels = add_weights(L, img_data)

    astar_path = nx.astar_path(L, (5, 5), (0, 4))

    H = L.subgraph(astar_path)

    h_pos = nx.get_node_attributes(H, 'pos')

    pos = nx.get_node_attributes(L,'pos')
    nx.draw(L, pos, node_size=5)

    edge_weight=dict([((u,v,),int(d['weight'])) for u,v,d in L.edges(data=True)])

    nx.draw_networkx_edge_labels(L,pos,edge_labels=edge_weight)
    nx.draw_networkx_nodes(L,pos, node_size=0)
    nx.draw_networkx_edges(L,pos)
    nx.draw_networkx_labels(L,pos, labels=custom_labels)

    nx.draw(H,h_pos, node_size=5, edge_color='r')


    plt.show()
Example #12
0
    def get_pos(self,RAT=None):
        """ get node positions

        Parameters 
        ----------

        RAT : specify a RAT to display node position. If None, all RAT are displayed    
        
        Returns 
        -------

        dictionnary :     key     : node ID
        value     : np.array node position
        

        """
        if RAT == None:
            if self.node[self.nodes()[0]].has_key('p'):
                return nx.get_node_attributes(self,'p')
            else :
                return nx.get_node_attributes(self,'pe')
        else :
            try:
                if self.SubNet[RAT].node[self.SubNet[RAT].nodes()[0]].has_key('p'):
                    return nx.get_node_attributes(self.SubNet[RAT],'p')
                else :
                    return nx.get_node_attributes(self.SubNet[RAT],'pe')
            except: 
                raise NameError('invalid RAT name')
Example #13
0
    def consistent(self):
        """
        Determine if the assignment of values to the lattice is self-consistant.

        Returns
        -------
        valid : bool
            True if the lattice is self-consistent, False otherwise.
        """
        reds = nx.get_node_attributes(self._lattice, 'red')
        pis = nx.get_node_attributes(self._lattice, 'pi')

        if self.SELF_REDUNDANCY:  # pragma: no branch
            for node in self._lattice:
                if len(node) == 1:
                    red = reds[node]
                    mi = coinformation(self._dist, [node[0], self._output])
                    if not np.isclose(red, mi, atol=1e-5, rtol=1e-5):  # pragma: no cover
                        return False

        # ensure that the mobius inversion holds
        for node in self._lattice:
            red = reds[node]
            parts = sum(pis[n] for n in descendants(self._lattice, node, self=True))
            if not np.isnan(red) and not np.isnan(parts):
                if not np.isclose(red, parts, atol=1e-5, rtol=1e-5):
                    return False

        return True
def recursive_f (scopegraph, scope):
    #no need to calc this every scope, find another solution 
    ex_scopes=nx.get_node_attributes(scopegraph, 'ex')
    cond_scopes=nx.get_node_attributes(scopegraph, 'cond')
    ab_scopes=nx.get_node_attributes(scopegraph, 'ab')
    
    print ("new scope {0}".format(scope))
    ex = ex_scopes[scope]
    print
    ab_state = ab_scopes[scope]
    cond = cond_scopes[scope]
    #print ("ex: {0}, cond{1}".format(ex, cond))
    flag = True
    while(len(ab_state._abstract_store) != 0):
        if(flag):
            flag = False
            ab_state = abex.abstract_execution_for_simple_loop(ab_state, ex, cond, True)
            if len(scopegraph.successors(scope)) != 0: #multi edges not supported
                print ("scopegraph succesors {0}".format(scopegraph.successors(scope)[0]))
                recursive_f(scopegraph, scopegraph.successors(scope)[0])

                #scopegraph.remove_node(scopegraph.succesors(node))
        else: 
            ab_state = abex.abstract_execution_for_simple_loop(ab_state, ex, cond, False)
            if len(scopegraph.successors(scope)) != 0: #multi edges not supported
                print ("scopegraph succesors {0}".format(scopegraph.successors(scope)[0]))
                recursive_f(scopegraph, scopegraph.successors(scope)[0])
Example #15
0
    def test_build_unique_fragments(self):
        edges = {(e[0], e[1]): None for e in self.pc_edges}
        mol_graph = MoleculeGraph.with_edges(self.pc, edges)
        unique_fragments = mol_graph.build_unique_fragments()
        self.assertEqual(len(unique_fragments), 295)
        nm = iso.categorical_node_match("specie", "ERROR")
        for ii in range(295):
            # Test that each fragment is unique
            for jj in range(ii + 1, 295):
                self.assertFalse(
                    nx.is_isomorphic(unique_fragments[ii].graph,
                                     unique_fragments[jj].graph,
                                     node_match=nm))

            # Test that each fragment correctly maps between Molecule and graph
            self.assertEqual(len(unique_fragments[ii].molecule),
                             len(unique_fragments[ii].graph.nodes))
            species = nx.get_node_attributes(unique_fragments[ii].graph, "specie")
            coords = nx.get_node_attributes(unique_fragments[ii].graph, "coords")

            mol = unique_fragments[ii].molecule
            for ss, site in enumerate(mol):
                self.assertEqual(str(species[ss]), str(site.specie))
                self.assertEqual(coords[ss][0], site.coords[0])
                self.assertEqual(coords[ss][1], site.coords[1])
                self.assertEqual(coords[ss][2], site.coords[2])

            # Test that each fragment is connected
            self.assertTrue(nx.is_connected(unique_fragments[ii].graph.to_undirected()))
Example #16
0
def import_layout(from_fname, to_graph):
    if not from_fname[-4:]  =='.gml': from_fname +='.gml'

    print 'importing layout from', from_fname+'..'
    g1 =  NX.read_gml(from_fname)
    labels1 = NX.get_node_attributes(g1, 'label')
    n1 = set(labels1.values())
    
    g2 =    to_graph
    n2 = set(g2.nodes())

    if not n1:
        print '   empty target graph'
        return
    if not n2:
        print '   empty layout graph'
        return

    mapping = {}
    for L1 in labels1:
        for name in n2:
            if labels1[L1]==name:
                mapping[L1] = name
                break

    intersection = len(n2.intersection(n1))
    percent=100.*intersection/len(n2)
    print '   %.1f%%'%percent,'(%i positions)'%intersection

    layout = NX.get_node_attributes(g1, 'graphics')
    attr = dict([  (  mapping[ID],  {'x':layout[ID]['x'],'y':layout[ID]['y']}  )   for ID in mapping])
    
    NX.set_node_attributes(g2, 'graphics', attr)
Example #17
0
    def compare_list(self, graph_list, types, h, D):
        """
        Compute the all-pairs kernel values for a list of graph representations of verification tasks
        """
        all_graphs_number_of_nodes = 0
        node_labels = [0] * (h+1)
        node_depth = [0] * len(graph_list)
        edge_types = [0] * len(graph_list)
        edge_truth = [0] * len(graph_list)

        for it in range(h+1):
            node_labels[it] = [0] * len(graph_list)

        for i, g in enumerate(graph_list):
            node_labels[0][i] = {key: self._compress(value)
                                 for key, value in nx.get_node_attributes(g, 'label').items()}
            node_depth[i] = nx.get_node_attributes(g, 'depth')
            edge_types[i] = nx.get_edge_attributes(g, 'type')
            edge_truth[i] = nx.get_edge_attributes(g, 'truth')
            all_graphs_number_of_nodes += len([node for node in nx.nodes_iter(g) if node_depth[i][node] <= D])
            # if i == 0:
            #     self._graph_to_dot(g, node_labels[0][i], "graph{}.dot".format(i))

        # all_graphs_number_of_nodes is upper bound for number of possible edge labels
        phi = np.zeros((all_graphs_number_of_nodes, len(graph_list)), dtype=np.uint64)

        # h = 0
        for i, g in enumerate(graph_list):
            for node in g.nodes_iter():
                if node_depth[i][node] <= D:
                    label = node_labels[0][i][node]
                    phi[self._compress(label), i] += 1

        K = np.dot(phi.transpose(), phi)

        # h > 0
        for it in range(1, h+1):
            # Todo check if the shape fits in all cases
            phi = np.zeros((2*all_graphs_number_of_nodes, len(graph_list)), dtype=np.uint64)

            print('Updating node labels of graphs in iteration {}'.format(it), flush=True)

            # for each graph update edge labels
            for i, g in tqdm(list(enumerate(graph_list))):
                node_labels[it][i] = {}
                for node in g.nodes_iter():
                    if node_depth[i][node] <= D:
                        label_collection = self._collect_labels(node, i, g, it-1, node_labels, node_depth, types, D, edge_types, edge_truth)
                        long_label = "_".join(str(x) for x in [np.concatenate([np.array([node_labels[it-1][i][node]]),
                                                               np.sort(label_collection)])])
                        node_labels[it][i][node] = self._compress(long_label)
                        phi[self._compress(long_label), i] += 1
                        # node_labels[it][i][node] = long_label
                        # phi[self._compress(long_label), i] += 1
                # if i == 0:
                #     self._graph_to_dot(g, node_labels[it][i], "graph{}_it{}.dot".format(i, it))

            K += np.dot(phi.transpose(), phi)

        return K
Example #18
0
	def fts_async_product(self,fts2):
		assert type(fts2) == FTS
		result =  FTS(set([]),set([])) 
		
		nodes_prod = lambda u,v: tuple((list(u) if self.size>1 else [u]) + (list(v) if fts2.size>1 else [v]))
		labels_prod = lambda u,v: flatten((u,v))

		labels = nx.get_node_attributes(self,'label')
		labels_fts2 = nx.get_node_attributes(fts2,'label')

		for u in self.nodes():
			for v in fts2.nodes():
				result.add_node(nodes_prod(u,v),label=labels_prod(labels[u],labels_fts2[v]),weight=1.0)
		for (u,v) in self.edges():
			for (x,y) in fts2.edges():
				result.add_edge(nodes_prod(u,x),nodes_prod(u,y),weight=1.0)
				result.add_edge(nodes_prod(u,x),nodes_prod(v,x),weight=1.0)
				result.add_edge(nodes_prod(u,x),nodes_prod(v,y),weight=1.0)

		for u in self.graph['initial']:
			for v in fts2.graph['initial']:
				result.graph['initial'].add(nodes_prod(u,v))
		
		for u in self.graph['symbols']:
			for v in fts2.graph['symbols']:
				result.graph['symbols'].add(nodes_prod(u,v))

		result.size = self.size + fts2.size

		return copy.deepcopy(result)
def draw_network_plt(graph, name=False, scale=100, size=False):
	"""Will create a networkx matplotlib graph.
	Options: If name is specified, will save with name, otherwise will show plot"""
	colors          = False
	scaled_sizes    = False
	labels          = {}
	for node in graph.nodes():
		labels[node] = graph.node[node]['label']
	if 'color' in graph.nodes(data=True)[0][1].keys():
		colors = nx.get_node_attributes(graph, 'color').values()
	if size:
		sizes = nx.get_node_attributes(graph, size).values()
		scaled_sizes = [x * scale for x in sizes]
	if colors and scaled_sizes:
		nx.draw(graph, node_size=scaled_sizes, node_color=colors, labels=labels)
	elif colors and not scaled_sizes:
		nx.draw(graph, node_color=colors, labels=labels)
	elif scaled_sizes and not colors:
		nx.draw(graph, node_size=scaled_sizes, labels=labels)
	else:
		nx.draw(graph, labels=labels)
	if not name:
		plt.show()
	else:
		plt.savefig('../Images/'+str(name)+'.png')
	plt.clf()
Example #20
0
def main(global_map, init, goal, steering_noise, distance_noise, measurement_noise, 
         weight_data, weight_smooth, p_gain, d_gain):

    start_pos = [2650, 2650]
    goal_pos = [1900, 400]

    graph_path = plan_path(start_pos, goal_pos)
    path_pos = nx.get_node_attributes(graph_path, 'pos')

    sg = smooth_graph(graph_path, start_pos, goal_pos, True, 
                      weight_data, weight_smooth)

    sg_pos = nx.get_node_attributes(sg, 'pos')

    spath = graph_to_path(sg)

    global_map.plot()
    
    nx.draw(sg, sg_pos, node_size=5, edge_color='r')
    
    (reached_goal, penalty, num_steps, mg) = run(global_map, start_pos, 
                                                 goal_pos, spath, 
                                                 [p_gain, d_gain])

    mg_pos = nx.get_node_attributes(mg, 'pos')
    nx.draw(mg, mg_pos, node_size=5, edge_color='b')

    plt.show()
Example #21
0
def generator_priority(G):
    nodes_number = G.nodes()
    node_name_data = nx.get_node_attributes(G,'name')
    type_data = nx.get_node_attributes(G,'type')
    generator_list = []
    APU_list = []
    for i in range(0,nx.number_of_nodes(G)):
        x = nodes_number[i]
        if type_data[x]=='generator':
            generator_list.append(x)
        elif type_data[x]=='APU':
            APU_list.append(x)
    if len(APU_list) == 0:
        return
    clause = '(assert (=> '
    if len(generator_list) > 1:
        clause += '(and'
    for i in range(0, len(generator_list)):
        g_name = node_name_data[generator_list[i]]
        clause += ' ' + g_name
    clause += ') '
    if len(APU_list) > 1:
        clause += '(and '
    for i in range(0, len(APU_list)):
        apu_name = node_name_data[APU_list[i]]
        clause +=  '(not ' + apu_name + ')'
    clause += ')))\n'
    return clause
Example #22
0
def showXY(fnm, x="kx", y="ky"):
    g = nx.read_graphml(fnm)
    x = nx.get_node_attributes(g, x)
    y = nx.get_node_attributes(g, y)
    coords = zip(x.values(),y.values())
    pos = dict(zip(g.nodes(), coords))
    nx.draw(g,pos)
Example #23
0
	def plotGraph(self):
		self.posH = nx.get_node_attributes(self.H,'xy')
		self.posG = nx.get_node_attributes(self.G,'xy')
		self.posDG = nx.get_node_attributes(self.DG,'xy')

		self.color=[]
		for i,j in self.H.edges_iter():
			self.color.append(self.H[i][j]['color'])
		self.elabels = dict([((u,v,),int(d['key'])) for u,v,d in self.G.edges(data=True)])
		#for i,j in self.H.edges_iter():
		#	print self.H[i][j]['color']
		#print elabels
		#nx.draw_networkx_nodes(self.G, posG, with_labels=False, node_size = 50, ax=self.a)
		#nx.draw_networkx_edges(self.G, posG, edge_labels=elabels, edge_color='b', with_labels=True, width = 2, ax=self.a)

		nx.draw_networkx_nodes(self.G, self.posG, with_labels=False, node_size = 150, node_color='#A0CBE2', ax=self.a)
		nx.draw_networkx_edges(self.H, self.posH, edge_color=self.color, edge_cmap=self.cmap, width = 5, alpha=1, ax=self.a) # , edge_vmin=0, edge_vmax=1
		nx.draw_networkx_nodes(self.DG, self.posDG, node_size=150, alpha=1, ax=self.a, arrows=True, edge_color='r')
		#nx.draw_networkx_edges(self.DG, self.posDG, alpha=1, width = 2, ax=self.a, arrows=True, edge_color='r')
		#nx.draw_networkx_edges(self.H, posH, edge_color='k', width = 0.5, alpha=1., ax=self.a)
		nx.draw_networkx_edge_labels(self.G, self.posG, edge_labels=self.elabels, font_size= 12, alpha = 0.4, rotate=True)
		self.a.set_title("Gas Network colored by values of pressure.")
		self.a.text(0.45,0.05, "Time t = %.4f hours" % 0,transform=self.a.transAxes )
		self.ax1 = self.f.add_axes([ 0.02, 0.05, 0.04, 0.9])
		norm = mpl.colors.Normalize(vmin=3, vmax=7)
		cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, norm=norm, orientation='vertical') #, norm=norm, orientation='horizontal')
		cb1.set_ticks([3, 4, 5, 6, 7])
		cb1.set_label('Pressure in MPa', labelpad=3)
		self.canvas.draw()
Example #24
0
def init(G, uncon_comp_tups, contactor_tups):
    nodes_number = G.nodes()
    edges_number = G.edges()
    node_name_data = nx.get_node_attributes(G, 'name')
    edge_name_data = nx.get_edge_attributes(G, 'name')
    edge_type_data = nx.get_edge_attributes(G, 'type')
    node_type_data = nx.get_node_attributes(G, 'type')
    declaration = '(set-option :print-success false)\n'
    declaration += '(set-option :produce-models true)\n(set-logic QF_UF)\n'
    for i in range(0, len(nodes_number)):
        x = nodes_number[i]
        node_type = node_type_data[x]
        if node_type != 'dummy':
            clause = '(declare-fun ' + node_name_data[x] + ' () Bool)\n'
            if node_type == 'generator' or node_type=='APU' or node_type == 'rectifier_dc':
                uncon_comp_tups.append(node_name_data[x])
            declaration += clause
    for i in range(0, len(edges_number)):
        idx = edges_number[i]
        edge_type = edge_type_data[idx]
        if edge_type == 'contactor':
            edge_name = edge_name_data[idx]
            flag = 0
            for j in range(0, len(contactor_tups)):
                if edge_name == contactor_tups[j]:
                    flag = 1
                    break
            if flag == 0: contactor_tups.append(edge_name)
    for i in range(0, len(contactor_tups)):
        clause = '(declare-fun ' + contactor_tups[i] + ' () Bool)\n'
        declaration += clause
    return declaration
Example #25
0
def copy_layout_GML2NX(Fname, Graph, verbose=1):
    if not Fname[-4:]=='.gml': Fname+='.gml'
    print 'Copying layout from', Fname+'..'
    
    g1 =  NX.read_gml( Fname )
    labels1 = NX.get_node_attributes(g1, 'label')
    n1 = set(labels1.values())
    
    nodes = set( Graph.nodes() )

    if not n1:
        print '   empty layout graph'
        return
    if not nodes:
        print '   empty target graph'
        return

    mapping = {}
    for L1 in labels1:
        for name in nodes:
            if labels1[L1]==name:
                mapping[L1] = name
                break

    intersection = len(nodes.intersection(n1))
    percent=100.*intersection/len(nodes)
    print '   %.1f%%'%percent,'(%i positions)'%intersection

    layout = NX.get_node_attributes(g1, 'graphics')
    attr = dict([  (  mapping[ID],  {'x':layout[ID]['x'],'y':layout[ID]['y']}  )   for ID in mapping])
    
    NX.set_node_attributes( Graph, 'graphics', attr)
Example #26
0
def drawGraph(G):
	xcords = nx.get_node_attributes(G,'x')
	ycords = nx.get_node_attributes(G,'y')
	coordinates = dict([(k, [xcords[k], ycords[k]]) for k in xcords])
	ns = nx.get_node_attributes(G, 'n')
	nx.draw_networkx(G,pos=coordinates,node_color=[1 if int(ns[i])>0 else 0 for i in ns],cmap=py.cm.PuBu)
	py.show() 
Example #27
0
    def _reproduce_sexually(self, partner):  # TODO: Broken.
        """Sexual reproduction between two networks"""
        inherited_state = -1  # -1 would be most recent
        network_props = ['num_nodes']
        node_props = ['threshold', 'energy_consumption', 'spontaneity']
        # node_props = ['threshold']
        edge_props = ['weight']
        child = copy.deepcopy(self)
        partner.children.append(child)
        # partner.reproductive_energy_cost = self.reproductive_energy_cost
        child.parents, child.children = [self, partner], []
        if np.random.randint(0, 2) == 1:
            internal_net = copy.deepcopy(self.internal)
            child._cloned_from, child._not_cloned_from = self, partner
        else:
            internal_net = copy.deepcopy(partner.internal)
            child._cloned_from, child._not_cloned_from = partner, self
        # print "Kin with %d neurons, copied from net with %d neurons" %(internal_net.simdata[-1].number_of_nodes(), self.internal.simdata[-1].number_of_nodes())
        child.set_internal_network(copy.deepcopy(internal_net), t0=self.t)
        child.internal.simdata[inherited_state] = copy.copy(internal_net.simdata[inherited_state])

        choices = np.random.randint(2, size=(2, len(node_props)))  # randomly choose attributes
        for j, n in enumerate(node_props):
            p1 = nx.get_node_attributes(self.internal.simdata[inherited_state], n)
            p2 = nx.get_node_attributes(partner.internal.simdata[inherited_state], n)
            # add/remove nodal information based on the inherited number of nodes
            # chosen = self if choices[0][j] else partner
            # print "Using %s(N%d) for %s" %(chosen.ind_id, chosen.internal.simdata[inherited_state].number_of_nodes(), n)
            utils.set_node_attributes(child.internal.simdata[inherited_state], n, p1 if choices[0][j] else p2)

        for j, e in enumerate(edge_props):
            p1 = nx.get_edge_attributes(self.internal.simdata[inherited_state], e)
            p2 = nx.get_edge_attributes(partner.internal.simdata[inherited_state], e)
            utils.set_edge_attributes(child.internal.simdata[inherited_state], n, p1 if choices[1][j] else p2)
        return child
Example #28
0
    def compute_LDPs(self,ln,RAT):
        """compute edge LDP

        Parameters
        ----------

        n1      : float/string
            node ID
        n2      : float/string
            node ID
        RAT     : string
            A specific RAT which exist in the network ( if not , raises an error)
        value    : list : [LDP value , LDP standard deviation] 
        method    : ElectroMagnetic Solver method ( 'direct', 'Multiwall', 'PyRay'


        """
        p=nx.get_node_attributes(self.SubNet[RAT],'p')
        epwr=nx.get_node_attributes(self.SubNet[RAT],'epwr')
        sens=nx.get_node_attributes(self.SubNet[RAT],'sens')
        e=self.link[RAT]#self.SubNet[RAT].edges()
        re=self.relink[RAT] # reverse link aka other direction of link
        lp,lt, d, v= self.EMS.solve(p,e,'all',RAT,epwr,sens)
        lD=[{'Pr':lp[i],'TOA':lt[np.mod(i,len(e))] ,'d':d[np.mod(i,len(e))],'vis':v[i]} for i in range(len(d))]
        self.update_LDPs(iter(e+re),RAT,lD)
Example #29
0
    def drawLabels(self, labelAll=True):
        """Draw labels on nodes in graph (nodeId by default)"""
#        circleNodes = [ n for (n, m) in self.G.nodes(data=True) if m.get('shape','circle') == 'circle']
#        pdb.set_trace()
        if labelAll:
            nx.draw_networkx_labels(self.G, 
              pos=nx.get_node_attributes(self.G, 'pos'),
              font_size=10,
              labels=self.labelMap)
        else:
#             boxNodes = [ n for (n, m) in self.G.nodes(data=True) if m.get('shape','circle') == 'box']
#             lm = {} 
#             for n in boxNodes:
#                 lm[n]=self.labelMap[n]
#             nx.draw_networkx_labels(self.G, 
#               pos=nx.get_node_attributes(self.G, 'pos'),
#               font_size=10,
#               labels=lm)
            emptyNodes = [ n for (n,m) in self.G.nodes(data=True) if m.get('shape','circle') == 'empty']
            if emptyNodes:
                lm = {} 
                for n in emptyNodes:
                    lm[n]=self.labelMap[n]
                nx.draw_networkx_labels(self.G,
                  pos=nx.get_node_attributes(self.G, 'pos'),
                  font_size=10,
                  labels=lm)
Example #30
0
def CheapestPath(Net, pNet, cost_dictionary, origin, destination, depth):
	global rscost
	global stations_data
	global track_data
	"Iterates through every simple path calculating cost and finds the cheapest"
	cost_path_dict = {}
	build_flag_dict = {}
	for path in nx.all_simple_paths(pNet, source=origin, target=destination, cutoff = depth):
		cost = 0
		build_flag = 0
		#check if we need to add cost of reloading buildings for start nodes
		if (5*(nx.get_node_attributes(Net,'obj')[path[0]].reloading_buildings)) < (nx.get_node_attributes(Net,'obj')[path[0]].ship_in + 1):
			#rscost is cost of adding reloading station
			cost += rscost
			build_flag = 1
		for i in range(0,len(path) - 1):
			cost += cost_dictionary['{0}--->{1}'.format(path[i],path[i +1])]
		cost_path_dict[cost] = path
		build_flag_dict[cost] = build_flag
	##find cheapest path and its cost
	ccost = min(list(cost_path_dict.keys()))
	cpath = cost_path_dict[ccost]
	print('For next additional unit, Cheapest Path from {0} to {1} is {2} at ${3}'.format(origin,destination,\
		cpath, ccost))
	#check if we needed to build a reloading station at start node
	if build_flag_dict[ccost] == 1:
		stations_data[cpath[0]-1][2] += 1
		print('  -build reloading building at station {0}'\
			.format(cpath[0]))
	##find recomendations based on that path
	for i in range(0,len(cpath) - 1):
		x = Net.get_edge_data(cpath[i],cpath[i+1])
		if type(x) is dict:
			#if no edges along the path are built
			y = Net.get_edge_data(cpath[i+1], cpath[i])
			if (type(y) is dict and all(y[key]['obj'].is_built == 0 for key in y) ) and all(x[key]['obj'].is_built == 0 for key in x):
				print('  -build path and track from {0} to {1}'\
					.format(cpath[i],cpath[i+1]))
				track_data.append([cpath[i],cpath[i+1],1])
			
			#if line built but at capacity, need to build another so add that cost
			elif all((x[key]['obj'].load + 1) > 10 for key in x) or all(x[key]['obj'].is_built == 0 for key in x):
				print('  -build track from {0} to {1}'\
					.format(cpath[i],cpath[i+1]))
				track_data.append([cpath[i],cpath[i+1],1])
			#iterate through each edge in dictionary of edges along that path
			#print(x) #print edge objects for debuging
			#track reloading buildings added
			rl_added = 0
			for key in x:
				#if not enough reloading stations at destination, add that cost
				if 5*(nx.get_node_attributes(Net,'obj')[cpath[i+1]].reloading_buildings+rl_added) < \
					(nx.get_node_attributes(Net,'obj')[cpath[i+1]].ship_in + 1):
					print('  -build reloading building at station {0}'\
						.format(cpath[i+1]))
					stations_data[cpath[i+1]-1][2] += 1
					rl_added += 1

	return 	[origin, destination, cpath, ccost]
Example #31
0

rnaList = [
    rotate("ACCGGUAGU", 0),
    rotate("ACAUGAUGGCCAUGU", 0),
    rotate("UCGUAACGAUACGAGCAUAGCGGCUA", 0)
]

for rna in rnaList:
    n = len(rna)
    print(f"Input RNA string {rna} of length {len(rna)}")
    pairsCount, resultPairs = find_secondary_structure(rna)
    print(f"Has secondary structure of {pairsCount} pairs: ")
    print(f"\t{resultPairs} (len {len(resultPairs)})\n")

    G = nx.Graph()
    G.add_nodes_from([(i + 1) for i in range(len(rna))])
    for node in G.nodes():
        G.nodes()[node]["pos"] = (node, node * node * node)
    for i in range(1, n):
        G.add_edge(i, i + 1)
    for i, j in resultPairs:
        G.add_edge(i, j)
    pos = nx.get_node_attributes(G, "pos")

    labels = dict([(i + 1, rna[i]) for i in range(n)])

    plt.figure(figsize=(8, 8))
    plt.axis("off")
    nx.draw(G, labels=labels, with_labels=True, pos=pos)
    plt.show()
Example #32
0
def draw_plotly_3d_scatter(in_graph):
    """COMMENT ME!

    """

    # Get the coordinate attributes back out of the graph.
    x_vals = [x for x in nx.get_node_attributes(in_graph, 'x').values()]
    y_vals = [x for x in nx.get_node_attributes(in_graph, 'y').values()]
    z_vals = [x for x in nx.get_node_attributes(in_graph, 'z').values()]

    # Create the color and size lists.
    c_vals = [x for x in nx.get_node_attributes(in_graph, 'color').values()]
    s_vals = [x for x in nx.get_node_attributes(in_graph, 'size').values()]

    # Add the points to a scatter plot.
    trace1 = go.Scatter3d(
        x=x_vals,
        y=y_vals,
        z=z_vals,
        mode='markers',
        hovertext=[x for x in in_graph.nodes()],
        marker=dict(sizemode='area', color=c_vals, size=s_vals, opacity=0.8),
    )

    # Create empty list for line segments
    Xe = []
    Ye = []
    Ze = []

    # Iterate through the edges, and draw line segments for each set.
    for edge in in_graph.edges():

        # Assign the node edge indecies.
        node_a = edge[0]
        node_b = edge[1]

        # Assign the x, y, z variables.
        xa = in_graph.node('x')[node_a]
        ya = in_graph.node('y')[node_a]
        za = in_graph.node('z')[node_a]

        xb = in_graph.node('x')[node_b]
        yb = in_graph.node('y')[node_b]
        zb = in_graph.node('z')[node_b]

        # Add these values to a corresponding list.
        # The third value is none, otherwise a plane would be drawn.
        Xe += [xa, xb, None]
        Ye += [ya, yb, None]
        Ze += [za, zb, None]

    # Add the node edges / line segments to the plot.
    trace2 = go.Scatter3d(x=Xe,
                          y=Ye,
                          z=Ze,
                          mode='lines',
                          line=go.Line(color='rgb(125,125,125)', width=1),
                          hoverinfo='none')

    # Show the plot.
    data = [trace1, trace2]
    fig = go.Figure(data=data)
    # iplot(fig)
    return fig
Example #33
0
#121
g.add_edge('Stafford','Woodbridge', distance = 121.68)
#132
g.add_edge('Stafford','Glen_Allen', distance = 88.78)
g.add_edge('Gloucester','Richmond', distance = 44.93)
g.add_edge('Gloucester','Woodbridge', distance =92.68)
'''

#Without Weights Edges 12, 113, 133, 30 , 64 (p=5)

g.add_edge('Norton', 'Wytheville', distance=85.84)
g.add_edge('Henry', 'Wytheville', distance=67.78)
g.add_edge('Henry', 'South_Hill', distance=98.14)
g.add_edge('Southampton', 'South_Hill', distance=78.99)
g.add_edge('Southampton', 'Norfolk', distance=1.50)
g.add_edge('Middlesex', 'Glen_Allen', distance=48.29)
g.add_edge('Middlesex', 'Hampton', distance=137.48)
g.add_edge('Hampton', 'Norfolk', distance=1.50)

pos = {
    city: (long, lat)
    for (city, (lat, long)) in nx.get_node_attributes(g, 'pos').items()
}
nx.draw(g, pos, with_labels=True, node_size=50)
#red current superchargers
nx.draw_networkx_nodes(g, pos, node_color='r', node_size=50, alpha=0.4)
#Blue new superchargers
print(nx.dijkstra_path(g, 'Wytheville', 'Woodbridge'))

plt.show()
Example #34
0
dol_part_FGreedy = clusteringDolphins('fg')
dol_part_IMap = clusteringDolphins('im')
dol_part_NewGir = dol_part_NewGirAll[3]

part_methods = {
    'l': dol_part_Louvain,
    'fg': dol_part_FGreedy,
    'im': dol_part_IMap,
    'ng': dol_part_NewGir
}
dfc = {}

# Number of nodes
N = len(dolphins.nodes())
# Number of males
Nm = list(nx.get_node_attributes(dolphins, 'gender').values()).count('m')

pThresh = 0.05

dfFisher = pd.DataFrame()
for m in part_methods:
    for comm in range(max(list(part_methods[m].values())) + 1):
        L0 = []
        for n in dolphins.nodes():
            if part_methods[m][n] == comm:
                L0.append(dolphins.nodes[n]['gender'])
        A = len(L0)
        Am = L0.count('m')
        oddsratio, pvalue = stats.fisher_exact(
            [[Am, A - Am], [Nm - Am, N - A - (Nm - Am)]], 'less')
        if pvalue < pThresh:
Example #35
0
    G = ox.graph_from_point(location_point,
                            network_type='drive',
                            distance=distance,
                            simplify=True)
    print(G.number_of_nodes())
    print(G.number_of_edges())
    print(G.is_directed())

# ox.save_graphml(G, filename="tt.graphml")
# ox.save_graph_shapefile(G, 'cityshape')

# %% get node properties

pos = {}
nDic = {}
x = nx.get_node_attributes(G, "x")
y = nx.get_node_attributes(G, "y")
t = nx.get_node_attributes(G, "highway")
idx = []
lat = []
lon = []
n_idx = []

for i, n in enumerate(G.nodes()):
    idx.append(i + 1)
    n_idx.append(n)
    lon.append(x[n])
    lat.append(y[n])
    nDic[n] = i + 1
    pos[n] = (x[n], y[n])
    if n in t:
Example #36
0
def get_map(serv):
    from datetime import datetime
    from pytz import timezone
    import networkx as nx
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt

    date = funct.get_data('config')
    cfg = hap_configs_dir + serv + "-" + date + ".cfg"

    print('<center>')
    print("<h3>Map from %s</h3><br />" % serv)

    G = nx.DiGraph()

    funct.get_config(serv, cfg)
    try:
        conf = open(cfg, "r")
    except IOError:
        print(
            '<div class="alert alert-danger">Can\'t read import config file</div>'
        )

    node = ""
    line_new2 = [1, ""]
    i = 1200
    k = 1200
    j = 0
    m = 0
    for line in conf:
        if "listen" in line or "frontend" in line:
            if "stats" not in line:
                node = line
                i = i - 500
        if line.find("backend") == 0:
            node = line
            i = i - 500
            G.add_node(node, pos=(k, i), label_pos=(k, i + 150))

        if "bind" in line:
            try:
                bind = line.split(":")
                if stats_port not in bind[1]:
                    bind[1] = bind[1].strip(' ')
                    bind = bind[1].split("crt")
                    node = node.strip(' \t\n\r')
                    node = node + ":" + bind[0]
                    G.add_node(node, pos=(k, i), label_pos=(k, i + 150))
            except:
                pass

        if "server " in line or "use_backend" in line or "default_backend" in line and "stats" not in line:
            if "timeout" not in line and "default-server" not in line and "#" not in line and "stats" not in line:
                i = i - 300
                j = j + 1
                if "check" in line:
                    line_new = line.split("check")
                else:
                    line_new = line.split("if ")
                if "server" in line:
                    line_new1 = line_new[0].split("server")
                    line_new[0] = line_new1[1]
                    line_new2 = line_new[0].split(":")
                    line_new[0] = line_new2[0]

                line_new[0] = line_new[0].strip(' \t\n\r')
                line_new2[1] = line_new2[1].strip(' \t\n\r')

                if j % 2 == 0:
                    G.add_node(line_new[0],
                               pos=(k + 250, i - 350),
                               label_pos=(k + 225, i - 100))
                else:
                    G.add_node(line_new[0],
                               pos=(k - 250, i - 50),
                               label_pos=(k - 225, i + 180))

                if line_new2[1] != "":
                    G.add_edge(node, line_new[0], port=line_new2[1])
                else:
                    G.add_edge(node, line_new[0])

    os.system("/bin/rm -f " + cfg)
    os.chdir(cgi_path)

    pos = nx.get_node_attributes(G, 'pos')
    pos_label = nx.get_node_attributes(G, 'label_pos')
    edge_labels = nx.get_edge_attributes(G, 'port')

    try:
        plt.figure(10, figsize=(9.5, 15))
        nx.draw(G,
                pos,
                with_labels=False,
                font_weight='bold',
                width=3,
                alpha=0.1,
                linewidths=5)
        nx.draw_networkx_nodes(G,
                               pos,
                               node_color="skyblue",
                               node_size=100,
                               alpha=0.8,
                               node_shape="p")
        nx.draw_networkx_labels(G,
                                pos=pos_label,
                                alpha=1,
                                font_color="green",
                                font_size=10)
        nx.draw_networkx_edges(G,
                               pos,
                               width=0.5,
                               alpha=0.5,
                               edge_color="#5D9CEB",
                               arrows=False)
        nx.draw_networkx_edge_labels(G,
                                     pos,
                                     label_pos=0.5,
                                     font_color="blue",
                                     labels=edge_labels,
                                     font_size=8)

        plt.savefig("map.png")
        plt.show()
    except Exception as e:
        print('<div class="alert alert-danger">' + str(e) + '</div>')

    commands = [
        "rm -f " + fullpath + "/map*.png",
        "mv %s/map.png %s/map%s.png" % (cgi_path, fullpath, date)
    ]
    funct.ssh_command("localhost", commands)
    print('<img src="/map%s.png" alt="map">' % date)
Example #37
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')

        self.reset_centrality()
        rc = rumor_center.RumorCenter()
        rc.set_data(self.data)
        rc.detect()
        rumor_centralities = nx.get_node_attributes(self.subgraph, 'centrality')

        self.reset_centrality()
        infected_nodes = set(self.subgraph.nodes())
        n = len(infected_nodes)
        posterior = {}
        included = set()
        neighbours = set()
        weights = self.data.weights
        for v in infected_nodes:
            """find the approximate upper bound by greedy searching"""
            included.clear()
            neighbours.clear()
            included.add(v)
            neighbours.add(v)
            likelihood_upper = 1
            w = {}  # effective propagation probabilities: node->w
            w_key_sorted = blist()
            w[v] = 1
            w_key_sorted.append(v)
            while len(included) < n:
                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_upper *= w[u] / w_sum
                included.add(u)
                neighbours.remove(u)
                new = nx.neighbors(self.data.graph, u)
                for h in new:
                    if h in included:
                        continue
                    neighbours.add(h)
                    # compute w for h
                    w_h2u = weights[self.data.node2index[u],self.data.node2index[h]]
                    if h in w.keys():
                        w[h] = 1-(1-w[h])*(1-w_h2u)
                    else:
                        w[h] = w_h2u
                    # 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:
                        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
                        w_key_sorted.insert(k,h)
                        #w_key_sorted[k:k] = [h]

            """find the approximate lower bound by greedy searching"""
            included.clear()
            neighbours.clear()
            included.add(v)
            neighbours.add(v)
            likelihood_lower = 1
            w = {}  # effective propagation probabilities: node->w
            w_key_sorted = blist()
            w[v] = 1
            w_key_sorted.append(v)
            while len(included) < n:
                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_lower *= w[u] / w_sum
                included.add(u)
                neighbours.remove(u)
                new = nx.neighbors(self.data.graph, u)
                for h in new:
                    if h in included:
                        continue
                    neighbours.add(h)
                    # compute w for h
                    w_h2u = weights[self.data.node2index[u], self.data.node2index[h]]
                    if h in w.keys():
                        w[h] = 1 - (1 - w[h]) * (1 - w_h2u)
                    else:
                        w[h] = w_h2u
                    """insert h into w_key_sorted, ranking by w from large to small"""
                    if h in infected_nodes:
                        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
                        w_key_sorted.insert(k, h)
                        # w_key_sorted[k:k] = [h]
            likelihood = decimal.Decimal(likelihood_lower + likelihood_upper)*rumor_centralities[v]/2
            posterior[v] = decimal.Decimal(self.prior[v])* likelihood
        nx.set_node_attributes(self.subgraph, 'centrality', posterior)
        return self.sort_nodes_by_centrality()
n_nodes = adj.shape[0]

# Some preprocessing
adj_tilde = adj + np.identity(n=adj.shape[0])
d_tilde_diag = np.squeeze(np.sum(np.array(adj_tilde), axis=1))
d_tilde_inv_sqrt_diag = np.power(d_tilde_diag, -1 / 2)
d_tilde_inv_sqrt = np.diag(d_tilde_inv_sqrt_diag)
adj_norm = np.dot(np.dot(d_tilde_inv_sqrt, adj_tilde), d_tilde_inv_sqrt)
adj_norm_tuple = us.sparse_to_tuple(scipy.sparse.coo_matrix(adj_norm))

# Features are just the identity matrix
feat_x = np.identity(n=adj.shape[0])
feat_x_tuple = us.sparse_to_tuple(scipy.sparse.coo_matrix(feat_x))

# Semi-supervised
memberships = [m - 1 for m in nx.get_node_attributes(g, 'membership').values()]

nb_classes = len(set(memberships))
targets = np.array([memberships], dtype=np.int32).reshape(-1)
one_hot_targets = np.eye(nb_classes)[targets]

# Pick one at random from each class
labels_to_keep = [
    np.random.choice(np.nonzero(one_hot_targets[:, c])[0])
    for c in range(nb_classes)
]

y_train = np.zeros(shape=one_hot_targets.shape, dtype=np.float32)
y_val = one_hot_targets.copy()

train_mask = np.zeros(shape=(n_nodes, ), dtype=np.bool)
Example #39
0
                                              solver='full')

# Set the 4 centrality measures as node attributes
nx.set_node_attributes(Gw, 'betweenness', bw)
nx.set_node_attributes(Gw, 'current_flow_closeness', cfc)
nx.set_node_attributes(Gw, 'eigenvector', ev)
nx.set_node_attributes(Gw, 'current_flow_betweenness', cfbw)
centrality_measures = ['betweenness', 'current_flow_closeness', \
                       'eigenvector', 'current_flow_betweenness']

for cm in centrality_measures:
    # Visualise the graph
    plt.figure(figsize=(10, 10))
    plt.title(cm + ' centrality measure')
    pos = nx.spring_layout(Gw, k=.25, iterations=2000)
    labs = nx.get_node_attributes(Gw, cm)
    for key, value in labs.items():
        labs[key] = round(value, 3)
    nx.draw(Gw,
            pos,
            node_color='lightblue',
            edge_color=[d['weight'] for (u, v, d) in Gw.edges(data=True)],
            width=3,
            edge_cmap=plt.cm.Blues,
            alpha=.4,
            node_size=np.add(
                np.multiply(list(nx.get_node_attributes(Gw, cm).values()),
                            10000), 200))
    nx.draw_networkx_labels(Gw,
                            pos,
                            labels=labs,
Example #40
0
    def run(self, raw_data, objects):
        graphs = []
        new_objects = []
        for overlay in self.__sub_overlays:
            g = overlay.substitutes(raw_data)
            graphs.append(g)

        graph = graphs.pop(0)
        for h in graphs:
            graph = nx.compose(graph, h)

        components = nx.connected_component_subgraphs(graph)
        closed_polygons = []
        polygons = []
        for component in components:
            minimum_cycles = planar_cycles(component)
            collected_options = itertools.chain(
                *nx.get_node_attributes(component, 'options').values())
            options = list(set(collected_options))

            # the polygons are the same as the minimum cycles
            closed_polygons += minimum_cycles
            path_graph = nx.Graph()
            path_graph.add_nodes_from(component.nodes())

            for polygon in minimum_cycles:
                polygons.append(Polygon(polygon, options))
                path_graph.add_cycle(polygon)

            remaining_graph = nx.difference(component, path_graph)
            for n in remaining_graph.nodes():
                if remaining_graph.degree(n) == 0:
                    remaining_graph.remove_node(n)
            if len(remaining_graph.edges()) > 0:
                remaining_components = nx.connected_component_subgraphs(
                    remaining_graph)
                for c in remaining_components:
                    new_objects.append(OpenGraph(c, options))

        new_objects = new_objects + polygons
        z_order_graph = nx.DiGraph()
        z_order_graph.add_nodes_from(new_objects)

        for i in range(0, len(polygons)):
            polygon1 = polygons[i]
            for j in range(i + 1, len(polygons)):
                polygon2 = polygons[j]
                if polygon1 != polygon2:
                    if polygon1.contains(polygon2):
                        z_order_graph.add_edge(polygon1, polygon2)
                    elif polygon2.contains(polygon1):
                        z_order_graph.add_edge(polygon2, polygon1)

        for obj in new_objects:
            for edge in obj.edges():
                if 'below' in graph[edge[0]][edge[1]]:
                    below = graph[edge[0]][edge[1]]['below']
                    if below != None:
                        for obj_above in new_objects:
                            if obj != obj_above:
                                if obj_above.has_edge(below.start(),
                                                      below.end()):
                                    z_order_graph.add_edge(obj, obj_above)
                if 'above' in graph[edge[0]][edge[1]]:
                    above = graph[edge[0]][edge[1]]['above']
                    if above != None:
                        for obj_below in new_objects:
                            if obj != obj_below:
                                if obj_below.has_edge(above.start(),
                                                      above.end()):
                                    z_order_graph.add_edge(obj_below, obj)
                if 'z_order' in graph[edge[0]][edge[1]]:
                    z_order = graph[edge[0]][edge[1]]['z_order']
                    if z_order != None:
                        for other_obj in new_objects:
                            if obj != other_obj:
                                if (isinstance(other_obj, Polygon)
                                        and other_obj.frame().intersects(edge)
                                    ) or (isinstance(other_obj, OpenGraph)
                                          and other_obj.intersects(edge)):
                                    if z_order == 'above':
                                        z_order_graph.add_edge(other_obj, obj)
                                    elif z_order == 'below':
                                        z_order_graph.add_edge(obj, other_obj)
                                    else:
                                        raise ValueError, "Wrong value for z_order."

        cycle_gen = nx.simple_cycles(z_order_graph)
        try:
            cycles = list(cycle_gen)
            for cycle in cycles:
                cycle_edges = [(cycle[i], cycle[i + 1])
                               for i in range(0,
                                              len(cycle) - 1)]
                for edge in cycle_edges:
                    z_order_graph.remove_edge(edge[0], edge[1])
            if cycles:
                warnings.warn(
                    "The diagram contains objects. that have an ambiguous z-order. Shaape estimates their z-order.",
                    RuntimeWarning)
        except:
            pass

        current_z_order = 0
        while z_order_graph.nodes():
            nodes_without_predecessors = [
                node for node in z_order_graph.nodes()
                if not z_order_graph.predecessors(node)
            ]
            for node in nodes_without_predecessors:
                node.set_z_order(current_z_order)
            current_z_order = current_z_order + 1
            z_order_graph.remove_nodes_from(nodes_without_predecessors)

        for o in new_objects:
            if type(o) == Polygon or type(o) == OpenGraph:
                o.reduce_nodes()
        objects = objects + new_objects
        objects.append(graph)

        self._objects = objects
        self._parsed_data = raw_data
        return
        edges = network_graph.edges()
        edgelist = []
        for u,v in edges:
            if network_graph[u][v]["weight"] > 2:
                edgelist.append((u,v))
        #colors = [G[u][v]['color'] for u,v in edges]
        #print (edgelist)
        color_map = []
        for node in network_graph.nodes(data=True):
            if "color" in node[1]:
                color_map.append(node[1]["color"])
            else:
                color_map.append("blue")
<<<<<<< HEAD
        labels = nx.get_node_attributes(network_graph, 'label')
=======
        labels = nx.get_node_attributes(network_graph, 'label') 
>>>>>>> 37c289bb9f1ea1bf3d1b2249381a42ba3f24027d

        weights = [network_graph[u][v]['weight'] for u,v in list(edgelist)]
        degree = network_graph.degree()


<<<<<<< HEAD

=======
        
>>>>>>> 37c289bb9f1ea1bf3d1b2249381a42ba3f24027d
#        blockchain_length = broadcasting_block_logs[key][0]["metadata"]["message"][1]["protocol_state"]["body"]["consensus_state"]["blockchain_length"]
        pos=nx.nx_agraph.graphviz_layout(network_graph, prog="dot", args="-Nk=30")
Example #42
0
    def train(self, max_iterations=10000, output_images_dir='images'):
        """."""

        if not os.path.isdir(output_images_dir):
            os.makedirs(output_images_dir)

        print("Ouput images will be saved in: {0}".format(output_images_dir))
        fignum = 0
        self.save_img(fignum, output_images_dir)

        for i in xrange(1, max_iterations):
            print("Iterating..{0:d}/{1}".format(i, max_iterations))
            for x in self.data:
                self.update_winner(x)

                # step 8: if number of input signals generated so far
                if i % self.lambda_ == 0 and len(self.graph.nodes()) <= self.max_nodes:
                    # find a node with the largest error
                    errorvectors = nx.get_node_attributes(self.graph, 'error')
                    import operator
                    node_largest_error = max(iteritems(errorvectors), key=operator.itemgetter(1))[0]

                    # find a node from neighbor of the node just found,
                    # with largest error
                    neighbors = self.graph.neighbors(node_largest_error)
                    max_error_neighbor = None
                    max_error = -1
                    errorvectors = nx.get_node_attributes(self.graph, 'error')
                    for n in neighbors:
                        if errorvectors[n] > max_error:
                            max_error = errorvectors[n]
                            max_error_neighbor = n

                    # insert a new unit half way between these two
                    self.pos = nx.get_node_attributes(self.graph, 'pos')

                    newnodepos = self.get_average_dist(self.pos[node_largest_error], self.pos[max_error_neighbor])
                    self.count = self.count + 1
                    newnode = self.count
                    self.graph.add_node(newnode, pos=newnodepos)

                    # insert edges between new node and other two nodes
                    self.graph.add_edge(newnode, max_error_neighbor, age=0)
                    self.graph.add_edge(newnode, node_largest_error, age=0)

                    # remove edge between the other two nodes

                    self.graph.remove_edge(max_error_neighbor, node_largest_error)

                    # decrease error variable of other two nodes by multiplying with alpha
                    errorvectors = nx.get_node_attributes(self.graph, 'error')
                    error_max_node = self.alpha * errorvectors[node_largest_error]
                    error_max_second = self.alpha * max_error
                    self.graph.add_node(max_error_neighbor, error=error_max_second)
                    self.graph.add_node(node_largest_error, error=error_max_node)

                    # initialize the error variable of newnode with max_node
                    self.graph.add_node(newnode, error=error_max_node)

                    fignum += 1
                    self.save_img(fignum, output_images_dir)

                # step 9: Decrease all error variables
                errorvectors = nx.get_node_attributes(self.graph, 'error')
                for i in self.graph.nodes():
                    olderror = errorvectors[i]
                    newerror = olderror - self.d * olderror
                    self.graph.add_node(i, error=newerror)
Example #43
0
              or link_types[links] == 'left_bell']

# set delay equal to 1 ms in edge links and equal to 2 ms in core links
fnss.set_delays_constant(fnss_topo, 1, 'ms', edge_links)
fnss.set_delays_constant(fnss_topo, 2, 'ms', core_links)

# set capacity of 10 Mbps in edge links and 40 Mbps in core links
fnss.set_capacities_constant(fnss_topo, 10, 'Mbps', edge_links)
fnss.set_capacities_constant(fnss_topo, 40, 'Mbps', core_links)

# Set buffer size constant to all interfaces
fnss.set_buffer_sizes_constant(fnss_topo, 50, 'packets')

# Now we deploy a traffic sources on right bell and traffic receivers on left
# bell
node_types = nx.get_node_attributes(fnss_topo, 'type')
core_nodes = [nodes for nodes in node_types 
              if node_types[nodes] == 'core']
left_nodes = [nodes for nodes in node_types 
              if node_types[nodes] == 'left_bell']
right_nodes = [nodes for nodes in node_types 
              if node_types[nodes] == 'right_bell']

# Set nodes on bells to be hosts and nodes on bottleneck path to be switches.
# Differently from datacenter topologies, which already provide annotation of
# what nodes are hosts and switches, we need to explicitly tell which nodes
# are switches and which are hosts to deploy the topology in Mininet 
hosts = left_nodes + right_nodes
switches = core_nodes

# Convert FNSS topology to Mininet
Example #44
0
        G.nodes[i]['pos'] = (3,x-7)
        G.nodes[i]['dem'] = retailers[i]
        x += 1

G.add_edges_from([(f,d) for f in factories for d in dcs])
G.add_edges_from([(d,r) for d in dcs for r in retailers])

# Add costs to edges
costs = [40,40,42.5,32.5,35,45,35,42.5,25,25,35,40,27.5,25,42.5,35,40,32.5,20,32.5,40,30,27.5,30]

c = 0
for i in G.edges:
    G.edges[i]['cost'] = costs[c]
    c += 1

pos = nx.get_node_attributes(G, 'pos')
sup = nx.get_node_attributes(G, 'sup')
dem = nx.get_node_attributes(G, 'dem')
cap = nx.get_node_attributes(G, 'cap')
cost = nx.get_edge_attributes(G, 'cost')


dem_loc, sup_loc, cap_loc = {}, {}, {}
for x in pos:
    if x in factories.keys():
        sup_loc[x] = (pos[x][0], pos[x][1]+.15)
    elif x in retailers.keys():
        dem_loc[x] = (pos[x][0], pos[x][1]+.15)
    else:
        if x == 'IA':
            cap_loc[x] = (pos[x][0], pos[x][1]-.15)
def get_infected(G):
    return [
        k for k, v in nx.get_node_attributes(G, STATUS).items()
        if v == INFECTED
    ]
def get_status(G):
    return nx.get_node_attributes(G, STATUS)
Example #47
0
    os.path.abspath(cmd_args.in_filename))
start_nodes = len(main_dict)

#Print plot showing kmer_distribution
fig = plt.hist([math.log10(x) for x in main_dict.values()])
#fig = plt.gcf()
plt.savefig("kmer_frequency_histo" + output_id + ".png", format="png")

#Convert Primary Dictionary to Node_set, filter out kmers with fewer than a minimum occurance cutoff
node_set = [(i, {
    'weight': j
}) for i, j in main_dict.items() if j >= cmd_args.min_node]

#Create Nodes in Network with a weight characteristic
m_graph.add_nodes_from(node_set)
node_weights = nx.get_node_attributes(m_graph, 'weight').values()

#If background is defined load the data, check enrichment levels
#Remove all nodes that have an enrichment lower than allowable enrichment.
if cmd_args.in_background != "":
    print("Input path resolved to:",
          os.path.abspath(cmd_args.in_background),
          file=error_log)
    background_dict, total_back, seq_back = load_sequences(
        os.path.abspath(cmd_args.in_background))

    back_set = [(i, {'weight': j}) for i, j in background_dict.items()]
    b_graph = nx.Graph()
    b_graph.add_nodes_from(back_set)

    remove_list = []
def any_infected(G):
    return INFECTED in nx.get_node_attributes(G, STATUS).values()
Example #49
0
def drawGraph(G, pos, a, labels):

    print(globalLabSize, "glob")
    #Changing G for threshold here

    #Check filter metric
    if (globalOptionsMet4 == "Default"):
        G = G

    if (globalOptionsMet4 == "Degree"):
        displayedNodes = []
        nx.set_node_attributes(G,
                               values=nx.degree_centrality(G),
                               name='degree')
        for node, data in G.nodes(data=True):
            if (data['degree'] > threshold):
                displayedNodes.append(node)
        G = G.subgraph(displayedNodes)

    if (globalOptionsMet4 == "Between Centrality"):
        displayedNodes = []
        # Metrics computing
        betweenCentralities = nx.betweenness_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=betweenCentralities,
                               name='betweenCentrality')

        # iterate trough nodes
        for node, data in G.nodes(data=True):
            if (data['betweenCentrality'] > threshold):
                displayedNodes.append(node)

        G = G.subgraph(displayedNodes)

    if (globalOptionsMet4 == "Load Centrality"):
        displayedNodes = []
        # Metrics computing
        loadCentralities = nx.load_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=loadCentralities,
                               name='loadCentrality')

        # iterate trough nodes
        for node, data in G.nodes(data=True):
            if (data['loadCentrality'] > threshold):
                displayedNodes.append(node)

        G = G.subgraph(displayedNodes)

    if (globalOptionsMet4 == "Subgraph Centrality"):
        displayedNodes = []
        # Metrics computing
        subgraphCentralities = nx.subgraph_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=subgraphCentralities,
                               name='subgraphCentrality')

        # iterate trough nodes
        for node, data in G.nodes(data=True):
            if (data['subgraphCentrality'] > threshold):
                displayedNodes.append(node)

        G = G.subgraph(displayedNodes)

    #Choosing size,  the size list will be defined here for the rest of the function
    sizeM = maxSize
    sizes = []
    if (globalOptionsMet3 == "Default"):
        for node, data in G.nodes(data=True):
            sizes.append(sizeM)
    if (globalOptionsMet3 == "Degree"):
        degrees = nx.degree_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G, values=degrees, name='degree')
        # minmax
        minDeg = min(degrees.values())
        maxDeg = max(degrees.values())
        #loop trough nodes
        for node, data in G.nodes(data=True):
            sizes.append(
                (normalizeSize(data['degree'], maxDeg, minDeg, sizeM) + 5))

    if (globalOptionsMet3 == "Between Centrality"):
        # Metrics computing
        betweenCentralities = nx.betweenness_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=betweenCentralities,
                               name='betweenCentrality')
        # minmax
        minBetween = min(betweenCentralities.values())
        maxBetween = max(betweenCentralities.values())
        #loop trough nodes
        for node, data in G.nodes(data=True):
            sizes.append((normalizeSize(data['betweenCentrality'], maxBetween,
                                        minBetween, sizeM) + 5))

    if (globalOptionsMet3 == "Load Centrality"):
        # Metrics computing
        loadCentralities = nx.load_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=loadCentralities,
                               name='loadCentrality')
        # minmax
        minLoad = min(loadCentralities.values())
        maxLoad = max(loadCentralities.values())
        #loop trough nodes
        for node, data in G.nodes(data=True):
            sizes.append((normalizeSize(data['loadCentrality'], maxLoad,
                                        minLoad, sizeM) + 5))

    if (globalOptionsMet3 == "Subgraph Centrality"):
        # Metrics computing
        subgraphCentralities = nx.subgraph_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=subgraphCentralities,
                               name='subgraphCentrality')
        # minmax
        minSub = min(subgraphCentralities.values())
        maxSub = max(subgraphCentralities.values())
        #loop trough nodes
        for node, data in G.nodes(data=True):
            sizes.append((normalizeSize(data['subgraphCentrality'], maxSub,
                                        minSub, sizeM) + 5))

    #Choosing cmap for colors
    cmapChosen = plt.cm.viridis
    if (cmap1 == "Viridis"):
        cmapChosen = plt.cm.viridis
    if (cmap1 == "Magma"):
        cmapChosen = plt.cm.magma
    if (cmap1 == "Plasma"):
        cmapChosen = plt.cm.plasma
    if (cmap1 == "Blues"):
        cmapChosen = plt.cm.Blues
    if (cmap1 == "Purples"):
        cmapChosen = plt.cm.Purples
    if (cmap1 == "Reds"):
        cmapChosen = plt.cm.Reds
    if (cmap1 == "Greens"):
        cmapChosen = plt.cm.Greens
    if (cmap1 == "YlOrRd"):
        cmapChosen = plt.cm.YlOrRd

    #drawing
    if (globalOptionsMet2 == "Default"):
        labelDic = nx.get_node_attributes(G, 'label')
        nx.draw_networkx_nodes(G,
                               pos=pos,
                               ax=a,
                               node_color=range(len(G)),
                               cmap=cmapChosen,
                               node_size=sizes)
        nx.draw_networkx_edges(G,
                               pos,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity,
                               ax=a)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    font_size=globalLabSize,
                                    ax=a,
                                    font_color=globalLabelCol)
    if (globalOptionsMet2 == "Communities"):
        cmapUsed = ""
        if (cmap1 == "Pale"):
            cmapUsed = ListedColormap(
                palettable.colorbrewer.qualitative.Set3_12.mpl_colors,
                N=len(G))
        if (cmap1 == "Bright"):
            cmapUsed = ListedColormap(
                palettable.colorbrewer.qualitative.Set1_9.mpl_colors, N=len(G))

        partition = community.best_partition(G)
        labelSet = nx.get_node_attributes(G, 'label')
        size = float(len(set(partition.values())))
        count = 0.
        for com in set(partition.values()):
            count = count + 1.
            list_nodes = [
                nodes for nodes in partition.keys() if partition[nodes] == com
            ]
            nx.draw_networkx_nodes(G,
                                   pos,
                                   list_nodes,
                                   label=labelSet,
                                   node_color=cmapUsed(com),
                                   ax=a,
                                   node_size=sizes)
        nx.draw_networkx_edges(G,
                               pos,
                               ax=a,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    ax=a,
                                    font_size=globalLabSize,
                                    font_color=globalLabelCol)
    if (globalOptionsMet2 == "Degree"):
        # Metrics computing
        degrees = nx.degree_centrality(G)
        # Add metrics as params of the nodes
        nx.set_node_attributes(G, values=degrees, name='degree')

        # minmax
        minDeg = min(degrees.values())
        maxDeg = max(degrees.values())

        colDeg = []

        # Set the colors that could be used next
        for node, data in G.nodes(data=True):
            colDeg.append(normalize(data['degree'], maxDeg, minDeg))

        nx.draw_networkx_nodes(G,
                               pos=pos,
                               vmax=1,
                               vmin=0,
                               cmap=cmapChosen,
                               with_labels=labels,
                               node_size=sizes,
                               node_color=colDeg,
                               ax=a)
        nx.draw_networkx_edges(G,
                               pos,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity,
                               ax=a)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    font_size=globalLabSize,
                                    ax=a,
                                    font_color=globalLabelCol)

    if (globalOptionsMet2 == "Between Centrality"):
        # Metrics computing
        betweenCentralities = nx.betweenness_centrality(G)

        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=betweenCentralities,
                               name='betweenCentrality')

        # minmax
        minBetween = min(betweenCentralities.values())
        maxBetween = max(betweenCentralities.values())

        colBetween = []

        # Set the colors that could be used next
        for node, data in G.nodes(data=True):
            colBetween.append(
                normalize(data['betweenCentrality'], maxBetween, minBetween))

        nx.draw_networkx_nodes(G,
                               pos=pos,
                               vmax=1,
                               vmin=0,
                               cmap=cmapChosen,
                               with_labels=labels,
                               node_size=sizes,
                               node_color=colBetween,
                               ax=a)
        nx.draw_networkx_edges(G,
                               pos,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity,
                               ax=a)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    font_size=globalLabSize,
                                    ax=a,
                                    font_color=globalLabelCol)

    if (globalOptionsMet2 == "Subgraph Centrality"):
        # Metrics computing
        subgraphCentralities = nx.subgraph_centrality(G)

        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=subgraphCentralities,
                               name='subgraphCentrality')

        # minmax
        minSub = min(subgraphCentralities.values())
        maxSub = max(subgraphCentralities.values())

        colSub = []

        # Set the colors that could be used next
        for node, data in G.nodes(data=True):
            colSub.append(normalize(data['subgraphCentrality'], maxSub,
                                    minSub))

        nx.draw_networkx_nodes(G,
                               pos=pos,
                               vmax=1,
                               vmin=0,
                               cmap=cmapChosen,
                               with_labels=labels,
                               node_size=sizes,
                               node_color=colSub,
                               ax=a)
        nx.draw_networkx_edges(G,
                               pos,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity,
                               ax=a)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    font_size=globalLabSize,
                                    ax=a,
                                    font_color=globalLabelCol)

    if (globalOptionsMet2 == "Load Centrality"):
        # Metrics computing
        loadCentralities = nx.load_centrality(G)

        # Add metrics as params of the nodes
        nx.set_node_attributes(G,
                               values=loadCentralities,
                               name='loadCentrality')

        # minmax
        minLoad = min(loadCentralities.values())
        maxLoad = max(loadCentralities.values())

        colLoad = []

        # Set the colors that could be used next
        for node, data in G.nodes(data=True):
            colLoad.append(normalize(data['loadCentrality'], maxLoad, minLoad))

        nx.draw_networkx_nodes(G,
                               pos=pos,
                               vmax=1,
                               vmin=0,
                               cmap=cmapChosen,
                               with_labels=labels,
                               node_size=sizes,
                               node_color=colLoad,
                               ax=a)
        nx.draw_networkx_edges(G,
                               pos,
                               edge_color=globalEdgeCol,
                               style=globalEdgeType,
                               alpha=globalEdgeOpacity,
                               ax=a)
        if (labels):
            nx.draw_networkx_labels(G,
                                    pos,
                                    font_size=globalLabSize,
                                    ax=a,
                                    font_color=globalLabelCol)
Example #50
0
    def setup(self):

        self._base_class_super_called = True

        # Code that follows the flow-graph and propagates thermo setup data down the chain
        node_types = nx.get_node_attributes(self._flow_graph, 'type')
        node_parents = nx.get_node_attributes(self._flow_graph, 'parent')
        node_port_names = nx.get_node_attributes(self._flow_graph, 'port_name')

        G = self._flow_graph

        # put all starting nodes into a FIFO queue
        queue = [node for node in self._flow_graph if G.in_degree(node) == 0]
        visited = set()  # use a set, because checking "in" on a queue is slow

        # loop over all child subsystems and push down cycle level options
        cycle_level_options = ['thermo_method', 'thermo_data', 'design']
        for child_name, child in self._children.items():
            for opt in cycle_level_options:
                if opt in child.options:
                    child.options[opt] = self.options[opt]

        # note: three kinds of nodes in graph, elements, in_ports, out_ports.
        #       The graph is a tree with (potentially) multiple separate root nodes.
        #       We will do a breadth first search, starting from all starting nodes at the same time.
        #       This makes sure that Elements with multiple inputs will have all
        #       predecessors set up before we get to them.
        while queue:
            node = queue.pop(0)
            node_type = node_types[node]

            # make sure we've already processed all predecessor nodes
            # if not skip this one, we'll hit it again later
            ready_for_node = True

            for p in G.predecessors(node):
                if p not in visited:
                    ready_for_node = False
                    break
            if not ready_for_node:
                continue

            queue.extend(G.successors(node))

            if node not in visited:

                if node_type == 'element':
                    node_element = self._get_subsystem(node)
                    node_element.pyc_setup_output_ports()

                # connection will be out_port -> in_port
                elif node_type == 'out_port':
                    src_element = self._get_subsystem(node_parents[node])
                    links = G.out_edges(node)
                    for link in links:
                        # in almost every case there should only be one link, because otherwise you are creating extra mass flow
                        # the one exception is for the cooling calcs, which get some "weak" connections from turbine and bleed srcs

                        target_element = self._get_subsystem(
                            node_parents[link[1]])

                        # if target element is None there are two options:
                        # 1) there is a sub-cycle that you need to push into
                        #        in this case, get the containing sub-cycle and push some starting nodes into its graph based on this linkage
                        # 2) they made a mistake in the element name, so throw an error
                        print(node_parents[link[1]])

                        out_port = node_port_names[node]
                        in_port = node_port_names[link[1]]
                        # this passes whatever configuration data there was from the src element to the target keyed by port names

                        if out_port not in src_element.Fl_O_data:
                            raise RuntimeError(
                                f'in {self.pathname},{src_element.pathname}.{out_port} has not been properly setup.'
                                f'something is wrong with one of your `pyc_setup_output_ports` method in {src_element.pathname}'
                            )

                        target_element.Fl_I_data[
                            in_port] = src_element.Fl_O_data[out_port]

                visited.add(node)
	def __init__(self, G):
		self._graph = G
		self.intersections = nx.get_node_attributes(G, "pos")
		self.roads = [list(G[node]) for node in G.nodes()]
def show_map(M, start=None, goal=None, path=None):
    G = M._graph
    pos = nx.get_node_attributes(G, 'pos')
    edge_trace = Scatter(
    x=list([]),
    y=list([]),
    line=Line(width=0.5,color='#888'),
    hoverinfo='none',
    mode='lines')

    for edge in G.edges():
        x0, y0 = G.node[edge[0]]['pos']
        x1, y1 = G.node[edge[1]]['pos']
        edge_trace['x'] += (x0, x1, None)
        edge_trace['y'] += (y0, y1, None)
	
    x_list = list()
    y_list = list()
    color_list = list()
    text_list = list()

    for node in G.nodes():
        x, y = G.node[node]['pos']
        x_list.append(x)
        y_list.append(y)

    for node, adjacencies in enumerate(G.adjacency()):
        color = 0
        if path and node in path:
            color = 2
        if node == start:
            color = 3
        elif node == goal:
            color = 1
        # node_trace['marker']['color'].append(len(adjacencies))
        color_list.append(color)
        node_info = "Intersection " + str(node)
        text_list.append(node_info)


    node_trace = Scatter(
        x=x_list,
        y=y_list,
        text=text_list,
        mode='markers',
        hoverinfo='text',
        marker=Marker(
            showscale=False,
            # colorscale options
            # 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' |
            # Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'
            colorscale='Hot',
            reversescale=True,
            color=color_list,
            size=10,
            colorbar=dict(
                thickness=15,
                title='Node Connections',
                xanchor='left',
                titleside='right'
            ),
            line=dict(width=2)))

    fig = Figure(data=Data([edge_trace, node_trace]),
                 layout=Layout(
                    title='<br>Network graph made with Python',
                    titlefont=dict(size=16),
                    showlegend=False,
                    hovermode='closest',
                    margin=dict(b=20,l=5,r=5,t=40),
                   
                    xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False),
                    yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False)))

    fig.show()
Example #53
0
        distance = sqrt((normalizedX - normalized_nodeX) ** 2 + (normalizedy - normalized_nodeY) ** 2)
        #print(distance)
        if distance < 1000:
            node_color[i] = 'green'
            refreshGraph(graph, node_color, fig)
            break
    '''

for file_path in file_paths:
    relative_path = relpath(file_path,
                            r"C:\Users\Chrips\Aalborg Universitet\Frederik Myrup Thiesson - data\data_for_paper")
    print(f"{relative_path}")
    # Plot Original Graph to Get Scale Factor
    nxg = nx.read_gpickle(file_path)

    label_dict = nx.get_node_attributes(nxg, 'label')
    labels = list(label_dict.values())
    values = list(set(labels))
    # create empty list for node colors
    node_color = []
    # for each node in the graph
    for lab in labels:
        if lab == 0.0:
            node_color.append('blue')
        elif lab == 1.0:
            node_color.append('red')

    pos = nx.get_node_attributes(nxg, 'pos')
    w, h = figaspect(5 / 3)
    fig, ax = plt.subplots(figsize=(w, h))
    nx.draw(nxg, pos, node_color=node_color, node_size=20, ax=ax)
Method : spectral clustering []
http://scikit-learn.org/stable/modules/clustering.html#spectral-clustering 
"""
import numpy as np
import networkx as nx
from sklearn.cluster import SpectralClustering
from sklearn import metrics

np.random.seed(1)

# Get your mentioned graph
G = nx.karate_club_graph()

# Get ground-truth: club-labels -> transform to 0/1 np-array
#     (possible overcomplicated networkx usage here)
gt_dict = nx.get_node_attributes(G, 'club')
gt = [gt_dict[i] for i in G.nodes()]
gt = np.array([0 if i == 'Mr. Hi' else 1 for i in gt])

# Get adjacency-matrix as numpy-array
adj_mat = nx.to_numpy_matrix(G)

print('ground truth')
print(gt)

# Cluster
sc = SpectralClustering(2, affinity='precomputed', n_init=100)
sc.fit(adj_mat)

# Compare ground-truth and clustering-results
print('spectral clustering')
Example #55
0
core_links = [links for links in link_types if link_types[links] == 'core']
edge_links = [links for links in link_types
              if link_types[links] == 'right_bell'
              or link_types[links] == 'left_bell']

# set delay equal to 1 ms in edge links and equal to 2 ms in core links
fnss.set_delays_constant(topology, 1, 'ms', edge_links)
fnss.set_delays_constant(topology, 2, 'ms', core_links)

# set capacity of 10 Mbps in edge links and 40 Mbps in core links
fnss.set_capacities_constant(topology, 10, 'Mbps', edge_links)
fnss.set_capacities_constant(topology, 40, 'Mbps', core_links)

# Now we deploy a traffic sources on right bell and traffic receivers on left
# bell
node_types = nx.get_node_attributes(topology, 'type')
left_nodes = [nodes for nodes in node_types
              if node_types[nodes] == 'left_bell']
right_nodes = [nodes for nodes in node_types
              if node_types[nodes] == 'right_bell']

for node in left_nodes:
    fnss.add_application(topology, node, 'receiver', {})

for node in right_nodes:
    fnss.add_application(topology, node, 'source', {})

# now create a function that generate events
def rand_request(source_nodes, receiver_nodes):
    source = random.choice(source_nodes)
    receiver = random.choice(receiver_nodes)
Example #56
0
    def __init__(self, molecule, optimize=False):
        """
        Instantiation method for FunctionalGroupExtractor.

        :param molecule: Either a filename, a pymatgen.core.structure.Molecule
            object, or a pymatgen.analysis.graphs.MoleculeGraph object.
        :param optimize: Default False. If True, then the input molecule will be
            modified, adding Hydrogens, performing a simple conformer search,
            etc.
        """

        self.molgraph = None

        if isinstance(molecule, str):
            try:
                if optimize:
                    obmol = BabelMolAdaptor.from_file(molecule,
                                                      file_format="mol")
                    # OBMolecule does not contain pymatgen Molecule information
                    # So, we need to wrap the obmol in a BabelMolAdapter
                    obmol.add_hydrogen()
                    obmol.make3d()
                    obmol.localopt()
                    self.molecule = obmol.pymatgen_mol
                else:
                    self.molecule = Molecule.from_file(molecule)
            except OSError:
                raise ValueError("Input must be a valid molecule file, a "
                                 "Molecule object, or a MoleculeGraph object.")

        elif isinstance(molecule, Molecule):
            if optimize:
                obmol = BabelMolAdaptor(molecule)
                obmol.add_hydrogen()
                obmol.make3d()
                obmol.localopt()

                self.molecule = obmol.pymatgen_mol
            else:
                self.molecule = molecule

        elif isinstance(molecule, MoleculeGraph):
            if optimize:
                obmol = BabelMolAdaptor(molecule.molecule)
                obmol.add_hydrogen()
                obmol.make3d()
                obmol.localopt()

                self.molecule = obmol.pymatgen_mol

            else:
                self.molecule = molecule.molecule
                self.molgraph = molecule

        else:
            raise ValueError("Input to FunctionalGroupExtractor must be"
                             "str, Molecule, or MoleculeGraph.")

        if self.molgraph is None:
            self.molgraph = MoleculeGraph.with_local_env_strategy(
                self.molecule, OpenBabelNN())

        # Assign a specie and coordinates to each node in the graph,
        # corresponding to the Site in the Molecule object
        self.molgraph.set_node_attributes()

        self.species = nx.get_node_attributes(self.molgraph.graph, "specie")
def test_preimage_random_grid_k_median_nb():    
    ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
          'extra_params': {}}  # node/edge symb
    Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
#    Gn = Gn[0:50]
    remove_edges(Gn)
    gkernel = 'marginalizedkernel'
    
    lmbda = 0.03 # termination probalility
    r_max = 5 # iteration limit for pre-image.
    l = 500 # update limit for random generation
#    alpha_range = np.linspace(0.5, 0.5, 1)
#    k = 5 # k nearest neighbors
    # parameters for GED function
    ged_cost='CHEM_1'
    ged_method='IPFP'
    saveGXL='gedlib'
    
    # number of graphs; we what to compute the median of these graphs. 
    nb_median_range = [2, 3, 4, 5, 10, 20, 30, 40, 50, 100]
    # number of nearest neighbors.
    k_range = [5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 100]
    
    # find out all the graphs classified to positive group 1.
    idx_dict = get_same_item_indices(y_all)
    Gn = [Gn[i] for i in idx_dict[1]]
    
#    # compute Gram matrix.
#    time0 = time.time()
#    km = compute_kernel(Gn, gkernel, True)
#    time_km = time.time() - time0    
#    # write Gram matrix to file.
#    np.savez('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm', gm=km, gmtime=time_km)
        
    
    time_list = []
    dis_ks_min_list = []
    sod_gs_list = []
    sod_gs_min_list = []
    nb_updated_list = []
    g_best = []
    for idx_nb, nb_median in enumerate(nb_median_range):
        print('\n-------------------------------------------------------')
        print('number of median graphs =', nb_median)
        random.seed(1)
        idx_rdm = random.sample(range(len(Gn)), nb_median)
        print('graphs chosen:', idx_rdm)
        Gn_median = [Gn[idx].copy() for idx in idx_rdm]
        
#        for g in Gn_median:
#            nx.draw(g, labels=nx.get_node_attributes(g, 'atom'), with_labels=True)
##            plt.savefig("results/preimage_mix/mutag.png", format="PNG")
#            plt.show()
#            plt.clf()                         
                    
        ###################################################################
        gmfile = np.load('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm.npz')
        km_tmp = gmfile['gm']
        time_km = gmfile['gmtime']
        # modify mixed gram matrix.
        km = np.zeros((len(Gn) + nb_median, len(Gn) + nb_median))
        for i in range(len(Gn)):
            for j in range(i, len(Gn)):
                km[i, j] = km_tmp[i, j]
                km[j, i] = km[i, j]
        for i in range(len(Gn)):
            for j, idx in enumerate(idx_rdm):
                km[i, len(Gn) + j] = km[i, idx]
                km[len(Gn) + j, i] = km[i, idx]
        for i, idx1 in enumerate(idx_rdm):
            for j, idx2 in enumerate(idx_rdm):
                km[len(Gn) + i, len(Gn) + j] = km[idx1, idx2]
                
        ###################################################################
        alpha_range = [1 / nb_median] * nb_median
        
        time_list.append([])
        dis_ks_min_list.append([])
        sod_gs_list.append([])
        sod_gs_min_list.append([])
        nb_updated_list.append([])
        g_best.append([])   
        
        for k in k_range:
            print('\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')
            print('k =', k)
            time0 = time.time()
            dhat, ghat, nb_updated = preimage_random(Gn, Gn_median, alpha_range, 
                range(len(Gn), len(Gn) + nb_median), km, k, r_max, l, gkernel)
                
            time_total = time.time() - time0 + time_km
            print('time: ', time_total)
            time_list[idx_nb].append(time_total)
            print('\nsmallest distance in kernel space: ', dhat) 
            dis_ks_min_list[idx_nb].append(dhat)
            g_best[idx_nb].append(ghat)
            print('\nnumber of updates of the best graph: ', nb_updated)
            nb_updated_list[idx_nb].append(nb_updated)
            
            # show the best graph and save it to file.
            print('the shortest distance is', dhat)
            print('one of the possible corresponding pre-images is')
            nx.draw(ghat, labels=nx.get_node_attributes(ghat, 'atom'), 
                    with_labels=True)
            plt.savefig('results/preimage_random/mutag_median_nb' + str(nb_median) + 
                        '_k' + str(k) + '.png', format="PNG")
    #        plt.show()
            plt.clf()
    #        print(ghat_list[0].nodes(data=True))
    #        print(ghat_list[0].edges(data=True))
        
            # compute the corresponding sod in graph space.
            sod_tmp, _ = ged_median([ghat], Gn_median, ged_cost=ged_cost, 
                                         ged_method=ged_method, saveGXL=saveGXL)
            sod_gs_list[idx_nb].append(sod_tmp)
            sod_gs_min_list[idx_nb].append(np.min(sod_tmp))
            print('\nsmallest sod in graph space: ', np.min(sod_tmp))
        
    print('\nsods in graph space: ', sod_gs_list)
    print('\nsmallest sod in graph space for each set of median graphs and k: ', 
          sod_gs_min_list)  
    print('\nsmallest distance in kernel space for each set of median graphs and k: ', 
          dis_ks_min_list) 
    print('\nnumber of updates of the best graph for each set of median graphs and k by IAM: ', 
          nb_updated_list)
    print('\ntimes:', time_list)
def test_random_preimage_2combination():
    ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
          'extra_params': {}}  # node/edge symb
    Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
#    Gn = Gn[0:12]
    remove_edges(Gn)
    gkernel = 'marginalizedkernel'
    
#    dis_mat, dis_max, dis_min, dis_mean = kernel_distance_matrix(Gn, gkernel=gkernel)
#    print(dis_max, dis_min, dis_mean)
    
    lmbda = 0.03 # termination probalility
    r_max = 10 # iteration limit for pre-image.
    l = 500
    alpha_range = np.linspace(0, 1, 11)
    k = 5 # k nearest neighbors
    
    # randomly select two molecules
    np.random.seed(1)
    idx_gi = [187, 167] # np.random.randint(0, len(Gn), 2)
    g1 = Gn[idx_gi[0]].copy()
    g2 = Gn[idx_gi[1]].copy()
    
#    nx.draw(g1, labels=nx.get_node_attributes(g1, 'atom'), with_labels=True)
#    plt.savefig("results/random_preimage/mutag10.png", format="PNG")
#    plt.show()
#    nx.draw(g2, labels=nx.get_node_attributes(g2, 'atom'), with_labels=True)
#    plt.savefig("results/random_preimage/mutag11.png", format="PNG")
#    plt.show()    
    
    ######################################################################
#    Gn_mix = [g.copy() for g in Gn]
#    Gn_mix.append(g1.copy())
#    Gn_mix.append(g2.copy())
#    
##    g_tmp = iam([g1, g2])
##    nx.draw_networkx(g_tmp)
##    plt.show()
#    
#    # compute 
#    time0 = time.time()
#    km = compute_kernel(Gn_mix, gkernel, True)
#    time_km = time.time() - time0
    
    ###################################################################
    idx1 = idx_gi[0]
    idx2 = idx_gi[1]
    gmfile = np.load('results/gram_matrix_marg_itr10_pq0.03.gm.npz')
    km = gmfile['gm']
    time_km = gmfile['gmtime']
    # modify mixed gram matrix.
    for i in range(len(Gn)):
        km[i, len(Gn)] = km[i, idx1]
        km[i, len(Gn) + 1] = km[i, idx2]
        km[len(Gn), i] = km[i, idx1]
        km[len(Gn) + 1, i] = km[i, idx2]
    km[len(Gn), len(Gn)] = km[idx1, idx1]
    km[len(Gn), len(Gn) + 1] = km[idx1, idx2]
    km[len(Gn) + 1, len(Gn)] = km[idx2, idx1]
    km[len(Gn) + 1, len(Gn) + 1] = km[idx2, idx2]
            
    ###################################################################

    time_list = []
    nb_updated_list = []
    g_best = []
    dis_ks_min_list = []
    # for each alpha
    for alpha in alpha_range:
        print('\n-------------------------------------------------------\n')
        print('alpha =', alpha)
        time0 = time.time()
        dhat, ghat, nb_updated = preimage_random(Gn, [g1, g2], [alpha, 1 - alpha], 
                                          range(len(Gn), len(Gn) + 2), km,
                                          k, r_max, l, gkernel)
        time_total = time.time() - time0 + time_km
        print('time: ', time_total)
        time_list.append(time_total)
        dis_ks_min_list.append(dhat)
        g_best.append(ghat)
        nb_updated_list.append(nb_updated)
        
    # show best graphs and save them to file.
    for idx, item in enumerate(alpha_range):
        print('when alpha is', item, 'the shortest distance is', dis_ks_min_list[idx])
        print('one of the possible corresponding pre-images is')
        nx.draw(g_best[idx], labels=nx.get_node_attributes(g_best[idx], 'atom'), 
                with_labels=True)
        plt.show()
        plt.savefig('results/random_preimage/mutag_alpha' + str(item) + '.png', format="PNG")
        plt.clf()
        print(g_best[idx].nodes(data=True))
        print(g_best[idx].edges(data=True))
            
#        # compute the corresponding sod in graph space. (alpha range not considered.)
#        sod_tmp, _ = median_distance(g_best[0], Gn_let)
#        sod_gs_list.append(sod_tmp)
#        sod_gs_min_list.append(np.min(sod_tmp))
#        sod_ks_min_list.append(sod_ks)
#        nb_updated_list.append(nb_updated)
                      
#    print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list)  
    print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list) 
    print('\nnumber of updates for each alpha: ', nb_updated_list)             
    print('\ntimes:', time_list)
def draw_subnetwork_bottom(ax, graph, edge_interest, n=4):
    """
    Creates the sub network from the original network which just includes the
    neighbors of the interesting edge. The figure displays the rating of the
    lines in the neighborhood of the critical line.

    Parameters
    ----------
    ax : matplotlib axes object
        The axes on which the networkx network would be plotted.
    graph: networkx MultiGraph object
        The networkx graph representing the power grid network.
    n : integer, optional
        Number of hops to consider from the interesting edge.. The default is 4.

    Returns
    -------
    ax : matplotlib axes object
        The axes on which the networkx network would be plotted.

    """
    rate = nx.get_edge_attributes(graph, 'rating')
    p_inj = nx.get_node_attributes(graph, 'power')
    interest = get_neighbors(graph, edge_interest, n)
    xpts = [buses.cord[n][0] for n in list(interest.nodes())]
    ypts = [buses.cord[n][1] for n in list(interest.nodes())]

    # Color by rating
    edgelist_inset = list(interest.edges(keys=True))
    nodelist_inset = list(interest.nodes())
    ewidth = []
    ecolor = []
    for e in edgelist_inset:
        alt_e = (e[1], e[0], e[2])
        if e == critical or alt_e == critical:
            ewidth.append(10)
            # if e in rate: ewidth.append(rate[e]/200.0)
            # else: ewidth.append(rate[alt_e]/200.0)
            ecolor.append('crimson')
        elif (e in t_sce) or (alt_e in t_sce):
            ewidth.append(10)
            ecolor.append('blue')
        else:
            ewidth.append(3)
            # if e in rate: ewidth.append(rate[e]/200.0)
            # else: ewidth.append(rate[alt_e]/200.0)
            ecolor.append('black')

    # Color/size by load and generation
    nsize = []
    ncolor = []
    for n in nodelist_inset:
        if p_inj[n] > 0.0:
            nsize.append(p_inj[n] / 1.0)
            ncolor.append('lightsalmon')
        elif p_inj[n] < 0.0:
            nsize.append(-p_inj[n] / 1.0)
            ncolor.append('royalblue')
        else:
            nsize.append(2.0)
            ncolor.append('limegreen')

    # elabel={(r[0],r[1]):rate[r] for r in rate \
    #         if r in edgelist_inset or (r[1],r[0],r[2]) in edgelist_inset}
    nx.draw_networkx(interest,
                     with_labels=False,
                     ax=ax,
                     nodelist=nodelist_inset,
                     pos=buses.cord,
                     node_size=nsize,
                     node_color=ncolor,
                     edgelist=edgelist_inset,
                     width=ewidth,
                     style='dashed',
                     edge_color=ecolor)
    # nx.draw_networkx_edge_labels(interest,ax=ax,pos=buses.cord,edge_labels=elabel,
    #                              font_weight='normal',font_size=25)

    ax.set_xlim(min(xpts), max(xpts))
    ax.set_ylim(min(ypts), max(ypts))
    ax.tick_params(bottom=False,
                   left=False,
                   labelleft=False,
                   labelbottom=False)
    return ax
Example #60
0
 def get_node_indx(self, node_id) -> int:
     return nx.get_node_attributes(self._graph, 'indx')[node_id]