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

WassimTenachi / PhySO / #16

28 Jul 2025 07:07AM UTC coverage: 70.145% (-10.8%) from 80.984%
#16

push

coveralls-python

WassimTenachi
fix

5963 of 8501 relevant lines covered (70.14%)

0.7 hits per line

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

54.95
/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_old_sympy = '                                     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
        expected_pretty_new_sympy = '                                      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✔
78
        result_pretty = my_programs.get_infix_pretty(prog_idx=0)
1✔
79
        # Compare without spaces as they can vary depending on sympy version
80
        expected_pretty_no_spaces = expected_pretty_new_sympy.replace(" ", "")
×
81
        result_pretty_no_spaces   = result_pretty.replace(" ", "")
×
82
        works_bool = expected_pretty_no_spaces == result_pretty_no_spaces
1✔
83
        self.assertTrue(works_bool)
1✔
84

85
        # TEST get_latex
86
        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}}}}'
×
87
        result_latex = my_programs.get_infix_latex(prog_idx=0)
×
88
        works_bool = expected_latex == result_latex
1✔
89
        self.assertTrue(works_bool)
1✔
90

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

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

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

117
        return None
1✔
118

119
    def test_tree_rpr(self):
×
120

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

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

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

160
            # TEST
161
            for my_programs in [my_programs_wo_dummies,my_programs_w_dummies]:
1✔
162
                # get_tree_latex
163
                try:
1✔
164
                    t0 = time.perf_counter()
×
165
                    tree_latex = my_programs.get_tree_latex(prog_idx=0,)
×
166
                    t1 = time.perf_counter()
×
167
                    print("\nget_tree_latex time = %.3f s"%(t1-t0))
1✔
168
                except:
1✔
169
                    print("Tree generation failed : get_tree_latex (Acceptable failure)")
1✔
170
                # get_tree_image
171
                try:
1✔
172
                    t0 = time.perf_counter()
×
173
                    img = my_programs.get_tree_image(prog_idx=0)
×
174
                    t1 = time.perf_counter()
×
175
                    print("\nget_tree_image time = %.3f s"%(t1-t0))
1✔
176
                except:
1✔
177
                    print("Tree generation failed : get_tree_image (Acceptable failure)")
1✔
178
                # get_tree_image_via_tex
179
                try:
1✔
180
                    t0 = time.perf_counter()
×
181
                    img = my_programs.get_tree_image_via_tex(prog_idx=0, fname="tree_repr_test")
×
182
                    t1 = time.perf_counter()
1✔
183
                    print("\nget_tree_image_via_tex time = %.3f s"%(t1-t0))
×
184
                except:
1✔
185
                    print("Tree generation failed : get_tree_image_via_tex (Acceptable failure)")
×
186
                # show_tree (via_tex=True)
187
                try:
×
188
                    t0 = time.perf_counter()
×
189
                    my_programs.show_tree(prog_idx=0, via_tex=True)
×
190
                    plt.close()
×
191
                    t1 = time.perf_counter()
×
192
                    print("\nshow_tree (via_tex=True) time = %.3f s"%(t1-t0))
×
193
                except:
×
194
                    print("Tree generation failed : show_tree (via_tex=True) (Acceptable failure)")
×
195
                # show_tree (via_tex=False)
196
                try:
×
197
                    t0 = time.perf_counter()
×
198
                    my_programs.show_tree(prog_idx=0, via_tex=False)
×
199
                    plt.close()
×
200
                    t1 = time.perf_counter()
×
201
                    print("\nshow_tree (via_tex=False) time = %.3f s"%(t1-t0))
×
202
                except:
×
203
                    print("Tree generation failed : show_tree (via_tex=False) (Acceptable failure)")
×
204
        return None
×
205

206
if __name__ == '__main__':
×
207
    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

© 2026 Coveralls, Inc