Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sympy parsing workaround for specific tokens #5178

Merged
merged 11 commits into from
Oct 18, 2024
Merged

Conversation

mwdchang
Copy link
Member

@mwdchang mwdchang commented Oct 17, 2024

Summary

When rate-laws contain specific tokens sympy parsing tend to break as they are a part of reserved symbols. To parse them properly we encode them to something else, then revert them back when we need the parsing result.

So far, we have

  • lambda
  • S
  • Ci

For example:

>>> parse_expr("A")
A
>>> parse_expr("A + 1")
A + 1
>>> parse_expr("S + 1")
ValueError: Error from parse_expr with transformed code: 'S +Integer (1 )'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1087, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1078, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 906, in eval_expr
    expr = eval(
           ^^^^^
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'SingletonRegistry' and 'One'

Testing

Import this model
pascale-strata-problem-templates.json

You should be able to view it, open up parameter matrices, open up template matrices without causing JS errors.

Alternatively you can mess around in the engineering interface (http://localhost:8080/pyodide-test) ... note there are JS errors here because it does partial evaluations as you type, but the output (eval, symbols...etc) should make sense.

Closes #5161
Closes #5185

@mwdchang mwdchang marked this pull request as ready for review October 18, 2024 15:18
Copy link
Member

@YohannParis YohannParis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@blanchco blanchco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to work well

@mwdchang mwdchang merged commit 1a9ed32 into main Oct 18, 2024
4 checks passed
@mwdchang mwdchang deleted the sympy-parsing-workaround branch October 18, 2024 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: Stratify's Matrix Evaluate Expression [TASK]: Sympy equation parsing issues
4 participants