removed np_cache
This commit is contained in:
@@ -1,77 +0,0 @@
|
|||||||
from collections import namedtuple
|
|
||||||
from copy import copy
|
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
CacheInfo = namedtuple("CacheInfo", "hits misses size")
|
|
||||||
|
|
||||||
|
|
||||||
def make_arg_hashable(arg):
|
|
||||||
if isinstance(arg, np.ndarray):
|
|
||||||
return arg.tobytes()
|
|
||||||
elif isinstance(arg, list):
|
|
||||||
return tuple(make_arg_hashable(a) for a in arg)
|
|
||||||
return arg
|
|
||||||
|
|
||||||
|
|
||||||
def np_cache(func):
|
|
||||||
def new_cached_func():
|
|
||||||
cache = {}
|
|
||||||
hits = misses = 0
|
|
||||||
|
|
||||||
@wraps(func)
|
|
||||||
def wrapped(*args, **kwargs):
|
|
||||||
nonlocal cache, hits, misses
|
|
||||||
hashable_args = tuple(make_arg_hashable(a) for a in args)
|
|
||||||
hashable_kwargs = tuple({k: make_arg_hashable(a) for k, a in kwargs.items()}.items())
|
|
||||||
key = hash((hashable_args, hashable_kwargs))
|
|
||||||
if key not in cache:
|
|
||||||
misses += 1
|
|
||||||
cache[key] = func(*args, **kwargs)
|
|
||||||
else:
|
|
||||||
hits += 1
|
|
||||||
return copy(cache[key])
|
|
||||||
|
|
||||||
def reset():
|
|
||||||
nonlocal cache, hits, misses
|
|
||||||
cache = {}
|
|
||||||
hits = misses = 0
|
|
||||||
|
|
||||||
wrapped.cache_info = lambda: CacheInfo(hits, misses, len(cache))
|
|
||||||
wrapped.reset = reset
|
|
||||||
|
|
||||||
return wrapped
|
|
||||||
|
|
||||||
return new_cached_func()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import random
|
|
||||||
import time
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def lol(a):
|
|
||||||
time.sleep(random.random() * 4)
|
|
||||||
return a / 2
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def ggg(b):
|
|
||||||
time.sleep(random.random() * 4)
|
|
||||||
return b * 2
|
|
||||||
|
|
||||||
x = np.arange(6)
|
|
||||||
for i in range(5):
|
|
||||||
print(lol.cache_info())
|
|
||||||
print(lol(x))
|
|
||||||
|
|
||||||
print(f"{ggg.cache_info()=}")
|
|
||||||
print(f"{lol.cache_info()=}")
|
|
||||||
lol.reset()
|
|
||||||
|
|
||||||
print(ggg(np.arange(3)))
|
|
||||||
print(ggg(np.arange(8)))
|
|
||||||
print(ggg(np.arange(3)))
|
|
||||||
|
|
||||||
print(f"{ggg.cache_info()=}")
|
|
||||||
print(f"{lol.cache_info()=}")
|
|
||||||
@@ -9,7 +9,6 @@ import numpy as np
|
|||||||
from scipy.optimize import minimize_scalar
|
from scipy.optimize import minimize_scalar
|
||||||
|
|
||||||
from scgenerator import math
|
from scgenerator import math
|
||||||
from scgenerator.cache import np_cache
|
|
||||||
from scgenerator.physics import fiber, materials, pulse, units
|
from scgenerator.physics import fiber, materials, pulse, units
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
@@ -21,7 +20,6 @@ def group_delay_to_gdd(wavelength: np.ndarray, group_delay: np.ndarray) -> np.nd
|
|||||||
return gdd
|
return gdd
|
||||||
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def material_dispersion(
|
def material_dispersion(
|
||||||
wavelengths: np.ndarray,
|
wavelengths: np.ndarray,
|
||||||
material: str,
|
material: str,
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from numpy.polynomial.chebyshev import Chebyshev, cheb2poly
|
|||||||
from scipy.interpolate import interp1d
|
from scipy.interpolate import interp1d
|
||||||
|
|
||||||
from scgenerator import utils
|
from scgenerator import utils
|
||||||
from scgenerator.cache import np_cache
|
|
||||||
from scgenerator.math import argclosest, u_nm
|
from scgenerator.math import argclosest, u_nm
|
||||||
from scgenerator.physics import materials as mat
|
from scgenerator.physics import materials as mat
|
||||||
from scgenerator.physics import units
|
from scgenerator.physics import units
|
||||||
@@ -284,7 +283,6 @@ def capillary_resonance_strengths(
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def n_eff_hasan(
|
def n_eff_hasan(
|
||||||
wl_for_disp: np.ndarray,
|
wl_for_disp: np.ndarray,
|
||||||
n_gas_2: np.ndarray,
|
n_gas_2: np.ndarray,
|
||||||
@@ -548,7 +546,6 @@ def constant_effective_area_arr(l: np.ndarray, effective_area: float) -> np.ndar
|
|||||||
return np.ones_like(l) * effective_area
|
return np.ones_like(l) * effective_area
|
||||||
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def n_eff_pcf(wl_for_disp: np.ndarray, pcf_pitch: float, pcf_pitch_ratio: float) -> np.ndarray:
|
def n_eff_pcf(wl_for_disp: np.ndarray, pcf_pitch: float, pcf_pitch_ratio: float) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
semi-analytical computation of the dispersion profile of a triangular Index-guiding PCF
|
semi-analytical computation of the dispersion profile of a triangular Index-guiding PCF
|
||||||
@@ -685,7 +682,6 @@ def load_custom_loss(l: np.ndarray, loss_file: str) -> np.ndarray:
|
|||||||
return interp1d(wl, loss, fill_value=0, bounds_error=False)(l)
|
return interp1d(wl, loss, fill_value=0, bounds_error=False)(l)
|
||||||
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def dispersion_coefficients(
|
def dispersion_coefficients(
|
||||||
w_for_disp: np.ndarray,
|
w_for_disp: np.ndarray,
|
||||||
beta2_arr: np.ndarray,
|
beta2_arr: np.ndarray,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import numpy as np
|
|||||||
|
|
||||||
import scgenerator.math as math
|
import scgenerator.math as math
|
||||||
from scgenerator import utils
|
from scgenerator import utils
|
||||||
from scgenerator.cache import np_cache
|
|
||||||
from scgenerator.logger import get_logger
|
from scgenerator.logger import get_logger
|
||||||
from scgenerator.physics import units
|
from scgenerator.physics import units
|
||||||
from scgenerator.physics.units import NA, c, epsilon0, kB
|
from scgenerator.physics.units import NA, c, epsilon0, kB
|
||||||
@@ -318,7 +317,6 @@ class Gas:
|
|||||||
return f"{self.__class__.__name__}({self.name!r})"
|
return f"{self.__class__.__name__}({self.name!r})"
|
||||||
|
|
||||||
|
|
||||||
@np_cache
|
|
||||||
def n_gas_2(wl_for_disp: np.ndarray, gas_name: str, pressure: float, temperature: float):
|
def n_gas_2(wl_for_disp: np.ndarray, gas_name: str, pressure: float, temperature: float):
|
||||||
"""Returns the sqare of the index of refraction of the specified gas"""
|
"""Returns the sqare of the index of refraction of the specified gas"""
|
||||||
return Sellmeier.load(gas_name).n_gas_2(wl_for_disp, temperature, pressure)
|
return Sellmeier.load(gas_name).n_gas_2(wl_for_disp, temperature, pressure)
|
||||||
|
|||||||
Reference in New Issue
Block a user