Skip to content

Commit

Permalink
Models can now be set directly to model attribute
Browse files Browse the repository at this point in the history
Does the same as 'setModel'
  • Loading branch information
JR-1991 committed Mar 24, 2022
1 parent 35983d8 commit b45c4aa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
44 changes: 41 additions & 3 deletions pyenzyme/enzymeml/core/enzymemlbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import json
import logging

from pydantic import BaseModel, PrivateAttr
from pyenzyme.utils.log import log_change
from typing import Optional
from pydantic import BaseModel
from pyenzyme.utils.log import log_change, log_object


logger = logging.getLogger("pyenzyme")

Expand Down Expand Up @@ -40,6 +40,15 @@ def fromJSON(cls, json_string):
def __setattr__(self, name, value):
"""Modified attribute setter to document changes in the EnzymeML document"""

# TODO Refactor this to spearate methods

# Check if there is a model that is to be added
if name == "model" and value is not None:
if self._enzmldoc:
value = self.set_kinetic_model(
model=value, reaction=self, enzmldoc=self._enzmldoc
)

# Check for changing units and assign a new one
if "unit" in name and not name.startswith("_") and hasattr(self, "_enzmldoc"):
if self._enzmldoc and value:
Expand Down Expand Up @@ -95,3 +104,32 @@ def __setattr__(self, name, value):

# Finally, set the new attribute's value
super().__setattr__(name, value)

@staticmethod
def set_kinetic_model(
model,
reaction,
enzmldoc,
) -> None:
"""Sets the kinetic model of the reaction and in addition converts all units to UnitDefs.
Args:
model (KineticModel): Kinetic model that has been derived.
enzmldoc (EnzymeMLDocument): The EnzymeMLDocument that holds the reaction.
"""

# ID consistency
enzmldoc._check_kinetic_model_ids(
model=model,
)

# Unit conversion
enzmldoc._convert_kinetic_model_units(model.parameters, enzmldoc=enzmldoc)

# Log creator object
log_object(logger, model)
logger.debug(
f"Added {type(model).__name__} '{model.name}' to reaction '{reaction.name}'"
)

return model
14 changes: 9 additions & 5 deletions pyenzyme/enzymeml/models/kineticmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,12 @@ def _apply_mapping(self, mapping: dict, model: KineticModel):

@staticmethod
def parse_equation(equation: str):
return [
node.id
for node in ast.walk(ast.parse(equation))
if isinstance(node, ast.Name)
]
return list(
set(
[
node.id
for node in ast.walk(ast.parse(equation))
if isinstance(node, ast.Name)
]
)
)

0 comments on commit b45c4aa

Please sign in to comment.