Skip to content

convert: a compartment size the SBML model refuses to change is left out or marked, like a frozen parameter (#1992) - #2139

Merged
wshlavacek merged 2 commits into
lanl:mainfrom
Montana:fix/1992-unwritable-size-convert
Dec 4, 2026
Merged

wshlavacek merged 2 commits into
lanl:mainfrom
Montana:fix/1992-unwritable-size-convert

Conversation

@Montana

@Montana Montana commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1992.

Important

sbml_to_net and sbml_to_bngl wrote a compartment size the SBML model refuses to change (Model.unwritable_compartment_size_params) as a free parameter, and the model loaded from the file took set_param on it. The write could move a rate law that names the size and not what the SBML model fixed from it, which the file holds as a number; in the issue's model it did. Such a size is now written as a frozen parameter is (#1792): left out where nothing in the file reads it, and marked folded_at_load= where something does, so that the model loaded from the file refuses the write as the SBML model does.

The issue's model (c1 = 2, c2 = 2*c1 by an initialAssignment, A -> B at k*A*c1), through sbml_to_bngl (sbml_to_net refuses it, as on main):

On main at 44e51182 and this branch at 58838262:

main this branch
the c1 line 1 c1 2.0 1 c1 2.0 # folded_at_load=what the SBML model fixed from the size of compartment c1, a size it cannot resolve to a live volume and refuses to change (...)
reloaded frozen_params [] ['c1']
set_param("c1", 3) accepted: A(2) = 0.049787 ParameterError
A(2), B(2) as written 0.135335, 0.432332 0.135335, 0.432332
report ok=True, no note ok=True, folded_marked=['c1'], a bngsim_only note

The SBML model refuses the write, and at c1 = 3 it gives A(2) = 0.135335 (closed form exp(-k t), whatever c1 is).

What changed

  • convert/_frozen.py, _reasons: the names the module handles are the model's frozen parameters and, new, its unwritable compartment sizes, each with what it was folded into: "what the SBML model fixed from the size of compartment <c>, a size it cannot resolve to a live volume and refuses to change (Model.unwritable_compartment_size_params of the SBML model)". That text is the folded_at_load= comment and the reloaded model's refusal. A size already in frozen_params keeps its own entry (the SBML loader keeps the two apart; a BNG2.pl unit_conversion= one stays written as before).
  • _validate._folded_left_out: the structural check compares parameter counts net of what the writer left out; a size left out is now among them, or L1 would count it missing.
  • _frozen.notes: the dropped note on a size left out names the list it was in, Model.unwritable_compartment_size_params, beside Model.frozen_params. The note on a frozen parameter is unchanged.
  • Docs: Model.frozen_params, Model.unwritable_compartment_size_params, the interchange guide, the report field's comment.

The diff (abridged)

# python/bngsim/convert/_frozen.py: the sizes join the frozen parameters
+def _size_reason(name: str) -> str:
+    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]:
     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)}
+    if hasattr(type(model), "unwritable_compartment_size_params"):
+        for n in model.unwritable_compartment_size_params:
+            if str(n) not in frozen:  # its own reason, unit_conversion= included
+                reasons[str(n)] = _size_reason(str(n))
+    return reasons

# python/bngsim/convert/_validate.py: L1's count, net of a size left out
-    return set(getattr(src, "_frozen_params", None) or ()) - set(dst.param_names)
+    return (set(getattr(src, "_frozen_params", None) or ()) | handled(src)) - set(dst.param_names)

# python/bngsim/convert/_frozen.py, notes(): the dropped note names the size's list
+        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"
+        )

Testing

python/tests/test_convert_unwritable_compartment_size.py, 14 tests, four models with closed forms. Against main's library code, 11 fail and 3 pass, the 3 by design: the source model's own refusal (2, controls), and the issue's write on the file with its mark taken off, which still gives A(2) = e^-3 where the SBML gives e^-2.

  • The issue's model (.bngl): c1 marked, frozen_params == ['c1'], the write refused with the size's reason, a write of the value it holds taken, A(2) and B(2) at their closed forms.
  • The rate's volume ratio reads the size (A -> 0 at k*A; the file has k/(c1/_V0_c1)), .net and .bngl: marked and refused; A(2) = exp(-k t / c1). Here a write would have been whole (the file follows c1 in the ratio, and nothing reads c2's folded size): it is refused because the SBML model refuses it, not because it would be wrong.
  • Nothing reads it (A -> 0 at k*A*c1, folded into numbers), .net and .bngl: left out, the dropped note names it and its list, the write fails as a name the model has not, A(2) = exp(-k t).
  • Two compartments one mass-action scalar divides by (A + B -> C across c1 and c2, both size 1; the SBML refuses both), .net and .bngl: both left out, A(2) = B(2) = 1/(1 + k t), C(2) = 1 - A(2).
  • L1 and the full gate pass with a size left out, the parameter counts equal.
  • The module on a stand-in with no size list; a unit_conversion= parameter that is also a size, written as before; the note on a frozen parameter unchanged.

How this was checked

  1. Corpus A/B, main's Python against this branch's, the same core. The written files (bngsim-preflights' writer_ab.py, main at 44e51182 against 58838262), on the 47 models below and the 331 BioModels (361 files): 336 identical; the 25 that differ are each a model with an unwritable size (24 .net, 24 .bngl). Conversion, reload and run (main at d44b6a33, whose convert/ differs from 44e51182's only in _bng2.py, BNG2.pl's lookup): the 1,823 SBML test-suite cases (highest level each) and 331 BioModels; 239 fail to load on both sides (unsupported constructs). 47 have an unwritable size; the change reads nothing else, so the other 1,868 are the same by construction. On the 47, sbml_to_net and sbml_to_bngl: 94 conversions, 14 refused with the same refusal on both sides, 80 written:
    • Every reloaded model runs identically to main's (to t = 1), and ok, lossy and the structural verdict are unchanged in all 80.
    • Each written file differs from main's only in its parameters block: lines left out, a comment added, indices renumbered; no value changed (48 files).
    • The write to a size, main → this branch: accepted → refused (marked) for 66 sizes, accepted → absent (left out) for 38; refused → refused for 32 (the size is no parameter in the file, e.g. one an assignment rule sets).
    • Main's accepted write was wrong on every marked size checked: against Model.from_sbml(..., compartment_sizes={size: 1.5 v + 0.5}), the documented way to move one, the converted model after the same set_param was off by 3.7e-3 to 528 times the value (relative, over the species at t = 1), on 18 models; the 19th's reference fails to integrate. That is the corpus, not a rule: the second test model above is a shape where the write would have been whole. The .net and the SBML agree exactly at the loaded size on all 28 checked, so the species line up.
  2. Mutants, each a bug planted on purpose, run against the new file and sbml_to_net and sbml_to_bngl write a folded parameter (Model.frozen_params) as a free, unreferenced one: set_param('f', 1.5) on the converted BIOMD0000000040 is accepted and changes nothing, Br(5) = 0.026263 for 0.0394 (from_sbml refuses the same write) #1792's converter tests: 6 of 6 killed (sizes not read; the validator blind to them; the note never naming the size list; always naming it; the reason text lost; the unit_conversion= guard dropped).
  3. macOS arm64, full suite on 58838262: 27,177 passed, 200 skipped, 153 xfailed, 0 failed.
  4. Linux x86-64 (bngsim-docker's image), the six touched test files: 446 passed, 116 skipped, 0 failed in the Debian image, where the .bngl tests skip (no BNG2.pl); in the BioNetGen image, which has it, 471 passed, 91 skipped, 0 failed, all 14 new tests among them on 58838262.
  5. Timing: only a model with such a size pays anything: the writer renders the model block again to decide what to leave out or mark, as it already does for a model with a frozen parameter (sbml_to_net and sbml_to_bngl write a folded parameter (Model.frozen_params) as a free, unreferenced one: set_param('f', 1.5) on the converted BIOMD0000000040 is accepted and changes nothing, Br(5) = 0.026263 for 0.0394 (from_sbml refuses the same write) #1792). Paired and interleaved, 7 runs each, median, on the largest corpus models with sizes: MODEL1508170000 .net 79 → 96 ms; MODEL2109110001 .net 75 → 100 ms, .bngl 78 → 101 ms; MODEL2012040002 .net 20 → 26 ms, .bngl 17 → 27 ms. +6 to +25 ms a conversion.
  6. pre-commit --all-files passes; the docs build with sphinx -W.
  7. The pre-PR gate (bngsim-preflights' prepr_check.py), on 58838262 and this description: 37 passed, 0 failed, 6 warnings, each read: the CI legs (above); the conversion A/B's main at d44b6a33 (said above, before the rebase); the untouched readers of the changed functions, the changelog's sites, the claim and scope words, each covered by the reviews and the recounts above.

CI legs. python-tests runs the whole of python/tests on ubuntu, macOS and LAPACK. Python only, no src/ change, so MIR and Windows KLU do not fire. wheels.yml and windows-tail.yml fire on python/bngsim/_model.py, where the change is two docstrings; neither runs the new file, and the converters' code is the same on every platform (Linux above).

Review

Three rounds, each by a separate reader of the branch.

  • A whole-branch read on the staged change, before its commit: no correctness bug. It checked every other reader of frozen_params (the protocol runner's held numbers, describe, the OMEX actions note, coupling, digest, record) and the assignment-rule and shared-scalar kinds of size. It found the refusal's "(its Model.unwritable_compartment_size_params)" pointing at a list that is empty on the model refusing, now "of the SBML model"; the docstring and changelog reading as if a size left out were listed in frozen_params, now said apart; and asked for the cost, measured above.
  • A second read of the commit: no correctness bug. It found that the docs, the changelog and the comment said a write to such a size moves the wrong thing, where in the second test model the write would have been whole (A(2) = e^(-2/3) after c1 = 3, as from_sbml(..., compartment_sizes={"c1": 3}) gives). The claim is now that a write can move a rate law and not what the size was folded into, and that the file refuses it as the SBML model does.
  • One round of a whole-branch /code-review; 58838262 is that code plus its fixes. No blocking bug; seven nits. Fixed: the comment's "computed at load", untrue of a size an assignment rule sets (now "fixed from the size"); a unit_conversion= parameter that is also a size, now written as before as the module says; the except AttributeError around the size list, which would have read an error inside the property of a real model as "no sizes" (now a check on the type); six docstrings naming frozen_params alone; and a test of the second kind of size, the shared scalar. Two are under "Not changed".

Not changed:

  • _protocol._held_as_numbers, which omex_to_net and run_protocol read for a parameter declared by an expression, reads frozen_params only. A size the SBML model refuses that is also declared by an expression would be followed there; none of the 47 corpus models with such a size has an expression on it (0 of 47 checked).
  • _actions_on_folded's note on an action that sets such a size says it "was folded into a number when the model was loaded", where the source refuses it for its own reason; the note does name the parameter, and the report lists it in folded_left_out or folded_marked.

Pre-existing, on main too:

  • A compartment size the SBML model can change is written to a .bngl as a free parameter where the file holds the volume as a number in begin compartments: in the issue's model, c2 (= 2*c1, writable) takes set_param("c2", 3) in the SBML (B(2) 0.432332 → 0.576443) and in the reloaded .bngl, where it moves nothing. That is not this list, and is left for its own issue.
  • A writable size is carried in part to both formats: one compartment, an amount-declared species, A -> 0 at k*A; after set_param("c1", 3) the SBML gives A(2) = 0.171139 and the .net and .bngl 0.256709 (the rate ratio follows, the initial concentration does not). Also left for its own issue.

…out or marked, like a frozen parameter (lanl#1992)

sbml_to_net and sbml_to_bngl read Model.frozen_params only, and the sizes in
Model.unwritable_compartment_size_params are kept out of it on purpose, so
the written file held such a size as a free parameter: the model loaded from
it took set_param, which moved a rate law that names the size and not the
volume the species are divided by. _frozen now hands these sizes to the same
rule: left out where nothing in the file reads one, marked folded_at_load=
where something does. The structural check's left-out set and the dropped
note follow.
@wshlavacek
wshlavacek merged commit cba30d5 into lanl:main Dec 4, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants