0

我在 Dendropy 中做一些系统发育学:我正在尝试按照教程,使用过滤器(lambda 函数)遍历树的所有叶子。

如果迭代器是 leaf_iter,则使用 lambda 函数会出错,但如果迭代器是任何其他迭代器(例如 postorder_node_iterate)则不会。我错过了什么?

回溯有几百行长,所以我只给你看一个摘录:

Traceback (most recent call last):
  File "<pyshell#274>", line 1, in <module>
    for node in tree_test.leaf_iter(survival):
  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 915, in leaf_iter
    for node in self.seed_node.leaf_iter(filter_fn=filter_fn):
  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 2444, in leaf_iter
    for node in self.postorder_iter(filter_fn):
  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 2427, in postorder_iter
    if filter_fn is None or filter_fn(node):
  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 2441, in <lambda>
    filter_fn = lambda x: x.is_leaf() and filter_fn(x) or None

[... line 2441 ...]

  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 2441, in <lambda>
    filter_fn = lambda x: x.is_leaf() and filter_fn(x) or None
  File "build\bdist.win32\egg\dendropy\dataobject\tree.py", line 2537, in is_leaf
    return bool(not self._child_nodes)
RuntimeError: maximum recursion depth exceeded while calling a Python object

要测试这个问题,您可以使用以下代码,几乎取自教程:

#call a bit of modules and build a simulated tree
import dendropy
import random
from dendropy import TaxonSet, Tree, TreeList, treesim
taxa = dendropy.TaxonSet(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
tree_test = treesim.birth_death(0.4, 0.1, ntax=8, taxon_set=taxa, repeat_until_success=True)

#define an evolutionary character
def evolve_char(tree, start=1.0):
    for node in tree.preorder_node_iter():
        if node.parent_node is None:
            node.value = 1.0
        else:
            node.value = random.gauss(node.parent_node.value, node.edge.length)
    return tree
#evolve the character on the simulated tree
evolve_char(tree_test)

#positive_char select the nodes with positive character
positive_char = lambda node: True if node.value > 0. else False

#iter with postorder_node_iter (no problem)
for node in tree_test.postorder_node_iter():
    print("Node %s with value %s" % (node.taxon, node.value))
for node in tree_test.leaf_iter(positive_char):
    print("Node %s with value %s" % (node.taxon, node.value))

#iter with leaf_iter without filtering (no problem)
for node in tree_test.leaf_iter():
    print("Node %s with value %s" % (node.taxon, node.value))

#iter with leaf_iter and filtering (problem!)
for node in tree_test.leaf_iter(positive_char):
    print("Node %s with value %s" % (node.taxon, node.value))
4

1 回答 1

0

好吧,那是一个错误。刚刚在新的 github 版本中修复。

我将把它留在这里,因为其他人可能会发现同样的问题。

于 2013-06-20T00:05:02.843 回答