Skip to content

ENH: let a convergence study be reproduced from one seed - #1198

Open
thc1006 wants to merge 9 commits into
RocketPy-Team:developfrom
thc1006:enh/a-convergence-study-from-one-seed-v2
Open

thc1006 wants to merge 9 commits into
RocketPy-Team:developfrom
thc1006:enh/a-convergence-study-from-one-seed-v2

Conversation

@thc1006

@thc1006 thc1006 commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #1077.

While this was open, #1213, for the release blockers in #1209, put the seed pass-through and its changelog entry on develop (9e3cc63). I merged develop and took its side of both conflicts, so what this PR adds now is the follow-up from review.

Pull request type

  • Code changes (bugfix, features)

Checklist

  • Tests for the changes have been added (if needed)
  • Docs have been reviewed and added / updated
  • Lint (ruff check / ruff format --check, pylint) has passed locally
  • All tests have passed locally
  • CHANGELOG.md: a line under Unreleased. The changelog workflow skips a PR that already has one.

Current behavior

simulate_convergence passes random_seed to every batch but bootstraps each confidence interval from SciPy's global generator. Two runs with the same seed can return different widths and, near the tolerance, stop after a different number of batches. A bad random_seed fails only inside the first batch, and not at all if the logs already hold max_simulations rows or more.

New behavior

The bootstrap is seeded from the study's seed, and set_inputs_log and set_outputs_log return the rows in simulation order when each row records its index. So a study run again with the same seed stops after the same batch, in parallel or not. That includes a study that drew its own seed, run again with the one it printed. random_seed is checked with the other inputs, before any log is read, and the docstring says what the seed does not fix: where the study stops can depend on batch_size, and a finished study runs nothing. That case now warns.

Breaking change

  • No

Additional information

The integration tests sit in test_monte_carlo_release.py, beside the convergence tests develop added, and the cross-batch one there now compares whole input rows. Taking out the bootstrap seed, seeding it from the argument instead of the study, reading the logs in the order workers wrote them, or dropping the up-front check or the warning each makes one of them fail.

Merged with develop at v1.14.0, the unit, integration, doctest and acceptance tests pass on Python 3.10 and 3.14 with .[all], and so do the slow Monte Carlo tests. ruff 0.17.6 and pylint 4.1.4 are clean.

Two things I found along the way are filed separately as #1229 and #1230.

Each batch derived a root of its own, so a study could not be repeated and
the same max_simulations reached different samples per batch size.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006
thc1006 requested a review from a team as a code owner September 16, 2026 20:54
@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.85%. Comparing base (2f09362) to head (f8e20dc).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1198   +/-   ##
========================================
  Coverage    87.84%   87.85%           
========================================
  Files          130      130           
  Lines        18185    18194    +9     
========================================
+ Hits         15975    15984    +9     
  Misses        2210     2210           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Gui-FernandesBR Gui-FernandesBR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nine findings inline. The one on random_seed=random_seed affects what the feature promises; the rest are smaller.

Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread CHANGELOG.md Resolved
develop now carries the seed pass-through and its changelog entry
(9e3cc63, RocketPy-Team#1209), so both conflicts take develop's side.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
simulate_convergence handed its seed to every batch but bootstrapped each
confidence interval from SciPy's global generator, so the same seed gave a
different confidence_interval_history and, near the tolerance, a different
stopping batch. The bootstrap now takes a 32-bit seed spawned off the study
seed. That also covers a study that drew its own seed and is run again with
the one it recorded.

A random_seed of the wrong type or sign is now refused with the other inputs,
before the logs are read, including when they already hold every simulation
and nothing would run. The docstring says what the seed does not fix: where
the study stops can depend on batch_size, and a finished study runs nothing
whatever seed it is given.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
The cross-batch test compared mass alone, which comes from the rocket's
stream, so a change in the environment or flight draws would still pass. It
now compares each input row in full and in order.

The three tests this branch added ran twenty Calisto flights, keyed the rows
they compared by index, and copied their helpers from other files. What they
checked is now covered next to the convergence tests develop already has, on
its light release_study fixture, so the file is gone.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006
thc1006 force-pushed the enh/a-convergence-study-from-one-seed-v2 branch from 3f7b767 to 1176b3e Compare November 20, 2026 11:09

@Gui-FernandesBR Gui-FernandesBR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough round. One finding left, inline: with parallel=True the seeded bootstrap can still give a different stopping batch.

Comment thread rocketpy/simulation/monte_carlo.py Resolved
Parallel workers append rows as they finish, so two runs with the same seed
could hand the seeded bootstrap the same values in a different order and get
different widths, which near the tolerance means a different stopping batch.
Each batch now reads the outputs, sorts both logs by index and rebuilds
results before the bootstrap. Sorting the inputs as well keeps each input
row next to its output.

The changelog entry for this pull request now mentions the seeded bootstrap.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread rocketpy/simulation/monte_carlo.py Outdated

return confidence_interval_history

def __read_outputs_in_simulation_order(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): the order fix only exists inside simulate_convergence. After it returns, results, inputs_log and outputs_log are sorted in memory while the files keep arrival order, and the next simulate or import_outputs reloads them unsorted.

So estimate_confidence_interval(..., random_state=s) called by a user on a parallel study still depends on which worker finished first, and gives different numbers before and after a reload. Fix: sort once where the logs are read (set_outputs_log / set_inputs_log) so every path sees index order.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5d214b3. set_inputs_log and set_outputs_log now sort the rows by index when every row has an integer one, and keep the file order otherwise, as in the old example.outputs.txt fixture. test_logs_written_out_of_order_are_read_back_by_index reverses both logs, reloads them, and checks the order and that a seeded estimate_confidence_interval gives the same interval as before.

Comment thread rocketpy/simulation/monte_carlo.py Resolved
Comment thread rocketpy/simulation/monte_carlo.py Resolved
…study-from-one-seed-v2

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
Sorting by index only inside simulate_convergence left results, and any
seeded estimate_confidence_interval a user ran, in arrival order after the
next reload. set_inputs_log and set_outputs_log now sort when every row
records an integer index, so the convergence loop no longer parses the
outputs a second time per batch or rebuilds results next to a stale
processed_results.

A finished study now warns that nothing was run, also when the logs hold
more than max_simulations rows, and the Returns section says so. The study
seed defaults to None on the class, so a class overriding simulate no
longer has to set the mangled name.

The convergence tests derive their tolerance from the run's own widths,
run the parallel case on two workers for two batches, and compare its
widths with pytest.approx. Since v1.14.0 has shipped, the changelog gets a
new entry under Unreleased.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006

thc1006 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Merged develop. v1.14.0 shipped with the earlier changelog line, so the seeded bootstrap, the log order and the warning have a new entry under Unreleased.

simulate_convergence is at 25 statements, pylint's limit. Merged with #1233 as it stands, it comes to 27, so whichever of the two lands second still needs the extraction I mentioned.

One docs example now warns. In monte_carlo_class_usage.ipynb, the simulate_convergence cell runs on the study the cell above filled to 1000 simulations, with max_simulations=1000, so it already ran nothing. A study of its own would leave untracked logs next to the tracked ones in monte_carlo_analysis_outputs/ each time someone runs the notebook, so I have left it for you to decide. I can change it here or separately.

@Gui-FernandesBR

Copy link
Copy Markdown
Member

Two pre-existing bugs showed up in this round; neither comes from this PR, so they shouldn't block it, but could you track them?

  • simulate_convergence(batch_size=1) on a new study fails in SciPy's bootstrap, which needs at least two rows. Already simulate_convergence(batch_size=1) fails on the first batch of a new study #1229, just linking it.
  • set_num_of_loaded_sims counts every line of .outputs.txt, blank ones included, while _checkpoint_rows and the log readers skip them. A log with a blank line overstates num_of_loaded_sims, so the new "logs already hold N simulations" warning can quote the wrong N or fire on an unfinished study. Could you open an issue for this one?

@Gui-FernandesBR Gui-FernandesBR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything from the previous rounds is fixed in 5d214b3, and CI is green. Thanks!

@thc1006

thc1006 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Opened #1255 for the blank-line count. It reproduces on develop: two rows and two blank lines load as four simulations, and simulate_convergence(max_simulations=4) returns [] without running anything. A .csv log is affected the same way.

@Gui-FernandesBR

Copy link
Copy Markdown
Member

@thc1006 could you kindly fix git conflict before merging?

develop added the RocketPy-Team#1254 and RocketPy-Team#1199 lines under Fixed. The RocketPy-Team#1198 line
goes above them, since the changelog asks for the newest changes on top.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
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.

2 participants