Skip to content
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
24 changes: 18 additions & 6 deletions pyenzyme/thinlayers/psyces.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,16 @@ def optimize(self, method="leastsq"):

self.parameters = parameters

return self.minimizer.minimize(method=method)
result = self.minimizer.minimize(method=method)
# add standard error if available in enzmldoc and if param.fit=False
# (e.g. if parameter was fitted before)
for param in self.enzmldoc.parameters:
if not param.fit and param.stderr is not None:
if param.symbol in result.params:
result.params[param.symbol].stderr = param.stderr

return result


def write(self) -> v2.EnzymeMLDocument:
"""
Expand All @@ -255,16 +264,18 @@ def write(self) -> v2.EnzymeMLDocument:
>>> pe.write_enzymeml(optimized_doc, "optimized_model.json")
"""
nu_enzmldoc = self.enzmldoc.model_copy(deep=True)
results = self.minimizer.result.params.valuesdict() # type: ignore
results = self.minimizer.result.params # type: ignore

for name, value in results.items():
for name in results:
query = nu_enzmldoc.filter_parameters(symbol=name)

if len(query) == 0:
raise ValueError(f"Parameter {name} not found")

parameter = query[0]
parameter.value = value
parameter.value = results[name].value
if results[name].stderr is not None:
parameter.stderr = results[name].stderr

return nu_enzmldoc

Expand All @@ -286,8 +297,9 @@ def _initialize_parameters(self):
for param in self.enzmldoc.parameters:
# Build kwargs dictionary with conditional assignments
kwargs = {
**({"min": param.lower_bound} if param.lower_bound is not None else {}),
**({"max": param.upper_bound} if param.upper_bound is not None else {}),
**({'min': param.lower_bound} if param.lower_bound is not None else {}),
**({'max': param.upper_bound} if param.upper_bound is not None else {}),
**({'vary': param.fit}),
}

# Determine parameter value
Expand Down
35 changes: 21 additions & 14 deletions pyenzyme/versions/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class EnzymeMLDocument(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"name": "schema:title",
"created": "schema:dateCreated",
"modified": "schema:dateModified",
Expand Down Expand Up @@ -603,6 +603,7 @@ def add_to_parameters(
initial_value: Optional[float] = None,
upper_bound: Optional[float] = None,
lower_bound: Optional[float] = None,
fit: Optional[bool] = True,
stderr: Optional[float] = None,
constant: Optional[bool] = True,
**kwargs,
Expand All @@ -616,6 +617,7 @@ def add_to_parameters(
"initial_value": initial_value,
"upper_bound": upper_bound,
"lower_bound": lower_bound,
"fit": fit,
"stderr": stderr,
"constant": constant,
}
Expand Down Expand Up @@ -659,8 +661,8 @@ class Creator(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"given_name": "schema:givenName",
"family_name": "schema:familyName",
"mail": "schema:email",
Expand Down Expand Up @@ -773,8 +775,8 @@ class Vessel(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -911,8 +913,8 @@ class Protein(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1044,8 +1046,8 @@ class Complex(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1195,8 +1197,8 @@ class SmallMolecule(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1330,8 +1332,8 @@ class Reaction(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1517,8 +1519,8 @@ class ReactionElement(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"species_id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1622,8 +1624,8 @@ class ModifierElement(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"species_id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1738,8 +1740,8 @@ class Equation(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"species_id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1874,8 +1876,8 @@ class Variable(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -1989,6 +1991,11 @@ class Parameter(BaseModel):
description="""Lower bound for the parameter value that was used
for the parameter estimation""",
)
fit: Optional[bool] = Field(
default=True,
description="""Whether this parameter should be varied or not in
the context of an optimization.""",
)
stderr: Optional[float] = Field(
default=None,
description="""Standard error of the estimated parameter.""",
Expand All @@ -2014,8 +2021,8 @@ class Parameter(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -2141,8 +2148,8 @@ class Measurement(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"id": {
"@id": "schema:identifier",
"@type": "@id",
Expand Down Expand Up @@ -2330,8 +2337,8 @@ class MeasurementData(BaseModel):
serialization_alias="@context",
default_factory=lambda: {
"enzml": "http://www.enzymeml.org/v2/",
"schema": "https://schema.org/",
"OBO": "http://purl.obolibrary.org/obo/",
"schema": "https://schema.org/",
"species_id": {
"@type": "@id",
},
Expand Down