-
Notifications
You must be signed in to change notification settings - Fork 8
Redo data setting in simulation #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c735db0
feat: Redo data setting in simulation
anth-volk 1e10d56
chore: Lint and changelog
anth-volk 23d3fab
fix: Reallow arbitrary dict passage to Simulation(data)
anth-volk bc16cb4
fix: Properly check for dict type before running _set_data
anth-volk f1c9434
chore: Remove unneeded print statement
anth-volk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| - bump: patch | ||
| changes: | ||
| changed: | ||
| - Disambiguated filepath management in Simulation._set_data() | ||
| - Refactored Simulation._set_data() to divide functionality into smaller methods | ||
| - Prevented passage of non-Path URIs to Dataset.from_file() at end of Simulation._set_data() execution | ||
| added: | ||
| - Tests for Simulation._set_data() |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Mainly simulation options and parameters.""" | ||
|
|
||
| from typing import Tuple, Optional | ||
|
|
||
| EFRS_2022 = "gs://policyengine-uk-data-private/enhanced_frs_2022_23.h5" | ||
| FRS_2022 = "gs://policyengine-uk-data-private/frs_2022_23.h5" | ||
| CPS_2023 = "gs://policyengine-us-data/cps_2023.h5" | ||
| CPS_2023_POOLED = "gs://policyengine-us-data/pooled_3_year_cps_2023.h5" | ||
| ECPS_2024 = "gs://policyengine-us-data/ecps_2024.h5" | ||
|
|
||
| POLICYENGINE_DATASETS = [ | ||
| EFRS_2022, | ||
| FRS_2022, | ||
| CPS_2023, | ||
| CPS_2023_POOLED, | ||
| ECPS_2024, | ||
| ] | ||
|
|
||
| # Contains datasets that map to particular time_period values | ||
| DATASET_TIME_PERIODS = { | ||
| CPS_2023: 2023, | ||
| CPS_2023_POOLED: 2023, | ||
| ECPS_2024: 2023, | ||
| } | ||
|
|
||
|
|
||
| def get_default_dataset( | ||
| country: str, region: str, version: Optional[str] = None | ||
| ) -> str: | ||
| if country == "uk": | ||
| return EFRS_2022 | ||
| elif country == "us": | ||
| if region is not None and region != "us": | ||
| return CPS_2023_POOLED | ||
| else: | ||
| return CPS_2023 | ||
|
|
||
| raise ValueError( | ||
| f"Unable to select a default dataset for country {country} and region {region}." | ||
| ) | ||
|
|
||
|
|
||
| def process_gs_path(path: str) -> Tuple[str, str]: | ||
| """Process a GS path to return bucket and object.""" | ||
| if not path.startswith("gs://"): | ||
| raise ValueError(f"Invalid GS path: {path}") | ||
|
|
||
| path = path[5:] # Remove 'gs://' | ||
| bucket, obj = path.split("/", 1) | ||
| return bucket, obj | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from policyengine.simulation import SimulationOptions | ||
| from unittest.mock import patch, Mock | ||
| import pytest | ||
| from policyengine.utils.data.datasets import CPS_2023 | ||
|
|
||
| non_data_uk_sim_options = { | ||
| "country": "uk", | ||
| "scope": "macro", | ||
| "region": "uk", | ||
| "time_period": 2025, | ||
| "reform": None, | ||
| "baseline": None, | ||
| } | ||
|
|
||
| non_data_us_sim_options = { | ||
| "country": "us", | ||
| "scope": "macro", | ||
| "region": "us", | ||
| "time_period": 2025, | ||
| "reform": None, | ||
| "baseline": None, | ||
| } | ||
|
|
||
| uk_sim_options_no_data = SimulationOptions.model_validate( | ||
| { | ||
| **non_data_uk_sim_options, | ||
| "data": None, | ||
| } | ||
| ) | ||
|
|
||
| us_sim_options_cps_dataset = SimulationOptions.model_validate( | ||
| {**non_data_us_sim_options, "data": CPS_2023} | ||
| ) | ||
|
|
||
| SAMPLE_DATASET_FILENAME = "sample_value.h5" | ||
| SAMPLE_DATASET_BUCKET_NAME = "policyengine-uk-data-private" | ||
| SAMPLE_DATASET_URI_PREFIX = "gs://" | ||
| SAMPLE_DATASET_FILE_ADDRESS = f"{SAMPLE_DATASET_URI_PREFIX}{SAMPLE_DATASET_BUCKET_NAME}/{SAMPLE_DATASET_FILENAME}" | ||
|
|
||
| uk_sim_options_pe_dataset = SimulationOptions.model_validate( | ||
| {**non_data_uk_sim_options, "data": SAMPLE_DATASET_FILE_ADDRESS} | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_get_default_dataset(): | ||
| with patch( | ||
| "policyengine.simulation.get_default_dataset", | ||
| return_value=SAMPLE_DATASET_FILE_ADDRESS, | ||
| ) as mock_get_default_dataset: | ||
| yield mock_get_default_dataset | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_dataset(): | ||
| """Simple Dataset mock fixture""" | ||
| with patch("policyengine.simulation.Dataset") as mock_dataset_class: | ||
| mock_instance = Mock() | ||
| # Set file_path to mimic Dataset's behavior of clipping URI and bucket name from GCS paths | ||
| mock_instance.from_file = Mock() | ||
| mock_instance.file_path = SAMPLE_DATASET_FILENAME | ||
| mock_dataset_class.from_file.return_value = mock_instance | ||
| yield mock_instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from .fixtures.simulation import ( | ||
| uk_sim_options_no_data, | ||
| uk_sim_options_pe_dataset, | ||
| us_sim_options_cps_dataset, | ||
| mock_get_default_dataset, | ||
| mock_dataset, | ||
| SAMPLE_DATASET_FILENAME, | ||
| ) | ||
| import sys | ||
| from copy import deepcopy | ||
|
|
||
| from policyengine import Simulation | ||
|
|
||
|
|
||
| class TestSimulation: | ||
| class TestSetData: | ||
| def test__given_no_data_option__sets_default_dataset( | ||
| self, mock_get_default_dataset, mock_dataset | ||
| ): | ||
|
|
||
| # Don't run entire init script | ||
| sim = object.__new__(Simulation) | ||
| sim.options = deepcopy(uk_sim_options_no_data) | ||
| sim._set_data(uk_sim_options_no_data.data) | ||
|
|
||
| assert str(sim.options.data.file_path) == SAMPLE_DATASET_FILENAME | ||
|
|
||
| def test__given_pe_dataset__sets_data_option_to_dataset( | ||
| self, mock_dataset | ||
| ): | ||
|
|
||
| sim = object.__new__(Simulation) | ||
| sim.options = deepcopy(uk_sim_options_pe_dataset) | ||
| sim._set_data(uk_sim_options_pe_dataset.data) | ||
|
|
||
| assert str(sim.options.data.file_path) == SAMPLE_DATASET_FILENAME | ||
|
|
||
| def test__given_cps_2023_in_filename__sets_time_period_to_2023( | ||
| self, mock_dataset | ||
| ): | ||
| from policyengine import Simulation | ||
|
|
||
| sim = object.__new__(Simulation) | ||
| sim.options = deepcopy(us_sim_options_cps_dataset) | ||
| sim._set_data(us_sim_options_cps_dataset.data) | ||
|
|
||
| assert mock_dataset.from_file.called_with( | ||
| us_sim_options_cps_dataset.data, time_period=2023 | ||
| ) | ||
|
|
||
| class TestSetDataTimePeriod: | ||
| def test__given_dataset_with_time_period__sets_time_period(self): | ||
| from policyengine import Simulation | ||
|
|
||
| sim = object.__new__(Simulation) | ||
|
|
||
| print("Dataset:", us_sim_options_cps_dataset.data, file=sys.stderr) | ||
| assert ( | ||
| sim._set_data_time_period(us_sim_options_cps_dataset.data) | ||
| == 2023 | ||
| ) | ||
|
|
||
| def test__given_dataset_without_time_period__does_not_set_time_period( | ||
| self, | ||
| ): | ||
| from policyengine import Simulation | ||
|
|
||
| sim = object.__new__(Simulation) | ||
| assert ( | ||
| sim._set_data_time_period(uk_sim_options_pe_dataset.data) | ||
| == None | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.