Support root populations: entities that are not built over persons

未关闭
#666 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend, data

调研方向

Start by tracing TaxBenefitSystem.init and instantiate_entities, then follow population handling through Simulation._bind_to_tax_benefit_system, Simulation.clone, commons/formulas.py, and SimulationBuilder.build_from_dict. Check the existing tests for these paths and add coverage for a root entity alongside the current person and group populations. Done means root populations are created, preserved by binding and cloning, handled as specified for formulas, exports, subsampling, and builder input, while systems without roots keep existing behavior.

由索引模型根据 Issue 内容生成。

描述

Background

PolicyEngine/policyengine-uk#2940 adds a firm entity. A firm population is not a group of persons:

  • firms have their own ids and weights;
  • no person belongs to a firm (a person-to-firm job link may come later);
  • firm and household formulas never read each other.

Core has no entity type for this, so the UK PR works around core, and the workarounds are the risk.

What core assumes today

Read from core 3.34.9; the UK workarounds below are still in use, and pass, on master (3.37.2). Core treats every non-person entity as a GroupPopulation over the single person population:

  1. TaxBenefitSystem.__init__ keeps the first is_person entity as the person entity and treats every other entity as a group.
  2. instantiate_entities builds only the person population and group populations.
  3. Simulation._bind_to_tax_benefit_system raises for any other population, and Simulation.clone copies only person and group populations, so another population is silently lost.
  4. commons/formulas.py calls entity.sum/all/max/min for non-person entities, which a plain Population lacks.
  5. SimulationBuilder.build_from_dict silently ignores an unknown plural, such as a firms: block beside people.

What the UK PR has to do instead

  • Registers Firm as a plain Entity, listed after Person. Core then marks it is_person=True, so downstream code that reads is_person sees two person entities. That forces PolicyEngine/policyengine.py#765 and PolicyEngine/policyengine-api#4339.
  • Replaces two methods on core's own classes at import: TaxBenefitSystem.instantiate_entities (to add a RootPopulation) and Simulation.clone. Both replacements pass through when there are no root populations, but they apply to every model in the process, for example when policyengine.py loads the US and UK models side by side.
  • Depends on core private names. It imports _branch_clone without a fallback in two modules (simulation.py, tax_benefit_system.py; main's utils/subsample.py already imports it with one) and reads _holders, _memory_storage, _get_exportable_input_periods, _to_person_dataframe and _bind_to_tax_benefit_system. A rename would break import policyengine_uk for every user, not only firm users.
  • Adds about 350 lines to the UK Simulation for binding, cloning, exports, map_result and the situation and dataset builders.

Until core supports root populations, policyengine-uk caps core below the next minor version and runs its firm and core-internals tests against core's master in a separate CI workflow.

Proposal

A first-class root entity type, for example build_entity(..., is_root=True), that:

  • is neither the person entity nor a group, and is not reported as is_person;
  • gets its own population from instantiate_entities;
  • is kept by bind, clone and branch;
  • makes map_result and projections raise across roots;
  • is skipped in person-level exports and subsampling (or exported as its own table);
  • is accepted by SimulationBuilder as its own plural block.

Systems without root entities behave exactly as before. A many-to-many link (person-to-firm jobs) could follow later.

If core would rather not

The fallback in policyengine-uk is a separate firm simulation that shares the parameter tree. It needs no core patches, because firms and households never meet inside a formula, but it loses a single simulation object for reforms that touch both sides.

主要语言
Python
星标
22
派生
31
平均合并
1 天 19 小时
30 天内合并 PR
14

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PolicyEngine/policyengine-core 的其他 Issue

查看 PolicyEngine/policyengine-core 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。