Skip to content

Keep loaded roles and IDs through subsample without backing variables - #622

Draft
MaxGhenis wants to merge 5 commits into
masterfrom
fix/subsample-preserve-roles
Draft

MaxGhenis wants to merge 5 commits into
masterfrom
fix/subsample-preserve-roles

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Simulation.subsample() rebuilds the simulation from a person-level frame. Before this change, the frame could omit structural fields that had no backing variable. The loader uses those fields to identify people, group them into households, and assign roles. Missing role columns could cause the reload to assign the default role to every member, or raise when the default was absent or at capacity. Missing ID columns could also make the rebuild fail. policyengine_core/simulations/simulation.py:1119, :3688.

The case was found during review of PolicyEngine/policyengine-core#595.

Before and after

The country template example removes person_household_role, sets default_role = "first_parent", and loads memberships [0, 0, 0, 1] and roles [first_parent, child, child, first_parent]. It then runs subsample(n=2, seed=0). tests/core/test_subsample_explicit_roles.py:57.

| Stage | First parents per household | Children per household |
|---|---|
| Original load | [1, 1] | [2, 0] |
| Historical subsample on 1cccc278e | [3, 1] | [0, 0] |
| Historical subsample with #595 applied | Capacity ValueError | Rebuild interrupted |
| Subsample with this PR | [1, 1] | [2, 0] |

With no backing ID variables, the array tests load people [100, 101, 102] with memberships [10, 10, 20]. Both samples and a fresh load of the sampled flat file retain those labels. tests/core/test_subsample_explicit_roles.py:397.

Changes

  • The rebuild includes loaded person IDs, membership IDs, and roles for fields without recorded inputs. Fields without a backing variable use the __eternity suffix. The sampled flat file uses its membership labels as group IDs. policyengine_core/simulations/simulation.py:3688, :1216, :3870.
  • String role columns carry loaded keys. An unmatched member receives a key outside the entity’s flattened keys, so it reloads unmatched even if the entity defines "" as a role. policyengine_core/simulations/simulation.py:3752.
  • Integer role columns carry flattened slot indices. Two slots can share a key and keep separate counts. When a detached role’s key identifies one slot, it resolves to that slot. policyengine_core/simulations/simulation.py:3725.
  • The loader tracks the structural columns it reads. It leaves those columns out of ignored-column warnings and still reports genuinely unknown columns and unused period-suffixed columns. policyengine_core/simulations/simulation.py:1161, :1284, tests/core/test_subsample_explicit_roles.py:329, :341.

Invariants

Recorded structural inputs take precedence over the loaded values and can change the roles or household partition that the sample uses. For flat files, the loader takes the first matching column in dataset order. Subsampling exports recorded periods in sorted order, so the reload can select a different period. For example, a source with its 2025 role column before its 2024 column loads the 2025 roles and can rebuild with the 2024 roles. policyengine_core/simulations/simulation.py:1124, :3458, :3651, tests/core/test_subsample_explicit_roles.py:253, :264.

  1. Roles. Where the rebuild uses loaded roles, it carries matched keys and leaves unmatched members unmatched. Integer reconstruction preserves the individual flattened slots. policyengine_core/simulations/simulation.py:3725, :3752.
  2. Whole households. Sampling keeps all rows associated with each selected household in the exported data. When the export uses loaded memberships, those rows form whole source households. policyengine_core/simulations/simulation.py:3708, :3810.
  3. Role counts. With the same members and role assignments, nb_persons(role) returns the same counts. The integer regression checks this with two adult slots. tests/core/test_subsample_explicit_roles.py:205.
  4. Defaults and backing variables. The structural fallback works with or without backing variables. The tests cover bounded, unbounded, unmatched, and absent defaults, and both entity-specific and generic role columns. tests/core/test_subsample_explicit_roles.py:96.
  5. Repeat sampling. The tests check role preservation over repeated samples and membership labels after repeated samples from both array formats. tests/core/test_subsample_explicit_roles.py:116, :397.
  6. Integer reconstruction errors. When there is no recorded integer role input and a member’s slot cannot be identified, subsampling raises ValueError before replacing the dataset or populations. policyengine_core/simulations/simulation.py:3737, tests/core/test_subsample_explicit_roles.py:153.
  7. Warnings. Ignored-column warnings omit the columns the loader consumes for structure. tests/core/test_subsample_explicit_roles.py:304, :316, :379.

Tests

Ran with Python 3.13.5 in an editable install.

Command Result
timeout 900 python -m pytest -q -p no:cacheprovider <file> for each of 29 focused files 382 passed
timeout 900 make test-country-template 39 passed
timeout 1500 python -m pytest -q -p no:cacheprovider tests 4,364 passed, 4 skipped, 3 xfailed

All commands in the table exited 0. The focused runs included all subsample modules, default roles, dataset warnings, the simulation builder, role projectors, dump and restore, and the YAML runners. The explicit-role module now has 41 example cases. The two Hypothesis properties run with 200 and 100 generated examples, plus their explicit examples. tests/core/test_subsample_explicit_roles.py, tests/core/test_subsample_explicit_roles_property.py:81, :143.

ruff format --check and ruff check passed on the changed Python files and the PR’s shared role fixture. towncrier check --compare-with origin/master found the .fixed.md fragment. The branch already contains the pinned master tip, and both explicit-role modules passed on the merge-check branch.

The PE-US Core gate and full-sample controls require real-data runs and remain with the maintainer.

axiom: n/a (Core engine infrastructure).

🤖 Generated with Claude Code

MaxGhenis and others added 4 commits October 18, 2026 14:37
subsample rebuilds the simulation from a person-level frame of recorded
inputs plus the structural leaves retain_structure adds. retain_structure
skipped any leaf the tax-benefit system had no variable for, so a system
without person_<entity>_role rebuilt every member with the default role
(silently on master; as a capacity error once #595 lands), and a system
without person_id or person_<entity>_id variables failed to subsample.

retain_structure now carries person IDs, memberships and role keys whether
or not a variable backs them. An integer role variable cannot hold "no
role", so subsample raises before changing anything instead of writing
role index 0. The loader records the columns it reads for structure and
no longer reports them as ignored.

Found in review P3-1 on #595.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For any households of bounded, unbounded and unmatched role keys, person
order, weights, default role, role source and sample, every kept household
is a whole source household whose role counts (by key and nb_persons)
equal the source's, with or without a role variable, through a second
subsample too, and no structural column is reported as ignored. An integer
role variable keeps the roles or raises before changing anything.

Move the household-table oracle into the shared fixture module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clarify structural input precedence during subsampling and cover loaded role keys across dataset formats.

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>

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.

1 participant