From 255b3f630f66509113628ce7e32498563a51dff4 Mon Sep 17 00:00:00 2001 From: "Zucker, Jeremy D" Date: Mon, 21 Oct 2024 13:46:57 -0700 Subject: [PATCH] Adding model_from_url for advection backward model --- docs/source/interfaces.ipynb | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/docs/source/interfaces.ipynb b/docs/source/interfaces.ipynb index cc463892..f93523b5 100644 --- a/docs/source/interfaces.ipynb +++ b/docs/source/interfaces.ipynb @@ -47,30 +47,26 @@ "metadata": {}, "outputs": [ { - "ename": "AttributeError", - "evalue": "'int' object has no attribute 'replace'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[3], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://raw.githubusercontent.com/DARPA-ASKEM/Model-Representations/pde-petri-amrs/petrinet/examples/pde/advection/advection_backward_1_0.01_3.json\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#res = requests.get(url)\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m#model_json = res.json()\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mmira\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msources\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mamr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel_from_url\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/sources/amr/__init__.py:27\u001b[0m, in \u001b[0;36mmodel_from_url\u001b[0;34m(url)\u001b[0m\n\u001b[1;32m 25\u001b[0m res \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mget(url)\n\u001b[1;32m 26\u001b[0m model_json \u001b[38;5;241m=\u001b[39m res\u001b[38;5;241m.\u001b[39mjson()\n\u001b[0;32m---> 27\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmodel_from_json\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_json\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/sources/amr/__init__.py:67\u001b[0m, in \u001b[0;36mmodel_from_json\u001b[0;34m(model_json)\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo schema defined in the AMR header. The schema \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 64\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhas to be a URL pointing to a JSON schema \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124magainst which the AMR is validated.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 66\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpetrinet\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m header[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mschema\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[0;32m---> 67\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mpetrinet\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtemplate_model_from_amr_json\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_json\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mregnet\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m header[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mschema\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[1;32m 69\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m regnet\u001b[38;5;241m.\u001b[39mtemplate_model_from_amr_json(model_json)\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/sources/amr/petrinet.py:121\u001b[0m, in \u001b[0;36mtemplate_model_from_amr_json\u001b[0;34m(model_json)\u001b[0m\n\u001b[1;32m 118\u001b[0m mira_parameters \u001b[38;5;241m=\u001b[39m {}\n\u001b[1;32m 119\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m parameter \u001b[38;5;129;01min\u001b[39;00m ode_semantics\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mparameters\u001b[39m\u001b[38;5;124m'\u001b[39m, []):\n\u001b[1;32m 120\u001b[0m mira_parameters[parameter[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m'\u001b[39m]] \u001b[38;5;241m=\u001b[39m \\\n\u001b[0;32m--> 121\u001b[0m \u001b[43mparameter_to_mira\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparameter\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparam_symbols\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msymbols\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 123\u001b[0m \u001b[38;5;66;03m# Next we process initial conditions\u001b[39;00m\n\u001b[1;32m 124\u001b[0m initials \u001b[38;5;241m=\u001b[39m {}\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/sources/util.py:193\u001b[0m, in \u001b[0;36mparameter_to_mira\u001b[0;34m(parameter, param_symbols)\u001b[0m\n\u001b[1;32m 190\u001b[0m processed_distr_parameters[param_key] \u001b[38;5;241m=\u001b[39m param_value\n\u001b[1;32m 191\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 192\u001b[0m processed_distr_parameters[param_key] \u001b[38;5;241m=\u001b[39m \\\n\u001b[0;32m--> 193\u001b[0m \u001b[43msafe_parse_expr\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparam_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 194\u001b[0m distr \u001b[38;5;241m=\u001b[39m Distribution(\n\u001b[1;32m 195\u001b[0m \u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39mdistr_type,\n\u001b[1;32m 196\u001b[0m parameters\u001b[38;5;241m=\u001b[39mprocessed_distr_parameters,\n\u001b[1;32m 197\u001b[0m )\n\u001b[1;32m 198\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/metamodel/utils.py:38\u001b[0m, in \u001b[0;36msafe_parse_expr\u001b[0;34m(s, local_dict)\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msafe_parse_expr\u001b[39m(s: \u001b[38;5;28mstr\u001b[39m, local_dict\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m sympy\u001b[38;5;241m.\u001b[39mExpr:\n\u001b[1;32m 37\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Parse an expression that may contain lambda functions.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 38\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m sympy\u001b[38;5;241m.\u001b[39mparse_expr(\u001b[43mget_parseable_expression\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m,\n\u001b[1;32m 39\u001b[0m local_dict\u001b[38;5;241m=\u001b[39m{get_parseable_expression(k): v\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m local_dict\u001b[38;5;241m.\u001b[39mitems()}\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m local_dict \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m)\n", - "File \u001b[0;32m~/.pyenv/versions/miniconda3-3.11-24.1.2-0/envs/pyciemss/lib/python3.12/site-packages/mira/metamodel/utils.py:16\u001b[0m, in \u001b[0;36mget_parseable_expression\u001b[0;34m(s)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Return an expression that can be parsed using sympy.\"\"\"\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;66;03m# Handle lambda which cannot be parsed by sympy\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m s \u001b[38;5;241m=\u001b[39m \u001b[43ms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreplace\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlambda\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mXXlambdaXX\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 17\u001b[0m \u001b[38;5;66;03m# Handle dots which also cannot be parsed\u001b[39;00m\n\u001b[1;32m 18\u001b[0m s \u001b[38;5;241m=\u001b[39m re\u001b[38;5;241m.\u001b[39msub(\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124m.(?=\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mD)\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mXX_XX\u001b[39m\u001b[38;5;124m'\u001b[39m, s)\n", - "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'replace'" - ] + "data": { + "text/plain": [ + "TemplateModel(templates=[NaturalDegradation(rate_law=0.01*t, name='r_a_0_0_boundary', display_name=None, type='NaturalDegradation', subject=Concept(name='a_0', display_name='a_0', description=None, identifiers={}, context={}, units=None), provenance=[]), NaturalConversion(rate_law=-a_0*u/dx, name='r_a_0_1_0', display_name=None, type='NaturalConversion', subject=Concept(name='a_1', display_name='a_1', description=None, identifiers={}, context={}, units=None), outcome=Concept(name='a_0', display_name='a_0', description=None, identifiers={}, context={}, units=None), provenance=[]), NaturalConversion(rate_law=-a_1*u/dx, name='r_a_0_2_1', display_name=None, type='NaturalConversion', subject=Concept(name='a_2', display_name='a_2', description=None, identifiers={}, context={}, units=None), outcome=Concept(name='a_1', display_name='a_1', description=None, identifiers={}, context={}, units=None), provenance=[]), NaturalProduction(rate_law=-a_2*u/dx, name='r_a_0_boundary_2', display_name=None, type='NaturalProduction', outcome=Concept(name='a_2', display_name='a_2', description=None, identifiers={}, context={}, units=None), provenance=[])], parameters={'u': Parameter(name='u', display_name=None, description=None, identifiers={}, context={}, units=None, value=1.0, distribution=Distribution(type='StandardUniform1', parameters={'minimum': 0.0, 'maximum': 1.0})), 'dx': Parameter(name='dx', display_name=None, description=None, identifiers={}, context={}, units=None, value=1.0, distribution=Distribution(type='StandardUniform1', parameters={'minimum': 0.0, 'maximum': 1.0}))}, initials={'a_0': Initial(concept=Concept(name='a_0', display_name='a_0', description=None, identifiers={}, context={}, units=None), expression=0.4), 'a_1': Initial(concept=Concept(name='a_1', display_name='a_1', description=None, identifiers={}, context={}, units=None), expression=1.0), 'a_2': Initial(concept=Concept(name='a_2', display_name='a_2', description=None, identifiers={}, context={}, units=None), expression=0.4)}, observables={}, annotations=Annotations(name='Advection Model', description='Advection as Petrinet model created by Dan Bryce and Drisana Iverson (Mosaphir)', license=None, authors=[], references=[], time_scale=None, time_start=None, time_end=None, locations=[], pathogens=[], diseases=[], hosts=[], model_types=[]), time=Time(name='t', units=Unit(expression=day)))" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "import requests\n", "import mira\n", + "from mira.sources.amr import model_from_url\n", + "\n", "url = 'https://raw.githubusercontent.com/DARPA-ASKEM/Model-Representations/pde-petri-amrs/petrinet/examples/pde/advection/advection_backward_1_0.01_3.json'\n", "#res = requests.get(url)\n", "#model_json = res.json()\n", - "model = mira.sources.amr.model_from_url(url)" + "model = model_from_url(url)\n", + "model" ] }, { @@ -3359,7 +3355,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "pyciemss", "language": "python", "name": "python3" },