• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WassimTenachi / PhySO / #15

10 Jun 2024 12:47AM UTC coverage: 80.984% (+28.9%) from 52.052%
#15

push

coveralls-python

WassimTenachi
monitoring test

26 of 27 new or added lines in 1 file covered. (96.3%)

131 existing lines in 18 files now uncovered.

6814 of 8414 relevant lines covered (80.98%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.87
/physo/physym/tests/program_display_UnitTest.py
1
import unittest
1✔
2
import warnings
1✔
3

4
import matplotlib.pyplot as plt
1✔
5
import numpy as np
1✔
6
import time as time
1✔
7
import platform
1✔
8

9
# Internal imports
10
from physo.physym import library as Lib
1✔
11
from physo.physym import vect_programs as VProg
1✔
12

13

14
def make_lib():
1✔
15
    # LIBRARY CONFIG
16
    args_make_tokens = {
×
17
                    # operations
18
                    "op_names"             : "all",  # or ["mul", "neg", "inv", "sin"]
19
                    "use_protected_ops"    : False,
20
                    # input variables
21
                    "input_var_ids"        : {"x" : 0         , "v" : 1          , "t" : 2,        },
22
                    "input_var_units"      : {"x" : [1, 0, 0] , "v" : [1, -1, 0] , "t" : [0, 1, 0] },
23
                    "input_var_complexity" : {"x" : 0.        , "v" : 1.         , "t" : 0.,       },
24
                    # constants
25
                    "constants"            : {"pi" : np.pi     , "c" : 3e8       , "M" : 1e6       },
26
                    "constants_units"      : {"pi" : [0, 0, 0] , "c" : [1, -1, 0], "M" : [0, 0, 1] },
27
                    "constants_complexity" : {"pi" : 0.        , "c" : 0.        , "M" : 1.        },
28
                    # free constants
29
                    "free_constants"            : {"c0"             , "c1"               , "c2"             },
30
                    "free_constants_init_val"   : {"c0" : 1.        , "c1"  : 10.        , "c2" : 1.        },
31
                    "free_constants_units"      : {"c0" : [0, 0, 0] , "c1"  : [1, -1, 0] , "c2" : [0, 0, 1] },
32
                    "free_constants_complexity" : {"c0" : 0.        , "c1"  : 0.         , "c2" : 1.        },
33
                           }
34

35
    my_lib = Lib.Library(args_make_tokens = args_make_tokens,
×
36
                         superparent_units = [1, -2, 1], superparent_name = "y")
37
    return my_lib
×
38

39

40
class DisplayTest(unittest.TestCase):
1✔
41

42
    # ------------------------------------------------------------------------------------------------------------------
43
    # --------------------------------------------- PROGRAM REPRESENTATION ---------------------------------------------
44
    # ------------------------------------------------------------------------------------------------------------------
45

46
    def test_infix_repr(self):
1✔
47
        # LIBRARY CONFIG
48
        args_make_tokens = {
1✔
49
                        # operations
50
                        "op_names"             : "all",  # or ["mul", "neg", "inv", "sin"]
51
                        "use_protected_ops"    : True,
52
                        # input variables
53
                        "input_var_ids"        : {"x" : 0         , "v" : 1          , "t" : 2,        },
54
                        "input_var_units"      : {"x" : [1, 0, 0] , "v" : [1, -1, 0] , "t" : [0, 1, 0] },
55
                        "input_var_complexity" : {"x" : 0.        , "v" : 1.         , "t" : 0.,       },
56
                        # constants
57
                        "constants"            : {"pi" : np.pi     , "c" : 3e8       , "M" : 1e6       , "const1" : 1         },
58
                        "constants_units"      : {"pi" : [0, 0, 0] , "c" : [1, -1, 0], "M" : [0, 0, 1] , "const1" : [0, 0, 0] },
59
                        "constants_complexity" : {"pi" : 0.        , "c" : 0.        , "M" : 1.        , "const1" : 1.        },
60
                            }
61
        my_lib = Lib.Library(args_make_tokens = args_make_tokens,
1✔
62
                             superparent_units = [1, -2, 1], superparent_name = "y")
63

64
        # TEST PROGRAM
65
        test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "const1", "div", "n2", "v", "n2",
1✔
66
                            "c", "cos", "div", "sub", "const1", "div", "v", "c", "div", "v", "c"]
67
        test_program_idx = np.array([my_lib.lib_name_to_idx[tok_str] for tok_str in test_program_str])
1✔
68
        test_program_length = len(test_program_str)
1✔
69
        test_program_idx = test_program_idx[np.newaxis, :]
1✔
70

71
        # BATCH
72
        my_programs = VProg.VectPrograms(batch_size=1, max_time_step=test_program_length, library=my_lib, n_realizations=1)
1✔
73
        my_programs.set_programs(test_program_idx)
1✔
74

75
        # TEST get_pretty
76
        expected_pretty = '                                      2       \n     2    ⎛c⋅const₁    ⎞           M⋅c        \n- M⋅c ⋅cos⎜──────── - 1⎟ + ───────────────────\n          ⎝   v        ⎠         _____________\n                                ╱           2 \n                               ╱           v  \n                              ╱   const₁ - ── \n                             ╱              2 \n                           ╲╱              c  '
1✔
77
        result_pretty = my_programs.get_infix_pretty(prog_idx=0)
1✔
78
        works_bool = expected_pretty == result_pretty
1✔
79
        self.assertTrue(works_bool)
1✔
80

81
        # TEST get_latex
82
        expected_latex = '- M c^{2} \\cos{\\left(\\frac{c const_{1}}{v} - 1 \\right)} + \\frac{M c^{2}}{\\sqrt{const_{1} - \\frac{v^{2}}{c^{2}}}}'
1✔
83
        result_latex = my_programs.get_infix_latex(prog_idx=0)
1✔
84
        works_bool = expected_latex == result_latex
1✔
85
        self.assertTrue(works_bool)
1✔
86

87
        # TEST get_sympy
88
        t0 = time.perf_counter()
1✔
89
        N = int(1e4)
1✔
90
        for _ in range (N):
1✔
91
            my_programs.get_prog(0).get_infix_sympy()
1✔
92
        t1 = time.perf_counter()
1✔
93
        print("get_infix_sympy time = %.3f ms"%((t1-t0)*1e3/N))
1✔
94

95
        # TEST get_infix_str
96
        t0 = time.perf_counter()
1✔
97
        N = int(1e4)
1✔
98
        for _ in range (N):
1✔
99
            my_programs.get_prog(0).get_infix_str()
1✔
100
        t1 = time.perf_counter()
1✔
101
        print("get_infix_str time = %.3f ms"%((t1-t0)*1e3/N))
1✔
102

103
        # TEST
104
        try:
1✔
105
            t0 = time.perf_counter()
1✔
106
            img = my_programs.get_infix_image(prog_idx=0,)
1✔
107
            # my_programs.show_infix(prog_idx=0,)
108
            t1 = time.perf_counter()
1✔
109
            print("\nget_infix_image time = %.3f s" % (t1 - t0))
1✔
110
        except:
×
111
            print("Infix generation failed : get_infix_image (Acceptable failure)")
×
112

113
        return None
1✔
114

115
    def test_tree_rpr(self):
1✔
116

117
        if platform.system() == "Windows":
1✔
118
            print("Not testing tree representation features on Windows as this generally causes problems and is only "
×
119
                  "useful for physo developers.")
120
        else:
121
            # LIBRARY CONFIG
122
            args_make_tokens = {
1✔
123
                            # operations
124
                            "op_names"             : "all",  # or ["mul", "neg", "inv", "sin"]
125
                            "use_protected_ops"    : True,
126
                            # input variables
127
                            "input_var_ids"        : {"x" : 0         , "v" : 1          , "t" : 2,        },
128
                            "input_var_units"      : {"x" : [1, 0, 0] , "v" : [1, -1, 0] , "t" : [0, 1, 0] },
129
                            "input_var_complexity" : {"x" : 0.        , "v" : 1.         , "t" : 0.,       },
130
                            # constants
131
                            "constants"            : {"pi" : np.pi     , "c" : 3e8       , "M" : 1e6       , "const1" : 1         },
132
                            "constants_units"      : {"pi" : [0, 0, 0] , "c" : [1, -1, 0], "M" : [0, 0, 1] , "const1" : [0, 0, 0] },
133
                            "constants_complexity" : {"pi" : 0.        , "c" : 0.        , "M" : 1.        , "const1" : 1.        },
134
                                }
135
            my_lib = Lib.Library(args_make_tokens = args_make_tokens,
1✔
136
                                 superparent_units = [1, -2, 1], superparent_name = "y")
137

138
            # TEST PROGRAM WO DUMMIES
139
            test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "const1", "div", "n2", "v", "n2",
1✔
140
                                "c", "cos", "div", "sub", "const1", "div", "v", "c", "div", "v", "c"]
141
            test_program_idx = np.array([my_lib.lib_name_to_idx[tok_str] for tok_str in test_program_str])
1✔
142
            test_program_length = len(test_program_str)
1✔
143
            test_program_idx = test_program_idx[np.newaxis, :]
1✔
144
            my_programs_wo_dummies = VProg.VectPrograms(batch_size=1, max_time_step=test_program_length, library=my_lib, n_realizations=1)
1✔
145
            my_programs_wo_dummies.set_programs(test_program_idx)
1✔
146

147
            # TEST PROGRAM W DUMMIES
148
            test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "const1", "div", "n2", "v", "n2",
1✔
149
                                "c", "cos", "div", "sub", "const1", "div", "v", "c", "div", "v",]
150
            test_program_idx = np.array([my_lib.lib_name_to_idx[tok_str] for tok_str in test_program_str])
1✔
151
            test_program_length = len(test_program_str) + 1
1✔
152
            test_program_idx = test_program_idx[np.newaxis, :]
1✔
153
            my_programs_w_dummies = VProg.VectPrograms(batch_size=1, max_time_step=test_program_length, library=my_lib, n_realizations=1)
1✔
154
            my_programs_w_dummies.set_programs(test_program_idx)
1✔
155

156
            # TEST
157
            for my_programs in [my_programs_wo_dummies,my_programs_w_dummies]:
1✔
158
                # get_tree_latex
159
                try:
1✔
160
                    t0 = time.perf_counter()
1✔
161
                    tree_latex = my_programs.get_tree_latex(prog_idx=0,)
1✔
162
                    t1 = time.perf_counter()
1✔
163
                    print("\nget_tree_latex time = %.3f s"%(t1-t0))
1✔
164
                except:
×
165
                    print("Tree generation failed : get_tree_latex (Acceptable failure)")
×
166
                # get_tree_image
167
                try:
1✔
168
                    t0 = time.perf_counter()
1✔
169
                    img        = my_programs.get_tree_image(prog_idx=0)
1✔
170
                    t1 = time.perf_counter()
1✔
171
                    print("\nget_tree_image time = %.3f s"%(t1-t0))
1✔
172
                except:
×
173
                    print("Tree generation failed : get_tree_image (Acceptable failure)")
×
174
                # get_tree_image_via_tex
175
                try:
1✔
176
                    t0 = time.perf_counter()
1✔
177
                    img        = my_programs.get_tree_image_via_tex(prog_idx=0)
1✔
178
                    t1 = time.perf_counter()
1✔
179
                    print("\nget_tree_image_via_tex time = %.3f s"%(t1-t0))
1✔
180
                except:
×
UNCOV
181
                    print("Tree generation failed : get_tree_image_via_tex (Acceptable failure)")
×
182
        return None
1✔
183

184
if __name__ == '__main__':
1✔
185
    unittest.main(verbosity=2)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc