Skip to content

bngl: a folded_at_load= comment is read off the declaration BNG2.pl reads (#1994) - #2140

Merged
wshlavacek merged 1 commit into
lanl:mainfrom
Montana:fix/1994-folded-comment-continued
Dec 4, 2026
Merged

wshlavacek merged 1 commit into
lanl:mainfrom
Montana:fix/1994-folded-comment-continued

Conversation

@Montana

@Montana Montana commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1994.

Important

Model.from_bngl read a folded_at_load= comment off each physical line of a .bngl's parameters block, and took the first blank-separated word as the parameter's name. BNG2.pl reads the block otherwise: a line whose code ends in \ is joined with the next, and the name is the identifier the declaration starts with. So a comment written by hand after f \ / k froze k, which the file says nothing about, and left f writable; and one on f(2.0), f(k+1), f-2.0 or a continued number froze nothing. A .bngl is now read as BNG2.pl reads it, so the comment freezes the parameter its declaration declares. A .net, which bngsim's loader reads one line to a declaration, and every file sbml_to_net and sbml_to_bngl write, are read as before.

The issue's script (BNG2.pl 2.9.3), on main at 44e51182 and on this branch at a7981c69:

parameter line(s) BNG2.pl declares main: frozen_params, set_param f / k this branch
f 2.0 # folded_at_load=by hand f 2.0 ['f'], refused / taken ['f'], refused / taken
f \ # folded_at_load=by hand / 2.0 f 2.0 ['f'], refused / taken ['f'], refused / taken
f \ / 2.0 # folded_at_load=by hand f 2.0 [], taken / taken ['f'], refused / taken
f \ / k # folded_at_load=by hand f k ['k'], taken / refused ['f'], refused / taken
f \ / k * 2 # folded_at_load=by hand f k*2 ['k'], taken / refused ['f'], refused / taken
f(2.0) # folded_at_load=by hand f 2.0 [], taken / taken ['f'], refused / taken
f(k+1) # folded_at_load=by hand f k+1 [], taken / taken ['f'], refused / taken
f-2.0 # folded_at_load=by hand f -2.0 [], taken / taken ['f'], refused / taken

What changed

  • _folded_at_load_in(text, *, bngl=False) (python/bngsim/_model.py): with bngl=True the text is read as BNG2.pl 2.9.3 reads it (get_line and read_block_array in BNGModel.pm, ParamList::readString):
    • Lines: a comment runs from the first #; a line whose code ends in \ and blanks has those taken off and the next line appended, nothing between (f\ / 2.0 is f2.0, the parameter f2 to BNG2.pl). A comment on any of the joined lines is the declaration's; the first mark wins.
    • The block: opens on begin and its name, with or without a blank between them (beginparameters too, which main did not read), and closes on end and a blank.
    • The name: blanks, an index (\d+\s+) and a label (\w+\s*:\s+, not starting with a digit) are taken off, and the name is the leading [A-Za-z_]\w*, whatever follows it.
    • Blanks and words are ASCII, as Perl reads them in BNG2.pl.
  • The .net reading is the same code as before, moved into _net_text_lines and _net_parameter_name: bngsim's .net loader (net_file_loader.cpp) joins no lines, so neither does the reader.
  • from_bngl (_bngl_loader.py) passes bngl=True; from_net and parse_net_file do not.
  • Docs: Model.frozen_params says how a .bngl's comment is read.

The diff (abridged)

# python/bngsim/_model.py: a .bngl's lines and names as BNG2.pl reads them
-def _folded_at_load_in(text: str) -> list[tuple[str, str]]:
+def _folded_at_load_in(text: str, *, bngl: bool = False) -> list[tuple[str, str]]:
-    for line in text.split("\n"):
-        code, _, comment = line.partition("#")
-        ...
+    for code, comments in _bngl_text_lines(text) if bngl else _net_text_lines(text):
+        if bngl:
+            opened = _BNGL_BEGIN.match(code)
+            if opened or (inside and _BNGL_END.match(code)):
+                inside = opened is not None and opened.group(1).split() == ["parameters"]
+                continue
+        ...
+        name = _bngl_parameter_name(code) if bngl else _net_parameter_name(code)

+def _bngl_text_lines(text: str):
+    lines = text.split("\n")
+    i = 0
+    while i < len(lines):
+        code, _, comment = lines[i].partition("#")
+        comments = [comment]
+        i += 1
+        while (cut := _CONTINUED.search(code)) is not None and i < len(lines):
+            more, _, comment = lines[i].partition("#")
+            code = code[: cut.start()] + more
+            comments.append(comment)
+            i += 1
+        yield code, comments

+def _bngl_parameter_name(code: str) -> str | None:
+    rest = _BNGL_INDEX.sub("", code.lstrip(_ASCII_BLANKS), count=1)
+    labelled = _BNGL_LABEL.match(rest)
+    if labelled and labelled.group(1)[0].isdigit():
+        return None  # "label begins with a number"
+    rest = rest[labelled.end() :] if labelled else rest
+    name = _BNGL_NAME.match(rest)
+    return name.group(0) if name else None

# python/bngsim/_bngl_loader.py: from_bngl reads its .bngl as one
-    model._frozen_params.update(_folded_at_load(_folded_at_load_in(_file_text(path)), model._core))
+    model._frozen_params.update(
+        _folded_at_load(_folded_at_load_in(_file_text(path), bngl=True), model._core)
+    )

Testing

python/tests/test_folded_at_load_bngl_lines.py, 24 tests:

  • BNG2.pl as the oracle (10, skipped without BNG2.pl): each row of the table above, and an index, a label and three joined lines, loaded with from_bngl. The test reads f's value from the network BNG2.pl generates and checks it is the value the declaration gives. It then checks frozen_params == ['f'], that the write to f is refused with the comment's reason, and that k takes one. With no blank before the backslash, g\ / 2.0 declares g2 in BNG2.pl's network, and g2 is the parameter frozen.
  • The reader (14): the same forms; a CRLF file; the first of two marks; a backslash inside a comment; beginparameters and end parameters; another block's name; what declares nothing (=, 2f 1.0, 3 4 1.0, other:, : f 2, 3abc: f 2); a no-break space after the backslash (not a blank to Perl); a .net read one line at a time.
  • test_convert_frozen_parameters.py: the forms convert: a parameter folded at load is left out or marked, not written free (#1792) #1988 tests read the same with bngl=True.

Against main's library code, 8 of the 10 BNG2.pl tests fail, and the 2 that pass are the first two rows, which main read right. The reader tests fail on main at the new argument.

How this was checked

  1. Every .bngl in the repo, against BNG2.pl. There are 113 files under benchmarks/, python/tests and docs/. In each, every line of the parameters block was marked folded_at_load=x, and the names read were compared with the parameters of the network BNG2.pl 2.9.3 generates from the marked file, leaving out the ones it generates itself (_rateLaw<n>, _R<n>_local<k>, _AEact<n>_<k>).
    • 96 files generate a network, 36 of them with a continued line. This branch's names equal BNG2.pl's in all 96. Main's differ in 2: both copies of RAFi_ground.bngl continue a parameter over several lines, and main takes the continuation's expression for a name. That matters only once those lines are marked; the files carry no mark.
    • The other 17 do not generate a network: 7 time out at 90 s (large rule sets), and 10 fail in BNG2.pl without the marks too.
  2. What the converters write. The 41 marked .bngl files sbml_to_bngl wrote from the sbml_to_bngl writes a compartment size the SBML model refuses to change (unwritable_compartment_size_params) as a free parameter: set_param('c1', 3) on the converted model is accepted and gives A(2) = 0.049787 for 0.135335 #1992 corpus A/B read the same on main and on this branch.
  3. Mutants, each a bug planted on purpose and 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: 12 of 12 killed. They are: comments of joined lines dropped; a blank put between joined lines; no label taken off; no index taken off; .net joined; .bngl named the .net way; a name starting with a digit; the last mark preferred to the first; begin needing a blank; a label starting with a digit accepted; Unicode blanks; the block never closed. A guard the first round of mutants showed to be dead code was taken out.
  4. macOS arm64, full suite on a7981c69: 27,187 passed, 200 skipped, 153 xfailed, 0 failed.
  5. Linux x86-64 (bngsim-docker's images), the 19 test files the gate lists as touching what changed (every bngl, folded and load test among them) on a7981c69: in the BioNetGen image, which has BNG2.pl, 882 passed, 51 skipped, 0 failed, all 24 new tests among them; in the Debian image, 813 passed, 120 skipped, 0 failed (the BNG2.pl tests skip: no BNG2.pl there).
  6. Timing: the reader runs only on a file that has folded_at_load in it. On the three largest repo .bngl files with every parameter line marked, main against this branch, 7 runs of 20 reads each, median: 0.22 → 0.34 ms, 0.25 → 0.53 ms, 1.53 → 2.23 ms (Dolan2015, 273 KB). The BNG2.pl run that the same load makes takes seconds.
  7. pre-commit --all-files passes; the docs build with sphinx -W.
  8. The pre-PR gate (bngsim-preflights' prepr_check.py), on a7981c69 and this description: GATE.

With #2139. #2139 (#1992) also changes python/bngsim/_model.py, in the frozen_params and unwritable_compartment_size_params docstrings. The two merge cleanly in either order (git merge-tree, 58838262 with a7981c69), and neither reads what the other changes.

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; neither runs the new file, and the reader is pure Python, the same on every platform (Linux above).

Review

One round of a whole-branch /code-review, on 48cff45e; a7981c69 is that code plus its fixes and a rename (the helpers were _net_lines and _bngl_lines, and convert/_net_writer.py has a _net_lines of its own). It found no bug the change introduced; it checked the reader against get_line, read_block_array and readString, and found three gaps against them, all fixed in a7981c69:

  • A block opened beginparameters was not read; BNG2.pl opens it (s/^begin\s*//). Pre-existing, on main too. Now read, with a test against BNG2.pl.
  • Blanks were Unicode where Perl's are ASCII, so a no-break space after a backslash continued the line. BNG2.pl refuses such a file, so nothing was frozen wrongly. Now ASCII.
  • A label starting with a digit (3abc: f 2) gave a name, where BNG2.pl refuses the line. It now gives none. The docstring no longer says every declaration BNG2.pl refuses gives none: lbl:f 2 gives lbl, and BNG2.pl refuses that file before the name is read.

Not changed:

  • .net files are read one line to a declaration, with convert: a parameter folded at load is left out or marked, not written free (#1792) #1988's name rule, as bngsim's loader reads them. BNG2.pl reads a .net through the same readFile and get_line (BNGModel.pm), so it would join a continued line there, but bngsim's loader does not, and the reader follows the loader that builds the model. No .net BNG2.pl writes has one.
  • _refuse_tables_in_parameters reads the .bngl's parameters block line by line, but looks for tfun( on any line of the block, so a continued declaration cannot hide one from it.

…eads (lanl#1994)

Model.from_bngl read each line of the parameters block as one declaration
and the first blank-separated word as its name. BNG2.pl joins a line whose
code ends in a backslash with the next (get_line), and takes the name as
the identifier the declaration starts with (ParamList::readString). So a
hand-written comment after `f \` / `k` froze k and left f writable, and
one on `f(2.0)`, `f(k+1)`, `f-2.0` or a continued number froze nothing.

The .bngl is now read as BNG2.pl reads it. A .net is read one line to a
declaration as before, as bngsim's loader reads it.
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