Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/soi-table-1-4-entity-legs.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IRS SOI Table 1.4 now carries the partnership and S corporation net income and
net loss legs (returns and amounts by AGI range) for TY2021 to TY2023. A source
package measure can now declare the years it applies to; TY2020 combines the two
legs, so these eight measures skip it and its record layout keeps its hash.
50 changes: 50 additions & 0 deletions chronicle/source_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,7 @@ def to_record_set_spec(self, year: int) -> SourceRecordSetSpec:
measures = tuple(
_measure_from_mapping(measure, year=year)
for measure in _required(self.payload, "measures", "record_set")
if _mapping_applies_to_year(measure, year=year, context="measure")
)
return SourceRecordSetSpec(
record_set_id=_render_required_string(
Expand Down Expand Up @@ -3861,6 +3862,55 @@ def _required(payload: dict[str, Any], key: str, context: str) -> Any:
return payload[key]


def _mapping_applies_to_year(
payload: dict[str, Any],
*,
year: int,
context: str,
) -> bool:
"""Return whether a declarative mapping applies to the requested year."""
years = payload.get("years")
if years is None:
return True
identifier = payload.get(f"{context}_id")
context_label = f"{context} {identifier!r}" if identifier else context
if not isinstance(years, list) or not years:
raise ValueError(f"{context_label} years must be a non-empty list")
if any(isinstance(item, bool) or not isinstance(item, int) for item in years):
raise ValueError(f"{context_label} years entries must be integers")
if len(set(years)) != len(years):
raise ValueError(f"{context_label} years entries must be unique")
# Every per-year map must cover exactly the declared years: a map year
# outside ``years`` would drop the mapping at that year without an error.
by_year_keys = sorted(
key for key in payload if isinstance(key, str) and key.endswith("_by_year")
)
if "column_by_year" in by_year_keys:
by_year_keys.remove("column_by_year")
by_year_keys.insert(0, "column_by_year")
for key in by_year_keys:
mapped = payload[key]
if not isinstance(mapped, dict):
raise ValueError(f"{context_label} {key} must map years to values")
for declared_year in years:
if declared_year not in mapped and str(declared_year) not in mapped:
raise ValueError(
f"{context_label} {key} has no entry for "
f"declared year {declared_year}"
)
extra = sorted(
str(mapped_year)
for mapped_year in mapped
if str(mapped_year) not in {str(item) for item in years}
)
if extra:
raise ValueError(
f"{context_label} {key} maps year(s) {', '.join(extra)} "
f"outside its years {years}"
)
return year in years


def _year_mapping(files_by_year: dict[Any, Any], year: int) -> dict[str, str]:
if year in files_by_year:
return files_by_year[year]
Expand Down
14 changes: 14 additions & 0 deletions docs/agent-source-package-harness.md
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,20 @@ where HMRC writes "the" — Chronicle's UK register gives one name where it
carries the identifier (chronicle#281), and the bundle warns about the areas it
does not carry yet.

### Measures that apply to some years

A measure may list `years`. A build at one of those years includes it; a build
at any other year compiles the record set without it, so that year's record
layout, and its hash, match a package without the measure. Table 1.4 uses this
for its partnership and S corporation legs, which SOI publishes separately from
TY2021 and combined in TY2020.

The list must be non-empty and hold unique integers, and every `*_by_year` map
the measure declares (`column_by_year`, `expected_column_header_by_year`) must
cover exactly those years. A `years` entry with no map entry, or a map entry
outside `years`, refuses the build, so a new year added to the maps alone
cannot drop the measure without an error.

### Pinned artifacts and year labels

`artifact.artifact_year` pins the file. A package with `artifact_year: 2022`
Expand Down
145 changes: 144 additions & 1 deletion packages/irs_soi/table_1_4/source_package.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schema_version: ledger.source_package.v1
package_id: soi-table-1-4
label: IRS SOI Publication 1304 Table 1.4 total wages
label: IRS SOI Publication 1304 Table 1.4 income items by AGI class
dimension_labels:
'agi_lower_usd': 'Adjusted gross income band lower bound'
'agi_upper_usd': 'Adjusted gross income band upper bound'
Expand Down Expand Up @@ -883,3 +883,146 @@ record_sets:
(PolicyEngine/populace#451 item 2).
legal_vintage: tax_year_{year}
value_scale: 1000
# Table 1.4 publishes separate partnership and S corporation legs from
# TY2021 onward. TY2020 combines them, so these measures are deliberately
# scoped away from 2020 rather than duplicating the combined values.
- measure_id: partnership_net_income_returns
label: Returns with partnership net income
ordinal: 29
years: [2021, 2022, 2023]
column: BP
column_by_year:
2021: BD
2022: BP
2023: BP
expected_column_header_row: 8
expected_column_header_by_year:
2021: 55
2022: 67
2023: 67
concept: irs_soi.returns_with_partnership_net_income
unit: count
aggregation: sum
- measure_id: partnership_net_income_amount
label: Partnership net income
ordinal: 30
years: [2021, 2022, 2023]
column: BQ
column_by_year:
2021: BE
2022: BQ
2023: BQ
expected_column_header_row: 8
expected_column_header_by_year:
2021: 56
2022: 68
2023: 68
concept: irs_soi.partnership_net_income
unit: usd
aggregation: sum
value_scale: 1000
- measure_id: partnership_net_loss_returns
label: Returns with partnership net loss
ordinal: 31
years: [2021, 2022, 2023]
column: BR
column_by_year:
2021: BF
2022: BR
2023: BR
expected_column_header_row: 8
expected_column_header_by_year:
2021: 57
2022: 69
2023: 69
concept: irs_soi.returns_with_partnership_net_loss
unit: count
aggregation: sum
- measure_id: partnership_net_loss_amount
label: Partnership net loss (positive magnitude)
ordinal: 32
years: [2021, 2022, 2023]
column: BS
column_by_year:
2021: BG
2022: BS
2023: BS
expected_column_header_row: 8
expected_column_header_by_year:
2021: 58
2022: 70
2023: 70
concept: irs_soi.partnership_net_loss
unit: usd
aggregation: sum
value_scale: 1000
- measure_id: s_corporation_net_income_returns
label: Returns with S corporation net income
ordinal: 33
years: [2021, 2022, 2023]
column: BT
column_by_year:
2021: BH
2022: BT
2023: BT
expected_column_header_row: 8
expected_column_header_by_year:
2021: 59
2022: 71
2023: 71
concept: irs_soi.returns_with_s_corporation_net_income
unit: count
aggregation: sum
- measure_id: s_corporation_net_income_amount
label: S corporation net income
ordinal: 34
years: [2021, 2022, 2023]
column: BU
column_by_year:
2021: BI
2022: BU
2023: BU
expected_column_header_row: 8
expected_column_header_by_year:
2021: 60
2022: 72
2023: 72
concept: irs_soi.s_corporation_net_income
unit: usd
aggregation: sum
value_scale: 1000
- measure_id: s_corporation_net_loss_returns
label: Returns with S corporation net loss
ordinal: 35
years: [2021, 2022, 2023]
column: BV
column_by_year:
2021: BJ
2022: BV
2023: BV
expected_column_header_row: 8
expected_column_header_by_year:
2021: 61
2022: 73
2023: 73
concept: irs_soi.returns_with_s_corporation_net_loss
unit: count
aggregation: sum
- measure_id: s_corporation_net_loss_amount
label: S corporation net loss (positive magnitude)
ordinal: 36
years: [2021, 2022, 2023]
column: BW
column_by_year:
2021: BK
2022: BW
2023: BW
expected_column_header_row: 8
expected_column_header_by_year:
2021: 62
2022: 74
2023: 74
concept: irs_soi.s_corporation_net_loss
unit: usd
aggregation: sum
value_scale: 1000
17 changes: 11 additions & 6 deletions tests/test_chronicle_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,9 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
# SSA's Supplement Tables 7.B3 and 7.B7 add 193 (128 and 65).
# Historic Table 2's state Schedule 1 line 1 and Schedule A lines 7, 8a
# and 14 add 408 (see above).
"fact_count": 2246063,
# IRS SOI Table 1.4's partnership and S corporation net income and loss legs
# add 160 (eight measures in 20 AGI ranges, TY2023).
"fact_count": 2246223,
# The same change adds 82,064 geography keys: 46,363 Scottish 2022
# output areas (UV404), 35,673 LSOA-level keys from the DESNZ LSOA
# workbooks (Scotland's data zones already exist), the 26 Northern
Expand Down Expand Up @@ -748,7 +750,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
if row_count == 1:
assert row["aggregate_fact_key"].startswith("ledger.aggregate_fact.v2:")
assert row["semantic_fact_key"].startswith("ledger.semantic_fact.v2:")
assert row_count == 2246063
assert row_count == 2246223
assert provenance_classes <= {
"administrative",
"census",
Expand Down Expand Up @@ -783,7 +785,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
"jct-obbba-revenue-estimates-2025",
"jct-tax-expenditures-2024",
]
assert coverage["fact_count"] == 2246063
assert coverage["fact_count"] == 2246223
assert coverage["counts"]["by_source"] == {
"bea": 446,
"bfp_economic_outlook": 5,
Expand All @@ -810,7 +812,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
"hmrc": 40025,
"ici": 12,
"ird": 4362,
"irs_soi": 44371,
"irs_soi": 44531,
"isc": 4,
"jrc_euromod_be": 90,
"kff": 52,
Expand Down Expand Up @@ -2413,6 +2415,8 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
expected_period_counts[key] = expected_period_counts.get(key, 0) + count
# PolicyEngine/microcosm#1418: SPI 2023-24 Table 3.10.
expected_period_counts["tax_year:2023"] += 159
# IRS SOI Table 1.4's partnership and S corporation net income and loss legs, TY2023.
expected_period_counts["tax_year:2023"] += 160
# chronicle#393: the OBR October 2026 EFO packages, by fiscal and calendar year.
for key, count in {
"calendar_year:2024": 1,
Expand Down Expand Up @@ -2903,7 +2907,8 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
== 35
)
# SSA's Supplement Tables 7.B3 and 7.B7 add four (two national cells each).
assert coverage["counts"]["by_geography"]["country:0100000US"] == 2120
# IRS SOI Table 1.4's partnership and S corporation net income and loss legs add 160.
assert coverage["counts"]["by_geography"]["country:0100000US"] == 2280
# 255, less Vintage 2024's state total (chronicle#397), + 16 Census Vintage
# 2025 age groups (15 groups and the total), + 6 from SSA's Supplement
# Tables 7.B3 (four California cells) and 7.B7 (two), + 8 Historic Table 2
Expand Down Expand Up @@ -3034,7 +3039,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path):
"person": 734519,
"return": 16087,
"social_protection_scheme": 36,
"tax_unit": 45676,
"tax_unit": 45836,
}
assert not coverage["duplicates"]["aggregate_fact_keys"]
assert len(coverage["duplicates"]["semantic_fact_keys"]) == 4135
Expand Down
2 changes: 1 addition & 1 deletion tests/test_chronicle_soi_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_build_soi_table_1_4_wage_facts_from_packaged_source():
}

assert validate_facts(facts).valid
assert len(facts) == 580
assert len(facts) == 740
assert (
facts_by_concept_and_range[("irs_soi.returns_with_total_wages", "all")].value
== 128_591_050
Expand Down
Loading
Loading