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

WassimTenachi / PhySO / #13

10 Jun 2024 12:28AM UTC coverage: 52.052% (-30.3%) from 82.385%
#13

push

coveralls-python

WassimTenachi
Update requirements.txt

2980 of 5725 relevant lines covered (52.05%)

0.52 hits per line

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

48.21
/physo/physym/execute.py
1
from physo.physym import token as Tok
1✔
2

3
# ------------------------------------------------------------------------------------------------------------------
4
# ------------------------------------------------ SINGLE EXECUTION ------------------------------------------------
5
# ------------------------------------------------------------------------------------------------------------------
6

7
def ExecuteProgram (input_var_data, program_tokens, class_free_consts_vals=None, spe_free_consts_vals=None):
1✔
8
    """
9
    Executes a symbolic function program.
10
    Parameters
11
    ----------
12
    input_var_data : torch.tensor of shape (n_dim, ?,) of float
13
        Values of the input variables of the problem with n_dim = nb of input variables.
14
    program_tokens : list of token.Token
15
        Symbolic function program in reverse Polish notation order.
16
    class_free_consts_vals : torch.tensor of shape (n_class_free_const,) or of shape (n_class_free_const, ?) of float or None
17
        Values of class free constants to use for program execution. Works with either a single value for each constant
18
        (shape (n_class_free_const,)) or a value for each constant and each data point (shape (n_class_free_const, ?)).
19
        class_free_consts_vals must be given if program_tokens contains one or more class free constants.
20
    spe_free_consts_vals : torch.tensor of shape (n_spe_free_const,) or of shape (n_spe_free_const, ?) of float or None
21
        Values of spe free constants to use for program execution. Works with either a single value for each constant
22
        (shape (n_spe_free_const,)) or a value for each constant and each data point (shape (n_spe_free_const, ?)).
23
        spe_free_consts_vals must be given if program_tokens contains one or more spe free constants.
24
    Returns
25
    -------
26
    y : torch.tensor of shape (?,) of float
27
        Result of computation.
28
    """
29

30
    # Size
31
    (n_dim, data_size,) = input_var_data.shape
×
32

33
    # Number of tokens in the program
34
    n_tokens = len(program_tokens)
1✔
35

36
    # Current stack of computed results
37
    curr_stack = []
1✔
38

39
    # De-stacking program (iterating from last token to first)
40
    start = n_tokens - 1
1✔
41
    for i in range (start, -1, -1):
×
42
        token = program_tokens[i]
×
43
        # Terminal token
44
        if token.arity == 0:
1✔
45
            # Function type token
46
            if token.var_type == Tok.VAR_TYPE_OP:
×
47
                #curr_stack.append(token.function())
48
                raise ValueError("Function of arity = 0 encountered. Use var_type = %i for fixed constants."%(Tok.VAR_TYPE_FIXED_CONST))
×
49
            # Input variable (eg. x0, x1 etc.)
50
            elif token.var_type == Tok.VAR_TYPE_INPUT_VAR:
×
51
                curr_stack.append(input_var_data[token.var_id])
×
52
            # Class free constant variable (eg. c0, c1 etc.)
53
            elif token.var_type == Tok.VAR_TYPE_CLASS_FREE_CONST:
1✔
54
                if class_free_consts_vals is not None:
1✔
55
                    curr_stack.append(class_free_consts_vals[token.var_id])
×
56
                else:
57
                    raise ValueError("Class free constant encountered in program evaluation but class free constant values "
1✔
58
                                     "were not given.")
59
            # Spe free constant variable (eg. k0, k1 etc.)
60
            elif token.var_type == Tok.VAR_TYPE_SPE_FREE_CONST:
×
61
                if spe_free_consts_vals is not None:
×
62
                    curr_stack.append(spe_free_consts_vals[token.var_id])
×
63
                else:
64
                    raise ValueError("Spe free constant encountered in program evaluation but spe free constant values "
1✔
65
                                     "were not given.")
66
            # Fixed constant (eg. pi, 1 etc.)
67
            elif token.var_type == Tok.VAR_TYPE_FIXED_CONST:
×
68
                curr_stack.append(token.fixed_const)
×
69
            else:
70
                raise NotImplementedError("Token of unknown var_type encountered in ExecuteProgram.")
×
71
        # Non-terminal token
72
        elif token.arity > 0:
1✔
73
            # Last pending elements are those needed for next computation (in reverse order)
74
            args = curr_stack[-token.arity:][::-1]
1✔
75
            res = token.function(*args)
×
76
            # Removing those pending elements as they were used
77
            curr_stack = curr_stack[:-token.arity]
1✔
78
            # Appending last result to stack
79
            curr_stack.append(res)
×
80
    y = curr_stack[0]
1✔
81
    return y
×
82

83
def ComputeInfixNotation (program_tokens):
×
84
    """
85
    Computes infix str representation of a program.
86
    (which is the usual way to note symbolic function: +34 (in polish notation) = 3+4 (in infix notation))
87
    Parameters
88
    ----------
89
    program_tokens : list of token.Token
90
        List of tokens making up the program.
91
    Returns
92
    -------
93
    program_str : str
94
    """
95
    # Number of tokens in the program
96
    n_tokens = len(program_tokens)
1✔
97

98
    # Current stack of computed results
99
    curr_stack = []
1✔
100

101
    # De-stacking program (iterating from last token to first)
102
    start = n_tokens - 1
×
103
    for i in range (start, -1, -1):
1✔
104
        token = program_tokens[i]
1✔
105
        # Last pending elements are those needed for next computation (in reverse order)
106
        args = curr_stack[-token.arity:][::-1]
1✔
107
        if token.arity == 0:
1✔
108
            res = token.sympy_repr
1✔
109
        elif token.arity == 1:
1✔
110
            if token.is_power is True:
×
111
                pow = '{:g}'.format(token.power)  # without trailing zeros
1✔
112
                res = "((%s)**(%s))" % (args[0], pow)
1✔
113
            else:
114
                res = "%s(%s)" % (token.sympy_repr, args[0])
×
115
        elif token.arity == 2:
×
116
            res = "(%s%s%s)" % (args[0], token.sympy_repr, args[1])
×
117
        elif token.arity > 2 :
×
118
            args_str = ""
×
119
            for arg in args: args_str+="%s,"%arg
1✔
120
            args_str = args_str[:-1] # deleting last ","
×
121
            res = "%s(%s)" % (token.sympy_repr, args_str)
1✔
122
        if token.arity > 0:
×
123
            # Removing those pending elements as they were used
124
            curr_stack = curr_stack[:-token.arity]
1✔
125
        # Appending last result to stack
126
        curr_stack.append(res)
×
127
    return curr_stack[0]
×
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