Skip to content

Add optional Gaussian pulse lifetime and a posx sampler - #163

Merged
Sosnowsky merged 8 commits into
mainfrom
pulse_lifetime
Sep 16, 2026
Merged

Sosnowsky merged 8 commits into
mainfrom
pulse_lifetime

Conversation

@Sosnowsky

Copy link
Copy Markdown
Member

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.

`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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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_lifetime support to blobs and the default factory.
  • Adds a configurable posx sampler 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 == stop when the lifetime and crossing windows do not overlap, but _sum_up_blobs still reduces the resulting empty array with np.max when labels is "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 == 0 blob with a large amplitude, samples just outside this window can still contribute well above truncation_error, so speed_up can 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.

Comment thread blobmodel/blobs.py Resolved
Sosnowsky and others added 2 commits September 16, 2026 13:50
…exact reference test

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 == 0 with t_init outside the sampled time range). _sum_up_blobs then still calls np.max(_single_blob, axis=(0, 1)) for labels="same" or labels="individual"; the empty time axis raises instead of producing an all-zero realization. Handle start == stop before 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

Comment thread blobmodel/model.py Resolved
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 at t_init=50, so the crossing and lifetime windows overlap. The start <= stop assertion would also pass if the crossing window were never intersected. Place the blob far enough inside the past (for example pos_x0=30) and assert start == stop so this new truncation behavior is actually covered.
  • Files reviewed: 12/13 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread blobmodel/model.py Resolved
…amplitude

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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_blob sums the center plus the +Ly and -Ly ghost 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 exceeds truncation_error. Include a geometry-aware ghost-copy factor in the lifetime bound (or otherwise bound the periodic sum), and add a periodic-y regression for speed_up versus 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_gain bound 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

Comment thread blobmodel/model.py Resolved
Comment thread tests/test_lifetime.py Resolved
Sosnowsky and others added 2 commits September 16, 2026 14:41
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…n inf lifetime under speed_up

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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 as exp, double_exp, and rect, whose peak approaches/reaches 1 rather than 1/sqrt(pi). With no temporal gain (for example, a long lifetime and infinite drain), the margin is short by width_x*log(sqrt(pi))/|v_x*dt|, so speed_up can 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 Geometry supports an arbitrary x0; 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.x0 and geometry.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 -pad to geometry.Lx + pad instead of around the geometry's absolute x-domain, so the literal-included example does not demonstrate homogeneous seeding for offset domains. Use geometry.x0 - pad and geometry.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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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 AbstractBlobShape is normalized to a maximum magnitude of 1, but Model accepts custom implementations of that public interface (see docs/blob_shapes.rst:12) without such a contract. A valid shape such as 2 * exp(-theta**2) will be truncated when the envelope reaches truncation_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_gain returns 0 for t_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

@Sosnowsky
Sosnowsky merged commit d9ffebb into main Sep 16, 2026
5 checks passed
Sosnowsky pushed a commit that referenced this pull request Sep 25, 2026
The note that the 2.0.0 bump "has not been done yet" was stale: 2.0.0 was
bumped in 6bdf76a and 2.1.0 landed with PR #163.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

Finite pulse lifetime: optional Gaussian temporal envelope on Blob

2 participants