Population.__call__ passes unweighted=True to calculate_add and calculate_divide, which reject it

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend, testing

调研方向

Start with policyengine_core/populations/population.py around the Population.__call__ argument handling, then inspect Simulation.calculate_add and calculate_divide in policyengine_core/simulations/simulation.py. Reproduce with the described UKStyleMicrosimulation fixture and cover ADD and DIVIDE reads both directly and through formulas. Done when those reads no longer raise the unexpected unweighted TypeError and the regression tests pass.

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

描述

When a simulation class has "micro" in its name and doesn't subclass core's Microsimulation, Population.__call__ adds unweighted=True to a read's keyword arguments (population.py#L152-L154). It sends a read with options=[ADD] or options=[DIVIDE] to calculate_add or calculate_divide with those arguments (#L157-L164). Simulation.calculate_add and calculate_divide take no unweighted argument (simulation.py#L2159-L2164, #L2237-L2242), so Python rejects the call:

TypeError: Simulation.calculate_add() got an unexpected keyword argument 'unweighted'

policyengine-uk's Microsimulation meets both conditions: it subclasses policyengine-uk's Simulation, a core Simulation (microsimulation.py#L14). Its calculate takes unweighted (#L41-L58). policyengine-uk's calculate_add and calculate_divide forward their keyword arguments to core (simulation.py#L1961-L1973). So a formula that read with ADD or DIVIDE would raise the TypeError in a policyengine-uk Microsimulation. We read policyengine-uk's code for this and ran no policyengine-uk simulation.

Nothing fails today. No formula on policyengine-uk main reads with an option. Two test reads use one (test_stored_draw_input_source.py#L343-L347), and both expect the StoredDrawError that policyengine-uk raises before it calls core.

Reproduction

UKStyleMicrosimulation takes unweighted in calculate, as policyengine-uk's class does. Run this from the root of a policyengine-core checkout that has tests/fixtures/microsimulation_option_reads.py:

from policyengine_core.country_template import Simulation
from policyengine_core.data import Dataset
from policyengine_core.model_api import ADD, DIVIDE
from tests.fixtures.microsimulation_option_reads import (
    DATASET_YEAR,
    Inputs,
    dataframe,
    tax_benefit_system,
)


class UKStyleMicrosimulation(Simulation):
    def calculate(
        self,
        variable_name,
        period=None,
        map_to=None,
        decode_enums=False,
        unweighted=False,
    ):
        return super().calculate(
            variable_name, period, map_to=map_to, decode_enums=decode_enums
        )


inputs = Inputs(
    household_of_person=(0, 0, 1),
    household_weight=(10.0, 20.0),
    monthly_pay=((1.0,) * 12, (2.0,) * 12, (5.0,) * 12),
    yearly_grant=(120.0, 240.0, 600.0),
    household_monthly_fee=((0.0,) * 12, (0.0,) * 12),
    household_yearly_grant=(0.0, 0.0),
)


def uk_style():
    return UKStyleMicrosimulation(
        tax_benefit_system=tax_benefit_system(),
        dataset=Dataset.from_dataframe(dataframe(inputs), DATASET_YEAR),
    )


reads = [
    lambda simulation: simulation.populations["person"]("monthly_pay", "2022-06"),
    lambda simulation: simulation.populations["person"](
        "monthly_pay", "2022", options=[ADD]
    ),
    lambda simulation: simulation.populations["person"](
        "yearly_grant", "2022-06", options=[DIVIDE]
    ),
    lambda simulation: simulation.calculate("pay_added", "2022"),
    lambda simulation: simulation.calculate("grant_divided", "2022-06"),
]
for read in reads:
    try:
        print(read(uk_style()))
    except TypeError as error:
        print("TypeError:", error)

At #647's head it printed the lines below. Master's calculate_add and calculate_divide still take no unweighted argument.

[1. 2. 5.]
TypeError: Simulation.calculate_add() got an unexpected keyword argument 'unweighted'
TypeError: Simulation.calculate_divide() got an unexpected keyword argument 'unweighted'
TypeError: Simulation.calculate_add() got an unexpected keyword argument 'unweighted'
TypeError: Simulation.calculate_divide() got an unexpected keyword argument 'unweighted'

The plain read computes. The ADD read, the DIVIDE read and the two formulas that make them, pay_added and grant_divided, raise.

What a fix would change

  • Population.__call__ would pass unweighted=True to calculate alone, and send ADD and DIVIDE reads with decode_enums=False only.
  • Simulation.calculate_add and calculate_divide would then read each term with the class's own calculate. In policyengine-uk's Microsimulation, that method skips weights while the tracer's stack holds a calculation, so a read inside a formula would sum or divide plain arrays.
  • A test would read with ADD and DIVIDE, directly and through a formula, in a Simulation subclass that has "micro" in its name and takes unweighted in calculate. It fails today with the TypeError.
  • #647's signature test checks the keyword calls that Simulation's calculate methods take, and unweighted isn't one of them, so that test doesn't cover this call.
主要语言
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 摘要。