Пример #1
0
    def calculate(self):
        new_g1 = DfgMetricUtil.remove_frequencies_from_labels(self.model1)
        new_g2 = DfgMetricUtil.remove_frequencies_from_labels(self.model2)

        # calulate the nodes similarity first
        nodes_metric = DfgNodesSimilarityMetric(self.window,
                                                self.initial_trace,
                                                self.metric_name,
                                                self.model1,
                                                self.model2,
                                                convert=False)
        nodes_metric.calculate()

        # if the nodes similarity is different than 1
        # IPDD removes the different nodes
        # then it calculated the edges similarity metric
        if nodes_metric.value < 1:
            new_g1, new_g2 = DfgMetricUtil.remove_different_nodes(
                new_g1, new_g2,
                set.union(nodes_metric.diff_added, nodes_metric.diff_removed))
        # get the different edges
        self.diff_removed = set()
        diff_removed = nx.difference(new_g1, new_g2)
        for e in diff_removed.edges:
            self.diff_removed.add(e)

        self.diff_added = set()
        diff_added = nx.difference(new_g2, new_g1)
        for e in diff_added.edges:
            self.diff_added.add(e)

        # calculate the edges similarity metric
        inter = set(new_g1.edges).intersection(set(new_g2.edges))
        self.value = 2 * len(inter) / (len(new_g1.edges) + len(new_g2.edges))
        return self.value, self.diff_added, self.diff_removed
    def updateCurrentDag(self, dst_prefix, new_activeDag):
        """
        :param dst_prefix:
        :param new_activeDag: DAG directing any possible router towards dst_prefix
        """
        # Get current complete DAG
        c_completeDag = self.getCurrentDag(dst_prefix)
        
        # Get current active DAG
        c_activeDag = self.getActiveDag(dst_prefix)

        # Get edges to set 'active' = False in c_completeDag
        edges_to_inactive = nx.difference(c_activeDag, new_activeDag)
        # Set them
        for (x, y) in edges_to_inactive.edges_iter():
            if not c_completeDag.has_edge(x, y):
                c_completeDag.add_edge(x, y)
                c_completeDag[x][y]['default'] = False
            c_completeDag[x][y]['active'] = False
            c_completeDag[x][y]['ongoing_flows'] = False
            
        # Get edges to set 'active' = True in c_completeDag
        edges_to_active = nx.difference(new_activeDag, c_activeDag)
        # Set them
        for (x, y) in edges_to_active.edges_iter():
            if not c_completeDag.has_edge(x, y):
                c_completeDag.add_edge(x, y)
                c_completeDag[x][y]['default'] = False
            c_completeDag[x][y]['active'] = True
            c_completeDag[x][y]['ongoing_flows'] = True

        # Set new computed curren complete DAG to dict attribute
        self.setCurrentDag(dst_prefix, c_completeDag)
Пример #3
0
def compare_graphs(nx_graph, cu_graph):
    sources, destinations = cu_graph.view_edge_list()

    df = pd.DataFrame()
    df['source'] = sources.to_pandas()
    df['target'] = destinations.to_pandas()

    cu_to_nx_graph = nx.from_pandas_edgelist(df, create_using=nx.DiGraph())

    # first compare nodes

    ds0 = pd.Series(nx_graph.nodes)
    ds1 = pd.Series(cu_to_nx_graph.nodes)

    if not ds0.equals(ds1):
        return False

    # second compare edges

    diff = nx.difference(nx_graph, cu_to_nx_graph)
    if diff.number_of_edges() > 0:
        return False

    diff = nx.difference(cu_to_nx_graph, nx_graph)
    if diff.number_of_edges() > 0:
        return False

    # need to compare edge weights for weighted graphs as well but currently
    # users cannot retrieve edge weights from cugraph Graph objects
    # (cugraph issue #319).

    return True
Пример #4
0
def is_same_tree(tree1, tree2):
    """
    :param tree1: networkx Graph
    :param tree2: networkx Graph
    :return: True if the graphs have the same edges, False otherwise
    """
    dif1 = nx.difference(tree1, tree2)
    dif2 = nx.difference(tree2, tree1)
    same_graph = len(dif1.edges) == 0 and len(dif2.edges) == 0
    return same_graph
Пример #5
0
def compare_graphs(nx_graph, cu_graph):
    edgelist_df = cu_graph.view_edge_list().reset_index(drop=True)

    df = cudf.DataFrame()
    df["source"] = edgelist_df["src"]
    df["target"] = edgelist_df["dst"]
    if len(edgelist_df.columns) > 2:
        df["weight"] = edgelist_df["weights"]
        cu_to_nx_graph = nx.from_pandas_edgelist(
            df.to_pandas(),
            source="source",
            target="target",
            edge_attr=["weight"],
            create_using=nx.DiGraph(),
        )
    else:
        cu_to_nx_graph = nx.from_pandas_edgelist(
            df.to_pandas(), create_using=nx.DiGraph()
        )

    # first compare nodes
    ds0 = pd.Series(list(nx_graph.nodes)).sort_values(ignore_index=True)
    ds1 = pd.Series(list(cu_to_nx_graph.nodes)).sort_values(ignore_index=True)

    if not ds0.equals(ds1):
        print('ds0 != ds1')
        return False

    # second compare edges
    diff = nx.difference(nx_graph, cu_to_nx_graph)

    if diff.number_of_edges() > 0:
        print('diff.number_of_edges = ', diff.number_of_edges())
        return False

    diff = nx.difference(cu_to_nx_graph, nx_graph)
    if diff.number_of_edges() > 0:
        print('2: diff.number_of_edges = ', diff.number_of_edges())
        return False

    if len(edgelist_df.columns) > 2:
        df0 = cudf.from_pandas(nx.to_pandas_edgelist(nx_graph))
        merge = df.merge(df0, on=["source", "target"],
                         suffixes=("_cugraph", "_nx"))
        print("merge = \n", merge)
        print(merge[merge.weight_cugraph != merge.weight_nx])
        if not merge["weight_cugraph"].equals(merge["weight_nx"]):
            print('weights different')
            print(merge[merge.weight_cugraph != merge.weight_nx])
            return False

    return True
Пример #6
0
def low(G: nx.Graph, root) -> Tuple[Dict[int, int], nx.Graph]:
    """
    Calculate low function values for graph nodes.
    :param G: input Graph
    :param root: root node
    :return: tuple (low_map, tree) with dictionary mapping nodes to low function values and tree used in computations
    """
    low_map = dict()

    T = build_dfs_tree(G, root)

    G.remove_nodes_from(set(G.nodes()) - set(T.nodes))

    depth = nx.get_node_attributes(T, "depth")
    T_nodes = sorted(list(T.nodes()), reverse=True, key=lambda v: depth[v])
    T_ud = T.to_undirected()
    G_secondary = nx.difference(G, T_ud)

    for v in T_nodes:
        v_children_low = [low_map[w] for w in T.neighbors(v)]
        v_sec_dep = [depth[w] for w in G_secondary.neighbors(v)]
        v_dep = depth[v]
        low_map[v] = min(v_children_low + v_sec_dep + [v_dep])

    return low_map, T
Пример #7
0
def remove_bcc_edges_from_new_graph(new_graph, bcc_edges_list):
    bcc_edges_graph = nx.Graph()
    bcc_edges_graph.add_nodes_from(list(new_graph.nodes))
    bcc_edges_graph.add_edges_from(bcc_edges_list)
    # find the edges that are present in new graph but not in any bcc's.
    edge_list = list(nx.difference(new_graph, bcc_edges_graph).edges)
    return edge_list
Пример #8
0
 def draw_difference(self, path_old, path_new):
     self.draw_path(path_old)
     H = self.G.copy()
     H.add_edges_from(path_edges(path_new.path))
     H_ = nx.difference(self.G, H)
     nx.draw(self.G, self.pos)
     nx.draw(H_, self.pos, edge_color='blue')
Пример #9
0
def extract_dodag_before_after(data, list_nodes, neighbors, time, args):
    tic = tm.perf_counter()
    # Get DODAG some time steps before the anomaly is raised
    window_time = args.time_window
    data_before = extract_data_up_to(data, time - 1 * window_time, args)
    dodag_before = get_dodag(data_before)
    # Plot the graph corresponding to the DODAG extracted
    # fig, axs = plt.subplots(1, 3, figsize=(30,15))
    # axs[0].set_title('DODAG before anomaly')
    # nx.draw(dodag_before, with_labels=True, ax=axs[0])
    # Get DODAG after the anomaly is raised
    data_after = extract_data_up_to(data, time, args)
    dodag_after = get_dodag(data_after)
    # Plot the graph corresponding to the DODAG extracted
    # pos = nx.spring_layout(dodag_after)
    # axs[1].set_title('DODAG after anomaly')
    # nx.draw(dodag_after, pos=pos, with_labels=True, ax=axs[1])
    # nx.draw(dodag_after.subgraph(list_nodes), pos=pos, node_color='orange', with_labels=True, ax=axs[1])
    # nx.draw(dodag_after.subgraph(neighbors), pos=pos, node_color='yellow', with_labels=True, ax=axs[1])
    # Compute difference between graphs and plot everything
    dodag_difference = nx.difference(dodag_after, dodag_before)
    dodag_difference.remove_nodes_from(list(nx.isolates(dodag_difference)))
    # axs[2].set_title('DODAG difference')
    # nx.draw(dodag_difference, with_labels=True, ax=axs[2])
    # plt.suptitle(args.scenario)
    toc = tm.perf_counter()
    # print('Everything DODAG took {:.5f}'.format(toc - tic))
    # plt.show()
    if (len(dodag_difference) == 0):
        return False, []
    nodes_changing = dodag_difference.nodes()

    return True, nodes_changing
Пример #10
0
def cal_existed_edges_ratio(g1, g2, type):
    g1.remove_node(keyword)
    g2.remove_node(keyword)
    ngs = set(g1.nodes())
    ngs1 = set(g2.nodes())
    ngs2 = ngs & ngs1

    # 计算每个月的网络
    ngs = ngs - ngs2
    init_edge_num = len(g1.edges())
    for n in ngs:
        g1.remove_node(n)

    extra_node_edge = init_edge_num - len(g1.edges())

    # 计算总网络
    ngs1 = ngs1 - ngs2
    for n in ngs1:
        g2.remove_node(n)

    g3 = nx.difference(g1, g2)
    # 计算保留率
    r = 1 - (len(g3.edges()) + extra_node_edge) / init_edge_num
    print(r)
    return r
Пример #11
0
    def write_flowtable(self, f):
        output = {}

        graph = nx.Graph(self.graph)
        mst = nx.minimum_spanning_tree(graph)
        # Links that need to be disabled
        diff = nx.difference(graph, mst)

        for switch in self.switches.values():
            output[switch.dpid] = {}
            output[switch.dpid]["fdb"] = {}

            # Write FDB
            for src in switch.fdb._fdb.keys():
                output[switch.dpid]["fdb"][src.mac] = {}

                for dst, nex in switch.fdb._fdb[src].items():
                    port = self.graph[switch.name][nex.name]["port"]
                    output[switch.dpid]["fdb"][src.mac][dst.mac] = port

            # Write list of blocked ports (to remove loops)
            output[switch.dpid]["blocked_ports"] = [
                self.graph[switch.name][u]["port"]
                for u in diff.neighbors_iter(switch.name)
            ]

        yaml.dump(output, f, explicit_start=True, default_flow_style=False)
def difference(graph2, graph1):

    D1 = nx.difference(graph2, graph1)
    pos = nx.circular_layout(D1)
    D2 = nx.difference(graph1, graph2)  # edges in graph1 but not in graph2
    pos = nx.circular_layout(D2)

    nx.draw_networkx_nodes(D1,pos, node_color="g", node_size=1000)
    nx.draw_networkx_edges(D1,pos, edge_color='g',width=10) 
    nx.draw_networkx_nodes(D2,pos, node_color="r", node_size=1000)
    nx.draw_networkx_edges(D2,pos, edge_color='r',width=10) 

    plt.show()
#   plt.savefig("M.PDF",facecolor='pink')  #save graph

    return difference
Пример #13
0
    def difference(self, other_kg):
        """ Returns the difference of the knowledge graph and other_kg

        :param other_kg: The other knowledge graph
        :type other_kg: :class:`.KnowledgeGraph`
        """
        # Returns a KnowledgeGraph containing edges that exist in self but not in other_kg
        return KnowledgeGraph(nx.difference(self.net, other_kg.net))
Пример #14
0
def test_pbc_on_off_difference(universe, lipid_heads):
    lfls_pbc_on = LeafletFinder(universe, lipid_heads, cutoff=7, pbc=True)
    lfls_pbc_off = LeafletFinder(universe, lipid_heads, cutoff=7, pbc=False)
    pbc_on_graph = lfls_pbc_on.graph
    pbc_off_graph = lfls_pbc_off.graph
    diff_graph = NX.difference(pbc_on_graph, pbc_off_graph)
    assert_equal(set(diff_graph.edges), {(69, 153), (73, 79), (206, 317),
                                         (313, 319)})
Пример #15
0
def test_difference(testgraph):
    """
    Test the Difference of the two graphs are same
    """

    a = nx.difference(testgraph[0], testgraph[1])
    b = sg.digraph_operations.difference(testgraph[2], testgraph[3])
    digraph_equals(a, b)
Пример #16
0
def test_difference(testgraph):
    """
    Test the Difference of the two graphs are same
    """

    a = nx.difference(testgraph[0], testgraph[1])
    b = sg.graph_operations.difference(testgraph[2], testgraph[3])
    graph_equals(a, b)
Пример #17
0
 def __eq__(self, other: 'ScoringGraph'):
     if other == None:
         return False
     if (set(self.node.keys()) == set(other.node.keys())):
         result = nx.difference(self, other)
         return len(result) == 0
     else:
         return False
def getchangedlinks(G, Gnew):
    #get all the changed links
    edges_add = []
    Gdiff = nx.difference(Gnew, G)
    edges_add.append(Gdiff.edges())

    edges_rm = []
    Gdiff = nx.difference(G, Gnew)
    edges_rm.append(Gdiff.edges())

    # pdb.set_trace()

    # for e in edges:
    #   nodes.append(e[0])
    #   nodes.append(e[1])

    return edges_add, edges_rm
Пример #19
0
def main():
    print("Processing Aqualung Wiki")
    Aq = Aqualung()
    print("Processing Ian Anderson Wiki")
    IanA = Ian()

    print("Generating Similarity rankings for Aqualung")
    As = nx.simrank_similarity(Aq)
    A = [[As[u][v] for v in sorted(As[u])] for u in sorted(As)]
    sim_array = array(A)

    print("Generating Similarity rankings for Ian Anderson")
    Ia = nx.simrank_similarity(Aq)
    IanAr = [[Ia[u][v] for v in sorted(Ia[u])] for u in sorted(Ia)]
    Ian_array = array(IanAr)

    AqL = list(Aq.nodes)
    IanAL = list(IanA.nodes)

    print("\nSimilarities for Aqualung")
    for x in range(len(sim_array)):
        for y in range(len(sim_array[x])):
            if x == y:
                break
            elif sim_array[x][y] >= 0.01:
                print(AqL[x], " | ", AqL[y], " | ", sim_array[x][y])

    print("\nSimilarities for Ian Anderson")
    for x in range(len(Ian_array)):
        for y in range(len(Ian_array[x])):
            if x == y:
                break
            elif Ian_array[x][y] >= 0.01:
                print(IanAL[x], " | ", IanAL[y], " | ", Ian_array[x][y])

    #removing nodes for networkx.difference
    for node in AqL:
        if (node in IanAL):
            continue
        else:
            Aq.remove_node(node)

    for node in IanAL:
        if (node in AqL):
            continue
        else:
            IanA.remove_node(node)
    print("\nComputing Difference")

    D = nx.difference(Aq, IanA)
    D.remove_nodes_from(list(nx.isolates(D)))
    print(nx.info(D))

    #Computing Intersection
    print("\nIntersection")
    I = nx.intersection(Aq, IanA)
    I.remove_nodes_from(list(nx.isolates(I)))
    print(nx.info(I))
Пример #20
0
def compare_graphs(nx_graph, cu_graph):
    sources, destinations, values = cu_graph.view_edge_list()

    df = cudf.DataFrame()
    df['source'] = sources
    df['target'] = destinations
    if values is not None:
        df['weight'] = values
        cu_to_nx_graph = nx.from_pandas_edgelist(df.to_pandas(),
                                                 source='source',
                                                 target='target',
                                                 edge_attr=['weight'],
                                                 create_using=nx.DiGraph())
    else:
        cu_to_nx_graph = nx.from_pandas_edgelist(df.to_pandas(),
                                                 create_using=nx.DiGraph())

    # first compare nodes

    ds0 = pd.Series(nx_graph.nodes)
    ds1 = pd.Series(cu_to_nx_graph.nodes)

    if not ds0.equals(ds1):
        return False

    # second compare edges

    diff = nx.difference(nx_graph, cu_to_nx_graph)

    if diff.number_of_edges() > 0:
        return False

    diff = nx.difference(cu_to_nx_graph, nx_graph)
    if diff.number_of_edges() > 0:
        return False

    if values is not None:
        df0 = cudf.from_pandas(nx.to_pandas_edgelist(nx_graph))
        df0 = df0.sort_values(by=['source', 'target'])
        df1 = df.sort_values(by=['source', 'target'])

        if not df0['weight'].equals(df1['weight']):
            return False

    return True
Пример #21
0
def test_difference():
    G = nx.Graph()
    H = nx.Graph()
    G.add_nodes_from([1, 2, 3, 4])
    G.add_edge(1, 2)
    G.add_edge(2, 3)
    H.add_nodes_from([1, 2, 3, 4])
    H.add_edge(2, 3)
    H.add_edge(3, 4)
    D = nx.difference(G, H)
    assert set(D.nodes()) == set([1, 2, 3, 4])
    assert sorted(D.edges()) == [(1, 2)]
    D = nx.difference(H, G)
    assert set(D.nodes()) == set([1, 2, 3, 4])
    assert sorted(D.edges()) == [(3, 4)]
    D = nx.symmetric_difference(G, H)
    assert set(D.nodes()) == set([1, 2, 3, 4])
    assert sorted(D.edges()) == [(1, 2), (3, 4)]
Пример #22
0
def test_difference2():
    G = nx.Graph()
    H = nx.Graph()
    G.add_nodes_from([1, 2, 3, 4])
    H.add_nodes_from([1, 2, 3, 4])
    G.add_edge(1, 2)
    H.add_edge(1, 2)
    G.add_edge(2, 3)
    D = nx.difference(G, H)
    assert set(D.nodes()) == {1, 2, 3, 4}
    assert sorted(D.edges()) == [(2, 3)]
    D = nx.difference(H, G)
    assert set(D.nodes()) == {1, 2, 3, 4}
    assert sorted(D.edges()) == []
    H.add_edge(3, 4)
    D = nx.difference(H, G)
    assert set(D.nodes()) == {1, 2, 3, 4}
    assert sorted(D.edges()) == [(3, 4)]
Пример #23
0
def test_difference2():
    G = nx.Graph()
    H = nx.Graph()
    G.add_nodes_from([1, 2, 3, 4])
    H.add_nodes_from([1, 2, 3, 4])
    G.add_edge(1, 2)
    H.add_edge(1, 2)
    G.add_edge(2, 3)
    D = nx.difference(G, H)
    assert_equal(set(D.nodes()), set([1, 2, 3, 4]))
    assert_equal(sorted(D.edges()), [(2, 3)])
    D = nx.difference(H, G)
    assert_equal(set(D.nodes()), set([1, 2, 3, 4]))
    assert_equal(sorted(D.edges()), [])
    H.add_edge(3, 4)
    D = nx.difference(H, G)
    assert_equal(set(D.nodes()), set([1, 2, 3, 4]))
    assert_equal(sorted(D.edges()), [(3, 4)])
Пример #24
0
def test_difference():
    G=nx.Graph()
    H=nx.Graph()
    G.add_nodes_from([1,2,3,4])
    G.add_edge(1,2)
    G.add_edge(2,3)
    H.add_nodes_from([1,2,3,4])
    H.add_edge(2,3)
    H.add_edge(3,4)
    D=nx.difference(G,H)
    assert_equal( set(D.nodes()) , set([1,2,3,4]) )    
    assert_equal( sorted(D.edges()) , [(1,2)] )    
    D=nx.difference(H,G)
    assert_equal( set(D.nodes()) , set([1,2,3,4]) )    
    assert_equal( sorted(D.edges()) , [(3,4)] )    
    D=nx.symmetric_difference(G,H)
    assert_equal( set(D.nodes()) , set([1,2,3,4]) )    
    assert_equal( sorted(D.edges()) , [(1,2),(3,4)] )    
Пример #25
0
def compare_graphs(nx_graph, cu_graph):
    edgelist_df = cu_graph.view_edge_list()

    df = cudf.DataFrame()
    df['source'] = edgelist_df['src']
    df['target'] = edgelist_df['dst']
    if len(edgelist_df.columns) > 2:
        df['weight'] = edgelist_df['weights']
        cu_to_nx_graph = nx.from_pandas_edgelist(df.to_pandas(),
                                                 source='source',
                                                 target='target',
                                                 edge_attr=['weight'],
                                                 create_using=nx.DiGraph())
    else:
        cu_to_nx_graph = nx.from_pandas_edgelist(df.to_pandas(),
                                                 create_using=nx.DiGraph())

    # first compare nodes

    ds0 = pd.Series(nx_graph.nodes)
    ds1 = pd.Series(cu_to_nx_graph.nodes)

    if not ds0.equals(ds1):
        return False

    # second compare edges

    diff = nx.difference(nx_graph, cu_to_nx_graph)

    if diff.number_of_edges() > 0:
        return False

    diff = nx.difference(cu_to_nx_graph, nx_graph)
    if diff.number_of_edges() > 0:
        return False

    if len(edgelist_df.columns) > 2:
        df0 = cudf.from_pandas(nx.to_pandas_edgelist(nx_graph))
        df0 = df0.sort_values(by=['source', 'target'])
        df1 = df.sort_values(by=['source', 'target'])
        if not df0['weight'].equals(df1['weight']):
            return False

    return True
Пример #26
0
    def diff_graphs0(self, g1, g2):
        """ Diff nx graphs. """
        g1_copy = g1.copy()
        g1_copy.remove_nodes_from(n for n in g1 if not n in g2)
        g2_copy = g2.copy()
        g2_copy.remove_nodes_from(n for n in g2 if not n in g1)

        g1_minus_g2 = nx.difference(g1_copy, g2_copy)
        #self.print_graph (g1_minus_g2)
        return g1_minus_g2
Пример #27
0
def is_different(dag1, dag2):
    diff_dag = None
    is_diff = False
    try:
        diff_dag = nx.difference(dag1, dag2)
        is_diff = len(diff_dag.edges()) != 0
    except nx.exception.NetworkXError:
        is_diff = True

    return is_diff
Пример #28
0
def main():
    print("Generating Aq")
    Aq = Aqualung()
    print("Generating IanA")
    IanA = Ian()
    print("Computing similarity")
    nx.write_edgelist(Aq, "aq.edgelist")
    #As = nx.simrank_similarity(Aq,max_iterations=5)
    #print("Done with simrank")

    G1 = nx.DiGraph()
    G1.add_nodes_from([1, 2, 3, 4, 5])
    G1.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 5)])

    #s = nx.simrank_similarity(G1)
    #A = [[s[u][v] for v in sorted(s[u])] for u in sorted(s)]
    #sim_array = array(A)
    #print(sim_array)
    #AqL = list(Aq.nodes)
    #IanAL = list(IanA.nodes)

    #for node in AqL:
    #   for node2 in AqL:
    #       print("Checking",node," ",node2)
    #       sim_val = nx.simrank_similarity(Aq,source=node,target=node2)
    #       if sim_val >= 0.01:
    #           print(node, node2, sim_val)

    #removing nodes for networkx.difference
    AqL = list(Aq.nodes)
    IanAL = list(IanA.nodes)

    for node in AqL:
        if (node in IanAL):
            continue
        else:
            Aq.remove_node(node)

    for node in IanAL:
        if (node in AqL):
            continue
        else:
            IanA.remove_node(node)
    print("\nComputing Difference")

    D = nx.difference(Aq, IanA)
    D.remove_nodes_from(list(nx.isolates(D)))
    print(nx.info(D))

    #Computing Intersection
    print("\nIntersection")
    I = nx.intersection(Aq, IanA)
    I.remove_nodes_from(list(nx.isolates(I)))
    print(nx.info(I))
Пример #29
0
  def calcConvergeTime(self, t1, t2, converge_time = -1):
    # calculate the exact time that the network becomes stable:
    # switch states and controller states match with the physical network
    converged = True

    # controllers should be consistent with each other and the underlying graph
    for c in self.controller_names:
      ctrl = self.objs[c]
      if not nx.is_isomorphic(self.graph, ctrl.graph):
        converged = False
        cgraph = nx.Graph(ctrl.graph)
        node_diff = set(self.graph.nodes()) - set(cgraph.nodes())
        cgraph.add_nodes_from(list(node_diff))
        node_diff = set(cgraph.nodes()) - set(self.graph.nodes())
        self.graph.add_nodes_from(list(node_diff))
        diff = nx.difference(self.graph, cgraph)
        print "%f %s not converged %s"%(self.ctx.now, ctrl.name, diff.edges())
        #break
      else: 
        print "%f %s converged"%(self.ctx.now, ctrl.name)
    # switches should also be consistent with each other and the controllers
    if converged:
      c = self.objs[self.controller_names[0]]
      rules = {}
      for s in self.switch_names:
        rules[s] = {}
      sp = nx.shortest_paths.all_pairs_shortest_path(c.graph)
      for host in c.hosts:
        for h2 in c.hosts:
          if h2 == host:
            continue
          if h2.name in sp[host.name]:
            p = SourceDestinationPacket(host.address, h2.address)
            path = zip(sp[host.name][h2.name], \
                       sp[host.name][h2.name][1:])
            for (a, b) in path[1:]:
              link = c.graph[a][b]['link']
              rules[a][p.pack()] = link

      for s in self.switch_names:
        sw = self.objs[s]
        if rules[s] != sw.rules:
          print self.ctx.now, s, len(rules), len(sw.rules)
          converged = False
          break

    if converged:
      if converge_time == -1:
        print "CONVERGE_TIME: ", t1, t2, self.ctx.now
        self.ctx.schedule_task(10, lambda: self.calcConvergeTime(t1, t2, self.ctx.now))
      else:
        self.ctx.schedule_task(10, lambda: self.calcConvergeTime(t1, t2, converge_time))
    else:
      self.ctx.schedule_task(10, lambda: self.calcConvergeTime(t1, t2, -1))
Пример #30
0
 def test_save_results(self):
     se1 = StructureConstraintBasedEstimator(self.s1, 0.1, 0.1)
     se1.ctpc_algorithm()
     file_name = './PyCTBN/tests/estimators/test_save.json'
     se1.save_results(file_name)
     with open(file_name) as f:
         js_graph = json.load(f)
         result_graph = nx.json_graph.node_link_graph(js_graph)
         self.assertFalse(
             nx.difference(se1._complete_graph, result_graph).edges)
     os.remove(file_name)
Пример #31
0
def main(argv):
    args = get_args(argv)

    inf_gene_pairs = np.genfromtxt(args['reg_net_file'],
                                   dtype=str,
                                   delimiter='\t')
    large_known_net = np.genfromtxt(args['known_network_file'],
                                    dtype=str,
                                    delimiter='\t')

    # get set of gene names that are in both inferred and known networks
    inf_gene_set = get_inf_gene_set(inf_gene_pairs)
    kno_gene_set = set(
        large_known_net[:, (0, 2)].flat)  # 0th an 2nd col are gene names
    gene_set = inf_gene_set.intersection(kno_gene_set)

    inf_graph = get_inf_graph(inf_gene_pairs, gene_set, 0.776)
    kno_graph = get_kno_graph(large_known_net, gene_set)

    # print number of nodes and edges in inferred and known regulatory networks
    print str(len(inf_graph.nodes())) + ' nodes in inferred graph'
    print str(len(kno_graph.nodes())) + ' nodes in known graph'
    print str(len(inf_graph.edges())) + ' edges in inferred graph'
    print str(len(kno_graph.edges())) + ' edges in known graph'

    # find edges exclusive to each graph and shared between graphs
    inf_graph_only = nx.difference(inf_graph, kno_graph)
    kno_graph_only = nx.difference(kno_graph, inf_graph)
    shared_graph = nx.difference(inf_graph, inf_graph_only)

    # print edges shared between graphs
    print str(len(inf_graph_only.edges())) + ' of ' + str(
        len(inf_graph.edges())) + ' edges unique to inf_graph'
    print str(len(kno_graph_only.edges())) + ' of ' + str(
        len(kno_graph.edges())) + ' edges unique to kno_graph'
    print 'the ' + str(len(inf_graph.edges()) -
                       len(inf_graph_only.edges())) + ' edge(s) in common are:'
    print shared_graph.edges()

    # plot ego graph for specific gene
    write_ego_graph(shared_graph, EGO_GRAPH_GENE)
Пример #32
0
    def difference(cls, topo1, topo2):
        try:
            self = cls(nx.difference(topo1, topo2))
        except nx.NetworkXError:
            return None

        if len(self.edges()) == 0:
            return None

        self.copy_attributes(topo1)
        self.reconcile_attributes(topo1)
        return self
Пример #33
0
    def difference(cls,topo1,topo2):
        try:
            self = cls(nx.difference(topo1,topo2))
        except nx.NetworkXError:
            return None

        if len(self.edges()) == 0:
            return None

        self.copy_attributes(topo1)
        self.reconcile_attributes(topo1)
        return self
Пример #34
0
    def spurious_edges(self) -> typing.List:
        """Return the spurious edges present in the estimated structure, if a prior net structure is present in
            ``_sample_path.structure``.

        :return: A list containing the spurious edges
        :rtype: List
        """
        if not self._sample_path.has_prior_net_structure:
            return []
        real_graph = nx.DiGraph()
        real_graph.add_nodes_from(self._sample_path.structure.nodes_labels)
        real_graph.add_edges_from(self._sample_path.structure.edges)
        return nx.difference(real_graph, self._complete_graph).edges
Пример #35
0
def test_difference_multigraph_attributes():
    g = nx.MultiGraph()
    g.add_edge(0, 1, key=0)
    g.add_edge(0, 1, key=1)
    g.add_edge(0, 1, key=2)
    h = nx.MultiGraph()
    h.add_edge(0, 1, key=0)
    h.add_edge(0, 1, key=3)
    gh = nx.difference(g, h)
    assert set(gh.nodes()) == set(g.nodes())
    assert set(gh.nodes()) == set(h.nodes())
    assert sorted(gh.edges()) == [(0, 1), (0, 1)]
    assert sorted(gh.edges(keys=True)) == [(0, 1, 1), (0, 1, 2)]
Пример #36
0
def test_difference_multigraph_attributes():
    g = nx.MultiGraph()
    g.add_edge(0, 1, key=0)
    g.add_edge(0, 1, key=1)
    g.add_edge(0, 1, key=2)
    h = nx.MultiGraph()
    h.add_edge(0, 1, key=0)
    h.add_edge(0, 1, key=3)
    gh = nx.difference(g, h)
    assert_equal(set(gh.nodes()), set(g.nodes()))
    assert_equal(set(gh.nodes()), set(h.nodes()))
    assert_equal(sorted(gh.edges()), [(0, 1)])
    assert_equal(sorted(gh.edges(keys=True)), [(0, 1, 3)])
Пример #37
0
def bayesnet_compare_plot(bayesmodel_true, bayesmodel_predicted, ax, pos):
    nx.draw_networkx_nodes(bayesmodel_true,
                           ax=ax,
                           pos=pos,
                           node_size=800,
                           node_color='g',
                           alpha=0.6)
    nx.draw_networkx_labels(bayesmodel_true,
                            ax=ax,
                            pos=pos,
                            font_size=16,
                            font_weight='bold')
    nx.draw_networkx_edges(nx.intersection(bayesmodel_true,
                                           bayesmodel_predicted),
                           ax=ax,
                           pos=pos,
                           width=4,
                           edge_color='k',
                           style='solid',
                           alpha=0.6,
                           arrowsize=25)
    nx.draw_networkx_edges(nx.difference(bayesmodel_true,
                                         bayesmodel_predicted),
                           ax=ax,
                           pos=pos,
                           width=4,
                           edge_color='r',
                           style='dashed',
                           alpha=0.6,
                           arrowsize=25)
    nx.draw_networkx_edges(nx.difference(bayesmodel_predicted,
                                         bayesmodel_true),
                           ax=ax,
                           pos=pos,
                           width=4,
                           edge_color='b',
                           style='dashed',
                           alpha=0.6,
                           arrowsize=25)
Пример #38
0
def test_difference_multigraph_attributes():
    g = nx.MultiGraph()
    g.add_edge(0, 1, key=0)
    g.add_edge(0, 1, key=1)
    g.add_edge(0, 1, key=2)
    h = nx.MultiGraph()
    h.add_edge(0, 1, key=0)
    h.add_edge(0, 1, key=3)
    gh = nx.difference(g, h)
    assert_equal( set(gh.nodes()) , set(g.nodes()) )
    assert_equal( set(gh.nodes()) , set(h.nodes()) )
    assert_equal( sorted(gh.edges()) , [(0,1)] )
    assert_equal( sorted(gh.edges(keys=True)) , [(0,1,3)] )
Пример #39
0
def test_difference_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

    gh = nx.difference(g, h)
    assert_equal( set(gh.nodes()) , set(g.nodes()) )
    assert_equal( set(gh.nodes()) , set(h.nodes()) )
    assert_equal( sorted(gh.edges()) , [])

    h.remove_node(0)
    assert_raises(nx.NetworkXError, nx.intersection, g, h)
Пример #40
0
    def removeAllocationEntry(self, prefix, flow):
        """
        """
        # Wait until flow finishes
        time.sleep(flow['duration']) 
        
        # Acquire locks for self.flow_allocation and self.dags
        # dictionaries
        self.flowAllocationLock.acquire()
        self.dagsLock.acquire()
        
        log.info(lineend)
        if prefix not in self.flow_allocation.keys():
            # prefix not in table
            raise KeyError("The is no such prefix allocated: %s"%str(prefix))
        else:
            if flow in self.flow_allocation[prefix].keys():
                path_list = self.flow_allocation[prefix].pop(flow, None)
            else:
                raise KeyError("%s is not alloacated in this prefix %s"%str(repr(flow)))

        t = time.strftime("%H:%M:%S", time.gmtime())
        log.info("%s - Flow REMOVED from Paths\n"%t)
        log.info("\t* Dest_prefix: %s\n"%prefix)
        to_log = "\t* Paths (%s): %s\n"
        log.info(to_log%(len(path_list), str(self.toLogRouterNames(path_list))))
        log.info("\t* Flow: %s\n"%self.toLogFlowNames(flow))

        # Current dag for destination
        current_dag = self.getCurrentDag(prefix)
        
        # Get the active Dag
        activeDag = self.getActiveDag(prefix)

        # Log active DAG
        to_log = "\t* removeAllocationEntry: current active DAG\n\t  %s\n"
        log.info(to_log%str(self.toLogDagNames(activeDag).edges()))
        
        # Get current remaining allocated flows for destination
        remaining_flows = self.getAllocatedFlows(prefix)

        # Create DAG showing remaining flow paths only
        edges_with_flows = []
        for (f, pl) in remaining_flows:
            edges_with_flows += self.getEdgesFromPathList(pl)
        edges_with_flows = list(set(edges_with_flows))
        remaining_traffic_dag = nx.DiGraph()
        remaining_traffic_dag.add_nodes_from(activeDag.nodes())
        remaining_traffic_dag.add_edges_from(edges_with_flows)
        
        # Difference with active DAG can be set to 'ongoing_flows' = False
        to_set_noflows = nx.difference(activeDag, remaining_traffic_dag)
        for (x, y) in to_set_noflows.edges_iter():
            current_dag[x][y]['ongoing_flows'] = False
            
        # Set the new calculated dag to its destination prefix dag
        self.setCurrentDag(prefix, current_dag)

        # Check if we can set destination forwarding to the initial
        # default OSPF DAG
        if len(remaining_flows) == 0:
            # Log a bit
            to_log = "\t* No more flows remain to prefix."
            to_log += " Re-setting to initial OSPF DAG\n"
            log.info(to_log)
            
            # Set forwarding to original
            self.setOSPFOriginalDAG(prefix)
        else:
            # Log it only
            log.info("\t* Some flows to prefix still remain.\n")

            # Log final DAG that is foced
            activeDag = self.getActiveDag(prefix)
            to_log = "\t* removePrefixLies: final active DAG\n\t  %s\n"
            log.info(to_log%str(self.toLogDagNames(activeDag).edges()))

            # Force it to fibbing
            self.sbmanager.add_dag_requirement(prefix, activeDag.copy())
            log.info(lineend)
        
        # Release locks
        self.flowAllocationLock.release()
        self.dagsLock.release()
Пример #41
0
    def run(self, raw_data, objects):
        graphs = []
        new_objects = []
        for overlay in self.__sub_overlays:
            g = overlay.substitutes(raw_data)
            graphs.append(g)

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

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

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

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

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

        
        new_objects = new_objects + polygons
        z_order_graph = nx.DiGraph()
        z_order_graph.add_nodes_from(new_objects)
        
        for i in range(0, len(polygons)):
            polygon1 = polygons[i]
            for j in range(i + 1, len(polygons)):
                polygon2 = polygons[j]
                if polygon1 != polygon2:
                    if polygon1.contains(polygon2):
                        z_order_graph.add_edge(polygon1, polygon2)
                    elif polygon2.contains(polygon1):
                        z_order_graph.add_edge(polygon2, polygon1)
            
       
        for obj in new_objects:
            for edge in obj.edges():
                if 'below' in graph[edge[0]][edge[1]]:
                    below = graph[edge[0]][edge[1]]['below']
                    if below != None:
                        for obj_above in new_objects:
                            if obj != obj_above:
                                if obj_above.has_edge(below.start(), below.end()):
                                    z_order_graph.add_edge(obj, obj_above)
                if 'above' in graph[edge[0]][edge[1]]:
                    above = graph[edge[0]][edge[1]]['above']
                    if above != None:
                        for obj_below in new_objects:
                            if obj != obj_below:
                                if obj_below.has_edge(above.start(), above.end()):
                                    z_order_graph.add_edge(obj_below, obj)
                if 'z_order' in graph[edge[0]][edge[1]]:
                    z_order = graph[edge[0]][edge[1]]['z_order']
                    if z_order != None:
                        for other_obj in new_objects:
                            if obj != other_obj:
                                if (isinstance(other_obj, Polygon) and other_obj.frame().intersects(edge)) or (isinstance(other_obj, OpenGraph) and other_obj.intersects(edge)):
                                    if z_order == 'above':
                                        z_order_graph.add_edge(other_obj, obj)
                                    elif z_order == 'below':
                                        z_order_graph.add_edge(obj, other_obj)
                                    else:
                                        raise ValueError, "Wrong value for z_order."

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

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

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

        self._objects = objects
        self._parsed_data = raw_data
        return
Пример #42
0
def test_difference_raise():
    G = nx.path_graph(4)
    H = nx.path_graph(3)
    GH = nx.difference(G, H)
G1.edges()

""" Intersection de graphes """
# Les noeuds de H et G doivent etre les memes
H.clear()
H.add_nodes_from([1,2,3,4])
H.add_edge(1,2)
GH4=nx.intersection(G,H)
GH4.nodes()
#[1,2,3,4]
GH4.edges()
#[(1,2)]

""" Difference de graphes """
# Les noeuds de H et G doivent etre les memes
GH5=nx.difference(G,H)
GH5.nodes()
# [1,2,3,4]
GH5.edges()
# [((2,3),(3,4)]
# Retourne un graphe avec des aretes qui existent dans G mais pas dans H

""" Difference symetrique de graphes """
# Les noeuds de H et G doivent etre les memes
GH6=nx.symmetric_difference(G,H)
GH6.nodes()
# [1,2,3,4]
GH6.edges()
# [((2,3),(3,4)]
# Retourne un graphe avec des aretes qui sont soit dans G soit dans H mais pas les deux
Пример #44
0
G_list['firstth'] = G6

G7 = nx.read_gml("hdb_collab_cluster_sample_firstth20.gml")
G_list['firstth20'] = G7


print 'ground truth sample cluster:'
print 'nodes:', len(G0.nodes())
print 'edges:', len(G0.edges())
print '\n\n'


for node in G1.nodes():
    for key, value in G_list.iteritems():
        if not value.has_node(node):
            value.add_node(node)


for key, value in G_list.iteritems():
    print key, 'sample cluster'
    print 'nodes:', len(value.nodes())
    print 'edges:', len(value.edges())

    print '\nDIFF normal test:'
    print 'missing edges:', len(nx.difference(G1, value).edges())
    print 'added edges:', len(nx.difference(value, G1).edges())
    print '\n\n'



Пример #45
0
def test_mixed_type_difference():
    G = nx.Graph()
    H = nx.MultiGraph()
    U = nx.difference(G,H)