Skip to content

net loader: a value that is a declared parameter's name is that parameter, also where the name reads as a number (#1856) - #2143

Merged
wshlavacek merged 3 commits into
lanl:mainfrom
Montana:fix/1856-net-inf-nan-names
Dec 4, 2026
Merged

wshlavacek merged 3 commits into
lanl:mainfrom
Montana:fix/1856-net-inf-nan-names

Conversation

@Montana

@Montana Montana commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1856

BNG2.pl accepts a parameter named inf, nan or Infinity, and writes the name into the .net for a parameter or a seed species set to it alone (k2 inf, 2 B() inf). The loader tried the text as a number before it tried it as a name, and strtod reads those words, in any case, as infinity or NaN. So k2 and B(0) were infinite where BNG2.pl holds the parameter's 5, and k4 1/k2 was 0 with nothing said.

A value that is a declared parameter's name is now read as that parameter.

The issue's model (inf 5, k2 inf, k3 2*inf, k4 1/k2, B() inf, A() -> B() k4 from A = 100), through Model.from_net on BNG2.pl 2.9.3's .net:

name BNG2.pl: k2, B(0), B(1) main: k2, B(0), B(1) here
inf 5, 5, 23.1269 inf, inf, inf 5, 5, 23.1269
nan 5, 5, 23.1269 nan, nan, the run raises 5, 5, 23.1269
Infinity 5, 5, 23.1269 inf, inf, inf 5, 5, 23.1269
true, q 5, 5, 23.1269 5, 5, 23.1269 5, 5, 23.1269

The word beside no parameter of that name is still the number it was (k2 inf with no parameter inf is infinity), and so is one in another case than the parameter's (INF beside inf), since BNGL names are case-sensitive.

Important

#2139 and #2140 touch convert/__init__.py and _bngl_loader.py in other hunks: this branch merges clean with each, in either order.

The diff (abridged)

src/net_file_loader.cpp

+    std::unordered_set<std::string> declared;
+    for (const auto &p : params)
+        declared.insert(p.name);
+    for (auto &p : params) {
+        if (p.is_expression)
+            continue;
+        const std::string &text = p.expression;
+        const bool is_signed = text.size() > 1 && (text[0] == '-' || text[0] == '+');
+        const std::string named = is_signed ? text.substr(1) : text;
+        if (named != p.name && declared.count(named) != 0) {
+            p.is_expression = true;
+            p.value = 0.0;
+        }
+    }

Parameters: after the whole block is read, since the name may be declared further down. A value that is another parameter's name, bare or under one sign, is an expression, as k2 q always was.

+        const bool conc_signed = conc_str.size() > 1 && (conc_str[0] == '-' || conc_str[0] == '+');
+        if (param_name_to_idx.count(conc_signed ? conc_str.substr(1) : conc_str) != 0) {
+            s.is_param_ref = true;
+            s.param_ref_name = conc_str;
+        } else {
             try {
                 size_t pos;
                 s.concentration = c_stod(conc_str, &pos);

Seed species: a declared name is looked up before the text is tried as a number. Under a sign it goes the way an expression-valued concentration already does.

python/bngsim/convert/_protocol.py

-        return StateChange(kind=kind, target=target, value=float(raw))
+        number = float(raw)
     except (TypeError, ValueError):
         return StateChange(kind=kind, target=target, value_expr=str(raw))
+    if not math.isfinite(number):
+        return StateChange(kind=kind, target=target, value_expr=str(raw))
+    return StateChange(kind=kind, target=target, value=number)

An action's quoted value: setParameter("k2","inf") keeps the text, which the model evaluates.

python/bngsim/_bngl_loader.py

+    try:
+        number = float(text)
+    except (TypeError, ValueError):
+        return None
+    return None if math.isfinite(number) else number

Where the model has no parameter of that name, the text is the number it was read as before.

python/bngsim/convert/__init__.py

+    def number(pname: str, declared_text: str) -> float:
+        named = declared_text.strip().lstrip("+-").strip()
+        if named != pname and named in declared_names:
+            raise ValueError(declared_text)
+        return float(declared_text)

The check that a .net is the network its .bngl generates, at its three float(declared_text) reads.

Review

One round of a whole-branch /code-review on bbfe233e, at high effort, to the bar in "How much to review". The change as written came back clean; the round found the same reading in three neighbouring paths, each reproduced against BNG2.pl. All three are fixed in 1f310f98, and I read that commit's diff once more and stopped.

Finding Kind Outcome
Under a sign the word is still a number: BNG2.pl writes k2 -inf, k3 +inf, k5 -nan; the loader gave -inf, inf, nan for BNG2.pl's -5, 5, -3, and k4 -1/k2 was 0. wrong result Fixed; test_a_signed_name_is_the_parameter_too
An action's value: setParameter("k2","inf") and setConcentration("B()","inf") beside a parameter inf set infinity under run_bngl (Atot, Btot = 100, inf for BNG2.pl's 81.873, 23.127). wrong result Fixed; test_an_action_that_sets_such_a_word_keeps_it_as_text, test_run_bngl_sets_the_parameters_value
net_to_omex(net, out, bngl=...) refused BNG2.pl's own network for k2 nan ("parameter k2 is 5 there, nan as declared"). breakage Fixed; test_net_to_omex_takes_the_network_its_bngl_generates

The round also ran a model with the parameter named inf, nan, Infinity, NaN or INF against the same model named q: values, right-hand side, Jacobian, ODE with and without codegen, forward sensitivities (also against finite differences), set_param with clone, SSA, and NFsim and RuleMonkey on the BNG-XML all agree.

Left alone

  • A pair of parameters set to each other, inf nan and nan inf, is now refused as a reference cycle, where it loaded as infinity and NaN. BNG2.pl refuses the pair too ("Parameter nan has a dependency cycle nan->inf->nan").
  • setParameter("k2","inf") with no parameter named inf sets infinity, as it did.

Testing

python/tests/test_net_number_word_names.py, new, on 1f310f98: 53 cases. Without BNG2.pl 38 run and 15 skip. On main's build with BNG2.pl: 28 failed, 25 passed. Here: 53 passed. The 25 that pass on main are the controls: names that are not such words (true, q), the word with no parameter of that name, numbers, and an action that sets a number.

How this was checked

  1. Full suite on 1f310f98 (main at 44e51182), macOS arm64, Python 3.13, uv sync --extra test plus h5py, BNGPATH at BioNetGen 2.9.3: 26707 passed, 547 skipped, 151 xfailed, 1 failed. The failure is test_run_tests_sh.py::test_the_script_itself_runs_walk_up_dependent_tests_green, which fails the same way on main in this environment (it counts skips for jax, diffrax, roadrunner and vivarium, which the test extra does not install).
  2. Mutants (mutate.py from bngsim-preflights) on 1f310f98: 8 of 8 killed: 5 that rebuild the extension (a parameter's value not checked against the names, its own name read as a reference, a species' name tried as a number first, either sign not stripped) and 3 in Python (an action's word read as a float, no fallback to the number, the .bngl check reading a name as a float). The test file is green on the rebuilt original.
  3. Cost: Model.from_net on benchmarks/models/net/ode/fceri_gamma.net (62,061 lines), medians of 7 loads, five runs a side, sides alternated: main 121 to 138 ms, here 122 to 134 ms. The change is one hash lookup per parameter and per seed species.
  4. pre-commit run --all-files, ci/changelog.py check: pass.
  5. Linux with BNG2.pl (bngsim-docker, bngsim_docker.py test --merge-main --kind bionetgen, amd64 under emulation, which builds the C++ there): 1f310f98 merged with main 44e51182, test_net_number_word_names.py, test_bngl_protocol.py and test_protocol_run.py: 159 passed. All 53 of this PR's cases pass there.
  6. Every .bngl the repository tracks, its actions parsed by main's package and by this one (writer_ab.py): 978 of 978 protocols identical, as written back to BNGL, as network actions and as SED-ML (851 of 851). No tracked file sets such a word in an action.
  7. Pre-PR gate (prepr_check.py from bngsim-preflights) on 1f310f98 with this description: 0 failed, 8 warnings, 37 passed. The warnings: convert: a compartment size the SBML model refuses to change is left out or marked, like a frozen parameter (#1992) #2139 and bngl: a folded_at_load= comment is read off the declaration BNG2.pl reads (#1994) #2140 on the same files (above); the CI legs that fire; lists of callers and of sites that name the changed functions, to read (read: _value returns something new only for a text that is one of these words and does not evaluate in the model, where it returned None); code changed after the reviewed commit (described under Review); the Linux run covers this PR's file and the two protocol files, not every file with names or number in its name (CI's legs run them all); claims to recount.
  8. Not run: Windows (CI's leg builds and runs it), and benchmarks/perf_ab.py (it times runs; the loader's cost is measured above).

Montana and others added 3 commits December 3, 2026 10:18
…eter, also where the name reads as a number (lanl#1856)

BNG2.pl takes a parameter named inf, nan or Infinity and writes the name
for a parameter or a seed species set to it alone. The loader tried the
text as a number first, and strtod reads those words as infinity or NaN:
k2 and B(0) were infinite where BNG2.pl holds 5, and k4 = 1/k2 a silent
0. Both sites read a declared parameter's name as the parameter.

Signed-off-by: Montana <montana@linux.com>
…ue, and in the check of a .net against its .bngl

k2 -inf stayed -infinity; setParameter("k2","inf") set infinity under
run_bngl; net_to_omex(bngl=) refused BNG2.pl's own network for k2 nan.

Signed-off-by: Montana <montana@linux.com>
@wshlavacek
wshlavacek merged commit 6ea26f0 into lanl:main Dec 4, 2026
20 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

Development

Successfully merging this pull request may close these issues.

The .net loader reads a parameter named inf, nan or Infinity, used as a whole value, as that number

2 participants