Tools#
The policyengine_core.tools module contains miscellaneous utility functions, including for running YAML tests.
run_tests#
- policyengine_core.tools.test_runner.run_tests(tax_benefit_system, paths, options=None)#
Runs all the YAML tests contained in a file or a directory.
If path is a directory, subdirectories will be recursively explored.
- Parameters:
tax_benefit_system (TaxBenefitSystem) – the tax-benefit system to use to run the tests
paths ((str/list)) – A path, or a list of paths, towards the files or directories containing the tests to run. If a path is a directory, subdirectories will be recursively explored.
options (dict) – See more details below.
- Raises:
AssertionError – if a test does not pass
- Returns:
the number of sucessful tests excecuted
Testing options:
dump_simulation#
- policyengine_core.tools.simulation_dumper.dump_simulation(simulation, directory)#
Write simulation data to directory, so that it can be restored later.
Writes each population’s ids and count and each group’s memberships, positions and roles; the simulation’s branch values, including inherited values; and the periods it calculated (see
restore_simulation).
restore_simulation#
- policyengine_core.tools.simulation_dumper.restore_simulation(directory, tax_benefit_system, **kwargs)#
Restore simulation from directory
Each value is restored as the dumped simulation stored it: one it calculated (listed in the variable’s
derived_periods.txt) as a calculated value, and every other value as an input, recorded in_user_input_keysasset_inputrecords one, soapply_reformkeeps the inputs and drops the calculated values, as it would have in the dumped simulation.A dump lists which values were calculated, not where an input came from: a value the dumped simulation stored with
put_in_cachewithoutderived=True(an input to auto-carry-over and uprating thatset_inputdid not record, so thatapply_reformdropped it there) is restored as a recorded input too.A dump written before policyengine-core 3.32.16 lists no calculated value, so every value in it is restored as an input. A dump without a format marker that lists none may be one of those, so restoring it warns:
apply_reformthen keeps any calculated value in it as dumped instead of recalculating it.Each population gets the count the dump records, which may be zero. A dump written before counts were recorded gives each count as the smallest its ids and memberships allow: that is the count when the ids are set, one per instance, as
SimulationBuildersets them.
dependency_map#
Trace the parameter → variable dependency map of a country package.
Downstream tools need to know which variables a parameter path moves and
which variables feed which: validation matching against calibration targets
and scorecards, model-coverage audits, reform classifiers. Static scans of
formula source miss bracket, scale, and vectorised reads; the only exact
record is what the model reads at run time. This module runs simulations
under FullTracer and folds the trace trees into two edge sets:
readers[parameter_path] -> variables whose formula read that parameter consumers[variable] -> variables whose formula read that variable
Parameter paths are recorded at the node the formula indexed, so a bracket
read p.base.calc(age) is recorded as gov.irs.credits.ctc.amount.base.
A read of which children a node has, or of a parameter or node outside a dated
call (its value history, for example), is recorded at that node’s own path and
stands for everything below it; the empty path is the whole tree. A look-up
by name that finds no child on a node at an instant is recorded at the missing
child’s path.
Core’s check of each variable’s own abolition switch,
gov.abolitions.<variable>, is not recorded, so it is not in the map. A
formula’s read of a switch is, like any other read.
Populations#
testsbuilds a simulation for every YAML test under the package’stests directory (skipping tests that apply reforms, extensions, or inline parameter changes) and calculates the test’s outputs. Deterministic, no data download, and country test suites deliberately exercise every program.
microdatacalculates every variable over a subsample of the package’sdefault microdata. Broad, but a formula behind
defined_foronly runs when someone in the sample qualifies.
both the union.
Tests in one file mostly vary inputs for the same outputs and record the same
edges. By default a test is skipped when, earlier in its file, the formula
each of its outputs uses at the test’s period has already run. A formula that
did not run, because defined_for was false for everyone or the value was
an input, does not count, so a later test of that output is still traced.
every_test traces them all; it can still find more edges, because a
skipped test can reach formulas and conditional reads below its outputs that
earlier tests did not.
A test’s output block is read the way the YAML runner reads it: a key is a
variable, an entity’s singular key holding variables, or an entity’s plural
key holding instances of variables, and a dict value keyed by period checks
the variable at each period.
Output#
The map is one JSON object:
generatedAt ISO-8601 UTC timestamp
model package, version, fingerprint, coreVersion. The
fingerprint is a sha256 over the package's files and, when
tests were traced, the traced test files. Microdata is
not hashed.
populations per population traced, its selection and counts:
tests: root, everyTest, tests (situations built), skipped
(tests not traced because their outputs were covered),
excluded (tests with reforms, extensions or inline
parameter changes), failed (situations that could not be
built, and test files or entries that could not be read),
outputs
(variables calculated), outputErrors (output
calculations that raised; their edges are whatever
ran before the error); microdata: households, year,
variables (calculated), failed (raised at every period)
tracingSeconds wall time
readers parameter path -> sorted variables that read it
consumers variable -> sorted variables whose formula read it
The map records what ran. A test that could not be built contributes no
edges, and an output that raised contributes only the edges recorded before
it raised, so failed and outputErrors bound how incomplete a map may
be; the command prints both, and a map with non-zero counts should be read
as a lower bound on the model’s dependencies.
- policyengine_core.tools.dependency_map.model_fingerprint(package_root: Path, tests_root: Optional[Path] = None) str#
sha256 over what the map depends on, so a consumer can tell whether a map still matches the model it runs against.
It covers every file of the package except its
testsdirectory, since formulas import helpers from anywhere in the package, and the files undertests_root, which decide what the map traced.
- policyengine_core.tools.dependency_map.trace_yaml_tests(system: TaxBenefitSystem, paths: Iterable[Path], progress: Optional[Progress] = None, every_test: bool = False) tuple[Edges, dict[str, int]]#
Trace the baseline YAML tests under
pathsinto edge sets.Unless
every_testis set, a test is skipped once the formulas of all its outputs have run in an earlier test of the same file.