Skip to content

fix(Renewal): unify the constructors and give seeding a real slot - #347

Merged
seabbs merged 3 commits into
mainfrom
269-renewal
Sep 2, 2026
Merged

seabbs merged 3 commits into
mainfrom
269-renewal

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #269
Closes #263

Also fixes Renewal's half of #344; #346 covers the other six types.

Breaking: the drawn coupling operator is renamed

A Gravity, or any AbstractMixingModel drawn through mixing, now names its
parameters mixing.α, mixing.β, mixing.γ. Through the positional
constructor they were core.mixing.α and so on. Anyone indexing
chain[@varname(core.mixing.α)] gets a KeyError after this lands.

The two constructors disagreed on this before the PR. The keyword form stored
the bare force-of-infection core and gave mixing.α; the positional form
wrapped that core in a RenewalStep, whose seam prefixed it, and gave
core.mixing.α. Unifying the constructors forces a choice between them, and
leaving it implicit would have made the name depend on whether the renewal
happens to carry modifiers. RenewalStep's seam now composes its core flat, so
the name is mixing.* in every case — either constructor, modifiers or none.
docs/src/tutorials/patch-models.md and the MixingStep docstring are updated
to match.

#269 — a continuous generation time can carry a modifier

The keyword constructor discretised generation_time but took no modifiers; the
positional one took modifiers but required a discretised pmf and accepted no
D_gen/Δd. Both now take any generation time, the discretisation keywords and
modifiers, and the positional form delegates to the keyword one, so there is one
construction path and one discretisation of the interval:

Renewal(Gamma(2, 1.5), SusceptibleDepletion(1000.0);
    D_gen = 15.0, rt = RandomWalk())
Renewal(; generation_time = Gamma(2, 1.5), D_gen = 15.0,
    modifiers = SusceptibleDepletion(1000.0), rt = RandomWalk())

Renewal gains a modifiers field. It is needed because an inferred generation
interval bakes no step at construction, so the step is rebuilt per draw and the
modifiers have to be reachable there. A modifier now composes onto an inferred
interval as well as a fixed one. The keyword form has no signature to constrain
what it is given, so it checks its argument and names the type it wants.

Every existing call form still works; call sites in src/, test/, docs/ and
benchmark/ were checked.

#263 — initialisation says what it is, and a process gets a real slot

Neither option in the issue works exactly as written, because rejecting a
process at construction is not implementable. A process in that slot is
legitimate and in use: initialisation = IID(Normal(log(20), 0.3)) gives one
seed per stratum for a Dims{2} model (test/unit/infection_models/strata_renewal.jl,
test/unit/inference/inference.jl). At construction there is no way to tell a
level-process from a seeding-path process, since the shape is only known when
as_turing_model is called. Rejecting processes would break working stratified
models. The reframing is written up on the issue itself.

So this takes option (a), scoped:

  • initialisation is documented as what it is: the LEVEL at t₀, one value or
    one per stratum. A draw of any other length now raises an error naming the
    alternative instead of failing inside only.

  • A new SeedingPath(process) wrapper changes what the slot means. The process
    is drawn at the seeding window's own shape — len_gen_int for a single
    series, n_strata × len_gen_int for a stratified renewal, so
    SeedingPath(Stratify(...)) works through the existing shape contract —
    transformed, and used as the initial incidence window directly. Nothing is
    decayed and no growth rate is implied, so the data inform the run-up:

    Renewal(; generation_time = gen_int, rt = RandomWalk(),
        initialisation = SeedingPath(RandomWalk(; init = Normal(log(50), 0.5))))
  • Observability: the renewal returns (; I_t, Z_t, I_seed) and IDModel passes
    I_seed on when the infection model exposes one. It is the window the scan
    started from, whether drawn or decayed, so the deterministic seeding window is
    now inspectable too. It is not part of I_t, which keeps its length.

  • SeedingPath outside a renewal initialisation slot errors rather than
    silently behaving as the process it wraps.

On the name: the wrappers in src/latent_models/modifiers/ are
<Verb>LatentModel-shaped because they wrap a latent and stay a latent. This is
not that — it marks how one slot is read and is a component of no role — so a
noun for the thing it produces reads better at the call site. Seeding is the
obvious alternative if you prefer it; say so and I will rename.

#344 — Renewal's PATH slot is widened on every construction path

path_prior was applied in the outer keyword constructor only, so Julia's
generated positional constructor bypassed it and a bare Distribution in rt
was never widened to an Intercept. This PR takes Renewal to seven fields,
which lengthens and reorders that positional signature, so the fix belongs here
rather than in a PR racing this one. path_prior moves into an inner
constructor, following the pattern #346 uses for the other six types. It is
idempotent, so no ConstructionBase.constructorof is needed, and although the
inner signature leaves rt unbounded, the widening runs before new, so the
PriorLike guard still fires.

Other notes

  • A modifier-free renewal keeps the bare force-of-infection core as
    recurrent_step from both constructors; the keyword form already did this,
    the positional form wrapped it in a RenewalStep with an empty modifier
    tuple.
  • src/base/roles.jl and src/base/interfaces.jl stated the infection contract
    as (; I_t, Z_t), which was already stale. They now say at least those
    two, and that a model may return more and consumers read by name, so the next
    field added to that tuple does not make them wrong again.
  • An infection model with no seeding window returns exactly the tuple it always
    did through IDModel.

Tests

New @testitems in test/unit/infection_models/renewal_step.jl (the call #269
reported as inexpressible, as a regression test; a modifier on an inferred
generation interval; the modifiers keyword's input check; and the rt
widening on both construction paths) and a new
test/unit/infection_models/seeding_path.jl covering the decayed window, a
drawn window used as-is, the first scanned step, a stratified seeding path, a
seeding path sized to an inferred interval, composition with a modifier,
exposure through IDModel, and both error paths.

Renewal, infection, inference, compose and mixing test items pass locally, as do
base and utils. Runic --check exits 0, Test.detect_ambiguities on the module
is clean with the inner constructor in place, and a ForwardDiff gradient through
a seeded renewal is finite.

Not done

I_seed is exposed but no AD fixture was added for a seeded renewal, to keep the
AD suite's runtime where it is. Say if you want one.

This was opened by a bot. Please ping @seabbs for any questions.

`Renewal` had two constructors that could not be combined. The keyword one
discretised a continuous generation time but took no modifiers; the positional
one took modifiers but required an already discretised pmf and accepted no
`D_gen`/`Δd`. Both forms now take any generation time, the discretisation
keywords, and modifiers, with the positional form delegating to the keyword one
so there is a single construction path. `Renewal` gains a `modifiers` field, so
a modifier also composes onto an inferred generation interval, where no step is
baked at construction.

`initialisation` is a `PriorLike` slot but is consumed as a single level: a
process handed to it is drawn at length 1 and collapsed by `only`, and the
resulting seeding window is deterministic geometric decay that no output
exposes. The slot is now documented as the level at t₀, and a `SeedingPath`
wrapper estimates the whole seeding window instead: the process is drawn at the
window shape and used as the initial incidence window directly. The window is
returned as `I_seed` by the renewal and by `IDModel`, drawn or decayed, so the
seeding phase can be inspected.

A modifier-free renewal keeps the bare force-of-infection core as its step from
both constructors, as the keyword form already did.

Closes #269
Closes #263

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner September 2, 2026 13:05
@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

@seabbs-review-bot

@seabbs-review-bot seabbs-review-bot Bot 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.

The PR unifies Renewal's keyword and positional constructors onto one construction path, adds a modifiers keyword, and introduces SeedingPath so the renewal's pre-window incidence can be estimated as a process instead of decayed from a single level, threading I_seed through to generated quantities. The dispatch-based design (RenewalStep vs bare core, level vs SeedingPath, fixed vs inferred generation interval) follows the ecosystem's conventions and is well tested for the main paths; the only real defect is a stray issue-number reference in a test comment, plus two minor gaps (an untested SeedingPath/inferred-generation-interval combination and unvalidated modifiers keyword input).

Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: re-review requested by seabbs-bot. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.

@testitem "Renewal combines a continuous generation time with a modifier" begin
using ComposableTuringIDModels, Distributions
using ComposableTuringIDModels: RenewalStep
# The call issue #269 reported as inexpressible: a continuous generation

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue This comment cites "issue #269", which breaches the project standard that comments and docs carry no development history or issue/PR numbers (git blame and the PR already hold that). Reword to state the current behaviour only, e.g. "# A continuous generation time discretised by the constructor, carrying a modifier."

# per stratum — or a whole seeding path, drawn at the incidence window's own
# shape. Both branches are decided by the slot's type, so nothing is chosen
# at run time.
if infection.initialisation isa SeedingPath

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion No test combines a SeedingPath initialisation with an inferred (AbstractPriorModel) generation_time, even though this branch explicitly supports it (the seeding shape is computed from the per-draw, lag-dropped gen_int). Add a @testitem combining the two (mirroring "Renewal composes a modifier onto an inferred generation interval") so the interaction is locked in.

Comment thread src/infection_models/Renewal.jl Outdated
# The modifier slot takes one modifier or a collection of them; a tuple keeps
# the step's modifier types concrete.
_modifier_tuple(mod::AbstractRenewalModifier) = (mod,)
_modifier_tuple(mods) = Tuple(mods)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion _modifier_tuple(mods) = Tuple(mods) accepts any iterable for the new modifiers keyword with no element-type check, unlike the positional constructor which is statically constrained to AbstractRenewalModifier.... A bad value (e.g. modifiers = [SomeUnrelatedObject()]) builds a Renewal fine and only fails later as an opaque MethodError at sample time. Validate at entry, e.g. _modifier_tuple(mods) = (foreach(m -> m isa AbstractRenewalModifier || throw(ArgumentError("modifiers must be AbstractRenewalModifier")), mods); Tuple(mods)).

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Sep 2, 2026
…erred interval

The keyword form has no signature constraining what `modifiers` holds, so it
checks its argument and names the type it wants rather than failing later as a
`MethodError` inside the step's seam.

Adds a test for a `SeedingPath` with an inferred generation interval, where the
window length comes from the interval drawn per iteration, and drops an issue
number from a test comment.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

Thanks — all three findings acted on in a50a403.

  • Issue number in a test comment: fixed. Reworded to state the behaviour only.
  • SeedingPath with an inferred generation interval untested: added "a SeedingPath sizes itself to an inferred generation interval" in test/unit/infection_models/seeding_path.jl. It checks the window length against a fixed Renewal built over the same horizon, so it pins the equivalence rather than a magic number.
  • modifiers keyword unvalidated: added a check in _modifier_tuple, which names the type it wants and lists what it was given, plus a test that a single modifier, a tuple and a vector are all still accepted.

Renewal and seeding test items: 92 passed, 0 failed. Runic --check exits 0.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableTuringIDModels.jl", rev="269-renewal")
using ComposableTuringIDModels

Option 2: Local Checkout

If you have the repo locally:

git checkout 269-renewal
julia --project=. -e "using Pkg; Pkg.instantiate()"

`path_prior` ran in the outer keyword constructor only, so Julia's generated
positional constructor bypassed it and a bare `Distribution` in the `rt` PATH
slot was never widened to an `Intercept`. Adding the `modifiers` field lengthens
and reorders that positional signature, so the widening moves into an inner
constructor, as `TransformLatentModel` and the other widened components do.
`path_prior` is idempotent, so rebuilding from stored fields is a no-op and no
`ConstructionBase.constructorof` is needed.

A drawn coupling operator is now named `mixing.*` whichever constructor built
the renewal and whether or not the step carries modifiers: `RenewalStep`'s seam
composes its core flat rather than prefixing it. This renames `core.mixing.*`,
which the positional constructor used to give. The tutorial and the `MixingStep`
docstring are updated.

The infection role contract said the model returns `(; I_t, Z_t)`. It now says
at least those two, with consumers reading by name, so a model returning more
(a `Renewal` returns `I_seed`) does not make it stale.

Refs #344, whose other six types are fixed in #346.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-reviewed Reviewed by seabbs-review-bot

Projects

None yet

2 participants