Пример #1
0
def build_graph_from_dict_nx(graph_dict):
    import networkx as nx
    import networkx.classes.function as fn

    # currently only undirected graph, with no feature stored
    # TODO: option for directed graph
    # TODO(DONE): see features

    # graph attributes
    g = nx.Graph()
    g.graph['num_evils'] = graph_dict['num_evils']
    g.graph['num_evil_edges'] = graph_dict['num_evil_edges']

    # node attributes
    g.add_nodes_from(range(graph_dict['num_nodes']))
    node_attr = {i: is_bot for i, is_bot in enumerate(graph_dict['y'])}
    fn.set_node_attributes(G=g, values=node_attr, name='is_bot')

    # edge attributes
    g.add_edges_from(
        zip(graph_dict['edge_index'][0], graph_dict['edge_index'][1]))
    edges = zip(graph_dict['edge_index'][0], graph_dict['edge_index'][1])
    edge_attr = {e: bot for e, bot in zip(edges, graph_dict['edge_y'])}
    fn.set_edge_attributes(G=g, values=edge_attr, name='is_bot_connection')

    return g
Пример #2
0
def set_edge_attributes(G, values, name=None):  # noqa: C901
    if G.is_multigraph():
        # multigraph forward NetworkX
        func.set_edge_attributes(G, values, name)
        return

    if name is not None:
        # `values` does not contain attribute names
        try:
            # if `values` is a dict using `.items()` => {edge: value}
            for (u, v), value in values.items():
                dd = G.get_edge_data(u, v)
                if dd is not None:
                    dd[name] = value
                    G.set_edge_data(u, v, dd)
        except AttributeError:
            # treat `values` as a constant
            for u, v, data in G.edges(data=True):
                data[name] = values
    else:
        for (u, v), d in values.items():
            dd = G.get_edge_data(u, v)
            if dd is not None:
                dd.update(d)
                G.set_edge_data(u, v, dd)
Пример #3
0
    def enforcement_representation(self) -> DiGraph:
        """
        Fully connected graph representation used for enforcement, where
        each edge contains a feature donoting whether it represents an existing attack,
        whether it represents a self attack and whether an opposing attack exists.
        """
        graph = self.base_representation

        # add input if edge exists
        set_edge_attributes(graph, 1, "edge_exists")
        for u, v in non_edges(graph):
            graph.add_edge(u, v, edge_exists=0)

        # self attacks
        set_edge_attributes(graph, 0, "edge_self")
        for n in graph.nodes:
            if not graph.has_edge(n, n):
                graph.add_edge(n, n, edge_exists=0)
            graph.edges[n, n]["edge_self"] = 1

        # add feature (exist or none exist) of the opposite edge
        set_edge_attributes(graph, None, "edge_opposite")
        for u, v in graph.edges:
            graph.edges[u, v]["edge_opposite"] = graph.edges[v,
                                                             u]["edge_exists"]

        set_edge_attributes(graph, 0, "edge_y")
        set_edge_attributes(graph, 0, "edge_flipped")

        return graph
Пример #4
0
 def friction_coefficients(self, c):
     if isinstance(c, dict) and len(c.values()) == len(self.edges):
         keys, values = list(self.edges), list(c.values())
         attr = dict(zip(keys, values))
         set_edge_attributes(self, attr, name="c")
         self._c_init = True
     else:
         raise IOError(
             "Check input object type is dict and has as many values as there are edges."
         )
     return None
Пример #5
0
 def maximum_pressure_ratio(self, alpha_max):
     if isinstance(alpha_max, dict) and len(alpha_max.values()) == len(
             self.edges):
         keys, values = list(self.edges), list(alpha_max.values())
         attr = dict(zip(keys, values))
         set_edge_attributes(self, attr, name="alpha_max")
         self._alpha_max_init = True
     else:
         raise IOError(
             "Check input object type is dict and has as many values as there are edges."
         )
     return None
Пример #6
0
 def reference_flows(self, ref_flows):
     if isinstance(ref_flows, dict) and len(ref_flows.values()) == len(
             self.edges):
         keys, values = list(
             self.edges), [2 * val for val in list(ref_flows.values())]
         attr = dict(zip(keys, values))
         set_edge_attributes(self, attr, name="ref_flows")
         self._ref_flows_init = True
     else:
         raise IOError(
             "Check input object type is dict and has as many values as there are edges."
         )
     return None
Пример #7
0
    def agnn_representation(self) -> DiGraph:
        """
        Basic graph representation with an empty node input and target variable
        """
        graph = self.base_representation
        set_node_attributes(graph, 0, "node_y")

        # add opposing edge for each existing edge
        set_edge_attributes(graph, 1, 'edge_exists')
        for u, v in graph.edges:
            if not graph.has_edge(v, u) and v != u:
                graph.add_edge(v, u, edge_exists=0)

        # add feature (exist or none exist) of the opposite edge
        set_edge_attributes(graph, None, "edge_opposite")
        for u, v in graph.edges:
            graph.edges[u, v]["edge_opposite"] = graph.edges[v,
                                                             u]["edge_exists"]

        # self attacks
        set_edge_attributes(graph, 0, "edge_self")
        for n in graph.nodes:
            if graph.has_edge(n, n):
                graph.edges[n, n]["edge_self"] = 1

        return graph