Skip to content

Separate BUGSExamples into standalone package (WIP) - #453

Draft
shravanngoswamii wants to merge 18 commits into
mainfrom
Redesign-BUGSExamples
Draft

shravanngoswamii wants to merge 18 commits into
mainfrom
Redesign-BUGSExamples

Conversation

@shravanngoswamii

Copy link
Copy Markdown
Member

Closes #436 (partially — work-in-progress; see REMAINING_WORK.md in the workspace for the full tracking doc).

Summary

Splits the BUGSExamples module out of the JuliaBUGS package into a standalone subpackage at BUGSExamples/, with its own Project.toml, tests, CI, and Literate.jl + DocumenterVitepress docs site (with DoodleBUGS graph embeds where available).

  • New BUGSExamples/ package — UUID cb193daa-…-89ec8f36026b, only depends on JSON. No JuliaBUGS dependency — model code is stored as plain strings.
  • New BUGSExample struct (name, original_syntax_program, model_def, model_function, stan_code, numpyro_code, data, inits, inits_alternative, reference_results).
  • New data_loader.jl that converts data.json → typed NamedTuple (handles nested vectors → matrices).
  • Auto-discovering Literate build: every src/<example>/model.jl becomes a page.
  • DoodleBUGS web-component is injected via docs/src/.vitepress/config.mts and theme-synced via public/sync_theme.js.
  • New CI: .github/workflows/BUGSExamplesTests.yml (Julia 1.10/1.11/pre) and BUGSExamplesDocs.yml (DocumenterVitepress deploy under gh-pages/BUGSExamples/).

⚠️ Breaking change vs old in-tree examples

Example.model_def::Expr (old) → BUGSExample.model_def::String (new). Consumers should switch to @bugs(ex.original_syntax_program) instead of using model_def directly.

What's in this PR

  • Package scaffolding, types, JSON loader, docs build, both CI workflows
  • 2 examples ported: rats, pumps

What's NOT in this PR (follow-ups)

The full plan lives in REMAINING_WORK.md in the workspace. Highlights:

  • Port the remaining ~36 examples from Volumes 1–2 (and decide on 3–4).
  • Real tests (test/runtests.jl is a placeholder).
  • Remove old JuliaBUGS/src/BUGSExamples/ and migrate 6 JuliaBUGS test files that reach into JuliaBUGS.BUGSExamples.* — this is where the breaking model_def: Expr → String change lands.
  • Pin DoodleBUGS widget URL (currently hardcoded to DoodleBUGS/pr-previews/451/lib/…).
  • Decide: keep in monorepo or extract to TuringLang/BUGSExamples.jl.
  • README + landing-page polish.

Test plan

  • BUGSExamples package builds: julia --project=BUGSExamples -e 'using BUGSExamples; BUGSExamples.list()'
  • BUGSExamplesTests.yml green on Julia 1.10 / 1.11 / pre
  • BUGSExamplesDocs.yml builds the Vitepress site and the DoodleBUGS widget renders for the rats and pumps pages
  • Dark/light theme toggle syncs to the embedded widget

🤖 Generated with Claude Code

@shravanngoswamii
shravanngoswamii force-pushed the Redesign-BUGSExamples branch from 3f4416f to f76d4b0 Compare May 16, 2026 11:09
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@codecov

codecov Bot commented May 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.26%. Comparing base (991256d) to head (566dd98).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #453      +/-   ##
==========================================
+ Coverage   87.99%   88.26%   +0.27%     
==========================================
  Files          29       29              
  Lines        3732     3742      +10     
==========================================
+ Hits         3284     3303      +19     
+ Misses        448      439       -9     
Flag Coverage Δ
callbacks 28.45% <0.00%> (-0.07%) ⬇️
compilation_model 40.23% <100.00%> (+1.35%) ⬆️
compilation_source_gen 42.99% <0.00%> (-0.11%) ⬇️
distributed_sampling 37.08% <0.00%> (-0.10%) ⬇️
elementary 26.80% <0.00%> (-0.07%) ⬇️
frontend 36.41% <0.00%> (-0.09%) ⬇️
gibbs 39.55% <0.00%> (-0.10%) ⬇️
graphs 20.71% <0.00%> (-0.06%) ⬇️
inference_chains 36.92% <0.00%> (-0.09%) ⬇️
inference_flexichains 41.22% <0.00%> (-0.10%) ⬇️
inference_hmc 29.53% <0.00%> (-0.08%) ⬇️
inference_marginalization 30.78% <0.00%> (-0.08%) ⬇️
inference_mh 35.45% <0.00%> (-0.09%) ⬇️
inference_slice_sampling 25.88% <0.00%> (-0.07%) ⬇️
log_density 56.57% <0.00%> (-0.14%) ⬇️
model_operations 42.48% <0.00%> (-0.11%) ⬇️
parallel_sampling 23.48% <0.00%> (-0.06%) ⬇️
params_with_stats 38.06% <0.00%> (-0.10%) ⬇️
serialization 32.87% <0.00%> (-0.09%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@TuringLang TuringLang deleted a comment from github-actions Bot May 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

JuliaBUGS.jl documentation for PR #453 is available at:
https://TuringLang.github.io/JuliaBUGS.jl/previews/PR453/

shravanngoswamii added a commit to TuringLang/actions that referenced this pull request May 17, 2026
DocumenterVitepress's writer calls `Documenter.deploy_folder(...)` while
`makedocs()` runs in order to compute the `bases` list it writes to
`build/bases.txt`. `deploy_folder` requires `GITHUB_TOKEN` to recognise a
CI build; without it the writer falls back to `all_ok=false,
subfolder=""`, bases.txt ends up empty, and the deploy step then logs
"Found no bases suitable for deployment" and exits without deploying.

Plumbing the token through the build step fixes PR-preview deploys
end-to-end for downstream packages that use `use-vitepress: 'true'`
(observed on TuringLang/JuliaBUGS.jl#453).

Co-Authored-By: Claude <noreply@anthropic.com>
shravanngoswamii added a commit to TuringLang/actions that referenced this pull request May 17, 2026
When the rendered site is a Vitepress build, the TuringNavbar previously
stayed pinned to its own hardcoded palette (`--primary-bg: white`, Source
Sans Pro, heavy box-shadow) regardless of Vitepress's light/dark toggle.
Visually the result was two stacked headers with totally different colour
and typography on the same page (jarring on TuringLang/JuliaBUGS.jl#453).

`html:has(.VPNavBar)` rebinds the navbar's CSS variables to Vitepress's
own design tokens (`--vp-c-bg`, `--vp-c-text-1`, `--vp-c-brand-1`,
`--vp-font-family-base`, ...) so the navbar follows the theme. Also
trade the navbar's heavy shadow for a 1px divider so the boundary with
the Vitepress nav below reads as one continuous chrome.

Documenter HTML output is unaffected — `:has(.VPNavBar)` simply doesn't
match anything there.

Co-Authored-By: Claude <noreply@anthropic.com>
yebai pushed a commit to TuringLang/actions that referenced this pull request Jun 4, 2026
* add DocumenterVitepress support to DocsDocumenter action

* DocsDocumenter: expose GITHUB_TOKEN to the build step

DocumenterVitepress's writer calls `Documenter.deploy_folder(...)` while
`makedocs()` runs in order to compute the `bases` list it writes to
`build/bases.txt`. `deploy_folder` requires `GITHUB_TOKEN` to recognise a
CI build; without it the writer falls back to `all_ok=false,
subfolder=""`, bases.txt ends up empty, and the deploy step then logs
"Found no bases suitable for deployment" and exits without deploying.

Plumbing the token through the build step fixes PR-preview deploys
end-to-end for downstream packages that use `use-vitepress: 'true'`
(observed on TuringLang/JuliaBUGS.jl#453).

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

* DocsNav: make TuringNavbar coexist with DocumenterVitepress

The static HTML navbar is inserted right after <body>, but Vitepress's
own .VPNav / .VPNavBar / .VPLocalNav are `position: fixed; top: 0` —
they overlap the TuringLang navbar end-to-end. The existing CSS in
TuringNavbar.html only handles Documenter HTML's `.docs-sidebar` /
`#documenter`, which don't exist in a Vitepress build.

Add a "DocumenterVitepress Tweaks" block that nudges every Vitepress
top-pinned element down by `--navbar-height`. The selectors only match
Vitepress output, so Documenter HTML rendering is unaffected.

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

* DocsNav: inherit Vitepress design tokens for the TuringLang navbar

When the rendered site is a Vitepress build, the TuringNavbar previously
stayed pinned to its own hardcoded palette (`--primary-bg: white`, Source
Sans Pro, heavy box-shadow) regardless of Vitepress's light/dark toggle.
Visually the result was two stacked headers with totally different colour
and typography on the same page (jarring on TuringLang/JuliaBUGS.jl#453).

`html:has(.VPNavBar)` rebinds the navbar's CSS variables to Vitepress's
own design tokens (`--vp-c-bg`, `--vp-c-text-1`, `--vp-c-brand-1`,
`--vp-font-family-base`, ...) so the navbar follows the theme. Also
trade the navbar's heavy shadow for a 1px divider so the boundary with
the Vitepress nav below reads as one continuous chrome.

Documenter HTML output is unaffected — `:has(.VPNavBar)` simply doesn't
match anything there.

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

* DocsNav: fix Vitepress chrome stacking — gap + content overlap

Two bugs in the previous Vitepress overrides:

1. Both `.VPNav` (outer position:fixed) and `.VPNavBar` (inner
   position:relative inside `.VPNav`) were getting `top:
   var(--navbar-height)`. Relative-positioned children's `top` shifts
   them WITHIN their parent, so this double-offset the bar by
   `2 × --navbar-height` and produced a visible empty band between the
   TuringLang navbar and the Vitepress nav. Fix: shift only the outer
   `.VPNav`.

2. `.VPSidebar` and `.VPContent` were getting `padding-top:
   var(--navbar-height) !important`. That REPLACED Vitepress's own
   padding-top for its nav, so the content's top moved up by
   `var(--vp-nav-height) - var(--navbar-height)` — the Vitepress nav
   ended up floating in the middle of the page and content rendered
   both above and below it. Fix:

   - For `.VPSidebar` and `.VPLocalNav` (independently position:fixed at
     `top: var(--vp-nav-height)`) set `top: calc(var(--vp-nav-height) +
     var(--navbar-height))` so they clear both navs.
   - For `.VPContent` use `margin-top: var(--navbar-height)`. Margin is
     additive to Vitepress's existing padding-top, so the content
     correctly sits below both navs without removing the Vitepress nav
     padding.

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

* DocsNav: use Vitepress's --vp-layout-top-height hook for stacking

Replace the manual shift overrides for `.VPNav` / `.VPSidebar` /
`.VPLocalNav` / `.VPContent` with a single rule that binds Vitepress's
own `--vp-layout-top-height` variable to the TuringLang navbar height.

Vitepress's default theme is authored exactly for this case: every
layout-sensitive component already adds `--vp-layout-top-height` to its
own offset (verified against vitepress@1.6.4 — `VPNav.vue` uses
`top: var(--vp-layout-top-height, 0px)`, `VPContent.vue` uses
`margin: var(--vp-layout-top-height, 0px) ...`, `VPSidebar.vue` uses
`top: var(--vp-layout-top-height, 0px)`, `VPLocalNav.vue` uses
`padding-top: var(--vp-layout-top-height, 0px)`, and `VPDoc.vue`'s
sticky aside includes the variable in its `padding-top` calc).

Using the hook fixes:
- The empty band that the previous `.VPNav` + `.VPNavBar` double-shift
  produced.
- The content / TOC clipping behind the Vitepress nav from
  `padding-top:var(--navbar-height) !important` replacing Vitepress's
  own padding.
- The sidebar dropping far below the Vitepress nav because manual
  `calc(--vp-nav-height + --navbar-height)` over-counted Vitepress's
  internal padding.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
@sunxd3

sunxd3 commented Jun 24, 2026

Copy link
Copy Markdown
Member

@shravanngoswamii anything I can help with?

@shravanngoswamii

Copy link
Copy Markdown
Member Author

@shravanngoswamii anything I can help with?

Actually, I am moving DoodleBUGS core codebase to mcmcjs, so things here will require later efforts. So I am holding this until porting to mcmcjs completes. Once DoodleBUGS core is in mcmcjs, than only DoodleBUGS web app will live here and everything else will be managed by mcmcjs cli

Volume 3 was never included in the BUGSExamples module. Fourteen of its
eighteen examples compile as written; the other four need language features
JuliaBUGS does not have yet (partially observed multivariate nodes in Camel,
dloglik in Fire, interp.lin in Jama and St Veit).

Three latent bugs surfaced while enabling it. Each example file assigns bare
names into one shared module scope, so a file that omits one silently inherits
the previous file's value:

- Volume_3/05_Hepatitis_ME.jl passed reference_results, which leaked in from
  05_Hepatitis.jl, instead of its own reference_results_me.
- Volume_2/04_Biopsies.jl spells its initial values intis, so the Example got
  03_Multivariate_Orange_trees.jl's inits instead.
- Volume_4/11_Methadone.jl called joinpath(readdir(), ...), which has no
  method, named a file that does not exist, and never built an Example.

Also enables Stagnant and Asia, which compile fine and were commented out
without a recorded reason. Inhalers stays disabled: its Example call had the
wrong arity, and fixing that reveals the real blocker, a cyclic graph from
using the logical node group[i] as an index.

Registered examples go from 34 to 50.
The 19 Volume 1 example pages were wrapped in hide(), so they built but never
appeared in the sidebar and nobody could find them. They are now a top-level
Examples section split by volume.

Volume 2 and 3 pages are generated by docs/gen_examples.jl. They hold no copy
of the model, data, or initial values: each page pulls those from
JuliaBUGS.BUGSExamples at build time, so there is one source of truth and the
pages cannot drift from the registry. The generator probes each example first
and, where JuliaBUGS cannot compile it yet, renders the compile step as an
unexecuted block under a warning naming the actual limitation instead of
failing the build. It never overwrites a page whose PROSE marker has been
filled in, so hand-written prose can be layered in per page.

Twelve Volume 1 pages now carry a live graph, the twelve models whose graphs
ship inside doodleppl. docs/src/assets/doodleppl.js injects the widget bundle
only on pages that embed one, pins it to 0.8.1 with an SRI hash, and mirrors
Documenter's light/dark theme onto the widget through a MutationObserver on
the html class, which is how Documenter signals a theme change. If the CDN is
unreachable, as in an offline build, the embed collapses to a short note
pointing at the model definition above it.

Docs build clean: 79 pages, 52 of them examples.
Also simplifies the Methadone data construction. The original built a NamedTuple
through two nested comprehensions that the formatter could only wrap into a very
long line; a generator splat reads better and stays stable under the formatter.
Every generated page now opens with a real description of the data and the
model instead of the placeholder, so the PROSE marker is gone from all 30 and
the generator will no longer overwrite them.

Four formulas were wrong before this and are fixed. They share a cause: the
OpenBUGS pages render Greek in a symbol font, so text copied from them turns mu
into m, alpha into a, and tau into T. The in-tree Volume_2/01_Dugongs.md still
shows the damage. Every formula on these pages was checked symbol by symbol
against the @bugs block it describes.

Some substantive corrections came out of that check. The Stagnant page claimed
the log density has jump discontinuities at the covariate values; it does not,
the branch multiplier is zero exactly where the branch switches, so the density
is continuous and merely kinked. The real difficulty is the correlation between
alpha and the changepoint. Alligators notes that the published table counts 221
animals while the shipped array sums to 219. Eye Tracking notes that the stored
reference results come from the fixed-A/B run while the model samples both.

Passages that tracked the OpenBUGS wording too closely were rewritten. That
documentation is GPL-3 and this package is MIT, so the pages paraphrase and
cite rather than reproduce.

The new bugsexamples test group compiles every registered example and asserts
that no two share reference_results, inits, or inits_alternative by identity.
That is the regression test for the shared-module-scope bug: a file that
forgets to define one of those silently inherits the previous file's value,
which is how Hepatitis ME published Hepatitis's numbers and how Biopsies used
the orange-trees initial values. There are no collisions across all 50.

399 assertions pass. Docs build clean: 79 pages, 12 with a live graph.
The docs build failed on endo: it compiles from some random starting values and
throws DomainError from others, and the generator had probed it once, seen it
succeed, and emitted an executable block.

The single probe was hiding a second, worse problem. The hand-written Volume 1
pages call epil(data, inits), passing the example's own initial values, which is
exactly why epil, kidney and leukfr never flake. The generated pages passed no
inits at all. Testing both ways: epil, kidney, leukfr, endo and alligators all
fail bare and succeed with inits, so the alligators page was about to ship a
'JuliaBUGS cannot compile this model yet' warning that is simply untrue.

biopsies goes the other way. Its initial values leave missing in the upper
triangle of error, which Multinomial rejects, so it needs the bare call even
though it has inits.

The generator now tries each call eight times and takes the first that never
fails, and the probe and the page make the identical call so the probe proves
something. Every page compiles for real: 25 with inits, 5 bare, no warnings.

Docs build clean: 79 pages, 12 with a live graph.
The generator carried a graph-embedding path that could never fire: all twelve
keys in BUNDLED_GRAPHS are Volume 1 keys and the generator only emits Volume 2
and 3, so no generated page has ever contained a doodle-ppl element. The twelve
real embeds are hand-written in the Volume 1 pages. Also gone: page_title, whose
two branches returned the same value, and the volume_1 and volume_4 entries in
three dicts that are only ever indexed by the two volumes it generates.

The Sampling section is deleted from all thirty pages. It was byte-identical on
every one of them, was never executed, and contradicted the section directly
above it: twenty-five pages compile with inits and say inits are not optional,
and the Sampling block then recompiled without them. On the five Fun Shapes
pages it recommended NUTS with Mooncake on models whose own prose says there is
no gradient anywhere. The pages now link to Getting Started instead.

Three real bugs came out of the same pass. The reference-results guard tested
only for nothing, so an empty NamedTuple passed it and six pages rendered the
line about published posterior summaries above nothing at all; the five Fun
Shapes rendered an empty Data section too, on pages whose prose says they have
no data. The eye_tracking page told readers to look at a
reference_results_variable field, which is a bare binding in the source file and
not one of the seven fields of Example, so the call throws.

Volume_3/notes.md said Eye Tracking and Hips2 through Hips4 were blocked by
discrete variables and graph loops. All four compile and are registered by this
PR, so it now records what is actually true per file. The Pigs and Simulated
data rows in notes.md claimed modelling limitations for source files that are
one byte long.

Test cleanup: four assertions were tautologies guaranteed by the struct's field
types, and the compile testset duplicated test/model/bugsmodel.jl, which already
compiles all of Volume 1 with inits. Its skip list called kidney, leukfr and
epil known-broken on main when that test compiles them successfully every run.
It now covers only the volumes this PR adds, and picks the compile call per
example the way the pages do. Folded into the compilation_model group rather
than paying two more CI jobs for a seventy-line file.

Dropped the Methadone changes: Volume_4 is included nowhere, so all sixty-two
lines were unreachable, and this PR's own index page explains Volume 4 is out of
scope. Also dropped the unused doodleppl-caption CSS and the index Model column,
which repeated the link text beside it on all thirty rows.

gen_examples.jl goes from 356 to 265 lines. Docs build clean: 79 pages, 12 with
a live graph. 310 assertions pass.
docs/gen_bundles.jl writes each registered example as a model file, a data file,
and a shell script that fits it with the mcmcjs CLI and exports one
self-contained run bundle. The bundles can be hosted anywhere and opened in the
report app through a bundle URL, which is what lets an example page link to real
posterior results instead of only the published summary table.

Two details had to be got right and are worth recording. The model file carries
the original BUGS program verbatim and parses it with the string form of @bugs;
deparsing model_def instead produces a quote block, which @bugs rejects. And the
data has to be written row by row, because the Julia driver rebuilds matrices
with stack(elems; dims = 1) while JSON.print writes a matrix column-wise, so the
30 by 5 outcome matrix in Rats arrives transposed and the fit dies on the first
out-of-range index.

The sampler settings are the ones the OpenBUGS pages report for these examples,
a 1000 update burn in followed by a further 10000 updates, over the two chains
implied by the two sets of initial values every example ships. Thinning by 10
keeps 1000 of those 10000 draws so a bundle stays inside the size the report app
will fetch; the sampling effort is unchanged.

Verified on two examples. Rats reproduces the published table, alpha0 106.6
against 106.6 and sigma 6.079 against 6.093, from a 2.7 MB bundle, and Pumps
fits in 55 s to 775 KB. JuliaBUGS samples with NUTS where OpenBUGS used Gibbs,
so posterior means agree but Monte Carlo error will not, and that belongs on any
page that shows both.

The script only writes files, so it adds no dependency on mcmcjs, and fitting
stays a separate deliberate step because the full set takes hours.
The workflow that fits these needs to do a volume at a time, since the full set
is hours of sampling, so the selector now takes volume_1, volume_2, volume_3,
all, or a single example key.

The generated run.sh no longer uses set -e. A model that fails is reported and
skipped, and the script exits with the number of failures, because losing an
hour of finished fits to one bad model is worse than an incomplete set.
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

A preview of this pull request has been deployed.
https://turinglang.org/JuliaBUGS.jl/DoodlePPL/pr-previews/453

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results

Julia 1.11.9 on AMD EPYC 9V74 80-Core Processor (Linux)

Ratio = JuliaBUGS/Stan (lower is better for JuliaBUGS)

Model Stan Params JBUGS Params LD Ratio Grad Ratio
rats 65 65 0.33x 2.04x
pumps 12 12 0.63x 2.36x
bones 13 13 0.64x 4.45x
oxford 244 244 1.65x 4.49x
epil 303 303 0.42x 1.67x
lsat 1006 1006 0.64x 2.24x
schools 133 133 0.26x 1.52x
beetles 2 2 1.48x 5.38x
air 5 5 0.51x 1.93x

Note: Stan benchmarks use hand-optimized Stan models, not direct BUGS translations. Comparison is illustrative only.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add all BUGSExamples to JuliaBUGS docs

2 participants