Source code for traveltimes_prediction.support_files.extrapolating_cache

from scipy.interpolate import interp1d
from . import MyQueue


[docs]class ExtrapolatingCache: """ Class - cache for caching of values, has ability to extrapolate using cached values. """ def __init__(self, maxsize=10): """ Constructor. :param int maxsize: max size of the cache """ self._cache = MyQueue(maxsize=maxsize)
[docs] def put(self, item): """ Method for inserting the values to cache. :param number item: :return: """ self._cache.push(item) return self
[docs] def get(self, x=None): """ Method for extrapolating the values from the cache. :param int x: 'x' index for extrapolation :return: float - interpolated value """ interpolator = interp1d(x=list(range(len(self._cache))), y=self._cache, kind='linear', fill_value='extrapolate') return float(interpolator(len(self._cache) + 1 if x is None else x))
[docs] def empty(self): """ Method for investigating if the self._cache is empty or not :return: boolean """ return True if len(self._cache) < 2 else False