CachedFilter
framework3.plugins.filters.cache.cached_filter
¶
__all__ = ['Cached']
module-attribute
¶
Cached
¶
Bases: BaseFilter
A filter that manages the storage of models and data in a BaseStorage type.
This class extends BaseFilter to provide caching capabilities for both the filter model and the processed data. It allows for efficient reuse of previously computed results and trained models.
Key Features
- Caches both filter models and processed data
- Supports various storage backends through BaseStorage
- Allows for overwriting existing cached data
- Provides methods for managing the cache
Usage
The Cached filter can be used to wrap any BaseFilter, providing caching capabilities:
from framework3.storage import LocalStorage
from framework3.container import Container
from your_custom_filter import CustomFilter
# Configure storage
Container.storage = LocalStorage(storage_path='cache')
# Create a custom filter and wrap it with Cached
custom_filter = CustomFilter()
cached_filter = Cached(
filter=custom_filter,
cache_data=True,
cache_filter=True,
overwrite=False
)
# Use the cached filter
X = XYData(_hash='input_data', _path='/datasets', _value=input_data)
y = XYData(_hash='target_data', _path='/datasets', _value=target_data)
cached_filter.fit(X, y)
predictions = cached_filter.predict(X)
# Clear the cache if needed
cached_filter.clear_cache()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter
|
BaseFilter
|
The underlying filter to be cached. |
required |
cache_data
|
bool
|
Whether to cache the processed data. |
True
|
cache_filter
|
bool
|
Whether to cache the trained filter. |
True
|
overwrite
|
bool
|
Whether to overwrite existing cached data/models. |
False
|
storage
|
BaseStorage | None
|
The storage backend for caching. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
filter |
BaseFilter
|
The underlying filter being cached. |
cache_data |
bool
|
Flag indicating whether to cache processed data. |
cache_filter |
bool
|
Flag indicating whether to cache the trained filter. |
overwrite |
bool
|
Flag indicating whether to overwrite existing cached data/models. |
_storage |
BaseStorage
|
The storage backend used for caching. |
_lambda_filter |
Callable[..., BaseFilter] | None
|
Lambda function for lazy loading of cached filter. |
Methods:
Name | Description |
---|---|
init |
Initialize the cached filter. |
fit |
XYData, y: Optional[XYData]): Fit the filter to the input data, caching the model if necessary. |
predict |
XYData) -> XYData: Make predictions using the filter, caching the results if necessary. |
clear_cache |
Clear the cache in the storage. |
Note
The caching behavior can be customized by adjusting the cache_data, cache_filter, and overwrite parameters. The storage backend can be changed by providing a different BaseStorage implementation.
Source code in framework3/plugins/filters/cache/cached_filter.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
cache_data = cache_data
instance-attribute
¶
cache_filter = cache_filter
instance-attribute
¶
filter = filter
instance-attribute
¶
overwrite = overwrite
instance-attribute
¶
__init__(filter, cache_data=True, cache_filter=True, overwrite=False, storage=None)
¶
Initialize a new Cached filter instance.
This constructor sets up the Cached filter with the specified parameters and initializes the underlying filter and storage backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter
|
BaseFilter
|
The underlying filter to be cached. |
required |
cache_data
|
bool
|
Whether to cache the processed data. Defaults to True. |
True
|
cache_filter
|
bool
|
Whether to cache the trained filter. Defaults to True. |
True
|
overwrite
|
bool
|
Whether to overwrite existing cached data/models. Defaults to False. |
False
|
storage
|
BaseStorage | None
|
The storage backend for caching. If None, uses the Container's storage. Defaults to None. |
None
|
Note
If no storage is provided, the method will use the storage defined in the Container. The _lambda_filter attribute is initialized as None and will be set later if needed.
Source code in framework3/plugins/filters/cache/cached_filter.py
clear_cache()
¶
Clear the cache in the storage.
This method should implement the logic to clear all cached data and models associated with this filter from the storage backend.
Note
This method is not yet implemented.
Source code in framework3/plugins/filters/cache/cached_filter.py
fit(x, y)
¶
Fit the filter to the input data, caching the model if necessary.
This method checks if a cached model exists and uses it if available. If not, it trains the model and caches it if caching is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
XYData
|
The input data. |
required |
y
|
Optional[XYData]
|
The target data, if any. |
required |
Source code in framework3/plugins/filters/cache/cached_filter.py
init()
¶
Initialize the cached filter.
This method initializes both the underlying filter and the Cached filter itself.
predict(x)
¶
Make predictions using the filter, caching the results if necessary.
This method checks if cached predictions exist and uses them if available. If not, it makes new predictions and caches them if caching is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
XYData
|
The input data for prediction. |
required |
Returns:
Name | Type | Description |
---|---|---|
XYData |
XYData
|
The prediction results. |