How to use the networkx.topological_sort function in networkx

To help you get started, we’ve selected a few networkx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Oshlack / Lace / Streamline.py View on Github external
#			#print(G.node[b]['Base'])
#			seq = seq + G.node[b]['Base']
#		#print(seq)
#		path_alternatives.append(seq)

#	#Loop through and check the strings match
#	for path in path_alternatives:
#		if(path == transcripts[key]): print("Found path for: ", key)
		


#Order base in graph
#base_order = nx.topological_sort(G)
#Will crash if there is a cycle, therefore do a try
try:
	base_order = nx.topological_sort(G)
except nx.NetworkXUnfeasible:
	print("CYCLESSSSSS!!!!!!")
	sys.exit()

seq =''
for index in base_order:
	seq = seq + G.node[index]['Base']
print(seq)	

#Save sequence to file
superf = open('Super.fasta','w')
superf.write('>Super\n')
superf.write(seq)
superf.close()
github nipy / nipype / nipype / pipeline / engine / utils.py View on Github external
def _identity_nodes(graph, include_iterables):
    """Return the IdentityInterface nodes in the graph

    The nodes are in topological sort order. The iterable nodes
    are included if and only if the include_iterables flag is set
    to True.
    """
    return [
        node for node in nx.topological_sort(graph)
        if isinstance(node.interface, IdentityInterface) and (
            include_iterables or getattr(node, 'iterables') is None)
    ]
github StackStorm / st2 / st2common / st2common / util / param.py View on Github external
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
github wolfv / pyjet / jet / compressor.py View on Github external
self.graph = expander.graph
        self.args = [] if args is None else [arg.producer for arg in args]
        self.ops = []
        self.variables = []
        self.constants = []
        self.Op = OpCollector()
        self.file_name = file_name
        self.fun_name = fun_name + 'Func'
        self.class_name = fun_name.title() + 'Class'
        self.extract_return(out)

        # extract sequential list, starting at output
        current_nodes = self._extract_nodes_for_return(out)
        self.subgraph = self.graph.subgraph(current_nodes)
        topo_sorted = nx.topological_sort(self.subgraph)
        if not config.debug and config.merge:
            self._merge_ops(self.subgraph, topo_sorted)

        for el in topo_sorted:
            if el not in current_nodes:
                continue
            if el.op == 'Placeholder':
                if args is None:
                    self.args.append(el)
            elif el.op == 'Variable':
                self.variables.append(el)
            elif el.op == 'Const':
                self.constants.append(el)
            else:
                self.ops.append(el)
github ARMmbed / mbed-cloud-sdk-python / scripts / generate_ci_config.py View on Github external
)

    workflow.add_edge(
        release_name(release_target_map['prod']),
        deploy_name(python_versions['three'], release_target_map['prod'])
    )
	
    workflow.add_edge(
        release_name(release_target_map['prod']),
        deploy_docker_name(python_versions['three'], release_target_map['prod']),
    )

    workflow_jobs = base['workflows']['python_sdk_workflow']['jobs']

    # build the workflow graph
    for job_name in networkx.topological_sort(workflow):
        job_config = {}
        per_node_config = workflow.nodes[job_name].get('workflow')
        if per_node_config:
            job_config.update(per_node_config)
        workflow_jobs.append({job_name: job_config})
        for edge in workflow.in_edges(job_name):
            job_config.update(workflow.get_edge_data(*edge))
            job_config.setdefault('requires', []).append(edge[0])

    LOG.info('%s circle jobs', len(base['jobs']))
    return dict(base)
github estnltk / estnltk / estnltk / resolve_layer_dag.py View on Github external
def list_layers(self):
        return nx.topological_sort(self.graph)
github nipy / nipype / nipype / pipeline / engine / utils.py View on Github external
def get_levels(G):
    levels = {}
    for n in nx.topological_sort(G):
        levels[n] = 0
        for pred in G.predecessors(n):
            levels[n] = max(levels[n], levels[pred] + 1)
    return levels
github tanghaibao / jcvi / jcvi / algorithms / lpsolve.py View on Github external
def edges_to_path(edges):
    """
    Connect edges and return a path.
    """
    if not edges:
        return None

    G = edges_to_graph(edges)
    path = nx.topological_sort(G)
    return path
github jythontools / jython / Tools / pbcvm / extract.py View on Github external
def extract(self):
        mod = self.mod
        writer = self.writer
        functionobjs = self.candidate_functions()
        for name, f in functionobjs:
            self.extract_code_obj(f)

        print >> writer, "from %s import *" % mod.__name__
        print >> writer, "from org.python.core import PyBytecode"
        print >> writer
        print >> writer, "_codeobjs = {}"
        print >> writer

        objs = networkx.topological_sort(self.depend)
        for obj in objs:
            if not inspect.iscode(obj):
                continue
            name = self.codeobjs_names[obj]
            print >> writer, "_codeobjs[%r] = %s" % (name, self.codeobjs[obj])
            print >> writer
        for name, f in functionobjs:
            # this may be a Jython diff, need to determine further; need to check if im_func or not on the object
            print >> writer, "try: %s.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code])
            print >> writer, "except (AttributeError, ValueError): pass" # ignore setting cells, im_func, etc... %s.im_func.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code])
    
        print >> writer
        print >> writer, 'if __name__ == "__main__":'
        print >> writer, '    test_main()'
github microsoft / dowhy / dowhy / do_samplers / mcmc_sampler.py View on Github external
def apply_data_types(self, g, data_types):
        for node in nx.topological_sort(g):
            g.nodes()[node]["variable_type"] = data_types[node]
        return g