def test_cache_data_dict(): my_dict = {} c = Cache(available_bytes=nbytes(1) * 3, cache_data=my_dict) c.put('x', 1, 10) assert c.get('x') == 1 assert my_dict['x'] == 1 c.clear() assert 'x' not in c
def test_cache_scores_update(): c = Cache(available_bytes=nbytes(1) * 2) c.put('x', 1, 1) c.put('y', 1, 1) c.get('x') c.get('x') c.get('x') c.put('z', 1, 1) assert set(c.data) == set('xz')
def test_just_one_reference(): c = Cache(available_bytes=1000) o = object() x = sys.getrefcount(o) c.put('key', o, cost=10) y = sys.getrefcount(o) assert y == x + 1 c.retire('key') z = sys.getrefcount(o) assert z == x
def test_memoize(): c = Cache(available_bytes=nbytes(1) * 3) flag = [0] def slow_inc(x): flag[0] += 1 sleep(0.01) return x + 1 memo_inc = c.memoize(slow_inc) assert memo_inc(1) == 2 assert memo_inc(1) == 2 assert list(c.data.values()) == [2]
def test_cache(): c = Cache(available_bytes=nbytes(1) * 3) c.put('x', 1, 10) assert c.get('x') == 1 assert 'x' in c c.put('a', 1, 10) c.put('b', 1, 10) c.put('c', 1, 10) assert set(c.data) == set('xbc') c.put('d', 1, 10) assert set(c.data) == set('xcd') c.clear() assert 'x' not in c assert not c.data assert not c.heap
def test_callbacks(): hit_flag = [False] def hit(key, value): hit_flag[0] = (key, value) miss_flag = [False] def miss(key): miss_flag[0] = key c = Cache(100, hit=hit, miss=miss) c.get('x') assert miss_flag[0] == 'x' assert hit_flag[0] == False c.put('y', 1, 1) c.get('y') assert hit_flag[0] == ('y', 1)
def test_cache_resize(): c = Cache(available_bytes=nbytes(1) * 3) c.put('x', 1, 10) assert c.get('x') == 1 assert 'x' in c c.put('a', 1, 10) c.put('b', 1, 10) c.put('c', 1, 10) assert set(c.data) == set('xbc') c.put('d', 1, 10) assert set(c.data) == set('xcd') # resize will shrink c.resize(available_bytes=nbytes(1) * 1) assert set(c.data) == set('x') c.resize(available_bytes=nbytes(1) * 10) assert set(c.data) == set('x')
ax.set_xticks(np.arange(0, 24, 1)) ax.set_xlabel('Start Time (hours)') ax.set_ylabel('Travel Time (seconds)') ax.set_title('Travel Time During the Day') plt.plot(x, y) plt.grid() plt.savefig("mygraph.png") def predict(model, row, time_of_day=None, buffer=1000, buffer_samples=21): rows = [] time_of_day = time_of_day or row['time_of_day'] for i in np.linspace(max(time_of_day - buffer, 0), min(time_of_day + buffer, 60 * 60 * 24), buffer_samples): row = row.copy() row['time_of_day'] = i rows.append(row) preds = model.predict(pd.DataFrame(rows)) return preds.mean() def model_predict(model, row, time_of_day=None): row['time_of_day'] = time_of_day or row.get('time_of_day') pred = model.predict(pd.DataFrame([row])) return pred[0] c = Cache(1e9) def get_regressor(model, x, predict): regressor = lambda t: predict(model, x, t) regressor = c.memoize(regressor) # In order to speed up the evaluation - Cache is used on the regressor return regressor
def cache(self): """ Cache Property """ if self._cache is None: self._cache = Cache(**self._cache_kws) return self._cache