Skip to content

Simulation.subsample() raises TypeError: build_from_dataset() needs a dataset #2254

Description

@MaxGhenis

What happens

Calling subsample() on a PolicyEngine-UK simulation raises a TypeError:

Simulation.build_from_dataset() missing 1 required positional argument: 'dataset'

Reproduction

Run by the US + core hub on policyengine-uk 2.122.2, with policyengine-core at master 78a6c503 and at PolicyEngine/policyengine-core#573's head (same failure on both):

Microsimulation(dataset=...).subsample(n=10_000, seed=0, time_period=2026)

It has not been run on 2.125.1. The code below is unchanged on main, so the same failure is expected there.

Cause, read in the code

  • policyengine-core, policyengine_core/simulations/simulation.py (master 78a6c503): Simulation.subsample ends with self.build_from_dataset(), called with no arguments (line 3354). Core's own build_from_dataset(self) takes none (line 1080).
  • policyengine-uk, policyengine_uk/simulation.py (main e89b8abc): the override is build_from_dataset(self, dataset: Dataset) (line 455), which requires a positional dataset.
  • policyengine-uk defines no subsample of its own in simulation.py or microsimulation.py, so the inherited core method runs and hits the mismatch.

This predates core#573: it fails the same way on core master.

Options

  1. Override subsample in policyengine-uk so that it works with the UK dataset classes, and add a test.
  2. Override it to raise a clear NotImplementedError that points to the supported way to subsample UK data.
  3. Make the override's dataset argument optional and fall back to self.dataset. Check first that core's subsample (which rebuilds self.dataset through Dataset.from_dataframe) produces something the UK builder can load; the signature may not be the only mismatch.

Whichever is chosen, a test that calls subsample() on a small UK simulation would catch a regression.

Not affected

Full-sample UK totals are identical under core master and core#573.

Activity

  1. MaxGhenis commented on Oct 10, 2026

    @MaxGhenis
    CollaboratorAuthor

    Fix opened: #2265

    The fix is in policyengine-uk only (option 1, a UK subsample); nothing needs to change in policyengine-core. Making dataset optional (option 3) would have run and given wrong results: core's method reads memberships back from person_household_id and person_benunit_id, which the UK model stores as 32-bit floats, so identifiers above 2**24 would merge households. The PR description lists the other mismatches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions