Exemplo n.º 1
0
    def test_restricted_induced_subgraph_chains(self):
        """ Test subgraph chains that both restrict and show nodes/edges.

        A restricted_view subgraph should allow induced subgraphs using
        G.subgraph that automagically without a chain (meaning the result
        is a subgraph view of the original graph not a subgraph-of-subgraph.
        """
        hide_nodes = [3, 4, 5]
        hide_edges = [(6, 7)]
        RG = nx.restricted_view(self.G, hide_nodes, hide_edges)
        nodes = [4, 5, 6, 7, 8]
        SG = nx.induced_subgraph(RG, nodes)
        SSG = RG.subgraph(nodes)
        assert_is(SSG.root_graph, SSG._graph)
        assert_is_not(SG.root_graph, SG._graph)
        assert_edges_equal(SG.edges, SSG.edges)
        # should be same as morphing the graph
        CG = self.G.copy()
        CG.remove_nodes_from(hide_nodes)
        CG.remove_edges_from(hide_edges)
        assert_edges_equal(CG.edges(nodes), SSG.edges)
        CG.remove_nodes_from([0, 1, 2, 3])
        assert_edges_equal(CG.edges, SSG.edges)
        # switch order: subgraph first, then restricted view
        SSSG = self.G.subgraph(nodes)
        RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges)
        assert_is_not(RSG.root_graph, RSG._graph)
        assert_edges_equal(RSG.edges, CG.edges)
Exemplo n.º 2
0
    def test_restricted_induced_subgraph_chains(self):
        """ Test subgraph chains that both restrict and show nodes/edges.

        A restricted_view subgraph should allow induced subgraphs using
        G.subgraph that automagically without a chain (meaning the result
        is a subgraph view of the original graph not a subgraph-of-subgraph.
        """
        hide_nodes = [3, 4, 5]
        hide_edges = [(6, 7)]
        RG = nx.restricted_view(self.G, hide_nodes, hide_edges)
        nodes = [4, 5, 6, 7, 8]
        SG = nx.induced_subgraph(RG, nodes)
        SSG = RG.subgraph(nodes)
        assert RG._graph is self.G
        assert SSG._graph is self.G
        assert SG._graph is RG
        assert_edges_equal(SG.edges, SSG.edges)
        # should be same as morphing the graph
        CG = self.G.copy()
        CG.remove_nodes_from(hide_nodes)
        CG.remove_edges_from(hide_edges)
        assert_edges_equal(CG.edges(nodes), SSG.edges)
        CG.remove_nodes_from([0, 1, 2, 3])
        assert_edges_equal(CG.edges, SSG.edges)
        # switch order: subgraph first, then restricted view
        SSSG = self.G.subgraph(nodes)
        RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges)
        assert RSG._graph is not self.G
        assert_edges_equal(RSG.edges, CG.edges)
Exemplo n.º 3
0
def draw_cooccurrence_network(net_type=None,
                              db_path=None,
                              output_path=None,
                              top_n=30):
    assert net_type is not None and output_path is not None and db_path is not None

    engine = get_engine(db_path)
    session = get_session(engine)

    print('正在处理共现数据')
    graph_data = []
    data = []
    title = None
    if net_type == 'keyword':
        title = 'Author Keyword Co-occurrence Network'
        data = session.query(WosDocument.unique_id,
                             func.group_concat(WosKeyword.keyword, ';'))\
                                .join(WosKeyword).group_by(WosDocument.unique_id)
        filter_data = session.query(WosKeyword.keyword, func.count('*').label('num')) \
            .group_by(WosKeyword.keyword).order_by(desc('num'))
    elif net_type == 'keyword_plus':
        title = 'WoS Keyword Co-occurrence Network'
        data = session.query(WosDocument.unique_id,
                             func.group_concat(WosKeywordPlus.keyword_plus, ';'))\
                                .join(WosKeywordPlus).group_by(WosDocument.unique_id)
        filter_data = session.query(WosKeywordPlus.keyword_plus, func.count('*').label('num')) \
            .group_by(WosKeywordPlus.keyword_plus).order_by(desc('num'))
    elif net_type == 'author':
        title = 'Author Co-authorship Network'
        data = session.query(WosDocument.unique_id,
                             func.group_concat(WosAuthor.last_name +','+ WosAuthor.first_name, ';'))\
                                .join(WosAuthor).group_by(WosDocument.unique_id)
        filter_data = session.query(WosAuthor.last_name + ',' + WosAuthor.first_name, func.count('*').label('num')) \
            .group_by(WosAuthor.last_name + ',' + WosAuthor.first_name).order_by(desc('num'))

    else:
        print('未考虑到的作图情况:', net_type)
        exit(-1)

    for row in data:
        row_split = row[1].split(';')
        if len(row_split) > 1:
            graph_data += list(combinations(row_split, 2))

    # network是包含了全部关键词的共现网络
    print('正在生成共现网络')
    network = get_network(graph_data, directed=False)

    session.close()

    nx.write_graphml(network, 'test.gml')

    filter_nodes = [i[0] for i in filter_data[top_n:]]
    sub = nx.restricted_view(network, filter_nodes, [])

    # 最大联通子图
    # sub = sorted(nx.connected_component_subgraphs(sub), key = len, reverse=True)[0]

    # print('正在绘图')
    draw_net(sub, title=title, output_path=os.path.join(output_path, net_type))
Exemplo n.º 4
0
def PP(gr, s):
    i = 0
    global filenames
    piles = [s]
    noeuds_decouverts = []
    nodePos = {}
    while len(piles) != 0:
        #   while len(noeuds_decouverts)<20:
        noeud_courant = piles[-1]
        piles = piles[:-1]
        for voisin in gr[str(noeud_courant)].values():
            nx.add_path(G, [noeud_courant, voisin])
            if (voisin not in noeuds_decouverts) and (voisin not in piles):
                piles.append(voisin)
        noeuds_decouverts.append(noeud_courant)

    pos = nx.spring_layout(G)
    for i in range(len(noeuds_decouverts)):
        out = noeuds_decouverts[i:]
        H = nx.restricted_view(G, out, [])
        nx.draw_networkx(H, pos=pos)
        filename = f'{i}.png'
        i += 1
        filenames.append(filename)
        plt.savefig(filename)
        plt.close()
    nx.draw_networkx(G, pos=pos)
    filename = f'{i}.png'
    filenames.append(filename)
    plt.savefig(filename)
    plt.savefig('profondeur.png')
    plt.close()
    return (noeuds_decouverts)
Exemplo n.º 5
0
def main():
    """ Reads in a value of k and a graph and prints an approximate k-centers solution """

    k = int(sys.argv[1])
    graph_file_path = sys.argv[2]

    input_graph = nx.read_weighted_edgelist(graph_file_path)

    # sort the edges of G by nondecreasing weight
    all_edges = list(input_graph.edges.items())
    all_edges.sort(key=lambda pair: pair[1]['weight'])

    # Construct the squares of the edge-induced subgraphs for each edge subset [1..i]
    power_graphs = []
    for i in range(0, len(all_edges)):
        edges_to_remove = list(map(lambda pair: pair[0], all_edges[i + 1:]))
        induced_graph = nx.restricted_view(input_graph, [], edges_to_remove)
        power_graphs.append(nx.power(induced_graph, 2))

    # Compute a maximal independent set for each power graph
    # If its size is less than k, return it as our approximate solution
    for pow_graph in power_graphs:
        indep_set = nx.maximal_independent_set(pow_graph)
        if len(indep_set) <= k:
            print("k centers are:", indep_set)
            break
Exemplo n.º 6
0
def test_restricted_view_multi(G):
    G.add_edges_from([(0, 1, 0), (0, 2, 0), (0, 3, 0), (0, 1, 1), (1, 0, 0),
                      (1, 1, 0), (1, 2, 0)])
    G.add_node(4)
    H = nx.restricted_view(G, [0, 2, 5], [(1, 2, 0), (3, 4, 0)])
    assert set(H.nodes()) == {1, 3, 4}
    assert set(H.edges()) == {(1, 1)}
Exemplo n.º 7
0
def find_chamber(graph):
    """Detect chambers (rooms with a single square entrance).

    Return (entrance, chamber), where `entrance` is the node representing the
    entrance to the chamber (None if no chamber is found), and `chamber` is the
    list of nodes within the chamber (empty list if no nodes are in the chamber).

    The entrance to a chamber is a node that when removed from the graph
    will result in the graph to be split into two disconnected graphs."""
    # minimum_node_cut returns a set of nodes of minimum cardinality that
    # disconnects the graph. This means that we have a chamber if the length
    # of this set is one, i.e. there is one node that when removed disconnects
    # the graph
    cuts = nx.minimum_node_cut(graph)
    if len(cuts) > 1:
        # no chambers, yeah!
        return None, []
    entrance = cuts.pop()
    # remove the cut, i.e. put a wall on the entrance
    lgraph = nx.restricted_view(graph, [entrance], [])
    # now get the resulting subgraphs
    subgraphs = sorted(nx.connected_components(lgraph), key=len)
    # let's get the smallest subgraph: this is going to be a chamber
    # (other subgraphs are other chambers (if any) and the 'rest' of the graph
    # return a list of nodes, instead of a set
    chamber = list(subgraphs[0])
    return entrance, chamber
Exemplo n.º 8
0
def is_triconnected(graph):
    """
    Determines if a given undirected simple graph is triconnected or not.

    Parameters
    ----------
    graph: nx.Graph

    Returns
    -------
    : bool
        Whether the given graph is triconnected or not.
    """
    if not isinstance(graph, nx.Graph):
        print("'graph' must be an undirected simple graph.")
    node_list = list(graph.nodes)
    for i in range(graph.number_of_nodes()):
        u = node_list[i]
        for j in range(i + 1, graph.number_of_nodes()):
            v = node_list[j]
            # print(u, end=", ")
            # print(v, end=": ")
            H = nx.restricted_view(graph, nodes=[u, v], edges=[])
            if not nx.algorithms.is_connected(H):
                # print("Separated !")
                return False
            else:
                # print("Still Connected !")
                pass
    return True
Exemplo n.º 9
0
def PL(gr, s):
    i = 0
    j = 0
    global filenames
    files = [s]
    noeuds_decouverts = [s]
    nodePos = {}
    while len(files) != 0:
        #    while len(noeuds_decouverts)<20:
        noeud_courant = files[0]
        files = files[1:]
        for voisin in gr[noeud_courant].values():
            nx.add_path(G, [noeud_courant, voisin])
            if voisin not in noeuds_decouverts:
                noeuds_decouverts.append(voisin)
                files.append(voisin)
    pos = nx.spring_layout(G)
    for i in range(len(noeuds_decouverts)):
        out = noeuds_decouverts[i:]
        H = nx.restricted_view(G, out, [])
        nx.draw_networkx(H, pos=pos)
        filename = f'{i}.png'
        i += 1
        filenames.append(filename)
        plt.savefig(filename)
        plt.close()
    nx.draw_networkx(G, pos=pos)
    filename = f'{i}.png'
    filenames.append(filename)
    plt.savefig(filename)
    plt.savefig('largeur.png')
    plt.close()
    return (noeuds_decouverts)
Exemplo n.º 10
0
def shortest_path(graph: nx.DiGraph, start: int, end: int,
                  avoid: typing.Collection[int]) ->\
        typing.Optional[typing.List[int]]:
    try:
        return nx.shortest_path(nx.restricted_view(graph, avoid, []), start,
                                end)
    except nx.NetworkXNoPath:
        return None
Exemplo n.º 11
0
def get_graph_distance(graph, row):
    graph_restricted = nx.restricted_view(graph, [], [(row.from_motif_index, row.to_motif_index)])
    try:
        distance = nx.shortest_path_length(graph_restricted, source=row.from_motif_index, target=row.to_motif_index,
                                           weight='path_weight')
    except:
        distance = 100000
    return distance
Exemplo n.º 12
0
def _is_separating_set(G, cut):
    """Assumes that the input graph is connected"""
    if len(cut) == len(G) - 1:
        return True

    H = nx.restricted_view(G, cut, [])
    if nx.is_connected(H):
        return False
    return True
Exemplo n.º 13
0
    def hierarchical_view(graph):
        def filter_edge(e):
            return e[-1]['edgetype'] == EdgeType.HIERARCHICAL

        restricted_node = []
        restricted_edge = [(n1, n2)
                           for (n1, n2, attr) in graph.edges(data=True)
                           if not filter_edge((n1, n2, attr))]
        return nx.restricted_view(graph, restricted_node, restricted_edge)
Exemplo n.º 14
0
    def temporal_view(graph):
        def filter_edge(e):
            return e[-1]['edgetype'] == EdgeType.CAUSAL

        restricted_node = []
        restricted_edge = [(n1, n2)
                           for (n1, n2, attr) in graph.edges(data=True)
                           if not filter_edge((n1, n2, attr))]
        return nx.restricted_view(graph, restricted_node, restricted_edge)
Exemplo n.º 15
0
def _is_separating_set(G, cut):
    """Assumes that the input graph is connected"""
    if len(cut) == len(G) - 1:
        return True

    H = nx.restricted_view(G, cut, [])
    if nx.is_connected(H):
        return False
    return True
Exemplo n.º 16
0
def test_alternative_flow_functions():
    graphs = [nx.grid_2d_graph(4, 4), nx.cycle_graph(5)]
    for G in graphs:
        node_conn = nx.node_connectivity(G)
        for flow_func in flow_funcs:
            all_cuts = nx.all_node_cuts(G, flow_func=flow_func)
            # Only test a limited number of cut sets to reduce test time.
            for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
                assert_equal(node_conn, len(cut))
                assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
Exemplo n.º 17
0
def get_filtered_graph(graph, filter_node=None, filter_edge=None):
    if filter_node:
        return nx.subgraph_view(graph, filter_node=filter_node)
    elif filter_edge:
        return nx.restricted_view(graph, [],
                                  [(n1, n2)
                                   for (n1, n2, attr) in graph.edges(data=True)
                                   if not filter_edge((n1, n2, attr))])
    else:
        return graph
Exemplo n.º 18
0
def _check_separating_sets(G):
    for cc in nx.connected_components(G):
        if len(cc) < 3:
            continue
        Gc = G.subgraph(cc)
        node_conn = nx.node_connectivity(Gc)
        all_cuts = nx.all_node_cuts(Gc)
        # Only test a limited number of cut sets to reduce test time.
        for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
            assert_equal(node_conn, len(cut))
            assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
Exemplo n.º 19
0
def test_alternative_flow_functions():
    graphs = [nx.grid_2d_graph(4, 4),
              nx.cycle_graph(5)]
    for G in graphs:
        node_conn = nx.node_connectivity(G)
        for flow_func in flow_funcs:
            all_cuts = nx.all_node_cuts(G, flow_func=flow_func)
            # Only test a limited number of cut sets to reduce test time.
            for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
                assert_equal(node_conn, len(cut))
                assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
Exemplo n.º 20
0
def _check_separating_sets(G):
    for cc in nx.connected_components(G):
        if len(cc) < 3:
            continue
        Gc = G.subgraph(cc)
        node_conn = nx.node_connectivity(Gc)
        all_cuts = nx.all_node_cuts(Gc)
        # Only test a limited number of cut sets to reduce test time.
        for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
            assert_equal(node_conn, len(cut))
            assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
Exemplo n.º 21
0
def loop_back_nodes(cfg: DiGraph) -> FrozenSet[int]:
    """
    Find all the nodes of a CFG that are exclusively part of a loop.

    A node is exclusively part of a loop if it belongs only to those paths that traverse the back-loop of a cycle.

    :arg cfg: the CFG representation of a program
    :return: a frozen set of all the loop-exclusive nodes
    """

    # Node 0 closes an improper loop over the CFG, so it must be ignored
    cycle_nodes = frozenset(
        chain.from_iterable(simple_cycles(restricted_view(cfg, [0], []))))
    return frozenset(
        cycle_nodes.difference(
            chain.from_iterable(
                # For every path, its last component is node 0; therefore, we have to cut it.
                map(lambda l: l[:-1], all_simple_paths(cfg, 1, 0)))))
Exemplo n.º 22
0
 def test_restricted_view(self):
     H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)])
     assert_equal(set(H.nodes), {1, 3, 4})
     assert_equal(set(H.edges), {(1, 1)})
Exemplo n.º 23
0
 def test_restricted_view(self):
     H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)])
     assert set(H.nodes) == {1, 3, 4}
     assert set(H.edges) == {(1, 1)}
Exemplo n.º 24
0
def register_heatmap(cfg: DiGraph, max_heat: int) -> Mapping[int, List[int]]:
    """
    Calculate the register heatmap of the program.

    Given the program's representation as a CFG, an heatmap laid over all the reachable nodes is drawn.
    When a node on which multiple execution paths converge is found, its portion of heatmap is calculated starting from
    the mean heat levels of all the incoming arcs.

    For the sake of simplicity, loops are pessimistically ignored (i.e. as if all looping conditions are false before
    entering the first iteration).

    :arg cfg: the program's representation as a CFG
    :arg max_heat: the maximum heat level a register can reach
    :return: an heatmap mapping every reachable line to a heat vector
    """

    # Clean the CFG from all loop arcs that are not part of simple paths
    noloop_cfg = restricted_view(cfg, loop_back_nodes(cfg), [])

    paths = list(all_simple_paths(noloop_cfg, 1, 0))
    # All nodes on which more than one execution flow converge
    merges = merge_points(noloop_cfg)
    # A collection of paths that cannot be completed because we still miss the initialization vector, indexed by node ID
    waiting_paths: MutableMapping[int, List[List[int]]] = {}
    # The scratchpad in which node heatmaps and final heat vectors are stored
    node_heatmaps: MutableMapping[int, Tuple[Mapping[int, List[int]],
                                             List[int]]] = {
                                                 0: ({}, [0] * len(Register))
                                             }

    while len(paths) > 0:
        lin_path = paths.pop()

        # Don't recalculate heatmaps for already-visited nodes
        for node in filter(lambda n: n not in node_heatmaps, lin_path):
            if node in merges:
                if node not in waiting_paths:
                    # Multiple paths converge on this node and we miss the initialization vector: initialize the waiting
                    # paths list for this node and store the path's stump
                    del lin_path[:lin_path.index(node)]
                    waiting_paths[node] = [lin_path]
                    break
                elif frozenset(
                        noloop_cfg.predecessors(node)).issubset(node_heatmaps):
                    # The initialization vector can finally be calculated: requeue all incomplete paths
                    paths.extend(waiting_paths[node])
                    del waiting_paths[node]
                    # Calculate this node's heatmap, mediating the incoming heat vectors
                    node_heatmaps[node] = node_register_heat(
                        noloop_cfg.nodes[node], max_heat,
                        mediate_heat([
                            node_heatmaps[n][1]
                            for n in noloop_cfg.predecessors(node)
                        ]))
                else:
                    # Multiple paths converge on this node and we miss the initialization vector: store the path's stump
                    del lin_path[:lin_path.index(node)]
                    waiting_paths[node].append(lin_path)
                    break
            else:
                # This node is part of a linear path: calculate its heatmap using the predecessor's final heat vector
                node_heatmaps[node] = node_register_heat(
                    noloop_cfg.nodes[node], max_heat,
                    node_heatmaps[next(noloop_cfg.predecessors(node))][1])

    heatmap = {}
    # Remove the initialization heat vector from the scratchpad
    del node_heatmaps[0]
    # Extend heatmaps onto cycle-only loops
    close_cycles(cfg, node_heatmaps, max_heat)
    # Collapse the scratchpad into the resulting global heatmap
    for nhm in node_heatmaps.values():
        heatmap.update(nhm[0])

    return heatmap
Exemplo n.º 25
0
 def test_restricted_view(self):
     H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)])
     assert_equal(set(H.nodes), {1, 3, 4})
     assert_equal(set(H.edges), {(1, 1)})
Exemplo n.º 26
0
 def broken_dag(self):
     return nx.restricted_view(self.dag, nodes=(), edges=self.broken_edges)