-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_semantic_cache.py
57 lines (42 loc) · 4 KB
/
test_semantic_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from ontolearn.semantic_caching import run_semantic_cache, run_non_semantic_cache
class TestSemanticCache:
def setup_method(self):
self.path_kg = "KGs/Family/father.owl" #path to the father dataset
self.path_kge = None
self.symbolic_reasoner = "HermiT"
self.neural_reasoner = "EBR"
self.num_concepts = 90
self.cache_size = 0.8*self.num_concepts
self.eviction = "LRU"
self.cache_type = "cold"
def run_cache_tests(self, cache_semantic, cache_non_semantic):
assert cache_semantic["hit_ratio"] >= cache_non_semantic["hit_ratio"], f"Expected semantic caching to have higher hit ratio, but got {cache_semantic['hit_ratio']} vs {cache_non_semantic['hit_ratio']}"
assert cache_semantic["miss_ratio"] <= cache_non_semantic["miss_ratio"], f"Expected semantic caching to have lower miss ratio, but got {cache_semantic['miss_ratio']} vs {cache_non_semantic['miss_ratio']}"
def test_run_time_and_jaccard(self):
cache_neural,_ = run_semantic_cache(self.path_kg, self.path_kge, self.cache_size, self.neural_reasoner, self.eviction, 0, self.cache_type, True)
cache_symbolic,_ = run_semantic_cache(self.path_kg, self.path_kge, self.cache_size, self.symbolic_reasoner, self.eviction, 0, self.cache_type, True)
assert float(cache_neural["avg_jaccard"]) >= float(cache_neural["avg_jaccard_reas"]), "Expected average Jaccard similarity to be at least as good as reasoner-based retrieval."
assert float(cache_symbolic["avg_jaccard"]) >= float(cache_symbolic["avg_jaccard_reas"]), "Expected average Jaccard similarity to be at least as good as reasoner-based retrieval."
assert float(cache_neural["RT_cache"]) <= float(cache_neural["RT"]), "Expected runtime with cache to be less or equal to direct retrieval time."
def test_cache_methods(self):
for reasoner in [self.neural_reasoner, self.symbolic_reasoner]:
cache_semantic,_ = run_semantic_cache(self.path_kg, self.path_kge, self.cache_size, reasoner, self.eviction, 0, self.cache_type, True)
cache_non_semantic,_ = run_non_semantic_cache(self.path_kg, self.path_kge, self.cache_size, reasoner, True)
self.run_cache_tests(cache_semantic, cache_non_semantic)
def test_cache_size(self):
cache_large,_ = run_semantic_cache(self.path_kg, self.path_kge, self.cache_size, self.neural_reasoner, self.eviction, 0, self.cache_type, True)
for k in [0.1, 0.5]:
cache_small,_ = run_semantic_cache(self.path_kg, self.path_kge, k * self.num_concepts, self.neural_reasoner, self.eviction, 0, self.cache_type, True)
assert cache_small["RT_cache"] >= cache_large["RT_cache"], "Expected runtime to decrease with larger cache."
assert cache_small["hit_ratio"] <= cache_large["hit_ratio"], f"Expected hit ratio to increase with cache size, but got {cache_small['hit_ratio']} vs {cache_large['hit_ratio']}"
assert cache_small["miss_ratio"] >= cache_large["miss_ratio"], f"Expected miss ratio to decrease with cache size, but got {cache_small['miss_ratio']} vs {cache_large['miss_ratio']}"
def test_eviction_strategy(self):
eviction_strategies = ["LRU", "FIFO", "LIFO", "MRU", "RP"]
results = {strategy: float(run_semantic_cache(self.path_kg, self.path_kge, self.cache_size, self.neural_reasoner, strategy, 10, self.cache_type, True)[0]["hit_ratio"]) for strategy in eviction_strategies}
for strategy, hit_ratio in results.items():
assert isinstance(hit_ratio, float), f"Hit ratio for {strategy} should be a float, but got {type(hit_ratio)}"
best_strategy = max(results, key=results.get)
assert best_strategy == "LRU", f"Expected LRU to be the best, but got {best_strategy}"
assert results, "No results were generated, possibly due to a failure in the cache evaluation process."
for strategy, hit_ratio in results.items():
assert 0.0 <= hit_ratio <= 1.0, f"Hit ratio for {strategy} is out of bounds: {hit_ratio}"