Example #1
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))
Example #2
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 #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
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 #5
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)
 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)
class Test(TestCase):
    def setUp(self) -> None:
        self.ft = FractalTree(value=10)

    def test_1(self):
        # with self.assertRaises(ValueError):
        self.ft.generate_children(number_of_children=0)
        result = [None]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_2(self):
        ft = self.ft
        ft.generate_children(number_of_children=1)
        result = [3]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_3(self):
        ft = self.ft
        ft.generate_children(number_of_children=2)
        result = [3, 2]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_4(self):
        ft = self.ft
        ft.generate_children(number_of_children=3)
        result = [3, 1, 2]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_5(self):
        ft = self.ft
        with self.assertRaises(ValueError):
            ft.generate_children(number_of_children=4)

    def test_6(self):
        ft = self.ft
        ft.generate_children(number_of_children=(1, 1, 1))
        result = [[3], [3], [3]]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_7(self):
        ft = self.ft
        ft.generate_children(number_of_children=(0, 1, 2))
        result = [[2, 3], 1, [3]]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_8(self):
        ft = self.ft
        ft.generate_children(number_of_children=(1, 2, (1, 2, 3)))
        result = [
            [[3], [2, 3], [3, 1, 2]],
            [3],
            [2, 3]
        ]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    def test_9(self):
        ft = self.ft
        ft.generate_children(
            number_of_children=(
                (1, 3),
                2,
                (1,
                 (1, 3),
                 3)
            )
        )
        result = [
            [
                [3],
                [
                    [3],
                    [2, 3, 1]
                ],
                [3, 1, 2]
            ],
            [
                [3, 1, 2],
                [3]
            ],
            [2, 3]
        ]
        self.assertEqual(result, self.ft.get_leaves(key=lambda leaf: leaf.fractal_order))

    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)

    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)
Example #8
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)