forked from moj-analytical-services/splink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_linker_variants.py
199 lines (142 loc) · 6.08 KB
/
test_linker_variants.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from copy import deepcopy
import pandas as pd
from splink.internals.comparison_library import ExactMatch
from splink.internals.duckdb.database_api import DuckDBAPI
from splink.internals.linker import Linker
settings_template = {
"probability_two_random_records_match": 0.01,
"unique_id_column_name": "id",
"blocking_rules_to_generate_predictions": [
"l.first_name = r.first_name",
"l.surname = r.surname",
],
"comparisons": [
ExactMatch("first_name").configure(term_frequency_adjustments=True),
ExactMatch("surname"),
ExactMatch("dob"),
],
"retain_matching_columns": True,
"retain_intermediate_calculation_columns": True,
}
data = [
{"id": 1, "source_ds": "d"},
{"id": 2, "source_ds": "d"},
{"id": 1, "source_ds": "b"},
{"id": 2, "source_ds": "b"},
{"id": 3, "source_ds": "c"},
{"id": 4, "source_ds": "c"},
]
df = pd.DataFrame(data)
df["first_name"] = "John"
df["surname"] = "Smith"
df["dob"] = "01/01/1980"
sds_b_only = df.query("source_ds == 'b'").drop(columns=["source_ds"], axis=1)
sds_c_only = df.query("source_ds == 'c'").drop(columns=["source_ds"], axis=1)
sds_d_only = df.query("source_ds == 'd'").drop(columns=["source_ds"], axis=1)
def test_dedupe_only_join_condition():
data = [
{"id": 1},
{"id": 2},
{"id": 3},
{"id": 4},
{"id": 5},
{"id": 6},
]
df = pd.DataFrame(data)
df["first_name"] = "John"
df["surname"] = "Smith"
df["dob"] = "01/01/1980"
settings = deepcopy(settings_template)
settings["link_type"] = "dedupe_only"
settings_salt = deepcopy(settings_template)
settings_salt["link_type"] = "dedupe_only"
for s in [settings, settings_salt]:
db_api = DuckDBAPI()
linker = Linker(df.copy(), s, db_api=db_api)
df_predict = linker.inference.predict().as_pandas_dataframe()
assert len(df_predict) == (6 * 5) / 2
# Check that the lower ID is always on the left hand side
assert all(df_predict["id_l"] < df_predict["id_r"])
# self_link = linker._self_link().as_pandas_dataframe()
# assert len(self_link) == len(data)
def test_link_only_two_join_condition():
settings = deepcopy(settings_template)
settings = deepcopy(settings_template)
settings["link_type"] = "link_only"
settings_salt = deepcopy(settings_template)
settings_salt["link_type"] = "link_only"
for s in [settings, settings_salt]:
db_api = DuckDBAPI()
linker = Linker([sds_d_only, sds_b_only], s, db_api=db_api)
df_predict = linker.inference.predict().as_pandas_dataframe()
assert len(df_predict) == 4
# Check that the lower ID is always on the left hand side
df_predict["id_concat_l"] = (
df_predict["source_dataset_l"] + "-__-" + df_predict["id_l"].astype(str)
)
df_predict["id_concat_r"] = (
df_predict["source_dataset_r"] + "-__-" + df_predict["id_r"].astype(str)
)
assert all(df_predict["id_concat_l"] < df_predict["id_concat_r"])
# self_link = linker._self_link().as_pandas_dataframe()
# assert len(self_link) == 4
def test_link_only_three_join_condition():
settings = deepcopy(settings_template)
settings["link_type"] = "link_only"
settings_salt = deepcopy(settings_template)
settings_salt["link_type"] = "link_only"
for s in [settings, settings_salt]:
db_api = DuckDBAPI()
linker = Linker([sds_d_only, sds_b_only, sds_c_only], s, db_api=db_api)
df_predict = linker.inference.predict().as_pandas_dataframe()
assert len(df_predict) == 12
# Check that the lower ID is always on the left hand side
df_predict["id_concat_l"] = (
df_predict["source_dataset_l"] + "-__-" + df_predict["id_l"].astype(str)
)
df_predict["id_concat_r"] = (
df_predict["source_dataset_r"] + "-__-" + df_predict["id_r"].astype(str)
)
assert all(df_predict["id_concat_l"] < df_predict["id_concat_r"])
# self_link = linker._self_link().as_pandas_dataframe()
# assert len(self_link) == 6
def test_link_and_dedupe_two_join_condition():
settings = deepcopy(settings_template)
settings["link_type"] = "link_and_dedupe"
settings_salt = deepcopy(settings_template)
settings_salt["link_type"] = "link_and_dedupe"
for s in [settings, settings_salt]:
db_api = DuckDBAPI()
linker = Linker([sds_d_only, sds_b_only], s, db_api=db_api)
df_predict = linker.inference.predict().as_pandas_dataframe()
assert len(df_predict) == (4 * 3) / 2
# Check that the lower ID is always on the left hand side
df_predict["id_concat_l"] = (
df_predict["source_dataset_l"] + "-__-" + df_predict["id_l"].astype(str)
)
df_predict["id_concat_r"] = (
df_predict["source_dataset_r"] + "-__-" + df_predict["id_r"].astype(str)
)
assert all(df_predict["id_concat_l"] < df_predict["id_concat_r"])
# self_link = linker._self_link().as_pandas_dataframe()
# assert len(self_link) == 4
def test_link_and_dedupe_three_join_condition():
settings = deepcopy(settings_template)
settings["link_type"] = "link_and_dedupe"
settings_salt = deepcopy(settings_template)
settings_salt["link_type"] = "link_and_dedupe"
for s in [settings, settings_salt]:
db_api = DuckDBAPI()
linker = Linker([sds_d_only, sds_b_only, sds_c_only], s, db_api=db_api)
df_predict = linker.inference.predict().as_pandas_dataframe()
assert len(df_predict) == (6 * 5) / 2
# Check that the lower ID is always on the left hand side
df_predict["id_concat_l"] = (
df_predict["source_dataset_l"] + "-__-" + df_predict["id_l"].astype(str)
)
df_predict["id_concat_r"] = (
df_predict["source_dataset_r"] + "-__-" + df_predict["id_r"].astype(str)
)
assert all(df_predict["id_concat_l"] < df_predict["id_concat_r"])
# self_link = linker._self_link().as_pandas_dataframe()
# assert len(self_link) == 4