|
| 1 | +from policyengine_uk.data import UKMultiYearDataset, UKSingleYearDataset |
| 2 | +from policyengine_uk.data.economic_assumptions import apply_uprating |
| 3 | +from policyengine_uk import Microsimulation |
| 4 | +from policyengine_uk_data.storage import STORAGE_FOLDER |
| 5 | +from policyengine_core.data import Dataset |
| 6 | + |
| 7 | + |
| 8 | +def convert_legacy_to_multi_year_dataset( |
| 9 | + file_path: str, |
| 10 | + new_file_path: str, |
| 11 | + start_year: int = 2023, |
| 12 | + end_year: int = 2029, |
| 13 | +) -> UKMultiYearDataset: |
| 14 | + """ |
| 15 | + Convert a legacy single year dataset to a multi-year dataset. |
| 16 | + """ |
| 17 | + sim = Microsimulation(dataset=Dataset.from_file(file_path)) |
| 18 | + |
| 19 | + dataset = UKSingleYearDataset.from_simulation(sim, fiscal_year=start_year) |
| 20 | + dataset.time_period = str(start_year) |
| 21 | + |
| 22 | + datasets = [dataset] |
| 23 | + |
| 24 | + for year in range(start_year + 1, end_year + 1): |
| 25 | + dataset = dataset.copy() |
| 26 | + dataset.time_period = str(year) |
| 27 | + |
| 28 | + multi_year_dataset = UKMultiYearDataset( |
| 29 | + datasets=datasets, |
| 30 | + ) |
| 31 | + multi_year_dataset = apply_uprating(multi_year_dataset) |
| 32 | + |
| 33 | + multi_year_dataset.save(new_file_path) |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + file_paths = [ |
| 38 | + STORAGE_FOLDER / "frs_2023_24.h5", |
| 39 | + STORAGE_FOLDER / "enhanced_frs_2023_24.h5", |
| 40 | + ] |
| 41 | + out_file_paths = [ |
| 42 | + STORAGE_FOLDER / "frs_2023_29.h5", |
| 43 | + STORAGE_FOLDER / "enhanced_frs_2023_29.h5", |
| 44 | + ] |
| 45 | + |
| 46 | + for file_path, new_file_path in zip(file_paths, out_file_paths): |
| 47 | + convert_legacy_to_multi_year_dataset( |
| 48 | + file_path=str(file_path), |
| 49 | + new_file_path=str(new_file_path), |
| 50 | + start_year=2023, |
| 51 | + end_year=2029, |
| 52 | + ) |
| 53 | + print(f"Converted {file_path} to {new_file_path}") |
0 commit comments