本文整理汇总了Python中cogent.parse.tree.DndParser.makeIdIndex方法的典型用法代码示例。如果您正苦于以下问题:Python DndParser.makeIdIndex方法的具体用法?Python DndParser.makeIdIndex怎么用?Python DndParser.makeIdIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cogent.parse.tree.DndParser
的用法示例。
在下文中一共展示了DndParser.makeIdIndex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: analysisTests
# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import makeIdIndex [as 别名]
class analysisTests(TestCase):
"""Tests of top-level functions."""
def setUp(self):
"""Make a couple of standard trees"""
self.t1 = DndParser('((a,(b,c)),(d,e))', RangeNode)
#selt.t1 indices: ((0,(1,2)5)6,(3,4)7)8
def test_threeway_counts(self):
"""threeway_counts should produce correct count matrix"""
self.t1.makeIdIndex()
ind = self.t1.IdIndex
ind[0].Sequence = array([0,0,0])
ind[1].Sequence = array([0,1,0])
ind[2].Sequence = array([1,0,1])
ind[3].Sequence = array([1,1,0])
ind[4].Sequence = array([1,1,1])
depths = self.t1.leafLcaDepths()
result = tree_threeway_counts(self.t1, depths, ABPairs)
#check we got the right number of comparisons
self.assertEqual(len(result), 20)
#check we got the right keys
for k in [(1,2,0),(2,1,0),(0,1,3),(1,0,3),(0,1,4),(1,0,4),(0,2,3),\
(2,0,3),(0,2,4),(2,0,4),(1,2,3),(2,1,3),(1,2,4),(2,1,4),(3,4,1),\
(4,3,1),(3,4,2),(4,3,2)]:
assert k in result
#spot-check a few results
self.assertEqual(result[(1,2,0)]._data, array([[2,1],[0,0]]))
self.assertEqual(result[(2,1,0)]._data, array([[1,2],[0,0]]))
self.assertEqual(result[(2,1,3)]._data, array([[0,1],[1,1]]))
def test_twoway_counts(self):
"""twoway_counts should produce correct count matrix"""
self.t1.makeIdIndex()
ind = self.t1.IdIndex
ind[0].Sequence = array([0,0,0])
ind[1].Sequence = array([0,1,0])
ind[2].Sequence = array([1,0,1])
ind[3].Sequence = array([1,1,0])
ind[4].Sequence = array([1,1,1])
depths = self.t1.leafLcaDepths()
#check that it works with averaging
result = tree_twoway_counts(self.t1, ABPairs)
#check we got the right number of comparisons: average by default
self.assertEqual(len(result), 10)
#check we got the right keys
for k in [(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]:
assert k in result
#spot-check a few results
self.assertEqual(result[(0,1)]._data, array([[2,.5],[.5,0]]))
self.assertEqual(result[(2,3)]._data, array([[0,1],[1,1]]))
#check that it works when we don't average
result = tree_twoway_counts(self.t1, ABPairs, average=False)
self.assertEqual(len(result), 20)
#check we got the right keys
for k in [(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]:
assert k in result
#reverse should be in result too
assert (k[1],k[0]) in result
#spot-check values
self.assertEqual(result[(0,1)]._data, array([[2,1],[0,0]]))
self.assertEqual(result[(1,0)]._data, array([[2,0],[1,0]]))
def test_counts_to_probs(self):
"""counts_to_probs should skip cases with zero rows"""
counts = {
(0,1): Counts(array([[0,1],[1,0]]), ABPairs),
(1,2): Counts(array([[0,0],[1,0]]), ABPairs), #bad row
(0,3): Counts(array([[0,0],[0,0]]), ABPairs), #bad row
(0,4): Counts(array([[0.0,0.0],[0.0,0.0]]), ABPairs), #bad row
(0,5): Counts(array([[0.1,0.3],[0.0,0.0]]), ABPairs), #bad row
(3,4): Counts(array([[0.1,0.3],[0.4,0.1]]), ABPairs),
(2,1): Counts(array([[0,5],[1,0]]), ABPairs),
}
result = counts_to_probs(counts)
self.assertEqual(len(result), 3)
self.assertFloatEqual(result[(0,1)]._data, array([[0,1],[1,0]]))
self.assertFloatEqual(result[(3,4)]._data, \
array([[0.25,0.75],[0.8,0.2]]))
self.assertFloatEqual(result[(2,1)]._data, array([[0,1],[1,0]]))
def test_probs_to_rates(self):
"""probs_to_rates converts probs to rates, omitting problem cases"""
probs = dict([(i, Probs.random(DnaPairs)) for i in range(100)])
rates = probs_to_rates(probs)
#check we got at most the same number of items as in probs
assert len(rates) <= len(probs)
#check that we didn't get anything bad
vals = rates.values()
for v in vals:
assert not v.isSignificantlyComplex()
#check that we didn't miss anything good
for key, val in probs.items():
if key not in rates:
try:
r = val.toRates()
print r.isValid()
assert r.isSignificantlyComplex() or (not r.isValid())
except (ZeroDivisionError, OverflowError, ValueError):
pass
#.........这里部分代码省略.........
示例2: OldPhyloNodeTests
# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import makeIdIndex [as 别名]
class OldPhyloNodeTests(TestCase):
"""Tests of the PhyloNode class -- these are all now methods of RangeNode."""
def setUp(self):
"""Make a couple of standard trees"""
self.t1 = DndParser('((a,(b,c)),(d,e))', RangeNode)
#selt.t1 indices: ((0,(1,2)5)6,(3,4)7)8
def test_makeIdIndex(self):
"""RangeNode makeIdIndex should assign ids to every node"""
self.t1.makeIdIndex()
result = self.t1.IdIndex
nodes = list(self.t1.traverse(self_before=True))
#check we got an entry for each node
self.assertEqual(len(result), len(nodes))
#check the ids are in the result
for i in nodes:
assert hasattr(i, 'Id')
assert i.Id in result
def test_assignQ_single_passed(self):
"""RangeNode assignQ should propagate single Q param down tree"""
#should work if Q explicitly passed
t = self.t1
Q = ['a']
t.assignQ(Q)
for node in t.traverse(self_before=True):
assert node.Q is Q
def test_assignQ_single_set(self):
"""RangeNode assignQ should propagate single Q if set"""
t = self.t1
Q = ['a']
assert not hasattr(t, 'Q')
t.Q = Q
t.assignQ()
for node in t.traverse(self_before=True):
assert node.Q is Q
def test_assignQ_single_overwrite(self):
"""RangeNode assignQ should overwrite root Q if new Q passed"""
t = self.t1
Q = ['a']
Q2 = ['b']
t.Q = Q
t.assignQ(Q2)
for node in t.traverse(self_before=True):
assert node.Q is Q2
assert not node.Q is Q
def test_assignQ_multiple(self):
"""RangeNode assignQ should propagate multiple Qs"""
t = self.t1
Q1 = ['a']
Q2 = ['b']
Q3 = ['c']
t.makeIdIndex()
t.IdIndex[7].Q = Q1
t.IdIndex[5].Q = Q2
t.assignQ(Q3)
result = [i.Q for i in t.traverse(self_after=True)]
assert t.Q is Q3
self.assertEqual(result, [Q3,Q2,Q2,Q2,Q3,Q1,Q1,Q1,Q3])
def test_assignQ_multiple_overwrite(self):
"""RangeNode assignQ should allow overwrite"""
t = self.t1
Q1 = ['a']
Q2 = ['b']
Q3 = ['c']
t.makeIdIndex()
t.IdIndex[7].Q = Q1
t.IdIndex[5].Q = Q2
t.assignQ(Q3, overwrite=True)
for i in t.traverse(self_after=True):
assert i.Q is Q3
def test_assignQ_special(self):
"""RangeNode assignQ should work with special Qs"""
t = self.t1
Q1 = 'a'
Q2 = 'b'
Q3 = 'c'
t.makeIdIndex()
special = {7:Q1, 1:Q2}
#won't work if no Q at root
self.assertRaises(ValueError, t.assignQ, special_qs=special)
t.assignQ(Q3, special_qs=special)
result = [i.Q for i in t.traverse(self_after=True)]
self.assertEqual(result, ['c','b','c','c','c','a','a','a','c'])
def test_assignP(self):
"""RangeNode assignP should work when Qs set."""
t = self.t1
for i in t.traverse(self_before=True):
i.Length = random() * 0.5 #range 0 to 0.5
t.Q = Rates.random(DnaPairs)
t.assignQ()
t.assignP()
t.assignIds()
#.........这里部分代码省略.........