Skip to content

Commit

Permalink
fix: Display pooled 3-year CPS as dataset for state-level runs
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Dec 5, 2024
1 parent 1111475 commit 59dd51e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 27 deletions.
49 changes: 47 additions & 2 deletions src/__tests__/data/reformDefinitionCode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,31 @@ 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);
});
test("If policy, return lines", () => {
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));
Expand All @@ -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",
Expand Down
53 changes: 28 additions & 25 deletions src/data/reformDefinitionCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function getReproducibilityCodeBlock(
householdInput,
earningVariation,
),
...getImplementationCode(type, region, year, policy, dataset),
...getImplementationCode(type, region, metadata, year, policy, dataset),
];
}

Expand Down Expand Up @@ -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 [];
}
Expand All @@ -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",
Expand Down

0 comments on commit 59dd51e

Please sign in to comment.