Beispiel #1
0
    def test5(self):

        particles = Particles(10)
        particles.mass = list(range(10)) | units.kg
        particles[0].child1 = particles[1]
        particles[0].child2 = particles[2]
        particles[1].child1 = particles[3]
        particles[1].child2 = particles[4]

        x = particles.as_binary_tree()
        roots = list(x.iter_branches())

        self.assertEqual(len(roots), 1)
        binary = roots[0]
        output = ''
        for level, node in binary.iter_levels():
            output += '..' * level
            output += str(node.particle.mass.value_in(units.kg))
            output += '\n'

        self.assertEqual(output, """0.0
..1.0
....3.0
....4.0
..2.0
""")
Beispiel #2
0
    def test5(self):
        
        particles = Particles(10)
        particles.mass = list(range(10)) | units.kg
        particles[0].child1 = particles[1]
        particles[0].child2 = particles[2]
        particles[1].child1 = particles[3]
        particles[1].child2 = particles[4]
    
        x = particles.as_binary_tree()
        roots = list(x.iter_branches())
    
        self.assertEquals(len(roots), 1)
        binary = roots[0]
        output = ''
        for level, node in binary.iter_levels():
            output += '..' * level
            output += str(node.particle.mass.value_in(units.kg))
            output += '\n'
        
        print output
        self.assertEquals(output, """0.0
..1.0
....3.0
....4.0
..2.0
""")
Beispiel #3
0
    def test2(self):
        particles = Particles(10)
        particles.mass = list(range(10)) | units.kg
        particles[0].child1 = particles[1]
        particles[0].child2 = particles[2]

        self.assertEqual(particles[0].mass, 0 | units.kg)
        self.assertEqual(particles[0].child1.mass, 1 | units.kg)
        self.assertEqual(particles[1].child1, None)

        tree = particles.as_binary_tree()
        self.assertFalse(tree.is_leaf())
        self.assertEqual(len(list(tree.iter_descendant_leafs())), 9)
        self.assertEqual(len(list(tree.iter_descendant_branches())), 1)
        branches = list(tree.iter_branches())
        self.assertEqual(len(list(branches[0].iter_descendant_leafs())), 2)
        self.assertEqual(len(list(branches[0].iter_descendant_branches())), 0)
Beispiel #4
0
 def test2(self):
     particles = Particles(10)
     particles.mass = list(range(10)) | units.kg
     particles[0].child1 = particles[1]
     particles[0].child2 = particles[2]
     
     self.assertEquals(particles[0].mass, 0 | units.kg)
     self.assertEquals(particles[0].child1.mass, 1 | units.kg)
     self.assertEquals(particles[1].child1, None)
 
     tree = particles.as_binary_tree()
     self.assertFalse(tree.is_leaf())
     self.assertEquals(len(list(tree.iter_descendant_leafs())), 9)
     self.assertEquals(len(list(tree.iter_descendant_branches())), 1)
     branches = list(tree.iter_branches())
     self.assertEquals(len(list(branches[0].iter_descendant_leafs())), 2)
     self.assertEquals(len(list(branches[0].iter_descendant_branches())), 0)
Beispiel #5
0
    def test4(self):
        particles = Particles(10)
        particles.mass = list(range(10)) | units.kg
        particles[0].child1 = particles[2]
        particles[0].child2 = particles[1]
        particles[1].child1 = particles[4]
        particles[1].child2 = particles[3]
        particles[2].child1 = particles[6]
        particles[2].child2 = particles[5]

        tree = particles.as_binary_tree()
        masses = [] | units.kg
        stack = list(reversed(tree.get_children()))
        while stack:
            x = stack.pop()
            masses.append(x.particle.mass)
            stack.extend(reversed(x.get_children()))

        self.assertEqual(masses, [0, 2, 6, 5, 1, 4, 3, 7, 8, 9] | units.kg)
Beispiel #6
0
 def test4(self):
     particles = Particles(10)
     particles.mass = list(range(10)) | units.kg
     particles[0].child1 = particles[2]
     particles[0].child2 = particles[1]
     particles[1].child1 = particles[4]
     particles[1].child2 = particles[3]
     particles[2].child1 = particles[6]
     particles[2].child2 = particles[5]
     
     tree = particles.as_binary_tree()
     masses = [] | units.kg
     stack = list(reversed(tree.get_children()))
     while stack:
         x = stack.pop()
         masses.append(x.particle.mass)
         stack.extend(reversed(x.get_children()))
         
     self.assertEquals(masses, [0,2,6,5,1,4,3,7,8,9] | units.kg)
Beispiel #7
0
    def test3(self):
        particles = Particles(10)
        particles.mass = list(range(10)) | units.kg
        particles[0].child1 = particles[1]
        particles[0].child2 = particles[2]
        particles[1].child1 = particles[3]
        particles[1].child2 = particles[4]
        particles[2].child1 = particles[5]
        particles[2].child2 = particles[6]

        tree = particles.as_binary_tree()
        self.assertFalse(tree.is_leaf())
        self.assertEqual(len(list(tree.iter_children())), 4)
        self.assertEqual(len(list(tree.iter_branches())), 1)
        self.assertEqual(len(list(tree.iter_leafs())), 3)
        self.assertEqual(len(list(tree.iter_descendant_leafs())), 7)
        self.assertEqual(len(list(tree.iter_descendant_branches())), 3)
        branches = list(tree.iter_branches())
        self.assertEqual(len(list(branches[0].iter_children())), 2)
        self.assertEqual(len(list(branches[0].iter_branches())), 2)
        self.assertEqual(len(list(branches[0].iter_leafs())), 0)
        self.assertEqual(len(list(branches[0].iter_descendant_leafs())), 4)
        self.assertEqual(len(list(branches[0].iter_descendant_branches())), 2)
Beispiel #8
0
 def test3(self):
     particles = Particles(10)
     particles.mass = list(range(10)) | units.kg
     particles[0].child1 = particles[1]
     particles[0].child2 = particles[2]
     particles[1].child1 = particles[3]
     particles[1].child2 = particles[4]
     particles[2].child1 = particles[5]
     particles[2].child2 = particles[6]
     
 
     tree = particles.as_binary_tree()
     self.assertFalse(tree.is_leaf())
     self.assertEquals(len(list(tree.iter_children())), 4)
     self.assertEquals(len(list(tree.iter_branches())), 1)
     self.assertEquals(len(list(tree.iter_leafs())), 3)
     self.assertEquals(len(list(tree.iter_descendant_leafs())), 7)
     self.assertEquals(len(list(tree.iter_descendant_branches())), 3)
     branches = list(tree.iter_branches())
     self.assertEquals(len(list(branches[0].iter_children())), 2)
     self.assertEquals(len(list(branches[0].iter_branches())), 2)
     self.assertEquals(len(list(branches[0].iter_leafs())), 0)
     self.assertEquals(len(list(branches[0].iter_descendant_leafs())), 4)
     self.assertEquals(len(list(branches[0].iter_descendant_branches())), 2)