Skip to content

Commit 32254dd

Browse files
committed
Refactor structural mortgage input compatibility
1 parent 9f7ad45 commit 32254dd

5 files changed

Lines changed: 206 additions & 137 deletions

File tree

policyengine_us_data/datasets/cps/extended_cps.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010
from policyengine_us_data.datasets.puf import PUF, PUF_2024
1111
from policyengine_us_data.storage import STORAGE_FOLDER
1212
from policyengine_us_data.utils.mortgage_interest import (
13+
STRUCTURAL_MORTGAGE_VARIABLES,
1314
convert_mortgage_interest_to_structural_inputs,
1415
impute_tax_unit_mortgage_balance_hints,
15-
supports_structural_mortgage_inputs,
1616
)
17+
from policyengine_us_data.utils.policyengine import has_policyengine_us_variables
1718
from policyengine_us_data.utils.retirement_limits import (
1819
get_retirement_limits,
1920
get_se_pension_limits,
2021
)
2122

2223
logger = logging.getLogger(__name__)
2324

25+
26+
def _supports_structural_mortgage_inputs() -> bool:
27+
return has_policyengine_us_variables(*STRUCTURAL_MORTGAGE_VARIABLES)
28+
29+
2430
# CPS-only variables that should be QRF-imputed for the PUF clone half
2531
# instead of naively duplicated from the CPS donor. These are
2632
# income-correlated variables that exist only in the CPS; demographics,
@@ -450,14 +456,15 @@ def generate(self):
450456
)
451457

452458
new_data = self._rename_imputed_to_inputs(new_data)
453-
new_data = impute_tax_unit_mortgage_balance_hints(
454-
new_data,
455-
self.time_period,
456-
)
457-
new_data = convert_mortgage_interest_to_structural_inputs(
458-
new_data,
459-
self.time_period,
460-
)
459+
if _supports_structural_mortgage_inputs():
460+
new_data = impute_tax_unit_mortgage_balance_hints(
461+
new_data,
462+
self.time_period,
463+
)
464+
new_data = convert_mortgage_interest_to_structural_inputs(
465+
new_data,
466+
self.time_period,
467+
)
461468
new_data = self._drop_formula_variables(new_data)
462469
self.save_dataset(new_data)
463470

@@ -492,7 +499,7 @@ def _rename_imputed_to_inputs(cls, data):
492499
@classmethod
493500
def _keep_formula_vars(cls):
494501
keep = set(cls._KEEP_FORMULA_VARS)
495-
if not supports_structural_mortgage_inputs():
502+
if not _supports_structural_mortgage_inputs():
496503
keep.add("interest_deduction")
497504
return keep
498505

policyengine_us_data/datasets/puf/puf.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
disaggregate_aggregate_records,
1414
)
1515
from policyengine_us_data.utils.mortgage_interest import (
16+
STRUCTURAL_MORTGAGE_VARIABLES,
1617
convert_mortgage_interest_to_structural_inputs,
1718
)
19+
from policyengine_us_data.utils.policyengine import has_policyengine_us_variables
1820
from policyengine_us_data.utils.uprating import (
1921
create_policyengine_uprating_factors_table,
2022
)
@@ -650,10 +652,11 @@ def generate(self):
650652
variable: {self.time_period: values}
651653
for variable, values in self.holder.items()
652654
}
653-
holder_tp = convert_mortgage_interest_to_structural_inputs(
654-
holder_tp,
655-
self.time_period,
656-
)
655+
if has_policyengine_us_variables(*STRUCTURAL_MORTGAGE_VARIABLES):
656+
holder_tp = convert_mortgage_interest_to_structural_inputs(
657+
holder_tp,
658+
self.time_period,
659+
)
657660
self.holder = {
658661
variable: values[self.time_period] for variable, values in holder_tp.items()
659662
}

0 commit comments

Comments
 (0)