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
1 change: 1 addition & 0 deletions changelog.d/1992.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **`sbml_to_net` and `sbml_to_bngl` wrote a compartment size the SBML model refuses to change as a free parameter.** A size in `Model.unwritable_compartment_size_params` (one an initialAssignment folds into another compartment's size, for one) is not in `Model.frozen_params`, so the converters' handling of frozen parameters passed it over: the model loaded from the written file took `set_param` on it, which could move a rate law that names the size and not what the size was folded into, held there as a number. In the issue's model `set_param("c1", 3)` gave A(2) = 0.0498 where the SBML model at that size gives 0.1353. Such a size is now written as a frozen parameter is: left out where nothing in the file reads it, so that a write to it is refused as a name the model has not, and marked `folded_at_load=` where something does, so that the model loaded from the file lists it in `frozen_params` and refuses the write. The conversion report names each in `folded_left_out` or `folded_marked`.
7 changes: 6 additions & 1 deletion docs/user-guide/interchange.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ What to watch out for:
read in a value (`setConcentration("A()", "f*0.5")`) stays as well, marked,
so that the actions run. These parameters are listed under `bngsim_only`, and
the report names both kinds in `folded_left_out` and `folded_marked`. To
change one, edit the SBML and convert again. The other direction does not
change one, edit the SBML and convert again. A compartment size the SBML
model refuses to change (`Model.unwritable_compartment_size_params`) is
written the same way: the file holds what the size was folded into as a
number, so a write can move a rate law that names the size and not that
number, and the converted model refuses it as the SBML model does. The
other direction does not
carry the comment: `net_to_sbml` and `net_to_omex` write a marked parameter
as an ordinary one.

Expand Down
12 changes: 11 additions & 1 deletion python/bngsim/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,12 @@ def frozen_params(self) -> list[str]:
which the written network or its actions still read, has ``folded_at_load=`` and
what it was folded into in a comment on its line, and is listed again
in the model loaded from the file. (One that nothing in the network
reads is left out of the file.) The comment may be written by hand
reads is left out of the file.) A compartment size of the SBML model
in :attr:`unwritable_compartment_size_params` is written the same way
(issue #1992), since the file holds what the size was folded into as
a number: a marked one is listed here on the model loaded from
the file, and a write to one left out is refused as a name the model
has not. The comment may be written by hand
on a parameter line of either format, to the same effect; one on a
line that declares no parameter of the model says nothing. In a
``.bngl`` it is read off the declaration as BNG2.pl reads it: a line
Expand Down Expand Up @@ -2247,6 +2252,11 @@ def unwritable_compartment_size_params(self) -> list[str]:
``Model.from_sbml(..., compartment_sizes={...})``; that refuses a size an
assignment rule sets, which moves with what the rule reads.

A ``.net`` or ``.bngl`` that ``sbml_to_net`` or ``sbml_to_bngl`` writes
leaves one out where nothing in it reads the size, and marks it
otherwise, so the model loaded from the file has it in
:attr:`frozen_params` (issue #1992).

Returns
-------
list[str]
Expand Down
11 changes: 7 additions & 4 deletions python/bngsim/convert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ class ConversionReport:
# the protocol from its annotation. ``ok`` and ``lossy`` judge the network.
sedml_lossy: list[str] = field(default_factory=list)
# The parameters folded into a number when the source model loaded
# (Model.frozen_params), by what the written file does with each: left
# out, since nothing in it reads one, or marked with a folded_at_load
# comment (sbml_to_net and sbml_to_bngl, issue #1792). n_parameters is the
# (Model.frozen_params) and the compartment sizes it refuses to change
# (Model.unwritable_compartment_size_params, issue #1992), by what the
# written file does with each: left out, since nothing in it reads one, or
# marked with a folded_at_load comment (sbml_to_net and sbml_to_bngl,
# issue #1792). n_parameters is the
# source model's count less the ones left out, which is the count the
# structural check compares. New fields go after every older one, so a
# report built by position keeps its meaning.
Expand Down Expand Up @@ -380,7 +382,8 @@ def sbml_to_net(
mirror of :func:`net_to_sbml`'s ``bngl=`` (avoids the blanket-grid
stiff-hang and exercises the trajectory the modeller actually ran). The
parsed :class:`~bngsim.convert.ProtocolSpec` is attached to the report.
A parameter folded at load that one of its actions reads in a value it
A parameter folded at load, or a compartment size the model refuses
to change (issue #1992), that one of its actions reads in a value it
evaluates stays in the network, marked (issue #1792).
read_by_actions : iterable of str
Further names that actions to be run on the network read, as for
Expand Down
8 changes: 5 additions & 3 deletions python/bngsim/convert/_bngl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _eg(names: list[Any]) -> str:
# Issue #1792: a parameter folded at load is left out where nothing in the
# model block reads it, and marked for bngsim's reader where something does.
left_out, marked = _folded_parameters(model, read_by_actions)
gone, kept = _frozen.notes(left_out, marked)
gone, kept = _frozen.notes(left_out, marked, model)
dropped += gone
bngsim_only += kept

Expand All @@ -471,7 +471,8 @@ def _eg(names: list[Any]) -> str:
def _folded_parameters(
model: Model, read_by_actions: Iterable[str] = ()
) -> tuple[list[str], list[str]]:
"""The frozen parameters :func:`write_bngl` leaves out, and the ones it
"""The frozen parameters and refused compartment sizes (issue #1992)
:func:`write_bngl` leaves out, and the ones it
marks, read off the model block it writes (:mod:`bngsim.convert._frozen`).
*read_by_actions* names what the actions after the block read, which
stays."""
Expand Down Expand Up @@ -618,7 +619,8 @@ def write_bngl(
``setConcentration`` state changes. Its ``StateChange.target`` is a
*codegen species name*, which this writer translates to the
compartment-qualified pattern (``@comp:Mol()``) the model declares.
A parameter folded at load is left out or marked by what the model
A parameter folded at load, or a compartment size the model refuses
to change (issue #1992), is left out or marked by what the model
block reads and by what the actions read in a value to evaluate
(``setConcentration("A()", "f*0.5")`` keeps ``f``, marked; issue
#1792). What an action sets is no read: a ``setParameter`` on a
Expand Down
60 changes: 54 additions & 6 deletions python/bngsim/convert/_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
``Model.from_bngl`` read back into ``frozen_params``. Only bngsim reads the
comment: any other reader of the file takes the parameter as free.

A compartment size the SBML model refuses to change
(:attr:`bngsim.Model.unwritable_compartment_size_params`, issue #1992) is
handled the same way. It is not in ``frozen_params``, which leaves it to the
size's own refusal, but the written file holds it as a parameter like any
other, and what the SBML model folded it into as a number. A write could move
a rate law that names the size and not that number, which is why the SBML
model refuses it; the file now refuses it too, also where the write would
have happened to move everything that reads the size.

A parameter BNG2.pl folded into the rate constants of a compartmental network
(a ``unit_conversion=`` comment, issue #711) is not handled here: it is
written as before.
Expand All @@ -36,10 +45,38 @@
_INDEX = re.compile(r"(\s*)\d+(?=\s)")


def _size_reason(name: str) -> str:
"""What a compartment size the model refuses to change was folded into
(issue #1992), for the comment on its line and the refusal of the model
loaded from the file."""
return (
f"what the SBML model fixed from the size of compartment {name}, a size it "
"cannot resolve to a live volume and refuses to change "
"(Model.unwritable_compartment_size_params of the SBML model)"
)


def _reasons(model: Any) -> dict[str, str]:
"""What each frozen parameter this module handles was folded into."""
"""What each frozen parameter this module handles was folded into: the
model's frozen parameters, and the compartment sizes it refuses to change
(:attr:`bngsim.Model.unwritable_compartment_size_params`), which the
written file holds as parameters like any other (issue #1992)."""
frozen = getattr(model, "_frozen_params", None) or {}
return {str(n): str(w) for n, w in frozen.items() if "unit_conversion=" not in str(w)}
reasons = {str(n): str(w) for n, w in frozen.items() if "unit_conversion=" not in str(w)}
# A stand-in with no such property (a test's) has no sizes; an error raised
# inside the property of a Model is its own, and is not taken for none.
if hasattr(type(model), "unwritable_compartment_size_params"):
for n in model.unwritable_compartment_size_params:
# A parameter frozen for its own reason keeps it, a unit_conversion=
# one too, which this module writes as before.
if str(n) not in frozen:
reasons[str(n)] = _size_reason(str(n))
return reasons


def handled(model: Any) -> frozenset[str]:
"""The parameters of *model* this module leaves out or marks."""
return frozenset(_reasons(model))


def has_folded(model: Any) -> bool:
Expand Down Expand Up @@ -165,16 +202,27 @@ def named(n: str, i: int) -> bool:
)


def notes(left_out: list[str], marked: list[str]) -> tuple[list[str], list[str]]:
"""``(dropped, bngsim_only)`` notes for a capability report."""
def notes(
left_out: list[str], marked: list[str], model: Any = None
) -> tuple[list[str], list[str]]:
"""``(dropped, bngsim_only)`` notes for a capability report. With *model*,
the model written, a note on a compartment size among *left_out* names the
list it is in (issue #1992)."""
dropped: list[str] = []
bngsim_only: list[str] = []
if left_out:
frozen = set(getattr(model, "_frozen_params", None) or ())
where = (
"Model.frozen_params of the model written"
if model is None or all(n in frozen for n in left_out)
else "Model.frozen_params, or Model.unwritable_compartment_size_params for a "
"compartment size, of the model written"
)
dropped.append(
f"{len(left_out)} parameter(s) folded into a number when the model was "
f"loaded, which nothing in the written network reads ({', '.join(left_out)}): "
"a write to one moved nothing, and is now refused as a name the model "
"has not (Model.frozen_params of the model written)"
f"a write to one moved nothing, and is now refused as a name the model "
f"has not ({where})"
)
if marked:
bngsim_only.append(
Expand Down
14 changes: 9 additions & 5 deletions python/bngsim/convert/_net_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _eg(names: list[str]) -> str:
# Issue #1792: a parameter folded at load is left out where nothing in the
# network reads it, and marked for bngsim's reader where something does.
left_out, marked = _folded_parameters(model, read_by_actions)
gone, kept = _frozen.notes(left_out, marked)
gone, kept = _frozen.notes(left_out, marked, model)
dropped += gone
bngsim_only += kept

Expand All @@ -281,7 +281,8 @@ def _eg(names: list[str]) -> str:
def _folded_parameters(
model: Model, read_by_actions: Iterable[str] = ()
) -> tuple[list[str], list[str]]:
"""The frozen parameters :func:`write_net` leaves out, and the ones it
"""The frozen parameters and refused compartment sizes (issue #1992)
:func:`write_net` leaves out, and the ones it
marks, read off the text it writes (:mod:`bngsim.convert._frozen`).
*read_by_actions* names what actions run on the network read, which
stays."""
Expand Down Expand Up @@ -737,9 +738,12 @@ def write_net(
Names that actions to be run on the network read in a value to
evaluate (:func:`bngsim.convert._frozen.names_read` of the protocol);
a ``str`` is taken as one name.
A parameter folded at load (``Model.frozen_params``) that nothing in
the network reads is left out of it, unless it is among these: then it
stays, marked, so that the actions find it (issue #1792).
A parameter folded at load (``Model.frozen_params``), or a compartment
size the model refuses to change
(``Model.unwritable_compartment_size_params``, issue #1992), that
nothing in the network reads is left out of it, unless it is among
these: then it stays, marked, so that the actions find it (issue
#1792).

Returns
-------
Expand Down
8 changes: 6 additions & 2 deletions python/bngsim/convert/_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,12 @@ def _folded_left_out(src: Model, dst: Model) -> set[str]:
no parameter the two are to agree on. A parameter lost any other way does
not hide here: one that the network still reads and that it no longer
declares stops *dst* from loading, and one whose readers went with it
moves the right-hand side, which the next level compares."""
return set(getattr(src, "_frozen_params", None) or ()) - set(dst.param_names)
moves the right-hand side, which the next level compares. The compartment
sizes *src* refuses to change are left out on the same terms (issue
#1992)."""
from bngsim.convert._frozen import handled

return (set(getattr(src, "_frozen_params", None) or ()) | handled(src)) - set(dst.param_names)


def _structural_metrics(src: Model, dst: Model) -> dict:
Expand Down
Loading
Loading