Skip to content

Commit 8a29bb7

Browse files
authored
Merge pull request #85 from EnzymeML/add-fit-flag
Add `fit` flag for `Parameter`
2 parents 81f19cc + d64745d commit 8a29bb7

3 files changed

Lines changed: 40 additions & 21 deletions

File tree

pyenzyme/thinlayers/psyces.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,16 @@ def optimize(self, method="leastsq"):
232232

233233
self.parameters = parameters
234234

235-
return self.minimizer.minimize(method=method)
235+
result = self.minimizer.minimize(method=method)
236+
# add standard error if available in enzmldoc and if param.fit=False
237+
# (e.g. if parameter was fitted before)
238+
for param in self.enzmldoc.parameters:
239+
if not param.fit and param.stderr is not None:
240+
if param.symbol in result.params:
241+
result.params[param.symbol].stderr = param.stderr
242+
243+
return result
244+
236245

237246
def write(self) -> v2.EnzymeMLDocument:
238247
"""
@@ -255,16 +264,18 @@ def write(self) -> v2.EnzymeMLDocument:
255264
>>> pe.write_enzymeml(optimized_doc, "optimized_model.json")
256265
"""
257266
nu_enzmldoc = self.enzmldoc.model_copy(deep=True)
258-
results = self.minimizer.result.params.valuesdict() # type: ignore
267+
results = self.minimizer.result.params # type: ignore
259268

260-
for name, value in results.items():
269+
for name in results:
261270
query = nu_enzmldoc.filter_parameters(symbol=name)
262271

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

266275
parameter = query[0]
267-
parameter.value = value
276+
parameter.value = results[name].value
277+
if results[name].stderr is not None:
278+
parameter.stderr = results[name].stderr
268279

269280
return nu_enzmldoc
270281

@@ -286,8 +297,9 @@ def _initialize_parameters(self):
286297
for param in self.enzmldoc.parameters:
287298
# Build kwargs dictionary with conditional assignments
288299
kwargs = {
289-
**({"min": param.lower_bound} if param.lower_bound is not None else {}),
290-
**({"max": param.upper_bound} if param.upper_bound is not None else {}),
300+
**({'min': param.lower_bound} if param.lower_bound is not None else {}),
301+
**({'max': param.upper_bound} if param.upper_bound is not None else {}),
302+
**({'vary': param.fit}),
291303
}
292304

293305
# Determine parameter value

pyenzyme/versions/v2.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ class EnzymeMLDocument(BaseModel):
201201
serialization_alias="@context",
202202
default_factory=lambda: {
203203
"enzml": "http://www.enzymeml.org/v2/",
204-
"schema": "https://schema.org/",
205204
"OBO": "http://purl.obolibrary.org/obo/",
205+
"schema": "https://schema.org/",
206206
"name": "schema:title",
207207
"created": "schema:dateCreated",
208208
"modified": "schema:dateModified",
@@ -603,6 +603,7 @@ def add_to_parameters(
603603
initial_value: Optional[float] = None,
604604
upper_bound: Optional[float] = None,
605605
lower_bound: Optional[float] = None,
606+
fit: Optional[bool] = True,
606607
stderr: Optional[float] = None,
607608
constant: Optional[bool] = True,
608609
**kwargs,
@@ -616,6 +617,7 @@ def add_to_parameters(
616617
"initial_value": initial_value,
617618
"upper_bound": upper_bound,
618619
"lower_bound": lower_bound,
620+
"fit": fit,
619621
"stderr": stderr,
620622
"constant": constant,
621623
}
@@ -659,8 +661,8 @@ class Creator(BaseModel):
659661
serialization_alias="@context",
660662
default_factory=lambda: {
661663
"enzml": "http://www.enzymeml.org/v2/",
662-
"schema": "https://schema.org/",
663664
"OBO": "http://purl.obolibrary.org/obo/",
665+
"schema": "https://schema.org/",
664666
"given_name": "schema:givenName",
665667
"family_name": "schema:familyName",
666668
"mail": "schema:email",
@@ -773,8 +775,8 @@ class Vessel(BaseModel):
773775
serialization_alias="@context",
774776
default_factory=lambda: {
775777
"enzml": "http://www.enzymeml.org/v2/",
776-
"schema": "https://schema.org/",
777778
"OBO": "http://purl.obolibrary.org/obo/",
779+
"schema": "https://schema.org/",
778780
"id": {
779781
"@id": "schema:identifier",
780782
"@type": "@id",
@@ -911,8 +913,8 @@ class Protein(BaseModel):
911913
serialization_alias="@context",
912914
default_factory=lambda: {
913915
"enzml": "http://www.enzymeml.org/v2/",
914-
"schema": "https://schema.org/",
915916
"OBO": "http://purl.obolibrary.org/obo/",
917+
"schema": "https://schema.org/",
916918
"id": {
917919
"@id": "schema:identifier",
918920
"@type": "@id",
@@ -1044,8 +1046,8 @@ class Complex(BaseModel):
10441046
serialization_alias="@context",
10451047
default_factory=lambda: {
10461048
"enzml": "http://www.enzymeml.org/v2/",
1047-
"schema": "https://schema.org/",
10481049
"OBO": "http://purl.obolibrary.org/obo/",
1050+
"schema": "https://schema.org/",
10491051
"id": {
10501052
"@id": "schema:identifier",
10511053
"@type": "@id",
@@ -1195,8 +1197,8 @@ class SmallMolecule(BaseModel):
11951197
serialization_alias="@context",
11961198
default_factory=lambda: {
11971199
"enzml": "http://www.enzymeml.org/v2/",
1198-
"schema": "https://schema.org/",
11991200
"OBO": "http://purl.obolibrary.org/obo/",
1201+
"schema": "https://schema.org/",
12001202
"id": {
12011203
"@id": "schema:identifier",
12021204
"@type": "@id",
@@ -1330,8 +1332,8 @@ class Reaction(BaseModel):
13301332
serialization_alias="@context",
13311333
default_factory=lambda: {
13321334
"enzml": "http://www.enzymeml.org/v2/",
1333-
"schema": "https://schema.org/",
13341335
"OBO": "http://purl.obolibrary.org/obo/",
1336+
"schema": "https://schema.org/",
13351337
"id": {
13361338
"@id": "schema:identifier",
13371339
"@type": "@id",
@@ -1517,8 +1519,8 @@ class ReactionElement(BaseModel):
15171519
serialization_alias="@context",
15181520
default_factory=lambda: {
15191521
"enzml": "http://www.enzymeml.org/v2/",
1520-
"schema": "https://schema.org/",
15211522
"OBO": "http://purl.obolibrary.org/obo/",
1523+
"schema": "https://schema.org/",
15221524
"species_id": {
15231525
"@id": "schema:identifier",
15241526
"@type": "@id",
@@ -1622,8 +1624,8 @@ class ModifierElement(BaseModel):
16221624
serialization_alias="@context",
16231625
default_factory=lambda: {
16241626
"enzml": "http://www.enzymeml.org/v2/",
1625-
"schema": "https://schema.org/",
16261627
"OBO": "http://purl.obolibrary.org/obo/",
1628+
"schema": "https://schema.org/",
16271629
"species_id": {
16281630
"@id": "schema:identifier",
16291631
"@type": "@id",
@@ -1738,8 +1740,8 @@ class Equation(BaseModel):
17381740
serialization_alias="@context",
17391741
default_factory=lambda: {
17401742
"enzml": "http://www.enzymeml.org/v2/",
1741-
"schema": "https://schema.org/",
17421743
"OBO": "http://purl.obolibrary.org/obo/",
1744+
"schema": "https://schema.org/",
17431745
"species_id": {
17441746
"@id": "schema:identifier",
17451747
"@type": "@id",
@@ -1874,8 +1876,8 @@ class Variable(BaseModel):
18741876
serialization_alias="@context",
18751877
default_factory=lambda: {
18761878
"enzml": "http://www.enzymeml.org/v2/",
1877-
"schema": "https://schema.org/",
18781879
"OBO": "http://purl.obolibrary.org/obo/",
1880+
"schema": "https://schema.org/",
18791881
"id": {
18801882
"@id": "schema:identifier",
18811883
"@type": "@id",
@@ -1989,6 +1991,11 @@ class Parameter(BaseModel):
19891991
description="""Lower bound for the parameter value that was used
19901992
for the parameter estimation""",
19911993
)
1994+
fit: Optional[bool] = Field(
1995+
default=True,
1996+
description="""Whether this parameter should be varied or not in
1997+
the context of an optimization.""",
1998+
)
19921999
stderr: Optional[float] = Field(
19932000
default=None,
19942001
description="""Standard error of the estimated parameter.""",
@@ -2014,8 +2021,8 @@ class Parameter(BaseModel):
20142021
serialization_alias="@context",
20152022
default_factory=lambda: {
20162023
"enzml": "http://www.enzymeml.org/v2/",
2017-
"schema": "https://schema.org/",
20182024
"OBO": "http://purl.obolibrary.org/obo/",
2025+
"schema": "https://schema.org/",
20192026
"id": {
20202027
"@id": "schema:identifier",
20212028
"@type": "@id",
@@ -2141,8 +2148,8 @@ class Measurement(BaseModel):
21412148
serialization_alias="@context",
21422149
default_factory=lambda: {
21432150
"enzml": "http://www.enzymeml.org/v2/",
2144-
"schema": "https://schema.org/",
21452151
"OBO": "http://purl.obolibrary.org/obo/",
2152+
"schema": "https://schema.org/",
21462153
"id": {
21472154
"@id": "schema:identifier",
21482155
"@type": "@id",
@@ -2330,8 +2337,8 @@ class MeasurementData(BaseModel):
23302337
serialization_alias="@context",
23312338
default_factory=lambda: {
23322339
"enzml": "http://www.enzymeml.org/v2/",
2333-
"schema": "https://schema.org/",
23342340
"OBO": "http://purl.obolibrary.org/obo/",
2341+
"schema": "https://schema.org/",
23352342
"species_id": {
23362343
"@type": "@id",
23372344
},

0 commit comments

Comments
 (0)