Python networkx.topological_sort() Examples

The following are 30 code examples of networkx.topological_sort(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: dagcircuit.py    From qiskit-terra with Apache License 2.0 7 votes vote down vote up
def from_networkx(cls, graph):
        """Take a networkx MultiDigraph and create a new DAGCircuit.

        Args:
            graph (networkx.MultiDiGraph): The graph to create a DAGCircuit
                object from. The format of this MultiDiGraph format must be
                in the same format as returned by to_networkx.

        Returns:
            DAGCircuit: The dagcircuit object created from the networkx
                MultiDiGraph.
        """

        dag = DAGCircuit()
        for node in nx.topological_sort(graph):
            if node.type == 'out':
                continue
            if node.type == 'in':
                dag._add_wire(node.wire)
            elif node.type == 'op':
                dag.apply_operation_back(node.op.copy(), node.qargs,
                                         node.cargs, node.condition)
        return dag 
Example #2
Source File: dependency_graph.py    From FlowKit with Mozilla Public License 2.0 6 votes vote down vote up
def store_queries_in_order(dependency_graph: nx.DiGraph) -> Dict[str, "Future"]:
    """
    Execute queries in an order that ensures each query store is triggered after its dependencies.

    Parameters
    ----------
    dependency_graph : networkx.DiGraph
        Dependency graph of query objects to be stored
    
    Returns
    -------
    dict
        Mapping from query nodes to Future objects representing the store tasks
    """
    ordered_list_of_queries = list(nx.topological_sort(dependency_graph))[::-1]
    logger.debug(f"Storing queries with IDs: {ordered_list_of_queries}")
    store_futures = {}
    for query in ordered_list_of_queries:
        try:
            store_futures[query] = dependency_graph.nodes[query]["query_object"].store()
        except UnstorableQueryError:
            # Some queries cannot be stored
            pass
    return store_futures 
Example #3
Source File: entangled_states.py    From forest-benchmarking with Apache License 2.0 6 votes vote down vote up
def create_ghz_program(tree: nx.DiGraph):
    """
    Create a Bell/GHZ state with CNOTs described by tree.

    :param tree: A tree that describes the CNOTs to perform to create a bell/GHZ state.
    :return: the program
    """
    assert nx.is_tree(tree), 'Needs to be a tree'
    nodes = list(nx.topological_sort(tree))
    n_qubits = len(nodes)
    program = Program(H(nodes[0]))

    for node in nodes:
        for child in tree.successors(node):
            program += CNOT(node, child)

    ro = program.declare('ro', 'BIT', n_qubits)
    for i, q in enumerate(nodes):
        program += MEASURE(q, ro[i])

    return program 
Example #4
Source File: runner.py    From openprescribing with MIT License 6 votes vote down vote up
def __iter__(self):
        if self._ordered:
            graph = nx.DiGraph()
            for task in self._tasks.values():
                graph.add_node(task)
                for dependency in task.dependencies:
                    graph.add_node(dependency)
                    graph.add_edge(dependency, task)
            tasks = nx.topological_sort(graph)
        else:
            tasks = [task for _, task in sorted(self._tasks.items())]

        for task in tasks:
            if self._type is None:
                yield task
            else:
                if self._type == task.task_type:
                    yield task 
Example #5
Source File: mcmc_sampler.py    From dowhy with MIT License 6 votes vote down vote up
def apply_parameters(self, g, df, initialization_trace=None):
        for node in nx.topological_sort(g):
            parent_names = g.nodes()[node]["parent_names"]
            if parent_names:
                if not initialization_trace:
                    sd = np.array([df[node].std()] + (df[node].std() / df[parent_names].std()).tolist())
                    mu = np.array([df[node].std()] + (df[node].std() / df[parent_names].std()).tolist())
                    node_sd = df[node].std()
                else:
                    node_sd = initialization_trace["{}_sd".format(node)].mean()
                    mu = initialization_trace["beta_{}".format(node)].mean(axis=0)
                    sd = initialization_trace["beta_{}".format(node)].std(axis=0)
                g.nodes()[node]["parameters"] = pm.Normal("beta_{}".format(node), mu=mu, sd=sd,
                                                          shape=len(parent_names) + 1)
                g.nodes()[node]["sd"] = pm.Exponential("{}_sd".format(node), lam=node_sd)
        return g 
Example #6
Source File: mcmc_sampler.py    From dowhy with MIT License 6 votes vote down vote up
def build_bayesian_network(self, g, df):
        for node in nx.topological_sort(g):
            if g.nodes()[node]["parent_names"]:
                mu = g.nodes()[node]["parameters"][0]  # intercept
                mu += pm.math.dot(df[g.nodes()[node]["parent_names"]],
                                  g.nodes()[node]["parameters"][1:])
                if g.nodes()[node]["variable_type"] == 'c':
                    sd = g.nodes()[node]["sd"]
                    g.nodes()[node]["variable"] = pm.Normal("{}".format(node),
                                                            mu=mu, sd=sd,
                                                            observed=df[node])
                elif g.nodes()[node]["variable_type"] == 'b':
                    g.nodes()[node]["variable"] = pm.Bernoulli("{}".format(node),
                                                               logit_p=mu,
                                                               observed=df[node])
                else:
                    raise Exception("Unrecognized variable type: {}".format(g.nodes()[node]["variable_type"]))
        return g 
Example #7
Source File: __init__.py    From bioconda-utils with MIT License 6 votes vote down vote up
def __init__(self, config: Dict, recipe_folder: str,
                 exclude: List[str] = None, nocatch: bool=False) ->None:
        self.config = config
        self.recipe_folder = recipe_folder
        self.skip = self.load_skips()
        self.exclude = exclude or []
        self.nocatch = nocatch
        self._messages = []

        dag = nx.DiGraph()
        dag.add_nodes_from(str(check) for check in get_checks())
        dag.add_edges_from(
            (str(check), str(check_dep))
            for check in get_checks()
            for check_dep in check.requires
        )
        self.checks_dag = dag

        try:
            self.checks_ordered = nx.topological_sort(dag, reverse=True)
        except nx.NetworkXUnfeasible:
            raise RunTimeError("Cycle in LintCheck requirements!")
        self.reload_checks() 
Example #8
Source File: graph_utils.py    From nucleus7 with Mozilla Public License 2.0 6 votes vote down vote up
def topological_sort_of_nucleotides(graph: nx.DiGraph) -> List[Nucleotide]:
    """
    Perform topological order of the graph

    Parameters
    ----------
    graph

    Returns
    -------
    sorted_nucleotides
        list of nucleotides sorted in topological order
    """
    nucleotides_without_inputs = [
        each_nucleotide for each_nucleotide in graph
        if not list(graph.predecessors(each_nucleotide))]
    nucleotides_without_inputs_sorted = sorted(
        nucleotides_without_inputs, key=lambda x: x.name)
    topological_order = list(nx.topological_sort(graph))
    topological_order_only_with_inputs = [
        each_nucleotide for each_nucleotide in topological_order
        if each_nucleotide not in nucleotides_without_inputs]
    topological_order_sorted = (nucleotides_without_inputs_sorted
                                + topological_order_only_with_inputs)
    return topological_order_sorted 
Example #9
Source File: qgraph.py    From qkeras with Apache License 2.0 6 votes vote down vote up
def CreateGraph(model, input_quantizers=None,
                default_source_quantizer=cfg.default_source_quantizer,
                debug=False):
  """create graph."""

  K.set_image_data_format("channels_last")

  (graph, source_quantizer_list) = GenerateGraphFromModel(
      model, input_quantizers, default_source_quantizer)
  GraphAddSingleSourceSingleSink(graph)
  GraphRemoveNodeWithNodeType(graph, "Dropout")
  GraphRemoveNodeWithNodeType(graph, "InputLayer")

  scheduler = list(nx.topological_sort(graph))

  if debug:
    for vertex in scheduler[1:-1]:
      for _, v in graph.edges(vertex):
        if v == SINK:
          continue
        print("... calling", graph.nodes[v][
            "layer"][0].name, graph.nodes[v]["type"])

  return (graph, source_quantizer_list) 
Example #10
Source File: test_dag.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_topological_sort3(self):
        DG = nx.DiGraph()
        DG.add_edges_from([(1, i) for i in range(2, 5)])
        DG.add_edges_from([(2, i) for i in range(5, 9)])
        DG.add_edges_from([(6, i) for i in range(9, 12)])
        DG.add_edges_from([(4, i) for i in range(12, 15)])

        def validate(order):
            ok_(isinstance(order, list))
            assert_equal(set(order), set(DG))
            for u, v in combinations(order, 2):
                assert_false(nx.has_path(DG, v, u))
        validate(nx.topological_sort_recursive(DG))
        validate(nx.topological_sort(DG))

        DG.add_edge(14, 1)
        assert_raises(nx.NetworkXUnfeasible, nx.topological_sort, DG)
        assert_raises(nx.NetworkXUnfeasible, nx.topological_sort_recursive, DG) 
Example #11
Source File: core.py    From kale with Apache License 2.0 6 votes vote down vote up
def print_pipeline(self, pipeline_graph):
        """Prints a complete definition of the pipeline with all the tags."""
        for block_name in nx.topological_sort(pipeline_graph):
            block_data = pipeline_graph.nodes(data=True)[block_name]

            print("Block: {}".format(block_name))
            print("Previous Blocks:")
            if 'previous_blocks' in block_data['tags']:
                pprint.pprint(block_data['tags']['previous_blocks'], width=1)
            print("Ins")
            if 'ins' in block_data:
                pprint.pprint(sorted(block_data['ins']), width=1)
            print("Outs")
            if 'outs' in block_data:
                pprint.pprint(sorted(block_data['outs']), width=1)
            print()
            print("-------------------------------")
            print() 
Example #12
Source File: dag.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def is_directed_acyclic_graph(G):
    """Return True if the graph G is a directed acyclic graph (DAG) or 
    False if not.

    Parameters
    ----------
    G : NetworkX graph
        A graph

    Returns
    -------
    is_dag : bool
        True if G is a DAG, false otherwise
    """
    if not G.is_directed():
        return False
    try:
        topological_sort(G, reverse=True)
        return True
    except nx.NetworkXUnfeasible:
        return False 
Example #13
Source File: compute_build_graph.py    From conda-concourse-ci with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def order_build(graph):
    '''
    Assumes that packages are in graph.
    Builds a temporary graph of relevant nodes and returns it topological sort.

    Relevant nodes selected in a breadth first traversal sourced at each pkg
    in packages.
    '''
    reorder_cyclical_test_dependencies(graph)
    try:
        order = list(nx.topological_sort(graph))
        order.reverse()
    except nx.exception.NetworkXUnfeasible:
        raise ValueError("Cycles detected in graph: %s", nx.find_cycle(graph))

    return order 
Example #14
Source File: acyclic_graph_generator.py    From CausalDiscoveryToolbox with MIT License 6 votes vote down vote up
def generate(self, rescale=True):
        """Generate data from an FCM defined in ``self.init_variables()``.

        Args:
            rescale (bool): rescale the generated data (recommended)

        Returns:
            tuple: (pandas.DataFrame, networkx.DiGraph), respectively the
            generated data and graph.
        """
        if self.cfunctions is None:
            self.init_variables()

        for i in nx.topological_sort(self.g):
            # Root cause

            if not sum(self.adjacency_matrix[:, i]):
                self.data['V{}'.format(i)] = self.cfunctions[i](self.npoints)
            # Generating causes
            else:
                self.data['V{}'.format(i)] = self.cfunctions[i](self.data.iloc[:, self.adjacency_matrix[:, i].nonzero()[0]].values)
            if rescale:
                self.data['V{}'.format(i)] = scale(self.data['V{}'.format(i)].values)

        return self.data, self.g 
Example #15
Source File: dag.py    From clusim with MIT License 6 votes vote down vote up
def to_dendropy_tree(self, taxon_namespace=None, weighted=False):
        import dendropy

        tree = dendropy.Tree(taxon_namespace=taxon_namespace)

        seed_node = self.roots()[0]

        if weighted:
            def edge_length(par, child):
                return np.abs(self.linkage_dist[par] -
                              self.linkage_dist[child])
        else:
            def edge_length(par, child): return 1.0

        tree_dict = {seed_node: tree.seed_node}
        for clus in nx.topological_sort(self):
            for child in self.successors(clus):
                tree_dict[child] = tree_dict[clus].new_child(
                    edge_length=edge_length(clus, child))

        for clus in self.leaves():
            tree_dict[clus].taxon = taxon_namespace.get_taxon(str(clus))

        return tree 
Example #16
Source File: run_hooks_hook.py    From flask-unchained with MIT License 6 votes vote down vote up
def resolve_hook_order(self, hook_tuples: List[HookTuple]) -> List[HookTuple]:
        dag = nx.DiGraph()

        for hook_tuple in hook_tuples:
            dag.add_node(hook_tuple.Hook.name, hook_tuple=hook_tuple)
            for dep_name in hook_tuple.Hook.run_after:
                dag.add_edge(hook_tuple.Hook.name, dep_name)
            for successor_name in hook_tuple.Hook.run_before:
                dag.add_edge(successor_name, hook_tuple.Hook.name)

        try:
            order = reversed(list(nx.topological_sort(dag)))
        except nx.NetworkXUnfeasible:
            msg = 'Circular dependency detected between hooks'
            problem_graph = ', '.join(f'{a} -> {b}'
                                      for a, b in nx.find_cycle(dag))
            raise Exception(f'{msg}: {problem_graph}')

        rv = []
        for hook_name in order:
            hook_tuple = dag.nodes[hook_name].get('hook_tuple')
            if hook_tuple:
                rv.append(hook_tuple)
        return rv 
Example #17
Source File: param.py    From st2 with Apache License 2.0 6 votes vote down vote up
def _resolve_dependencies(G):
    '''
    Traverse the dependency graph starting from resolved nodes
    '''
    context = {}
    for name in nx.topological_sort(G):
        node = G.node[name]
        try:
            context[name] = _render(node, context)

        except Exception as e:
            LOG.debug('Failed to render %s: %s', name, e, exc_info=True)
            msg = 'Failed to render parameter "%s": %s' % (name, six.text_type(e))
            raise ParamException(msg)

    return context 
Example #18
Source File: test_dag.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_reverse_topological_sort1(self):
        DG = nx.DiGraph()
        DG.add_edges_from([(1, 2), (1, 3), (2, 3)])
        assert_equal(nx.topological_sort(DG, reverse=True), [3, 2, 1])
        assert_equal(
            nx.topological_sort_recursive(DG, reverse=True), [3, 2, 1])

        DG.add_edge(3, 2)
        assert_raises(nx.NetworkXUnfeasible,
                      nx.topological_sort, DG, reverse=True)
        assert_raises(nx.NetworkXUnfeasible,
                      nx.topological_sort_recursive, DG, reverse=True)

        DG.remove_edge(2, 3)
        assert_equal(nx.topological_sort(DG, reverse=True), [2, 3, 1])
        assert_equal(
            nx.topological_sort_recursive(DG, reverse=True), [2, 3, 1]) 
Example #19
Source File: compute_build_graph.py    From staged-recipes with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def order_build(graph):
    '''
    Assumes that packages are in graph.
    Builds a temporary graph of relevant nodes and returns it topological sort.

    Relevant nodes selected in a breadth first traversal sourced at each pkg
    in packages.
    '''
    reorder_cyclical_test_dependencies(graph)
    try:
        order = list(nx.topological_sort(graph))
        order.reverse()
    except nx.exception.NetworkXUnfeasible:
        raise ValueError("Cycles detected in graph: %s", nx.find_cycle(graph,
                                                                       orientation='reverse'))

    return order 
Example #20
Source File: _base.py    From sumpy with Apache License 2.0 5 votes vote down vote up
def build_pipeline(self):
        self.build_dependency_graph()
        self._pipeline = []
        for node in nx.topological_sort(self._dependency_graph):
            if node in self._annotators:
                self._pipeline.append(self._annotators[node])
                if self.verbose:
                    print "{} ({}) build".format(self.__class__.__name__,
                        self._annotators[node].name(self))
                self._annotators[node].build(self) 
Example #21
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_dependency(self,
                       from_name,
                       to_name,
                       from_key="default",
                       to_key="default"):
        """
        adds a computation graph dependency from a node with "from_name" as a
        name to a node with "to_name" as a name

        from_key: key in output of from-node to be passed into to-node

        to_key: key to-node will use to query the dependency - each input to
        a node must have a unique to_key
        """
        # make sure network is mutable
        assert self.is_mutable
        # make sure that these nodes are part of this graph
        assert from_name in self.name_to_node
        assert to_name in self.name_to_node
        # make sure that to_key is unique for to-node
        for _, edge_to, datamap in self.computation_graph.edges(data=True):
            if edge_to == to_name and datamap.get("to_key") == to_key:
                raise ValueError("Non-unique to_key(%s) found for node %s"
                                 % (to_key, to_name))
        # add the dependency
        self.computation_graph.add_edge(from_name,
                                        to_name,
                                        from_key=from_key,
                                        to_key=to_key)
        # make sure that the dependency doesn't cause any cycles
        try:
            nx.topological_sort(self.computation_graph)
        except nx.NetworkXUnfeasible:
            # TODO this might not be sufficient, since an edge between
            # from_name to to_name might have existed before this operation
            self.computation_graph.remove_edge(from_name, to_name)
            # TODO maybe use a custom exception
            raise 
Example #22
Source File: gridlabd.py    From ditto with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __create():
    dir_path = os.path.dirname(os.path.realpath(__file__))

    with open(os.path.join(dir_path, "schema.json")) as f:
        string = f.read()

    schema = json.loads(string)
    klasses = OrderedDict()

    G = nx.DiGraph()
    for i in schema["objects"]:
        parent = schema["objects"][i]["parent"]
        if parent is not None:
            G.add_edge(parent, i)
        else:
            G.add_node(i)

    def generate_class(klass, properties, parent=None):
        if parent is None:
            parent = GridLABDBase

        return type(
            common_str(klass), (parent,), dict(_properties=properties["properties"])
        )

    for klass in nx.topological_sort(G):
        properties = schema["objects"][klass]

        if properties["parent"] is not None:
            parent = klasses[properties["parent"]]
        else:
            parent = None

        c = generate_class(klass, properties, parent=parent)
        klasses[klass] = c

    for k, c in klasses.items():
        globals()[k] = c 
Example #23
Source File: task_graph.py    From incubator-ariatosca with Apache License 2.0 5 votes vote down vote up
def topological_order(self, reverse=False):
        """
        Topological sort of the graph.

        :param reverse: whether to reverse the sort
        :return: list which represents the topological sort
        """
        task_ids = topological_sort(self._graph)
        if reverse:
            task_ids = reversed(tuple(task_ids))
        for task_id in task_ids:
            yield self.get_task(task_id) 
Example #24
Source File: io.py    From osmnx with MIT License 5 votes vote down vote up
def _get_unique_nodes_ordered_from_way(df_way_edges):
    """
    Recover original node order from df of edges associated w/ single OSM way.

    Parameters
    ----------
    df_way_edges : pandas.DataFrame
        Dataframe containing columns 'u' and 'v' corresponding to
        origin/destination nodes.

    Returns
    -------
    unique_ordered_nodes : list
        An ordered list of unique node IDs.
        Note: If the edges do not all connect (e.g. [(1, 2), (2,3),
        (10, 11), (11, 12), (12, 13)]), then this method will return
        only those nodes associated with the largest component of
        connected edges, even if subsequent connected chunks are contain
        more total nodes. This is done to ensure a proper topological
        representation of nodes in the XML way records because if there
        are unconnected components, the sorting algorithm cannot recover
        their original order. We would not likely ever encounter this
        kind of disconnected structure of nodes within a given way, but
        it is not explicitly forbidden in the OSM XML design schema.
    """
    G = nx.MultiDiGraph()
    all_nodes = list(df_way_edges["u"].values) + list(df_way_edges["v"].values)

    G.add_nodes_from(all_nodes)
    G.add_edges_from(df_way_edges[["u", "v"]].values)

    # copy nodes into new graph
    H = utils_graph.get_largest_component(G, strongly=False)
    unique_ordered_nodes = list(nx.topological_sort(H))
    num_unique_nodes = len(np.unique(all_nodes))

    if len(unique_ordered_nodes) < num_unique_nodes:
        utils.log(f"Recovered order for {len(unique_ordered_nodes)} of {num_unique_nodes} nodes")

    return unique_ordered_nodes 
Example #25
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _nodes(self, order=None):
        """
        returns all nodes in the graph
        """
        if order is None:
            node_names = self.name_to_node.keys()
        elif order == "architecture":
            node_names = nx.topological_sort(self.architectural_tree)
        elif order == "computation":
            node_names = nx.topological_sort(self.computation_graph)
        else:
            raise ValueError("Unknown order: %s" % order)
        # make sure that all of the original nodes are returned
        assert set(self.name_to_node.keys()) == set(node_names)
        return [self.name_to_node[name] for name in node_names] 
Example #26
Source File: utils.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def sort_notebooks(
    notebooks: Dict[str, Dict[str, Any]]
) -> OrderedDict[str, Dict[str, Any]]:
    """
    Perform a topological sort on a dictionary of notebook task specifications.

    Parameters
    ----------
    notebooks : dict
        Dictionary of dictionaries describing notebook tasks.
    
    Returns
    -------
    OrderedDict
        Ordered dict of notebook task dicts, ordered so that no notebook depends on another
        notebook that comes after it.
    
    Raises
    ------
    ValueError
        If the notebook specifications contain circular dependencies.
    """
    notebooks_graph = nx.DiGraph(
        {
            key: [
                value
                for value in notebooks[key].get("parameters", {}).values()
                if value in notebooks
            ]
            for key in notebooks
        }
    ).reverse()
    try:
        sorted_notebook_keys = list(nx.topological_sort(notebooks_graph))
    except nx.NetworkXUnfeasible:
        raise ValueError("Notebook specifications contain circular dependencies.")

    return collections.OrderedDict(
        (key, notebooks[key]) for key in sorted_notebook_keys
    ) 
Example #27
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_dependency(self,
                       from_name,
                       to_name,
                       from_key="default",
                       to_key="default"):
        """
        adds a computation graph dependency from a node with "from_name" as a
        name to a node with "to_name" as a name

        from_key: key in output of from-node to be passed into to-node

        to_key: key to-node will use to query the dependency - each input to
        a node must have a unique to_key
        """
        # make sure network is mutable
        assert self.is_mutable
        # make sure that these nodes are part of this graph
        assert from_name in self.name_to_node
        assert to_name in self.name_to_node
        # make sure that to_key is unique for to-node
        for _, edge_to, datamap in self.computation_graph.edges(data=True):
            if edge_to == to_name and datamap.get("to_key") == to_key:
                raise ValueError("Non-unique to_key(%s) found for node %s"
                                 % (to_key, to_name))
        # add the dependency
        self.computation_graph.add_edge(from_name,
                                        to_name,
                                        from_key=from_key,
                                        to_key=to_key)
        # make sure that the dependency doesn't cause any cycles
        try:
            nx.topological_sort(self.computation_graph)
        except nx.NetworkXUnfeasible:
            # TODO this might not be sufficient, since an edge between
            # from_name to to_name might have existed before this operation
            self.computation_graph.remove_edge(from_name, to_name)
            # TODO maybe use a custom exception
            raise 
Example #28
Source File: graph_generator.py    From RandWire_tensorflow with MIT License 5 votes vote down vote up
def graph_generator(model, graph_param, save_path, file_name):
    graph_param[0] = int(graph_param[0])
    if model == 'ws':
        graph_param[1] = int(graph_param[1])
        graph = nx.random_graphs.connected_watts_strogatz_graph(*graph_param)
    elif model == 'er':
        graph = nx.random_graphs.erdos_renyi_graph(*graph_param)
    elif model == 'ba':
        graph_param[1] = int(graph_param[1])
        graph = nx.random_graphs.barabasi_albert_graph(*graph_param)

    if os.path.isfile(save_path + '/' + file_name + '.yaml') is True:
        print('graph loaded')
        dgraph = nx.read_yaml(save_path + '/' + file_name + '.yaml')

    else:
        dgraph = nx.DiGraph()
        dgraph.add_nodes_from(graph.nodes)
        dgraph.add_edges_from(graph.edges)

    in_node = []
    out_node = []
    for indeg, outdeg in zip(dgraph.in_degree, dgraph.out_degree):
        if indeg[1] == 0:
            in_node.append(indeg[0])
        elif outdeg[1] == 0:
            out_node.append(outdeg[0])
    # print(in_node, out_node)
    sorted = list(nx.topological_sort(dgraph))
    # nx.draw(dgraph)
    # plt.draw()
    # plt.show()

    if os.path.isdir(save_path) is False:
        os.makedirs(save_path)

    if os.path.isfile(save_path + '/' + file_name + '.yaml') is False:
        print('graph_saved')
        nx.write_yaml(dgraph, save_path + '/' + file_name + '.yaml')

    return dgraph, sorted, in_node, out_node 
Example #29
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _nodes(self, order=None):
        """
        returns all nodes in the graph
        """
        if order is None:
            node_names = self.name_to_node.keys()
        elif order == "architecture":
            node_names = nx.topological_sort(self.architectural_tree)
        elif order == "computation":
            node_names = nx.topological_sort(self.computation_graph)
        else:
            raise ValueError("Unknown order: %s" % order)
        # make sure that all of the original nodes are returned
        assert set(self.name_to_node.keys()) == set(node_names)
        return [self.name_to_node[name] for name in node_names] 
Example #30
Source File: build_all.py    From staged-recipes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build_folders(recipes_dir, folders, arch, channel_urls):

    index_path = os.path.join(sys.exec_prefix, 'conda-bld')
    os.makedirs(index_path, exist_ok=True)
    conda_build.api.update_index(index_path)
    index = conda_build.conda_interface.get_index(channel_urls=channel_urls)
    conda_resolve = conda_build.conda_interface.Resolve(index)

    config = get_config(arch, channel_urls)
    platform = get_host_platform()

    worker = {'platform': platform, 'arch': arch,
              'label': '{}-{}'.format(platform, arch)}

    G = construct_graph(recipes_dir, worker=worker, run='build',
                        conda_resolve=conda_resolve, folders=folders,
                        config=config, finalize=False)
    order = list(nx.topological_sort(G))
    order.reverse()

    print('Computed that there are {} distributions to build from {} recipes'
          .format(len(order), len(folders)))
    if not order:
        print('Nothing to do')
        return
    print("Resolved dependencies, will be built in the following order:")
    print('    '+'\n    '.join(order))

    d = OrderedDict()
    for node in order:
        d[G.node[node]['meta'].meta_path] = 1

    for recipe in d.keys():
        conda_build.api.build([recipe], config=get_config(arch, channel_urls))