-
Notifications
You must be signed in to change notification settings - Fork 7
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
template_model_from_askenet_json cannot read json from AskeNetPetriNetModel #183
Comments
I think the issue here is mainly that the model you're ingesting is an older one and the representation has changed since then. For instance, the model attribute of |
Hi @bgyori That makes sense. We have the same issues, so we are tagging our repos for the demos and hackathons so that it is still reproducible. Are there any mira models that can be converted to AMR? Even the ensemble models run into the same problem. from mira.modeling.askenet.petrinet import AskeNetPetriNetModel
import mira
import requests
url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/ensemble/BIOMD0000000955_template_model.json'
res = requests.get(url)
model_json = res.json()
sidarthe_template = mira.metamodel.TemplateModel.from_json(model_json)
sidarthe_model = mira.modeling.Model(sidarthe_template)
askenet_sidarthe = AskeNetPetriNetModel(sidarthe_model) ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[12], line 12
10 sidarthe_template = mira.metamodel.TemplateModel.from_json(model_json)
11 sidarthe_model = mira.modeling.Model(sidarthe_template)
---> 12 askenet_sidarthe = AskeNetPetriNetModel(sidarthe_model)
File ~/Projects/Proposals/ASKEM/mira/mira/modeling/askenet/petrinet.py:103, in AskeNetPetriNetModel.__init__(self, model)
92 obs_data = {
93 'id': observable.observable.name,
94 'name': observable.observable.name,
(...)
97 observable.observable.expression),
98 }
99 self.observables.append(obs_data)
101 self.time = {
102 'id': model.template_model.time.name,
--> 103 } if model.template_model.time else None
105 # Transition structure
106 # {
107 # "id": "t1",
(...)
117 # expression_mathml
118 # }
119 for idx, transition in enumerate(model.transitions.values()):
AttributeError: 'TemplateModel' object has no attribute 'time' |
I can't reproduce these errors. I'm on the latest |
Oh I think @kkaris is right, I just looked at the code and guessed instead of testing it. It does seem to work well if you're using the latest MIRA code. |
Ah thanks! I no longer get that error when I use the latest mira. But now when I call: mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_sidarthe.to_json()) I get the same ValueError: Error from parse_expr with transformed code: 'Infected *lambda '
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3505 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[15], line 1
mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_sidarthe.to_json())
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:194 in template_model_from_askenet_json
templates.extend(transition_to_templates(rate_obj,
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:259 in transition_to_templates
rate_law = sympy.parse_expr(rate_law_expression, local_dict=symbols) \
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1101 in parse_expr
raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1092 in parse_expr
rv = eval_expr(code, local_dict, global_dict)
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:907 in eval_expr
expr = eval(
File <string>:1
Infected *lambda
^
SyntaxError: invalid syntax The same thing happens with the Zongo 2020 model for the same reason: def convert_mira_template_to_askenet_json(url):
res = requests.get(url)
model_json = res.json()
model_template = mira.metamodel.TemplateModel.from_json(model_json)
mira_model = mira.modeling.Model(model_template)
askenet_model = AskeNetPetriNetModel(mira_model)
return askenet_model.to_json()
zongo_url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/ensemble/BIOMD0000000983_template_model.json'
askenet_json = convert_mira_template_to_askenet_json(zongo_url)
template_from_askenet = mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_json) ValueError: Error from parse_expr with transformed code: 'Quarantined *lambda *theta '
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3505 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[21], line 3
template_from_askenet = mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_json)
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:194 in template_model_from_askenet_json
templates.extend(transition_to_templates(rate_obj,
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:259 in transition_to_templates
rate_law = sympy.parse_expr(rate_law_expression, local_dict=symbols) \
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1101 in parse_expr
raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1092 in parse_expr
rv = eval_expr(code, local_dict, global_dict)
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:907 in eval_expr
expr = eval(
File <string>:1
Quarantined *lambda *theta
^
SyntaxError: invalid syntax Obviously both of these are sympy parse errors based on the reserved word Paiva2020_url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/ensemble/BIOMD0000000960_template_model.json'
askenet_json = convert_mira_template_to_askenet_json(Paiva2020_url)
template_from_askenet = mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_json) ---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In[22], line 4
1 Paiva2020_url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/ensemble/BIOMD0000000960_template_model.json'
3 askenet_json = convert_mira_template_to_askenet_json(Paiva2020_url)
----> 4 template_from_askenet = mira.sources.askenet.petrinet.template_model_from_askenet_json(askenet_json)
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:103, in template_model_from_askenet_json(model_json)
101 mira_parameters = {}
102 for parameter in ode_semantics.get('parameters', []):
--> 103 mira_parameters[parameter['id']] = parameter_to_mira(parameter)
104 symbols[parameter['id']] = sympy.Symbol(parameter['id'])
106 param_values = {
107 p['id']: p['value'] for p in ode_semantics.get('parameters', [])
108 }
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:250, in parameter_to_mira(parameter)
247 """Return a MIRA parameter from a parameter"""
248 distr = Distribution(**parameter['distribution']) \
249 if parameter.get('distribution') else None
--> 250 return Parameter(name=parameter['id'],
251 value=parameter.get('value'),
252 distribution=distr)
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
ValidationError: 1 validation error for Parameter
value
none is not an allowed value (type=type_error.none.not_allowed) |
Hi folks, Sorry for hammering you with bug reports, but I was trying to figure out a pathway to success, so I thought I would roll my own AMR file: This AMR file was generated by hand using the mira modeling tools in this notebook: import mira
from mira.sources.askenet.petrinet import template_model_from_askenet_json
import requests
url = 'https://raw.githubusercontent.com/ciemss/pyciemss/177-examples-for-ta2-model-representation-issues/notebook/Examples_for_TA2_Model_Representation/SIAERDH_AMR.json'
res = requests.get(url)
model_json = res.json()
model = template_model_from_askenet_json(model_json) ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: Error from parse_expr with transformed code: 'S *beta *(A +I *delta )/N '
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Cell In[23], line 7
5 res = requests.get(url)
6 model_json = res.json()
----> 7 model = template_model_from_askenet_json(model_json)
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:194, in template_model_from_askenet_json(model_json)
191 output_concepts = [concepts[i] for i in outputs]
192 controller_concepts = [concepts[i] for i in controllers]
--> 194 templates.extend(transition_to_templates(rate_obj,
195 input_concepts,
196 output_concepts,
197 controller_concepts,
198 symbols))
200 # Finally, we gather some model-level annotations
201 name = model_json.get('name')
File ~/Projects/Proposals/ASKEM/mira/mira/sources/askenet/petrinet.py:259, in transition_to_templates(transition_rate, input_concepts, output_concepts, controller_concepts, symbols)
257 """Return a list of templates from a transition"""
258 rate_law_expression = transition_rate.get('expression')
--> 259 rate_law = sympy.parse_expr(rate_law_expression, local_dict=symbols) \
260 if rate_law_expression else None
261 if not controller_concepts:
262 if not input_concepts:
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1101, in parse_expr(s, local_dict, transformations, global_dict, evaluate)
1099 for i in local_dict.pop(null, ()):
1100 local_dict[i] = null
-> 1101 raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1092, in parse_expr(s, local_dict, transformations, global_dict, evaluate)
1089 code = compile(evaluateFalse(code), '<string>', 'eval')
1091 try:
-> 1092 rv = eval_expr(code, local_dict, global_dict)
1093 # restore neutral definitions for names
1094 for i in local_dict.pop(null, ()):
File ~/.pyenv/versions/jz-sviivr/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:907, in eval_expr(code, local_dict, global_dict)
901 def eval_expr(code, local_dict: DICT, global_dict: DICT):
902 """
903 Evaluate Python code generated by ``stringify_expr``.
904
905 Generally, ``parse_expr`` should be used.
906 """
--> 907 expr = eval(
908 code, global_dict, local_dict) # take local objects in preference
909 return expr
File <string>:1
TypeError: unsupported operand type(s) for /: 'Mul' and 'function' |
I debugged this and I found that the issue is that the model doesn't seem to define |
I was able to read the SIDARTHE AMR and MIRA template models into mira by renaming |
That's weird, renaming shouldn't be necessary. Could you send me the code you are using to read the model? |
Here you go: import requests
import mira
from mira.modeling.askenet.petrinet import AskeNetPetriNetModel
from mira.sources.askenet.petrinet import template_model_from_askenet_json
url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/evaluation_2023.01/scenario2_sidarthe_mira.json'
#url = 'https://raw.githubusercontent.com/indralab/mira/main/notebooks/evaluation_2023.01/scenario1_sir_mira.json'
res = requests.get(url)
model_json = res.json()
mira_template_model = mira.metamodel.TemplateModel.from_json(model_json)
mira_model = mira.modeling.Model(mira_template_model)
askenet_model = AskeNetPetriNetModel(mira_model)
askenet_mira_template_model = template_model_from_askenet_json(askenet_model.to_json())
askenet_mira_model = mira.modeling.Model(askenet_mira_template_model) results in: ValueError: Error from parse_expr with transformed code: 'Infected *lambda '
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3442 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[4], line 13
askenet_mira_template_model = template_model_from_askenet_json(askenet_model.to_json())
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/mira/sources/askenet/petrinet.py:194 in template_model_from_askenet_json
templates.extend(transition_to_templates(rate_obj,
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/mira/sources/askenet/petrinet.py:259 in transition_to_templates
rate_law = sympy.parse_expr(rate_law_expression, local_dict=symbols) \
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1087 in parse_expr
raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1078 in parse_expr
rv = eval_expr(code, local_dict, global_dict)
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:906 in eval_expr
expr = eval(
File <string>:1
Infected *lambda
^
SyntaxError: invalid syntax |
Also: import requests
from mira.sources.askenet.petrinet import template_model_from_askenet_json
amr_url = 'https://raw.githubusercontent.com/ciemss/pyciemss/724b848b11294c4c2c5eb6b53c3eac3670783940/test/models/AMR_examples/BIOMD0000000955_askenet.json'
res = requests.get(amr_url)
amr_model_json=res.json()
amr_model = template_model_from_askenet_json(amr_model_json) results in: ValueError: Error from parse_expr with transformed code: 'Infected *lambda '
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3442 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[6], line 6
amr_model = template_model_from_askenet_json(amr_model_json)
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/mira/sources/askenet/petrinet.py:194 in template_model_from_askenet_json
templates.extend(transition_to_templates(rate_obj,
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/mira/sources/askenet/petrinet.py:259 in transition_to_templates
rate_law = sympy.parse_expr(rate_law_expression, local_dict=symbols) \
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1087 in parse_expr
raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:1078 in parse_expr
rv = eval_expr(code, local_dict, global_dict)
File ~/.pyenv/versions/pyciemss-main/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py:906 in eval_expr
expr = eval(
File <string>:1
Infected *lambda
^
SyntaxError: invalid syntax |
@djinnome this was indeed a bug, I fixed it so if you get the latest code, the two examples above work. |
I can confirm that the two examples work in my hands |
* Error: gyorilab/mira#182 * Multiple controllers are added together * still getting pydantic error * Error: gyorilab/mira#183 * added more .gitignore because of silly things like .idea * added sidarthe amr * Still can't parse SIDARTHE model using mira yet * removed offending lambda to XXlambdaXX in AMR * SIDARTHE MIRA model and SIDARTHE AMR model produce same outputs * cleaned up test_askenet to have more understandable variable names * Confirmed that all of Sabina's models can be converted to ASKEM and loaded into a ScaledBetaNoisePetriNetODESystem without errors. Next up: try to interpret the sympy rate law * AMR can have lambda and it will still pass all tests. (Mira template still needs XXlambdaXX)
* Started notebook for examples regarding TA2 issues for model validation (#178) * Started notebook for examples regarding TA2 issues for model validation * Started notebook to produce AskeNet Model Representation examples in JSON format. Note: created JSON dictionary successfully; Error creating JSON fileg * Success for SIAERDH model conversion to AMR * created AMR for Model2 from custom models (SEIARHD), see JSON file * added total_population as a parameter * Deaceased -> Deceased * Fixed a few more minor errors * added SIRHD and SEIARHDS models * added SIRHD and SEIARHDS models --------- Co-authored-by: Jeremy Zucker <[email protected]> Co-authored-by: sabinala <[email protected]> Co-authored-by: Jeremy Zucker <[email protected]> * remove .idea * gitignore * Update SEIARHD_AMR.json removed git conflicts in favor of local * Fixing issue in deriv method (#187) * Error: gyorilab/mira#182 * Multiple controllers are added together * still getting pydantic error * Error: gyorilab/mira#183 * added more .gitignore because of silly things like .idea * added sidarthe amr * Still can't parse SIDARTHE model using mira yet * removed offending lambda to XXlambdaXX in AMR * SIDARTHE MIRA model and SIDARTHE AMR model produce same outputs * cleaned up test_askenet to have more understandable variable names * Confirmed that all of Sabina's models can be converted to ASKEM and loaded into a ScaledBetaNoisePetriNetODESystem without errors. Next up: try to interpret the sympy rate law * AMR can have lambda and it will still pass all tests. (Mira template still needs XXlambdaXX) * Changes to interfaces to support data IO for TA4 (#190) * added data * added interface_utils * Added updated demo with data processing * revised interfaces with AMR * removed old file * added test for interfaces demo * added results * Add detailed docstrings for TA4 interfaces (#191) * added detailed docstrings for askem interfaces * added return description * indent error * Removed untested `add_uncertainty` from TA4 interfaces (#192) * removed add_uncertainty from TA4 interfaces, as they are not thoroughly tested * fixed test * Add parameter interventions to TA4 interfaces (#193) * first pass at adding interventions to interfaces. Incomplete formatting. * lingering notebook * adding interventions to outputs * messy but it works. * Removed test_samples_intervened_values because it compares timepoint with timepoint_id, which will not work. Added test_load_and_sample_petri_net to confirm it outputs the correct dataframe under intervention * adde expected intervened samples * added load_and_calibrate_and_sample_petri * Cleaned up demo.ipynb * Cleaned up demo.ipynb * cleaned up documentation of assign_interventions_to_timepoints --------- Co-authored-by: Jeremy Zucker <[email protected]> * End-to-end integration of ensemble modeling w/ TA4 (#194) * first pass at ensemble interface for TA4 * processed output of ensemble modeling * added ensemble calibration interface * added tests and docstrings * added timepoints to ensemble output processing * removed lingering files (#199) --------- Co-authored-by: ShantMahserejian <[email protected]> Co-authored-by: Jeremy Zucker <[email protected]> Co-authored-by: sabinala <[email protected]> Co-authored-by: Jeremy Zucker <[email protected]> Co-authored-by: Sam Witty <[email protected]>
* Error: gyorilab/mira#182 * Multiple controllers are added together * still getting pydantic error * Error: gyorilab/mira#183 * added more .gitignore because of silly things like .idea * added sidarthe amr * Still can't parse SIDARTHE model using mira yet * removed offending lambda to XXlambdaXX in AMR * SIDARTHE MIRA model and SIDARTHE AMR model produce same outputs * cleaned up test_askenet to have more understandable variable names * Confirmed that all of Sabina's models can be converted to ASKEM and loaded into a ScaledBetaNoisePetriNetODESystem without errors. Next up: try to interpret the sympy rate law * AMR can have lambda and it will still pass all tests. (Mira template still needs XXlambdaXX)
Hi folks,
Trying to convert a simpler mira model to AMR:
I get this error:
The text was updated successfully, but these errors were encountered: