-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete fits which are uncacheable. Draws from a model made without setting a random seed will never be retrieved a second time because the random seed is unavailable. Users who do want their fits to be cached should set a random seed. This fix helps solve the problem of the cache growing without limit. The cache will now grow much slower. Closes #154
- Loading branch information
1 parent
5609670
commit 9aa950d
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Tests related to cached fits.""" | ||
import shutil | ||
import pathlib | ||
import random | ||
|
||
import stan | ||
import httpstan.cache | ||
|
||
program_code = "parameters {real y;} model {y ~ normal(0,1);}" | ||
|
||
|
||
def cache_path(): | ||
return pathlib.Path(httpstan.cache.model_directory("models/abcdef")).parent | ||
|
||
|
||
def test_fit_cache(): | ||
"""Test that a fit with a random seed set is cached.""" | ||
|
||
cache_size_before = shutil.disk_usage(cache_path()).used | ||
print(cache_size_before) | ||
# this fit is cacheable | ||
random_seed = random.randrange(1, 2 ** 16) | ||
normal_posterior = stan.build(program_code, random_seed=random_seed) | ||
normal_posterior.sample() | ||
cache_size_after = shutil.disk_usage(cache_path()).used | ||
print(cache_size_after) | ||
assert cache_size_after > cache_size_before | ||
|
||
# fit is now in cache | ||
cache_size_before = shutil.disk_usage(cache_path()).used | ||
normal_posterior.sample() | ||
cache_size_after = shutil.disk_usage(cache_path()).used | ||
assert cache_size_before == cache_size_after | ||
|
||
|
||
def test_fit_cache_uncacheable(): | ||
"""Test that a fit with a random seed set is cached.""" | ||
cache_size_before = shutil.disk_usage(cache_path()).used | ||
# this fit is NOT cacheable, should not be saved | ||
normal_posterior = stan.build(program_code) | ||
normal_posterior.sample() | ||
cache_size_after = shutil.disk_usage(cache_path()).used | ||
# allow for a 4096 byte difference (an empty directory takes 4K) | ||
assert abs(cache_size_before - cache_size_after) <= 4096 |