Exemple #1
1
def test_union_all_and_compose_all():
    K3=nx.complete_graph(3)
    P3=nx.path_graph(3)

    G1=nx.DiGraph()
    G1.add_edge('A','B')
    G1.add_edge('A','C')
    G1.add_edge('A','D')
    G2=nx.DiGraph()
    G2.add_edge('1','2')
    G2.add_edge('1','3')
    G2.add_edge('1','4')

    G=nx.union_all([G1,G2])
    H=nx.compose_all([G1,G2])
    assert_edges_equal(G.edges(),H.edges())
    assert_false(G.has_edge('A','1'))
    assert_raises(nx.NetworkXError, nx.union, K3, P3)
    H1=nx.union_all([H,G1],rename=('H','G1'))
    assert_equal(sorted(H1.nodes()),
        ['G1A', 'G1B', 'G1C', 'G1D',
         'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])

    H2=nx.union_all([H,G2],rename=("H",""))
    assert_equal(sorted(H2.nodes()),
        ['1', '2', '3', '4',
         'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])

    assert_false(H1.has_edge('NB','NA'))

    G=nx.compose_all([G,G])
    assert_edges_equal(G.edges(),H.edges())

    G2=nx.union_all([G2,G2],rename=('','copy'))
    assert_equal(sorted(G2.nodes()),
        ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])

    assert_equal(G2.neighbors('copy4'),[])
    assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4'])
    assert_equal(len(G),8)
    assert_equal(nx.number_of_edges(G),6)

    E=nx.disjoint_union_all([G,G])
    assert_equal(len(E),16)
    assert_equal(nx.number_of_edges(E),12)

    E=nx.disjoint_union_all([G1,G2])
    assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

    G1=nx.DiGraph()
    G1.add_edge('A','B')
    G2=nx.DiGraph()
    G2.add_edge(1,2)
    G3=nx.DiGraph()
    G3.add_edge(11,22)
    G4=nx.union_all([G1,G2,G3],rename=("G1","G2","G3"))
    assert_equal(sorted(G4.nodes()),
        ['G1A', 'G1B', 'G21', 'G22',
         'G311', 'G322'])
Exemple #2
0
    def __init__(
        self,
        design_teams: Dict[str, Dict[str, List[int]]],
        dw_teams: Dict[str, Dict[str, List[int]]],
    ):
        all_design_groupings = []
        all_dw_groupings = []
        self.ged = gm.GraphEditDistance(
            1, 1, 1, 1
        )  # All edit costs are equal to 1 (these are modification costs, which are not weights of the graphs' edges to be very precise)

        for class_number, values in design_teams.items():
            for team_number, team_members in values.items():
                temp = nx.complete_graph(
                    team_members
                )  # Each team is considered to be a complete subgraph (clique)
                all_design_groupings.append(temp)

        for class_number, values in dw_teams.items():
            for team_number, team_members in values.items():
                temp = nx.complete_graph(
                    team_members
                )  # Each team is considered to be a complete subgraph (clique)
                all_dw_groupings.append(temp)

        # We can do a union all since no person is in 2 different groups for the same course
        self.design_graph = nx.union_all(all_design_groupings)
        self.dw_graph = nx.union_all(all_dw_groupings)

        # We only need to compare the 03.007 groupings and the 10.009 groupings to each other once, even as we utilize different benchmarks/thresholds in the subsequent functions/methods (order matters for non-symmetrical cost matrices and their corresponding "meanings")
        self.design_dw_result = self.ged.compare(
            [self.design_graph, self.dw_graph], None
        )
Exemple #3
0
    def __init__(
        self,
        design_teams: Dict[str, Dict[str, List[int]]],
        dw_teams: Dict[str, Dict[str, List[int]]],
    ):
        all_design_groupings = []
        all_dw_groupings = []

        for class_number, values in design_teams.items():
            for team_number, team_members in values.items():
                temp = nx.complete_graph(
                    team_members
                )  # Each team is considered to be a complete subgraph
                all_design_groupings.append(temp)

        for class_number, values in dw_teams.items():
            for team_number, team_members in values.items():
                temp = nx.complete_graph(
                    team_members
                )  # Each team is considered to be a complete subgraph
                all_dw_groupings.append(temp)

        # We can do a union all since no person is in 2 different groups for the same course
        self.design_graph = nx.union_all(all_design_groupings)
        self.dw_graph = nx.union_all(all_dw_groupings)
Exemple #4
0
def test_union_all_and_compose_all():
    K3 = nx.complete_graph(3)
    P3 = nx.path_graph(3)

    G1 = nx.DiGraph()
    G1.add_edge('A', 'B')
    G1.add_edge('A', 'C')
    G1.add_edge('A', 'D')
    G2 = nx.DiGraph()
    G2.add_edge('1', '2')
    G2.add_edge('1', '3')
    G2.add_edge('1', '4')

    G = nx.union_all([G1, G2])
    H = nx.compose_all([G1, G2])
    assert_edges_equal(G.edges(), H.edges())
    assert not G.has_edge('A', '1')
    pytest.raises(nx.NetworkXError, nx.union, K3, P3)
    H1 = nx.union_all([H, G1], rename=('H', 'G1'))
    assert (sorted(H1.nodes()) ==
            ['G1A', 'G1B', 'G1C', 'G1D',
             'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])

    H2 = nx.union_all([H, G2], rename=("H", ""))
    assert (sorted(H2.nodes()) ==
            ['1', '2', '3', '4',
                  'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])

    assert not H1.has_edge('NB', 'NA')

    G = nx.compose_all([G, G])
    assert_edges_equal(G.edges(), H.edges())

    G2 = nx.union_all([G2, G2], rename=('', 'copy'))
    assert (sorted(G2.nodes()) ==
            ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])

    assert sorted(G2.neighbors('copy4')) == []
    assert sorted(G2.neighbors('copy1')) == ['copy2', 'copy3', 'copy4']
    assert len(G) == 8
    assert nx.number_of_edges(G) == 6

    E = nx.disjoint_union_all([G, G])
    assert len(E) == 16
    assert nx.number_of_edges(E) == 12

    E = nx.disjoint_union_all([G1, G2])
    assert sorted(E.nodes()) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    G1 = nx.DiGraph()
    G1.add_edge('A', 'B')
    G2 = nx.DiGraph()
    G2.add_edge(1, 2)
    G3 = nx.DiGraph()
    G3.add_edge(11, 22)
    G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3"))
    assert (sorted(G4.nodes()) ==
            ['G1A', 'G1B', 'G21', 'G22',
             'G311', 'G322'])
Exemple #5
0
def test_union_all_and_compose_all():
    K3 = nx.complete_graph(3)
    P3 = nx.path_graph(3)

    G1 = nx.DiGraph()
    G1.add_edge("A", "B")
    G1.add_edge("A", "C")
    G1.add_edge("A", "D")
    G2 = nx.DiGraph()
    G2.add_edge("1", "2")
    G2.add_edge("1", "3")
    G2.add_edge("1", "4")

    G = nx.union_all([G1, G2])
    H = nx.compose_all([G1, G2])
    assert_edges_equal(G.edges(), H.edges())
    assert_false(G.has_edge("A", "1"))
    assert_raises(nx.NetworkXError, nx.union, K3, P3)
    H1 = nx.union_all([H, G1], rename=("H", "G1"))
    assert_equal(sorted(H1.nodes()), ["G1A", "G1B", "G1C", "G1D", "H1", "H2", "H3", "H4", "HA", "HB", "HC", "HD"])

    H2 = nx.union_all([H, G2], rename=("H", ""))
    assert_equal(sorted(H2.nodes()), ["1", "2", "3", "4", "H1", "H2", "H3", "H4", "HA", "HB", "HC", "HD"])

    assert_false(H1.has_edge("NB", "NA"))

    G = nx.compose_all([G, G])
    assert_edges_equal(G.edges(), H.edges())

    G2 = nx.union_all([G2, G2], rename=("", "copy"))
    assert_equal(sorted(G2.nodes()), ["1", "2", "3", "4", "copy1", "copy2", "copy3", "copy4"])

    assert_equal(G2.neighbors("copy4"), [])
    assert_equal(sorted(G2.neighbors("copy1")), ["copy2", "copy3", "copy4"])
    assert_equal(len(G), 8)
    assert_equal(nx.number_of_edges(G), 6)

    E = nx.disjoint_union_all([G, G])
    assert_equal(len(E), 16)
    assert_equal(nx.number_of_edges(E), 12)

    E = nx.disjoint_union_all([G1, G2])
    assert_equal(sorted(E.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

    G1 = nx.DiGraph()
    G1.add_edge("A", "B")
    G2 = nx.DiGraph()
    G2.add_edge(1, 2)
    G3 = nx.DiGraph()
    G3.add_edge(11, 22)
    G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3"))
    assert_equal(sorted(G4.nodes()), ["G1A", "G1B", "G21", "G22", "G311", "G322"])
Exemple #6
0
 def __merge_cloud(self):
     '合并多个cloud'
     print "Processing: merging cloud into big cloud... "
     for eachFD in self.regs:
         succs = self.successors(eachFD)  #其中的每一个succ都是nx.DiGraph()
         if len(succs) <= 1:
             continue
         else:
             big_cloud = nx.union_all(succs)
         pre_fds = set()
         succ_fds = set()
         for succ_cloud in self.successors(eachFD):
             pre_fds = pre_fds.union(set(self.predecessors(succ_cloud)))
             succ_fds = succ_fds.union(set(self.successors(succ_cloud)))
             self.remove_node(succ_cloud)
         self.add_node(big_cloud)
         for pre_fd in pre_fds:
             self.add_edge(pre_fd, big_cloud)
         for succ_fd in succ_fds:
             self.add_edge(big_cloud, succ_fd)
     self.big_clouds = [
         node for node in self.nodes_iter() if isinstance(node, nx.DiGraph)
     ]
     print "Note: merge_cloud() successfully."
     return None
Exemple #7
0
 def direct_sum(modules: List):
     subclass = type(modules[0])
     new_graph = nx.union_all([m.graph for m in modules])
     return subclass(modules[0].ring, modules[0].left_algebra,
                     modules[0].right_algebra,
                     modules[0].left_scalar_action,
                     modules[0].right_scalar_action, new_graph)
Exemple #8
0
def create_karakte_mirror_network(edge_labels, node_labels):
    """
    creates the mirrored karakte network as a networkx graph
    args:
        edge_weight : A dict with values label name : {"random" | float} value used for the edge
                    weight. If random is selected then a uniform distributed random value is used.
        node_labels : A dict with the node label name and value.

    returns:
        A networkx graph of the karakte club
    """
    graph = create_karakte_network(edge_labels, node_labels)

    # add the mirrored part of the network
    graph_mirror = graph.copy()
    offset = max(list(graph.nodes)) + 1
    mapping = dict([(x, x + offset) for x in list(graph_mirror.nodes)])
    graph_mirror = nx.relabel_nodes(graph_mirror, mapping)
    graph = nx.union_all([graph, graph_mirror])

    #add single connection
    graph.add_edge(1, 1 + offset, weight=0.5)
    graph.add_edge(1 + offset, 1, weight=0.5)

    return graph
Exemple #9
0
def test_union_all_attributes():
    g = nx.Graph()
    g.add_node(0, x=4)
    g.add_node(1, x=5)
    g.add_edge(0, 1, size=5)
    g.graph["name"] = "g"

    h = g.copy()
    h.graph["name"] = "h"
    h.graph["attr"] = "attr"
    h.nodes[0]["x"] = 7

    j = g.copy()
    j.graph["name"] = "j"
    j.graph["attr"] = "attr"
    j.nodes[0]["x"] = 7

    ghj = nx.union_all([g, h, j], rename=("g", "h", "j"))
    assert set(ghj.nodes()) == {"h0", "h1", "g0", "g1", "j0", "j1"}
    for n in ghj:
        graph, node = n
        assert ghj.nodes[n] == eval(graph).nodes[int(node)]

    assert ghj.graph["attr"] == "attr"
    assert ghj.graph["name"] == "j"  # j graph attributes take precendent
Exemple #10
0
def test_union_all_attributes():
    g = nx.Graph()
    g.add_node(0, x=4)
    g.add_node(1, x=5)
    g.add_edge(0, 1, size=5)
    g.graph['name'] = 'g'

    h = g.copy()
    h.graph['name'] = 'h'
    h.graph['attr'] = 'attr'
    h.nodes[0]['x'] = 7

    j = g.copy()
    j.graph['name'] = 'j'
    j.graph['attr'] = 'attr'
    j.nodes[0]['x'] = 7

    ghj = nx.union_all([g, h, j], rename=('g', 'h', 'j'))
    assert set(ghj.nodes()) == set(['h0', 'h1', 'g0', 'g1', 'j0', 'j1'])
    for n in ghj:
        graph, node = n
        assert ghj.nodes[n] == eval(graph).nodes[int(node)]

    assert ghj.graph['attr'] == 'attr'
    assert ghj.graph['name'] == 'j'  # j graph attributes take precendent
Exemple #11
0
def test_union_all_attributes():
    g = nx.Graph()
    g.add_node(0, x=4)
    g.add_node(1, x=5)
    g.add_edge(0, 1, size=5)
    g.graph["name"] = "g"

    h = g.copy()
    h.graph["name"] = "h"
    h.graph["attr"] = "attr"
    h.node[0]["x"] = 7

    j = g.copy()
    j.graph["name"] = "j"
    j.graph["attr"] = "attr"
    j.node[0]["x"] = 7

    ghj = nx.union_all([g, h, j], rename=("g", "h", "j"))
    assert_equal(set(ghj.nodes()), set(["h0", "h1", "g0", "g1", "j0", "j1"]))
    for n in ghj:
        graph, node = n
        assert_equal(ghj.node[n], eval(graph).node[int(node)])

    assert_equal(ghj.graph["attr"], "attr")
    assert_equal(ghj.graph["name"], "j")  # j graph attributes take precendent
Exemple #12
0
def find_errors(theNetwork, G):
    """
    This function attempt to find potential topology errors in the stream network.
    :param theNetwork:
    :param G: multidgraph
    :return:
    """
    net_ids = theNetwork.attribute_as_list(G, "_netid_")
    # iterate through list of network IDs generate attributes, and produce a subnetwork graph
    list_subnets = []
    for id in net_ids:
        arcpy.AddMessage("Finding errors for subnet {0}...".format(id))
        subnet_G = theNetwork.select_by_attribute(G, "_netid_", id)
        duplicates_G = theNetwork.error_dup(subnet_G)
        outflow_G = theNetwork.error_outflow(subnet_G)
        conf_G = theNetwork.error_confluence(subnet_G)
        # merge all error graphs
        error_G = nx.compose_all([subnet_G, duplicates_G, conf_G, outflow_G])
        list_subnets.append(error_G)
        arcpy.AddMessage("Subnetwork #{} complete...".format(id))

    # Union all subnetwork graphs
    union_G = nx.union_all(list_subnets)
    if theNetwork.check_attribute(union_G, "_edgetype_"):
        theNetwork.delete_attribute(union_G, "_edgetype_")
    return union_G
Exemple #13
0
def test_union_all_attributes():
    g = nx.Graph()
    g.add_node(0, x=4)
    g.add_node(1, x=5)
    g.add_edge(0, 1, size=5)
    g.graph['name'] = 'g'

    h = g.copy()
    h.graph['name'] = 'h'
    h.graph['attr'] = 'attr'
    h.node[0]['x'] = 7

    j = g.copy()
    j.graph['name'] = 'j'
    j.graph['attr'] = 'attr'
    j.node[0]['x'] = 7

    ghj = nx.union_all([g, h, j], rename=('g', 'h', 'j'))
    assert_equal( set(ghj.nodes()) , set(['h0', 'h1', 'g0', 'g1', 'j0', 'j1']) )
    for n in ghj:
        graph, node = n
        assert_equal( ghj.node[n], eval(graph).node[int(node)] )

    assert_equal(ghj.graph['attr'],'attr')
    assert_equal(ghj.graph['name'],'j') # j graph attributes take precendent
Exemple #14
0
def trim_small_components(G,threshold=None):
    """Remove small components of G that are not connected to the rest."""
    import networkx
    sg = networkx.connected_component_subgraphs(G)
    if threshold is None:
        threshold = len(sg[0])/5
    return networkx.union_all([g for g in sg if len(g)>=threshold])
Exemple #15
0
    def visualize_figures(propDB_df,
                          property_name_column='property_name',
                          property_value_column='property_value',
                          figID_column='figure_ID',
                          figIDs_to_visualize=None,
                          graph_label=" "):

        if figIDs_to_visualize is not None:
            propDB_df = propDB_df[propDB_df[figID_column].isin(
                figIDs_to_visualize)]

        propDB_grouped_by_figID = propDB_df.groupby(figID_column)
        figureIDs = list(propDB_grouped_by_figID.groups.keys())

        gs = {}
        for figID in figureIDs:
            g = WODBVizLib.create_figure(
                propDB_grouped_by_figID.get_group(figID),
                figID=figID,
                property_name_column=property_name_column,
                property_value_column=property_value_column,
                figID_column=figID_column)
            gs[figID] = g

        G = nx.union_all(gs.values())
        G.graph['label'] = graph_label

        return G
Exemple #16
0
    def refresh(self):
        graphs = []
        graphs.append(self.network.toGraph())
        for n in self.otherNodes:
            if hasattr(n.tensor, 'compressedSize'):
                graphs.append(n.tensor.network.toGraph())

        assert len(graphs) > 0

        u = networkx.Graph(networkx.union_all(graphs))

        for n in self.otherNodes:
            if hasattr(n.tensor, 'compressedSize'):
                for b in n.tensor.network.externalBuckets:
                    if b.linked:
                        u.add_edge(b.node,
                                   b.otherNode,
                                   weight=np.log(b.node.tensor.size *
                                                 b.otherNode.tensor.size))

        self.selfGraph = self.network.toGraph()
        self.g = u

        self.adj = networkx.adjacency_matrix(self.g, weight='weight').todense()
        self.util = util(self.adj)
Exemple #17
0
def box_mixer(N, D, b, p):
    box_list = []
    edges_list = []
    for i in range(b):
        box = dl.box_generator(N, D)[0]
        box_list.append(box)
        edges_list.append(len(box.edges()))
    mixing_edges = int(p*sum(edges_list)) # approx number of edges between the boxes, if added fraction p of the total edges in all of the boxes
    print 'Adding %d edges' %mixing_edges
    mixed_box = nx.union_all(box_list)
    
    #assigns birthdays to the mixed_box DAG
    for node in mixed_box.nodes():
        birthday = sum(node)
        mixed_box.node[node]['birthday'] = birthday 
    birthday = nx.get_node_attributes(mixed_box, 'birthday')   
        
    while mixing_edges > 0:
        box_indices = range(b)
        rand_index_1 = choice(box_indices)
        box_indices.remove(rand_index_1) #prevents extra edge being created within a single box
        rand_index_2 = choice(box_indices)
        nodes_1 = box_list[rand_index_1].nodes()
        nodes_2 = box_list[rand_index_2].nodes()
        node_1 = choice(nodes_1)
        node_2 = choice(nodes_2)
        if birthday[node_1] > birthday[node_2]:
            mixed_box.add_edge(node_1,node_2)
            mixing_edges -= 1
    return [mixed_box,box_list]
def __create_centroids(gen_fun,
                       centroids: Tuple[int, int],
                       nodes_per_centroid: Tuple[int, int],
                       centroid_connectivity: float,
                       centroid_extra: Dict[str, Any] = None,
                       n_nodes=None,
                       **kwargs):

    if centroid_extra:
        raise NotImplementedError()

    graphs = [nx.null_graph()]
    n_centroids = random.randint(*centroids)
    for _ in range(n_centroids):
        n_nodes_centroid = random.randint(*nodes_per_centroid)
        graphs.append(gen_fun(n_nodes_centroid, **kwargs))

    graph = nx.union_all(graphs,
                         rename=map(lambda i: str(i) + "-",
                                    range(len(graphs))))

    central_nodes = filter(lambda name: name.split("-")[1] == "0", graph)
    centroid_edges = itertools.combinations(central_nodes, 2)

    for node_1, node_2 in centroid_edges:
        if random.random() < centroid_connectivity:
            graph.add_edge(node_1, node_2)

    return graph
Exemple #19
0
 def __merge(self):
     for fd in self.fds:
         succs = self.successors( fd)
         if len(succs ) <= 1:
             continue
         else:
             # TODO: 制约系统速度的关键
             big_cloud = nx.union_all(succs)
             # ----
         pre_fds = set()
         succ_fds = set()
         for succ_cloud in succs:
             assert isinstance(succ_cloud, nx.DiGraph)
             pre_fds = pre_fds.union( set( self.predecessors(succ_cloud) ) )
             succ_fds = succ_fds.union( set( self.successors( succ_cloud) ) )
             self.remove_node(succ_cloud)
         self.add_node( big_cloud, label = big_cloud.name)
         self.add_edges_from( [ (pre_fd, big_cloud) for pre_fd in pre_fds] )
         self.add_edges_from( [ (big_cloud, succ_fd) for succ_fd in succ_fds] )
     for fd in self.fds:
         if self.out_degree(fd) > 1 or self.in_degree(fd) != 1:
             err = "Error: %s indegree:%d outdegree:%d" %\
                 (fd, self.in_degree(fd), self.out_degree(fd) )
             raise CrgraphError, err 
         elif self.out_degree(fd) < 1:
             self.remove_node( fd)
             print "Waring FD:%s has no succ. Removed" % fd.name 
     self.fds = [node for node in self.nodes_iter() if isfd(node )]
     self.check()
Exemple #20
0
 def __merge(self):
     for fd in self.fds:
         succs = self.successors(fd)
         if len(succs) <= 1:
             continue
         else:
             # TODO: 制约系统速度的关键
             big_cloud = nx.union_all(succs)
             # ----
         pre_fds = set()
         succ_fds = set()
         for succ_cloud in succs:
             assert isinstance(succ_cloud, nx.DiGraph)
             pre_fds = pre_fds.union(set(self.predecessors(succ_cloud)))
             succ_fds = succ_fds.union(set(self.successors(succ_cloud)))
             self.remove_node(succ_cloud)
         self.add_node(big_cloud, label=big_cloud.name)
         self.add_edges_from([(pre_fd, big_cloud) for pre_fd in pre_fds])
         self.add_edges_from([(big_cloud, succ_fd) for succ_fd in succ_fds])
     for fd in self.fds:
         if self.out_degree(fd) > 1 or self.in_degree(fd) != 1:
             err = "Error: %s indegree:%d outdegree:%d" %\
                 (fd, self.in_degree(fd), self.out_degree(fd) )
             raise CrgraphError, err
         elif self.out_degree(fd) < 1:
             self.remove_node(fd)
             print "Waring FD:%s has no succ. Removed" % fd.name
     self.fds = [node for node in self.nodes_iter() if isfd(node)]
     self.check()
Exemple #21
0
def merge(fds, cr):
    #将每一个FD的扇出合并到一个Cloud
    assert isinstance( cr, nx.DiGraph)
    for fd in fds:
        succs = cr.successors( fd)
        if len(succs ) <= 1:
            continue
        else:
            big_cloud = nx.union_all(succs)
            big_cloud.collection = [succ.name for succ in succs]
        pre_fds = set()
        succ_fds = set()
        for succ_cloud in succs:
            assert isinstance(succ_cloud, nx.DiGraph)
            pre_fds = pre_fds.union( set( cr.predecessors(succ_cloud) ) )
            succ_fds = succ_fds.union( set( cr.successors( succ_cloud) ) )
            cr.remove_node(succ_cloud)
        cr.add_node( big_cloud, label = big_cloud.name)
        cr.add_edges_from( [ (pre_fd, big_cloud) for pre_fd in pre_fds] )
        cr.add_edges_from( [ (big_cloud, succ_fd) for succ_fd in succ_fds] )
    for fd in fds:
        if cr.out_degree(fd) != 1 or cr.in_degree(fd) != 1:
            print "Error: %s indegree:%d outdegree:%d" % (fd, cr.in_degree(fd), cr.out_degree(fd) )
            raise AssertionError
    #TODO:记录下来每一个bigcloud 由哪些cloud组成,
    return cr
Exemple #22
0
def union(G, H, rename=(None, None), name=None):
    """Return the union of graphs G and H.

    Graphs G and H must be disjoint after the renaming takes place,
    otherwise an exception is raised.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph

    rename : tuple , default=(None, None)
       Node names of G and H can be changed by specifying the tuple
       rename=('G-','H-') (for example).  Node "u" in G is then renamed
       "G-u" and "v" in H is renamed "H-v".

    name : string
       Specify the name for the union graph

       .. deprecated:: 2.7
           This is deprecated and will be removed in version v3.0.

    Returns
    -------
    U : A union graph with the same type as G.

    Notes
    -----
    To force a disjoint union with node relabeling, use
    disjoint_union(G,H) or convert_node_labels_to integers().

    Graph, edge, and node attributes are propagated from G and H
    to the union graph.  If a graph attribute is present in both
    G and H the value from H is used.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
    >>> H = nx.Graph([(0, 1), (0, 3), (1, 3), (1, 2)])
    >>> U = nx.union(G, H, rename=("G", "H"))
    >>> U.nodes
    NodeView(('G0', 'G1', 'G2', 'H0', 'H1', 'H3', 'H2'))
    >>> U.edges
    EdgeView([('G0', 'G1'), ('G0', 'G2'), ('G1', 'G2'), ('H0', 'H1'), ('H0', 'H3'), ('H1', 'H3'), ('H1', 'H2')])

    See Also
    --------
    disjoint_union
    """
    if name is not None:
        import warnings

        warnings.warn(
            "name parameter is deprecated and will be removed in version 3.0",
            DeprecationWarning,
            stacklevel=2,
        )

    return nx.union_all([G, H], rename)
Exemple #23
0
def t_unionG():
    
    Gs = [ nx.generators.random_graphs.random_powerlaw_tree(10) for i in range(2)]
    G = nx.union_all(Gs, rename=('a', 'b'))
    print G.edge
    G.add_edge('a5', 'b4')
    nx.draw(G)
    plt.show()
    def _build_network(self, demand_nodes):
        """
        project demand nodes onto optional existing supply network and
        network generation algorithm on it

        Args:
            demand_nodes:  GeoGraph of demand nodes

        Returns:
            GeoGraph  minimum spanning forest proposed by the chosen
                network algorithm
        """

        geo_graph = subgraphs = rtree = None

        existing = None
        if 'existing_networks' in self.config:
            existing = networker_runner.load_existing_networks(
                **self.config['existing_networks'])
            # rename existing nodes so that they don't intersect with metrics
            nx.relabel_nodes(existing,
                {n: 'grid-' + str(n) for n in existing.nodes()}, copy=False)
            existing.coords = {'grid-' + str(n): c for n, c in
                existing.coords.items()}
            geo_graph, subgraphs, rtree = \
                networker_runner.merge_network_and_nodes(existing, \
                    demand_nodes)
        else:
            geo_graph = demand_nodes

        # now run the selected algorithm
        network_algo = networker_runner.NetworkerRunner.ALGOS[\
                        self.config['network_algorithm']]
        result_geo_graph = network_algo(geo_graph, subgraphs=subgraphs,\
                                        rtree=rtree)

        # now filter out subnetworks via minimum node count
        min_node_count = self.config['network_parameters']\
                                    ['minimum_node_count']
        filtered_graph = nx.union_all(filter(
            lambda sub: len(sub.node) >= min_node_count,
            nx.connected_component_subgraphs(result_geo_graph)))

        # map coords back to geograph
        # NOTE:  explicit relabel to int as somewhere in filtering above, some
        #   node ids are set to numpy types which screws up comparisons to
        #   tuples in write op
        # TODO:  Google problem and report to networkx folks if needed
        # NOTE:  relabeling nodes in-place here drops node attributes for some
        #   reason so create a copy for now
        # NOTE:  use i+1 as node id in graph because dataset_store node ids
        # start at 1 (this is the realignment noted in _get_demand_nodes)
        coords = {i+1: result_geo_graph.coords[i] for i in filtered_graph}
        relabeled = nx.relabel_nodes(filtered_graph, {i: int(i+1)
            for i in filtered_graph}, copy=True)
        msf = GeoGraph(result_geo_graph.srs, coords=coords, data=relabeled)

        return existing, msf
Exemple #25
0
def HCS(G):
    graph_list = []
    for g in nx.connected_component_subgraphs(G):
        if len(g) <= 2:
            graph_list.append(g)
        else:
            graph_list.append(HCS_(g))
    gr = nx.union_all(sorted(graph_list, key=lambda x: len(x), reverse=True))
    return gr
Exemple #26
0
def generate_experiment3(num_nodes, edges, filename=""):
    start = time.time()
    scale_free_graphs = []
    scale_free_graphs_labels = (
    )  # tuple containing the prefix name of each node of the graphs
    subnetworks = 10
    nodes_per_network = num_nodes / subnetworks
    # create several scale free networks
    for i in range(0, subnetworks - 1):
        scale_free_graphs.append(
            nx.barabasi_albert_graph(nodes_per_network, edges))
        # tempGraph = nx.scale_free_graph(nodes_per_network, alpha=0.1, beta=0.6, gamma=0.3, delta_in=0.4, delta_out=0, create_using=None, seed=None)
        # tempGraph = tempGraph.to_undirected()
        # scale_free_graphs.append(tempGraph)
        scale_free_graphs_labels += (chr(i + 65) + "-"),  # node labeling

    # crete another scalefree network which random nodes inside will connect to the other networks
    graph = nx.barabasi_albert_graph(nodes_per_network, edges)

    scale_free_graphs.append(graph)
    # add all the scale free networks to the empty graph created
    graph = nx.union_all(scale_free_graphs, rename=scale_free_graphs_labels)

    random_nodes_central_network = []
    # connect the node '1' to a node on every other scale free network
    for i in range(0, len(scale_free_graphs) - 1):
        random_node = choice(list(scale_free_graphs[i].nodes())
                             )  # choose random node from a scale free network
        random_node_central_network = choice(list(graph.nodes()))
        random_nodes_central_network.append(random_node_central_network)
        graph.add_edge(random_node_central_network,
                       (scale_free_graphs_labels[i] + str(random_node)))

    # choose a random node in the central network
    nodes = list(graph.nodes())
    # prevent the central node from having a connection directly to the others surrounded networks
    nodes_allowed = [x for x in nodes if x not in random_nodes_central_network]
    central_random_node = choice(list(nodes_allowed))

    for node in random_nodes_central_network:
        graph.add_edge(central_random_node, node)

    for node in random_nodes_central_network:
        for n in random_nodes_central_network:
            if node != n:
                graph.add_edge(node, n)

    end = time.time()
    if filename != "":
        # write to file
        write_network_to_file(graph, filename)
    # print graph information
    end_generation_info(
        graph, end - start, "scale-free",
        "Note: the node with the highest load doesn't have the "
        "highest degree.")
    return graph
Exemple #27
0
def t_unionG():

    Gs = [
        nx.generators.random_graphs.random_powerlaw_tree(10) for i in range(2)
    ]
    G = nx.union_all(Gs, rename=('a', 'b'))
    print G.edge
    G.add_edge('a5', 'b4')
    nx.draw(G)
    plt.show()
Exemple #28
0
def test_union_all_multigraph():
    G = nx.MultiGraph()
    G.add_edge(1, 2, key=0)
    G.add_edge(1, 2, key=1)
    H = nx.MultiGraph()
    H.add_edge(3, 4, key=0)
    H.add_edge(3, 4, key=1)
    GH = nx.union_all([G, H])
    assert_equal(set(GH), set(G) | set(H))
    assert_equal(set(GH.edges(keys=True)), set(G.edges(keys=True)) | set(H.edges(keys=True)))
def merge_graphs(input_filenames, output_filename):

    graphs = []
    graphs_names = []
    for filename in input_filenames:
        graphs.append(nx.read_gml(filename))
        graphs_names.append(f'{filename_to_label(filename)}-')

    graph_union = nx.union_all(graphs, rename=graphs_names)   
    nx.write_gml(graph_union, output_filename)
Exemple #30
0
def test_union_all_multigraph():
    G = nx.MultiGraph()
    G.add_edge(1, 2, key=0)
    G.add_edge(1, 2, key=1)
    H = nx.MultiGraph()
    H.add_edge(3, 4, key=0)
    H.add_edge(3, 4, key=1)
    GH = nx.union_all([G, H])
    assert set(GH) == set(G) | set(H)
    assert set(GH.edges(keys=True)) == set(G.edges(keys=True)) | set(H.edges(keys=True))
 def isBisimilar(self, Q1, Q2):
     Q = nx.union_all([Q1,Q2], rename=('G-', 'H-'))
     blocks = self.getCoarsestPartition(Q)
     # Confirms that there is at least one of each type of node in each partition.
     # If there is, we have bisimilarity. Otherwise, we don't.
     for block in blocks:
         if not any('H' in nodeName for nodeName in block):
             return False
         if not any('G' in nodeName for nodeName in block):
             return False
     return True
Exemple #32
0
def generate_random_complete_clusters_with_interconnect(num_clusters, num_nodes_cluster,
                                                        density_interconnect, centroid_range_tuple, cluster_spread):
    """
    Generates a random graph with M clusters, each of which is the complete graph of N nodes, with a fraction of nodes
    randomly interconnected between clusters.  This base graph will serve as slice 1 in a temporal network, and be
    evolved from this point. Given a tuple of integers for the range of possible centroid X and Y coordinates, the
    clusters are distributed around randomly chosen centroids, with a spread factor given.
    """
    clusters = []
    cluster_id_ranges = []
    starting_id = 0
    for i in range(0,num_clusters):
        g = nx.complete_graph(num_nodes_cluster)
        g = nx.convert_node_labels_to_integers(g, first_label=starting_id)

        assign_uniform_intracluster_weights(g, args.intracluster_edgeweight)

        clusters.append(g)
        cluster_ids = range(starting_id, starting_id + num_nodes_cluster)
        cluster_id_ranges.append(cluster_ids)
        starting_id += num_nodes_cluster
    full_g = nx.union_all(clusters)
    log.debug("range of cluster ids per cluster: %s", cluster_id_ranges)


    # now, we interconnect random nodes in the formerly independent clusters, given
    # the known range of
    num_interconnects = int(math.ceil(density_interconnect * num_nodes_cluster))
    log.debug("interconnecting %s random nodes between each cluster", num_interconnects)
    cluster_ids = range(0, num_clusters)
    paired_clusters = list(itertools.product(cluster_ids,cluster_ids))
    non_self_pairs = [tup for tup in paired_clusters if tup[0] != tup[1] ]
    unique_pairs = filter_mirror_tuples(non_self_pairs)
    log.debug("num cluster pairs without self-pairing: %s", len(unique_pairs))
    log.debug("cluster pairs: %s", unique_pairs)

    for pair in unique_pairs:
        random_interconnect_clusters(full_g,pair,cluster_id_ranges,num_interconnects)

    xcentroids = np.random.random_integers(centroid_range_tuple[0], centroid_range_tuple[1], num_clusters)
    ycentroids = np.random.random_integers(centroid_range_tuple[0], centroid_range_tuple[1], num_clusters)
    centroids = zip(xcentroids, ycentroids)

    for cluster in range(0, num_clusters):
        ids = cluster_id_ranges[cluster]
        centroid = centroids[cluster]
        log.debug("cluster %s has centroid at: %s", cluster, centroid)
        assign_spatial_locations_to_cluster(full_g, ids, centroid[0], centroid[1], cluster_spread, cluster)

    # now, given spatial coordinates, assign the distance value to each edge
    assign_node_distances(full_g)

    return full_g
Exemple #33
0
    def tensorize_graph(graph_batch,
                        vocab,
                        tree=True,
                        atom_num=1,
                        extra_len=0):
        fnode, fmess = [None], [(0, 0, 0)]
        agraph, bgraph = [[]], [[]]
        scope = []
        edge_dict = {}
        all_G = []

        for bid, G in enumerate(graph_batch):
            offset = len(fnode)
            scope.append((offset, len(G)))
            G = nx.convert_node_labels_to_integers(G, first_label=offset)
            all_G.append(G)
            fnode.extend([None for v in G.nodes])

            for v, attr in G.nodes(data='label'):
                G.nodes[v]['batch_id'] = bid
                fnode[v] = vocab[attr]
                agraph.append([])

            for u, v, attr in G.edges(data='label'):
                if tree:
                    fmess.append((u, v, 0))
                else:
                    fmess.append((u, v, attr))
                edge_dict[(u, v)] = eid = len(edge_dict) + 1
                G[u][v]['mess_idx'] = eid

                if tree:
                    anchor = G[u][v]['anchor']
                    G[u][v]['anchor'] = [a + atom_num for a in anchor]
                agraph[v].append(eid)
                bgraph.append([])

            for u, v in G.edges:
                eid = edge_dict[(u, v)]
                for w in G.predecessors(u):
                    if w == v: continue
                    bgraph[eid].append(edge_dict[(w, u)])

            if tree:
                atom_num += max([max(G.nodes[idx]['clq'])
                                 for idx in G.nodes]) + 1

        fnode[0] = fnode[1]
        fnode = torch.IntTensor(fnode)
        fmess = torch.IntTensor(fmess)
        agraph = create_pad_tensor(agraph, extra_len=extra_len)
        bgraph = create_pad_tensor(bgraph, extra_len=extra_len)
        return (fnode, fmess, agraph, bgraph, scope), nx.union_all(all_G)
def generate_network(pathwaysNum, genesNum, connNeighboors, connProbability):
    pathways = []
    for n in range(0, pathwaysNum):
        pathway = nx.connected_watts_strogatz_graph(genesNum, connNeighboors,
                                                    connProbability)
        [pathway.add_node(x) for x in range(genesNum)]
        # Pathways are initialized as independant Watts-Strogatz networks
        mapping = dict(
            zip(pathway.nodes(), [x + genesNum * n for x in pathway.nodes()]))
        pathway = nx.relabel_nodes(pathway, mapping)
        pathways.append(pathway)
    PPI = nx.union_all(pathways)
    return PPI
def generate_network(pathwaysNum, genesNum, connNeighboors, connProbability):
    pathways = []
    for n in range(0, pathwaysNum):
        pathway = nx.connected_watts_strogatz_graph(genesNum, connNeighboors,
                                                    connProbability)
        [pathway.add_node(x) for x in range(genesNum)]
        # Pathways are initialized as independant Watts-Strogatz networks
        mapping = dict(
            zip(pathway.nodes(), [x+genesNum*n for x in pathway.nodes()]))
        pathway = nx.relabel_nodes(pathway, mapping)
        pathways.append(pathway)
    PPI = nx.union_all(pathways)
    return PPI
Exemple #36
0
    def build_network(self):
        """
        project demand nodes onto optional existing supply network and
        network generation algorithm on it

        Returns:
            GeoGraph  minimum spanning forest proposed by the chosen
                network algorithm
        """
        geo_graph = subgraphs = rtree = None

        demand_nodes = load_node_metrics(**self.config['demand_nodes'])
        if 'existing_networks' in self.config:
            existing = load_existing_networks(
                **self.config['existing_networks'])
            # rename existing nodes so that they don't intersect with metrics
            nx.relabel_nodes(existing,
                {n: 'grid-' + str(n) for n in existing.nodes()}, copy=False)
            existing.coords = {'grid-' + str(n): c for n, c in
                existing.coords.items()}
            geo_graph, subgraphs, rtree = \
                merge_network_and_nodes(existing, demand_nodes)
        else:
            geo_graph = demand_nodes

        # now run the selected algorithm
        network_algo = NetworkerRunner.ALGOS[self.config['network_algorithm']]
        result_geo_graph = network_algo(geo_graph, subgraphs=subgraphs,
                                        rtree=rtree)

        # TODO: Remove unreferenced fake nodes?

        # now filter out subnetworks via minimum node count
        min_node_count = self.config['network_parameters']\
                                    ['minimum_node_count']
        # TODO:  update union_all to support GeoGraph?
        filtered_graph = nx.union_all(filter(
            lambda sub: len(sub.node) >= min_node_count,
            nx.connected_component_subgraphs(result_geo_graph)))
        # map coords back to geograph

        # NOTE:  explicit relabel to int as somewhere in filtering above, some
        # node ids are set to numpy types which screws up comparisons to tuples
        # in write op
        # TODO:  Google problem and report to networkx folks if needed
        nx.relabel_nodes(filtered_graph, {i: int(i) for i in filtered_graph},
            copy=False)
        coords = {i: result_geo_graph.coords[i] for i in filtered_graph}
        geo = GeoGraph(result_geo_graph.srs, coords=coords,\
            data=filtered_graph)
        return geo
Exemple #37
0
    def formCompleteNetwork(self, end_steps, step_abbr_dict):

        self.state_name_dict = {
            state: (''.join(step_abbr_dict[step]
                            for step in state if step != '') + ' ')
            for state in self.state_network_dict.keys() if state != 'END'
        }
        self.state_name_dict[()] = 's '
        self.state_name_dict['END'] = 't '

        self.G = nx.union_all(
            [self.state_network_dict[state].G for state in self.states],
            rename=[self.state_name_dict[state] for state in self.states])

        for item in self.state_steps_dict.items():
            state = item[0]
            state_name = self.state_name_dict[state]
            for step in item[1][1]:
                node_1 = state_name + str(
                    self.state_network_dict[state].step_index[step][-1])
                if step in end_steps:
                    node_2 = 't 0'
                    self.G.add_edge(node_1, node_2)
                else:
                    new_state = tuple(sorted(set(state).union(set([step]))))
                    new_state_name = self.state_name_dict[new_state]
                    new_step = step + ' START'
                    node_2 = new_state_name + str(
                        self.state_network_dict[new_state].step_index[new_step]
                        [0])
                    self.G.add_edge(node_1, node_2)

        name_state_dict = {
            name[:-1]: state
            for state, name in self.state_name_dict.items()
        }
        self.node_state_dict = {
            node: (name_state_dict[name], int(node_index))
            for (node, (name, node_index)) in [(node, str.split(node))
                                               for node in self.G.nodes]
        }
        self.edges = list(self.G.edges)
        self.nodes = list(self.G.nodes)
        self._A = nx.incidence_matrix(self.G, oriented=True).toarray()
        self._b = np.zeros(self.G.number_of_nodes())
        self._b[list(self.G.nodes).index('s 0')] = -1
        self._b[list(self.G.nodes).index('t 0')] = 1
        self._networkFlag = True
        self.startDummy = True
        self.endDummy = True
Exemple #38
0
  def __init__(self, dataset_name, dataset_path):
    super(GraphSAINTDisjointDataset, self).__init__(dataset_name, dataset_path)

    self.name = dataset_name

    train_split = set(self.train_nodes)
    validation_split = set(self.validation_nodes)
    test_split = set(self.test_nodes)

    graph_train = _get_graph_for_split(self.adj_full, train_split)
    graph_validation = _get_graph_for_split(self.adj_full, validation_split)
    graph_test = _get_graph_for_split(self.adj_full, test_split)
    graph = nx.union_all((graph_train, graph_validation, graph_test))

    self.senders = [e[0] for e in graph.edges]
    self.receivers = [e[1] for e in graph.edges]
def generate_forest_balanced_trees(r, h, n):
    graphs = []
    roots = []
    num_nodes = stats.num_nodes_balanced_tree(r, h)
    starting_num = 0
    for i in range(0, n):
        #log.debug("building tree with starting root: %s", starting_num)
        g = nx.balanced_tree(r, h)
        g = nx.convert_node_labels_to_integers(g, first_label=starting_num)

        #log.debug("nodes: %s", pp.pformat(g.nodes()))

        graphs.append(g)
        roots.append(starting_num)
        starting_num += num_nodes
    trees = nx.union_all(graphs)
    return (trees, roots)
Exemple #40
0
 def __merge_cloud(self):
     '合并多个cloud'
     # ------------------------------------------------------------------
     # step 1 对每一个多扇出的FD,将FD的多个扇出succ cloud合并成一个大的cloud
     print "Processing: merging cloud into big cloud "
     for eachFD in self.regs:
         if self.debug:
             print "    Processing %s ..." % eachFD.name
         # 不论有没有扇入只有0-1个扇出的时候,查找下一个FD
         # 有多于1个的扇出的时候,将它的后继节点的所有cloud合并为一个cloud
         # 合并的大cloud的前驱结点,也就是与同级FD相邻的FD与大cloud相连接
         # 合并的大cloud的所有后继结点,连接到大的cloud上面,进而完成的任务是
         # 每一个D只有一个扇出
         succs = self.successors(eachFD)  #其中的每一个succ都是nx.DiGraph()
         if len(succs) <= 1:
             continue
         else:
             big_cloud = nx.union_all(succs)
         pre_fds = set()
         succ_fds = set()
         for succ_cloud in self.successors(eachFD):
             pre_fds = pre_fds.union(set(self.predecessors(succ_cloud)))
             succ_fds = succ_fds.union(set(self.successors(succ_cloud)))
             self.remove_node(succ_cloud)
         self.add_node(big_cloud)
         if self.debug:
             print "        %d nodes in cuurent graph" % self.number_of_nodes(
             )
             print "        %d egdes before adding edges-to bigclouds" % self.number_of_edges(
             )
         for pre_fd in pre_fds:
             self.add_edge(pre_fd, big_cloud)
         for succ_fd in succ_fds:
             #self.add_edge(big_cloud, pre_fd) ######F**K THIS BUGGGG
             self.add_edge(big_cloud, succ_fd)
         if self.debug:
             print "        %d edges in current graph" % self.number_of_edges(
             )
     # ------------------------------------------------------------------
     # step2 合并每一个FD的所有后继Cloud节点,把他们组成大的Big_Cloud
     self.big_clouds = []
     for node in self.nodes_iter():  #总终图上的nx.DiGraph类型只能是cloud
         if isinstance(node, nx.DiGraph):
             cloud = node
             self.big_clouds.append(cloud)
     return None
Exemple #41
0
 def calc_network_id(self, list_SG):
     """
     Assigns a unique identifier to the edges within each subgraph
     :param list_SG: list of subgraphs
     :return: new graph with network IDs added as attribute
     """
     attrb_field = netid
     try:
         subgraph_count = 1
         for SG in list_SG:
             network_id = "{0}{1:0>3}".format("net", subgraph_count)
             self.add_attribute(SG, attrb_field, network_id)
             subgraph_count += 1
         union_SG = nx.union_all(list_SG)
         return union_SG
     except:
         raise IndexError  # not sure about this... will probably change later
Exemple #42
0
def estimate_skeleton_naive_step(indep_test_func, data_matrix, alpha, level, g, **kwargs):
    def method_stable(kwargs):
        return ('method' in kwargs) and kwargs['method'] == "stable"

    stable = method_stable(kwargs)

    if nx.number_of_edges(g) == 0:
        print("level", level, ":", 1, "subgraphs")
        return g, None

    cont = True
    sep_sets = []
    while cont:
        edges_permutations = list(g.edges()) + [x[::-1] for x in list(g.edges())]
        task = Task(level, indep_test_func, data_matrix, kwargs,
                    stable, alpha, g)
        with Pool(10) as p:
            results = p.map(task.run, edges_permutations)
        conts, next_sep_sets, removable_edges = zip(*results)
        sep_sets.extend(next_sep_sets)
        remove_edges = filter(None, removable_edges)
        cont = any(conts)
        level += 1
        if stable:
            g.remove_edges_from(chain(*remove_edges))

            # additional subgraph handling
            subgraphs = list(nx.connected_component_subgraphs(g))
            
            print("level", level-1, ":", len(subgraphs), "subgraphs")
            
            if len(subgraphs) > 1:
                graphs = []
                for x in subgraphs:
                    cur_g, cur_sep_set = estimate_skeleton_naive_step(indep_test_func, data_matrix, alpha, level, x,
                                                                      **kwargs)

                    print("Results:", datetime.datetime.now(), len(cur_g))
                    graphs.append(cur_g)
                    if cur_sep_set is not None:
                        sep_sets.extend(cur_sep_set)
                return nx.union_all(graphs), sep_sets

        if ('max_reach' in kwargs) and (level > kwargs['max_reach']):
            break
    return g, sep_sets
def main():
    path_edge = os.getcwd() + "/Processed_Data/"
    path_partition = os.getcwd() + "/music_partition/"
    edges = data_import(path_edge+"edge_list2_t_12.txt")
    groups = group_import(path_partition+"graph_partition.txt")
    target = target_open("target_50_play.txt")

    G =nx.Graph()
    G.add_weighted_edges_from(edges)

    components = []
    for g in nx.connected_component_subgraphs(G):
        if len(g.nodes()) < 3:
            continue
        components.append(g)

    #graph_draw(nx.union_all(components),groups)
    targeted_draw(nx.union_all(components),target,groups[1])
Exemple #44
0
 def __merge_cloud(self):
     '合并多个cloud'
     # ------------------------------------------------------------------
     # step 1 对每一个多扇出的FD,将FD的多个扇出succ cloud合并成一个大的cloud
     print "Processing: merging cloud into big cloud "
     for eachFD in self.regs:
         if self.debug:
             print "    Processing %s ..." % eachFD.name
         # 不论有没有扇入只有0-1个扇出的时候,查找下一个FD
         # 有多于1个的扇出的时候,将它的后继节点的所有cloud合并为一个cloud
         # 合并的大cloud的前驱结点,也就是与同级FD相邻的FD与大cloud相连接
         # 合并的大cloud的所有后继结点,连接到大的cloud上面,进而完成的任务是
         # 每一个D只有一个扇出
         succs = self.successors(eachFD) #其中的每一个succ都是nx.DiGraph()
         if len(succs) <= 1:
             continue
         else: 
             big_cloud = nx.union_all( succs )
         pre_fds = set()
         succ_fds = set()
         for succ_cloud in self.successors(eachFD):
             pre_fds = pre_fds.union( set(self.predecessors(succ_cloud) ))
             succ_fds = succ_fds.union( set(self.successors(succ_cloud)) )
             self.remove_node(succ_cloud)
         self.add_node(big_cloud)
         if self.debug:
             print "        %d nodes in cuurent graph" % self.number_of_nodes()
             print "        %d egdes before adding edges-to bigclouds" % self.number_of_edges()
         for pre_fd in pre_fds:
             self.add_edge(pre_fd, big_cloud)
         for succ_fd in succ_fds:
             #self.add_edge(big_cloud, pre_fd) ######F**K THIS BUGGGG
             self.add_edge(big_cloud, succ_fd)
         if self.debug:
             print "        %d edges in current graph" % self.number_of_edges()
     # ------------------------------------------------------------------
     # step2 合并每一个FD的所有后继Cloud节点,把他们组成大的Big_Cloud
     self.big_clouds = []
     for node in self.nodes_iter(): #总终图上的nx.DiGraph类型只能是cloud
         if isinstance(node , nx.DiGraph):
             cloud = node   
             self.big_clouds.append( cloud )
     return None
def test_min_node_filter():
    """
    Test whether min node filter is applied properly
    """

    grid = grid_and_non_grid()
    min_node_count = 3

    grid_connected = filter(lambda sub: networker_runner.has_grid_conn(sub),
                            nx.connected_component_subgraphs(grid))

    non_grid_connected = filter(lambda sub:
                                not networker_runner.has_grid_conn(sub),
                                nx.connected_component_subgraphs(grid))

    filtered = networker_runner.filter_min_node_subnetworks(grid, min_node_count)

    grid_filtered = filter(lambda sub: networker_runner.has_grid_conn(sub),
                         nx.connected_component_subgraphs(filtered))

    non_grid_filtered = filter(lambda sub:
                               not networker_runner.has_grid_conn(sub),
                               nx.connected_component_subgraphs(filtered))

    assert len(grid_connected) == len(grid_filtered),\
           "number grid connected subnets should not change"

    assert min([len(g) for g in non_grid_filtered]) >= min_node_count,\
           "non-grid networks should have >= {} nodes".format(min_node_count)

    # make sure it works without existing grid
    filtered_non_grid = networker_runner.filter_min_node_subnetworks(
        nx.union_all(non_grid_connected), min_node_count)

    subnet_lens = [len(g) for g in
                   nx.connected_component_subgraphs(filtered_non_grid)]
    assert min(subnet_lens) >= min_node_count,\
           "non-grid networks should have >= {} nodes".format(min_node_count)
Exemple #46
0
def test_empty_union():
    nx.union_all([])
Exemple #47
0
    for node in list(graph.nodes()):
        if graph.degree(node) == 0:
            graph.remove_node(node)
            nodes_removed += 1
    print("Deleted {0} nodes".format(nodes_removed))

    print("Remove disconnected components")
    components = list()
    nodes_discarded = 0
    threshold = graph.size() / connectivity_threshold
    for component in list(connected_component_subgraphs(graph)):
        if component.size() >= threshold:
            components.append(component)
        else:
            nodes_discarded += component.size()
    graph = union_all(components)
    print("Deleted {0} nodes".format(nodes_discarded))
    print("{0} connected components remaining".format(len(components)))

    def heuristic(a, b):
        lat1 = math.radians(graph.node[a]["lat"])
        lon1 = math.radians(graph.node[a]["lon"])
        lat2 = math.radians(graph.node[b]["lat"])
        lon2 = math.radians(graph.node[b]["lon"])
        return gis.distance(lat1, lon1, lat2, lon2)

    print("Remove short edges")
    items, edges_removed, violates_triangle_inequality = 0, 0, 0
    for node in graph.nodes_iter():
        if graph.degree(node) == 2:
            weight = 0
Exemple #48
0
def test_mixed_type_union():
    G = nx.Graph()
    H = nx.MultiGraph()
    I = nx.Graph()
    U = nx.union_all([G,H,I])
Exemple #49
0
def union_all(union, name):
    res = CommunicatingProcess(name)
    a_set = [a.automaton for a in union]
    res.automaton = networkx.union_all(a_set)
    res.automaton.graph['name'] = name
    return res
def main(argv):
    try:
        opts, args = getopt.getopt(argv, "hb:f:ds", ["help", "shapefile=","inputfile="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    filename = None
    inputFile = None
    verbose = False
    shapefileFlag = None
    for opt, arg in opts:
        if opt == "-v":
            verbose = True
        elif opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-f", "--inputfile"):
            inputFile = arg
            shapefilename = inputFile[0:-4]
        elif opt in ("-s", "--shapefile"):
            shapefileFlag = arg
        else:
            assert False, "unhandled option"

    #Set up blank lists for data
    x,y,id_no,date,target = [], [], [], [], []
    nodeX = {}
    nodeY = {}
    nodeEasting = {}
    nodeNorthing = {}
    nodes = []
    graphs = []
    nodeSize = {}

    #read data from csv file and store in lists
    block = ""
    count = 0
    old_network = 0
    G = None
    graphCount = -1
    edgeCount = 0
    graphHash = {}
    row = ()
    size=[]
    pairwise={}
    pairwiseError={}


    ## Read in all of the data from the .vna file Reconstruct the graphs.
    try:
        file = open(inputFile)
    except IOError:
        print "can't open ", inputFile, ". I will now exit."
        sys.exit()
    except TypeError:
        print "No file specified (use --file=<filename>). I will now exit."
        sys.exit()

    outputFile = inputFile[0:-4]+"-bootstrap.vna"
    f = open(outputFile, 'w')
    f.write("*node data\n")
    f.write("ID Size X Y Easting Northing\n")

    megaGraph = nx.Graph()
    while 1:
        line=file.readline()
        #print line
        if not line:
            break
        if "*Node data" in line:
            block = "nodes"
            count = 0
        elif "*Node properties" in line:
            block = "properties"
            count= 0
        elif "*Tie data" in line:
            block = "ties"
            count = 0
        if line in ['\n', '\r\n']:
            break
        row =line.split()
        if row is None:
            break
        if count > 1 and block == "nodes":
            nodename = row[0]
            nodes.append(row[0])
            nodeX[nodename] = float(row[2])
            nodeY[nodename] = float(row[3])
            nodeEasting[nodename] = float(row[4])
            nodeNorthing[nodename] = float(row[5])
            nodeSize[nodename] = float(row[1])
            output = nodename+" " + str(nodeSize[nodename]) + str(nodeX[nodename])+ " " + str(nodeY[nodename])+ " " + str(nodeEasting[nodename]) + " " + str(nodeNorthing[nodename]) + "\n"
            f.write(output)

        if count > 1 and block == "ties":
            node1 = row[0]
            node2 = row[1]
            node1x,node1y = nodeEasting[node1], nodeNorthing[node1]
            node2x,node2y = nodeEasting[node2], nodeNorthing[node2]
            node1Size=nodeSize[node1]
            node2Size=nodeSize[node2]
            weight = float(row[5])
            weightError = float(row[6])
            pair = node1 + "#" + node2
            pair2 = node2 + "#" + node1
            pairwise[ pair ]=weight
            pairwise[ pair2 ]=weight
            #print weight
            network = int(row[4])
            print "now on network: ", network, " edge: ", edgeCount, " oldnetwork: ", old_network
            pvalue = float(row[5])
            pError = float(row[6])
            pairwiseError[ pair ]=pError
            pairwiseError[ pair2 ] = pError
            meanDistance = float(row[7])
            if network > old_network:
                #print "adding network..."
                old_network = network
                graphs.append(nx.Graph(ID=graphCount))
                graphCount += 1
                edgeCount = 0
            node1name = node1+"_"+str(network)
            node2name = node2+"_"+str(network)
            graphs[graphCount].add_node(node1name, label= node1, x = node1x, y = node1y,  size=node1Size )
            graphs[graphCount].add_node(node2name, label= node2, x = node2x, y = node2y,  size=node2Size )
            graphs[graphCount].add_edge(node1name, node2name, xy1=(node1x, node1y), xy2=(node2x, node2y),
                                        weight=weight,
                                        meanDistance=meanDistance,
                                        pvalue=weight,pError=pError,
                                        size=(nodeSize[node1],nodeSize[node2]))
            megaGraph.add_node(node1, x = node1x, y = node1y,  size=node1Size )
            megaGraph.add_node(node2, x = node2x, y = node2y, size=node2Size )
            megaGraph.add_edge(node1,node2, xy1=(node1x,node1y), xy2=(node2x,node2y),
                                   weight = weight,
                                   pvalueError = pError,
                                   meanDistance = meanDistance,
                                   pvalue = pvalue,
                                   pError = pError,
                                   color =network,
                                   size=(node1Size,node2Size),
                                    )
            edgeCount += 1
        count += 1

    if shapefileFlag is not None:
        w = shapefile.Writer(shapefile.POLYLINE)  # 3= polylines
        #print count, " graphs "
        c=0
        #pp.pprint(graphs)
        for g in graphs:
            edges = g.edges()
            for e in edges:
                node1 = e[0]
                node2 = e[1]
                print g[node1][node2]
                x1 = g[node1][node2]['xy1'][0]
                y1 = g[node1][node2]['xy1'][1]
                x2 = g[node2][node1]['xy2'][0]
                y2 = g[node2][node1]['xy2'][1]
                #print x1, "-", y1
                #print x2, "-", y2
                w.poly(parts=[[[x1,y1],[x2,y2]]])
            c += 1
        w.save(shapefilename)

    try:
        from networkx import graphviz_layout
    except ImportError:
        raise ImportError("This example needs Graphviz and either PyGraphviz or Pydot")

    plt.rcParams['text.usetex'] = False
    plt.figure(0,figsize=(8,8))
    mst=nx.minimum_spanning_tree(megaGraph,weight='weight')
    pos=nx.graphviz_layout(mst,prog="neato")
    #pos=nx.spring_layout(mst,iterations=500)

    # edge width is proportional number of games played
    edgewidth=[]
    weights = nx.get_edge_attributes(mst, 'weight')
    for w in weights:
        edgewidth.append(weights[w]*10)

    maxValue = np.max(edgewidth)
    widths=[]
    for w in edgewidth:
        widths.append(((maxValue-w)+1)*5)

    color = nx.get_edge_attributes(mst, 'color')
    colorList = []
    for c in color:
        colorList.append(color[c])
    colors=[]
    colorMax = max(colorList)
    for c in colorList:
        colors.append(c/colorMax)
    assemblageSizes=[]
    sizes = nx.get_node_attributes(mst, 'size')
    #print sizes
    for s in sizes:
        #print sizes[s]
        assemblageSizes.append(sizes[s])
    nx.draw_networkx_edges(mst,pos,alpha=0.3,width=widths, edge_color=colorList)
    sizes = nx.get_node_attributes(mst,'size')
    nx.draw_networkx_nodes(mst,pos,node_size=assemblageSizes,node_color='w',alpha=0.4)
    nx.draw_networkx_edges(mst,pos,alpha=0.4,node_size=0,width=1,edge_color='k')
    nx.draw_networkx_labels(mst,pos,fontsize=10)
    font = {'fontname'   : 'Helvetica',
        'color'      : 'k',
        'fontweight' : 'bold',
        'fontsize'   : 14}
    edgelist = list(mst) # make a list of the edges
    #print edgelist
    #nx.draw(mst)
    #plt.savefig("path.png")
    plt.axis('off')
    pngfile=shapefilename+"-mst.png"
    plt.savefig(pngfile,dpi=75)
    print(pngfile)


    f.write("*tie data\n*from to weight error distance\n")
    for (u,v,d) in mst.edges_iter(data=True):
        output = u +" "+ v + " "+str(d['weight'])+" "+str(d['pError'])+" "+str(d['meanDistance'])+"\n"
        #print output
        f.write(output)

    plt.figure(1,figsize=(30,20))
    # layout graphs with positions using graphviz neato

    UU=nx.Graph()
    # do quick isomorphic-like check, not a true isomorphism checker
    nlist=[] # list of nonisomorphic graphs
    for G in graphs:
        # check against all nonisomorphic graphs so far
        if not iso(G, nlist):
            nlist.append(G)

    UU=nx.union_all(graphs) # union the nonisomorphic graphs
    #UU=nx.disjoint_union_all(nlist) # union the nonisomorphic graphs
    #pos=nx.spring_layout(UU,iterations=50)

    ##pos=nx.graphviz_layout(UU,prog="neato")
    pos=nx.graphviz_layout(UU,prog="twopi",root=0)
    ##labels=nx.draw_networkx_labels(UU,pos)
    # color nodes the same in each connected subgraph
    C=nx.connected_component_subgraphs(UU)
    for g in C:
        c = [random.random()] * nx.number_of_nodes(g) # random color...
        nx.draw(g,
            pos,
            node_size=40,
            node_color=c,
            vmin=0.0,
            vmax=1.0,
            alpha=.2,
            font_size=7,
        )
    plt.savefig("atlas.png",dpi=250)

    plt.show() # display
Exemple #51
0
    def pre_vectorizer_graph(self, nested=True):
        '''
        generate the graph that will be used for evaluation ( it will be vectorized by eden and then used
        in a machine learning scheme).

        Parameters
        ----------
        nested: bool
            the graph returned here is the union of graph minor and the base graph.
            nested decides wether there edges between nodes in the base graph and their
            representative in the graph minor. these edges have the attribute 'nested'.

        Returns
        -------
            nx.graph


        if nested:
            # before we make the union we need to save the ids of all nodes in the base graph

            for n, d in self._base_graph.nodes(data=True):
                d["ID"] = n
            for n, d in self.abstract_graph().nodes(data=True):
                d.pop("ID",None)
        '''

        # transfer layer information to the nodes (otherwise it will be lost)
        graph = self._unaltered_graph
        while 'original' in graph.graph:
            def f(n, d): d['layer'] = graph.graph.get('layer', 0)

            utils.node_operation(graph, f)
            graph = graph.graph['original']

        # make union of everything
        graph = self._unaltered_graph
        graphs = [graph]
        while 'original' in graph.graph:
            graphs.append(graph.graph['original'])
            graph = graph.graph['original']

        # draw.graphlearn(graphs, vertex_label='id')
        try:
            g = nx.union_all(graphs)
        except:
            print 'decompose prevec graph union failed. '
            print ascii.nx_to_ascii(graph,xmax=30,ymax=15)
            #draw.graphlearn(graphs, vertex_label='id')
            # nobody cares... i just need to fix the overlap in ids
            #import graphlearn.minor.old.rnasampler as egraph
            #graphs = map (egraph._revert_edge_to_vertex_transform, graphs)
            #draw.graphlearn(graphs, vertex_label='id', font_size=7)

        if nested:
            # edge_nodes -> edges
            # then look at the contracted nodes to add dark edges.
            # g  = edengraphtools._revert_edge_to_vertex_transform(g)
            try:
                # updating the contracted sets
                # reconstrdict={  d["ID"]:n  for n,d in g.nodes(data=True) if "ID" in d  }
                # for n, d in g.nodes(data=True):
                #    if 'contracted' in d:
                #        d['contracted']=set( [reconstrdict[e] for e in d['contracted']] )


                for node_union_graph, d in g.nodes(data=True):
                    if 'contracted' in d:
                        for e in d['contracted']:
                            if e in g.nodes() and "edge" not in graph.node.get(e,{}): # graph is the original graph.
                                g.add_edge(node_union_graph, e, nesting=True, label='')
                g=eden.graph._revert_edge_to_vertex_transform(g)
            except:
                print 'can not build nested graph... input looks like this:'
                #draw.graphlearn(self._unaltered_graph.graph['original'], vertex_label='id', size=15)
                #draw.graphlearn(self._unaltered_graph, vertex_label='contracted', size=15)

        # add labels to all edges ( this is needed for eden. .. bu
        # g = fix_graph(g)

        return g
Exemple #52
0
def graphlearn_layered3(graphs, **args): # THIS IS THE NORMAL ONE
    '''
    HERE I TRY TO GET TEHE LAYOUT FROM RDKIT

    this is to draw a graph that has its layers as graph.graph['origial']

    Args:
        graphs:
        **args:

    Returns:

    '''
    DEBUG = False

    if args.get('n_graphs_per_line',5)!=1:
        for graph in graphs:
            graphlearn_layered3([graph],n_graphs_per_line=1)
        return


    def calc_avg_position(nodelist, posdict):
        # print 'calc avg pos'
        if len(nodelist) == 0:
            import traceback
            traceback.print_stack()
            print 'bad node list'
            return (0, 0)
        xpos = sum([posdict[i][0] for i in nodelist]) / len(nodelist)
        ypos = sum([posdict[i][1] for i in nodelist]) / len(nodelist)
        return (xpos, ypos)

    finished_graphs = []
    poslist = []
    for graph in graphs:

        # make a list of all the graphs
        layered_graphs = [graph]
        while 'original' in graph.graph:
            layered_graphs.append(graph.graph['original'])
            graph = graph.graph['original']
        maxlayers = len(layered_graphs)
        # make the layout for the biggest one :)

        from eden_chem.display.rdkitutils import nx_to_pos
        XSCALE,YSCALE=1,1

        pos = {n:(p[0]*XSCALE,p[1]*YSCALE) for n,p in nx_to_pos(graph).items()}

        aa,bb=zip(*pos.values())
        SCALE = (max(aa)-min(aa)) / (max(bb)-min(bb))


        aa,bb=zip(*pos.values())
        #SCALE = (max(bb)-min(bb)) / (max(aa)-min(aa))
        height = max(bb)-min(bb)
        #xmov=(0-min(aa))*1.1
        #pos={n:(p[0]+xmov,p[1]) for n,p in pos.items()}



        if DEBUG: print 'biggest:', pos

        # pos attribute loks like this:
        # pos = {i: (rna_object.get(i).X, rna_object.get(i).Y)
        #           for i in range(len(graph.graph['structure']))}

        for i in range(len(layered_graphs) - 2, -1, -1):
            new_positions = {}
            for node in layered_graphs[i].nodes():
                new_positions[node] = calc_avg_position(layered_graphs[i].node[node].get('contracted', set()), pos)
            if DEBUG: print 'new posis', new_positions
            # move all the nodes by such and such
            # nodes in prev layer:
            minpos = min([pos[n][0] for n in layered_graphs[i + 1].nodes()])
            moveby_x = (max([pos[n][0] for n in layered_graphs[i + 1].nodes()])  - minpos) * 1.2

            moveby_y = ((-1) ** i) * height * 0.2
            #moveby_y = 0
            for k, v in new_positions.items():
                new_positions[k] = (v[0] + moveby_x, v[1] + moveby_y)

            if DEBUG: print 'new posis updated', new_positions
            pos.update(new_positions)

        g = nx.union_all(layered_graphs)
        for n, d in g.nodes(data=True):
            for n2 in d.get('contracted', []):
                g.add_edge(n, n2, nesting=True, label='')
        finished_graphs.append(g)
        poslist.append(pos)


        aa,bb=zip(*pos.values())
        pad=.5
        xlim= ( min(aa)-pad,max(aa)+pad)
        ylim= (min(bb)-pad,max(bb)+pad)

    # draw
    args['xlim']= xlim
    args['ylim']= ylim
    args['size_x_to_y_ratio'] = maxlayers * SCALE
    args['n_graphs_per_line'] = 1
    args['size'] = 4
    args['pos'] = poslist
    args['dark_edge_color'] = 'dark_edge_color'
    graphlearn(finished_graphs, **args)
Exemple #53
0
    def createMST(self):
        #Set up blank lists for data
        x,y,id_no,date,target = [], [], [], [], []
        nodeX = {}
        nodeY = {}
        nodeEasting = {}
        nodeNorthing = {}
        nodes = []
        graphs = []
        nodeSize = {}

        #read data from csv file and store in lists
        block = ""
        count = 0
        old_network = 0
        G = None
        graphCount = -1
        edgeCount = 0
        graphHash = {}
        row = ()
        size=[]
        pairwise={}
        pairwiseError={}

        file,f=self.openFileHandles()

        f.write("*node data\n")
        f.write("ID Size X Y Easting Northing\n")

        megaGraph = nx.Graph()
        while 1:
            line=file.readline()
            #print line
            if not line:
                break
            if "*Node data" in line:
                block = "nodes"
                count = 0
            elif "*Node properties" in line:
                block = "properties"
                count= 0
            elif "*Tie data" in line:
                block = "ties"
                count = 0
            if line in ['\n', '\r\n']:
                break
            row =line.split()
            if row is None:
                break
            if count > 1 and block == "nodes":
                nodename = row[0]
                nodes.append(row[0])
                nodeX[nodename] = float(row[2])
                nodeY[nodename] = float(row[3])
                nodeEasting[nodename] = float(row[4])
                nodeNorthing[nodename] = float(row[5])
                nodeSize[nodename] = float(row[1])
                output = nodename+" " + str(nodeSize[nodename]) + str(nodeX[nodename])+ " " + str(nodeY[nodename])+ " " + str(nodeEasting[nodename]) + " " + str(nodeNorthing[nodename]) + "\n"
                f.write(output)
            if count > 1 and block == "ties":
                node1 = row[0]
                node2 = row[1]
                node1x,node1y = nodeEasting[node1], nodeNorthing[node1]
                node2x,node2y = nodeEasting[node2], nodeNorthing[node2]
                node1Size=nodeSize[node1]
                node2Size=nodeSize[node2]
                weight = float(row[5])
                weightError = float(row[6])
                pair = node1 + "#" + node2
                pair2 = node2 + "#" + node1
                pairwise[ pair ]=weight
                pairwise[ pair2 ]=weight
                #print weight
                network = int(row[4])
                print "now on network: ", network, " edge: ", edgeCount, " oldnetwork: ", old_network
                pvalue = float(row[5])
                pError = float(row[6])
                pairwiseError[ pair ]=pError
                pairwiseError[ pair2 ] = pError
                meanDistance = float(row[7])
                if network > old_network:
                    #print "adding network..."
                    old_network = network
                    graphs.append(nx.Graph(ID=graphCount))
                    graphCount += 1
                    edgeCount = 0
                node1name = node1+"_"+str(network)
                node2name = node2+"_"+str(network)
                graphs[graphCount].add_node(node1name, label= node1, x = node1x, y = node1y,  size=node1Size )
                graphs[graphCount].add_node(node2name, label= node2, x = node2x, y = node2y,  size=node2Size )
                graphs[graphCount].add_edge(node1name, node2name, xy1=(node1x, node1y), xy2=(node2x, node2y),
                                            weight=weight,
                                            meanDistance=meanDistance,
                                            pvalue=weight,pError=pError,
                                            size=(nodeSize[node1],nodeSize[node2]))
                megaGraph.add_node(node1, x = node1x, y = node1y,  size=node1Size )
                megaGraph.add_node(node2, x = node2x, y = node2y, size=node2Size )
                megaGraph.add_edge(node1,node2, xy1=(node1x,node1y), xy2=(node2x,node2y),
                                       weight = weight,
                                       pvalueError = pError,
                                       meanDistance = meanDistance,
                                       pvalue = pvalue,
                                       pError = pError,
                                       color =network,
                                       size=(node1Size,node2Size),
                                        )
                edgeCount += 1
            count += 1

        if self.shapefile is not None:
            w = shapefile.Writer(shapefile.POLYLINE)  # 3= polylines
            c=0
            for g in graphs:
                edges = g.edges()
                for e in edges:
                    node1 = e[0]
                    node2 = e[1]
                    print g[node1][node2]
                    x1 = g[node1][node2]['xy1'][0]
                    y1 = g[node1][node2]['xy1'][1]
                    x2 = g[node2][node1]['xy2'][0]
                    y2 = g[node2][node1]['xy2'][1]
                    w.poly(parts=[[[x1,y1],[x2,y2]]])
                c += 1
            w.save(self.shapefilename)

            time.sleep(15)

        plt.rcParams['text.usetex'] = False
        plt.figure(0,figsize=(8,8))
        mst=nx.minimum_spanning_tree(megaGraph,weight='weight')
        pos=nx.graphviz_layout(mst,prog="neato")
        #pos=nx.spring_layout(mst,iterations=500)

        # edge width is proportional number of games played
        edgewidth=[]
        weights = nx.get_edge_attributes(mst, 'weight')
        for w in weights:
            edgewidth.append(weights[w]*10)

        maxValue = np.max(edgewidth)
        widths=[]
        for w in edgewidth:
            widths.append(((maxValue-w)+1)*5)

        color = nx.get_edge_attributes(mst, 'color')
        colorList = []
        for c in color:
            colorList.append(color[c])
        colors=[]
        colorMax = max(colorList)
        for c in colorList:
            colors.append(c/colorMax)
        assemblageSizes=[]
        sizes = nx.get_node_attributes(mst, 'size')
        #print sizes
        for s in sizes:
            #print sizes[s]
            assemblageSizes.append(sizes[s])

        nx.draw_networkx_edges(mst,pos,alpha=0.3,width=widths, edge_color=colorList)
        sizes = nx.get_node_attributes(mst,'size')
        nx.draw_networkx_nodes(mst,pos,node_size=assemblageSizes,node_color='w',alpha=0.4)
        nx.draw_networkx_edges(mst,pos,alpha=0.4,node_size=0,width=1,edge_color='k')
        nx.draw_networkx_labels(mst,pos,fontsize=10)
        font = {'fontname'   : 'Helvetica',
            'color'      : 'k',
            'fontweight' : 'bold',
            'fontsize'   : 14}
        edgelist = list(mst) # make a list of the edges
        #print edgelist
        #nx.draw(mst)
        #plt.savefig("path.png")
        plt.axis('off')
        pngfile=self.filename+"-mst.png"
        plt.savefig(pngfile,dpi=75)
        print(pngfile)


        f.write("*tie data\n*from to weight error distance\n")
        for (u,v,d) in mst.edges_iter(data=True):
            output = u +" "+ v + " "+str(d['weight'])+" "+str(d['pError'])+" "+str(d['meanDistance'])+"\n"
            #print output
            f.write(output)

        plt.figure(1,figsize=(30,20))
        # layout graphs with positions using graphviz neato

        UU=nx.Graph()
        # do quick isomorphic-like check, not a true isomorphism checker
        nlist=[] # list of nonisomorphic graphs
        for G in graphs:
            # check against all nonisomorphic graphs so far
            if not self.iso(G, nlist):
                nlist.append(G)

        UU=nx.union_all(graphs) # union the nonisomorphic graphs
        #UU=nx.disjoint_union_all(nlist) # union the nonisomorphic graphs
        #pos=nx.spring_layout(UU,iterations=50)

        ##pos=nx.graphviz_layout(UU,prog="neato")
        pos=nx.graphviz_layout(UU,prog="twopi",root=0)
        ##labels=nx.draw_networkx_labels(UU,pos)
        # color nodes the same in each connected subgraph
        C=nx.connected_component_subgraphs(UU)
        for g in C:
            c = [random.random()] * nx.number_of_nodes(g) # random color...
            nx.draw(g,
                pos,
                node_size=40,
                node_color=c,
                vmin=0.0,
                vmax=1.0,
                alpha=.2,
                font_size=7,
            )
        atlasFile=self.outputdirectory+self.filename[0:-4]+"-atlas.png"
        plt.savefig(atlasFile,dpi=250)
        plt.show() # display
        return True
Exemple #54
0
def graphlearn_layered2(graphs, **args): # THIS IS THE NORMAL ONE
    '''

    THIS IS THE DEFAULT FOR LAYERED GRAPHZ
    this is to draw a graph that has its layers as graph.graph['origial']

    Args:
        graphs:
        **args:

    Returns:

    '''
    DEBUG = False

    def calc_avg_position(nodelist, posdict):
        # print 'calc avg pos'
        if len(nodelist) == 0:
            import traceback
            traceback.print_stack()
            print 'bad node list'
            return (0, 0)
        xpos = sum([posdict[i][0] for i in nodelist]) / len(nodelist)
        ypos = sum([posdict[i][1] for i in nodelist]) / len(nodelist)
        return (xpos, ypos)

    finished_graphs = []
    poslist = []
    for graph in graphs:

        # make a list of all the graphs
        layered_graphs = [graph]
        while 'original' in graph.graph:
            layered_graphs.append(graph.graph['original'])
            graph = graph.graph['original']
        maxlayers = len(layered_graphs)
        # make the layout for the biggest one :)

        pos = nx.graphviz_layout(layered_graphs[-1], prog='neato', args="-Gmode=KK")


        if DEBUG: print 'biggest:', pos

        # pos attribute loks like this:
        # pos = {i: (rna_object.get(i).X, rna_object.get(i).Y)
        #           for i in range(len(graph.graph['structure']))}

        for i in range(len(layered_graphs) - 2, -1, -1):
            new_positions = {}
            for node in layered_graphs[i].nodes():
                new_positions[node] = calc_avg_position(layered_graphs[i].node[node].get('contracted', set()), pos)
            if DEBUG: print 'new posis', new_positions
            # move all the nodes by such and such
            # nodes in prev layer:
            minpos = min([pos[n][0] for n in layered_graphs[i + 1].nodes()])
            moveby_x = max([pos[n][0] for n in layered_graphs[i + 1].nodes()]) + 200 - minpos
            #print moveby_x
            moveby_y = ((-1) ** i) * 30
            for k, v in new_positions.items():
                new_positions[k] = (v[0] + moveby_x, v[1] + moveby_y)

            if DEBUG: print 'new posis updated', new_positions
            pos.update(new_positions)

        g = nx.union_all(layered_graphs)
        for n, d in g.nodes(data=True):
            for n2 in d.get('contracted', []):
                g.add_edge(n, n2, nesting=True, label='')
        finished_graphs.append(g)
        poslist.append(pos)

    # draw
    args['size_x_to_y_ratio'] = maxlayers
    args['pos'] = poslist
    args['dark_edge_color'] = 'dark_edge_color'
    graphlearn(finished_graphs, **args)
def build_network(demand_nodes, 
                    existing=None, 
                    min_node_count=2,
                    single_network=True,
                    network_algorithm='mod_boruvka',
                    one_based=False 
                    ):
    """
    project demand nodes onto optional existing supply network and
    return the 'optimized' network

    Args:
        demand_nodes:  GeoGraph of demand nodes
        existing:  GeoGraph of existing grid (assumes node ids
            don't conflict with demand_nodes
        min_node_count:  minimum number of nodes allowed in a subgraph
            of the result
        network_algorithm:  Algorithm from ALGOS to run
        one_based:  Whether result GeoGraph's nodes should be one_based
            (if not, they are 0 based)

    Returns:
        msf: GeoGraph of minimum spanning forest proposed by the chosen
            network algorithm
        existing: The existing grid GeoGraph (None if it doesn't exist) 
        
    """
    geo_graph = subgraphs = rtree = None

    if existing:
        log.info("merging network and nodes")
        geo_graph, subgraphs, rtree = \
            merge_network_and_nodes(existing, demand_nodes, 
                single_network=single_network)
    else:
        geo_graph = demand_nodes

    log.info("running {} on {} demand nodes and {} total nodes".format(
              network_algorithm, len(demand_nodes), len(geo_graph)))

    # now run the selected algorithm
    network_algo = NetworkerRunner.ALGOS[network_algorithm]
    result_geo_graph = network_algo(geo_graph, subgraphs=subgraphs,
                                    rtree=rtree)

    # TODO: Remove unreferenced fake nodes?

    # now filter out subnetworks via minimum node count
    # TODO:  update union_all to support GeoGraph?
    filtered_graph = nx.union_all(filter(
        lambda sub: len(sub.node) >= min_node_count,
        nx.connected_component_subgraphs(result_geo_graph)))

    # map coords back to geograph
    # NOTE:  explicit relabel to int as somewhere in filtering above, some
    # node ids are set to numpy types which screws up comparisons to tuples
    # in write op
    # NOTE:  relabeling nodes in-place here drops node attributes for some
    #   reason so create a copy for now
    def id_label(i): 
        id = int(i+1) if one_based else int(i)
        return id

    msf = None
    if filtered_graph:
        coords = {id_label(i): result_geo_graph.coords[i]
                    for i in filtered_graph}
        relabeled = nx.relabel_nodes(filtered_graph, {i: id_label(i)
            for i in filtered_graph}, copy=True)
        msf = GeoGraph(result_geo_graph.srs, coords=coords, data=relabeled)

    log.info("filtered result has {} nodes and {} edges".format(
              len(msf.nodes()), len(msf.edges())))

    return msf
 def union_all_wrap(graph_list):
     """ handle empty list so that union_all returns empty graph """
     if len(graph_list) == 0:
         return nx.Graph()
     else:
         return nx.union_all(graph_list)