Repository navigation
Conversation
Fixes #732 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Group IDs numbered within households merge strangers, and rows of one group that disagree lose all but the first person's value. Neither raises at load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pinned loader sorts groups by ID but reads a one-row-per-group column in
row order, so one-person groups in any other order took each other's values.
Number memberships in file order, and keep the file's own IDs in {group}_id so
results merge back on them.
Check geography across every year the file holds, including county names and
tract, block and district columns, and drop stored copies of what the model
derives from the state. Record the year, state and a digest with the run, and
rewrite the recipe around what the trial found unclear.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mber IDs Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nstruments guide Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Round 3 found that stored county inputs leaked a future county into earlier years and masked county reforms; that state assignment dropped the Oregon child-care provider area; that a member of the wrong enum passed geography checks; that dated role headers collided; and that stored enum inputs with different members could share a fingerprint. The helper no longer writes county inputs: inferred counties come from the active model formula and stay out of cross-year lookups. State assignment keeps the Oregon provider area and the Mississippi facility location. Geography checks the enum class before encoding. The first role column per entity is kept before periods are added. The fingerprint now includes the enum and its member names. The guide and module text now match the base-registry path, the bare role column, the loader's role warning and the county fallback. Tests cover each case, and the overlapping-period test now runs for SNAP and SSI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The model uses household county as a proxy for the child care provider's location in four rate inputs: Oregon's provider area and the Mississippi, New York and Ohio county categories. State assignment now keeps all four when a file supplies them, and a parametrized test checks Mississippi, New York and Ohio against a value the clean run would not infer. The prepare_flat_file docstring and the guide now say that loading the prepared frame directly can carry an inferred county across years. Two new test calls are wrapped to the 88-character line limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Set the county holder's no-store flag before Core loads inputs, and drop values that load-time input handlers computed, so a handler that runs before or after geography never caches a stale county. - Stop state assignment from clearing 23 child-care provider rate selectors; a supplied rate area or county group now survives and sets the rate. - Accept a state_code, state_name or county member from another US system when its enum class has the same qualified name and source file, instead of refusing it. - Describe county inference during loading and the provider selectors that state assignment keeps in docs/simulated-instruments.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A flat-file load now clears simulation branches, the fast cache and the behavioural-response cache after each load-time input, and restores a registered baseline when loading ends. Branches are snapshots, so a handler's calculation on one could otherwise outlive the partial input set it saw. When a handler starts a calculation, the loader also drops computed arrays, snapshots the SPM forecast provider and re-records county input types. Preparation puts state FIPS and the county fields first, and variables with custom period-conversion handlers load after ordinary inputs, so a handler sees the file's geography rather than California's default. New tests cover the stale-branch case and four state and county cases; docs/simulated-instruments.md describes the load order. Co-Authored-By: GPT-6.2 Sol <noreply@openai.com> 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 #732
Adds
pe.us.prepare_flat_file, which validates a researcher's person-level flat file, andpe.us.flat_file_microsimulation, which runs the same people through one assigned state's rules or through their recorded states. The simulated instruments recipe is atdocs/simulated-instruments.md.Why here
policyengine.pygives researchers one interface for validating flat files and running the bundled models. PolicyEngine/policyengine-core#646 and PolicyEngine/policyengine-us#10574 address direct loading and geography in the models. The helpers work at this package's existing pins (policyengine-core==3.34.10,policyengine-us==2.2.2,spm-calculator==1.1.0). No pin moves in this PR, and no bundled number changes.What it does
The pinned loader accepts bare IDs and reads each group's values from its first member.
prepare_flat_fileadds periods to variable columns and keeps the barerolecolumn. It puts every ID on each person's row and numbers group memberships by first appearance.family_idas a 32-bit float. Use the prepared IDs or person row order to map recoded results back.county_fipsto five digits. For recorded states, it requires coherent geography in every covered year and accepts geographic enum names, valid indices and members of the expected enum classes, including members from another system with the same qualified class name and source file. It refuses a member of another enum, even one with a matching index.flat_file_microsimulation, when no supplied county takes precedence, the active model's county formula infers the county. Preparation puts state FIPS and county inputs first. The simulation loads ordinary inputs before custom period handlers, keeps inferred counties out of county storage, and discards provisional calculation state between inputs, so a handler sees the file's geography and the formula can't reuse a county it inferred for one year in another.state=, it setsstate_fipsin every covered year and clears household geography, including residence flags and location-based ACA, housing, child care and SNAP inputs. State assignment preserves supplied child-care provider rate areas and county groups: Oregon'sor_erdc_provider_area, Mississippi'sms_ccpp_facility_location, New York'sny_ccap_county_groupand Ohio'soh_ccap_county_rate_category. In either mode, it clears state-derived fields such asstate_group.prepare_flat_filelooks variables up in the base US registry.flat_file_microsimulationlooks them up in the registry of the system it loads into, which on the default system includes variables thatreform=adds. With an explicittax_benefit_system, register any input thatreform=adds on that system before calling the helper.It refuses empty files, missing required columns, blank or fractional IDs, columns that mix Boolean and numeric IDs, repeated person IDs, conflicting group memberships, and rows of one group that disagree on a group-level input.
flat_file_microsimulationrecords the resolved SPM selection and marks the runtime dataset asflat_fileinpolicyengine_bundle. The record holds the input year, the assigned state when supplied, and a SHA-256 fingerprint of the typed prepared frame and the model's stored inputs. For a stored enum input, the fingerprint also covers the enum and the names of its selected members.Decisions
spm={"geography_kind": "national"}, so the same people get the same national SPM thresholds in every state. After a move, model rules that read the county enum use the assigned state's first county by default.state=, the helper clears household location inputs for every household, including those already in the assigned state. It preserves those provider inputs because a child care provider's location can differ from the household's. To keep the recorded geography, leavestateunset.first_county_in_state, so it would ignore a reform to that fallback. The pinned county formula also returns the latest stored county for any year without its own county input. A simulation that loads the prepared frame directly stores the counties it infers, so pass the original frame toflat_file_microsimulationfor results.Invariants
flat_file_microsimulationreturns the same county and benefits for each covered year in any calculation order.state=, the helper keeps a supplied child care provider location that differs from the one a clean run infers.Changes since
cda0744c6In
c53060e6a:prepare_flat_fileno longer stores a county for each covered year when the file has county FIPS or changes states. The pinned county formula returns the latest stored county for any year without its own county input, so the model could give a year between two recorded years the later year's stored county. With a stored fallback county, the model also never called a reformedfirst_county_in_state. New tests cover a household in Vermont in 2024 and Mississippi in 2026, in both calculation orders, and a reformed fallback county. Two existing county tests now run throughflat_file_microsimulationinstead of loading the prepared frame directly.state=, the helper keeps Oregon'sor_erdc_provider_areaand Mississippi'sms_ccpp_facility_location. A new test supplies an OregonAREA_Aprovider and expects the $1,041 maximum monthly infant rate.state_code,state_nameandcountybefore encoding it. The pinned encoder reads only a member's index.County.BETHEL_CENSUS_AREA_AKandStateCode.CAboth have index 4, so the helper had accepted that county instate_codeasCA.rolecolumn, the first-role-column rule, the loader's role warning and the county-enum fallback.In
97a6fee04, the helper also keeps New York'sny_ccap_county_groupand Ohio'soh_ccap_county_rate_categoryunderstate=. The model derives both from household county as a proxy for the provider's county. A parametrized test supplies a Mississippi, New York or Ohio value that a clean run would not infer and expects it back. Theprepare_flat_filedocstring and the guide now say that a simulation loading the prepared frame directly can carry an inferred county across years.#785 tracks a fingerprint that can change between processes when a reform replaces an enum variable whose enum lives in another file.
Tests
At
ee4052d1e, local checks cover onlypy_compile,git diff --checkandruff format; GitHub CI runs the full suite on this head. At the earlier head97a6fee04, the code CI passed, including Test on Python 3.11 through 3.14, and the separate documentation build passed.At
cda0744c6(CPython 3.14.10, the locked dev environment),tests/test_us_flat_file.pypassed all 200 tests. The same file with the new tests overlaid at45ef535fagave 107 passes and 93 failures. The rest oftests/atcda0744c6gave 4,896 passes and 20 skips. Four UK qualification cases failed there because they read a release index that needs a credential, and they fail the same way at45ef535fa. Atcda0744c6, a hand-built CSV ran through all 50 states and DC with identical national SPM thresholds.Real-data check
The PR author ran this check at an earlier head and has not repeated it at
cda0744c6or97a6fee04.A 300-household Microcosm US sample with 822 people, read back from CSV, ran through the helper for CA, MS, MN, DC and NY, and through its own states.
state_codewas right in each, and national SPM thresholds matched across states. It logged five columns the pinned model does not know, as designed.Differential at the versions then pinned: the helper with
state="MN"gives the same SNAP eligibility, Medicaid eligibility, income, state tax and net income as the same file loaded directly with geography dropped by hand. Means match, and values match once sorted, because group results follow file order.axiom: n/a: microsim input handling, no policy change
🤖 Generated with Claude Code