Skip to content

Commit 9d4726d

Browse files
authored
DOC: tighten the comments that came with the sampler seed groups (#1154)
The prose added in #1102 restates itself across three places and narrates how each rule was arrived at. Keep the caller-visible rules, drop the retelling, and let a shared group's contract live on seed_group rather than being repeated at each call site. Comments only, no change to executable code. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 6a6910d commit 9d4726d

2 files changed

Lines changed: 19 additions & 40 deletions

File tree

‎rocketpy/stochastic/custom_sampler.py‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,18 @@ class CustomSampler(ABC):
1010

1111
@property
1212
def seed_group(self):
13-
"""The generator state this sampler shares, if it shares one.
13+
"""The generator this sampler shares, or ``self`` if it shares none.
1414
15-
Samplers are independent by default and each is seeded on its own. Two
16-
wrappers over one generator, as the correlated wind pair in the
17-
documentation are, should both return that generator here, so the pair
18-
is seeded once as a unit rather than one of them silently overwriting
19-
the other's seed.
20-
21-
Return the same object on every call. Building the answer each time,
22-
which a property invites, gives each member a different identity and
23-
puts it back in a group of its own.
24-
25-
A group belongs to one model. Declaring the same generator on two
26-
models has them both seed it, and whichever is seeded last decides the
27-
stream, which is the overwrite this is here to avoid.
15+
Samplers sharing a generator must all return it, so the group is seeded
16+
once rather than each member overwriting the previous seed. Return the
17+
same object every call: a rebuilt one has a new identity and forms a
18+
group of its own. A group belongs to one model; declared on two models,
19+
each seeds it and the last one wins.
2820
2921
Returns
3022
-------
3123
object
32-
Identity is what counts, not equality. ``self`` by default, which
33-
makes every sampler its own group.
24+
Matched by identity, not equality. Defaults to ``self``.
3425
"""
3526
return self
3627

‎rocketpy/stochastic/stochastic_model.py‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -585,39 +585,30 @@ def _validate_positive_int_list(self, input_name, input_value):
585585
)
586586

587587
def _reset_custom_samplers(self, seed):
588-
"""Give each sampler its own stream, and each shared group one between
589-
them.
588+
"""Seed each sampler, and each shared generator once.
590589
591-
Samplers that share a generator, as the documented wind pair do, are
592-
seeded once as a unit. Resetting each member in turn would leave every
593-
seed but the last discarded and the group's stream decided by whichever
594-
member happened to go last.
595-
596-
Its own pass rather than the validation loop below, whose order sets
597-
``__dict__`` and so the order every other input is drawn in.
590+
Kept out of the validation loop in ``_set_stochastic``, whose order
591+
sets ``__dict__`` and with it the order every other input is drawn in.
598592
"""
599593
groups = {}
600594
for input_name in sorted(self.__stochastic_dict):
601595
sampler = self.__stochastic_dict[input_name]
602596
if isinstance(sampler, CustomSampler):
603-
# Held in the value as well as keyed on, because `id` is
604-
# unique only among live objects. Defensive: a `seed_group`
605-
# that builds its answer did not merge in practice here.
597+
# Kept in the value too: `id` is unique only among live
598+
# objects, so the group has to outlive the dict.
606599
group = sampler.seed_group
607600
shared = groups.setdefault(id(group), ([], sampler, group))
608601
shared[0].append(input_name)
609602

610603
for names, sampler, group in groups.values():
611-
# The group itself when it can be reset, since it is the thing that
612-
# holds the shared state. Going through one member instead assumes
613-
# every member resets the same way and keeps nothing of its own.
604+
# The group holds the shared state, so reset it directly; a member
605+
# may reset differently, or keep state of its own.
614606
resetter = group if hasattr(group, "reset_seed") else sampler
615607
try:
616608
resetter.reset_seed(_sampler_seed(seed, names))
617609
except Exception as error:
618-
# Not just RuntimeError. The seed handed over is now 128 bits,
619-
# which the legacy RandomState refuses with a ValueError, and a
620-
# bare one of those does not say which sampler raised it.
610+
# Broad: the seed is 128 bits, which legacy RandomState refuses
611+
# with a ValueError that does not name the sampler.
621612
raise RuntimeError(
622613
f"An error occurred in the 'reset_seed' method of the "
623614
f"CustomSampler for {', '.join(names)}"
@@ -627,9 +618,7 @@ def _validate_custom_sampler(self, input_name, sampler):
627618
"""
628619
Validate a custom sampler.
629620
630-
Seeding is not done here. It happens in ``_reset_custom_samplers``,
631-
which runs in a fixed order because two samplers can share one
632-
generator and whichever is reset last decides the stream.
621+
Seeding happens in ``_reset_custom_samplers``, not here.
633622
634623
Parameters
635624
----------
@@ -643,9 +632,8 @@ def _validate_custom_sampler(self, input_name, sampler):
643632
AssertionError
644633
If the input is not in a valid format.
645634
"""
646-
# Raised rather than asserted, the same way #1103 handles it: `python -O`
647-
# strips an assert, and the documented AssertionError is kept so callers
648-
# that already catch it still do.
635+
# Raised, not asserted: `python -O` strips asserts. AssertionError is
636+
# kept so callers that catch it still do. Same as #1103.
649637
if not isinstance(sampler, CustomSampler):
650638
raise AssertionError(
651639
f"`{input_name}` must be a CustomSampler, not {type(sampler).__name__}"

0 commit comments

Comments
 (0)