本文整理汇总了Python中networkx.ancestors方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.ancestors方法的具体用法?Python networkx.ancestors怎么用?Python networkx.ancestors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.ancestors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ready_to_schedule_operation
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例2: calculate_mrcas
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例3: get_common_causes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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))
示例4: apply
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例5: filter_recipe_dag
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例6: filter
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例7: _lowest_common_anscestor
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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
示例8: OnClick
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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
示例9: lca_viewer_starter
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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
示例10: verify_nx_for_tutorial_algorithms
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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"
),
)
示例11: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例12: filter_redundant
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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
示例13: ancestors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
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)
示例14: _blanket
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
def _blanket(self, nid):
nodes = set()
for ont in self.id_to_ontology_map[nid]:
nodes.update(ont.ancestors(nid))
nodes.update(ont.descendants(nid))
return list(nodes)
示例15: test_lexmap_multi
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import ancestors [as 别名]
def test_lexmap_multi():
"""
Text lexical mapping
"""
factory = OntologyFactory()
print("Creating ont")
files = ['x','m','h','bto']
onts = [factory.create('tests/resources/autopod-{}.json'.format(f)) for f in files]
lexmap = LexicalMapEngine()
lexmap.index_ontologies(onts)
#print(lexmap.lmap)
#print(ont.all_synonyms())
g = lexmap.get_xref_graph()
for x in g.nodes():
print("{} --> {}".format(x,lexmap.grouped_mappings(x)))
for (x,y,d) in g.edges(data=True):
cl = nx.ancestors(g,x)
print("{} '{}' <-> {} '{}' :: {} CLOSURE={}".format(x,lexmap.label(x),y,lexmap.label(y),d,len(cl)))
cpr = d[lexmap.CONDITIONAL_PR]
assert cpr > 0 and cpr <= 1.0
unmapped = lexmap.unmapped_nodes(g)
print('U: {}'.format(len(unmapped)))
unmapped = lexmap.unmapped_nodes(g, rs_threshold=4)
print('U4: {}'.format(len(unmapped)))
cliques = lexmap.cliques(g)
maxc = max(cliques, key=len)
print('CLIQUES: {}'.format(cliques))
print('MAX CLIQUES: {}'.format(maxc))
df = lexmap.as_dataframe(g)
print(df.to_csv(sep="\t"))