Repository navigation
sensitivities: ExprTk's implicit products are written out before a derived expression or a rate law is scanned or parsed (#1937) - #2152
Merged
Conversation
…rived expression or a rate law is scanned or parsed (lanl#1937) The engine reads 2j, (1+1)j and j(2) as 2*j. The readers that build forward sensitivities scanned the text for the word j and found none: A0 = 2j gave dA/dj = [0, 0, 0] with no warning, a law k*2j failed its build on Python's imaginary I, and a plain run stepped over a pulse centred on c = 2j. The pass the C emitter uses (lanl#1212) now runs first in the derived parse, where derived expressions are collected for the seeds, the chain rule, the kink check and the pulse stops, and, for the pairs the lexer multiplies, in the rate-law parse. Signed-off-by: Montana <montana@linux.com>
…itten declines, a number against a parameter named time is left, reserved words are read as written, and a logical derived parameter has a zero column 0.5Atot(2) parsed with Atot(2) as a call once 0.5Atot was written out, and its output sensitivity came back zeros where main refused. 2time beside a parameter named time is the clock to the engine, and was given the parameter's column. 2In was left unwritten (the engine's ExprTk is case-sensitive). [j and k] reached sympy's derivative of And, which is 1. A pulse centred on j(2) in the law itself is found. Signed-off-by: Montana <montana@linux.com>
…aw text, let the law parse group with square and curly brackets, and tests for the kink refusal The derived parse writes its own products, so the dicts collected for the chain rule and the pulse gate read the same either way. A law with j[2] declines through the call it parses to, which leaves [j + 1]2 free to parse. Signed-off-by: Montana <montana@linux.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1937
The engine reads ExprTk's implicit products (
2j,2 j,(1+1)j,(j)2,j(2)) as2*j, so a plain run is right. The Python readers that build forward sensitivities scanned the text for the wordj, found none in2j, and took the expression to name no parameter: a column of exact zeros with no warning.The issue's model (
j 2,A0 2j,A -> 0atkd = 0.5,sensitivity_params=["j"]):A02*j2j,2 j,j(2),(j)2,(1+1)j,2.j,1e0j*2,2(j),j[2]The products are now written out, by the pass the C emitter already uses (
_insert_implicit_products, #1212), before a derived parameter's expression is scanned or parsed, and a rate law's lexer-level ones before its parse.Two more results of the same misreading, on main:
kd*2jfailed its sensitivity build: Python reads2jas an imaginary number, and the generated C had an undeclaredI. It builds here, and its column matcheskd*2*j's.0 -> Aat1000*exp(-((time() - c)/0.0005)^2)withc = 2j, j = 2, gave A(10) = 0 for 0.886227. The reader that places a stop at a pulse's centre could not evaluate the centre. Here: 0.886227.Important
The MIR and Windows legs fire on
_codegen.pyand run listed test files.test_codegen_sens_fd_oracle.pyis on the lists ofmir.ymlandwindows-tail.yml(the three new Terms run there); the newtest_sens_implicit_products.pyis on none of them, nor onwindows-klu.yml's. The change is Python only, and the Ubuntu and macOS suites run both.The diff (abridged)
python/bngsim/_codegen.pyThe one way a reader gets the text with its products written: the emitter's pass, with the names that are scalars (so
j(2)is a product andexp(2)a call), or with none (the pairs the lexer multiplies)._ScalarNameshands a set to the pass as its lookup without building a dict per expression._prepare_derived_expr, the parse every derived-parameter partial comes from: the products first, then the name scan that decided "names no parameter".compute_ic_param_sens_seed: its own scan for the primaries an expression reaches read1e0jas the namee0j.In the pass itself (and one more line like it): the engine builds ExprTk case-sensitive, so
InandIFare names, and2Inis twice the parameter. The emitter reads the same line.In both twins of the direct partials: a derived parameter that is a logical operation (
j and k) is 0 or 1, and sympy's derivative of the bareAndis 1.python/bngsim/_jacobian.py_exprtk_to_sympy, the rate-law parse. It is not told the model's names, so only the lexer's pairs are written andk(x)stays a call. A text it rewrote that then parses with a call of an unknown name (0.5Atot(2),j[2]) declines, as it did when it failed to parse.python/bngsim/_smooth_pulses.py,python/bngsim/_parameter_kinks.pyThe readers that inline derived parameters into a law before parsing it: the pulse stops (the derived text and the law's own) and the kink check (the derived text).
Review
One round of a whole-branch
/code-reviewon2fe05755, at high effort, to the bar in "How much to review". It returned two blocking findings, both new with that commit, and four that are wrong on main too. The two are fixed in2415ea89with three of the four; I read the fix commits' diff once more, ran the mutants below, and stopped.g() = 0.5Atot(2)parsed, once0.5Atotwas written, withAtot(2)a call.test_an_output_expression_with_a_name_against_a_bracket_is_refusedA0 = 2timebeside a parameter namedtime: the engine reads the clock (A0 = 0), and the column was the parameter's, [2, 1.41, 0.77] for zeros. Needs a hand-written.net; BNG2.pl refuses the name.test_a_number_against_a_parameter_named_time_is_read_as_the_engine_reads_itA0 = 2In, a parameter whose name lower-cased is a reserved word: zeros, as for2j.test_a_reserved_word_in_another_case_is_a_nameA0 = j and k: a column of [1, 0.70, 0.39] for zeros;[j and k]joined it with this change.test_a_derived_parameter_that_is_a_truth_value_has_a_zero_columnj(2)written in the law itself was still stepped over.test_a_pulse_centred_on_a_product_is_not_stepped_over1000 - 1000tanh(...),2time()insideexp,2abs(j - 1.5)): the pulse is stepped over, the kink is not refused.The round also ran about 24,000 generated derived-parameter texts through the pass against the engine (no change of meaning beyond the
timeand reserved-word rows), 16 steady-state sensitivity cases (right here; main gave 0 forks = 2j), and timed the new calls: 12.9 ms of a 7.8 s sensitivity build on Smith2013 (948 parameters) and 57 ms of 60.8 s on BIOMD0000000496.0b39afbdis from the mutant run and not the review: two write-outs that no test could tell from the raw text were removed (the derived parse writes its own), and the law parse's refusal of square and curly brackets was replaced by the call check above, which already coversj[2].Left alone
_rate_law_flat_partsin_switch_sensitivity.py, the engine refactor's (Tracking: hold each rate-law switch's branch fixed between located crossings, and rebuild the integrator's crossing handling around it #1781). Triggers, the same on main:0 -> Aat1000*exp(-((2time() - 8)/0.001)^2)gives A(10) = 0 for 0.886227;f() = 2abs(j - 1.5) + 0.5at j = 1.5 returns a column where2*abs(...)is refused.f() = (j(2))/8*(1 + 0.01*Atot)) is refused, or runs on the difference quotient, as on main: the law parse is not told the names.j(2)written in it (abs(j(2) - 3)): not refused, as on main. The derived-parameter spelling is._translate_expr_jaxleaves2jas written on both builds. jax is not installed here, so whether its check against the engine refuses the model was not run.Testing
python/tests/test_sens_implicit_products.py, new (135 cases), and three Terms intest_codegen_sens_fd_oracle.py(6 cases), on0b39afbd. With main's code (and a stand-in for the new helper, so the file imports): 89 failed, 52 passed. Here: all 141 pass. The 52 are controls and refusals: the*spelling, numbers with exponents, names with digits in them, calls, reserved words as written.How this was checked
0b39afbd(main at49c19797), macOS arm64, Python 3.13,uv sync --extra testplus h5py,BNGPATHat BioNetGen 2.9.3: 27003 passed, 548 skipped, 151 xfailed, 1 failed. The failure istest_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 thetestextra does not install).mutate.pyfrom bngsim-preflights) on0b39afbd: 14 of 14 killed: the derived parse writing no products, or given no names; the seed's expressions left raw; thetimerule dropped, in either place; reserved words lower-cased; a declined text raising; a logical operation keeping sympy's derivative, or a comparison counted as one; the law parse writing nothing, or keeping a call of an unknown name; the pulse reader's derived text, or its law, left raw; the kink reader's derived text left raw. A first run on2415ea89left 4 of 17 alive: one needed a test (the kink refusal, added) and three showed code no result depended on, removed in0b39afbd.2*jin 7 places an expression can sit (a seed, a rate constant, two chains through another derived parameter, a function, a function over an observable, one over the clock), both sensitivity methods, each column against a central difference of plain runs. Main: 64 of 196 wrong. Here: 0 wrong, 192 right, and the same 4 refused on both (j(2)in a law over the state or the clock).0b39afbd, sides alternated:_exprtk_to_sympyover 2,000 laws written to send every one through the tokenizer (an exponent and a call after a digit in each): main 1288 to 1361 ms, here 1337 to 1457 ms, 3 to 8% of the parse._prepare_derived_exprover 2,000 derived expressions among 6,000 names: main 1290 to 1383 ms, here 1313 to 1472 ms. A law with nothing that could be a product is one regex search more. On whole builds the review's timing is under Review.pre-commit run --all-files,ci/changelog.py check,sphinx -W: pass.bngsim_docker.py test --merge-main --kind debian --arch arm64):0b39afbdmerged with main49c19797,test_sens_implicit_products.py,test_codegen_sens_fd_oracle.py,test_smooth_pulse_stops.pyandtest_derived_seed_engine_rounding.py: 311 passed.prepr_check.pyfrom bngsim-preflights) on0b39afbdwith this description: 0 failed, 8 warnings, 35 passed. The warnings: the changelog fragment is 1,125 of 1,200 characters; the CI legs (above); the callers of the changed functions, to read (they take the parse's result, and a text with no product in it is returned as it was); code changed after the reviewed commit (described under Review); the suite and mutant logs are older than the files' time on disk (item 4 checked main'spython/bngsimout and back for each timed side, byte for byte); the Linux run covers this PR's two test files and two neighbours, not every file withsensin its name (CI's legs run them all); claims to recount; the Left alone triggers were rerun on0b39afbd.benchmarks/perf_ab.py(it times runs of models with no implicit product; the pass's cost is item 4).