From 59dd51e8ff6d49a56310d1deddfbe825dab6c236 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Fri, 29 Nov 2024 18:30:55 +0100 Subject: [PATCH 1/4] fix: Display pooled 3-year CPS as dataset for state-level runs --- .../data/reformDefinitionCode.test.js | 49 ++++++++++++++++- src/data/reformDefinitionCode.js | 53 ++++++++++--------- 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/src/__tests__/data/reformDefinitionCode.test.js b/src/__tests__/data/reformDefinitionCode.test.js index a73e7733e..35e28e1aa 100644 --- a/src/__tests__/data/reformDefinitionCode.test.js +++ b/src/__tests__/data/reformDefinitionCode.test.js @@ -211,7 +211,7 @@ describe("Test getSituationCode", () => { }); describe("Test getImplementationCode", () => { test("If not a policy type, return empty array", () => { - const output = getImplementationCode("household", "us", 2024); + const output = getImplementationCode("household", "us", metadataUS, 2024); expect(output).toBeInstanceOf(Array); expect(output.length).toBe(0); }); @@ -219,13 +219,23 @@ describe("Test getImplementationCode", () => { const output = getImplementationCode( "policy", "uk", + metadataUK, 2024, baselinePolicyUK, ); expect(output).toBeInstanceOf(Array); + // Ensure we don't code for reversed baseline and reform expect(output).not.toContain( "baseline = Microsimulation(reform=baseline_reform)", ); + // Ensure we don't code for state-level runs + expect(output).not.toContain( + 'baseline = Microsimulation(dataset="pooled_3_year_cps_2023")', + ); + // And ensure we don't code for both simultaneously + expect(output).not.toContain( + 'baseline = Microsimulation(reform=baseline_reform, dataset="pooled_3_year_cps_2023")', + ); }); test("If set baseline, return lines with baseline", () => { let testPolicy = JSON.parse(JSON.stringify(baselinePolicyUK)); @@ -242,10 +252,45 @@ describe("Test getImplementationCode", () => { id: 1, }, }; - const output = getImplementationCode("policy", "uk", 2024, testPolicy); + const output = getImplementationCode( + "policy", + "uk", + metadataUK, + 2024, + testPolicy, + ); expect(output).toBeInstanceOf(Array); expect(output).toContain("baseline = Microsimulation(reform=baseline)"); }); + test("If US state, return lines with pooled 3-year CPS dataset", () => { + let testPolicy = JSON.parse(JSON.stringify(baselinePolicyUS)); + testPolicy = { + ...testPolicy, + reform: { + data: { + "sample.reform.item": { + "2020.01.01": true, + "2022.01.01": true, + }, + }, + }, + }; + + const output = getImplementationCode( + "policy", + "ca", + metadataUS, + 2024, + testPolicy, + ); + expect(output).toBeInstanceOf(Array); + expect(output).toContain( + 'baseline = Microsimulation(dataset="pooled_3_year_cps_2023")', + ); + expect(output).toContain( + 'reformed = Microsimulation(reform=reform, dataset="pooled_3_year_cps_2023")', + ); + }); test("If dataset provided, return lines with dataset", () => { const output = getImplementationCode( "policy", diff --git a/src/data/reformDefinitionCode.js b/src/data/reformDefinitionCode.js index 02959ba6b..8a4b046f0 100644 --- a/src/data/reformDefinitionCode.js +++ b/src/data/reformDefinitionCode.js @@ -28,7 +28,7 @@ export function getReproducibilityCodeBlock( householdInput, earningVariation, ), - ...getImplementationCode(type, region, year, policy, dataset), + ...getImplementationCode(type, region, metadata, year, policy, dataset), ]; } @@ -160,10 +160,13 @@ export function getSituationCode( export function getImplementationCode( type, region, + metadata, timePeriod, policy, dataset, ) { + const countryId = metadata.economy_options.region[0].name; + if (type !== "policy") { return []; } @@ -176,34 +179,34 @@ export function getImplementationCode( const hasDatasetSpecified = Object.keys(DEFAULT_DATASETS).includes(dataset) || region === "enhanced_us"; - let formattedDataset = null; - if (region === "enhanced_us") { - formattedDataset = "enhanced_cps_2024"; - } else if (hasDatasetSpecified) { - formattedDataset = DEFAULT_DATASETS[dataset]; - } + // TODO: Check into how DEFAULT_DATASETS works now post-rebase + + const isState = + countryId === "us" && + region !== "us" && + region !== "enhanced_us" && + !Object.keys(DEFAULT_DATASETS).includes(region); + + let dataset = ""; + if (hasDatasetSpecified) { + dataset = DEFAULT_DATASETS[region]; + } else if (isState) { + dataset = "pooled_3_year_cps_2023"; + } + + const datasetSpecifier = dataset ? `dataset="${dataset}"` : ""; + + const baselineSpecifier = hasBaseline ? "reform=baseline" : ""; + const baselineComma = hasBaseline && dataset ? ", " : ""; + + const reformSpecifier = hasReform ? "reform=reform" : ""; + const reformComma = hasReform && dataset ? ", " : ""; return [ "", "", - `baseline = Microsimulation(${ - hasDatasetSpecified && hasBaseline - ? `reform=baseline, dataset='${formattedDataset}'` - : hasBaseline - ? `reform=baseline` - : hasDatasetSpecified - ? `dataset='${formattedDataset}'` - : "" - })`, - `reformed = Microsimulation(${ - hasDatasetSpecified && hasReform - ? `reform=reform, dataset='${formattedDataset}'` - : hasReform - ? `reform=reform` - : hasDatasetSpecified - ? `dataset='${formattedDataset}'` - : "" - })`, + `baseline = Microsimulation(${baselineSpecifier}${baselineComma}${datasetSpecifier})`, + `reformed = Microsimulation(${reformSpecifier}${reformComma}${datasetSpecifier})`, `baseline_income = baseline.calculate("household_net_income", period=${timePeriod || defaultYear})`, `reformed_income = reformed.calculate("household_net_income", period=${timePeriod || defaultYear})`, "difference_income = reformed_income - baseline_income", From b054a139ac01b50b044b9425bc0e01a01b1ccffe Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Fri, 6 Dec 2024 14:47:32 +0100 Subject: [PATCH 2/4] fix: Closer to fixing merge conflict --- src/data/reformDefinitionCode.js | 39 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/data/reformDefinitionCode.js b/src/data/reformDefinitionCode.js index 8a4b046f0..616a2128a 100644 --- a/src/data/reformDefinitionCode.js +++ b/src/data/reformDefinitionCode.js @@ -179,28 +179,27 @@ export function getImplementationCode( const hasDatasetSpecified = Object.keys(DEFAULT_DATASETS).includes(dataset) || region === "enhanced_us"; - // TODO: Check into how DEFAULT_DATASETS works now post-rebase + const COUNTRY_LEVEL_US_REGIONS = ["us", "enhanced_us"]; const isState = - countryId === "us" && - region !== "us" && - region !== "enhanced_us" && - !Object.keys(DEFAULT_DATASETS).includes(region); - - let dataset = ""; - if (hasDatasetSpecified) { - dataset = DEFAULT_DATASETS[region]; - } else if (isState) { - dataset = "pooled_3_year_cps_2023"; - } - - const datasetSpecifier = dataset ? `dataset="${dataset}"` : ""; - - const baselineSpecifier = hasBaseline ? "reform=baseline" : ""; - const baselineComma = hasBaseline && dataset ? ", " : ""; - - const reformSpecifier = hasReform ? "reform=reform" : ""; - const reformComma = hasReform && dataset ? ", " : ""; + countryId === "us" && !COUNTRY_LEVEL_US_REGIONS.includes(region); + + let datasetText = ""; + if (hasDatasetSpecified) { + datasetText = DEFAULT_DATASETS[dataset]; + } else if (isState) { + datasetText = "pooled_3_year_cps_2023"; + } else if (region === "enhanced_us") { + datasetText = "enhanced_cps_2024"; + } + + const datasetSpecifier = datasetText ? `dataset="${datasetText}"` : ""; + + const baselineSpecifier = hasBaseline ? "reform=baseline" : ""; + const baselineComma = hasBaseline && datasetText ? ", " : ""; + + const reformSpecifier = hasReform ? "reform=reform" : ""; + const reformComma = hasReform && datasetText ? ", " : ""; return [ "", From 9ed38384d370cfe929c24217d7f1a58fe599e49b Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Fri, 6 Dec 2024 15:04:00 +0100 Subject: [PATCH 3/4] fix: Fix display of enhanced_us --- src/data/reformDefinitionCode.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/data/reformDefinitionCode.js b/src/data/reformDefinitionCode.js index 616a2128a..853d43fa0 100644 --- a/src/data/reformDefinitionCode.js +++ b/src/data/reformDefinitionCode.js @@ -174,10 +174,8 @@ export function getImplementationCode( const hasBaseline = Object.keys(policy?.baseline?.data).length > 0; const hasReform = Object.keys(policy?.reform?.data).length > 0; - // Check if the region has a dataset specified; enhanced_us is legacy implemntation - // whereby enhanced_us region correlated with enhanced_cps dataset - const hasDatasetSpecified = - Object.keys(DEFAULT_DATASETS).includes(dataset) || region === "enhanced_us"; + // Check if the region has a dataset specified + const hasDatasetSpecified = Object.keys(DEFAULT_DATASETS).includes(dataset); const COUNTRY_LEVEL_US_REGIONS = ["us", "enhanced_us"]; @@ -185,6 +183,9 @@ export function getImplementationCode( countryId === "us" && !COUNTRY_LEVEL_US_REGIONS.includes(region); let datasetText = ""; + + // enhanced_us is legacy implemntation + // whereby enhanced_us region correlated with enhanced_cps dataset if (hasDatasetSpecified) { datasetText = DEFAULT_DATASETS[dataset]; } else if (isState) { From aba9ccba0a37c335385b80b54c22c10ef840d609 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Fri, 6 Dec 2024 15:13:49 +0100 Subject: [PATCH 4/4] fix: Update tests and add additional for new possible case --- .../data/reformDefinitionCode.test.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/__tests__/data/reformDefinitionCode.test.js b/src/__tests__/data/reformDefinitionCode.test.js index 35e28e1aa..c99347c6b 100644 --- a/src/__tests__/data/reformDefinitionCode.test.js +++ b/src/__tests__/data/reformDefinitionCode.test.js @@ -282,6 +282,7 @@ describe("Test getImplementationCode", () => { metadataUS, 2024, testPolicy, + null, ); expect(output).toBeInstanceOf(Array); expect(output).toContain( @@ -295,13 +296,28 @@ describe("Test getImplementationCode", () => { const output = getImplementationCode( "policy", "us", + metadataUS, + 2024, + baselinePolicyUS, + "enhanced_cps", + ); + expect(output).toBeInstanceOf(Array); + expect(output).toContain( + 'baseline = Microsimulation(dataset="enhanced_cps_2024")', + ); + }); + test("If dataset provided alongside US state, return lines with dataset, not Pooled 3-Year CPS", () => { + const output = getImplementationCode( + "policy", + "ks", + metadataUS, 2024, baselinePolicyUS, "enhanced_cps", ); expect(output).toBeInstanceOf(Array); expect(output).toContain( - "baseline = Microsimulation(dataset='enhanced_cps_2024')", + 'baseline = Microsimulation(dataset="enhanced_cps_2024")', ); }); });