Repository navigation
Add optional Gaussian pulse lifetime and a posx sampler - #163
Conversation
`Blob` gains `t_lifetime`: an optional Gaussian envelope `exp(-((t - t_init)/t_lifetime)^2)`, symmetric about `t_init`, applied on top of and independently of the one-sided `t_drain` decay. `None` (the default) multiplies by the float 1.0, which is exact in IEEE754, so existing realizations are bit-for-bit unchanged. `DefaultBlobFactory` forwards `t_lifetime` to every sampled blob and gains a `posx` sampler, defaulting to `DistributionEnum.zeros` — the previously hardcoded `pos_x0 = 0`. `zeros` draws nothing, so the RNG stream is untouched. `sample_blobs` keeps its signature: the seeding domain comes from a caller-supplied callable, not from new `Lx` plumbing, so existing custom factories are unaffected. `Model._compute_start_stop` now intersects the domain-crossing window with the lifetime support, and truncates `v_x == 0` blobs that previously returned the full time range. Without this the regime the envelope is meant for (many short-lived blobs on a long domain) is unaffordable. Both new arguments are last in their signatures, so positional callers are unaffected. Closes #162. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical numerical overflow and two moderate truncation issues block approval.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds optional Gaussian pulse lifetimes, configurable x-position sampling, lifetime-aware truncation, documentation, tests, and a version bump.
Changes:
- Adds
t_lifetimesupport to blobs and the default factory. - Adds a configurable
posxsampler while preserving default RNG behavior. - Updates truncation logic and project documentation.
File summaries
| File | Summary |
|---|---|
tests/test_lifetime.py |
Tests lifetime, sampling, truncation, and regressions. |
tests/test_docs.py |
Tests the lifetime documentation example. |
tests/test_docs_scripts.py |
Smoke-tests the plotting script. |
README.md |
Documents the lifetime feature. |
pyproject.toml |
Bumps the version to 2.1.0. |
docs/pulse_lifetime.rst |
Documents lifetime behavior and seeding. |
docs/pulse_lifetime_plot.py |
Adds lifetime visualization. |
docs/index.rst |
Includes the new documentation page. |
CLAUDE.md |
Records lifetime and seeding conventions. |
blobmodel/stochasticality.py |
Adds lifetime forwarding and posx sampling. |
blobmodel/model.py |
Adds lifetime-aware truncation; moderate issues remain for empty-window reductions and amplitude-unaware cutoff bounds. |
blobmodel/blobs.py |
Implements the lifetime envelope; a critical numerical issue remains where separate factors can produce inf * 0 = NaN. |
Review details
Suppressed comments (2)
blobmodel/model.py:523
- This return deliberately allows
start == stopwhen the lifetime and crossing windows do not overlap, but_sum_up_blobsstill reduces the resulting empty array withnp.maxwhenlabelsis"same"or"individual". A finite-lifetime blob outside the observation window therefore raises instead of producing an all-zero field for those public label modes. Skip empty windows before the label reduction (or otherwise handle the empty slice).
# The two windows may not overlap at all: collapse rather than invert.
return start, max(start, stop)
blobmodel/model.py:496
- The lifetime cutoff is computed from only the envelope/drain product, without accounting for the blob amplitude (or the peak of the spatial shape). For a
v_x == 0blob with a large amplitude, samples just outside this window can still contribute well abovetruncation_error, sospeed_upcan change the realization by more than its configured tolerance. Scale the cutoff using a conservative bound on the blob's maximum field contribution (and add a high-amplitude regression) before using this window for truncation.
half_width = self._lifetime_half_width(blob, truncation_error)
- Files reviewed: 12/13 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…exact reference test Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical and moderate model issues remain, along with a formatting nit.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (2)
blobmodel/model.py:523
- This can now return an empty time window for a valid finite-lifetime blob (for example,
v_x == 0witht_initoutside the sampled time range)._sum_up_blobsthen still callsnp.max(_single_blob, axis=(0, 1))forlabels="same"orlabels="individual"; the empty time axis raises instead of producing an all-zero realization. Handlestart == stopbefore the label reductions (or otherwise skip empty windows).
return start, max(start, stop)
tests/test_lifetime.py:241
- This new test statement is 89 characters long, so it is over the repository's default Black line length (CI runs
black --check .) and will make the formatting check fail. Please wrap the call as Black formats it.
both = _realize(_blob(t_drain=t_drain, t_lifetime=tau_d), geometry, speed_up=False)
- Files reviewed: 12/13 changed files
- Comments generated: 1
- Review effort level: Lite
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The critical model truncation issue remains unresolved.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
tests/test_lifetime.py:331
- This fixture does not exercise a non-overlapping intersection: with
pos_x0=0, the blob is at the inflow edge exactly att_init=50, so the crossing and lifetime windows overlap. Thestart <= stopassertion would also pass if the crossing window were never intersected. Place the blob far enough inside the past (for examplepos_x0=30) and assertstart == stopso this new truncation behavior is actually covered.
- Files reviewed: 12/13 changed files
- Comments generated: 1
- Review effort level: Lite
…amplitude Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The review identifies a lifetime handling bug and a failing array assertion.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (2)
blobmodel/model.py:581
- This temporal bound is not conservative for periodic-y realizations:
Blob.discretize_blobsums the center plus the+Lyand-Lyghost copies, so the field can be up to three shape contributions even though each individual shape is bounded by 1. A finite lifetime can therefore exclude time samples where the summed periodic contribution still exceedstruncation_error. Include a geometry-aware ghost-copy factor in the lifetime bound (or otherwise bound the periodic sum), and add a periodic-y regression forspeed_upversus the full realization.
`truncation_error` bounds the field itself, so ``epsilon`` is
``truncation_error / |amplitude|``: every blob shape peaks at or below
1 in magnitude, hence ``|amplitude| * envelope * drain`` bounds the
blob's contribution.
blobmodel/model.py:538
- The crossing-window gain bound also assumes one spatial shape, but periodic-y discretization adds the center and two ghost copies. Their sum can exceed the
|amplitude| * temporal_gainbound used here, so the resulting margin can still discard periodic contributions above the threshold. Apply the same conservative ghost-copy bound to this margin, not only to the lifetime half-width.
The crossing margin assumes the blob's contribution is at most its
spatial shape. With a finite `t_lifetime` the temporal factor
``exp(-dt / t_drain - (dt / tau_d)**2)`` peaks at
``exp(tau_d**2 / (4 t_drain**2))`` (before `t_init`), and the amplitude
scales the field too, so the log of ``|amplitude|`` times that peak
is added to the margin's log-threshold. Clamped at 0 so the margin
- Files reviewed: 12/13 changed files
- Comments generated: 2
- Review effort level: Lite
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…n inf lifetime under speed_up Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🔵 Needs a closer look
The moderate truncation-margin issue remains unresolved; two documentation nits also need correction.
Review details
Suppressed comments (3)
blobmodel/model.py:522
- This still hard-codes the Gaussian peak normalization (
sqrt(pi)) in the crossing margin, but the new lifetime tests and the public API also allow shapes such asexp,double_exp, andrect, whose peak approaches/reaches 1 rather than1/sqrt(pi). With no temporal gain (for example, a long lifetime and infinite drain), the margin is short bywidth_x*log(sqrt(pi))/|v_x*dt|, sospeed_upcan discard non-negligible spatial tails. Base this threshold on the selected shape's peak, or use the conservative unit peak instead of a Gaussian-specific constant.
- np.log(truncation_error * np.sqrt(np.pi))
docs/pulse_lifetime.rst:38
- This example assumes the x-domain starts at zero, but
Geometrysupports an arbitraryx0; for a domain such as[20, 30), it seeds[-pad, 10 + pad)and does not cover the observed domain. Use the geometry's absolute bounds (geometry.x0andgeometry.x0 + geometry.Lx) in the sampler example.
blob_factory.set_sampler("posx", lambda rng, n: rng.uniform(-pad, geometry.Lx + pad, n))
tests/test_docs.py:52
- This docs example is also wrong for a nonzero
Geometry.x0: it samples from-padtogeometry.Lx + padinstead of around the geometry's absolute x-domain, so the literal-included example does not demonstrate homogeneous seeding for offset domains. Usegeometry.x0 - padandgeometry.x0 + geometry.Lx + pad.
"posx", lambda rng, n: rng.uniform(-pad, geometry.Lx + pad, n)
- Files reviewed: 12/13 changed files
- Comments generated: 0 new
- Review effort level: Lite
…ometry.x0 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Two moderate issues remain in blobmodel/model.py concerning custom shape bounds and preserving legacy no-lifetime truncation behavior.
Review details
Suppressed comments (2)
blobmodel/model.py:496
- This new lifetime bound assumes every
AbstractBlobShapeis normalized to a maximum magnitude of 1, butModelaccepts custom implementations of that public interface (seedocs/blob_shapes.rst:12) without such a contract. A valid shape such as2 * exp(-theta**2)will be truncated when the envelope reachestruncation_error, even though its field is still above the threshold. Derive a conservative bound from the configured shape (including both axes) or explicitly validate/document a unit-peak requirement before using this window.
# Bound on |blob contribution| / (temporal factor): every shape peaks
# at or below 1, and periodic-y discretization sums three copies.
copies = 3 if self._geometry.periodic_y and not self._one_dimensional else 1
peak = copies * abs(blob.amplitude)
blobmodel/model.py:521
- The no-lifetime path no longer preserves the promised bit-for-bit behavior when
speed_up=True:_log_temporal_gainreturns 0 fort_lifetime=None, but this margin uses-log(truncation_error)instead of the previous-log(truncation_error * sqrt(pi)). For the default Gaussian shape this widens the slice and retains nonzero tail samples that the old implementation discarded, so default realizations can change despite the unchanged temporal factor. Retain the legacy Gaussian margin for the no-lifetime case or derive a shape-specific peak while preserving that value.
margin = (
width_x
* (self._log_temporal_gain(blob, peak) - np.log(truncation_error))
/ np.abs(blob.v_x * dt)
- Files reviewed: 13/14 changed files
- Comments generated: 0 new
- Review effort level: Lite
Blobgainst_lifetime: an optional Gaussian envelopeexp(-((t - t_init)/t_lifetime)^2), symmetric aboutt_init, applied on top of and independently of the one-sidedt_draindecay.None(the default) multiplies by the float 1.0, which is exact in IEEE754, so existing realizations are bit-for-bit unchanged.DefaultBlobFactoryforwardst_lifetimeto every sampled blob and gains aposxsampler, defaulting toDistributionEnum.zeros— the previously hardcodedpos_x0 = 0.zerosdraws nothing, so the RNG stream is untouched.sample_blobskeeps its signature: the seeding domain comes from a caller-supplied callable, not from newLxplumbing, so existing custom factories are unaffected.Model._compute_start_stopnow intersects the domain-crossing window with the lifetime support, and truncatesv_x == 0blobs that previously returned the full time range. Without this the regime the envelope is meant for (many short-lived blobs on a long domain) is unaffordable.Both new arguments are last in their signatures, so positional callers are unaffected. Closes #162.