0
我在Dendropy中做了一些系統發育: 我試着按照教程,使用過濾器(lambda函數)遍歷樹的所有葉子。Dendropy leaf_iter()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))