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

Fix unit processing for model ingestion #381

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mira/metamodel/template_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

from pydantic import ConfigDict

__all__ = [
Expand Down Expand Up @@ -473,6 +475,8 @@ def from_json(cls, data) -> "TemplateModel":
:
Returns the newly created template model.
"""
# Do a copy just to make sure we don't modify the original data
data = copy.deepcopy(data)
local_symbols = {p: sympy.Symbol(p) for p in data.get("parameters", [])}
for template_dict in data.get("templates", []):
# We need to figure out the template class based on the type
Expand Down
6 changes: 6 additions & 0 deletions mira/metamodel/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Regenerate the JSON schema by running ``python -m mira.metamodel.schema``.
"""
import copy

from pydantic import ConfigDict
from typing import Literal

Expand Down Expand Up @@ -376,6 +378,8 @@ def from_json(cls, data) -> "Concept":
if isinstance(data, Concept):
return data
elif data.get('units'):
# Copy so we don't update the input
data = copy.deepcopy(data)
data['units'] = Unit.from_json(data['units'])

return cls(**data)
Expand Down Expand Up @@ -413,6 +417,8 @@ def from_json(cls, data, rate_symbols=None) -> "Template":
:
A Template object
"""
# Make a copy to make sure we don't update the input
data = copy.deepcopy(data)
# We make sure to use data such that it's not modified in place,
# e.g., we don't use pop or overwrite items, otherwise this function
# would have unintended side effects.
Expand Down
15 changes: 8 additions & 7 deletions mira/metamodel/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class Unit(BaseModel):

@classmethod
def from_json(cls, data: Dict[str, Any]) -> "Unit":
# Use get_sympy from amr.petrinet, but avoid circular import
from mira.sources.amr.petrinet import get_sympy
data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS)
assert data.get('expression') is None or not isinstance(
data['expression'], str
)
return cls(**data)
# Use get_sympy from sources, but avoid circular import
from mira.sources.util import get_sympy
new_data = data.copy()
new_data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS)
assert (new_data.get('expression') is None or
not isinstance(new_data.get('expression'), str))

return cls(**new_data)

@classmethod
def model_validate(cls, obj):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_modeling/test_amr_ops.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import unittest
import requests
from copy import deepcopy as _d

from mira.modeling.amr.ops import *
from mira.metamodel.io import mathml_to_expression
from mira.metamodel import safe_parse_expr

try:
import sbmlmath
Expand Down Expand Up @@ -270,8 +272,9 @@ def test_replace_parameter_id(self):

self.assertEqual(old_param_dict[old_id]['value'], new_param_dict[new_id]['value'])
self.assertEqual(old_param_dict[old_id]['distribution'], new_param_dict[new_id]['distribution'])
self.assertEqual(str(old_param_dict[old_id]['units']['expression']),
new_param_dict[new_id]['units']['expression'])
self.assertEqual(safe_parse_expr(old_param_dict[old_id]['units']['expression']),
safe_parse_expr(new_param_dict[new_id]['units'][
'expression']))
self.assertEqual(mathml_to_expression(old_param_dict[old_id]['units']['expression_mathml']),
mathml_to_expression(new_param_dict[new_id]['units']['expression_mathml']))

Expand Down
Loading