From 675a4b1041d35529bafea5fb83e41782ba1215a7 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Thu, 19 Nov 2026 21:22:38 -0300 Subject: [PATCH 1/6] MNT: make the parachute noise seed private Parachute no longer takes a public seed. StochasticParachute and from_dict set it through _set_noise_seed, which also redraws the first noise sample so the sequence is unchanged. to_dict still writes the seed, and from_dict still loads dicts without one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW575d9C9zpS7Z1v2KXVq --- rocketpy/rocket/parachute.py | 32 ++++--- rocketpy/stochastic/stochastic_parachute.py | 10 ++- .../unit/rocket/test_parachute_noise_seed.py | 89 +++++++++++++++++-- 3 files changed, 110 insertions(+), 21 deletions(-) diff --git a/rocketpy/rocket/parachute.py b/rocketpy/rocket/parachute.py index bef27a6a5..0cd2b5aaf 100644 --- a/rocketpy/rocket/parachute.py +++ b/rocketpy/rocket/parachute.py @@ -167,7 +167,6 @@ def __init__( height=None, porosity=0.0432, drag_coefficient=1.4, - seed=None, ): """Initializes Parachute class. @@ -253,12 +252,6 @@ def __init__( - **1.5** — extended-skirt canopy Has no effect when ``radius`` is explicitly provided. - seed : int, array_like, SeedSequence, BitGenerator, Generator or None, optional - Seed for the per-instance NumPy Generator used by pressure noise. - A fixed seed makes the noise reproducible and independent of the - process-global NumPy RNG (and therefore usable under Monte Carlo). - ``None`` keeps the noise random but still drawn from this instance's - generator. Default is ``None``. """ # Save arguments as attributes @@ -273,8 +266,8 @@ def __init__( # Per-instance RNG: pressure noise must not draw from the process-global # NumPy RNG, or Monte Carlo cannot reproduce deployment (see #1091). - self._seed = seed - self._rng = np.random.default_rng(seed) + self._seed = None + self._rng = np.random.default_rng() # Initialize derived attributes self.radius = self.__resolve_radius(radius, cd_s, drag_coefficient) @@ -314,7 +307,7 @@ def __init_noise(self, noise): noise : tuple, list List in the format (mean, standard deviation, time-correlation). """ - self.noise_signal = [[-1e-6, self._rng.normal(noise[0], noise[1])]] + self.__draw_initial_noise() self.noisy_pressure_signal = [] self.clean_pressure_signal = [] self.noise_bias = noise[0] @@ -332,6 +325,21 @@ def __init_noise(self, noise): + beta * self._rng.normal(noise[0], noise[1]) ) + def __draw_initial_noise(self): + """Starts the noise signal with one sample from the current RNG.""" + self.noise_signal = [[-1e-6, self._rng.normal(self.noise[0], self.noise[1])]] + + def _set_noise_seed(self, seed): + """Reseeds the pressure noise and redraws its first sample. + + Not public API: ``StochasticParachute`` and ``from_dict`` use it so a + run can reproduce its noise. The first sample is redrawn because the + constructor took it from an unseeded generator. + """ + self._seed = seed + self._rng = np.random.default_rng(seed) + self.__draw_initial_noise() + def __evaluate_trigger_function(self, trigger): # pylint: disable=too-many-statements """This is used to set the triggerfunc attribute that will be used to interact with the Flight class. @@ -559,7 +567,9 @@ def from_dict(cls, data): drag_coefficient=data.get("drag_coefficient", 1.4), height=data.get("height", None), porosity=data.get("porosity", 0.0432), - seed=data.get("seed", None), ) + # Dicts written before the seed was recorded have none. + if data.get("seed") is not None: + parachute._set_noise_seed(data["seed"]) return parachute diff --git a/rocketpy/stochastic/stochastic_parachute.py b/rocketpy/stochastic/stochastic_parachute.py index 6ff3775be..1e933d7eb 100644 --- a/rocketpy/stochastic/stochastic_parachute.py +++ b/rocketpy/stochastic/stochastic_parachute.py @@ -207,11 +207,17 @@ def create_object(self): # Tie pressure noise into the Monte Carlo seed tree when one is set. # Key by parachute name so drogue and main on the same rocket do not # share one noise stream. + noise_seed = None if self._seed is not None: - generated_dict["seed"] = _sampler_seed( + noise_seed = _sampler_seed( self._seed, ("pressure_noise", generated_dict["name"]) ) + generated_dict["seed"] = noise_seed # Recorded after the seed is in, or the inputs describe a parachute # with noise nobody can reproduce. self._record_draw(generated_dict) - return Parachute(**generated_dict) + del generated_dict["seed"] + parachute = Parachute(**generated_dict) + if noise_seed is not None: + parachute._set_noise_seed(noise_seed) + return parachute diff --git a/tests/unit/rocket/test_parachute_noise_seed.py b/tests/unit/rocket/test_parachute_noise_seed.py index 1cd702eaf..d57efdd98 100644 --- a/tests/unit/rocket/test_parachute_noise_seed.py +++ b/tests/unit/rocket/test_parachute_noise_seed.py @@ -1,25 +1,28 @@ """Determinism tests for seeded parachute pressure noise (#1091). -Pressure noise is drawn from a per-instance ``numpy.random.Generator`` created -from the ``seed`` argument, instead of the process-global ``numpy.random``. -A seed makes the noise reproducible and independent of the global RNG state. +Pressure noise is drawn from a per-instance ``numpy.random.Generator`` instead +of the process-global ``numpy.random``. The seed is private: only +``StochasticParachute`` and ``from_dict`` set it, after construction. """ import numpy as np +import pytest from rocketpy import Parachute from rocketpy.stochastic import StochasticParachute -def _parachute(seed, noise=(0, 8.3, 0.5)): - return Parachute( - name="main", +def _parachute(seed, noise=(0, 8.3, 0.5), name="main"): + parachute = Parachute( + name=name, cd_s=10.0, trigger="apogee", sampling_rate=100, noise=noise, - seed=seed, ) + if seed is not None: + parachute._set_noise_seed(seed) + return parachute def _noise_sequence(parachute, n=16): @@ -80,7 +83,27 @@ def test_seed_survives_serialization_round_trip(): def test_from_dict_defaults_seed_to_none_when_absent(): data = _parachute(11).to_dict() del data["seed"] - assert Parachute.from_dict(data).to_dict()["seed"] is None + restored = Parachute.from_dict(data) + assert restored.to_dict()["seed"] is None + assert any(sample != 0.0 for sample in _noise_sequence(restored)) + + +def test_seed_is_not_a_constructor_argument(): + with pytest.raises(TypeError): + Parachute( # pylint: disable=unexpected-keyword-arg + name="main", + cd_s=10.0, + trigger="apogee", + sampling_rate=100, + seed=1, + ) + + +def test_set_noise_seed_redraws_the_initial_noise_sample(): + """The sample taken at construction came from an unseeded generator.""" + seeded = _parachute(5) + assert seeded.noise_signal[0][1] == _parachute(5).noise_signal[0][1] + assert len(seeded.noise_signal) == 1 def test_stochastic_parachute_threads_seed_into_created_object(): @@ -98,3 +121,53 @@ def test_stochastic_parachute_threads_seed_into_created_object(): other = stochastic.create_object() assert other._seed != first._seed assert _noise_sequence(other) != _noise_sequence(first) + + +# Pinned from a StochasticParachute seeded with 123, so a change to how the +# noise seed is derived or applied moves them. Same seed, same sequence. +_PINNED = { + "main": ( + 300800915345632928554392709592382146193, + [ + 1.5998005090773104, + 6.806974589196937, + -5.734488479408331, + -12.120858982750535, + -1.2021272309494897, + 2.4112665201271457, + -2.4247493851075026, + ], + ), + "drogue": ( + 199391130064412209597234822494885989553, + [ + -0.9058837789355014, + -8.190546150429622, + -13.014914070842513, + -11.083827502811186, + -5.026198797508498, + 0.8202720202675797, + -7.073751290001994, + ], + ), +} + + +@pytest.mark.parametrize("name", ["main", "drogue"]) +def test_stochastic_parachute_noise_sequence_is_pinned(name): + stochastic = StochasticParachute( + Parachute( # pylint: disable=unexpected-keyword-arg + name=name, + cd_s=10.0, + trigger="apogee", + sampling_rate=100, + noise=(0, 8.3, 0.5), + ) + ) + stochastic._set_stochastic(seed=123) + + built = stochastic.create_object() + + seed, sequence = _PINNED[name] + assert built._seed == seed + assert _noise_sequence(built, n=6) == pytest.approx(sequence, rel=1e-12) From ffd4a2e2599dd2268f421b4b4e3c0568692a35b6 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:08:35 -0300 Subject: [PATCH 2/6] BUG: cut the parachute noise seed to 63 bits so pandas reads the inputs Each Monte Carlo input row records every parachute's noise seed, and it was 128 bits, so pandas.read_json failed with "Value is too big!", the same failure #1213 fixed for the study seed. The test now runs a real study and reads its input file with pandas, which covers both seeds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574yAS8aQWohX238Bwc --- rocketpy/stochastic/stochastic_parachute.py | 4 ++- .../unit/rocket/test_parachute_noise_seed.py | 32 +++++++++---------- .../simulation/test_monte_carlo_seeding.py | 15 ++++----- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/rocketpy/stochastic/stochastic_parachute.py b/rocketpy/stochastic/stochastic_parachute.py index 1e933d7eb..1b6984496 100644 --- a/rocketpy/stochastic/stochastic_parachute.py +++ b/rocketpy/stochastic/stochastic_parachute.py @@ -209,9 +209,11 @@ def create_object(self): # share one noise stream. noise_seed = None if self._seed is not None: + # Cut to 63 bits: the seed is recorded in the inputs, and pandas + # reads JSON integers as int64. noise_seed = _sampler_seed( self._seed, ("pressure_noise", generated_dict["name"]) - ) + ) & (2**63 - 1) generated_dict["seed"] = noise_seed # Recorded after the seed is in, or the inputs describe a parachute # with noise nobody can reproduce. diff --git a/tests/unit/rocket/test_parachute_noise_seed.py b/tests/unit/rocket/test_parachute_noise_seed.py index d57efdd98..2a79b5ddc 100644 --- a/tests/unit/rocket/test_parachute_noise_seed.py +++ b/tests/unit/rocket/test_parachute_noise_seed.py @@ -127,27 +127,27 @@ def test_stochastic_parachute_threads_seed_into_created_object(): # noise seed is derived or applied moves them. Same seed, same sequence. _PINNED = { "main": ( - 300800915345632928554392709592382146193, + 6639568465033922193, [ - 1.5998005090773104, - 6.806974589196937, - -5.734488479408331, - -12.120858982750535, - -1.2021272309494897, - 2.4112665201271457, - -2.4247493851075026, + -3.78616199388814, + -13.452710525868634, + -4.673764102705483, + -10.801711039361278, + -8.053692181461434, + -16.713116227374677, + -21.497333383014304, ], ), "drogue": ( - 199391130064412209597234822494885989553, + 6152077226054995121, [ - -0.9058837789355014, - -8.190546150429622, - -13.014914070842513, - -11.083827502811186, - -5.026198797508498, - 0.8202720202675797, - -7.073751290001994, + -1.1998384111241425, + 5.348258635026273, + 2.8774014645733943, + 4.406981770690811, + 12.553121676963602, + 7.348730317452494, + -4.59114813843395, ], ), } diff --git a/tests/unit/simulation/test_monte_carlo_seeding.py b/tests/unit/simulation/test_monte_carlo_seeding.py index 798eaaa98..f27ccc55b 100644 --- a/tests/unit/simulation/test_monte_carlo_seeding.py +++ b/tests/unit/simulation/test_monte_carlo_seeding.py @@ -96,17 +96,16 @@ def test_study_seed_rejects_negative_values(): _study_seed(-1) -def test_a_generated_study_seed_lets_pandas_read_the_logs(): - """Every row carries the seed, and pandas reads JSON integers as int64.""" +def test_pandas_reads_the_inputs_of_an_unseeded_run(models, tmp_path): + """Rows carry the study seed and each parachute's noise seed, and pandas + reads JSON integers as int64.""" pandas = pytest.importorskip("pandas") - rows = "".join( - json.dumps({"index": index, "seed": _study_seed(None)}) + "\n" - for index in range(20) - ) + analysis = _a_run(tmp_path, "study", models, count=2) - logs = pandas.read_json(io.StringIO(rows), lines=True) + logs = pandas.read_json(analysis.input_file, lines=True) - assert len(logs) == 20 + assert len(logs) == 2 + assert all("seed" in parachute for parachute in logs["parachutes"][0]) @pytest.mark.slow From e741f3e5550b731cb18755bd0a3b96f1bb1b0ed0 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:13:32 -0300 Subject: [PATCH 3/6] MNT: record a copy of the parachute draw and tidy the noise seed tests StochasticParachute records the drawn dict plus the noise seed instead of writing the seed in and deleting it again. The first noise sample now reads the same bias and deviation as the rest of the chain. The test that rejects seed= checks the message, and a dead pylint disable goes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574yAS8aQWohX238Bwc --- rocketpy/rocket/parachute.py | 14 ++++++-------- rocketpy/stochastic/stochastic_parachute.py | 8 +++----- tests/unit/rocket/test_parachute_noise_seed.py | 4 ++-- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/rocketpy/rocket/parachute.py b/rocketpy/rocket/parachute.py index 0cd2b5aaf..54142ece2 100644 --- a/rocketpy/rocket/parachute.py +++ b/rocketpy/rocket/parachute.py @@ -307,11 +307,11 @@ def __init_noise(self, noise): noise : tuple, list List in the format (mean, standard deviation, time-correlation). """ - self.__draw_initial_noise() self.noisy_pressure_signal = [] self.clean_pressure_signal = [] self.noise_bias = noise[0] self.noise_deviation = noise[1] + self.__draw_initial_noise() self.noise_corr = (noise[2], (1 - noise[2] ** 2) ** 0.5) self.clean_pressure_signal_function = Function(0) self.noisy_pressure_signal_function = Function(0) @@ -327,15 +327,13 @@ def __init_noise(self, noise): def __draw_initial_noise(self): """Starts the noise signal with one sample from the current RNG.""" - self.noise_signal = [[-1e-6, self._rng.normal(self.noise[0], self.noise[1])]] + self.noise_signal = [ + [-1e-6, self._rng.normal(self.noise_bias, self.noise_deviation)] + ] def _set_noise_seed(self, seed): - """Reseeds the pressure noise and redraws its first sample. - - Not public API: ``StochasticParachute`` and ``from_dict`` use it so a - run can reproduce its noise. The first sample is redrawn because the - constructor took it from an unseeded generator. - """ + """Reseeds the noise and redraws the unseeded first sample, before any + Flight. Used by ``StochasticParachute`` and ``from_dict``.""" self._seed = seed self._rng = np.random.default_rng(seed) self.__draw_initial_noise() diff --git a/rocketpy/stochastic/stochastic_parachute.py b/rocketpy/stochastic/stochastic_parachute.py index 1b6984496..c654e799d 100644 --- a/rocketpy/stochastic/stochastic_parachute.py +++ b/rocketpy/stochastic/stochastic_parachute.py @@ -214,11 +214,9 @@ def create_object(self): noise_seed = _sampler_seed( self._seed, ("pressure_noise", generated_dict["name"]) ) & (2**63 - 1) - generated_dict["seed"] = noise_seed - # Recorded after the seed is in, or the inputs describe a parachute - # with noise nobody can reproduce. - self._record_draw(generated_dict) - del generated_dict["seed"] + # Recorded with the seed, or the inputs describe a parachute with + # noise nobody can reproduce. Rebuild it with Parachute.from_dict. + self._record_draw({**generated_dict, "seed": noise_seed}) parachute = Parachute(**generated_dict) if noise_seed is not None: parachute._set_noise_seed(noise_seed) diff --git a/tests/unit/rocket/test_parachute_noise_seed.py b/tests/unit/rocket/test_parachute_noise_seed.py index 2a79b5ddc..a05339e23 100644 --- a/tests/unit/rocket/test_parachute_noise_seed.py +++ b/tests/unit/rocket/test_parachute_noise_seed.py @@ -89,7 +89,7 @@ def test_from_dict_defaults_seed_to_none_when_absent(): def test_seed_is_not_a_constructor_argument(): - with pytest.raises(TypeError): + with pytest.raises(TypeError, match="seed"): Parachute( # pylint: disable=unexpected-keyword-arg name="main", cd_s=10.0, @@ -156,7 +156,7 @@ def test_stochastic_parachute_threads_seed_into_created_object(): @pytest.mark.parametrize("name", ["main", "drogue"]) def test_stochastic_parachute_noise_sequence_is_pinned(name): stochastic = StochasticParachute( - Parachute( # pylint: disable=unexpected-keyword-arg + Parachute( name=name, cd_s=10.0, trigger="apogee", From 16bf23064dacc9cbd3e09cf7c238ec7902728f42 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:18:14 -0300 Subject: [PATCH 4/6] TST: derive the expected parachute noise instead of pinning its floats The test keeps the pinned noise seeds and rebuilds the AR(1) chain from a fresh generator, so a NumPy release that changes its normal stream no longer fails it while the seeding code is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574yAS8aQWohX238Bwc --- .../unit/rocket/test_parachute_noise_seed.py | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/tests/unit/rocket/test_parachute_noise_seed.py b/tests/unit/rocket/test_parachute_noise_seed.py index a05339e23..0e051d6e8 100644 --- a/tests/unit/rocket/test_parachute_noise_seed.py +++ b/tests/unit/rocket/test_parachute_noise_seed.py @@ -123,38 +123,25 @@ def test_stochastic_parachute_threads_seed_into_created_object(): assert _noise_sequence(other) != _noise_sequence(first) -# Pinned from a StochasticParachute seeded with 123, so a change to how the -# noise seed is derived or applied moves them. Same seed, same sequence. -_PINNED = { - "main": ( - 6639568465033922193, - [ - -3.78616199388814, - -13.452710525868634, - -4.673764102705483, - -10.801711039361278, - -8.053692181461434, - -16.713116227374677, - -21.497333383014304, - ], - ), - "drogue": ( - 6152077226054995121, - [ - -1.1998384111241425, - 5.348258635026273, - 2.8774014645733943, - 4.406981770690811, - 12.553121676963602, - 7.348730317452494, - -4.59114813843395, - ], - ), -} +# Noise seeds a StochasticParachute seeded with 123 derives, pinned so a change +# to the derivation shows up here. +_PINNED_SEEDS = {"main": 6639568465033922193, "drogue": 6152077226054995121} + + +def _expected_noise(seed, mean, deviation, correlation, n): + """The AR(1) chain from a fresh generator, independent of NumPy's stream.""" + rng = np.random.default_rng(seed) + samples = [rng.normal(mean, deviation)] + for _ in range(n): + samples.append( + correlation * samples[-1] + + (1 - correlation**2) ** 0.5 * rng.normal(mean, deviation) + ) + return samples @pytest.mark.parametrize("name", ["main", "drogue"]) -def test_stochastic_parachute_noise_sequence_is_pinned(name): +def test_stochastic_parachute_noise_follows_the_pinned_seed(name): stochastic = StochasticParachute( Parachute( name=name, @@ -168,6 +155,7 @@ def test_stochastic_parachute_noise_sequence_is_pinned(name): built = stochastic.create_object() - seed, sequence = _PINNED[name] - assert built._seed == seed - assert _noise_sequence(built, n=6) == pytest.approx(sequence, rel=1e-12) + assert built._seed == _PINNED_SEEDS[name] + assert _noise_sequence(built, n=6) == pytest.approx( + _expected_noise(_PINNED_SEEDS[name], 0, 8.3, 0.5, n=6), rel=1e-12 + ) From cd564eef783855429461087bdc90bb51737525d0 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:18:14 -0300 Subject: [PATCH 5/6] DOC: say how parachute pressure noise can be reproduced With the public seed gone and numpy.random.seed no longer reaching the noise, the noise docstring now says it is reproducible only through the seed a Monte Carlo run records, or from_dict on a dict that carries one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574yAS8aQWohX238Bwc --- rocketpy/rocket/parachute.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rocketpy/rocket/parachute.py b/rocketpy/rocket/parachute.py index 54142ece2..6fe3c498f 100644 --- a/rocketpy/rocket/parachute.py +++ b/rocketpy/rocket/parachute.py @@ -223,7 +223,9 @@ def __init__( List in the format (mean, standard deviation, time-correlation). The values are used to add noise to the pressure signal which is passed to the trigger function. Default value is ``(0, 0, 0)``. - Units are in Pa. + Units are in Pa. The noise is reproducible only through the seed a + ``MonteCarlo`` run records, or with ``from_dict`` on a dict that + carries one; ``numpy.random.seed`` does not affect it. radius : float, optional Length of the non-unique semi-axis (radius) of the inflated hemispheroid parachute. If not provided, it is estimated from From 9e724fc1ebfe0d63e5f7aa2532e4ba83f67644a1 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 21 Nov 2026 10:25:53 -0300 Subject: [PATCH 6/6] STY: drop the io import the seeding test no longer uses Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574yAS8aQWohX238Bwc --- tests/unit/simulation/test_monte_carlo_seeding.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/simulation/test_monte_carlo_seeding.py b/tests/unit/simulation/test_monte_carlo_seeding.py index f27ccc55b..11a2054cc 100644 --- a/tests/unit/simulation/test_monte_carlo_seeding.py +++ b/tests/unit/simulation/test_monte_carlo_seeding.py @@ -1,4 +1,3 @@ -import io import json import os from time import time