Repository navigation
Conversation
Adds tests for tools/sample_us_base_households.py, which samples a US base H5 for the 5% national run, and a placeholder tool that draws base households one by one through the export sampler. Every test fails on its assertions against the placeholder: - a drawn source household enters with every copy; - source households with a capital-gains copy form their own groups, each group keeps its weight total, and one factor scales every copy; - rare-input source households are kept with every copy at source weight; - the dominant-household rule also checks the two main capital-gains amounts, weighting each copy's amount by that copy's own weight; - the base's root attributes are copied onto the sample; - the command defaults to 5% and seed 0; - the receipt names the base, both tools and the source households drawn; - bases without copy columns, with a repeated copy index or with copies that disagree on source_year are refused; - the fixture path imports no engine module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1.9 · 1) tools/sample_us_base_households.py writes a seeded sample of a US base H5 and its sample_receipt.json (kind us_base_source_household_sample), defaulting to 5% and seed 0. It loads the export sampler and reuses its chunked reader, rare-input and dominant-household rules, draw and post-write check, with these differences: - The draw's unit is the source household (household_source_id): it enters with every copy (household_support_clone_index 0, 1 and, when present, 2) and each copy with its persons and group units. - Source households are grouped by source_year and by whether they have a capital-gains copy; every copy of a drawn source household is scaled by its group's one factor, so group weight totals and the weight ratios between copies are kept. - The two rules keep source households whole at their source weights. A copy's input amount counts at that copy's own weight, and the dominant-household rule also checks long_term_capital_gains_before_response and short_term_capital_gains together. - The base's root attributes, among them the congressional-district crosswalk attributes the release checks on every run, are copied onto the sample; the writer sets none. - A base without the copy columns, with two households of one copy index in a source household, or with copies that disagree on source_year is refused. The size-certainty record in the capital-gains test now reads source_households, the receipt's name for counts of source households. Docs: the release build rule gains a section on sampling a base H5, and the agent guide points to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_the_copy_columns_match_the_provenance_module now checks that reading the provenance module's names imports no microcosm.build.us_runtime module: that package's init builds the policyengine-us tax-benefit system wherever the engine is installed, and the agent guide keeps both samplers' tests off it. It fails while the test imports the module through its package. The group test also checks that each group's receipt total equals the written weights' total, which the next commit computes from the written weights; on this fixture the two already agree, so that check passes before the change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the written weights (1.9 · 1) Review round 1 fixes: - test_the_copy_columns_match_the_provenance_module loads the provenance module from its file under a private name, so the test no longer runs microcosm.build.us_runtime's init, which builds the engine wherever policyengine-us is installed. - Each group's sampled_weight_total and conservation errors in the receipt are the written weights' total (the post-write check's stratum_weight_totals), not the draw's per-source-household vector. - The receipt's count and draw rules are written for groups and source households; the tool's docstring and the release build rule say which receipt keys hold the groups (strata) and the kept source households (certainty), and that the release checks the crosswalk attributes on every run against the packaged crosswalk unless another is given. - Tests: the fixtures write the base's root attributes with h5py, as the base stage does; a test covers the default deny-list path through a stub h5_io; the rare-input test checks a group holding both kept and drawn source households. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ry (1.9 · 1) The registry test that scans production code for HDF files opened for writing (test_registry_classifies_every_writable_production_hdf_site, already in the repository) failed at fe8dd20 on an assertion: the new tool's write_root_attributes opens the sample H5 in append mode and was not listed. It copies the base H5's root attributes and writes no Frame column, so it is listed as an exclusion. The test passes after this change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1859
What this changes
tools/sample_us_base_households.pywrites a seeded sample of a US base H5 (tools/build_us_puf_support_base.py's output) and itssample_receipt.json, for the 5% national run: the national release run with--base-h5on the sample. It defaults to--fraction 0.05and--seed 0. It is tracker task 1.9 · 1 of "15. US build memory, CPU and run time"; the release's--sample-receiptmode (1.9 · 2) reads the receipt, androute_a.sh's sample mode (1.9 · 3) runs the command.The tool loads
tools/sample_us_export_households.pyand reuses its chunked reader, its two rules for households kept whole, its draw, its writer and its post-write check. The export sampler,route_a.sh --probeand their receipts are unchanged. What differs for a base H5:household_source_idand differ inhousehold_support_clone_index: 0 the ASEC record, 1 its PUF tax-detail copy, 2 the capital-gains copy (on some source households only; its weight is split off copy 1). A selected source household enters with every copy, and each copy with its persons and every group unit they reference.source_yearand by whether they have a capital-gains copy, so the capital-gains copies, which carry little weight and much of the weighted capital gains, are drawn in their own groups. In each groupmin(N, max(2, floor(0.05 N)))of theNsource households not kept are drawn. Every copy of a drawn source household is multiplied by its group's one factor, so each group's weight total is kept exactly and the weight ratios between a source household's copies are unchanged.--certainty-threshold), every carrier is kept; a source household carries an input when any copy holds a nonzero value;--size-certainty-multiplier), repeated until none qualifies. A source household's amount is the sum over its copies' rows of the absolute value times that copy's own household weight, because a capital-gains copy holds large amounts at a small part of its source household's weight. This rule also checkslong_term_capital_gains_before_responseandshort_term_capital_gainstogether, which no probe checks._assert_cd_vintage_support_matches; the crosswalk defaults to the packaged one) and refuses a base without them, and the writer sets no root attributes.household_source_idorhousehold_support_clone_index, with two households of one copy index in one source household, or whose copies of a source household disagree onsource_year.The receipt
sample_receipt.json,kindus_base_source_household_sample,schema_version1. It keeps the export sampler's layout where the meaning is the same:source(path, SHA-256, size, rows, copies, root attributes),design(fraction, seed, rule texts),strata(the groups),certainty(the kept source households and each one's reasons),probes,capital_gains,selection,output(path, SHA-256, size, root attributes copied),verification,timing,tool_source(this tool's commit and SHA-256, and the export sampler's SHA-256). A count namedsource_householdscounts source households, and one namedhouseholdscounts base households. Each group'ssampled_weight_totalis the total of the weights written.Other files
docs/us-release-build-rule.md: a new section, "Sampling a base H5 for the 5% national run".docs/agent-guide.md: points to it beside the export sampler.changelog.d/us-base-household-sampler.added.md.packages/microcosm-build/src/microcosm/build/frame_serializer_registry.py: lists the tool'swrite_root_attributesas an HDF write that copies root attributes only.packages/microcosm-build/tests/engine_free/us/test_us_base_subsample.py,test_support/microcosm_build/us_base_subsample.py.No build's output changes: this adds a tool. No recorded hash or pin moves. No code shared with the UK build changes.
Tests
test_us_base_subsample.py(engine-free, 14 tests), on a synthetic base written as the base stage writes it (tables inUSSingleYearDataset's layout, root attributes through h5py):test_whole_source_households_enter_with_every_copytest_each_group_draws_its_share_and_keeps_its_weight_total(also: one factor for every copy of a group's drawn source households; the receipt's group total equals the written total)test_rare_input_source_households_are_kept_whole_at_source_weight(also: a group holding both kept and drawn source households)test_the_default_path_checks_the_base_against_the_deny_listtest_the_dominant_household_rule_weights_each_copy_by_its_own_weighttest_root_attributes_are_copied_onto_the_sampletest_the_cli_defaults_to_five_percent_and_seed_zerotest_the_receipt_names_the_base_and_both_toolstest_the_draw_depends_only_on_the_seedtest_a_base_without_copy_columns_is_refusedtest_a_source_household_with_two_rows_of_one_copy_is_refusedtest_copies_that_disagree_on_source_year_are_refusedtest_the_copy_columns_match_the_provenance_module(readssupport_provenancefrom its file, without runningmicrocosm.build.us_runtime's init)test_the_fixture_path_imports_no_engine(a fresh interpreter that refuses policyengine-us andmicrocosm.build.us_runtimeimports)Local results, each test selected by its node ID (US environment,
uv sync --all-packages --locked --extra us):279653358, the tests with a placeholder tool that draws base households one by one through the export sampler: 13 failed, each on an assertion.07dc24f05, the tool: the same 13 passed.cc134d1f5, after review round 1: the copy-column test now checks that it imports nomicrocosm.build.us_runtimemodule, and failed on that assertion. The group-total check added in the same commit passed before the change (the fixture's totals already agreed).fe8dd20a8, the round 1 fixes: all 14 passed.61e392eff: the engine-free (3.14) CI job failed one test already in the repository,test_registry_classifies_every_writable_production_hdf_site, which requires every production function that opens an HDF file for writing to be listed in the HDF write registry. It failed on that assertion locally atfe8dd20a8; after the registry entry it passes.Ruff check and format pass on the changed Python files;
tools/ci_test_plan.py verifypasses. CI has not run: this PR starts it.One run on the real base H5
The base of decision 1.1 · 5 (
base_populace_us_2024_puf_support.h5, 2,370,507,790 bytes, SHA-256b5db384f…; 352,932 households, 168,852 source households), at07dc24f05, with the defaults, on a 10-core, 32 GiB Apple M5 Mac with other work running:/usr/bin/time -l).The sample was deleted after these checks. The fixes after review round 1 changed only the receipt's group totals (now taken from the written weights), rule texts and tests, not the draw.
Review rounds
07dc24f05): one bug, low: the copy-column test importedmicrocosm.build.us_runtime, whose init builds the engine wherever policyengine-us is installed. Fixed with a test that failed first (cc134d1f5,fe8dd20a8). The same commits took up its code-quality points: receipt group totals from the written weights, rule texts for groups and source households, the receipt keys documented, root attributes written through h5py in the fixtures, a test of the default deny-list path, and a test of a group holding both kept and drawn source households.fe8dd20a8): no bugs found, so the reviews stopped there.Points from round 2 left for later (code quality, not bugs):
source_yearis grouped by capital-gains copy only; the receipt records the column as absent.sample_receipt.json), so a second sample written to the same--outreplaces it. For 1.9 · 2's--sample-receiptreader: compare the SHA-256 of the H5 the release is given with the receipt'soutput.sha256, not the path.source_yearon the household table, fraction 1, the five household-location attributes, or a writer that drops one copy.What the merge waits for
No earlier task: 1.9 · 1 is the first task of substage 1.9, has no dependencies, and the substage depends on none. This PR builds on
main. The merge waits only for this PR's own checks.🤖 Generated with Claude Code