Skip to content

fix(shape): let a component state which of its data's axes are streams - #363

Open
seabbs-bot wants to merge 7 commits into
mainfrom
246-shape
Open

seabbs-bot wants to merge 7 commits into
mainfrom
246-shape

Conversation

@seabbs-bot

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

Copy link
Copy Markdown
Collaborator

Closes #246
Closes #374

The description this PR carried until now was #348's, pasted here by mistake, which is why a question about ObservationError landed on this thread.
This is the shape fix and nothing else.

The fault

_obs_data_shape read the infection process's stratum count off any leading array axis or NamedTuple entry count.
That is right for a Split, whose rows or entries are observation streams.
It is wrong for a ReportTriangle, whose rows are reference days, and for a BinomialError, whose (y, N) is one stream and a trials covariate.
Both built a stratified infection process nobody asked for and failed with RandomWalk produces one path. Wrap it in a Stratify.
A ReportTriangle was unusable through IDProblem and through the two-argument as_turing_model.

The fix

observation_streams(model, y_t) is the seam a component uses to say how many observation streams a data value holds for it, or nothing for a single series.
The default reads the value's own axes.
A ReportTriangle says its matrix names no streams.
An error family counts the axis of the y field define_y_t unpacks, so (y = matrix, N = matrix) is one stream axis and (y = vector,) is none.
It is public, documented, and has IDModel and IDProblem methods beside observation_lead_in's.

_declared_strata is what a chain fixes for itself before it sees any data, a Split's weight map or stream names.
_obs_data_shape resolves the declared count first and reads the data only where the chain leaves the count open, mapping streams to strata through infection_strata in one place.
IDProblem, the two-argument as_turing_model and forecast share it, so the three build a data-driven shape the same way.

Tests

Direct tests of observation_streams on the default, a modifier, a ReportTriangle, a BinomialError, a Split and both composed types.
_obs_data_shape cases for the triangle as a matrix and as a carrier, the binomial as a single stream and as a panel, a series wrapped in (y = ...,) resolving the same shape as the bare series, and a nested Split whose map fixes the count.
A return-type check that the resolved shape is concrete, since it is built in a @model body.
Two IDProblem items building a BinomialError model and a ReportTriangle model and checking the variable names of a single-series process.

Review

Five findings from @seabbs-review-bot are fixed and their threads resolved.
One is rejected on all three passes, reading size(m.map, 2) in place of infection_strata(m, size(m.map, 1)), because the seam is where the streams-to-strata rule lives.

Merge note

#373 moves _obs_data_shape too.
This is the smaller change, so land it first and rebase #373 over it.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01Lw3PbYZfA1dZhfaaE9tbdp

@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner September 4, 2026 10:58
@seabbs-bot

This comment has been minimized.

@github-actions

github-actions Bot commented Sep 4, 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="246-shape")
using ComposableTuringIDModels

Option 2: Local Checkout

If you have the repo locally:

git checkout 246-shape
julia --project=. -e "using Pkg; Pkg.instantiate()"

seabbs-review-bot[bot]

This comment was marked as resolved.

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Sep 4, 2026
@seabbs-bot

This comment has been minimized.

seabbs-review-bot[bot]

This comment was marked as resolved.

@seabbs-bot

This comment has been minimized.

seabbs-review-bot[bot]

This comment was marked as resolved.

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/observation_models/requirements.jl 92.30% 2 Missing ⚠️
Flag Coverage Δ
ad-enzyme-forward 62.68% <0.00%> (-0.65%) ⬇️
ad-enzyme-reverse 61.48% <0.00%> (-0.63%) ⬇️
ad-forwarddiff 61.89% <0.00%> (-0.65%) ⬇️
ad-mooncake-forward 61.89% <0.00%> (-0.65%) ⬇️
ad-mooncake-reverse 61.89% <0.00%> (-0.65%) ⬇️
ad-reversediff 62.09% <0.00%> (-0.65%) ⬇️
ad-reversediff-compiled 62.09% <0.00%> (-0.65%) ⬇️
unit 94.01% <100.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/compose.jl 97.43% <100.00%> (+0.06%) ⬆️
src/inference/IDProblem.jl 94.44% <100.00%> (-1.86%) ⬇️
src/observation_models/requirements.jl 90.71% <92.30%> (+0.26%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark comparison vs base

Minimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:

Group 🟢 <50% 🟢 50–75% 🟢 75–95% ⚪ 95–105% 🔴 105–125% 🔴 125–150% 🔴 >150%
Evaluation · · · 1 6 2 ·
ForwardDiff · · 2 · · · ·
ReverseDiff (tape) · · · · 2 · ·
Mooncake reverse · · · · · 2 ·
Enzyme reverse · · · · · 1 ·
Evaluation — 9 benchmarks (by time change)
Benchmark base PR time memory
Model evaluation / DirectInfections+Poisson / forward 859.0 ns 1.11 μs 🔴 1.29× ⚪ 1.0×
Model evaluation / Renewal+NegativeBinomial / forward 3.65 μs 4.65 μs 🔴 1.27× ⚪ 1.0×
Model evaluation / RandomWalk latent / forward 347.0 ns 430.0 ns 🔴 1.24× ⚪ 1.0×
Model evaluation / AR latent / forward 454.0 ns 550.0 ns 🔴 1.21× ⚪ 1.0×
Model evaluation / Renewal+NegativeBinomial / rand 1.81 μs 2.17 μs 🔴 1.2× ⚪ 1.0×
Model evaluation / AR latent / rand 508.0 ns 601.0 ns 🔴 1.18× ⚪ 1.0×
Model evaluation / DirectInfections+Poisson / rand 543.0 ns 640.0 ns 🔴 1.18× ⚪ 1.0×
Model evaluation / RandomWalk latent / rand 379.0 ns 440.0 ns 🔴 1.16× ⚪ 1.0×
Sampling / NUTS (DirectInfections+Poisson, 50 draws) 112.54 ms 114.29 ms ⚪ 1.02× ⚪ 1.0×
AD gradients — 7 benchmarks (by time change)
Benchmark base PR time memory
AD gradients / DirectInfections+Poisson posterior / Mooncake reverse 6.4 μs 8.94 μs 🔴 1.4× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / Enzyme reverse 2.74 μs 3.76 μs 🔴 1.37× ⚪ 1.0×
AD gradients / AR latent logjoint / Mooncake reverse 4.15 μs 5.54 μs 🔴 1.33× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / ReverseDiff (tape) 80.12 μs 98.59 μs 🔴 1.23× ⚪ 1.0×
AD gradients / AR latent logjoint / ReverseDiff (tape) 60.77 μs 72.13 μs 🔴 1.19× ⚪ 1.0×
AD gradients / AR latent logjoint / ForwardDiff 9.93 μs 8.72 μs 🟢 0.88× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / ForwardDiff 16.39 μs 14.74 μs 🟢 0.9× ⚪ 1.0×

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

📖 Documentation preview is ready!

View the docs for this PR at: https://EpiAware.github.io/ComposableTuringIDModels.jl/previews/PR363/

This preview will be updated automatically when you push new commits.

seabbs-bot and others added 4 commits September 4, 2026 16:33
`_obs_data_shape` read the infection process's stratum count off the data's
array dimensions: a matrix's row count, or a NamedTuple's entry count. That is
right only where those dimensions happen to be observation streams. A
`ReportTriangle` handed a dense count matrix turned its reference days into
infection strata, and a `BinomialError` given `(y, N)` turned its trials vector
into a second stream. Both then failed under an unstratified latent model with
"RandomWalk produces one path. Wrap it in a `Stratify`", an error about a
stratification the caller never asked for.

Add `observation_streams(model, y_t)`, the seam a component uses to say how many
observation streams a data value holds for it. It follows the same
`wrapped_models` walk as `observation_lead_in` and `data_requirements`, takes its
field names from the existing data contract, and returns `nothing` for a value
with no stream axis, so a single series stays a plain length rather than a
one-row panel.

A `Split` that fixes its own stratum count now does so before any data is seen,
so a weight map is honoured through a wrapping modifier and with `y_t = missing`.
`IDModel`'s two-argument form shares the one implementation.

No sampled variable is renamed: `keys(VarInfo(...))` is unchanged across ten
composed models covering every branch.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The seam is public, so it needs a test that calls it rather than one that
reaches it through the private shape helper. Covers the composed-model
delegation, which the shape helper never exercises because it is always handed
an observation model already.

Say why the declared stratum count goes through `infection_strata` on a path
where a weight map already answers it.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`define_y_t` unpacks `y_t.y` for every error family, not only the ones whose
data contract names extra fields, so `(y = counts,)` is a supported way to
supply any of them. Reading the whole NamedTuple's arity therefore counted a
two-row panel wrapped in `(y = ...,)` as one stream, and left the same
observations building a different infection process depending on whether they
were wrapped.

Count the axis of `y` instead, which is the field the model will read.
`(y = vector,)` under an unstratified latent model now builds the model it
should rather than failing with "RandomWalk produces one path".

Give `IDModel` and `IDProblem` their own methods rather than reaching them
through an `Any` fallback, matching `observation_lead_in`.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Trim the new comments in `requirements.jl` to the facts a reader cannot
get from the code, and drop the em-dash. Split the colon-joined sentences
in the `IDModel` and `IDProblem` docstrings.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

Third pass answered on the threads, plus a quality sweep and a rebase.

Review threads. Five findings were already fixed by the earlier commits and now have replies saying where. The infection_strata(m, size(m.map, 1)) finding, raised on all three passes, is rejected on the code. src/observation_models/Split.jl:10 documents map as an obs_strata × inf_strata matrix and line 22 says the stream count is size(map, 1), so the argument passed is the value the parameter is named for, not filler, and inlining size(m.map, 2) would put the streams-to-strata mapping in a second place. The fair half of the finding was that the comment did not say rows are streams. It now does.

Quality sweep. Comment narration cut in src/observation_models/requirements.jl, one em-dash removed, and the colon-joined sentences in the IDModel and IDProblem docstrings split. The commit touches no executable line. One comment was added, recording that _shape_from_strata is split across two methods so _obs_data_shape stays concrete in a @model body rather than returning a union.

CI. ad / Enzyme reverse and ad / ReverseDiff (tape) fail with UndefVarError: SS_Intnot defined inAMD``, an upstream precompile break in AMD.jl/`LinearSolve` on Julia 1.12. It is not from this branch. `main` at 658bf8f, a docs-only commit, fails all six AD backends the same way. Tracked in #379. Everything else passed on the previous head, and the rebase onto 658bf8f is re-running now.

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

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@seabbs

seabbs commented Sep 4, 2026

Copy link
Copy Markdown
Member

Surely we don't need to replace reparameterised distributiosn here with wrapper functioanlt and why can't neg bin use observation erro? I thought it was just going to help with al the placeholder stuff obs error models use?

The rebuild-coverage test item added in #372 does
`using InteractiveUtils: subtypes`, but the stdlib was never added to
`test/Project.toml`. Julia 1.12 refuses to load a stdlib that is not in
the active environment, so every test matrix job on a branch carrying
that item errors in `reconstruction.jl`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lw3PbYZfA1dZhfaaE9tbdp
(cherry picked from commit 4d7322a)

Copy link
Copy Markdown
Collaborator Author

The question above about reparameterised distributions and NegativeBinomialError is about ObservationError, which this PR does not touch.
It is answered on #348, where that component lives.

This branch is caught up with main and carries the InteractiveUtils fix from #388, which is what the test matrix is red on repo-wide.
Every review thread is answered and the ones that changed code are resolved.
The one finding rejected on all three passes, reading size(m.map, 2) in place of infection_strata(m, size(m.map, 1)), is left as is for the reason given in the thread: the seam is where the streams-to-strata rule lives.

Merge order note: #373 moves _obs_data_shape too, so whichever of the two lands second needs a rebase over the other.
This one is the smaller change, so it is the one to land first.


Generated by Claude Code

This branch has not been deployed

No deployments
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

3 participants