-
Notifications
You must be signed in to change notification settings - Fork 19
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
add class: SimulationMacroCache #224
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
699898d
WIP: add class SimulationMacroCache
SylviaDu99 fbf018d
WIP: add class SimulationMacroCache
SylviaDu99 c5cdd81
WIP: add class SimulationMacroCache
SylviaDu99 e5449e3
test: Integrate use of sample tax_benefit_system
anth-volk d97a75a
test: Add (non-working) test for set_cache_path
anth-volk 31fba08
fix: Change how HDF5 stores country version
anth-volk 28b282a
WIP: add class SimulationMacroCache
SylviaDu99 0d57f3f
WIP: add class SimulationMacroCache
SylviaDu99 cdf022a
Merge branch 'master' into cache197_1
SylviaDu99 3a6f5c9
WIP: add class SimulationMacroCache
SylviaDu99 47cfcb9
add test for macro cache
SylviaDu99 9ce0861
add test for macro cache
SylviaDu99 28338f2
add check point to exclude DataFrames from macro cache
SylviaDu99 78f09c6
Merge branch 'PolicyEngine:master' into cache197_1
SylviaDu99 7c325fa
fix: minor change in code style
SylviaDu99 126ba26
fix: minor change in code style
SylviaDu99 31ebb51
fix: minor change in code style
SylviaDu99 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 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,4 @@ | ||
- bump: patch | ||
changes: | ||
added: | ||
- Added class SimulationMacroCache for macro simulation caching purposes. |
This file contains 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,84 @@ | ||
import shutil | ||
from pathlib import Path | ||
import h5py | ||
from numpy.typing import ArrayLike | ||
import importlib.metadata | ||
|
||
from policyengine_core.taxbenefitsystems import TaxBenefitSystem | ||
|
||
|
||
class Singleton(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super(Singleton, cls).__call__( | ||
*args, **kwargs | ||
) | ||
return cls._instances[cls] | ||
|
||
|
||
class SimulationMacroCache(metaclass=Singleton): | ||
def __init__(self, tax_benefit_system: TaxBenefitSystem): | ||
self.core_version = importlib.metadata.version("policyengine-core") | ||
self.country_package_metadata = ( | ||
tax_benefit_system.get_package_metadata() | ||
) | ||
self.country_version = self.country_package_metadata["version"] | ||
self.cache_folder_path = None | ||
self.cache_file_path = None | ||
|
||
def set_cache_path( | ||
self, | ||
parent_path: [Path, str], | ||
dataset_name: str, | ||
variable_name: str, | ||
period: str, | ||
branch_name: str, | ||
): | ||
storage_folder = Path(parent_path) / f"{dataset_name}_variable_cache" | ||
self.cache_folder_path = storage_folder | ||
storage_folder.mkdir(exist_ok=True) | ||
self.cache_file_path = ( | ||
storage_folder / f"{variable_name}_{period}_{branch_name}.h5" | ||
) | ||
|
||
def set_cache_value(self, cache_file_path: Path, value: ArrayLike): | ||
with h5py.File(cache_file_path, "w") as f: | ||
f.create_dataset( | ||
"metadata:core_version", | ||
data=self.core_version, | ||
) | ||
f.create_dataset( | ||
"metadata:country_version", | ||
data=self.country_version, | ||
) | ||
f.create_dataset("values", data=value) | ||
|
||
def get_cache_path(self): | ||
return self.cache_file_path | ||
|
||
def get_cache_value(self, cache_file_path: Path): | ||
with h5py.File(cache_file_path, "r") as f: | ||
# Validate both core version and country package metadata are uptodate, otherwise flush the cache | ||
anth-volk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
"metadata:core_version" in f | ||
and "metadata:country_version" in f | ||
): | ||
if ( | ||
f["metadata:core_version"][()].decode("utf-8") | ||
!= self.core_version | ||
or f["metadata:country_version"][()].decode("utf-8") | ||
!= self.country_version | ||
): | ||
f.close() | ||
self.clear_cache(self.cache_folder_path) | ||
return None | ||
else: | ||
f.close() | ||
self.clear_cache(self.cache_folder_path) | ||
return None | ||
return f["values"][()] | ||
|
||
def clear_cache(self, cache_folder_path: Path): | ||
shutil.rmtree(cache_folder_path) |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry- could you explain why we need this?