ENH: continue a Monte Carlo study from the root its rows were drawn with - #1187
ENH: continue a Monte Carlo study from the root its rows were drawn with#1187thc1006 wants to merge 5 commits into
Conversation
|
cc @zuorenchen, since this sits next to your Updating this, because what it originally said has been overtaken. The series it listed has landed: #1181, #1169, #1170 and #1174 are on What it does: an append continues the root its rows were drawn with, rather than quietly starting a second one in the same file. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1187 +/- ##
===========================================
+ Coverage 91.51% 91.58% +0.07%
===========================================
Files 132 132
Lines 18305 18404 +99
===========================================
+ Hits 16751 16855 +104
+ Misses 1554 1549 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
b238a13 to
ba3bfd3
Compare
893ce8a to
d7e843f
Compare
|
@thc1006 conflicts must be solved before reviewing have you tired stacked PRs before? See: https://docs.github.com/en/pull-requests/how-tos/stacked-pull-requests |
|
Conflicts are gone. The branch is squashed to one commit and rebased onto current On the stacking, since you asked: both of mine were stacked and neither is now. #1182 sat on #1181, which landed. This one sat on #1054, and I closed #1054 rather than keep the chain going, because this branch is a superset of it. The per-index seeding is here in full and the append lineage sits on top of it. Both target The merge was not mechanical, so one thing is worth flagging. #1169 landing made a sentence in this branch's documentation false. The note said a run moves the nominal as it goes, so a flight could still differ by which worker took an index. That is exactly what #1169 closed, and the four cases it leaves outside the rule turn out to be decided by the index too: a component position is read from a component the run never writes back to, and The existing split test could not have caught that. Every stochastic input in its fixture is given an explicit nominal, so nothing reads one off the wrapped object, and taking the kept nominal away leaves all five of those tests green. I added one that uses a bare standard deviation, which is the form that does read one, with a control that survives. The numbers are the test's own, so they are reproducible by running it: index 3 draws an elevation of 1400.221102854623 whether it is reached through 0, 1 and 2 or taken on its own, and taking the kept nominal away leaves 1367.1672115143288 against 1367.3883143689518. The commit message quotes a standalone probe with a nominal of 1000 instead, which is where 1002.211028546 and 671.672115143 come from. |
6d3192f to
c448dd0
Compare
c448dd0 to
e339980
Compare
|
Heads up @thc1006 — I rebased this branch onto The conflict was one hunk in Keeping both pushed the function to 27 statements, two over What the textual merge could not show is that #1177's interrupt tests were written against the loop you replace. Four things had to be carried over:
The last two are your intended change, pinned by your own One addition worth your review: the changelog entry was under Verified locally before pushing: |
e339980 to
dee8438
Compare
|
Thanks for doing that, and no apology needed. Resetting onto it was the right call over leaving it conflicted. I checked the two things you asked about and both hold. The The The four carried-over test changes read correctly to me, and the run confirms it. Two things from my side. #1137 landed after your rebase and conflicted again, on one line: it dropped the The rebase also dropped one line from my commit message, so the paragraph about #1182's worker tests started mid-sentence on Verified here on The ten are the same ten on both, all optional dependencies this machine does not have. |
|
Two commits on top, and one of them is about the It is not a flake in the usual sense, and it is not this branch's. The failure is in The clock is a counter in that test now, so what each worker is offered is decided by arithmetic rather than by the platform's timer resolution. I also asserted the shape it is actually about: inside one stage the offers shrink along the fleet, where one deadline each would hand every worker the whole grace. Giving each worker its own grace turns both assertions red and leaves the other twelve in the file green. It is a test-only change to a file this branch does not otherwise touch, and it is here because it blocks this pull request's CI. Glad to move it to its own pull request if you would rather keep them apart, and it would be worth landing either way, since it will keep catching Windows runners. The other commit came out of reading your Verified on The ten are the same ten on both, all optional dependencies this machine does not have. |
2d20c79 to
65fda0c
Compare
|
Rebased onto Only Your Full |
A Monte Carlo run cannot be seeded on develop: simulate() takes no random_seed. Seeding it per simulation index is not enough on its own, because an append then derives a fresh root and writes it into the same file, so a study resumed after a restart holds two lineages with nothing afterwards to say which simulation came from which. Both halves are here. A simulation takes its seed from its own index, so a serial run and a run split over workers draw the same inputs for the same index. Every input row records the root that drew it, and an append reads it back rather than needing to be given it again. A seed that disagrees with the rows is refused, as is a log whose rows disagree with each other, and one whose rows carry no root at all, which is how a log written before this looks. Output rows carry a digest of that root, so a log belonging to another study is refused even when its indices line up with this one's. The worker tests in RocketPy-Team#1182 drive the producer with a stand-in monitor, so they move to the claim along with it. A reseed failure now names the index it was seeding for rather than worker startup, because the seeding happens after the claim rather than once above the loop. The seeding half was RocketPy-Team#1054, closed in favour of this. Addresses RocketPy-Team#1053 and RocketPy-Team#1075. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
…ial loop The rebase onto develop was clean in three files and left one add/add conflict in __run_in_serial: both sides bind a name before the try so the handlers cannot meet it unbound. RocketPy-Team#1177 binds inputs_json for the KeyboardInterrupt handler, this branch binds sim_idx for the Exception one. Both are kept; dropping either puts back the UnboundLocalError the other side had just removed. Keeping both then pushed the function to 27 statements, two over max-statements, so the error-file append the two handlers had copies of moved into __record_failed_inputs. The guard travels with it: the error file is created in __setup_files, so appending "" to it was already a no-op, and the unconditional copy and the guarded one did the same thing. What the merge could not show is that RocketPy-Team#1177's tests were written against the loop this branch replaces: - _InterruptingMonteCarlo stubs only what __run_in_serial touched, and the loop now also calls __seed_this_simulation, which reads models the double has none of. Stubbed, like the other name-mangled members; seeding is pinned by test_monte_carlo_seeding.py. - Its rows carried no run_root, so the append test tripped the guard for studies written before that check existed instead of exercising the continuation. The double now writes the root the way the real row builders do, the inputs file whole and the outputs file by digest. - _SimMonitor.keep_simulating is now claim_next_index, and one test monkeypatches it by name. - Serial numbering now starts at zero, as the parallel path always did, so the three tests reading indices off disk expected 1, 2, 3 where a run writes 0, 1, 2. tests/unit/simulation: 322 passed, 8 skipped. ruff clean, pylint 10.00. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The entry this branch added sits under Added, and both of these are visible to someone whose code already reads a study off disk: a serial run now numbers its simulations from zero, so the index field shifts by one, and an append onto a study written before this release is refused rather than continued. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Reviewing the Changed entry @Gui-FernandesBR added turned up a gap on my side: the serial renumbering is a break for anyone reading indices off disk, and the only place it was written down was the changelog. The guide's note leans on it without saying so. It claims simulation 7 draws the same inputs serially or split over workers, which is only one claim if both paths agree on which simulation 7 is, so the note now says a run of n numbers them 0 to n - 1. The notebook says it where a reader meets simulate(). Its stored outputs predate this and are left alone: nbsphinx never re-executes them, and a seeding change moves every number in them, which is not a diff worth reading. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
This branch's CI went red on windows-latest 3.10 with
assert min(offered) < 0.05
E assert 0.0500000000001819 < 0.05
The test lives on develop rather than in this branch, and RocketPy-Team#1182 is where I
added it. The stand-in workers return at once, so nothing makes the clock
move between _wait_for_the_workers setting its deadline and reading it back
for the last of them, and (t + 0.05) - t is not exactly 0.05 in binary.
Every other leg passed, and so did the same job on 3.14.
The clock is a counter now, so what each worker is offered is decided by
arithmetic rather than by how coarse the platform's timer is. The shape the
test is about is asserted as well: inside one stage the offers shrink along
the fleet, where one deadline each would hand every worker the whole grace.
Giving each worker its own grace turns both assertions red and leaves the
other twelve in the file green.
It sits here because it blocks this pull request's CI, and it is a test-only
change to a file this branch does not otherwise touch. Happy to move it to
its own pull request if you would rather keep the two apart.
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
65fda0c to
ffb6f9c
Compare
|
Thanks for rebasing it twice, and for checking the clock fix on your own machine rather than taking my word for it. Reproducing the failure 3/3 on plain TL;DR: I pushed once more. Nothing in the tree changed, the two tree hashes are the same. Two of my commit messages are reworded so that no line in them starts with What happened to the message. My first commit had a paragraph beginning I could not pin down which step drops it, and I would rather say so than guess: I tried a conflicted rebase resolved with So a line starting with Please do not read this as a complaint about the rebases. The fragile thing was in my message, and it is gone now. On the approval. It is against CI agrees with your machine, for what it is worth as a second data point: Verified here on The failures are the same set on both, all of them optional dependencies this machine does not have. |
A Monte Carlo run cannot be seeded on
develop:simulate()takes norandom_seed. Seeding it per simulation index is not enough on its own, because an append then derives a fresh root and writes it into the same file, so a study resumed after a restart holds two lineages with nothing afterwards to say which simulation came from which. Both halves are here. Addresses #1053 and #1075.This branch carries #1054's work as well, which is where the root comes from. That pull request is closed in favour of this one: its per-index seeding is here in full, with the append lineage built on top. The branch is rebased onto
88aed5a5and is five commits: three mine, two @Gui-FernandesBR added when he resolved the rebase onto #1177. The first of mine is 353 lines ofmonte_carlo.py, three test files of its own, a changelog line, a note in the stochastic guide, three added lines in the Monte Carlo notebook, and 71 lines of #1182's worker tests moved onto the claim this replaces; the other two are 18 lines of documentation and one test. His carry #1177's interrupt tests onto the same loop and add aChangedchangelog entry. Together the diff is ten files, 1331 insertions and 71 deletions.Pull request type
Checklist
ruff check/ruff format --check,pylint) has passed locallydevelop, listed below.CHANGELOG.md: one line, since one pull request is one line here and the workflow that would have written it has not run since CI: run the changelog job for pull requests from forks #1112 (BUG: the changelog workflow stopped running, and CHANGELOG.md is 37 merged pull requests behind #1173).Current behavior
On
developthere is no seed to carry:random_seeddoes not appear inmonte_carlo.pyat all, so a run cannot be fixed and an append continues an unseeded one.Per-simulation-index seeding, which was #1054 and is carried here, is not enough by itself. With it in place and nothing else,
append=Truederives a fresh root and carries on writing into the file it was given. Nothing records which root produced which row, so the file ends up describing two studies with neither tellable from the other. That is what #1075 is filed against.New behavior
simulate()takes arandom_seed, and each simulation derives its own from its index rather than from the worker that happens to run it. A serial run and a run split over any number of workers draw the same inputs for the same index, and the serial path numbers its simulations from zero the way the parallel one always did, so the two no longer give one simulation two names.Every input row carries the root that drew it. An append reads it back and continues from it, so resuming needs no seed, and a fresh object over the same files is the ordinary way to do it. A seed that disagrees with the rows is refused rather than mixed in. A log whose rows disagree with each other is refused rather than resolved, since that is two studies already and continuing either buries the other.
Rows that carry no root at all are refused as well, rather than read as an empty log. That is how a study written before this release looks, and taking it for an empty one starts the second lineage this exists to prevent. A genuinely empty log still starts normally, which has a test of its own.
Why the root is in the rows
The obvious alternative is a small file written beside the log. I built that first, in the branch this came out of, and it grew four problems with one root between them: a sidecar cannot be shown to belong to a log.
Names and counts match by coincidence. A copy taken from another study passes every check that reads only itself. The write that claims ownership has to be transactional with the log replacement. And the count it carries can drift from the rows it describes.
Putting the root in the rows removes the first two rather than solving them: there is no ownership write to make atomic and no count that can drift, because there is no second file. The other two become questions about the rows themselves, which is where they can be answered. The root a row records has to be one a stream can be rebuilt from, and the two logs have to name the same study; both are checked before an append writes anything. The cost is one field per row, in rows that already carry a serialized rocket.
What this does not do
It reads both logs. Every output row carries a digest of the root its input row records, so an output log from another study is refused even when its indices match, and an append will not start until the two agree on the simulations they hold. What it does not do is judge a finished run: whether every simulation the run was asked for reached the logs is the completeness check that landed with #1182, which this branch now sits on top of.
It does not repair a log with a hole in it. A run that lost simulations is refused, not filled in.
Two things I have deliberately left, and would rather not decide alone on a format people will have on disk: versioning the persisted root, and representing a wide entropy losslessly for readers that parse JSON numbers as doubles.
Breaking change
Two of them, both visible to code that reads a study off disk.
A serial run numbers its simulations from zero, as the parallel path already did. The two used to name the same simulation
1, 2, 3and0, 1, 2, so theindexfield in the inputs and outputs rows shifts by one for a serial run:nsimulations are numbered0ton - 1whichever way the run was started.append=Trueonto a log written before this release is refused, because such a log carries no root and cannot be shown to be one study. Re-running it writes a log this release can continue.Coverage
The patch showed six lines uncovered. Four belong to the append and now have tests: a
sequence seed reaching the row whole, a blank line not being a row, and a row that
will not parse being refused rather than read as an empty log. The other two came in
with the seeding work and their tests came with it: one that the serial failure
message names the same index the parallel path would, and one that drives the worker
loop in this process rather than a child. All six are on this branch now.
The documentation change is prose, inside one
.. note::and one docstring. The notepoints at the custom sampler page with a
:ref:that page already defines andstochastic.rstalready uses once, andbuild-docsis green on it.Additional information
Verification, on a clean tree:
The ten are the same ten on both, which is why I ran
developat the commit this branch sits on,88aed5a5. Each of them is an optional dependency this machine does not have:statsmodelsandprettytablefor the sensitivity tests,imageiofor the ellipses one, andtimezonefinder,windrose,ipywidgetsandjsonpicklefor the environment analysis ones.Each mechanism is pinned by a mutation:
Counts are from the branch as it stands on
88aed5a5, overtest_append_lineage.pyandtest_monte_carlo_simulation_index.py, where thecontrol is 43 passed. They were measured again after each rebase rather than
carried forward: #1182, #1129, #1177 and #1137 have landed under this branch
since the table was first written, and one of the anchors stopped being unique
when #1182 brought a second blank-line skip into the file, so that row was
re-measured on its own against the one in
_what_the_rows_say_drew_them. Thelast row is measured separately, since undoing it means taking #1169's kept
nominal away rather than editing this branch. Every mutation is reverted from
git show HEAD:rather than from a copy, after one run left a file mutated andthe next took that as its baseline.
Bringing
developin was more than a conflict fix. #1169 landing made this branch's documentation note false: it said a run moves the nominal as it goes, so a flight could still differ by which worker took an index, and closing that is what #1169 did. The four cases it leaves outside the rule are decided by the index too, so the note is rewritten and moved to the end of the component seeding section that #1170 added. The existing split test could not have caught it, since every stochastic input in its fixture is given an explicit nominal and nothing reads one off the wrapped object. The new test uses a bare standard deviation, which is the form that does. It measures index 3 at an elevation of 1400.221102854623 whether it is reached through 0, 1 and 2 or taken on its own. Take the kept nominal away and the two stop agreeing, 1367.1672115143288 against 1367.3883143689518.A fifth commit takes the wall clock out of one of #1182's join tests. It is on
developrather than in this branch, and it is mine: the stand-in workers return at once, so nothing makes the clock move between the shared deadline being set and being read back for the last of them, and(t + 0.05) - tis not exactly0.05in binary. It went red onwindows-latest3.10 at0.0500000000001819while every other leg passed. The clock is a counter now and the shape the test is about is asserted as well. It is here because it blocks this pull request's CI, and I am happy to move it out.#1177 landing turned up the same thing from the other side, and @Gui-FernandesBR resolved it rather than leaving the branch conflicted. Its interrupt tests stand a double in for
MonteCarlo, and the double stubs only what__run_in_serialreached: the loop now reseeds per index, so the double had nothing to reseed, and its rows carried no root, so the append test tripped the guard for logs written before that check instead of exercising the continuation. His two commits carry those tests over and add aChangedchangelog entry for the two user-visible breaks. I checked both against the source rather than the diff:developreally did number serial runs from one and parallel from zero, and the error-file append he factored out was a no-op for an empty row either way, since__setup_filesopens that filew+orr+before either handler can run.#1182 landing turned up a second one that neither branch could see alone. Its worker tests drive
__sim_producerwith a stand-in monitor, and that stand-in offerskeep_simulating()andincrement(), which this branch replaces with one claim; its_runalso passes the per-worker seed the producer no longer takes. Nineteen of them went red on the rebase. They move to the claim here, and one of them changes meaning rather than shape: seeding runs after the claim now instead of once above the loop, so a model that will not reseed is reported against the index it was seeding for rather than against worker startup. The test that covered the old wording asserts the new one, and a separate test still covers a failure before any index is claimed.Three things came out of reviewing this against itself, and all three are in:
.csvwas reported as a row that could not be read rather than as a formatsimulatecannot use. BUG: refuse to run a Monte Carlo over a results file it cannot write #1161's message goes first now.