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])
Example #2
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])
Example #3
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)
Example #4
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)
Example #5
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))
 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)
Example #7
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)
Example #8
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)
 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)
Example #10
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)
Example #11
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)])
Example #12
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])
Example #13
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')
Example #14
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)
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)
        ])
Example #17
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)
Example #18
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)))
Example #19
0
class TestFractal(TestCase):
    def setUp(self) -> None:
        self.pdf = Pdf(orientation='l')
        self.ft = FractalTree(value=20)
        self.ft.add_layer()
        self.factor = 2
        fill_tree_with_columns(self.ft, self.factor)

    def test_draw(self):
        with self.file_path(path, 'draw', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            self.ft.manual_graphic.draw(self.pdf)
            self.pdf.write(pdf_path)

    def test_draw_with_text(self):
        leaf = self.ft.get_leaves()[1]
        leaf_start_mark_line = leaf.manual_graphic.draw_objects[
            0].start_mark_line
        leaf_start_mark_line.add_label('bla')
        leaf_start_mark_line.add_label('blue')
        row = self.ft.manual_graphic.draw_objects[1]
        row.top_margin = max([
            do.draw_objects[0].start_mark_line.get_above_text_labels_height()
            for do in row.draw_objects
        ])
        print(row.top_margin)
        with self.file_path(path, 'draw_with_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)
            self.ft.manual_graphic.draw(self.pdf)
            self.pdf.write(pdf_path)

    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_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)
Example #20
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)
Example #21
0
 def test_3(self):
     ft = FractalTree()
     with self.assertRaises(SetValueFirst):
         ft.add_layer()
Example #22
0
 def test_2(self):
     ft = FractalTree()
     ft.value = 50
     ft.add_layer()
     with self.assertRaises(FractalTreeException):
         ft.value = 100
Example #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)
Example #24
0
 def setUp(self) -> None:
     ft = FractalTree(value=10, reading_direction='vertical')
     ft.add_layer()
     ft.add_layer()
     self.ft = ft.get_children()[1]
     self.deep_copied = self.ft.__deepcopy__()
Example #25
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