def test_grid_graph():


    graph = nx.grid_graph([20,20])
    MC_steps = 1000000
    true_sample_trials = 1

    samples = run_markov_chain(graph, MC_steps, .1)
    #for sample in true_samples:
    #    viz(graph, sample[0], sample[1])
    viz(graph, samples[-10][1], samples[-10][2])

    #The question: Can we tune temp so that we get both rapid mixing AND polynomial concentration on the zero contradiction assignments??

    #We can falsify rapid mixing using the totally uniform samples that we have.

    non_cut_sets = [len(s[1]) for s in samples]

    sample_num_components = [len (list(
    nx.connected_components( nx.edge_subgraph(graph, x[1])) )) for x in samples]

    true_samples = test_rejection_sample(graph, true_sample_trials)

    true_non_cut_sets = [len(x[0]) for x in true_samples]
    true_num_components = [len (list( nx.connected_components( nx.edge_subgraph(graph, x[0])) )) for x in true_samples]

    print(np.mean(non_cut_sets), " vs. ", np.mean(true_non_cut_sets))

    for sample in true_samples:
        viz(graph, sample[0], sample[1])

    print(np.mean(sample_num_components), " vs. ", np.mean(true_num_components))
示例#2
0
 def test_edge_subgraph(self):
     assert (self.G.edge_subgraph([
         (1, 2), (0, 3)
     ]).adj == nx.edge_subgraph(self.G, [(1, 2), (0, 3)]).adj)
     assert (self.DG.edge_subgraph([
         (1, 2), (0, 3)
     ]).adj == nx.edge_subgraph(self.DG, [(1, 2), (0, 3)]).adj)
示例#3
0
def load_graph(a):
    global G
    if (a==0):
        try:
            G = ox.load_graphml('network.graphml') #load
            print("loaded from graphml file")   
        except: pass
    elif (a==1):
        try:
            G = ox.graph_from_xml('network.osm', bidirectional=False, simplify=True, retain_all=False)
            e = list(G.edges(data=True))
            sub_ed = []
            for el in e:
                if 'highway' in el[2]:
                    if (el[2]['highway'] in ['primary','motorway','trunk','secondary','tertiary','residential','road','unclassified']):
                        sub_ed.append(tuple([el[0],el[1],0]))

            G = nx.edge_subgraph(G,sub_ed)
            print("loaded from osm xml")
        except: pass
    if (G is None):
        try:
            G = ox.graph_from_place('Ижевск, Россия', network_type='drive',simplify=True)
            print("downloaded from internet")
        except: pass

    if (G is None):
        print('failed to load map. To download network from internet check internet connection. To load from file it\'s name should be \'network.graphml\' or \'network.xml\'')
        raise 0
    return
    def retrieve_topo_from_ONOS(self):
        logging.info("Retrieving Topology...")
        reply = json_get_req('http://%s:%d/onos/v1/devices' % (ONOS_IP, ONOS_PORT))
        if 'devices' not in reply:
            return
        devices = [dev['id'] for dev in reply['devices'] if dev['available']]
        self.G.remove_nodes_from([n for n in self.G if n not in set(devices)])
        self.G.add_nodes_from(devices)

        reply = json_get_req('http://%s:%d/onos/v1/hosts' % (ONOS_IP, ONOS_PORT))
        if 'hosts' not in reply:
            return
        for host in reply['hosts']:
            for location in host['locations']:
                self.hosts[host['id']] = location['elementId']

        reply = json_get_req('http://%s:%d/onos/v1/links' % (ONOS_IP, ONOS_PORT))
        if 'links' not in reply:
            return
        edges_set = set([(el['src']['device'], el['dst']['device']) \
                    for el in reply['links'] if el['state'] == 'ACTIVE'])
        for edge in itertools.permutations(self.G.nodes(), 2):
            is_edge_active = edge in edges_set
            self.G.add_edge(*edge, **{ 'active':is_edge_active })
            self.update_edge_metrics(is_edge_active, edge)
        self.topo = nx.edge_subgraph(self.G, edges_set)
        logging.debug("Edge set:", edges_set)
 def _set_contagion_subgraph(self):
     edge_set = set()
     for edge in self.graph.edges:
         if self.graph.edges[edge]['used_time'] == 1:
             edge_set.add(edge)
     self.contagion_subgraph = nx.Graph(
         nx.edge_subgraph(self.graph, edge_set))
示例#6
0
def plot_networkx(G, ax=None, only_true=False, edge_feature='solution', threshold=0.5):
    """G is networkx graph,
    node feature: {'pos': [r, phi, z]}
    edge feature: {"solution": []}
    """
    if ax is None:
        _, ax = plt.subplots()

    n_edges = len(G.edges())
    edge_colors = [0.]*n_edges
    true_edges = []
    for iedge,edge in enumerate(G.edges(data=True)):
        if np.isscalar(edge[2][edge_feature]):
            score = edge[2][edge_feature]
        else:
            score = edge[2][edge_feature][0]

        if score > threshold:
            edge_colors[iedge] = 'r'
            true_edges.append((edge[0], edge[1]))
        else:
            edge_colors[iedge] = 'grey'

    Gp = nx.edge_subgraph(G, true_edges) if only_true else G
    edge_colors = ['k']*len(true_edges) if only_true else edge_colors 

    pos = get_pos(Gp)

    nx.draw(Gp, pos, node_color='#A0CBE2', edge_color=edge_colors,
       width=0.5, with_labels=False, node_size=1, ax=ax, arrows=False)
示例#7
0
def get_true_subgraph(G):
    true_edges = []
    for iedge, edge in enumerate(G.edges(data=True)):
        if int(edge[2]['solution'][0]) == 1:
            true_edges.append((edge[0], edge[1]))

    Gp = nx.edge_subgraph(G, true_edges)
    return Gp
示例#8
0
def edge_type_subgraph(graph, edge_type):
    edge_types = []
    if isinstance(edge_types, list):
        edge_types = edge_type
    else:
        edge_types = [edge_type]
    subg = nx.edge_subgraph(
        graph, [(e[0], e[1])
                for e in graph.edges().data() if e[2]['type'] in edge_types])
    return subg
示例#9
0
def extract_year_mgs(mg):
    year_mgs = {}
    for year in years:
        select_edges = []
        for (u, v, k, d) in mg.edges(data=True, keys=True):
            edge_year = d["start"]
            if edge_year <= year:
                select_edges.append((u, v, k))
        year_mg = nx.edge_subgraph(mg, select_edges)
        year_mgs[year] = year_mg
    return year_mgs
示例#10
0
 def setup(self):
     # Create a path graph on five nodes.
     self.G = G = nx.path_graph(5)
     # Add some node, edge, and graph attributes.
     for i in range(5):
         G.nodes[i]['name'] = 'node{}'.format(i)
     G.edges[0, 1]['name'] = 'edge01'
     G.edges[3, 4]['name'] = 'edge34'
     G.graph['name'] = 'graph'
     # Get the subgraph induced by the first and last edges.
     self.H = nx.edge_subgraph(G, [(0, 1), (3, 4)])
 def setup(self):
     # Create a path graph on five nodes.
     self.G = G = nx.path_graph(5)
     # Add some node, edge, and graph attributes.
     for i in range(5):
         G.nodes[i]['name'] = 'node{}'.format(i)
     G.edges[0, 1]['name'] = 'edge01'
     G.edges[3, 4]['name'] = 'edge34'
     G.graph['name'] = 'graph'
     # Get the subgraph induced by the first and last edges.
     self.H = nx.edge_subgraph(G, [(0, 1), (3, 4)])
示例#12
0
 def setup_class(cls):
     # Create a path graph on five nodes.
     cls.G = G = nx.path_graph(5)
     # Add some node, edge, and graph attributes.
     for i in range(5):
         G.nodes[i]["name"] = f"node{i}"
     G.edges[0, 1]["name"] = "edge01"
     G.edges[3, 4]["name"] = "edge34"
     G.graph["name"] = "graph"
     # Get the subgraph induced by the first and last edges.
     cls.H = nx.edge_subgraph(G, [(0, 1), (3, 4)])
示例#13
0
def eval_recuit(sol, graph, terms):
    #arretes du graphe
    edges = list(nx.edges(graph))
    #arretes du sous graphe
    edgesSub = []
    #on selectionne les arretes à 1 dans 'sol'
    for i in range(len(edges)):
        if (sol[i]):
            edgesSub.append(edges[i])

    #sous graphe à partir des arretes selectionees
    subGraph = nx.edge_subgraph(graph, edgesSub)

    #somme des poids de ce sous-graphe
    sumW = subGraph.size(weight="weight")
    #nombre de termes non-reliés
    nbTermsNR = 0

    #termes présents dans le sous-graphe
    sg_terms = []
    for t in terms:
        if (subGraph.has_node(t)):
            sg_terms.append(t)
        else:
            #print(t," is not in subGraph ! ")
            # si le noeud n'est pas dans le sous-graphe alors il n'est pas relié
            nbTermsNR += 1

    linked = False
    for t1 in sg_terms:
        for t2 in sg_terms:
            if t1 != t2 and nx.has_path(subGraph, t1, t2):
                #print(t1,"is linked with",t2)
                linked = True
                break

        if not linked:
            nbTermsNR += 1
    #print_graph(subGraph, sg_terms)
    #print(nbTermsNR,"node not linked")

    Mt = graph.size()
    Mc = graph.size() / 2

    nbCC = len(list(nx.connected_components(subGraph)))

    res = sumW + Mt * nbTermsNR + Mc * (nbCC - 1)

    #print("sumW :",sumW,"NR :",nbTermsNR,"nbCC :",nbCC,"eval :",res)

    return res
示例#14
0
def check_simple_cycle(graph, edges):
    edge_list = []
    for x in edges:
        edge = []
        for e in x:
            edge.append(e)
        edge = tuple(edge)
        edge_list.append(edge)
    H = nx.edge_subgraph(graph, edge_list)
    if len(H) == 0:
        return True
    if not nx.is_connected(H):
        return False
    if np.max(list(dict(nx.degree(H)).values())) > 2:
        return False
    return True
示例#15
0
def drawSolGraph(graph, terms, sol):
    #arretes du graphe
    edges = list(nx.edges(graph))
    #arretes du sous graphe
    edgesSub = []
    #on selectionne les arretes à 1 dans 'sol'
    for i in range(len(edges)):
        if (sol[i]):
            edgesSub.append(edges[i])

    #sous graphe à partir des arretes selectionees
    subGraph = nx.edge_subgraph(graph, edgesSub)

    sg_terms = []
    for t in terms:
        if (subGraph.has_node(t)):
            sg_terms.append(t)

    print_graph(subGraph, sg_terms)
示例#16
0
 def __call__(self):
     
     G = self.G
     n = len(G)
     
     # Get list of edges with positive value in the current LP solution
     elist = [e for e in G.edges if self.get_values(G[e[0]][e[1]]['var'])>1.0E-5]
     
     # If with animation, show the (fractional) solution
     if self.anim:
         objv = self.get_objective_value()
         xval = [ self.get_values( G[e[0]][e[1]]['var'] ) for e in elist ]
         efrac = [ e for e in elist if self.get_values(G[e[0]][e[1]]['var'])< 0.99 ]
         plotEdgeList(self.problem, elist, specialEdges=efrac,\
               title="Fractional LP solution of objval="+str(objv) )
     
     # Build the solution's support graph, i.e. the sub-graph induced by
     # the edges in the list "elist" found above.
     supG = nx.edge_subgraph(G, elist)
     
     # If the support graph G is not connected, build the SCE constraint
     # based on the graph's smallest component.
     S = min( nx.connected_components(supG), key=len )
     weight = 1.0
     
     # If the graph is connected, obtain the minimum-weight cut set
     # using the algorithm of Stoer and Wagner
     if len(S)==n:
         # Set the edge weights equal to the variable's solution values
         for e in elist:
             i, j =  e[0], e[1]
             supG[i][j]['weight'] = self.get_values( G[i][j]['var'] )
         weight, part = nx.stoer_wagner(supG)    
         S = part[0] if len(part[0]) < len(part[1]) else part[1]            
     
     # If the cutset constraint is violated, include the cut in 
     # form of the equivalent subcycle elimination constraint
     if ( len(S) > 2 ) and ( weight < 1.98):
         E = list(combinat(S,2))
         cycVars = [ G[e[0]][e[1]]['var'] for e in E ]
         self.add( cplex.SparsePair(cycVars, [1.0]*len(E) ), sense="L", \
                   rhs=len(S)-1)           
示例#17
0
def build_adj_lcc(args):
    """Build the mutual mention largest connected component from
    a csv edgelist with weights."""

    logging.info("Read '%s' into an adjacency matrix",
                 args.weighted_edgelist_path)
    adj_orig, uid2orig = csv_to_adj(args.weighted_edgelist_path)

    logging.info("Convert adjacency matrix to a graph")
    g_orig = nx.from_scipy_sparse_matrix(adj_orig,
                                         create_using=nx.DiGraph,
                                         edge_attribute='weight')

    logging.info("Extract mutual mention graph from full graph")
    g_mutual = nx.edge_subgraph(g_orig, [
        e for e in g_orig.edges
        if is_edge_mutual(g_orig, e, args.mutual_threshold)
    ])
    g_mutual = nx.Graph(g_mutual)

    logging.info("Extract largest connected component from "
                 "mutual mention graph")
    g_lcc = g_mutual.subgraph(max(nx.connected_components(g_mutual), key=len))

    logging.info("Relabel largest connected component node ids")
    orig2lcc = dict((id, n) for n, id in enumerate(g_lcc.nodes))
    g_lcc = nx.relabel_nodes(g_lcc, orig2lcc)
    assert set(g_lcc.nodes) == set(range(len(g_lcc.nodes))), \
        "Mutual mention lcc does not cover all node ids"

    logging.info("Convert largest connected component to adjacency matrix")
    adj_lcc = nx.to_scipy_sparse_matrix(g_lcc,
                                        dtype=np.uint32,
                                        weight='weight',
                                        nodelist=sorted(g_lcc.nodes()))
    adj_lcc = coo_matrix(adj_lcc)

    logging.info("Number of nodes in the largest connected component = %s",
                 len(g_lcc.nodes))

    return adj_lcc, uid2orig, orig2lcc
示例#18
0
def get_tracks2(G, th=0.5, feature_name='solution'):
    used_nodes = []
    sub_graphs = []
    for node in G.nodes():
        if node in used_nodes:
            continue
        a_track = longest_track(G,
                                node,
                                used_nodes,
                                th=th,
                                feature_name=feature_name)
        if len(a_track) < 1:
            used_nodes.append(node)
            continue

        sub = nx.edge_subgraph(G, a_track)
        sub_graphs.append(sub)
        used_nodes += list(sub.nodes())

    n_tracks = len(sub_graphs)
    print("total tracks:", n_tracks)
    return sub_graphs
示例#19
0
def generate_unions_of_paths(n=4, l=3, r=3, random=False):
    graph = complete_layered_digraph(n, l)
    paths = nx.all_simple_paths(graph, graph.graph["s"], graph.graph["t"])
    '''
    We could do a backtraking tree, where we put edges either into J (the edge set
    ), or its complement. Checking whether such an assignment can be completed amounts to verifying
    that the complement is not a cut of the layered complete digraph, so we can delete and 
    ask networkx for connectivity. In some cases it may be that certain sets of edges
    will necessarily be included, but maybe sufficiently efficient to find them on later levels of the tree.
    Of course there are still a lot of connected layered digraphs. ... actually most likely the ones that are unions of paths are most interesting, since we want there to be many paths before we set weights.
    '''
    paths_as_edge_sets = [set(path_to_edge(path)) for path in paths]
    merged = []
    num_paths = len(paths_as_edge_sets)

    if random == False:
        path_iterator = itertools.combinations(paths_as_edge_sets, r)
    else:
        path_iterator = random_subset_iterator(paths_as_edge_sets, r)

    for path_collection in path_iterator:
        merge = set()
        for path in path_collection:
            merge = merge | path
        subgraph = nx.DiGraph(nx.edge_subgraph(graph, list(merge)))
        for x in graph.nodes():
            if x not in subgraph.nodes():
                subgraph.add_node(x)
        t = 0
        for x in subgraph.nodes():
            #print(x)
            subgraph.nodes[x]["pos"] = np.array([x[0], x[1]])
            subgraph.nodes[x]["label"] = t
            t += 1
            subgraph.nodes[x]["weight"] = 0

        yield subgraph
示例#20
0
def node(node_id):
    return str(
        json.dumps(
            nx.node_link_data(
                nx.edge_subgraph(G,
                                 list(nx.edges(G, node_id))[:20]))))
示例#21
0
 def test_edge_subgraph(self):
     assert_equal(self.G.edge_subgraph([(1, 2), (0, 3)]).adj,
                  nx.edge_subgraph(self.G, [(1, 2), (0, 3)]).adj)
     assert_equal(self.DG.edge_subgraph([(1, 2), (0, 3)]).adj,
                  nx.edge_subgraph(self.DG, [(1, 2), (0, 3)]).adj)
示例#22
0
 def graph(self):
     return nx.edge_subgraph(self._graph, self.reference_edges)
示例#23
0
def statistics():
    trials = 100
    #samples = trials*500
    m = 20
    graph = nx.grid_graph([m, m])
    graph.name = "grid_size:" + str(m)
    print(graph.name)
    for x in graph.nodes():
        graph.nodes[x]["pos"] = np.array([x[0], x[1]])

    dual = Facefinder.planar_dual(graph)
    W_trees = []
    branches = []

    supernode = find_supernode(dual)
    boundary_faces = list(dual.neighbors(supernode))
    face_1 = boundary_faces[0]
    face_2 = boundary_faces[int(len(boundary_faces) / 2) + 1]

    #if (face_1, supernode) or (supernode, face_1) in dual.edges():
    #    print("OK")
    #if (face_2, supernode) or (supernode, face_2) in dual.edges():
    #    print("OK2")

    cycles = []

    cycles_containing_prescribed_faces = []
    corresponding_walks = []

    boundary_faces_frequencies = {}
    for face_a in boundary_faces:
        for face_b in boundary_faces:
            boundary_faces_frequencies[(face_a, face_b)] = 0

    #print("testing", boundary_faces_frequencies[ ( face_1, face_2)])
    done = False
    sample_counter = 0
    while not done:
        tree = nx.to_undirected(random_spanning_tree_wilson(dual))
        available_edges = set(dual.edges())
        available_edges = available_edges - set(tree.edges())
        e = random.choice(list(available_edges))
        unicycle = copy.deepcopy(tree)
        unicycle = nx.Graph(unicycle)
        unicycle.add_edge(e[0], e[1])
        #cycles.append(simple_cycle(unicycle))
        cycle = simple_cycle(unicycle)
        #faces = [x[0] for x in cycle] + [x[1] for x in cycle]
        #faces = set(faces)
        #print(faces)
        for face_a in boundary_faces:
            for face_b in boundary_faces:
                if (face_a, supernode) in cycle or (supernode,
                                                    face_a) in cycle:
                    if (face_b, supernode) in cycle or (supernode,
                                                        face_b) in cycle:
                        boundary_faces_frequencies[(face_a, face_b)] += 1

        face_a = face_1
        face_b = face_2
        if (face_a, supernode) in cycle or (supernode, face_a) in cycle:
            if (face_b, supernode) in cycle or (supernode, face_b) in cycle:
                #print('1')
                cycles_containing_prescribed_faces.append(cycle)
                walk = nx.Graph(nx.edge_subgraph(dual, cycle))
                walk.remove_node(supernode)
                corresponding_walks.append(walk)
                sample_counter += 1
                print(sample_counter)
        if sample_counter == trials:
            done = True

    #print("testing2", boundary_faces_frequencies[ (face_1, face_2)])
    #print(corresponding_walks[0].edges())
    #print(len(cycles_containing_prescribed_faces))

    #print(boundary_faces_frequencies)
    print("finished with cycle portions")
    LERW = []
    dual.remove_node(supernode)
    #Because we are testing whether the distributions looks like a LERW in the grid
    #portion of the dual graph
    for i in range(trials):
        trip = random_walk_until_hit(dual, face_1, set([face_2]))
        new_branch, branch_length = loop_erasure(trip)
        LERW.append(new_branch)

    return LERW, corresponding_walks, dual, boundary_faces_frequencies
示例#24
0
 def to_edge_type_graph(self, edge_type):
     type_edges = self.edges[self.edges["edge_type"] == edge_type]
     view = nx.edge_subgraph(self.g, type_edges.index)
     return MaggotGraph(view, self.nodes, type_edges)
示例#25
0
def filtered_graph(graph, min_clearing):
    subG = nx.edge_subgraph(
        graph,
        ((u, v)
         for u, v, c in graph.edges(data='clearing') if c >= min_clearing))
    return subG  # max(nx.connected_component_subgraphs(subG), key=len)
示例#26
0
 def edge_subgraph(self, edges):
     return nx.edge_subgraph(self.Graph, edges)
示例#27
0
        generation = newgen.copy()
        prevdist = bestdist

    try:
        '''
        Draw visual representation 
        '''
        # Plot matrix path
        if matrix_plot:
            '''
            Adjacency Matrix plot
            '''
            matrix = np.array(mat)
            g = nx.from_numpy_matrix(matrix)
            g = nx.relabel_nodes(g, dict(zip(range(len(labels)), labels)))
            g = nx.edge_subgraph(g, [(labels[best[i]], labels[best[i + 1]])
                                     for i in range(len(best) - 1)])
            pos = nx.shell_layout(g)
            '''
            Path 
            '''
            nx.draw_networkx(g)
            plt.title("Traveling Salesman Route")
            plt.show()
        # Plot points and connections
        elif points_plot:
            x = [points[best[i]][0] for i in range(len(points))]
            y = [points[best[i]][1] for i in range(len(points))]
            for i in range(len(best) - 1):
                plt.plot(x[i:i + 2], y[i:i + 2], 'ro-')
            plt.show()
    except:
示例#28
0
 def subgraph(self, edges):
     return nx.DiGraph(nx.edge_subgraph(self.graph(), edges))