Skip to content

Commit 7095705

Browse files
committed
Refactor species initialization in InitMap
Improves species value assignment in InitMap by checking for both '<species>_init' and '<species>' attributes in the model, raising an error if neither is found. Also updates import order and minor formatting for consistency.
1 parent 114e1f0 commit 7095705

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

pyenzyme/thinlayers/psyces.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
from __future__ import annotations
88

9+
import contextlib
10+
import io
11+
import os
912
from dataclasses import dataclass
1013
from pathlib import Path
11-
from joblib import Parallel, delayed
14+
from typing import Dict, List, Optional, Tuple
15+
1216
import dill
1317
import numpy as np
1418
import pandas as pd
15-
import os
16-
import contextlib
17-
import io
18-
19-
from typing import Dict, List, Optional, Tuple
19+
from joblib import Parallel, delayed
2020

21-
from pyenzyme.thinlayers.base import BaseThinLayer, SimResult, Time, InitCondDict
21+
from pyenzyme.thinlayers.base import BaseThinLayer, InitCondDict, SimResult, Time
2222
from pyenzyme.versions import v2
2323

2424
try:
@@ -242,7 +242,6 @@ def optimize(self, method="leastsq"):
242242

243243
return result
244244

245-
246245
def write(self) -> v2.EnzymeMLDocument:
247246
"""
248247
Creates a new EnzymeML document with optimized parameter values.
@@ -297,9 +296,9 @@ def _initialize_parameters(self):
297296
for param in self.enzmldoc.parameters:
298297
# Build kwargs dictionary with conditional assignments
299298
kwargs = {
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}),
299+
**({"min": param.lower_bound} if param.lower_bound is not None else {}),
300+
**({"max": param.upper_bound} if param.upper_bound is not None else {}),
301+
**({"vary": param.fit}),
303302
}
304303

305304
# Determine parameter value
@@ -542,10 +541,13 @@ def to_pysces_model(self, model: pysces.model):
542541
"""
543542
model = dill.loads(dill.dumps(model))
544543
model.sim_time = np.array(self.time)
545-
model.__dict__.update(
546-
{
547-
f"{species_id}_init": value if value != 0 else 1.0e-9
548-
for species_id, value in self.species.items()
549-
}
550-
)
544+
545+
for species, value in self.species.items():
546+
if hasattr(model, f"{species}_init"):
547+
setattr(model, f"{species}_init", value)
548+
elif hasattr(model, species):
549+
setattr(model, species, value)
550+
else:
551+
raise ValueError(f"Species {species} not found in model")
552+
551553
return model

0 commit comments

Comments
 (0)