ml_grid.util.data_cache
Data caching module for ml_grid pipeline.
This module provides efficient data caching using joblib to cache preprocessed data, feature encoders/scalers, and transformed datasets for reuse between experiments. Implements smart cache invalidation based on file checksums/timestamps.
Exceptions
Initialize self. See help(type(self)) for accurate signature. |
Classes
Initialize the data cache system. |
|
Initialize cached pipeline instance. |
|
Initialize CachedDataPipeline wrapper. |
Functions
|
Decorator to add cache validation before method execution. |
|
Helper function to get cache statistics. |
|
Helper function to clear cache for an experiment. |
Module Contents
- exception ml_grid.util.data_cache.CacheValidationError[source]
Bases:
ExceptionInitialize self. See help(type(self)) for accurate signature.
- class ml_grid.util.data_cache.DataCache(base_cache_dir: str | None = None)[source]
Initialize the data cache system.
- property cache_dir: pathlib.Path[source]
Get the current experiment-specific cache directory.
- get_cache_path(experiment_name: str, run_id: str) pathlib.Path[source]
Get full path for a cached item.
- Parameters:
experiment_name – Name of the experiment
run_id – Unique identifier for this specific run
- Returns:
Full path to cache file
- save(experiment_name: str, run_id: str, data_dict: Dict[str, Any], metadata: Dict[str, Any] | None = None) bool[source]
Save data dictionary to cache.
- Parameters:
experiment_name – Name of the experiment
run_id – Unique identifier for this run
data_dict – Dictionary of data to cache (X_train, X_test, scaler, etc.)
metadata – Optional metadata to store
- Returns:
True if save was successful, False otherwise
- load(experiment_name: str, run_id: str) Dict[str, Any] | None[source]
Load cached data.
- Parameters:
experiment_name – Name of the experiment
run_id – Unique identifier for this run
- Returns:
Cached data dictionary or None if not found/invalid
- invalidate(experiment_name: str, run_id: str) bool[source]
Invalidate (delete) a specific cache entry.
- Parameters:
experiment_name – Name of the experiment
run_id – Unique identifier for this run
- Returns:
True if invalidated successfully
- class ml_grid.util.data_cache.CachedPipelineInstance(experiment_name: str, run_id: str, cache: DataCache, file_path: str, local_param_dict: Dict[str, Any], original_kwargs: Dict[str, Any], original_class: Any)[source]
Initialize cached pipeline instance.
- class ml_grid.util.data_cache.CachedDataPipeline(original_pipeline_class: Any, cache: DataCache | None = None)[source]
Initialize CachedDataPipeline wrapper.
- Parameters:
original_pipeline_class – The original pipe class to wrap
cache – DataCache instance (creates new if None)
- create_cached_pipeline(experiment_name: str, run_id: str, file_path: str, local_param_dict: Dict[str, Any], original_kwargs: Dict[str, Any]) CachedPipelineInstance[source]
Create a cached pipeline instance.
- Parameters:
experiment_name – Name of the experiment
run_id – Run identifier
file_name – Path to input data
local_param_dict – Pipeline parameters
original_kwargs – Original __init__ arguments
- Returns:
CachedPipelineInstance that uses cached data when available
- ml_grid.util.data_cache.with_cache_validation(func: Callable) Callable[source]
Decorator to add cache validation before method execution.