-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_retrieve.py
159 lines (133 loc) · 4.94 KB
/
test_retrieve.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from typing import Any
import polars as pl
import cbrkit
def _custom_numeric_sim(x: float, y: float) -> float:
return 1 - abs(x - y) / 100000
def test_retrieve_multiprocessing():
query_name = 42
casebase_file = "data/cars-1k.csv"
df = pl.read_csv(casebase_file)
casebase = cbrkit.loaders.polars(df)
retriever = cbrkit.retrieval.build(
cbrkit.sim.attribute_value(
attributes={
"price": cbrkit.sim.numbers.linear(max=100000),
"year": cbrkit.sim.numbers.linear(max=50),
"manufacturer": cbrkit.sim.strings.taxonomy.load(
"./data/cars-taxonomy.yaml",
measure=cbrkit.sim.strings.taxonomy.wu_palmer(),
),
"make": cbrkit.sim.strings.levenshtein(),
"miles": _custom_numeric_sim,
},
aggregator=cbrkit.sim.aggregator(pooling="mean"),
),
limit=5,
)
results = cbrkit.retrieval.mapply(
casebase,
{"default": casebase[query_name]},
retriever,
processes=2,
)
assert len(results) == 1
assert len(results["default"].ranking) == 5
result = cbrkit.retrieval.apply(
casebase,
casebase[query_name],
retriever,
processes=2,
)
assert len(result.ranking) == 5
def test_retrieve_dataframe():
query_name = 42
casebase_file = "data/cars-1k.csv"
df = pl.read_csv(casebase_file)
casebase = cbrkit.loaders.polars(df)
query = casebase[query_name]
retriever = cbrkit.retrieval.build(
cbrkit.sim.attribute_value(
attributes={
"price": cbrkit.sim.numbers.linear(max=100000),
"year": cbrkit.sim.numbers.linear(max=50),
"manufacturer": cbrkit.sim.strings.taxonomy.load(
"./data/cars-taxonomy.yaml",
measure=cbrkit.sim.strings.taxonomy.wu_palmer(),
),
"make": cbrkit.sim.strings.levenshtein(),
"miles": cbrkit.sim.numbers.linear(max=1000000),
},
aggregator=cbrkit.sim.aggregator(pooling="mean"),
),
limit=5,
)
result = cbrkit.retrieval.apply(casebase, query, retriever)
assert len(casebase) == 999 # csv contains header
assert len(result.similarities) == 5
assert len(result.ranking) == 5
assert len(result.casebase) == 5
assert result.similarities[query_name].value == 1.0
assert result.ranking[0] == query_name
def test_retrieve_dataframe_custom_query():
casebase_file = "data/cars-1k.csv"
df = pl.read_csv(casebase_file)
casebase = cbrkit.loaders.polars(df)
query = {
"price": 10000,
"year": 2010,
"manufacturer": "audi",
"make": "a4",
"miles": 100000,
}
retriever = cbrkit.retrieval.build(
cbrkit.sim.attribute_value(
attributes={
"price": cbrkit.sim.numbers.linear(max=100000),
"year": cbrkit.sim.numbers.linear(max=50),
"manufacturer": cbrkit.sim.strings.taxonomy.load(
"./data/cars-taxonomy.yaml",
measure=cbrkit.sim.strings.taxonomy.wu_palmer(),
),
"make": cbrkit.sim.strings.levenshtein(),
"miles": _custom_numeric_sim,
},
aggregator=cbrkit.sim.aggregator(pooling="mean"),
),
limit=5,
)
result = cbrkit.retrieval.apply(casebase, query, retriever)
assert len(result.similarities) == 5
assert len(result.ranking) == 5
assert len(result.casebase) == 5
def test_retrieve_nested():
query_name = 42
casebase_file = "data/cars-1k.yaml"
casebase: dict[int, Any] = cbrkit.loaders.yaml(casebase_file)
query = casebase[query_name]
retriever = cbrkit.retrieval.build(
cbrkit.sim.attribute_value(
attributes={
"price": cbrkit.sim.numbers.linear(max=100000),
"year": cbrkit.sim.numbers.linear(max=50),
"model": cbrkit.sim.attribute_value(
attributes={
"make": cbrkit.sim.strings.levenshtein(),
"manufacturer": cbrkit.sim.strings.taxonomy.load(
"./data/cars-taxonomy.yaml",
measure=cbrkit.sim.strings.taxonomy.wu_palmer(),
),
}
),
},
aggregator=cbrkit.sim.aggregator(pooling="mean"),
),
min_similarity=0.5,
)
result = cbrkit.retrieval.apply(casebase, query, retriever)
assert len(casebase) == 999
assert result.similarities[query_name].value == 1.0
assert result.ranking[0] == query_name
model_sim = result.similarities[query_name].attributes["model"]
assert isinstance(model_sim, cbrkit.sim.AttributeValueSim)
assert model_sim.value == 1.0
assert model_sim.attributes["make"] == 1.0