Skip to content

Commit 83b2f80

Browse files
committed
..
1 parent 86e67b6 commit 83b2f80

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

petab/v2/converters.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import libsbml
99
from sbmlmath import sbml_math_to_sympy, set_math
1010

11-
from .C import TIME_PREEQUILIBRATION
1211
from .core import Change, Condition, Experiment, ExperimentPeriod
1312
from .models._sbml_utils import add_sbml_parameter, check
1413
from .models.sbml_model import SbmlModel
@@ -165,16 +164,16 @@ def _convert_experiment(self, experiment: Experiment):
165164
"""Convert a single experiment to SBML events."""
166165
model = self._model
167166
experiment.sort_periods()
168-
has_preequilibration = (
169-
len(experiment.periods)
170-
and experiment.periods[0].time == TIME_PREEQUILIBRATION
171-
)
167+
has_preequilibration = experiment.has_preequilibration
172168

173169
# add experiment indicator
174170
exp_ind_id = self.get_experiment_indicator(experiment.id)
175171
if model.getElementBySId(exp_ind_id) is not None:
176-
raise AssertionError(
177-
f"Entity with ID {exp_ind_id} exists already."
172+
raise ValueError(
173+
f"The model has entity with ID `{exp_ind_id}`. "
174+
"IDs starting with `petab_` are reserved for "
175+
f"{self.__class__.__name__} and should not be used in the "
176+
"model."
178177
)
179178
add_sbml_parameter(model, id_=exp_ind_id, constant=False, value=0)
180179
kept_periods = []
@@ -192,7 +191,7 @@ def _convert_experiment(self, experiment: Experiment):
192191
"This cannot be represented in SBML."
193192
)
194193

195-
if period.time == TIME_PREEQUILIBRATION:
194+
if period.is_preequilibration:
196195
# pre-equilibration cannot be represented in SBML,
197196
# so we need to keep this period in the Problem.
198197
kept_periods.append(period)
@@ -227,7 +226,7 @@ def _convert_experiment(self, experiment: Experiment):
227226
period.condition_ids = [
228227
self._get_experiment_indicator_condition_id(experiment.id),
229228
self.CONDITION_ID_PREEQ_ON
230-
if period.time == TIME_PREEQUILIBRATION
229+
if period.is_preequilibration
231230
else self.CONDITION_ID_PREEQ_OFF,
232231
]
233232

@@ -253,7 +252,7 @@ def _create_period_start_event(
253252

254253
exp_ind_id = self.get_experiment_indicator(experiment.id)
255254

256-
if period.time == TIME_PREEQUILIBRATION:
255+
if period.is_preequilibration:
257256
trig_math = libsbml.parseL3Formula(
258257
f"({exp_ind_id} == 1) && ({self._preeq_indicator} == 1)"
259258
)
@@ -324,7 +323,8 @@ def _change_to_event_assignment(change: Change, event: libsbml.Event):
324323
sbml_model, id_=change.target_id, constant=False, value=0
325324
)
326325
else:
327-
# TODO: can that break models??
326+
# We can safely change the `constant` attribute of the target.
327+
# "Constant" does not imply "boundary condition" in SBML.
328328
target.setConstant(False)
329329

330330
# the target value may depend on parameters that are only

petab/v2/core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ def _validate_ids(cls, condition_ids):
521521
raise ValueError(f"Invalid {C.CONDITION_ID}: `{condition_id}'")
522522
return condition_ids
523523

524+
@property
525+
def is_preequilibration(self) -> bool:
526+
"""Check if this period is a preequilibration period."""
527+
return self.time == C.TIME_PREEQUILIBRATION
528+
524529

525530
class Experiment(BaseModel):
526531
"""An experiment or a timecourse defined by an ID and a set of different
@@ -555,9 +560,10 @@ def __iadd__(self, other: ExperimentPeriod) -> Experiment:
555560
self.periods.append(other)
556561
return self
557562

563+
@property
558564
def has_preequilibration(self) -> bool:
559565
"""Check if the experiment has preequilibration enabled."""
560-
return any(period.time == -np.inf for period in self.periods)
566+
return any(period.is_preequilibration for period in self.periods)
561567

562568
def sort_periods(self) -> None:
563569
"""Sort the periods of the experiment by time."""

petab/v2/models/_sbml_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ def check(res: int):
3333

3434
def add_sbml_parameter(
3535
model: libsbml.Model,
36-
id_: str = None,
36+
id_: str,
3737
value: float = None,
3838
constant: bool = None,
3939
) -> libsbml.Parameter:
4040
"""Add a parameter to the SBML model."""
4141
param = model.createParameter()
4242

43-
if id_ is not None:
44-
param.setId(id_)
43+
param.setId(id_)
4544

4645
if value is not None:
4746
param.setValue(value)

0 commit comments

Comments
 (0)