Skip to content

Make flat-file datasets safe to load, map and round-trip - #646

Draft
MaxGhenis wants to merge 5 commits into
masterfrom
simulated-instrument-flat-file
Draft

MaxGhenis wants to merge 5 commits into
masterfrom
simulated-instrument-flat-file

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Make person-level flat files easier to load, export, and reload. Companion to PolicyEngine/policyengine-us#10574 (state swap) and PolicyEngine/policyengine.py#732 (helper and recipe). Together they support reading a CSV, setting state_fips, building a Dataset.from_dataframe, and passing it to a Microsimulation.

What changes

Loading a flat file

  • The loader checks for an exact registered variable name before splitting a period suffix at the last __. Both survey__score and survey__score__2024 can load.
  • Each row represents one person. When the file lacks a person ID column, the loader numbers the rows, including in a frame with zero columns.
  • A missing group membership column raises a ValueError that names the expected columns. Dataset.from_dataframe documents the format. (policyengine_core/simulations/simulation.py:1123, policyengine_core/data/dataset.py:484)

map_to on text and enums

  • A target group receives the text or enum value its members share. If they disagree, the mapping raises ValueError.
  • An empty group receives an empty string or the first enum member. Enum decoding happens after mapping, so it preserves that default.
  • Default enum mapping returns an EnumArray on every dataset type. The previous default treated enum indices as numbers and aggregated them. Numeric variables still use sums and shares. (policyengine_core/simulations/simulation.py:1584, :1611, :1685)

to_input_dataframe

  • TaxBenefitSystem.source_input_variables declares recorded observations for variables that can also have formulas. Both input exporters keep these observations and recorded population IDs. Reforms inherit the declaration. Both exporters warn when they omit a supplied value for another formula variable.
  • A period export includes its stored inputs, all-time inputs, and inputs it carries or uprates. It retains the stored year when a requested month falls inside it. Where a formula can fall back to an earlier observation, the export includes that observation at its original period.
  • A period export uses reserved columns for person labels, group labels and positions, and role keys. The loader uses those values to rebuild the represented population. Stored ID inputs still load as inputs, and an ID formula can compute any ID without a stored input. (policyengine_core/simulations/simulation.py:3387, :3456, :3501, :3598, policyengine_core/reforms/reform.py:139)

One changed test

test_subsample_preserves_formula_backed_structural_ids now expects a recorded person_id in the input export. A stored ID can determine an entity's random seed after reload. The test still checks that a simulation with a formula-backed ID can be subsampled. (tests/core/test_subsample_invalidates_cache.py:67)

Invariants

  • Text and enums map to a group when its members agree. An empty group receives the corresponding default. (tests/core/test_map_result_categorical.py:106, :186, :196, tests/core/test_flat_file_property.py:25)
  • Reloading a period export preserves the included stored, carried, and uprated inputs and the population structure represented by person rows. A group needs a member row. (tests/core/test_flat_file_datasets.py:258, :297, :315, tests/core/test_flat_file_property.py:93, :113)
  • A formula that reads another period needs that period's inputs too. Omit period to export all stored periods. Supply the usual person and group columns if those inputs are absent from the unfiltered export. (docs/python_api/simulations.md:15, tests/core/test_flat_file_datasets.py:349)
  • Numeric mapping retains its sums and shares. (tests/core/test_map_result_categorical.py:163)

Migration

Use calculate("state_code", year, map_to="tax_unit", decode_enums=True) to compare enum labels such as "IA". The default returns an EnumArray. If code needs arithmetic on enum codes, convert the source enum to a numeric array before calling map_result and choose the numeric mapping explicitly. (policyengine_core/simulations/simulation.py:1611, docs/python_api/simulations.md:24)

Tests

Python 3.13.5, with the editable development install. The full suite passed with 2,899 passed, 4 skipped, and 3 xfailed. The focused checks passed on the fixed checkout and on a local merge with master.

timeout 900 python -m pytest -q -p no:cacheprovider tests/core/test_flat_file_datasets.py
timeout 900 python -m pytest -q -p no:cacheprovider tests/core/test_flat_file_property.py
timeout 900 python -m pytest -q -p no:cacheprovider tests/core/test_map_result_categorical.py
timeout 900 python -m pytest -q -p no:cacheprovider tests/core/test_subsample_invalidates_cache.py
timeout 900 python -m pytest -q -p no:cacheprovider \
  tests/core/test_simulations.py \
  tests/core/test_dataset_default_role.py \
  tests/core/test_dataset_unknown_column_warning.py \
  tests/core/test_group_population_empty.py \
  tests/core/test_join_with_persons_identity.py \
  tests/core/test_carry_over_order.py \
  tests/core/test_uprating_order.py \
  tests/core/test_reforms.py
timeout 1500 python -m pytest -q -p no:cacheprovider

ruff format --check and ruff check passed on the changed Python files. Local checks used synthetic fixtures. The Windows matrix, documentation build, country-package smoke jobs, and data-backed microsimulation were outside the local run.

axiom: n/a: microsim input handling, no policy change

🤖 Generated with Claude Code

MaxGhenis and others added 3 commits November 1, 2026 16:42
A researcher's person-level file should load, export and reload without
hand fixes. Flat files now number people when no column does, read column
names holding two separators, and name the missing column when a group
has no ID. Strings and enums map to a group as the value its members
share. to_input_dataframe keeps population IDs and declared source inputs,
takes a period, and warns about values it leaves out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A period export dropped inputs the period carries from an earlier one, so a
2023 export of a 2022 file reloaded as defaults. It now holds those values,
carried or uprated, and keeps a stored year when the request sits inside it.

A month request built a twelve-month period starting in that month instead of
the calendar year. A group with no members now maps to an empty string, not
"0". to_input_dict warns about left-out formula values as the DataFrame does.

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