Esempio n. 1
0
    def test_draw_unpruned_tree_with_some_text(self):
        ft = FractalTree(value=20)
        ft.add_layer()
        ft.get_children()[0].add_layer()
        ft.get_children()[-1].add_layer()
        fill_tree_with_columns(ft, 2)
        selected_node = ft.select_index([1])
        child_1 = selected_node.get_children()[1]
        child_2 = selected_node.get_children()[2]
        lb1 = child_1.manual_graphic.draw_objects[0].start_mark_line.add_label(
            1, font_size=8, bottom_margin=1)
        lb2 = child_1.manual_graphic.draw_objects[0].start_mark_line.add_label(
            2, font_size=8, bottom_margin=1)
        lb3 = child_2.manual_graphic.draw_objects[0].start_mark_line.add_label(
            3, font_size=8, bottom_margin=1)

        for child in ft.get_children():
            try:
                child.manual_graphic.draw_objects[1].top_margin += 2
            except IndexError:
                pass

        with self.file_path(path, 'draw_unpruned_tree_with_some_text',
                            'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            ft.manual_graphic.draw(self.pdf)
            self.pdf.write(pdf_path)
 def test_1(self):
     ft = FractalTree(reading_direction='vertical', value=10)
     ft.add_layer()
     permutation_orders = [
         child.permutation_order for child in ft.get_children()
     ]
     result = [[2, 1, 3], [3, 2, 1], [1, 3, 2]]
     self.assertEqual(result, permutation_orders)
Esempio n. 3
0
 def test_4(self):
     self.ft.multi = (3, 4)
     self.ft.add_layer()
     result = [leaf.value for leaf in self.ft.traverse_leaves()]
     ft = FractalTree(value=10, multi=(1, 1))
     ft.add_layer()
     expected = [leaf.value for leaf in ft.traverse_leaves()]
     self.assertEqual(expected, result)
Esempio n. 4
0
 def test_two(self):
     t = FractalTree(value=10)
     t.add_layer()
     child = t.get_children()[0]
     s = child.split(1, 1)
     actual = [x.value for x in s]
     expected = [child.value / 2, child.value / 2]
     self.assertEqual(expected, actual)
Esempio n. 5
0
 def test_split_is_leaf(self):
     t = FractalTree(value=10)
     t.add_layer()
     child = t.get_children()[0]
     s = child.split(1, 1, 1)
     actual = [x.is_leaf for x in s]
     expected = [True, True, True]
     self.assertEqual(expected, actual)
Esempio n. 6
0
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(proportions=[1, 2, 3],
                              tree_permutation_order=[3, 1, 2],
                              value=10)

    def test_1(self):
        self.ft.add_layer()
        self.ft.get_children()[1].add_layer()

        selected = self.ft.get_children()[1].get_children()[0]
        self.assertEqual(selected.__next__.name, '2.2')
Esempio n. 7
0
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(value=10)

    def test_1(self):
        self.assertEqual([self.ft], self.ft.get_leaves())

    def test_2(self):
        self.assertEqual([self.ft.value],
                         self.ft.get_leaves(key=lambda leaf: leaf.value))

    def test_3(self):
        self.ft.add_layer()
        [
            child for child in self.ft.get_children()
            if child.fractal_order == 1
        ][0].fertile = False
        self.ft.add_layer()
        result = [[Fraction(5, 6),
                   Fraction(5, 3),
                   Fraction(5, 2)],
                  Fraction(5, 3),
                  [Fraction(10, 9),
                   Fraction(5, 3),
                   Fraction(5, 9)]]
        self.assertEqual(result,
                         self.ft.get_leaves(key=lambda leaf: leaf.value))
Esempio n. 8
0
 def test_1(self):
     ft = FractalTree(proportions=(1, 2, 3), tree_permutation_order=(3, 1, 2))
     ft.tempo = 60
     ft.value = 10
     ft.add_layer()
     ft.add_layer()
     self.assertEqual([node.fractal_order for node in ft.traverse_leaves()], [1, 2, 3, 3, 1, 2, 2, 3, 1])
Esempio n. 9
0
 def test_3(self):
     ft = FractalTree(value=10, proportions=(1, 2, 3), tree_permutation_order=(3, 1, 2))
     ft.add_layer()
     ft.get_children()[0].add_layer()
     expected = [[0.8333333333333334, 1.6666666666666667, 2.5], 1.6666666666666667, 3.3333333333333335]
     result = ft.get_leaves(key=lambda leaf: float(leaf.value))
     self.assertEqual(expected, result)
Esempio n. 10
0
 def test_1(self):
     ft = FractalTree(proportions=(1, 2, 3, 4, 5), tree_permutation_order=(3, 5, 1, 2, 4), value=10)
     ft.add_layer()
     # ft.add_layer()
     # print(ft.get_leaves(key=lambda leaf: leaf.index))
     # print(ft.get_leaves(key=lambda leaf: leaf.fractal_order))
     # print(ft.get_leaves(key=lambda leaf: round(float(leaf.value), 2)))
     ft.merge_children(1, 2, 2)
     # print(ft.get_leaves(key=lambda leaf: leaf.index))
     self.assertEqual(ft.get_leaves(key=lambda leaf: leaf.fractal_order), [3, 5, 2])
     self.assertEqual(ft.get_leaves(key=lambda leaf: round(float(leaf.value), 2)), [2.0, 4.0, 4.0])
 def test_3(self):
     ft = FractalTree(proportions=[1, 2, 3, 4, 5, 6], tree_permutation_order=[4, 1, 5, 3, 6, 2], value=20,
                      multi=(4, 3))
     ft.add_layer()
     ft.reduce_children(lambda child: child.fractal_order not in [2, 3])
     actual = [node.value for node in ft.get_children()]
     expected = [12, 8]
     self.assertEqual(actual, expected)
 def test_11(self):
     ft = FractalTree(value=10, proportions=(1, 2, 3, 4, 5, 6, 7), tree_permutation_order=(2, 6, 4, 1, 3, 7, 5))
     output = []
     for i in range(7):
         ft_copy = ft.__deepcopy__()
         ft_copy.generate_children(mode='merge', number_of_children=2, merge_index=i)
         output.append(ft_copy.get_leaves(key=lambda leaf: float(leaf.value)))
     test_case = output
     # ft.add_layer()
     # print(ft.get_leaves(key=lambda leaf: float(leaf.value)))
     # print(output)
     expected = [[8.214285714285714, 1.7857142857142858],
                 [0.7142857142857143, 9.285714285714286],
                 [2.857142857142857, 7.142857142857143],
                 [4.285714285714286, 5.714285714285714],
                 [4.642857142857143, 5.357142857142857],
                 [5.714285714285714, 4.285714285714286],
                 [8.214285714285714, 1.7857142857142858]]
     self.assertEqual(expected, test_case)
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(proportions=(1, 2, 3, 4, 5, 6, 7), tree_permutation_order=(4, 2, 7, 1, 6, 3, 5))

    def test_1(self):
        actual = self.ft._get_merge_lengths(number_of_children=1, merge_index=4)
        expected = [7]
        self.assertEqual(expected, actual)

    def test_2(self):
        actual = self.ft._get_merge_lengths(number_of_children=2, merge_index=0)
        expected = [6, 1]
        self.assertEqual(expected, actual)

    def test_3(self):
        actual = self.ft._get_merge_lengths(number_of_children=2, merge_index=4)
        expected = [4, 3]
        self.assertEqual(expected, actual)

    def test_4(self):
        actual = self.ft._get_merge_lengths(number_of_children=3, merge_index=2)
        expected = [1, 1, 5]
        self.assertEqual(expected, actual)

    def test_5(self):
        actual = self.ft._get_merge_lengths(number_of_children=3, merge_index=3)
        expected = [2, 1, 4]
        self.assertEqual(expected, actual)

    def test_6(self):
        actual = self.ft._get_merge_lengths(number_of_children=3, merge_index=1)
        expected = [1, 5, 1]
        self.assertEqual(expected, actual)

    def test_7(self):
        actual = self.ft._get_merge_lengths(number_of_children=6, merge_index=3)
        expected = [1, 1, 1, 2, 1, 1]
        self.assertEqual(expected, actual)

    def test_8(self):
        actual = self.ft._get_merge_lengths(number_of_children=7, merge_index=3)
        expected = [1, 1, 1, 1, 1, 1, 1]
        self.assertEqual(expected, actual)

    def test_9(self):
        actual = self.ft._get_merge_lengths(number_of_children=4, merge_index=5)
        expected = [3, 1, 1, 2]
        self.assertEqual(expected, actual)
Esempio n. 14
0
 def test_2(self):
     ft = FractalTree(proportions=(1, 2, 3), tree_permutation_order=(3, 1, 2))
     ft.tempo = 60
     ft.value = 10
     ft.add_layer()
     ft.add_layer()
     self.assertEqual([node.value for node in ft.traverse_leaves()],
                      [Fraction(5, 6), Fraction(5, 3), Fraction(5, 2), Fraction(5, 6), Fraction(5, 18),
                       Fraction(5, 9),
                       Fraction(10, 9), Fraction(5, 3), Fraction(5, 9)])
 def test_1(self):
     ft = FractalTree(proportions=(1, 2, 3), tree_permutation_order=(3, 1, 2), value=10)
     ft.add_layer()
     ft.add_layer()
     for node in ft.get_layer(1):
         node.reduce_children(condition=lambda node: node.fractal_order == 1)
     self.assertEqual([node.fractal_order for node in ft.traverse_leaves()], [2, 3, 3, 2, 2, 3])
Esempio n. 16
0
    def test_draw_filled_tree(self):
        ft = FractalTree(value=20)
        ft.add_layer()
        ft.get_children()[1].add_layer()
        fill_tree_with_columns(ft, 2)

        with self.file_path(path, 'draw_filled_tree', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            ft.manual_graphic.draw(self.pdf)
            self.pdf.write(pdf_path)
 def test_2(self):
     ft = FractalTree(proportions=(1, 2, 3), tree_permutation_order=(3, 1, 2), value=10)
     ft.add_layer()
     ft.add_layer()
     for node in ft.get_layer(1):
         node.reduce_children(condition=lambda node: node.fractal_order == 1)
     actual = [node.value for node in ft.traverse_leaves()]
     expected = [Fraction(2, 1),
                 Fraction(3, 1),
                 Fraction(1, 1),
                 Fraction(2, 3),
                 Fraction(4, 3),
                 Fraction(2, 1)]
     self.assertEqual(actual, expected)
Esempio n. 18
0
 def setUp(self) -> None:
     self.ft = FractalTree(value=10)
Esempio n. 19
0
 def test_1(self):
     ft = FractalTree()
     ft.value = 50
     ft.add_layer()
     self.assertEqual(sum([leaf.value for leaf in ft.get_leaves()]), ft.value)
Esempio n. 20
0
 def test_3(self):
     ft = FractalTree()
     with self.assertRaises(SetValueFirst):
         ft.add_layer()
Esempio n. 21
0
 def test_2(self):
     ft = FractalTree()
     ft.value = 50
     ft.add_layer()
     with self.assertRaises(FractalTreeException):
         ft.value = 100
 def test_10(self):
     ft = FractalTree(value=10, proportions=(1, 2, 3, 4, 5, 6, 7), tree_permutation_order=(2, 6, 4, 1, 3, 7, 5))
     ft.generate_children(mode='merge', number_of_children=2)
     test_case = ft.get_leaves(key=lambda leaf: float(leaf.value))
     expected = [8.214285714285714, 1.7857142857142858]
     self.assertEqual(expected, test_case)
Esempio n. 23
0
 def test_child_is_not_leaf(self):
     t = FractalTree(value=10)
     t.add_layer()
     child = t.get_children()[0]
     child.split(1, 1, 1)
     self.assertFalse(child.is_leaf)
Esempio n. 24
0
def make_ft():
    ft = FractalTree(value=20)
    ft.add_layer()
    ft.get_children()[0].add_layer()
    ft.get_children()[-1].add_layer()
    return ft
Esempio n. 25
0
 def test_1(self):
     ft = FractalTree()
     self.assertEqual(3, ft.size)
Esempio n. 26
0
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(tree_permutation_order=[3, 1, 2],
                              proportions=[1, 2, 3],
                              value=10)

    def test_1(self):
        self.ft.add_layer()
        result = [(2, 1), (2, 2), (2, 3)]
        self.assertEqual(result,
                         [child.multi for child in self.ft.get_children()])

    def test_2(self):
        self.ft.multi = (1, 7)
        self.ft.add_layer()
        result = [leaf.value for leaf in self.ft.traverse_leaves()]
        ft = FractalTree(value=10, multi=(3, 1))
        ft.add_layer()
        expected = [leaf.value for leaf in ft.traverse_leaves()]
        self.assertEqual(expected, result)

    def test_3(self):
        self.ft.multi = (4, 1)
        self.ft.add_layer()
        result = [leaf.value for leaf in self.ft.traverse_leaves()]
        ft = FractalTree(value=10, multi=(1, 1))
        ft.add_layer()
        expected = [leaf.value for leaf in ft.traverse_leaves()]
        self.assertEqual(expected, result)

    def test_4(self):
        self.ft.multi = (3, 4)
        self.ft.add_layer()
        result = [leaf.value for leaf in self.ft.traverse_leaves()]
        ft = FractalTree(value=10, multi=(1, 1))
        ft.add_layer()
        expected = [leaf.value for leaf in ft.traverse_leaves()]
        self.assertEqual(expected, result)
Esempio n. 27
0
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(value=10)

    def test_1(self):
        self.ft.change_value(20)
        result = 20
        self.assertEqual(result, self.ft.value)

    def test_2(self):
        self.ft.add_layer()
        self.ft.get_children()[0].change_value(10)
        expected = 15
        self.assertEqual(expected, self.ft.value)

    def test_3(self):
        self.ft.add_layer()
        self.ft.get_children()[0].change_value(10)
        expected = [Fraction(10, 1), Fraction(5, 3), Fraction(10, 3)]
        self.assertEqual(expected,
                         [child.value for child in self.ft.get_children()])

    def test_4(self):
        self.ft.add_layer()
        self.ft.change_value(15)
        expected = [Fraction(15, 2), Fraction(5, 2), Fraction(5, 1)]
        self.assertEqual(expected,
                         [child.value for child in self.ft.get_children()])

    def test_5(self):
        self.ft.add_layer()
        self.ft.add_layer()
        self.ft.get_children()[0].change_value(10)
        expected = [[Fraction(15, 1)], [10,
                                        Fraction(5, 3),
                                        Fraction(10, 3)],
                    [[Fraction(5, 3),
                      Fraction(10, 3),
                      Fraction(5, 1)],
                     [Fraction(5, 6),
                      Fraction(5, 18),
                      Fraction(5, 9)],
                     [Fraction(10, 9),
                      Fraction(5, 3),
                      Fraction(5, 9)]]]
        self.assertEqual(expected, [
            self.ft.get_layer(layer=i, key='value')
            for i in range(self.ft.number_of_layers + 1)
        ])
Esempio n. 28
0
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(proportions=[1, 2, 3],
                              tree_permutation_order=[3, 1, 2],
                              value=10)

    def test_0(self):
        with self.assertRaises(Exception):
            self.ft.get_layer(1)

    def test_1(self):
        self.assertEqual([self.ft], self.ft.get_layer(0))

    def test_2(self):
        self.ft.add_layer()
        result = self.ft.get_children()
        self.assertEqual(result, self.ft.get_layer(1))

    def test_3(self):
        for i in range(3):
            self.ft.add_layer()

        result = self.ft.get_children()
        self.assertEqual(result, self.ft.get_layer(1))

    def test_4(self):
        for i in range(3):
            self.ft.add_layer()
        result = [child.get_children() for child in self.ft.get_children()]

        self.assertEqual(result, self.ft.get_layer(2))

    def test_5(self):
        for i in range(3):
            self.ft.add_layer()
        result = self.ft.get_leaves()

        self.assertEqual(result, self.ft.get_layer(3))

    def test_6(self):
        for i in range(3):
            self.ft.add_layer()

        with self.assertRaises(ValueError):
            self.ft.get_layer(4)

    def test_7(self):
        self.ft.add_layer()
        self.ft.add_layer(lambda n: True if n.fractal_order > 1 else False)
        self.ft.add_layer(lambda n: True if n.fractal_order > 1 else False)
        self.ft.add_layer(lambda n: True if n.fractal_order > 1 else False)
        self.ft.add_layer(lambda n: True if n.fractal_order > 1 else False)
        result = [[['1.1'],
                   [['1.2.1'], ['1.2.2.1', '1.2.2.2', '1.2.2.3'],
                    ['1.2.3.1', '1.2.3.2', '1.2.3.3']],
                   [['1.3.1.1', '1.3.1.2', '1.3.1.3'], ['1.3.2'],
                    ['1.3.3.1', '1.3.3.2', '1.3.3.3']]], '2',
                  [[['3.1.1'], ['3.1.2.1', '3.1.2.2', '3.1.2.3'],
                    ['3.1.3.1', '3.1.3.2', '3.1.3.3']],
                   [['3.2.1.1', '3.2.1.2', '3.2.1.3'], ['3.2.2'],
                    ['3.2.3.1', '3.2.3.2', '3.2.3.3']], ['3.3']]]
        self.assertEqual(result,
                         [name for name in self.ft.get_layer(4, key='name')])

    def test_7_1(self):
        self.ft.add_layer()
        self.ft.add_layer()
        self.assertEqual([10], self.ft.get_layer(0, key='value'))

    def test_7_2(self):
        self.ft.add_layer()
        self.ft.add_layer()
        result = [Fraction(5, 1), Fraction(5, 3), Fraction(10, 3)]
        self.assertEqual(result, self.ft.get_layer(1, key='value'))

    def test_7_3(self):
        self.ft.add_layer()
        self.ft.add_layer()
        result = [[Fraction(5, 6),
                   Fraction(5, 3),
                   Fraction(5, 2)],
                  [Fraction(5, 6),
                   Fraction(5, 18),
                   Fraction(5, 9)],
                  [Fraction(10, 9),
                   Fraction(5, 3),
                   Fraction(5, 9)]]
        self.assertEqual(result, self.ft.get_layer(2, key='value'))

    def test_get_layer_key_lambda(self):
        self.ft.add_layer()
        self.ft.add_layer()
        result = [[0.83, 1.67, 2.5], [0.83, 0.28, 0.56], [1.11, 1.67, 0.56]]
        self.assertEqual(
            result,
            self.ft.get_layer(2, key=lambda node: round(float(node.value), 2)))
Esempio n. 29
0
 def setUp(self) -> None:
     self.ft = FractalTree(proportions=[1, 2, 3],
                           tree_permutation_order=[3, 1, 2],
                           value=10)
 def setUp(self) -> None:
     self.ft = FractalTree(proportions=(1, 2, 3, 4, 5, 6, 7), tree_permutation_order=(4, 2, 7, 1, 6, 3, 5))