Esempio n. 1
0
def get_connection_multigraph_weighted(name2dp, connections):
    G = MultiDiGraph()
    for c in connections:
        dp1 = c.dp1
        dp2 = c.dp2
        if not G.has_edge(dp1, dp2):
            already = []
            G.add_edge(dp1, dp2)
        else:
            already = G.edge[dp1][dp2]['spaces']
        R = name2dp[c.dp1].get_rtype(c.s1)
        already.append(R)
        G.edge[dp1][dp2]['spaces'] = already

#     cycles = list(simple_cycles(G))
#     for cycle in cycles:
#         cycle = list(cycle)
#         cycle = cycle + [cycle[0]]
#         
#         for i in range(len(cycle) - 1):
#             # XXX
#             _val = G.edge[cycle[i]][cycle[i + 1]]['spaces']
#             # print('%s -> %s -> %s' % (cycle[i], val, cycle[i + 1]))

    return G
    
Esempio n. 2
0
def get_connection_multigraph(connections):
    G = MultiDiGraph()
    for c in connections:
        dp1 = c.dp1
        dp2 = c.dp2
        G.add_edge(dp1, dp2, s1=c.s1)
    return G
Esempio n. 3
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
def get_meausurements_graph(po):
    G = MultiDiGraph()
    for name, sr in iterate_measurements_relations((), po):
        a = sr.a
        b = sr.b
        attr_dict = dict(sr=sr)
        G.add_edge(a, b, attr_dict=attr_dict)
    return G
def add_edge(mygraph: MultiDiGraph, edge: str, origincolor: str,
             colormap: dict) -> MultiDiGraph:
    edge = reduce(str.removesuffix, [" bags", " bag"], edge)
    origincolornum = colormap.get(origincolor)
    num, tcolor = edge.split(" ", 1)
    for _ in range(int(num)):
        mygraph.add_edge(origincolornum, colormap.get(tcolor))
    return mygraph
Esempio n. 6
0
 def get_edges_data_from(graph: nx.MultiDiGraph, node_from_id: int, route_id: int) -> list:
     edges = set()
     for node_to_id in graph.neighbors(node_from_id):
         for edge in graph.get_edge_data(node_from_id, node_to_id).values():
             if edge['route_id'] == route_id:
                 edges.add(
                     (int(edge['route_id']), node_from_id, node_to_id, int(edge['duration']), int(edge['period'])))
     return list(edges)
Esempio n. 7
0
def relationship_edges(schema_graph_nx: nx.MultiDiGraph, class_add_mod: dict, **kwargs) -> nx.MultiDiGraph:
    """
    Notes:
    =====
    # pass the below dictionary as the third argument (kwargs) to relationship_edges().
    # "in" indicates that the relationship has an in-edges behaviour.
    # "out" indicates that the relationship has an out-edges behaviour.

    rel_dict = {
        "rdfs:subClassOf": {
            "parentOf": "in"
        },
        "schema:domainIncludes": {
            "domainValue": "in"
        },
        "sms:requiresDependency": {
            "requiresDependency": "out"
        },
        "sms:requiresComponent": {
            "requiresComponent": "out"
        },
        "schema:rangeIncludes": {
            "rangeValue": "out"
        }
    }
    """
    for rel, rel_lab_node_type in kwargs.items():
        for rel_label, node_type in rel_lab_node_type.items():
            if rel in class_add_mod:
                parents = class_add_mod[rel]
                if type(parents) == list:
                    for _parent in parents:

                        if node_type == "in":
                            n1 = extract_name_from_uri_or_curie(_parent["@id"])
                            n2 = class_add_mod["rdfs:label"]

                        if node_type == "out":
                            n1 = class_add_mod["rdfs:label"]
                            n2 = extract_name_from_uri_or_curie(_parent["@id"])

                        # do not allow self-loops
                        if n1 != n2:
                            schema_graph_nx.add_edge(n1, n2, key=rel_label)
                elif type(parents) == dict:
                    if node_type == "in":
                        n1 = extract_name_from_uri_or_curie(parents["@id"])
                        n2 = class_add_mod["rdfs:label"]

                    if node_type == "out":
                        n1 = class_add_mod["rdfs:label"]
                        n2 = extract_name_from_uri_or_curie(parents["@id"])

                    # do not allow self-loops
                    if n1 != n2:
                        schema_graph_nx.add_edge(n1, n2, key=rel_label)

    return schema_graph_nx
Esempio n. 8
0
def add_reshape_before_op_node(graph: nx.MultiDiGraph, data_node_name: str,
                               op_node_name: str, edge_attrs: dict):
    """
    Adds reshape operation which expands dimension of the specified data tensor to 4D.
    :param graph: graph to operate on.
    :param data_node_name: the name of the data node to be reshaped to 4D tensor.
    :param op_node_name: name of the TFCustomSubgraphCall node which produces the tensor.
    :param edge_attrs: edge attributes which should be preserved.
    :return: None
    """
    data_node = Node(graph, data_node_name)

    graph.remove_edge(data_node_name, op_node_name)

    assert data_node['shape'] is not None

    new_shape = make_shape_4d(data_node['shape'])

    # reshape shape data node
    reshape_shape_data_node_name = unique_id(graph, "Reshape_shape_")
    graph.add_node(reshape_shape_data_node_name,
                   kind='data',
                   precision="FP32",
                   name=reshape_shape_data_node_name,
                   value=new_shape,
                   shape=[1])

    # reshape operation node
    reshape_node_name = unique_id(graph, "Reshape_")
    graph.add_node(reshape_node_name,
                   kind='op',
                   precision="FP32",
                   type='Reshape',
                   name=reshape_node_name,
                   op='Reshape',
                   data_type=data_node['data_type'])
    update_ie_fields(graph.node[reshape_node_name])

    # reshaped data node
    reshaped_value = None
    if data_node['value'] is not None:
        reshaped_value = np.reshape(data_node['value'], new_shape)
    reshaped_data_node_name = unique_id(graph, "reshaped_data_")
    graph.add_node(reshaped_data_node_name,
                   kind='data',
                   precision="FP32",
                   name=reshaped_data_node_name,
                   shape=new_shape,
                   value=reshaped_value,
                   nchw_layout=True)

    graph.add_edges_from([(data_node_name, reshape_node_name, {
        'in': 0
    }), (reshape_shape_data_node_name, reshape_node_name, {
        'in': 1
    }), (reshape_node_name, reshaped_data_node_name, {
        'out': 0
    }), (reshaped_data_node_name, op_node_name, edge_attrs)])
Esempio n. 9
0
    def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
        node = match['pad']
        for port, input_node in node.in_nodes().items():
            if port != 0:
                graph.remove_edge(input_node.id, node.id)

        # remove Pad operation if all pads are equal to 0
        if np.all(node.pads == 0):
            remove_op_node_with_data_node(graph, node)
Esempio n. 10
0
 def find_and_replace_pattern(self, graph: nx.MultiDiGraph):
     for n in pseudo_topological_sort(graph):
         if graph.node[n][
                 'kind'] == 'data' or graph.node[n]['op'] != 'Switch':
             continue
         switch_op_node = Node(graph, n)
         pred_id_data_node = switch_op_node.in_node(1)
         graph.remove_edge(pred_id_data_node.id, switch_op_node.id)
         remove_op_node_with_data_node(graph, switch_op_node)
Esempio n. 11
0
def query_inductions(sample: nx.MultiDiGraph, edge_queries: List[List[Edge]],
                     ownership: Ownership):
    remote_queries = mpi.comm.alltoall(edge_queries)
    answers = [
        list(filter(lambda e: e[1] in ownership, q)) for q in remote_queries
    ]
    remote_answers = mpi.comm.alltoall(answers)
    for remote_answer in remote_answers:
        sample.add_edges_from(remote_answer)
Esempio n. 12
0
def get_graph():
    graph = MultiDiGraph()
    graph.add_edge('B', 'A', edge_label='biolink:sub_class_of')
    graph.add_edge('C', 'B', edge_label='biolink:sub_class_of')
    graph.add_edge('D', 'C', edge_label='biolink:sub_class_of')
    graph.add_edge('D', 'A', edge_label='biolink:related_to')
    graph.add_edge('E', 'D', edge_label='biolink:sub_class_of')
    graph.add_edge('F', 'D', edge_label='biolink:sub_class_of')
    return graph
Esempio n. 13
0
def get_vertex_attrib(G: nx.MultiDiGraph):
    X_V = np.zeros(shape=(G.number_of_nodes(), 2), dtype=np.float32)
    index = 0
    for node in G.nodes(data=True):
        u, info = node
        X_V[index][0] = G.in_degree(u)
        X_V[index][1] = G.out_degree(u)
        index += 1
    return X_V / 6
Esempio n. 14
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
Esempio n. 15
0
    def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
        decoder_node = match['decoder']
        graph.remove_edge(decoder_node.id, match['sparse_to_dense'].id)
        graph.remove_edge(decoder_node.id, match['cast'].id)
        replace_node(match['sparse_to_dense'], decoder_node)

        # update the TensorFlow infer function for the CTCGreedyDecoder to make necessary changes with the second input
        decoder_node['old_infer'] = decoder_node.infer
        decoder_node.infer = __class__.tf_greedy_decoder_infer
        return {}
Esempio n. 16
0
def get_edge_attrib_for_GCN(G: nx.MultiDiGraph, edge_attrib, vertex_attrib):
    N_E = edge_attrib.shape[1]
    N_V = vertex_attrib.shape[1]
    X_E = np.zeros(shape=(G.number_of_edges(), N_E + 2 * N_V))
    for i, e in enumerate(G.edges(keys=True, data=True)):
        u, v, k, info = e
        X_E[i, 0:N_E] = edge_attrib[i]
        X_E[i, N_E:N_E + N_V] = vertex_attrib[u]
        X_E[i, N_E + N_V:N_E + 2 * N_V] = vertex_attrib[v]
    return X_E
Esempio n. 17
0
 def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
     node = match['op']
     # here we request all data flow output edges (control flow edges will not be listed)
     out_edges = node.out_edges()
     if len(out_edges) == 0:
         graph.remove_node(node.id)
         log.debug('Assert op was removed {}'.format(node.id))
     else:
         raise Error('Data flow edge coming out of Assert node {}'.format(
             node.id))
Esempio n. 18
0
def construct_multi_di_graph(edges: List[Tuple[str, str, Any]]) -> MultiDiGraph:
    """Constructs a multi directed graph from a list of edges.

    Arguments:
        edges: A list of tuples with entries: start, end and identifier.
    """
    g = MultiDiGraph()
    for start, end, key in edges:
        g.add_edge(start, end, key=key)
    return g
Esempio n. 19
0
    def __init__(self, g_input: Graph) -> None:
        log.info(
            f"FeatureConcatenator: Initiating with a graph of {g_input.number_of_nodes()} nodes "
            f"and {g_input.number_of_edges()} edges"
        )
        self.g_input = g_input

        self.g = MultiDiGraph(deepcopy(g_input))
        self._obtain_attrs()
        self._init_feat_attrs()
Esempio n. 20
0
def get_all_successors(bag: str, graph: nx.MultiDiGraph) -> set:
    child_bags = []
    for child_bag in graph.successors(bag):
        succ = get_all_successors(child_bag, graph)

        for i in range(graph.number_of_edges(bag, child_bag)):
            child_bags.append(child_bag)
            child_bags += succ

    return child_bags
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)
Esempio n. 22
0
def eliminate_dead_nodes(graph: nx.MultiDiGraph):
    nodes_to_remove = set()
    for node_name, node_attrs in graph.nodes(data=True):
        if not node_attrs['is_output_reachable'] or (
                node_attrs['is_const_producer']
                and not node_attrs['is_undead']):
            nodes_to_remove.add(node_name)
    log.debug('Removing the following dead nodes: {}'.format('\n'.join(
        sorted(map(str, nodes_to_remove)))))
    graph.remove_nodes_from(nodes_to_remove)
Esempio n. 23
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
Esempio n. 24
0
def summarize_nodes(graph: nx.MultiDiGraph,
                    facet_properties: List = None) -> Dict:
    """
    Summarize the nodes in a graph.

    Parameters
    ----------
    graph: networkx.MultiDiGraph
        The graph
    facet_properties: List
        A list of properties to facet on

    Returns
    -------
    Dict
        The node stats

    """
    stats = {
        TOTAL_NODES: 0,
        NODE_CATEGORIES: set(),
        COUNT_BY_CATEGORY: {
            'unknown': {
                'count': 0
            }
        }
    }

    stats[TOTAL_NODES] = len(graph.nodes())
    if facet_properties:
        for facet_property in facet_properties:
            stats[facet_property] = set()

    for n, data in graph.nodes(data=True):
        if 'category' not in data:
            stats[COUNT_BY_CATEGORY]['unknown']['count'] += 1
            continue
        categories = data['category']
        stats[NODE_CATEGORIES].update(categories)
        for category in categories:
            if category in stats[COUNT_BY_CATEGORY]:
                stats[COUNT_BY_CATEGORY][category]['count'] += 1
            else:
                stats[COUNT_BY_CATEGORY][category] = {'count': 1}

            if facet_properties:
                for facet_property in facet_properties:
                    stats = get_facet_counts(data, stats, COUNT_BY_CATEGORY,
                                             category, facet_property)

    stats[NODE_CATEGORIES] = sorted(list(stats[NODE_CATEGORIES]))
    if facet_properties:
        for facet_property in facet_properties:
            stats[facet_property] = sorted(list(stats[facet_property]))
    return stats
Esempio n. 25
0
def from_osmnx(oxg: nx.MultiDiGraph, use_label: bool):
    """
    The method transforms an OSMX MultiDiGraph into a PrimalGraph object
    :param oxg: an OSMX MultiDiGraph
    :param use_label: if true, it maps streets' type as labels (required for the HICN algorithm)
                      otherwise, streets' type are standardized as "unlabeled" (required for the ICN algorithm)
    :return: PrimalGraph
    """

    # creating an empty primal graph
    primal_graph = PrimalGraph()

    # converting a MultiDiGraph into a simple Graph
    oxg = nx.Graph(oxg)

    # removing self-loops from the resulting graph
    oxg.remove_edges_from(oxg.selfloop_edges())

    # latitude (y-axis) and longitude (x-axis)
    node_dictionary = {
        nid: (data['y'], data['x'])
        for nid, data in oxg.nodes(data=True)
    }

    # updating the dictionary of nodes
    primal_graph.node_dictionary = node_dictionary

    eid = 0
    edge_dictionary = {}
    for source, target, data in oxg.edges(data=True):
        name = data.get(
            'name',
            'unknown')  # unknown is the default value for streets' name
        label = data.get(
            'highway', 'unclassified'
        )  # unclassified is the default value for streets' type
        length = compute_distance(
            node_dictionary[source], node_dictionary[target]
        )  # straight-line distance between source and target nodes

        # creating a new PrimalEdge with information from the current edge
        edge = primal_graph.Edge(
            eid, source, target, float(length),
            'unknown' if type(name) == list else name, 'unclassified'
            if type(label) == list else label if use_label else 'unclassified')

        # storing the new edge in the edge dictionary
        edge_dictionary[eid] = edge
        eid += 1

    # updating the dictionary of edges
    primal_graph.edge_dictionary = edge_dictionary

    # building and returning the resulting PrimalGraph
    return primal_graph.build_graph()
def scale_free_graph(n, alpha=0.41,beta=0.54,delta_in=0.2,delta_out=0):
    def _choose_node(G,distribution,delta):
        cumsum = 0.0
        psum = float(sum(distribution.values()))+float(delta)*len(distribution)
        r = random.random()
        for i in range(0, len(distribution)):
            cumsum += (distribution[i]+delta)/psum
            if r < cumsum:
                break
        return i

    G = MultiDiGraph()
    G.add_edges_from([(0,1),(1,2),(2,0)])
    gamma = 1 - alpha - beta

    while len(G)<n:
        r = random.random()
        if r < alpha:
            v = len(G)
            w = _choose_node(G, G.in_degree(),delta_in)
        elif r < alpha+beta:
            v = _choose_node(G, G.out_degree(),delta_out)
            w = _choose_node(G, G.in_degree(),delta_in)
        else:
            v = _choose_node(G, G.out_degree(),delta_out)
            w = len(G)
        G.add_edge(v,w)
    return G
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
Esempio n. 28
0
def sst(n):
    S = MultiDiGraph()
    morphisms = [('X' + str(m + 1), 'X' + str(m), 'd' + str(m) + str(i))
                 for m in range(n) for i in range(m + 2)]
    relations = []
    if n > 1:
        relations = [(('X'+str(m+2),'X'+str(m),'d'+str(m+1)+str(j)+'d'+str(m)+str(i)),\
                      ('X'+str(m+2),'X'+str(m),'d'+str(m+1)+str(i)+'d'+str(m)+str(j-1)))
                     for m in range(n-1) for j in range(1,m+3) for i in range(j)]
    S.add_edges_from(morphisms)
    return fic(S, relations)
Esempio n. 29
0
def delete_not_executable(graph: nx.MultiDiGraph):
    nodes_to_remove = set()
    for node_name, node_attrs in graph.nodes(data=True):
        if node_attrs[
                'kind'] == 'data' and 'executable' in node_attrs and not node_attrs[
                    'executable']:
            [nodes_to_remove.add(op) for op, _ in graph.in_edges(node_name)]
            nodes_to_remove.add(node_name)
    log.debug('Removing the following not executable nodes: {}'.format(
        '\n'.join(sorted(map(str, nodes_to_remove)))))
    graph.remove_nodes_from(nodes_to_remove)
Esempio n. 30
0
def combine_dots(dotlist, mathid):
    g1 = drawing.nx_agraph.read_dot(TMP + dotlist[-1])
    g = MultiDiGraph()
    for d in dotlist:
        g2 = drawing.nx_agraph.read_dot(TMP + d)
        g1 = compose(g1, g2)
    g.add_nodes_from(g1.nodes(data=True))
    g.add_edges_from(g1.edges(data=True))
    g.to_directed()
    g = nx_agraph.to_agraph(g)
    g.write(TMP + '-'.join(mathid) + '.dot')
Esempio n. 31
0
def transaction_cycle(graph: nx.MultiDiGraph) -> None:
    """
    Find a cycle from an account back to itself, to determine if
    a series of transactions ever comes back full circle.

    This function tracks a series of transactions from one account to
    others, to see if any Ether that this account sent out eventually
    may have come back to it, as a means of studying the flow of currency
    amongst accounts.

    Preconditions:
      - list(graph.nodes) != []
    """
    # Find any relevant nodes that we can recurse on to find cycles:
    # Criteria:
    # - Must have at least 1 successor (outgoing transaction)
    # - Must have at least 1 predecessor (incoming transaction)
    accounts = []
    for node in graph.nodes():
        num_predecessors = len(list(graph.predecessors(node)))
        num_successors = len(list(graph.successors(node)))

        if num_successors >= 1 and num_predecessors >= 1:
            accounts.append(node)

    # Loop through all the possible candidate accounts for having a cycle.
    cycles = []
    for account in accounts:
        main_acc = account

        # Now, we will check every successor of this main node (to see
        # whether or not it is involved in a cycle that ends back at
        # this main node.)
        print(f"Starting Account: {main_acc}")
        print("Beginning search for cycle starting with this account...")

        cycle = _check_cycle(graph, main_acc, main_acc, set(), 0)
        if cycle is not None:
            print(f"Cycle found for: {main_acc}")
            cycles.append(cycle)
        else:
            print(f"No Cycle found for: {main_acc}")

        print("---------------------------------------")

    # Print a summary of what was found.
    print("###########SUMMARY############")
    print(f"Number of cycles found: {len(cycles)}")

    if cycles == []:
        print("Unfortunately, no cycles could be found.")
    else:
        for i in range(0, len(cycles)):
            print(f"Cycle {i + 1}: {cycles[i]}")
Esempio n. 32
0
 def __init__(self, activation_fn=None):
     """Initialize the NetworkXKB."""
     # parameters
     if activation_fn is None:
         activation_fn = (lambda graph, mem_id: None)
     self.activation_fn = activation_fn
     # variables
     self.graph = MultiDiGraph()
     self.inverted_index = defaultdict(set)
     self.query_results = None
     self.result_index = None
     self.clear()
def add_data_from_single_image_graph_to_cluster_graph(line_graph: nx.DiGraph,
                                                      graph: nx.MultiDiGraph):
    for node in line_graph.adj:
        for neighbour in nx.neighbors(line_graph, node):
            edge = line_graph.get_edge_data(node, neighbour)
            for neighbour2 in nx.neighbors(line_graph, neighbour):
                edge2 = line_graph.get_edge_data(neighbour, neighbour2)
                graph.add_edge(edge['cluster'],
                               edge2['cluster'],
                               key=None,
                               nodes_from=str(node) + "_" + str(neighbour),
                               nodes_to=str(neighbour) + "_" + str(neighbour2))
Esempio n. 34
0
def add_cfg_edges(g: nx.MultiDiGraph, node):
    """Add edges with attr `cfg` or `in` for control flow for the given node"""

    if isinstance(node, clang.graph.FunctionInfo):
        for cfg_b in node.cfgBlocks:
            g.add_node(cfg_b, attr="cfg")
            for succ in cfg_b.successors:
                g.add_edge(cfg_b, succ, attr="cfg")
                g.add_node(succ, attr="cfg")
            for stmt in cfg_b.statements:
                g.add_edge(stmt, cfg_b, attr="in")
                g.add_node(stmt, attr=(stmt.name))
Esempio n. 35
0
def read_cref(inmap):
    modules = MultiDiGraph()
    while True:
        l = inmap.readline()
        words = l.split()
        if len(words) == 2:
            last_symbol = words[0]
            last_module = words[1]
        elif len(words) == 1:
            modules.add_edge(words[0], last_module, label=last_symbol);
        elif len(l) == 0:
            break
    return modules
Esempio n. 36
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
Esempio n. 37
0
    def to_directed(self):
        """Return a directed representation of the graph.
 
        A new multidigraph is returned with the same name, same nodes and
        with each edge (u,v,data) replaced by two directed edges
        (u,v,data) and (v,u,data).
        
        """
        from networkx import MultiDiGraph 
        G=MultiDiGraph()
        G.add_nodes_from(self)
        G.add_edges_from( ((u,v,data) for u,nbrs in self.adjacency_iter() \
                for v,datalist in nbrs.iteritems() for data in datalist) )
        return G
Esempio n. 38
0
def combine_dots(dotlist):
    g1=drawing.nx_agraph.read_dot('dots/'+dotlist[-1])
    g = MultiDiGraph()
    for i in xrange(len(dotlist)):
        g2=drawing.nx_agraph.read_dot('dots/'+dotlist[i])
        g1=compose(g1,g2)
    g.add_nodes_from(g1.nodes(data=True))
    g.add_edges_from(g1.edges(data=True))
    g.to_directed()
    g = nx_agraph.to_agraph(g)
    g.write('dots/combined.dot')
Esempio n. 39
0
def combine_dots(dotlist,sciid):
    g1=drawing.nx_agraph.read_dot(TMP+dotlist[-1])
    g = MultiDiGraph()
    for d in dotlist:
        g2=drawing.nx_agraph.read_dot(TMP + d)
        g1=compose(g1,g2)
    g.add_nodes_from(g1.nodes(data=True))
    g.add_edges_from(g1.edges(data=True))
    g.to_directed()
    g = nx_agraph.to_agraph(g)
    g.write(TMP+'-'.join(sciid)+'.dot')
Esempio n. 40
0
 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
Esempio n. 41
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))
Esempio n. 42
0
def compute_max_weight(G: networkx.MultiDiGraph,
                       start: int = META_REACTANT) -> None:
    """Compute maximum weight paths.

    This function annotates each node of the graph with its maximum
    weight from the starting node and a link to its predecessor.

    """
    def extract_max(labels):
        max_weight = -1
        max_label = None
        for label in labels:
            if G.node[label]['max_weight'] > max_weight:
                max_weight = G.node[label]['max_weight']
                max_label = label

        if max_label is not None:
            labels.remove(max_label)

        return max_label, max_weight

    labels = []
    for label in G.nodes_iter():
        G.node[label]['max_weight'] = np.inf if label == start else -1
        G.node[label]['predecessor'] = None
        G.node[label]['bottleneck'] = None
        labels.append(label)

    while labels:
        # Find out which node has maximal weight.
        current, weight = extract_max(labels)

        for neighbor in G[current]:
            m = min(weight, G.edge[current][neighbor][0]['weight'])
            if G.node[neighbor]['max_weight'] < m:
                # We can reach the neighbor node from the current node
                # through a path with a wider bottleneck that the one
                # we (perhaps) already knew.
                G.node[neighbor]['max_weight'] = m
                G.node[neighbor]['predecessor'] = current

                # Update the location of the bottleneck.
                if weight < m:
                    G.node[neighbor]['bottleneck'] \
                        = G.node[current]['bottleneck']
                else:
                    G.node[neighbor]['bottleneck'] = (current, neighbor)
Esempio n. 43
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)
Esempio n. 44
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])
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
Esempio n. 46
0
	def __init__(self):
		MultiDiGraph.__init__(self)
		self.elaborated = False
Esempio n. 47
0
 def in_edges(self,node):
     for _,_,data in MultiDiGraph.in_edges(self,node,data=True):#necessary because for out we can specify data="object", for in we can't
         yield data["object"]
Esempio n. 48
0
 def overwrite_edge(self,morph,edge2):
     edge = self.InverseLookUp[morph]
     MultiDiGraph.remove_edge(self,edge.source,edge.target,key = edge.key)
     self.InverseLookUp[morph] = edge2
Esempio n. 49
0
File: network.py Progetto: RP7/migen
	def __init__(self):
		MultiDiGraph.__init__(self)
		self.elaborated = False
		self.abstract_busy_signals = dict()
Esempio n. 50
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
Esempio n. 51
0
def imbalancedCitationsPublicationsExample():
    """
      Illustrative example of imbalanced citations / publications to verify ShapeSim is working correctly
    """

    graph = MultiDiGraph()
    authors = ['Alice', 'Bob', 'Carol', 'Dave', 'Ed', 'Frank']
    conference = 'KDD'

    # Citation & publication count configuration
    citationsPublications = {
        'Alice': (100, 10),
        'Bob': (80, 10),
        'Carol': (100, 100),
        'Dave': (50, 10),
        'Ed': (10, 10),
        'Frank': (1000, 100)
    }

    actualCitationsPublications = defaultdict(lambda: (0, 0))

    # Helper functions for repeatedly adding papers to the graph
    addPapersToAuthor = lambda n, author: [addPublicationPaper(author) for _ in itertools.repeat(None, n)]
    addCitationsToPaper = lambda n, paper, author: [addCitationPaper(paper, author) for _ in itertools.repeat(None, n)]

    # Helper for getting the next id
    def __getNextId():
        global nextId
        oldId = nextId
        nextId += 1
        return oldId

    def addPublicationPaper(author):
        """
          Helper method to add a 'publication' paper, connected to both an author and a conference
        """
        paper = "%s's Paper %d" % (author, (__getNextId()))
        graph.add_node(paper)
        graph.add_edges_from([(author, paper), (paper, author), (paper, conference), (conference, paper)])

        citationCount, publicationCount = actualCitationsPublications[author]
        actualCitationsPublications[author] = (citationCount, publicationCount + 1)

        return paper

    def addCitationPaper(citedPaper, citedAuthor):
        """
          Helper method to add a 'citation' paper, which is only connected to the conference and the paper it cites
        """
        citingPaper = "Citing Paper %d" % __getNextId()
        graph.add_node(citingPaper)
        graph.add_edges_from([(conference, citingPaper), (citingPaper, conference), (citingPaper, citedPaper)])

        citationCount, publicationCount = actualCitationsPublications[citedAuthor]
        actualCitationsPublications[citedAuthor] = (citationCount + 1, publicationCount)

        return citingPaper

    allPapers = []

    # Construct the graph
    graph.add_nodes_from(authors + [conference])
    for authorName in citationsPublications:
        citationCount, publicationCount = citationsPublications[authorName]

        # Add citations & publications to author
        authorPapers = addPapersToAuthor(publicationCount, authorName)
        allPapers.extend(authorPapers)
        citationsPerPaper = citationCount / publicationCount
        for paper in authorPapers:
            citingPapers = addCitationsToPaper(citationsPerPaper, paper, authorName)
            allPapers.extend(citingPapers)

    nodeIndex = {
        'paper': {i: allPapers[i] for i in xrange(0, len(allPapers))},
        'conference': {0: 'KDD'},
        'author': {0: 'Alice', 1: 'Bob', 2: 'Carol', 3: 'Dave', 4: 'Ed', 5: 'Frank'}
    }

    # Test PathSim / NeighborSim
    cpaAdjMatrix, extraData = getMetaPathAdjacencyData(graph, nodeIndex, ['conference', 'paper', 'author'])
    extraData['fromNodes'] = extraData['toNodes']
    extraData['fromNodesIndex'] = extraData['toNodesIndex']
    neighborSimMostSimilar, similarityScores = findMostSimilarNodes(
        cpaAdjMatrix, 'Alice', extraData, method=getNeighborSimScore
    )

    # Test ShapeSim
    cppaAdjTensor, extraData = getMetaPathAdjacencyTensorData(
        graph, nodeIndex, ['conference', 'paper', 'paper', 'author']
    )
    extraData['fromNodes'] = extraData['toNodes']
    extraData['fromNodesIndex'] = extraData['toNodesIndex']
    shapeSimMostSimilar, similarityScores = findMostSimilarNodes(
        cppaAdjTensor, 'Alice', extraData, method=getNumpyShapeSimScore, alpha=1.0
    )

    # Output similarity scores
    for name, mostSimilar in [('NeighborSim', neighborSimMostSimilar), ('ShapeSim', shapeSimMostSimilar)]:
        print('\n%s Most Similar to "%s":' % (name, 'Alice'))
        mostSimilarTable = texttable.Texttable()
        rows = [['Author', 'Score']]
        rows += [[name, score] for name, score in mostSimilar]
        mostSimilarTable.add_rows(rows)
        print(mostSimilarTable.draw())
Esempio n. 52
0
 def add_edge(self,node1,node2,**kwargs):# inefficient but nobody cares and it's convenient
     e=Edge(node1,node2,self.keycounter,kwargs)
     MultiDiGraph.add_edge(self,node1,node2,object = e,key = self.keycounter)
     self.InverseLookUp[kwargs["morphism"]] = e
     self.keycounter+=1
     return e
Esempio n. 53
0
def parseArnetminerDataset():
    """
      Parse the four area dataset, and use only barebones structures to keep everything efficient.

        Skips papers that:
            (1)

        The final parsed network
    """

    inputFile = open(os.path.join(projectRoot, 'data','DBLP-citation-Feb21.txt'))
    graph = MultiDiGraph()

    # Sets for authors, papers, conferences, and terms found so far
    indexToPaperIdMap = {}
    citationCountMap = {}
    indexSet = set()

    beginning = inputFile.tell()

    print "Parsing nodes for graph..."

    # Counts for statistics
    VALID_PAPERS = 1566322 # 99.62% of total papers in DBLP dataset
    papersProcessed = 0
    skippedPaperIndices = set()
    invalidPaperIndices = set()

    # Add each paper to graph (adding missing associated terms, authors, and conferences)
    for title, authors, conference, terms, citationCount, index in __papersFromFile(inputFile, skippedPaperIndices, invalidPaperIndices):

        # Check that index is unique, and record it
        assert index not in indexSet
        indexSet.add(index)

        # Create unique identifier with paper index & title
        paperId = '%d----%s' % (index, title)
        citationCountMap[paperId] = citationCount
        indexToPaperIdMap[index] = paperId

        # Add symmetric edges & nodes (if they don't already exist in the network)
        for author in authors:
            graph.add_edges_from([(author, paperId), (paperId, author)])
        graph.add_edges_from([(conference, paperId), (paperId, conference)])
        for term in terms:
            graph.add_edges_from([(term, paperId), (paperId, term)])

        # Output progress
        papersProcessed += 1
        sys.stdout.write("\r Processed %d / %d papers..." % (papersProcessed, VALID_PAPERS))

    # Rewind file
    inputFile.seek(beginning)

    print "Parsing citations for graph..."

    # Counts for statistics
    papersProcessed = 0
    successfulCitations = 0
    omittedPaperCitations = 0
    invalidPaperCitations = 0
    invalidCitations = 0

    # Add citations to the graph
    for title, index, citations in __citationsFromFile(inputFile):
        citingId = '%d----%s' % (index, title)
        for citationIndex in citations:

            # Add citation edge if it was found
            if citationIndex in indexToPaperIdMap:
                successfulCitations += 1
                graph.add_edge(citingId, indexToPaperIdMap[citationIndex])

            # Tally missing citation appropriately
            elif citationIndex in skippedPaperIndices:
                omittedPaperCitations += 1
            elif citationIndex in invalidPaperIndices:
                invalidPaperCitations += 1
            else:
                print "\nCitation '%d' not found for '%s'" % (citationIndex, title)
                invalidCitations += 1

        # Output progress
        papersProcessed += 1
        sys.stdout.write("\r Processed Citations for %d / %d papers..." % (papersProcessed, VALID_PAPERS))

    # Basic statistics about cleanliness of citations
    totalCitations = invalidCitations + successfulCitations
    successfulCitationsPercent = 100 * float(successfulCitations) / totalCitations
    omittedPaperCitationsPercent = 100 * float(omittedPaperCitations) / totalCitations
    invalidPaperCitationsPercent = 100 * float(invalidPaperCitations) / totalCitations
    invalidCitationsPercent = 100 * float(invalidCitations) / totalCitations
    print "\n\nTotal Citations: %d" % totalCitations
    print "  Citations Added (Successful): %d (%2.2f%%)" % (successfulCitations, successfulCitationsPercent)
    print "  Citations Skipped (Skipped Paper): %d (%2.2f%%)" % (omittedPaperCitations, omittedPaperCitationsPercent)
    print "  Citations Skipped (Invalid Paper): %d (%2.2f%%)" % (invalidPaperCitations, invalidPaperCitationsPercent)
    print "  Citations Invalid (Unknown): %d (%2.2f%%)" % (invalidCitations, invalidCitationsPercent)

    return graph
Esempio n. 54
0
 def out_edges(self,node):
     for _,_,data in MultiDiGraph.out_edges(self,node,data=True):
         yield data["object"]
Esempio n. 55
0
def scale_free_graph(n, G=None,
                     alpha=0.41,
                     beta=0.54,
                     gamma=0.05,
                     delta_in=0.2,
                     delta_out=0,
                     seed=None):
    """Return a scale free directed graph

    Parameters
    ----------
    n : integer
       Number of nodes in graph

    G : NetworkX graph (optional)
       Use as starting graph in algorithm

    alpha : float 
       Probability for adding a new node conecgted to an existing node
       chosen randomly according to the in-degree distribution.

    beta : float
       Probability for adding an edge between two existing nodes.
       One existing node is chosen randomly according the in-degree 
       distribution and the other chosen randomly according to the out-degree 
       distribution.
       
    gamma : float
       Probability for adding a new node conecgted to an existing node
       chosen randomly according to the out-degree distribution.
        
    delta_in : float
       Bias for choosing ndoes from in-degree distribution.

    delta_out : float
       Bias for choosing ndoes from out-degree distribution.

    delta_out : float
       Bias for choosing ndoes from out-degree distribution.

    seed : integer (optional)
       Seed for random number generator

    Examples
    --------
    >>> G=nx.scale_free_graph(100)

    
    Notes
    -----
    The sum of alpha, beta, and gamma must be 1.

    Algorithm from
    
    @article{bollobas2003dsf,
    title={{Directed scale-free graphs}},
    author={Bollob{\'a}s, B. and Borgs, C. and Chayes, J. and Riordan, O.},
    journal={Proceedings of the fourteenth annual ACM-SIAM symposium on Discrete algorithms},
    pages={132--139},
    year={2003},
    publisher={Society for Industrial and Applied Mathematics Philadelphia, PA, USA}
    }

"""

    def _choose_node(G,distribution,delta):
        cumsum=0.0
        # normalization 
        psum=float(sum(distribution.values()))+float(delta)*len(distribution)
        r=random.random()
        for i in range(0,len(distribution)):
            cumsum+=(distribution[i]+delta)/psum
            if r < cumsum:  
                break
        return i

    if G is None:
        # start with 3-cycle
        G=MultiDiGraph()
        G.add_edges_from([(0,1),(1,2),(2,0)])

    if alpha <= 0:
        raise ValueError('alpha must be >= 0.')
    if beta <= 0:
        raise ValueError('beta must be >= 0.')
    if gamma <= 0:
        raise ValueError('beta must be >= 0.')

    if alpha+beta+gamma !=1.0:
        raise ValueError('alpha+beta+gamma must equal 1.')
        
    G.name="directed_scale_free_graph(%s,alpha=%s,beta=%s,gamma=%s,delta_in=%s,delta_out=%s)"%(n,alpha,beta,gamma,delta_in,delta_out)

    # seed random number generated (uses None as default)
    random.seed(seed)

    while len(G)<n:
        r = random.random()
        # random choice in alpha,beta,gamma ranges
        if r<alpha:
            # alpha
            # add new node v
            v = len(G) 
            # choose w according to in-degree and delta_in
            w = _choose_node(G, G.in_degree(with_labels=True),delta_in)
        elif r < alpha+beta:
            # beta
            # choose v according to out-degree and delta_out
            v = _choose_node(G, G.out_degree(with_labels=True),delta_out)
            # choose w according to in-degree and delta_in
            w = _choose_node(G, G.in_degree(with_labels=True),delta_in)
        else:
            # gamma
            # choose v according to out-degree and delta_out
            v = _choose_node(G, G.out_degree(with_labels=True),delta_out)
            # add new node w
            w = len(G) 
        G.add_edge(v,w)
        
    return G
 def __init__(self, player1, player2, initial_gamestate):
     self.game_graph = MultiDiGraph()
     self.player1 = player1
     self.player2 = player2
     self.initial_gamestate = self.canonicalize(initial_gamestate)
Esempio n. 57
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()