Python networkx.ancestors() Examples

The following are 30 code examples of networkx.ancestors(). 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: causal_graph.py    From dowhy with MIT License 6 votes vote down vote up
def get_common_causes(self, nodes1, nodes2):
        """
        Assume that nodes1 causes nodes2 (e.g., nodes1 are the treatments and nodes2 are the outcomes)
        """
        # TODO Refactor to remove this from here and only implement this logic in causalIdentifier. Unnecessary assumption of nodes1 to be causing nodes2.
        nodes1 = parse_state(nodes1)
        nodes2 = parse_state(nodes2)
        causes_1 = set()
        causes_2 = set()
        for node in nodes1:
            causes_1 = causes_1.union(self.get_ancestors(node))
        for node in nodes2:
            # Cannot simply compute ancestors, since that will also include nodes1 and its parents (e.g. instruments)
            parents_2 = self.get_parents(node)
            for parent in parents_2:
                if parent not in nodes1:
                    causes_2 = causes_2.union(set([parent,]))
                    causes_2 = causes_2.union(self.get_ancestors(parent))
        return list(causes_1.intersection(causes_2)) 
Example #2
Source File: network.py    From graphkit with Apache License 2.0 6 votes vote down vote up
def ready_to_schedule_operation(op, has_executed, graph):
    """
    Determines if a Operation is ready to be scheduled for execution based on
    what has already been executed.

    Args:
        op:
            The Operation object to check
        has_executed: set
            A set containing all operations that have been executed so far
        graph:
            The networkx graph containing the operations and data nodes
    Returns:
        A boolean indicating whether the operation may be scheduled for
        execution based on what has already been executed.
    """
    dependencies = set(filter(lambda v: isinstance(v, Operation),
                              nx.ancestors(graph, op)))
    return dependencies.issubset(has_executed) 
Example #3
Source File: semsearch.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def calculate_mrcas(self, c1 : ClassId, c2 : ClassId) -> Set[ClassId]:
        """
        Calculate the MRCA for a class pair
        """
        G = self.G
        # reflexive ancestors
        ancs1 = self._ancestors(c1) | {c1}
        ancs2 = self._ancestors(c2) | {c2}
        common_ancestors = ancs1 & ancs2
        redundant = set()
        for a in common_ancestors:
            redundant = redundant | nx.ancestors(G, a)
        return common_ancestors - redundant

    #def calculate_mrcas_ic(self, c1 : ClassId, c2 : ClassId) -> InformationContent, Set[ClassId]:
    #    mrcas = self.calculate_mrcas(c1, c2) 
Example #4
Source File: graph.py    From treeano with Apache License 2.0 5 votes vote down vote up
def architecture_ancestors(self, node_name):
        """
        returns a generator of ancestors of the current node in the
        architectural tree, in the order of being closer to the node
        towards the root
        """
        for ancestor_name in self.architecture_ancestor_names(node_name):
            yield self.name_to_node[ancestor_name] 
Example #5
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_subtree_names(self, node_name):
        """
        returns an unordered set of descendant names of the current node in the
        architectural tree
        """
        # NOTE: this is actually the descendants, despite the name, because
        # of the way we set up the tree
        descendant_names = nx.ancestors(self.architectural_tree, node_name)
        subtree_names = descendant_names | {node_name}
        return subtree_names 
Example #6
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_ancestors(self, node_name):
        """
        returns a generator of ancestors of the current node in the
        architectural tree, in the order of being closer to the node
        towards the root
        """
        for ancestor_name in self.architecture_ancestor_names(node_name):
            yield self.name_to_node[ancestor_name] 
Example #7
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_ancestors(self, node_name):
        """
        returns a generator of ancestors of the current node in the
        architectural tree, in the order of being closer to the node
        towards the root
        """
        for ancestor_name in self.architecture_ancestor_names(node_name):
            yield self.name_to_node[ancestor_name] 
Example #8
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_subtree_names(self, node_name):
        """
        returns an unordered set of descendant names of the current node in the
        architectural tree
        """
        # NOTE: this is actually the descendants, despite the name, because
        # of the way we set up the tree
        descendant_names = nx.ancestors(self.architectural_tree, node_name)
        subtree_names = descendant_names | {node_name}
        return subtree_names 
Example #9
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_ancestors(self, node_name):
        """
        returns a generator of ancestors of the current node in the
        architectural tree, in the order of being closer to the node
        towards the root
        """
        for ancestor_name in self.architecture_ancestor_names(node_name):
            yield self.name_to_node[ancestor_name] 
Example #10
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_subtree_names(self, node_name):
        """
        returns an unordered set of descendant names of the current node in the
        architectural tree
        """
        # NOTE: this is actually the descendants, despite the name, because
        # of the way we set up the tree
        descendant_names = nx.ancestors(self.architectural_tree, node_name)
        subtree_names = descendant_names | {node_name}
        return subtree_names 
Example #11
Source File: SpreadingActivation.py    From Quadflor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def transform(self, X, y=None):
        ''' From each value in the feature matrix,
        traverse upwards in the hierarchy (including multiple parents in DAGs),
        and set all nodes to one'''
        hierarchy = self.hierarchy
        X_out = np.zeros(X.shape, dtype=np.bool_)
        samples, relevant_topics, _ = sp.find(X)
        for sample, topic in zip(samples, relevant_topics):
            X_out[sample, topic] = 1
            ancestors = nx.ancestors(hierarchy, topic)
            for ancestor in ancestors:
                X_out[sample, ancestor] = 1

        return X_out 
Example #12
Source File: graph.py    From flo with MIT License 5 votes vote down vote up
def subgraph_needed_for(self, start_at, end_at):
        """Find the subgraph of all dependencies to run these tasks. Returns a
        new graph.
        """
        assert start_at or end_at, "one of {start_at,end_at} must be a task id"
        start, end = map(self.task_dict.get, [start_at, end_at])
        if None in [start, end]:
            graph = self.get_networkx_graph()
            if start:
                task_subset = nx.descendants(graph, start)
                task_subset.add(start)
            elif end:
                task_subset = nx.ancestors(graph, end)
                task_subset.add(end)
        elif start == end:
            task_subset = set([start])
        else:
            graph = self.get_networkx_graph()
            task_subset = set()
            for path in nx.all_simple_paths(graph, start, end):
                task_subset.update(path)

        # make sure the tasks are added to the subgraph in the same
        # order as the original configuration file
        tasks_kwargs_list = [task.yaml_data for task in self.task_list
                             if task in task_subset]
        subgraph = TaskGraph(self.config_path, tasks_kwargs_list)
        return subgraph 
Example #13
Source File: RandWire.py    From RandWire_tensorflow with MIT License 5 votes vote down vote up
def build_stage(input, filters, dropout_rate, training, graph_data, scope):
    graph, graph_order, start_node, end_node = graph_data

    interms = {}
    with tf.variable_scope(scope):
        for node in graph_order:
            if node in start_node:
                interm = conv_block(input, 3, filters, 2, dropout_rate, training, scope='node' + str(node))
                interms[node] = interm
            else:
                in_node = list(nx.ancestors(graph, node))
                if len(in_node) > 1:
                    with tf.variable_scope('node' + str(node)):
                        weight = tf.get_variable('sum_weight', shape=len(in_node), dtype=tf.float32, constraint=lambda x: tf.clip_by_value(x, 0, np.infty))
                        weight = tf.nn.sigmoid(weight)
                        interm = weight[0] * interms[in_node[0]]
                        for idx in range(1, len(in_node)):
                            interm += weight[idx] * interms[in_node[idx]]
                        interm = conv_block(interm, 3, filters, 1, dropout_rate, training, scope='conv_block' + str(node))
                        interms[node] = interm
                elif len(in_node) == 1:
                    interm = conv_block(interms[in_node[0]], 3, filters, 1, dropout_rate, training, scope='node' + str(node))
                    interms[node] = interm

        output = interms[end_node[0]]
        for idx in range(1, len(end_node)):
            output += interms[end_node[idx]]

        return output 
Example #14
Source File: graph.py    From bioconda-utils with MIT License 5 votes vote down vote up
def filter_recipe_dag(dag, include, exclude):
    """Reduces **dag** to packages in **names** and their requirements"""
    nodes = set()
    for recipe in dag:
        if (recipe not in nodes
            and any(fnmatch(recipe.reldir, p) for p in include)
            and not any(fnmatch(recipe.reldir, p) for p in exclude)):
            nodes.add(recipe)
            nodes |= nx.ancestors(dag, recipe)
    return nx.subgraph(dag, nodes) 
Example #15
Source File: graph.py    From treeano with Apache License 2.0 5 votes vote down vote up
def architecture_subtree_names(self, node_name):
        """
        returns an unordered set of descendant names of the current node in the
        architectural tree
        """
        # NOTE: this is actually the descendants, despite the name, because
        # of the way we set up the tree
        descendant_names = nx.ancestors(self.architectural_tree, node_name)
        subtree_names = descendant_names | {node_name}
        return subtree_names 
Example #16
Source File: cfg.py    From polyfile with Apache License 2.0 5 votes vote down vote up
def ancestors(self, node) -> set:
        return nx.ancestors(self, node) 
Example #17
Source File: causal_graph.py    From dowhy with MIT License 5 votes vote down vote up
def get_ancestors(self, node_name, new_graph=None):
        if new_graph is None:
            graph=self._graph
        else:
            graph=new_graph
        return set(nx.ancestors(graph, node_name)) 
Example #18
Source File: causal_graph.py    From dowhy with MIT License 5 votes vote down vote up
def get_instruments(self, treatment_nodes, outcome_nodes):
        treatment_nodes = parse_state(treatment_nodes)
        outcome_nodes = parse_state(outcome_nodes)
        parents_treatment = set()
        for node in treatment_nodes:
            parents_treatment = parents_treatment.union(self.get_parents(node))
        g_no_parents_treatment = self.do_surgery(treatment_nodes,
                                                 remove_incoming_edges=True)
        ancestors_outcome = set()
        for node in outcome_nodes:
            ancestors_outcome = ancestors_outcome.union(nx.ancestors(g_no_parents_treatment, node))

        # [TODO: double check these work with multivariate implementation:]
        # Exclusion
        candidate_instruments = parents_treatment.difference(ancestors_outcome)
        self.logger.debug("Candidate instruments after exclusion %s",
                          candidate_instruments)
        # As-if-random setup
        children_causes_outcome = [nx.descendants(g_no_parents_treatment, v)
                                   for v in ancestors_outcome]
        children_causes_outcome = set([item
                                       for sublist in children_causes_outcome
                                       for item in sublist])

        # As-if-random
        instruments = candidate_instruments.difference(children_causes_outcome)
        return list(instruments) 
Example #19
Source File: dag.py    From sos with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def regenerate_target(self, target: BaseTarget):
        if target in self._all_output_files:
            for node in self._all_output_files[target]:
                if node._status == 'completed':
                    if isinstance(target, sos_step):
                        if env.config['error_mode'] == 'ignore':
                            raise RuntimeError(
                                f'Target {target} that was failed to generate is needed to continue.'
                            )
                        else:
                            raise RuntimeError(
                                f'Completed target {target} is being re-executed. Please report this bug to SoS developers.'
                            )
                    else:
                        env.logger.info(
                            f'Re-running {node._node_id} to generate {target}')
                        node._status = None
            return True
        else:
            return False

    # def subgraph_from(self, targets: sos_targets):
    #     '''Trim DAG to keep only nodes that produce targets'''
    #     if 'DAG' in env.config['SOS_DEBUG'] or 'ALL' in env.config['SOS_DEBUG']:
    #         env.log_to_file('DAG', 'create subgraph')
    #     # first, find all nodes with targets
    #     subnodes = []
    #     for node in self.nodes():
    #         if node._output_targets.valid() and any(
    #                 x in node._output_targets for x in targets):
    #             subnodes.append(node)
    #     #
    #     ancestors = set()
    #     for node in subnodes:
    #         ancestors |= nx.ancestors(self, node)
    #     return SoS_DAG(nx.subgraph(self, subnodes + list(ancestors))) 
Example #20
Source File: dag.py    From feagen with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_subgraph_with_ancestors(self, nodes):
        subgraph_nodes = set(nodes)
        for node in nodes:
            subgraph_nodes |= nx.ancestors(self._nx_dag, node)
        return self._nx_dag.subgraph(subgraph_nodes) 
Example #21
Source File: graph.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def architecture_subtree_names(self, node_name):
        """
        returns an unordered set of descendant names of the current node in the
        architectural tree
        """
        # NOTE: this is actually the descendants, despite the name, because
        # of the way we set up the tree
        descendant_names = nx.ancestors(self.architectural_tree, node_name)
        subtree_names = descendant_names | {node_name}
        return subtree_names 
Example #22
Source File: autobump.py    From bioconda-utils with MIT License 5 votes vote down vote up
def apply(self, recipe: Recipe) -> None:
        pending_deps = [
            dep for dep in nx.ancestors(self.dag, recipe)
            if dep.is_modified()
        ]
        if pending_deps:
            msg =  ", ".join(str(x) for x in pending_deps)
            raise self.DependencyPending(recipe, msg) 
Example #23
Source File: graph.py    From bioconda-utils with MIT License 5 votes vote down vote up
def filter(dag, packages):
    nodes = set()
    for package in packages:
        if package in nodes:
            continue  # already got all ancestors
        nodes.add(package)
        try:
            nodes |= nx.ancestors(dag, package)
        except nx.exception.NetworkXError:
            if package not in nx.nodes(dag):
                logger.error("Can't find %s in dag", package)
            else:
                raise

    return nx.subgraph(dag, nodes) 
Example #24
Source File: nx_edge_augmentation.py    From ibeis with Apache License 2.0 5 votes vote down vote up
def _lowest_common_anscestor(T, u, v, root):
    # Find a least common anscestors
    v_branch = nx.ancestors(T, v).union({v})
    u_branch = nx.ancestors(T, u).union({u})
    common = v_branch & u_branch
    if len(common) == 0:
        lca = None
    else:
        lca = max(
            (nx.shortest_path_length(T, root, c), c)
            for c in common
        )[1]
    return lca 
Example #25
Source File: lca.py    From Sark with MIT License 5 votes vote down vote up
def OnClick(self, node_id):
        self.color_nodes()
        self._current_node_id = node_id
        node_ea = self[node_id]

        self._remove_target_handler.unregister()
        self._disable_source_handler.unregister()
        self._enable_source_handler.unregister()

        if node_ea in self._targets:
            self._remove_target_handler.register()
            self._attach_to_popup(self._remove_target_handler.get_name())

            for ea in nx.ancestors(self._lca_graph, node_ea):
                if ea not in self._targets and ea not in self._sources:
                    self._set_node_bg_color(self._node_ids[ea], COLOR_PATH)

        if node_ea in self._sources:
            if node_ea in self._disabled_sources:
                self._enable_source_handler.register()
                self._attach_to_popup(self._enable_source_handler.get_name())
            else:
                self._disable_source_handler.register()
                self._attach_to_popup(self._disable_source_handler.get_name())

                for ea in nx.descendants(self._lca_graph, node_ea):
                    if ea not in self._targets and ea not in self._sources:
                        self._set_node_bg_color(self._node_ids[ea], COLOR_PATH)

        return False 
Example #26
Source File: lca.py    From Sark with MIT License 5 votes vote down vote up
def lca_viewer_starter(lca_plugin):
    class LCAViewerStarter(sark.ui.ActionHandler):
        TEXT = "LCA Graph"
        TOOLTIP = "Show an interactive lowest-common-ancestors graph."

        def _activate(self, ctx):
            lca_plugin.show_graph()

    return LCAViewerStarter 
Example #27
Source File: test_highlevel.py    From tskit with MIT License 5 votes vote down vote up
def verify_nx_for_tutorial_algorithms(self, tree, g):
        # traversing upwards
        for u in tree.leaves():
            path = []
            v = u
            while v != tskit.NULL:
                path.append(v)
                v = tree.parent(v)

            self.assertSetEqual(set(path), {u} | nx.ancestors(g, u))
            self.assertEqual(
                path,
                [u] + [n1 for n1, n2, _ in nx.edge_dfs(g, u, orientation="reverse")],
            )

        # traversals with information
        def preorder_dist(tree, root):
            stack = [(root, 0)]
            while len(stack) > 0:
                u, distance = stack.pop()
                yield u, distance
                for v in tree.children(u):
                    stack.append((v, distance + 1))

        for root in tree.roots:
            self.assertDictEqual(
                {k: v for k, v in preorder_dist(tree, root)},
                nx.shortest_path_length(g, source=root),
            )

        for root in tree.roots:
            # new traversal: measuring time between root and MRCA
            for u, v in itertools.combinations(nx.descendants(g, root), 2):
                mrca = tree.mrca(u, v)
                tmrca = tree.time(mrca)
                self.assertAlmostEqual(
                    tree.time(root) - tmrca,
                    nx.shortest_path_length(
                        g, source=root, target=mrca, weight="branch_length"
                    ),
                ) 
Example #28
Source File: semsearch.py    From ontobio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, assocmodel=None):
        self.assocmodel = assocmodel # type: AssociationSet
        self.assoc_df = assocmodel.as_dataframe()
        # TODO: test for cyclicity
        self.G = assocmodel.ontology.get_graph()
        self.ics = None # Optional
        # TODO: class x class df
        self.ancmap = {}
        for c in self.G.nodes():
            self.ancmap[c] = nx.ancestors(self.G, c) 
Example #29
Source File: ontol.py    From ontobio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def filter_redundant(self, ids):
        """
        Return all non-redundant ids from a list
        """
        sids = set(ids)
        for id in ids:
            sids = sids.difference(self.ancestors(id, reflexive=False))
        return sids 
Example #30
Source File: ontol.py    From ontobio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ancestors(self, node, relations=None, reflexive=False):
        """Return all ancestors of specified node.

        The default implementation is to use networkx, but some
        implementations of the Ontology class may use a database or
        service backed implementation, for large graphs.

        Arguments
        ---------
        node : str
            identifier for node in ontology
        reflexive : bool
            if true, return query node in graph
        relations : list
             relation (object property) IDs used to filter

        Returns
        -------
        list[str]
            ancestor node IDs

        """
        seen = set()
        nextnodes = [node]
        while len(nextnodes) > 0:
            nn = nextnodes.pop()
            if not nn in seen:
                seen.add(nn)
                nextnodes += self.parents(nn, relations=relations)
        if not reflexive:
            seen -= {node}
        return list(seen)