Ejemplo n.º 1
0
  def MaybeAddSingleExitBlock(self, g: nx.MultiDiGraph) -> None:
    """Add a magic exit block to unite the exit statements of a CFG.

    Args:
      g: The graph to add the exit block to.
    """
    exit_blocks = list(nx_utils.ExitBlockIterator(g))
    if not exit_blocks:
      raise ValueError("No exit blocks found in graph!")

    if (
      self.only_add_entry_and_exit_blocks_if_required and len(exit_blocks) == 1
    ):
      exit_block = exit_blocks[0][0]
    else:
      exit_block = f"{g.name}_exit"
      g.add_node(
        exit_block, name=exit_block, type="magic", x=self.dictionary["!MAGIC"]
      )
      # Connect exit blocks.
      for node, data in exit_blocks:
        g.add_edge(node, exit_block, flow="control")
      # Add a dataflow edge out, if there is one.
      for src, dst, data in nx_utils.DataFlowEdgeIterator(g):
        if dst == node:
          g.add_edge(node, exit_block, flow="data")
          break
    g.exit_block = exit_block
Ejemplo n.º 2
0
def add_ref_edges(g: nx.MultiDiGraph, node):
    """Add edges with attr `data` for data references of the given node"""

    if isinstance(node, clang.graph.StmtInfo):
        for ref_rel in node.ref_relations:
            g.add_node(ref_rel, attr=(filter_type(ref_rel.type)))
            g.add_edge(node, ref_rel, attr="data")
Ejemplo n.º 3
0
def _add_nodes_and_edges(G: nx.MultiDiGraph,
                         name: str,
                         stops_df: pd.DataFrame,
                         summary_edge_costs: pd.DataFrame,
                         bidirectional: bool = False) -> Dict[str, str]:
    # As we convert stop ids to actual nodes, let's keep track of those names
    # here so that we can reference them when we add connector edges across
    # the various feeds loaded into the graph
    sid_lookup = {}

    for i, row in stops_df.iterrows():
        sid = str(row.stop_id)
        full_sid = nameify_stop_id(name, sid)

        # Add to the lookup crosswalk dictionary
        sid_lookup[sid] = full_sid
        G.add_node(full_sid,
                   boarding_cost=row.avg_cost,
                   y=row.stop_lat,
                   x=row.stop_lon)

    for i, row in summary_edge_costs.iterrows():
        sid_fr = nameify_stop_id(name, row.from_stop_id)
        sid_to = nameify_stop_id(name, row.to_stop_id)
        G.add_edge(sid_fr, sid_to, length=row.edge_cost, mode='transit')

        # If want to add both directions in this step, we can
        # by reversing the to/from node order on edge
        if bidirectional:
            G.add_edge(sid_to, sid_fr, length=row.edge_cost, mode='transit')

    return sid_lookup
Ejemplo n.º 4
0
Archivo: op.py Proyecto: pc2/CustoNN2
    def create_data_node(graph: nx.MultiDiGraph,
                         op_node: Node,
                         attrs: dict = None,
                         edge_attrs: dict = None):
        assert op_node is not None and op_node.kind == 'op'
        assert len(op_node.out_nodes()) == 0
        if attrs is None:
            attrs = {}

        data_node = unique_id(graph, op_node.id)
        defaul_attrs = dict(kind='data',
                            precision="FP32",
                            name=data_node,
                            value=None,
                            shape=None,
                            data_type=None,
                            infer=None)
        defaul_attrs.update(attrs)
        graph.add_node(data_node, **add_attrs_props(defaul_attrs))
        data_node = Node(graph, data_node)
        if edge_attrs is not None:
            graph.add_edges_from([(op_node.id, data_node.id, {
                'out': 0,
                **edge_attrs
            })])
        else:
            graph.add_edges_from([(op_node.id, data_node.id, {'out': 0})])
        return data_node
Ejemplo n.º 5
0
def get_graphs():
    g1 = MultiDiGraph()
    g1.name = 'Graph 1'
    g1.add_node('A', id='A', name='Node A', category=['biolink:NamedThing'])
    g1.add_node('B', id='B', name='Node B', category=['biolink:NamedThing'])
    g1.add_node('C', id='C', name='Node C', category=['biolink:NamedThing'])
    g1.add_edge('C', 'B', key='C-biolink:subclass_of-B', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf')
    g1.add_edge('B', 'A', key='B-biolink:subclass_of-A', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf', provided_by='Graph 1')

    g2 = MultiDiGraph()
    g2.name = 'Graph 2'
    g2.add_node('A', id='A', name='Node A', description='Node A in Graph 2', category=['biolink:NamedThing'])
    g2.add_node('B', id='B', name='Node B', description='Node B in Graph 2', category=['biolink:NamedThing'])
    g2.add_node('C', id='C', name='Node C', description='Node C in Graph 2', category=['biolink:NamedThing'])
    g2.add_node('D', id='D', name='Node D', description='Node D in Graph 2', category=['biolink:NamedThing'])
    g2.add_node('E', id='E', name='Node E', description='Node E in Graph 2', category=['biolink:NamedThing'])
    g2.add_edge('B', 'A', key='B-biolink:subclass_of-A', edge_label='biolink:subclass_of', relation='rdfs:subClassOf', provided_by='Graph 2')
    g2.add_edge('B', 'A', key='B-biolink:related_to-A', edge_label='biolink:related_to', relation='biolink:related_to')
    g2.add_edge('D', 'A', key='D-biolink:related_to-A', edge_label='biolink:related_to', relation='biolink:related_to')
    g2.add_edge('E', 'A', key='E-biolink:related_to-A', edge_label='biolink:related_to', relation='biolink:related_to')


    g3 = MultiDiGraph()
    g3.name = 'Graph 3'
    g3.add_edge('F', 'E', key='F-biolink:same_as-E', edge_label='biolink:same_as', relation='OWL:same_as')

    return [g1, g2, g3]
Ejemplo n.º 6
0
def add_all_nodes(g1: nx.MultiDiGraph, g2: nx.MultiDiGraph, preserve: bool = True) -> int:
    """
    Add all nodes from source graph (``g2``) to target graph (``g1``).

    Parameters
    ----------
    g1: networkx.MultiDiGraph
        Target graph
    g2: networkx.MultiDiGraph
        Source graph
    preserve: bool
        Whether or not to preserve conflicting properties

    Returns
    -------
    int
        Number of nodes merged during this operation

    """
    logging.info(f"Adding {g2.number_of_nodes()} nodes from {g2.name} to {g1.name}")
    merge_count = 0
    for n, data in g2.nodes(data=True):
        if n in g1.nodes():
            merge_node(g1, n, data, preserve)
            merge_count += 1
        else:
            g1.add_node(n, **data)
    return merge_count
def _add_entry_point_for_graph(graph: nx.MultiDiGraph, tag=True):
    entry_points = []

    name = graph.graph["name"]
    for node_id in graph.nodes:
        if graph.in_degree(node_id) == 0:  # 入口节点
            entry_points.append(node_id)

    if tag is True:
        graph.add_node("{}@entry".format(name),
                       label="ENTRY",
                       type="ENTRY",
                       expression="ENTRY")
    else:
        graph.add_node("entry",
                       label="ENTRY",
                       type="ENTRY",
                       expression="ENTRY")

    for entry_point in entry_points:
        if tag is True:
            graph.add_edge("{}@entry".format(name), entry_point)
        else:
            graph.add_edge("entry", entry_point)

    return graph
Ejemplo n.º 8
0
    def __init__(self, F, arg):
        """
        If arg is a list:
        Generate the core graph corresponding to the group generated by group_gens.
        If arg is a graph:
        Check for validity and do all folds.
        """
        assert is_FreeGroup(F), "F must be a free group"
        self.F = F
        self.F_rank = F.rank()
        self.letter_types = range(1, self.F_rank + 1)
        self.letters = list(range(-self.F_rank, 0)) + list(range(1, self.F_rank + 1))
        # -r, ..., -1, 1, ..., r

        if isinstance(arg, list):
            group_gens = arg
            assert all([gen in F for gen in group_gens]), "The generators must be elements of F."
            self.group_gens = group_gens

            G = MultiDiGraph()
            G.add_node((0,))  # the marked vertex (id)
            for i, gen in enumerate(self.group_gens):
                word = gen.Tietze()
                word_len = len(word)
                # new_nodes = [(i, j) for j in range(1, word_len)]
                # G.add_nodes_from(new_nodes)
                get_node = lambda j: (0,) if (j % word_len == 0) else (i, j)
                for j in range(word_len):
                    G.add_edge(get_node(j), get_node(j + 1), label=word[j])
                    G.add_edge(get_node(j + 1), get_node(j), label=-word[j])

        elif isinstance(arg, MultiDiGraph):
            # We are going to copy the graph, add reverse edges when needed,
            # and sort the edges.
            # The reason we sort the edges is to get a "canonical" version
            # of the object, so subgroup_gens would be the same in different
            # objects with the same graph.
            G = MultiDiGraph()
            G.add_nodes_from(arg.nodes())
            edges = arg.edges(data='label')
            G_edges = [e for e in edges]
            assert len(edges) == len(arg.edges()), "Every edge has to be labelled."
            for src, dst, letter in edges:
                assert letter in self.letters, \
                    f"The edge betwen {src} and {dst} has an invalid label"
                if (dst, src, -letter) not in G.edges(data='label'):
                    G_edges.append((dst, src, -letter))
            G.add_weighted_edges_from(sorted(G_edges), weight='label')

        else:
            raise ValueError("arg must be a list of words or a MultiDiGraph.")

        self.G = do_all_folds(G)

        # The subgraph of positive edges
        G_pos = MultiDiGraph()
        G_pos.add_edges_from([e for e in self.G.edges(data=True) if e[2]['label'] > 0])
        self.G_pos = G_pos

        self.subgroup_gens = tuple(sorted(self.get_subgroup()))
Ejemplo n.º 9
0
    def convert_nodes(
        cls,
        node_uris: List[URIRef],
        rdf_graph: RDFGraph,
        namespaces: Union[NamespaceManager, Dict[str, Union[str, Namespace,
                                                            URIRef]]] = None,
        strict: bool = False,
    ) -> MultiDiGraph:
        """Converts a set of RDF nodes into a set of LPG nodes (networkx).

        :param node_uris: a list of node URIs to be converted
        :param rdf_graph: the rdflib.graph.Graph containing the raw data
        :param namespaces: the collection of namespaces used to simplify URIs
        :param strict: boolean for Literal conversion (True = supported types only)
        :return: the networkx MultiDiGraph containing all collected node/edge data
        """
        if cls.initNs is None:
            if namespaces is None:
                namespaces = dict(rdf_graph.namespaces())
            cls.initNs = dict(namespaces)

        nx_graph = MultiDiGraph()

        # TODO pass list of nodes to this function
        # TODO add edges for these nodes
        nodes = cls.transform_nodes(rdf_graph,
                                    node_iris=node_uris,
                                    strict=strict)
        for node_iri, node_attrs in nodes.items():
            nx_graph.add_node(node_iri, **node_attrs)

        return nx_graph
Ejemplo n.º 10
0
def im_json_to_graph(im_json):
    """Return networkx graph from Kappy's influence map JSON.

    Parameters
    ----------
    im_json : dict
        A JSON dict which contains an influence map generated by Kappy.

    Returns
    -------
    graph : networkx.MultiDiGraph
        A graph representing the influence map.
    """
    # This is for kappy compatibility: as of 4.1.2, im_json is a string,
    # whereas before it was a json object
    if isinstance(im_json, str):
        im_json = json.loads(im_json)
    imap_data = im_json['influence map']['map']

    # Initialize the graph
    graph = MultiDiGraph()

    id_node_dict = {}
    # Add each node to the graph
    for node_dict in imap_data['nodes']:
        # There is always just one entry here with the node type e.g. "rule"
        # as key, and all the node data as the value
        node_type, node = list(node_dict.items())[0]
        # Add the node to the graph with its label and type
        attrs = {
            'fillcolor': '#b7d2ff' if node_type == 'rule' else '#cdffc9',
            'shape': 'box' if node_type == 'rule' else 'oval',
            'style': 'filled'
        }
        graph.add_node(node['label'], node_type=node_type, **attrs)
        # Save the key of the node to refer to it later
        new_key = '%s%s' % (node_type, node['id'])
        id_node_dict[new_key] = node['label']

    def add_edges(link_list, edge_sign):
        attrs = {
            'sign': edge_sign,
            'color': 'green' if edge_sign == 1 else 'red',
            'arrowhead': 'normal' if edge_sign == 1 else 'tee'
        }
        for link_dict in link_list:
            source = link_dict['source']
            for target_dict in link_dict['target map']:
                target = target_dict['target']
                src_id = '%s%s' % list(source.items())[0]
                tgt_id = '%s%s' % list(target.items())[0]
                graph.add_edge(id_node_dict[src_id], id_node_dict[tgt_id],
                               **attrs)

    # Add all the edges from the positive and negative influences
    add_edges(imap_data['wake-up map'], 1)
    add_edges(imap_data['inhibition map'], -1)

    return graph
Ejemplo n.º 11
0
def convert_grandalf_graph_to_networkx_graph(G):
    from networkx import MultiDiGraph
    nxg = MultiDiGraph()
    for v in G.V():
        nxg.add_node(v.data)
    for e in G.E():
        nxg.add_edge(e.v[0].data, e.v[1].data)
    return nxg
Ejemplo n.º 12
0
def convert_grandalf_graph_to_networkx_graph(G):
    from networkx import MultiDiGraph
    nxg = MultiDiGraph()
    for v in G.V():
        nxg.add_node(v.data)
    for e in G.E():
        nxg.add_edge(e.v[0].data, e.v[1].data)
    return nxg
Ejemplo n.º 13
0
def get_graphs():
    g1 = MultiDiGraph()
    g1.name = 'Graph 1'
    g1.add_node('HGNC:12345', id='HGNC:12345', name='Test Gene', category=['biolink:Gene'])
    g1.add_node('B', id='B', name='Node B', category=['biolink:NamedThing'])
    g1.add_node('C', id='C', name='Node C', category=['biolink:NamedThing'])
    g1.add_edge('C', 'B', key='C-biolink:subclass_of-B', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf')
    g1.add_edge('B', 'A', key='B-biolink:subclass_of-A', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf', provided_by='Graph 1')
    return [g1]
Ejemplo n.º 14
0
def FullConnection(group1: Group, group2: Group, graph: nx.MultiDiGraph):
    for node in group1.get_nodes():
        graph.add_node(node)
    for node in group2.get_nodes():
        graph.add_node(node)

    for i in group1.get_nodes():
        for j in group2.get_nodes():
            graph.add_edge(i, j)
def inserir_nodo_grafo(grafo: MultiDiGraph, chave: int, nodo: dict,
                       nodo_novo: dict):
    grafo.add_node(chave,\
                   posicao_labirinto=nodo_novo["posicao_labirinto"],\
                   chave=chave,\
                   acao=nodo_novo["acao"],\
                   custo_caminho=nodo_novo["custo_caminho"])

    if nodo is not None:
        grafo.add_edge(nodo["chave"], chave)
Ejemplo n.º 16
0
def test_content_hash() -> None:
    # the order of properties should not matter for the content hash
    g = MultiDiGraph()
    g.add_node("1", reported={"a": {"a": 1, "c": 2, "b": 3}, "c": 2, "b": 3, "d": "foo", "z": True, "kind": "a"})
    g.add_node("2", reported={"z": True, "c": 2, "b": 3, "a": {"b": 3, "c": 2, "a": 1}, "d": "foo", "kind": "a"})

    access = GraphAccess(g)
    sha1 = node(access, "1")["hash"]  # type: ignore
    sha2 = node(access, "2")["hash"]  # type: ignore
    assert sha1 == sha2
def inserir_nodo_grafo(grafo: MultiDiGraph, chave: int, nodo: dict,
                       nodo_novo: dict):
    grafo.add_node(chave,\
                   A=nodo_novo["A"],\
                   B=nodo_novo["B"],\
                   C=nodo_novo["C"],\
                   chave=chave,\
                   acao=nodo_novo["acao"],\
                   custo_caminho=nodo_novo["custo_caminho"])

    if nodo is not None:
        grafo.add_edge(nodo["chave"], chave)
Ejemplo n.º 18
0
    def convert(
        cls,
        rdf_graph: RDFGraph,
        namespaces: Union[NamespaceManager, Dict[str, Union[str, Namespace,
                                                            URIRef]]] = None,
        strict: bool = False,
    ) -> MultiDiGraph:
        """The main method for converting an RDF graph to a networkx representation.

        :param rdf_graph: the rdflib.graph.Graph containing the raw data
        :param namespaces: the collection of namespaces used to simplify URIs
        :param strict: boolean for Literal conversion (True = supported types only)
        :return: the networkx MultiDiGraph containing all collected node/edge data
        """
        if cls.initNs is None:
            if namespaces is None:
                namespaces = rdf_graph.namespaces
            cls.initNs = dict(namespaces)
            assert (
                "base" in cls.initNs
            ), "For conversion, namespaces must include a base namespace."

        nx_graph = MultiDiGraph()

        nodes = cls.transform_nodes(rdf_graph, strict=strict)
        edges = cls.transform_edges(rdf_graph, strict=strict)

        for node_iri, node_attrs in nodes.items():
            nx_graph.add_node(node_iri, **node_attrs)

        for edge_iri, edge_data in edges.items():
            try:
                edge_source = edge_data.get("source")
                edge_target = edge_data.get("target")
                edge_attrs = edge_data.get("attrs")
            except AttributeError as err:
                logger.warning("Unable to get expected edge attributes.")
                raise err

            if edge_source in nx_graph.nodes and edge_target in nx_graph.nodes:
                edge_attrs["iri"] = edge_iri
                nx_graph.add_edge(edge_source, edge_target, **edge_attrs)
            else:
                if edge_source not in nx_graph.nodes:
                    logger.info(
                        f"Edge source '{edge_source}' missing in graph. Skipping..."
                    )
                elif edge_target not in nx_graph.nodes:
                    logger.info(
                        f"Edge target '{edge_target}' missing in graph. Skipping..."
                    )

        return nx_graph
Ejemplo n.º 19
0
def convert_add_to_scaleshift(graph: nx.MultiDiGraph):
    for n in list(graph.nodes()):
        node = Node(graph, n)
        if node.has('op') and (node.op == 'BiasAdd' or node.op
                               == 'Add') and len(node.in_nodes()) == 2:
            tensor_id, value_id = get_tensor_id(node), get_value_id(node)
            if tensor_id is not None and value_id is not None and node.soft_get(
                    'can_be_scaleshift') is not False:
                node['type'] = 'ScaleShift'
                node['op'] = 'ScaleShift'
                node.in_node(value_id).value = np.squeeze(
                    node.in_node(value_id).value)
                node.in_node(value_id).shape = node.in_node(
                    value_id).value.shape

                # if the node was created with eltwise then it has attribute 'operation' which should be removed from
                # the IR
                if node.has('operation'):
                    del graph.node[n]['operation']

                bias_data = node.in_node(value_id)
                graph[bias_data.node][node.node][0]['in'] = 2
                graph[bias_data.node][node.node][0]['bin'] = 'biases'

                input_data = node.in_node(tensor_id)
                graph[input_data.node][node.node][0]['in'] = 0

                update_ie_fields(graph.node[node.id])

                weights_id = unique_id(graph, 'weights_')
                graph.add_node(
                    weights_id,
                    **add_attrs_props(
                        dict(kind='data',
                             precision="FP32",
                             name=weights_id,
                             value=None,
                             shape=None,
                             data_type=None,
                             infer=None)))
                wnode = Node(graph, weights_id)

                wnode['value'] = np.full_like(bias_data.value,
                                              1,
                                              dtype=np.float32)
                wnode['shape'] = np.array(wnode['value'].shape)

                graph.add_edges_from([
                    (weights_id, node.node, {
                        'in': 1,
                        'bin': 'weights'
                    }),
                ])
Ejemplo n.º 20
0
def im_json_to_graph(im_json):
    """Return networkx graph from Kappy's influence map JSON.

    Parameters
    ----------
    im_json : dict
        A JSON dict which contains an influence map generated by Kappy.

    Returns
    -------
    graph : networkx.MultiDiGraph
        A graph representing the influence map.
    """
    imap_data = im_json['influence map']['map']

    # Initialize the graph
    graph = MultiDiGraph()

    id_node_dict = {}
    # Add each node to the graph
    for node_dict in imap_data['nodes']:
        # There is always just one entry here with the node type e.g. "rule"
        # as key, and all the node data as the value
        node_type, node = list(node_dict.items())[0]
        # Add the node to the graph with its label and type
        attrs = {'fillcolor': '#b7d2ff' if node_type == 'rule' else '#cdffc9',
                 'shape': 'box' if node_type == 'rule' else 'oval',
                 'style': 'filled'}
        graph.add_node(node['label'], node_type=node_type, **attrs)
        # Save the key of the node to refer to it later
        new_key = '%s%s' % (node_type, node['id'])
        id_node_dict[new_key] = node['label']

    def add_edges(link_list, edge_sign):
        attrs = {'sign': edge_sign,
                 'color': 'green' if edge_sign == 1 else 'red',
                 'arrowhead': 'normal' if edge_sign == 1 else 'tee'}
        for link_dict in link_list:
            source = link_dict['source']
            for target_dict in link_dict['target map']:
                target = target_dict['target']
                src_id = '%s%s' % list(source.items())[0]
                tgt_id = '%s%s' % list(target.items())[0]
                graph.add_edge(id_node_dict[src_id], id_node_dict[tgt_id],
                               **attrs)

    # Add all the edges from the positive and negative influences
    add_edges(imap_data['wake-up map'], 1)
    add_edges(imap_data['inhibition map'], -1)

    return graph
Ejemplo n.º 21
0
def change_edges(
    graph: MultiDiGraph,
    spec: Dict[str, Any],
    verbose: bool = True,
) -> MultiDiGraph:
    """Returns a graph with relabeled edges
    by specified edge labels mapping.

    Parameters
    ----------
    graph : MultiDiGraph
        Initial graph.

    spec: Dict
        Edge labels mapping.

    verbose : bool
        If true, a progress bar will be displayed.

    Examples
    --------
    >>> import cfpq_data
    >>> g = cfpq_data.labeled_cycle_graph(42, edge_label="a", verbose=False)
    >>> new_g = cfpq_data.change_edges(g, {"a": "b"}, verbose=False)
    >>> new_g.number_of_nodes()
    42
    >>> new_g.number_of_edges()
    42

    Returns
    -------
    g : MultiDiGraph
        A graph with changed edges.
    """
    g = MultiDiGraph()

    for node, node_labels in graph.nodes(data=True):
        g.add_node(node, **node_labels)

    for u, v, edge_labels in tqdm(graph.edges(data=True),
                                  disable=not verbose,
                                  desc="Generation..."):
        changed_edge_labels = dict()
        for key, value in edge_labels.items():
            if str(value) in spec.keys():
                changed_edge_labels[key] = spec[str(value)]
            else:
                changed_edge_labels[key] = value
        g.add_edge(u, v, **changed_edge_labels)

    return g
Ejemplo n.º 22
0
def InjectConnection(group1: Group, group2: Group, graph: nx.MultiDiGraph):
    neuron_group_1 = group1.get_nodes()
    neuron_group_2 = group2.get_nodes()

    if len(neuron_group_1) != len(neuron_group_2):
        raise Exception('Size of group do not match for inject connection')

    for node in neuron_group_1:
        graph.add_node(node)
    for node in neuron_group_2:
        graph.add_node(node)

    for i in range(len(neuron_group_1)):
        graph.add_edge(neuron_group_1[i], neuron_group_2[i])
Ejemplo n.º 23
0
def copy_node(src_node: Node,
              new_attrs: dict = None,
              dst_graph: nx.MultiDiGraph = None):
    ''' Copies node with all attributes (optionally updated) within the same graph or to different graph.'''
    if new_attrs is None:
        new_attrs = {}
    if dst_graph is None:
        dst_graph = src_node.graph

    attrs = deepcopy(src_node.attrs())
    attrs.update(new_attrs)
    new_id = unique_id(dst_graph)
    dst_graph.add_node(new_id, attrs)
    return Node(dst_graph, new_id)
Ejemplo n.º 24
0
def filter_edges(
    graph: MultiDiGraph,
    labels: Iterable[str],
    verbose: bool = True,
) -> MultiDiGraph:
    """Returns a graph
    with filtered edges.

    Parameters
    ----------
    graph : MultiDiGraph
        Initial graph.

    labels : Optional[Iterable[str]]
        Graph edge labels to be preserved.

    verbose : bool
        If true, a progress bar will be displayed.

    Examples
    --------
    >>> import cfpq_data
    >>> g = cfpq_data.labeled_two_cycles_graph(42, 29, edge_labels=("a", "b"), verbose=False)
    >>> new_g = cfpq_data.filter_edges(g, ["a"], verbose=False)
    >>> new_g.number_of_nodes()
    72
    >>> new_g.number_of_edges()
    43

    Returns
    -------
    g : MultiDiGraph
        Graph with filtered edges.
    """
    g = MultiDiGraph()

    for node, node_labels in graph.nodes(data=True):
        g.add_node(node, **node_labels)

    for u, v, edge_labels in tqdm(graph.edges(data=True),
                                  disable=not verbose,
                                  desc="Generation..."):
        filtered_edge_labels = dict()
        for key, value in edge_labels.items():
            if str(value) in labels:
                filtered_edge_labels[key] = value
        if filtered_edge_labels != dict():
            g.add_edge(u, v, **filtered_edge_labels)

    return g
Ejemplo n.º 25
0
def _build_cargo_outdated_graph():
    """
    :returns: A graph representation of `cargo updated` output
    :rtype: MultiDiGraph
    """
    command = ["cargo", "outdated"]
    proc = subprocess.Popen(command, stdout=subprocess.PIPE)

    stream = proc.stdout

    stream.readline()
    stream.readline()

    dependency_graph = MultiDiGraph()
    while True:
        line = stream.readline()

        if line == b"":
            break

        line_str = line.decode("utf-8")
        cargo_outdated_match = CARGO_OUTDATED_RE.match(line_str)

        dependencies = cargo_outdated_match.group("name")
        dependencies_split = dependencies.split("->")
        dependency = dependencies_split.pop(-1)
        pulled_in_by = None if dependencies_split == [] else dependencies_split[
            0]
        if pulled_in_by is None:
            dependency_graph.add_node(
                dependency,
                platform=cargo_outdated_match.group("platform"),
                project=cargo_outdated_match.group("project"),
                compat=cargo_outdated_match.group("compat"),
                latest=cargo_outdated_match.group("latest"),
                kind=cargo_outdated_match.group("kind"),
            )
        else:
            dependency_graph.add_edge(
                pulled_in_by,
                dependency,
                platform=cargo_outdated_match.group("platform"),
                project=cargo_outdated_match.group("project"),
                compat=cargo_outdated_match.group("compat"),
                latest=cargo_outdated_match.group("latest"),
                kind=cargo_outdated_match.group("kind"),
            )

    return dependency_graph
Ejemplo n.º 26
0
Archivo: op.py Proyecto: pc2/CustoNN2
 def create_input_data_node(graph: nx.MultiDiGraph,
                            name: str,
                            value: np.array,
                            attrs: dict = {}):
     data_node = unique_id(graph, name)
     defaul_attrs = dict(kind='data',
                         precision="FP32",
                         name=data_node,
                         value=np.array(value),
                         shape=value.shape,
                         data_type=None,
                         infer=None)
     defaul_attrs.update(attrs)
     graph.add_node(data_node, **add_attrs_props(defaul_attrs))
     return Node(graph, data_node)
def nebula2networkx(client: GraphClient, nebula_space: str,
                    graph: nx.MultiDiGraph, vertex_list, edge_types):
    do_simple_execute(client, 'use ' + nebula_space)
    yield_statement = ",".join(
        [edge_type + "._dst" for edge_type in edge_types])
    seen_vertex = set()
    queue_vertex = []
    all_edges = {}
    for v in vertex_list:
        queue_vertex.insert(0, v)
    while len(queue_vertex):
        vertex = queue_vertex.pop()
        seen_vertex.add(vertex)
        get_edge_go_statement = "GO FROM {} OVER {} YIELD ".format(
            vertex, ','.join(edge_types)) + yield_statement
        edges_resp = client.execute_query(get_edge_go_statement)
        edges = [[] for _ in edge_types]
        if edges_resp.rows is not None:
            for row in edges_resp.rows:
                for ids, col in enumerate(row.columns):
                    if (col.getType()
                            == ttypes.ColumnValue.ID) and col.get_id() != 0:
                        edges[ids].append(col.get_id())
                        if col.get_id() not in seen_vertex:
                            seen_vertex.add(col.get_id())
                            queue_vertex.insert(0, col.get_id())
        all_edges[vertex] = edges
        # build networkX graph Node
        vertex_info_resp = fetch_info(client, "* " + str(vertex))
        vertex_info = handle_fetch_resp(vertex_info_resp)
        graph.add_node(vertex,
                       **vertex_info[0] if len(vertex_info) > 0 else {})

    # build networkX graph Edge
    for vertex_src, edges in all_edges.items():
        for edge_type_ids, vertexs_dst in enumerate(edges):
            if len(vertexs_dst) != 0:
                edge_info_fetch_statement = edge_types[
                    edge_type_ids] + ' ' + ','.join([
                        str(vertex_src) + "->" + str(dst)
                        for dst in vertexs_dst
                    ])

                edges_info = handle_fetch_resp(
                    fetch_info(client, edge_info_fetch_statement))
                graph.add_edges_from([(vertex_src, vertexs_dst[i],
                                       edges_info[i])
                                      for i in range(len(edges_info))])
Ejemplo n.º 28
0
def simplify_state(state: SDFGState,
                   remove_views: bool = False) -> MultiDiGraph:
    """
    Returns a networkx MultiDiGraph object that contains all the access nodes
    and corresponding edges of an SDFG state. The removed code nodes and map
    scopes are replaced by edges that connect their ancestor and succesor access
    nodes.
    :param state: The input SDFG state.
    :return: The MultiDiGraph object.
    """

    sdfg = state.parent

    # Copy the whole state
    G = MultiDiGraph()
    for n in state.nodes():
        G.add_node(n)
    for n in state.nodes():
        for e in state.all_edges(n):
            G.add_edge(e.src, e.dst)
    # Collapse all mappings and their scopes into one node
    scope_children = state.scope_children()
    for n in scope_children[None]:
        if isinstance(n, nodes.EntryNode):
            G.add_edges_from([(n, x)
                              for (y, x) in G.out_edges(state.exit_node(n))])
            G.remove_nodes_from(scope_children[n])
    # Remove all nodes that are not AccessNodes or have incoming
    # wcr edges and connect their predecessors and successors
    for n in state.nodes():
        if n in G.nodes():
            if (not isinstance(n, nodes.AccessNode) or
                (remove_views and isinstance(sdfg.arrays[n.data], data.View))):
                for p in G.predecessors(n):
                    for c in G.successors(n):
                        G.add_edge(p, c)
                G.remove_node(n)
            else:
                for e in state.all_edges(n):
                    if e.data.wcr is not None:
                        for p in G.predecessors(n):
                            for s in G.successors(n):
                                G.add_edge(p, s)
                        G.remove_node(n)
                        break

    return G
Ejemplo n.º 29
0
    def replace_pattern(graph: nx.MultiDiGraph, match: dict):
        node = match['op']
        shapes = [in_node.shape for _, in_node in node.in_nodes().items()]
        out_shape = node.out_node().shape
        tname = node.name + '/Broadcast/'
        tile = Tile(graph, dict(name=tname))

        # Working with scalar values
        for i, shape in enumerate(shapes):
            if len(shape) == 0:
                shapes[i] = np.ones(len(out_shape), dtype=np.int64)
                node.in_node(i).shape = shapes[i].copy()
                if node.in_node(i).value is not None:
                    node.in_node(i).value = np.reshape(node.in_node(i).value, newshape=shapes[i])

        if not all([len(shape) == len(out_shape) for shape in shapes]):
            log.warning("Cannot apply broadcast for Eltwise layer {} "
                        "because not all input shapes {} have the same number of elements "
                        "as output shape {}.".format(node.soft_get('name'),
                                                     shapes,
                                                     out_shape
                                                     )
                        )
            return

        input_idx = 0
        for port, old_input in node.in_nodes().items():
            # old_input = node.in_node(input_idx)
            input = old_input
            for i in range(len(out_shape)):
                if shapes[input_idx][i] == 1 and out_shape[i] > 1:
                    new_op = tile.create_node([input], dict(axis=i, tiles=out_shape[i]))
                    # add a data node following a new operation node
                    data_id = unique_id(graph, node.name)
                    graph.add_node(data_id, kind='data', shape=None, value=None)
                    new_data = Node(graph, data_id)
                    graph.add_edge(new_op.id, new_data.id, **{'out': 0})
                    new_op.infer(new_op)
                    input = new_data
            if input != old_input:
                # create a new edge from new data node after Tile application to the eltwise
                # and copy all edge attributes from the old edge
                # [0] is not what we really want
                graph.add_edge(input.id, node.id, **graph[old_input.id][node.id][0])
                graph.remove_edge(old_input.id, node.id)
            input_idx += 1
Ejemplo n.º 30
0
def test_access_node() -> None:
    g = MultiDiGraph()
    g.add_node("1", reported=to_json(FooTuple(a="1")))
    access: GraphAccess = GraphAccess(g)
    elem: Json = node(access, "1")  # type: ignore
    assert elem["hash"] == "153c1a5c002f6213a95383f33b63aa18b8ed6939f57418fb0f27312576f0cea4"
    assert elem["reported"] == {
        "a": "1",
        "b": 0,
        "c": [],
        "d": "foo",
        "e": {"a": 12, "b": 32},
        "f": "2021-03-29",
        "g": 1.234567,
        "kind": "foo",
    }
    assert access.node("2") is None
Ejemplo n.º 31
0
def to_networkx(alchemist):
    pan_graph = MultiDiGraph()

    pan_clusters = alchemist.session.query(Cluster).all()
    for cluster in pan_clusters:
        pan_graph.add_node(cluster.ClusterID, Spread=cluster.Spread,
                           CentroidID=cluster.CentroidID,
                           CentroidSeq=cluster.CentroidSeq.decode("utf-8"))

    for cluster in pan_clusters:
        for identity_edge in cluster.NeighborhoodEdges:
            pan_graph.add_edge(identity_edge.Source, identity_edge.Target,
                               DBSeparation=identity_edge.DBSeparation,
                               CentroidDistance=identity_edge.CentroidDistance,
                               MinDistance=identity_edge.MinDistance)

    return pan_graph
Ejemplo n.º 32
0
def nodes_to_integers(graph: MultiDiGraph,
                      verbose: bool = True) -> MultiDiGraph:
    """Returns a graph with
    nodes converted to integers.

    Parameters
    ----------
    graph : MultiDiGraph
        Initial graph.

    verbose : bool
        If true, a progress bar will be displayed.

    Examples
    --------
    >>> import cfpq_data
    >>> g = cfpq_data.graph_from_text("29 A 42", verbose=False)
    >>> new_g = cfpq_data.nodes_to_integers(g, verbose=False)
    >>> g.edges(data=True)
    OutMultiEdgeDataView([('29', '42', {'label': 'A'})])
    >>> new_g.edges(data=True)
    OutMultiEdgeDataView([(0, 1, {'label': 'A'})])

    Returns
    -------
    g : MultiDiGraph
        A graph whose vertices are integers.
    """
    node2int = dict()

    for node in graph.nodes():
        if node not in node2int.keys():
            node2int[node] = len(node2int)

    g = MultiDiGraph()

    for node, node_labels in graph.nodes(data=True):
        g.add_node(node2int[node], **node_labels)

    for u, v, edge_labels in tqdm(graph.edges(data=True),
                                  disable=not verbose,
                                  desc="Generation..."):
        g.add_edge(node2int[u], node2int[v], **edge_labels)

    return g
Ejemplo n.º 33
0
Archivo: op.py Proyecto: pc2/CustoNN2
    def _create_data_node(graph: nx.MultiDiGraph,
                          name: str,
                          attrs: dict = None):
        if attrs is None:
            attrs = {}

        data_node = unique_id(graph, name)
        defaul_attrs = dict(kind='data',
                            precision="FP32",
                            name=data_node,
                            value=None,
                            shape=None,
                            data_type=None,
                            infer=None)
        defaul_attrs.update(attrs)
        graph.add_node(data_node, **add_attrs_props(defaul_attrs))
        data_node = Node(graph, data_node)
        return data_node
Ejemplo n.º 34
0
def convert_multi_input_conv(graph: nx.MultiDiGraph):
    for node in list(graph.nodes()):
        node = Node(graph, node)
        if node.kind == 'op' and node.op == 'ConvND':
            node.op = 'Conv2D'
            if node.bias_term == True:
                num_inputs = len(node.in_nodes()) - 2
                w_node = node.in_node(len(node.in_nodes()) - 2)
                b_node = node.in_node(len(node.in_nodes()) - 1)
            else:
                num_inputs = len(node.in_nodes()) - 1
                w_node = node.in_node(len(node.in_nodes()) - 1)

            for i in range(1, num_inputs):
                in_i = node.in_node(i)
                out_i = node.out_node(i)
                conv_id = unique_id(graph, node.id + '__')
                graph.add_node(conv_id, **copy.deepcopy(node.get_attrs()))
                new_conv = Node(graph, conv_id)
                new_conv.name = conv_id

                graph.remove_edge(in_i.id, node.id)
                graph.remove_edge(node.id, out_i.id)
                graph.add_edges_from([
                    (w_node.id, conv_id, {
                        'in': 1,
                        'bin': 'weights'
                    }),
                ])

                if node.bias_term == True:
                    graph.add_edges_from([
                        (b_node.id, conv_id, {
                            'in': 2,
                            'bin': 'biases'
                        }),
                    ])

                graph.add_edges_from([
                    (in_i.id, conv_id, {
                        'in': 0
                    }),
                ])
                graph.add_edge(conv_id, out_i.id, **{'out': 0})
Ejemplo n.º 35
0
def selector(request):
    request.session['viewer'] = request.path_info
    if request.method == 'POST':
        form = Neo4jQueryForm(request.POST)
        if form.is_valid():
            host = request.session.get('host', None)
            if host:
                gdb = GraphDatabase(host)
                node = gdb.node[form.cleaned_data['node']]
                depth = form.cleaned_data['depth']
                new_nodes = []
                graph = MultiDiGraph()
                node_id = str(node.id)
                graph.add_node(node_id, **node.properties)
                new_nodes.append(node_id)
                for i in range(depth):
                    added_nodes = []
                    for node_id in new_nodes:
                        node = gdb.node[node_id]
                        for relation in node.relationships.all():
                            start = relation.start
                            end = relation.end
                            if start not in new_nodes:
                                start_id = str(start.id)
                                graph.add_node(start_id, **start.properties)
                                added_nodes.append(start_id)
                            if end not in new_nodes:
                                end_id = str(end.id)
                                graph.add_node(end_id, **end.properties)
                                added_nodes.append(end_id)
                            graph.add_edge(start_id, end_id, **relation.properties)
                    new_nodes += added_nodes
                interactor = NetworkxInteractor(graph)
                request.session['interactor'] = interactor
                request.session['layout'] = None
                response_dictionary = set_response_dictionary(request)
            else:
                form = Neo4jConnectionForm()
                return render_to_response('neo4j/index.html', {
                                            'form': form,
                                            })
            return render_to_response('neo4j/explorer.html',
                                        response_dictionary)
    else:
        interactor = request.session.get('interactor')
        return render_to_response('neo4j/explorer.html',
                                set_response_dictionary(request))
class AbstractGame:
    def __init__(self, player1, player2, initial_gamestate):
        self.game_graph = MultiDiGraph()
        self.player1 = player1
        self.player2 = player2
        self.initial_gamestate = self.canonicalize(initial_gamestate)

    def canonicalize(self, gamestate):
        raise NotImplementedError

    def possible_moves(self, gamestate, player):
        raise NotImplementedError

    def other_player(self, player):
        if player == self.player1:
            return self.player2
        elif player == self.player2:
            return self.player1
        else:
            raise ValueError("%s is neither %s nor %s" % (player, self.player1, self.player2))

    def is_winning_position(self, gamestate, player):
        raise NotImplementedError

    def is_losing_position(self, gamestate, player):
        raise NotImplementedError

    def good_moves(self, gamestate, player, lookahead_depth):

        if lookahead_depth == 0:
            yield from self.possible_moves(gamestate, player)
        else:
            lookahead_depth -= 1
            sign = {self.player1: 1, self.player2: -1}

            def is_game_over(gamestate):
                return evaluate_gamestate(gamestate, 0) != 0

            def evaluate_gamestate(gamestate, depth):
                """
                Returns 1 if this gamestate favors the first player, -1 if it favors the second player,
                and 0 if it is neutral.
                """
                if self.is_winning_position(gamestate, self.player1):
                    return 1000 - depth
                elif self.is_winning_position(gamestate, self.player2):
                    return -1000 + depth
                else:
                    return 0

            def negamax(gamestate, current_depth, current_player):
                if is_game_over(gamestate) or current_depth > lookahead_depth:
                    return sign[current_player] * evaluate_gamestate(gamestate, current_depth)
                max_score = -1000
                for possible_gamestate in self.possible_moves(gamestate, current_player):
                    max_score = max(max_score,
                                    -negamax(possible_gamestate, current_depth + 1, self.other_player(current_player)))
                return max_score

            moves_and_lookaheads = {move: -negamax(move, 1, self.other_player(player)) for move in self.possible_moves(gamestate, player)}

            if len(moves_and_lookaheads) == 0:
                return
            best_lookahead = max(moves_and_lookaheads.values())
            good_moves = {self.canonicalize(move) for move, lookahead in moves_and_lookaheads.items() if lookahead == best_lookahead}
            for move in good_moves:
                yield move

    def play(self, lookahead=0):
        new_moves = set()  # A set of node, player tuples containing all new moves that can be played in this graph.

        def play_new_moves():
            new_new_moves = set()  # The moves that will be new after current new moves have been played.
            for gamestate, player in new_moves:
                next_player = self.other_player(player)
                for move_result in self.good_moves(gamestate, player, lookahead):
                    move_result = self.canonicalize(move_result)
                    if move_result not in self.game_graph:  # Add the result to the graph if it's not there.
                        self.game_graph.add_node(move_result, players=set())
                    self.game_graph.add_edge(gamestate, move_result, key=player)  # Add this move to the graph
                    if next_player not in self.game_graph.node[move_result]['players']:
                        self.game_graph.node[move_result]['players'].add(next_player)
                        new_new_moves.add((move_result, next_player))
            new_moves.clear()
            new_moves.update(new_new_moves)

        new_moves.add((self.initial_gamestate, self.player1))
        self.game_graph.add_node(self.initial_gamestate, players=set())

        while len(new_moves) != 0:
            play_new_moves()
        self.game_graph
Ejemplo n.º 37
0
def simu_abstract(ts, simu_type):
    """Create a bi/dual-simulation abstraction for a Finite Transition System.

    @param ts: input finite transition system, the one you want to get
                    its bi/dual-simulation abstraction.
    @type ts: L{FTS}
    @param simu_type: string 'bi'/'dual', flag used to switch b.w.
                      bisimulation algorithm and dual-simulation algorithm.
    @return: the bi/dual simulation, and the corresponding partition.
    @rtype: L{FTS}, C{dict}


    References
    ==========

    1. Wagenmaker, A. J.; Ozay, N.
       "A Bisimulation-like Algorithm for Abstracting Control Systems."
       54th Annual Allerton Conference on CCC 2016
    """
    # create MultiDiGraph instance from the input FTS
    G = MultiDiGraph(ts)
    # build coarsest partition
    S0 = dict()
    Part = MultiDiGraph()  # a graph associated with the new partition
    n_cells = 0
    hash_ap = dict()  # map ap to cells in Part
    for node in G:
        ap = repr(G.node[node]['ap'])
        if ap not in S0:
            S0[ap] = set()
            hash_ap[ap] = set()
            Part.add_node(n_cells, ap=ap, cov=S0[ap])  # hash table S0--->G
            n_cells += 1
        S0[ap].add(node)

    sol = []
    for ap in S0:
        sol.append(S0[ap])

    IJ = np.ones([n_cells, n_cells])
    transitions = np.zeros([n_cells, n_cells])
    while np.sum(IJ) > 0:
        # get i,j from IJ matrix, i--->j
        ind = np.nonzero(IJ)
        i = ind[1][0]
        j = ind[0][0]
        IJ[j, i] = 0
        si = sol[i]
        sj = sol[j]
        pre_j = _pre(G, sj)
        if si.issubset(pre_j):
            transitions[j, i] = 1
        else:
            isect = si.intersection(pre_j)
            if isect == set():
                continue
            else:
                # check if the isect has existed
                check_isect = False
                if simu_type == 'dual':
                    assert len(sol) == n_cells
                    for k in range(n_cells):
                        if sol[k] == isect:
                            check_isect = True
                            break
                if not check_isect:
                    # assume that i != j
                    sol.append(isect)
                    # update transition matrix
                    transitions = np.pad(transitions, (0, 1), 'constant')
                    transitions[n_cells, :] = 0
                    transitions[:, n_cells] = transitions[:, i]
                    transitions[j, n_cells] = 1
                    if simu_type == 'bi':
                        sol[i] = sol[i].difference(sol[n_cells])
                        transitions[i, :] = 0
                        if i == j:
                            transitions[j, n_cells] = 0
                    # update IJ matrix
                    IJ = np.pad(IJ, (0, 1), 'constant', constant_values=1)
                    if simu_type == 'bi':
                        IJ[i, :] = 1
                        IJ[:, i] = 1
                    n_cells += 1
                else:
                    transitions[j, k] = 1
        IJ = ((IJ - transitions) > 0).astype(int)
    [ts_simu, part_hash] = _output_fts(ts, transitions, sol)
    return ts_simu, part_hash
Ejemplo n.º 38
0
class Grafatality(object):
    def __init__(self, filename='graph.json'):
        self.filename=filename
        self.graph = MultiDiGraph()
        self.typed_nodes = {}
        self.typed_nodes[None] = []
        try:
            self.load_file(filename)
        except IOError as e:
            print 'file "%s" does not exist, creating' % filename
        
    def handle(self, line):
        obj = ujson.loads(line)
        action = obj['action']
        if action == 'add_node':
            self.load_node(obj)
        elif action == 'add_edge':
            self.load_edge(obj)
        elif action == 'remove_edge':
            self.load_remove_edge(obj)

    def load_file(self, filename):
        f = open(filename)
        for line in f:
            self.handle(line)
        f.close()

    def listify_typed_node(self, node):
        if type(node) == list:
            node = tuple(node)
            if len(node) == 2:
                node = tuple(node)
        return node

    def load_edge(self, obj):
        data = obj.get('data', None)
        key =  obj.get('key', None)
        src_node = obj['src_node']
        dst_node = obj['dst_node']
        src_node = self.listify_typed_node(src_node)
        dst_node = self.listify_typed_node(dst_node)
        if data:
            self.graph.add_edge(src_node, dst_node, key=key, **data)
        else:
            self.graph.add_edge(src_node, dst_node, key=key)
    
    def load_remove_edge(self, obj):
        key = obj.get('key', None)
        self.graph.remove_edge(obj['src_node'], obj['dst_node'], key=key)
        
    def load_node(self, obj):
        data = obj.get('data', None)
        node = obj['node']
        node_type = None
        node = self.listify_typed_node(node)
        if data:
            self.graph.add_node(node, node_type=node_type, **data)
        else:
            self.graph.add_node(node, node_type=node_type)
    
    def add_node(self, node, node_type=None, **attr):
        if node_type:
            if not node_type in self.typed_nodes:
                self.typed_nodes[node_type] = []
            node = (node, node_type)
            self.typed_nodes[node_type].append(node)
        else:
            self.typed_nodes[None].append(node)

        data = {"action": "add_node",
                "node": node}

        if len(attr):
            data['data'] = attr
        self.append_log(data)

        return self.graph.add_node(node, **attr)

    def nodes_of_type(self, node_type=None, full=True):
        if full:
            return self.typed_nodes[node_type]
        else:
            return self.typed_nodes[node_type][0]

    def add_edge(self, src_node, dst_node, key=None, **attr):
        
        data = {"action": "add_edge",
                "src_node": src_node,
                "dst_node": dst_node}
        if len(attr):
            data['data'] = attr
        if key:
            data['key'] = key
        self.append_log(data)
        
        return self.graph.add_edge(src_node, dst_node, key=key, **attr)

    def remove_edge(self, src_node, dst_node, key):
        data = {"action": "remove_edge",
                "src_node": src_node,
                "dst_node": dst_node,
                "key": key}
        self.append_log(data)
        
        return self.graph.remove_edge(src_node, dst_node, key)

    def append_log(self,data):
        """
        This persists the data and while it may be true that 
        the logfile could be kept open indefinitely, for some reason
        this causes dataloss when running multiprocess files.
        """
        self.log = open(self.filename, 'a')
        self.log.write(ujson.encode(data))
        self.log.write("\n")
        self.log.close()


    def shutdown(self):
        self.log.close()