python类DiGraph()的实例源码

networks.py 文件源码 项目:pysmap 作者: SMAPPNYU 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def retweet_network(collection, tweet_fields, user_fields):
    def replace_none(s):
        if s is None:
            return 'NULL'
        return s

    tp = TweetParser()
    dg = nx.DiGraph(name="retweet graph")

    for tweet in collection:

        um_dict = {field:replace_none(value) for field,value in tp.parse_columns_from_tweet(tweet['user'], user_fields)}
        t_dict = {field:replace_none(value) for field,value in tp.parse_columns_from_tweet(tweet, tweet_fields)}

        if tweet['user']['id_str'] not in dg:
            dg.add_node(tweet['user']['id_str'], attr_dict=um_dict)
        if 'retweeted_status' in tweet:
            rtu_dict = {field:replace_none(value) for field,value in tp.parse_columns_from_tweet(tweet['retweeted_status']['user'], user_fields)}
            dg.add_node(tweet['retweeted_status']['user']['id_str'], attr_dict=rtu_dict)
            dg.add_edge(tweet['user']['id_str'], tweet['retweeted_status']['user']['id_str'], attr_dict=t_dict)
    return dg
compiler.py 文件源码 项目:elfi 作者: elfi-dev 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def compile(cls, source_net, compiled_net):
        """Add runtime instruction nodes to the computation graph.

        Parameters
        ----------
        source_net : nx.DiGraph
        compiled_net : nx.DiGraph

        Returns
        -------
        compiled_net : nx.Digraph

        """
        logger.debug("{} compiling...".format(cls.__name__))

        instruction_node_map = dict(_uses_batch_size='_batch_size', _uses_meta='_meta')

        for instruction, _node in instruction_node_map.items():
            for node, d in source_net.nodes_iter(data=True):
                if d.get(instruction):
                    if not compiled_net.has_node(_node):
                        compiled_net.add_node(_node)
                    compiled_net.add_edge(_node, node, param=_node[1:])

        return compiled_net
compiler.py 文件源码 项目:elfi 作者: elfi-dev 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def compile(cls, source_net, compiled_net):
        """Remove redundant nodes from the computation graph.

        Parameters
        ----------
        source_net : nx.DiGraph
        compiled_net : nx.DiGraph

        Returns
        -------
        compiled_net : nx.Digraph

        """
        logger.debug("{} compiling...".format(cls.__name__))

        outputs = compiled_net.graph['outputs']
        output_ancestors = nbunch_ancestors(compiled_net, outputs)
        for node in compiled_net.nodes():
            if node not in output_ancestors:
                compiled_net.remove_node(node)
        return compiled_net
dependencygraph.py 文件源码 项目:one-day-with-cling 作者: mariana-scorp 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def nx_graph(self):
        """Convert the data in a ``nodelist`` into a networkx labeled directed graph."""
        import networkx as NX

        nx_nodelist = list(range(1, len(self.nodes)))
        nx_edgelist = [
            (n, self._hd(n))
            for n in nx_nodelist if self._hd(n)
        ]
        nx_labels = {}
        for n in nx_nodelist:
            nx_labels[n] = self.nodes[n]['word']

        g = NX.DiGraph()
        g.add_nodes_from(nx_nodelist)
        g.add_edges_from(nx_edgelist)

        return g, nx_labels
pathwise.py 文件源码 项目:watlink 作者: dustalov 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def isas(path):
    G = nx.DiGraph()

    with open(path, newline='') as f:
        reader = csv.reader(f, delimiter='\t')

        for row in reader:
            if len(row) > 1 and row[0] and row[1]:
                G.add_edge(sanitize(row[0]), sanitize(row[1]))

    # Note that we store the sense inventory as an attribute of G.
    # TODO: nx.DiGraph subclass?
    G.senses = defaultdict(list)

    for node in G.nodes():
        G.senses[node.rsplit('#', 1)[0]].append(node)

    return G
aborescene_synthesis.py 文件源码 项目:NetPower_TestBed 作者: Vignesh2208 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def compute_shortest_path_tree(self, dst_sw):

        spt = nx.DiGraph()

        mdg = self.network_graph.get_mdg()

        paths = nx.shortest_path(mdg, source=dst_sw.node_id)

        for src in paths:

            if src == dst_sw.node_id:
                continue

            for i in range(len(paths[src]) - 1):
                spt.add_edge(paths[src][i], paths[src][i+1])

        return spt
fwd.py 文件源码 项目:Ryu-SDN-IP 作者: sdnds-tw 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_nx_graph(self):
        graph = nx.DiGraph()
        switches = topo_api.get_all_switch(self)
        links = topo_api.get_all_link(self)

        for switch in switches:
            dpid = switch.dp.id
            graph.add_node(dpid)

        for link in links:
            src_dpid = link.src.dpid
            dst_dpid = link.dst.dpid
            src_port = link.src.port_no
            dst_port = link.dst.port_no
            graph.add_edge(src_dpid,
                           dst_dpid,
                           src_port=src_port,
                           dst_port=dst_port)
        return graph
__init__.py 文件源码 项目:gnpy 作者: Telecominfraproject 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def network_from_json(json_data):
    # NOTE|dutc: we could use the following, but it would tie our data format
    #            too closely to the graph library
    # from networkx import node_link_graph
    g = DiGraph()

    nodes = {}
    for el in json_data['elements']:
        el = getattr(elements, el['type'])(el['id'], **el['metadata'])
        g.add_node(el)
        nodes[el.id] = el

    for cx in json_data['connections']:
        from_node, to_node = nodes[cx['from_node']], nodes[cx['to_node']]
        g.add_edge(from_node, to_node)

    return g
ganalysis.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __len__(self):
        """Return the number of nodes. Use the expression 'len(G)'.

        Returns
        -------
        nnodes : int
            The number of nodes in the graph.

        Examples
        --------
        >>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
        >>> G.add_path([0,1,2,3])
        >>> len(G)
        4

        """
        return len(self.node)
ganalysis.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def number_of_nodes(self):
        """Return the number of nodes in the graph.

        Returns
        -------
        nnodes : int
            The number of nodes in the graph.

        See Also
        --------
        order, __len__  which are identical

        Examples
        --------
        >>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
        >>> G.add_path([0,1,2])
        >>> len(G)
        3
        """
        return len(self.node)
ganalysis.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def adjacency_list(self):
        """Return an adjacency list representation of the graph.

        The output adjacency list is in the order of G.nodes().
        For directed graphs, only outgoing adjacencies are included.

        Returns
        -------
        adj_list : lists of lists
            The adjacency structure of the graph as a list of lists.

        See Also
        --------
        adjacency_iter

        Examples
        --------
        >>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
        >>> G.add_path([0,1,2,3])
        >>> G.adjacency_list() # in order given by G.nodes()
        [[1], [0, 2], [1, 3], [2]]

        """
        return list(map(list,iter(self.adj.values())))
ganalysis.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def adjacency_iter(self):
        """Return an iterator of (node, adjacency dict) tuples for all nodes.

        This is the fastest way to look at every edge.
        For directed graphs, only outgoing adjacencies are included.

        Returns
        -------
        adj_iter : iterator
           An iterator of (node, adjacency dictionary) for all nodes in
           the graph.

        See Also
        --------
        adjacency_list

        Examples
        --------
        >>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
        >>> G.add_path([0,1,2,3])
        >>> [(n,nbrdict) for n,nbrdict in G.adjacency_iter()]
        [(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]

        """
        return iter(self.adj.items())
generate_beam_viz.py 文件源码 项目:seq2seq 作者: google 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def create_graph(predicted_ids, parent_ids, scores, vocab=None):
  def get_node_name(pred):
    return vocab[pred] if vocab else str(pred)

  seq_length = predicted_ids.shape[0]
  graph = nx.DiGraph()
  for level in range(seq_length):
    names = [get_node_name(pred) for pred in predicted_ids[level]]
    _add_graph_level(graph, level + 1, parent_ids[level], names, scores[level])
  graph.node[(0, 0)]["name"] = "START"
  return graph
tabulator.py 文件源码 项目:silverchain 作者: tomokinakamaru 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _create_eps_closure_func(table):
    edges = {(c.src, c.sym.text, c.dst[0]) for c in table.cells}
    graph = DiGraph((src, dst) for src, sym, dst in edges if sym == '')
    states = set(sum(((src, dst) for src, _, dst in edges), ()))

    cs = {s: descendants(graph, s) if s in graph else set() for s in states}
    return lambda ss: frozenset(set(ss).union(*(cs[s] for s in ss)))
git_commit_utils.py 文件源码 项目:saapy 作者: ashapochka 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def build_file_move_graph(file_frame):
    move_frame = file_frame[file_frame.move]
    move_graph = nx.from_pandas_dataframe(
        move_frame,
        source='file_path1', target='file_path2',
        edge_attr='hexsha', create_using=nx.DiGraph())
    return move_graph
generators.py 文件源码 项目:sparse-digraph-generator 作者: papoudakis 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def sdg(num_nodes, num_edges, epsilon1, epsilon2):
    gen_graph = nx.DiGraph()
    nodes = range(num_nodes)
    gen_graph.add_nodes_from(nodes)
    i = 0
    while i < num_edges:
        # choose out node uniformly
        if random.random() < epsilon1 or len(gen_graph.edges()) < 1:
            out_node = random.choice(nodes)
        # choose out based on preferential attachment
        else:
            out_nodes = gen_graph.out_degree().keys()
            out_degree = np.array(gen_graph.out_degree().values())

            out_node = np.random.choice(out_nodes, p=out_degree/(1.0 * i))
        # choose in node uniformly
        if random.random() < epsilon2 or len(gen_graph.edges()) < 1:
            if 0 not in gen_graph.in_degree().values():
                in_node = random.choice(nodes)
        # choose in based on preferential attachment
            else:

                if len(nx.isolates(gen_graph)) > 0:
                    in_node = random.choice(nx.isolates(gen_graph))
                else:
                    in_node_degree = 1
                    while in_node_degree > 0:
                        in_node = random.choice(gen_graph.nodes())
                        in_node_degree = gen_graph.in_degree(in_node)
        else:
            in_nodes = gen_graph.in_degree().keys()
            in_degree = np.array(gen_graph.in_degree().values())
            in_node = np.random.choice(in_nodes, p=in_degree / (1.0 * i))

        if out_node != in_node and (out_node, in_node) not in gen_graph.edges():
            gen_graph.add_edge(out_node, in_node)
            i += 1

    return gen_graph
utils.py 文件源码 项目:sparse-digraph-generator 作者: papoudakis 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def loadGraphFromEdgeListTxt(file_name):
    f = open(file_name, 'r')
    G = nx.DiGraph()
    for line in f:
        edge = line.split(';')
        n1 = int(edge[0])
        n2 = int(edge[1].split('\n')[0])
        G.add_edge(n1, n2)
    return G
modorganizer.py 文件源码 项目:skymod 作者: DelusionalLogic 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def make_profile(self, local_repo):
        modlist_path = self.cfg.profile_dir / "modlist.txt"
        with open(modlist_path, "w") as f:
            G = nx.DiGraph()
            for package in local_repo.get_all_packages():
                G.add_node(package)
                for dep_name in package.dependecies:
                    q = Query(dep_name)
                    dep = local_repo.find_package(q)

                    if dep is None:
                        # Skip mising dependecies. If we have an installed
                        # package which has a not installed dependency, then we
                        # just want to skip it. It's up to the user to make
                        # sure everything is resolved, of course assisted by
                        # the tool.  @COMPLETE it might be useful give the user
                        # some way of doing a full dependency verification of
                        # the local repo
                        continue

                    G.add_edge(package, dep)
            for package in nx.lexicographical_topological_sort(
                    G,
                    key=lambda x: x.priority):
                print("+" + package.name, file=f)
            print("*Unmanaged: Dawnguard", file=f)
            print("*Unmanaged: Dragonborn", file=f)
            print("*Unmanaged: HearthFires", file=f)
            print("*Unmanaged: HighResTexturePack01", file=f)
            print("*Unmanaged: HighResTexturePack02", file=f)
            print("*Unmanaged: HighResTexturePack03", file=f)
            print("*Unmanaged: Unofficial Dawnguard Patch", file=f)
            print("*Unmanaged: Unofficial Dragonborn Patch", file=f)
            print("*Unmanaged: Unofficial Hearthfire Patch", file=f)
            print("*Unmanaged: Unofficial High Resolution Patch", file=f)
            print("*Unmanaged: Unofficial Skyrim Patch", file=f)
main.py 文件源码 项目:skymod 作者: DelusionalLogic 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def visualize():
    import networkx as nx
    # How about we don't fire up a JVM just to start my script?
    from asciinet import graph_to_ascii

    G = nx.DiGraph()
    root = "Root"

    G.add_node(root)

    # @ENHANCEMENT
    # Right now we just visualize it as a stright up dependency graph. We might
    # want to show when we use provides instead in the future. This would
    # involve adding a fake node when we look for a provides and let that
    # depend on the actual implementors
    for package in local_repo.get_all_packages():
        G.add_node(package)
        if package.reason == InstallReason.REQ:
            G.add_edge(root, package)
        for dep_str in package.dependecies:
            q = Query(dep_str)
            dep = local_repo.find_package(q)
            if dep is None:
                print(package, q)
            G.add_edge(package, dep)

    print(graph_to_ascii(G))


# Show details about a package
installed.py 文件源码 项目:skymod 作者: DelusionalLogic 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def from_depends(subtrees):
        G = nx.DiGraph()
        root = RootNode()
        for subG in subtrees:
            self.G = nx.compose(G, subG.G)
            self.G.add_edge(root, subG.root)
        return InstalledGraph(G, root)


问题


面经


文章

微信
公众号

扫码关注公众号