-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Optuna and KFold to find best strategy for agent (#561)
- Loading branch information
Showing
15 changed files
with
660 additions
and
243 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
48 changes: 44 additions & 4 deletions
48
prediction_market_agent_tooling/tools/caches/inmemory_cache.py
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 |
---|---|---|
@@ -1,17 +1,57 @@ | ||
from functools import cache | ||
from typing import Any, Callable, TypeVar, cast | ||
from typing import Any, Callable, TypeVar, cast, overload | ||
|
||
from joblib import Memory | ||
|
||
from prediction_market_agent_tooling.config import APIKeys | ||
|
||
MEMORY = Memory(APIKeys().CACHE_DIR, verbose=0) | ||
|
||
|
||
T = TypeVar("T", bound=Callable[..., Any]) | ||
|
||
|
||
def persistent_inmemory_cache(func: T) -> T: | ||
@overload | ||
def persistent_inmemory_cache( | ||
func: None = None, | ||
*, | ||
in_memory_cache: bool = True, | ||
) -> Callable[[T], T]: | ||
... | ||
|
||
|
||
@overload | ||
def persistent_inmemory_cache( | ||
func: T, | ||
*, | ||
in_memory_cache: bool = True, | ||
) -> T: | ||
... | ||
|
||
|
||
def persistent_inmemory_cache( | ||
func: T | None = None, | ||
*, | ||
in_memory_cache: bool = True, | ||
) -> T | Callable[[T], T]: | ||
""" | ||
Wraps a function with both file cache (for persistent cache) and in-memory cache (for speed). | ||
Wraps a function with both file cache (for persistent cache) and optional in-memory cache (for speed). | ||
Can be used as @persistent_inmemory_cache or @persistent_inmemory_cache(in_memory_cache=False) | ||
""" | ||
return cast(T, cache(MEMORY.cache(func)) if APIKeys().ENABLE_CACHE else func) | ||
if func is None: | ||
# Ugly Pythonic way to support this decorator as `@persistent_inmemory_cache` but also `@persistent_inmemory_cache(in_memory_cache=False)` | ||
def decorator(func: T) -> T: | ||
return persistent_inmemory_cache( | ||
func, | ||
in_memory_cache=in_memory_cache, | ||
) | ||
|
||
return decorator | ||
else: | ||
# The decorator is called without arguments. | ||
if not APIKeys().ENABLE_CACHE: | ||
return func | ||
cached_func = MEMORY.cache(func) | ||
if in_memory_cache: | ||
cached_func = cache(cached_func) | ||
return cast(T, cached_func) |
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
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.