-
Notifications
You must be signed in to change notification settings - Fork 0
/
bencher_standard.py
1056 lines (898 loc) · 31.2 KB
/
bencher_standard.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import itertools
import operator
import os
import pickle
import re
from argparse import ArgumentParser, FileType
from collections import defaultdict
from typing import Callable, Collection, Mapping
import dask
import networkx as nx
import numpy as np
import pandas as pd
from dask.distributed import Client, as_completed, wait
from sklearn.cluster import DBSCAN
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from tqdm import tqdm
import config as cfg
import helper
from entity.candidate import Candidate
from entity.solution import Solution
from helper import logger
from module import candidatesimilarity as cs
from module import solving
from module.drawing import draw, draw_circular
from module.graphing import recursive_add_edge
from module.matching import Matcher
def get_max_stats(candidate_pair: Collection[Candidate], truth_pairs: set):
truth_left = truth_pairs.copy()
for node0 in candidate_pair[0].G:
for node1 in candidate_pair[1].G:
a = candidate_pair[0].get_attributes_keys(node0)
b = candidate_pair[1].get_attributes_keys(node1)
for pair in itertools.product(a, b):
if pair in truth_left:
truth_left.remove(pair)
matches = len(truth_pairs) - len(truth_left)
comparisons = len(candidate_pair[0]._attributes) * len(
candidate_pair[1]._attributes
)
return matches, comparisons
def print_indiv_stats(
candidate_pair: Collection[Candidate],
truth_pairs: set,
suffix,
recursive: bool,
):
matches = defaultdict(int)
comparisons = {}
for node0 in candidate_pair[0].G:
for node1 in candidate_pair[1].G:
if recursive:
a = list(
candidate_pair[0].get_recursive_attributes_keys(node0)
)
b = list(
candidate_pair[1].get_recursive_attributes_keys(node1)
)
else:
a = candidate_pair[0].get_attributes_keys(node0)
b = candidate_pair[1].get_attributes_keys(node1)
len_a = len(a)
len_b = len(b)
comparisons[(node0, node1)] = len_a * len_b
for pair in itertools.product(a, b):
if pair in truth_pairs:
matches[(node0, node1)] += 1
printable = []
for key, m in matches.items():
c = comparisons[key]
printable.append(key + (m, c))
pd.DataFrame(printable, columns=["x", "y", "matches", "compares"]).to_csv(
os.path.join(
cfg.OUTPUT_DIR, "matched_solution_indiv_{}.csv".format(suffix)
)
)
def get_pair_results_stbl(
candidate_pair: Collection[Candidate],
truth_pairs: set,
node_pair: tuple,
ret_size: bool = False,
):
a = dict(candidate_pair[0].get_attributes(node_pair[0]))
b = dict(candidate_pair[1].get_attributes(node_pair[1]))
if len(a) == 0 or len(b) == 0:
if ret_size:
return set(), 0, 0, (len(a), len(b))
return set(), 0, 0
titles = (a, b)
split = len(a)
corpus = []
keys = []
for title_dict in titles:
corpus.extend(title_dict.values())
keys.extend(title_dict.keys())
vectorizer = CountVectorizer(binary=True)
m = vectorizer.fit_transform(corpus).tocsr()
m.sort_indices()
assert m.has_sorted_indices == 1
m_arr_csr = (m[:split], m[split:])
m_arr = tuple(x.tocsc() for x in m_arr_csr)
key_tup = (keys[:split], keys[split:])
comparisons = 0
matches = set()
false_matches = 0
for i in range(len(vectorizer.vocabulary_)):
indices = tuple(
m_arr[mdx].indices[m_arr[mdx].indptr[i] : m_arr[mdx].indptr[i + 1]]
for mdx in range(2)
)
if len(indices[0]) == 0 or len(indices[1]) == 0:
continue
for x, y in itertools.product(*indices):
comparisons += 1
pair = (key_tup[0][x], key_tup[1][y])
if pair in truth_pairs:
matches.add(pair)
else:
false_matches += 1
logger.debug("Completed vocab {}".format(i))
if ret_size:
return matches, false_matches, comparisons, (len(a), len(b))
return matches, false_matches, comparisons
def get_pair_results(
candidate_pair: Collection[Candidate],
truth_pairs: set,
node_pair: tuple,
recursive: bool,
ret_size: bool = False,
):
matches = set()
false_matches = 0
if recursive:
a = list(candidate_pair[0].get_recursive_attributes_keys(node_pair[0]))
b = list(candidate_pair[1].get_recursive_attributes_keys(node_pair[1]))
else:
a = candidate_pair[0].get_attributes_keys(node_pair[0])
b = candidate_pair[1].get_attributes_keys(node_pair[1])
len_a = len(a)
len_b = len(b)
comparisons = len_a * len_b
for pair in itertools.product(a, b):
if pair in truth_pairs:
matches.add(pair)
else:
false_matches += 1
if ret_size:
return matches, false_matches, comparisons, (len_a, len_b)
return matches, false_matches, comparisons
@dask.delayed
def get_solution_stats(
candidate_pair: Collection[Candidate],
truth_pair: set,
raw_solution,
recursive: bool,
):
ret = dict()
x_nodes = list(candidate_pair[0].G)
y_nodes = list(candidate_pair[1].G)
overall_matches = set()
overall_false_matches = 0
overall_comparisons = 0
for x, y in raw_solution["x_route"]:
node_pair = (x_nodes[x], y_nodes[y])
matches, false_matches, comparisons = get_pair_results(
candidate_pair, truth_pair, node_pair, recursive
)
overall_matches.update(matches)
overall_false_matches += false_matches
overall_comparisons += comparisons
for y, x in raw_solution["y_route"]:
node_pair = (x_nodes[x], y_nodes[y])
matches, false_matches, comparisons = get_pair_results(
candidate_pair, truth_pair, node_pair, recursive
)
overall_matches.update(matches)
overall_false_matches += false_matches
overall_comparisons += comparisons
ret["tp"] = len(overall_matches)
ret["fp"] = overall_false_matches
ret["comparisons"] = overall_comparisons
clusters = len(raw_solution["x_fac"]) + len(raw_solution["y_fac"])
open_x = set()
for x, y in raw_solution["x_route"]:
open_x.add(x)
open_y = set()
for y, x in raw_solution["y_route"]:
open_y.add(y)
open_fac = len(open_y) + len(open_x)
if open_fac:
avg_size = (
len(raw_solution["x_route"]) + len(raw_solution["y_route"])
) / open_fac
else:
avg_size = 0
ret["clusters"] = clusters
ret["avg_size"] = avg_size
ret["elasped"] = raw_solution["elasped"]
return ret
def solve(
client: Client,
matching_futures,
solve_funcs: Collection[Callable],
candidate_pair_scatters: Mapping,
iterations: int = 1,
):
if isinstance(solve_funcs, Callable):
solve_funcs = [solve_funcs]
solutions = []
with tqdm(
total=len(matching_futures), desc="Submit solve", dynamic_ncols=True
) as pbar:
for future, (similarity, name) in as_completed(
matching_futures, with_results=True
):
sim_func = future.sim_func
candidate_pair = future.candidate_pair
candidate_pair_scatter = candidate_pair_scatters[candidate_pair]
similarity = client.scatter((similarity,))[0]
for solve_func in solve_funcs:
d_solution = dask.delayed(solve_func)(
candidate_pair_scatter, similarity
)
last_candidate_pair = candidate_pair_scatter
for r in range(iterations - 1):
last_candidate_pair = reorg(
last_candidate_pair, d_solution
)
new_similarity, _ = dask.delayed(sim_func, nout=2)(
last_candidate_pair
)
d_solution = dask.delayed(solve_func)(
last_candidate_pair,
new_similarity,
last_solution=d_solution,
)
solution = Solution(candidate_pair, sim_func, solve_func)
solution.solution_future = d_solution
solution._final_candidate_pair = last_candidate_pair
solutions.append(solution)
pbar.update()
return solutions
@dask.delayed
def reorg(
candidate_pair, raw_solution,
):
routes = ("x_route", "y_route")
new_candidate_pair = []
for i, candidate in enumerate(candidate_pair):
logger.info("Start {}".format(len(candidate.G)))
new_candidate = Candidate(candidate.name, candidate.G.copy())
new_candidate._node_attributes_map = dict(
candidate._node_attributes_map
)
new_candidate._node_recursive_attributes_map = (
candidate._node_recursive_attributes_map
)
new_candidate._attributes = candidate._attributes
new_candidate._stemmed_attributes = candidate._stemmed_attributes
nodes = list(candidate.G)
mergers = defaultdict(list)
for a, b in raw_solution[routes[i]]:
mergers[b].append(nodes[a])
for nodes in mergers.values():
if len(nodes) == 1:
continue
new_node = " + ".join(nodes)
if new_node in new_candidate._node_attributes_map:
continue
new_candidate.G.add_node(new_node)
new_candidate.G.remove_nodes_from(nodes)
new_candidate._node_attributes_map[new_node] = set()
for node in nodes:
new_candidate._node_attributes_map[new_node].update(
new_candidate._node_attributes_map[node]
)
del new_candidate._node_attributes_map[node]
logger.debug(len(new_candidate._node_attributes_map[new_node]))
logger.debug("End {}".format(len(new_candidate.G)))
new_candidate_pair.append(new_candidate)
return new_candidate_pair
def export(
candidate_pair: Collection[Candidate],
raw_solution,
name,
func: callable = None,
):
if func:
suffix = "-" + func.__name__
args = "-".join(("{}={}".format(*i) for i in func.keywords.items()))
if args:
suffix += "-" + args
else:
suffix = ""
x_nodes = list(candidate_pair[0].G)
y_nodes = list(candidate_pair[1].G)
printable = []
for x, y in raw_solution["x_route"]:
printable.append((x_nodes[x], y_nodes[y]))
for y, x in raw_solution["y_route"]:
printable.append((x_nodes[x], y_nodes[y]))
logger.info("Writing solution {} to CSV.".format(suffix))
pd.DataFrame(printable, columns=["x", "y"]).to_csv(
os.path.join(
cfg.OUTPUT_DIR, "matched_benchmark_{}-{}.csv".format(name, suffix)
)
)
def export_similarity(
candidate_pair, similarities, truth_pairs, suffix, recursive: bool
):
printable = []
for ((i, x), (j, y)) in itertools.product(
enumerate(candidate_pair[0].G), enumerate(candidate_pair[1].G)
):
if recursive:
a = list(candidate_pair[0].get_recursive_attributes_keys(x))
b = list(candidate_pair[1].get_recursive_attributes_keys(y))
else:
a = candidate_pair[0].get_attributes_keys(x)
b = candidate_pair[1].get_attributes_keys(y)
len_a = len(a)
len_b = len(b)
comparisons = len_a * len_b
matches = 0
for pair in itertools.product(a, b):
if pair in truth_pairs:
matches += 1
printout = (x, y, matches, comparisons)
for similarity, _ in similarities:
printout += (similarity[i, j],)
printable.append(printout)
columns = ["x", "y", "matches", "compares"]
for _, name in similarities:
columns.append(name)
pd.DataFrame(printable, columns=columns).to_csv(
os.path.join(
cfg.OUTPUT_DIR,
"matched_benchmark_{}-similarities.csv".format(suffix),
)
)
def retrieve(
candidates, dataframes, description: bool = False, fake: bool = False,
):
matcher = Matcher()
for i, (candidate, df) in enumerate(zip(candidates, dataframes)):
logger.info("Adding candidate...")
if fake:
candidate._attr_loader = helper._testing_attr_loader
else:
logger.debug(
"Generating listing dict for {}...".format(candidate.name)
)
grouped_listing = defaultdict(dict)
for row in df.iterrows():
title = str(row[1]["title"])
if description:
title += " " + str(row[1]["description"])
grouped_listing[row[1]["category"].strip()][
row[1]["id"]
] = title
logger.debug(
"Generating listing dict done {} done.".format(candidate.name)
)
def get_immediate_listings(node, node_name, listing_dict):
return listing_dict[node_name]
funct = functools.partial(
get_immediate_listings, listing_dict=grouped_listing
)
candidate._attr_loader = funct
matcher.add(candidate)
return matcher
def get_candidates(dataframes):
candidates = []
for n, df in enumerate(dataframes):
G = nx.DiGraph()
for category in set(df["category"].tolist()):
recursive_add_edge(G, category, "root", ": ")
candidates.append(Candidate(str(n), G))
return candidates
def wrapped_partial(func, name: str = None, *args, **kwargs):
partial_func = functools.partial(func, *args, **kwargs)
functools.update_wrapper(partial_func, func)
if name:
partial_func.__name__ = name
return partial_func
def get_sim_funcs(truth_pairs: set, recursive, p_sample, seed):
vocabulary = None
sim_func = [
wrapped_partial(
cs.similarity_unique_idf,
vocabulary=vocabulary,
add_node_name=False,
recursive=recursive,
p_sample=p_sample,
seed=seed,
),
wrapped_partial(
cs.similarity_category_idf,
vocabulary=vocabulary,
add_node_name=False,
recursive=recursive,
p_sample=p_sample,
seed=seed,
),
wrapped_partial(
cs.similarity_overlap,
add_node_name=False,
recursive=recursive,
p_sample=p_sample,
seed=seed,
),
wrapped_partial(
cs.similarity_category_idf_size,
vocabulary=vocabulary,
add_node_name=False,
recursive=recursive,
p_sample=p_sample,
seed=seed,
),
]
return sim_func
def get_solve_funcs(args):
solve_funcs = []
for w in np.around(np.arange(-1, 1.05, 0.1), 2):
for n in np.around(np.arange(-1, 1.05, 0.1), 2):
solve_func = wrapped_partial(solving.solve_bipoly, w=w, n=n)
solve_funcs.append(solve_func)
solve_func = wrapped_partial(solving.solve_stable_marriage)
solve_funcs.append(solve_func)
solve_func = wrapped_partial(solving.solve_bipartite)
solve_funcs.append(solve_func)
return solve_funcs
def expand(candidate: Candidate, eps: float = 4, min_samples: int = 2):
G: nx.DiGraph
G = candidate.G
for node in tqdm(list(G), desc="Node", dynamic_ncols=True):
if node == "root":
continue
keys = list(candidate.get_attributes_keys(node))
if not keys:
continue
corpus = [candidate._stemmed_attributes[k] for k in keys]
vectorizer = CountVectorizer(binary=True)
m = vectorizer.fit_transform(corpus)
successors = list(G.successors(node))
attributes_map = defaultdict(set)
labels = DBSCAN(eps=eps, min_samples=min_samples).fit_predict(m)
for n, label in enumerate(labels):
attributes_map["{} {}".format(node, label)].add(keys[n])
G.add_edges_from(
[(node, new_node) for new_node in attributes_map.keys()]
)
# Create a new virtual link to old nodes
if successors:
new_node = "{} existing".format(node)
attributes_map[new_node]
G.add_edge(node, new_node)
G.add_edges_from(
[(new_node, successor) for successor in successors]
)
candidate._node_attributes_map[node] = set()
candidate._node_attributes_map.update(attributes_map)
def cleanup(candidate: Candidate, rounds: int = 5):
if rounds <= 0:
return
candidate: Candidate
G = candidate.G
documents = []
for node in G:
cat_titles = [
candidate._stemmed_attributes[x]
for x in candidate.get_attributes_keys(node)
]
cat_titles.append(node)
documents.append(" ".join(cat_titles))
vectorizer = TfidfVectorizer()
vectorizer.fit(documents)
documents.extend(candidate._stemmed_attributes.values())
tfidf = vectorizer.transform(documents)
logger.debug(tfidf.shape)
key_map = {
key: i
for i, key in enumerate(
candidate._stemmed_attributes.keys(), start=len(G)
)
}
node_map = {node: i for i, node in enumerate(G)}
for r in tqdm(range(rounds), desc="Round", dynamic_ncols=True):
transfers = defaultdict(lambda: defaultdict(list))
for node in tqdm(G, desc="Node", dynamic_ncols=True):
keys = list(candidate.get_attributes_keys(node))
if not len(keys):
continue
keys_idx = [key_map[x] for x in keys]
nodes = list(
itertools.chain(G.predecessors(node), G.successors(node))
)
nodes.insert(0, node)
nodes_idx = [node_map[x] for x in nodes]
similarity = linear_kernel(tfidf[nodes_idx], tfidf[keys_idx])
max_idx = np.argmax(similarity, axis=0)
for k, i in enumerate(max_idx):
if i > 0:
transfers[node][nodes[i]].append(keys[k])
if len(transfers) == 0:
break
logger.debug("Transferring {} items...".format(len(transfers)))
for out_node, ins in transfers.items():
for in_node, keys in ins.items():
candidate._node_attributes_map[in_node].update(keys)
candidate._node_attributes_map[out_node].difference_update(
keys
)
def preprocess(candidate: Candidate, eps, min_samples, rounds):
cleanup(candidate, rounds=rounds)
if min_samples:
expand(candidate, eps, min_samples)
def main(args):
truth_df = pd.read_csv(args.truth_file)
logger.info("Truth collection: {}".format(truth_df.shape))
truth_pairs = set(truth_df.itertuples(index=False, name=None))
use_pickled = False
if args.pickle:
if os.path.isfile(
os.path.join(
cfg.OUTPUT_DIR, "matcher-{}.pickle".format(args.pickle)
)
):
logger.info("Using pickle")
with open(
os.path.join(
cfg.OUTPUT_DIR, "matcher-{}.pickle".format(args.pickle)
),
"rb",
) as handle:
matcher = pickle.load(handle)
use_pickled = True
if not use_pickled:
adf = pd.read_csv(args.a_file)
logger.info("Collection 1: {}".format(adf.shape))
bdf = pd.read_csv(args.b_file)
logger.info("Collection 2: {}".format(bdf.shape))
dataframes = (adf, bdf)
candidates = get_candidates(dataframes)
matcher = retrieve(
candidates,
dataframes,
description=args.description,
fake=args.fake,
)
sim_funcs = get_sim_funcs(
truth_pairs, args.recursive, args.p_sample, args.seed
)
solve_funcs = get_solve_funcs(args)
cluster = helper.get_cluster()
with Client(cluster) as client:
helper.ensure_worker(client)
if not use_pickled:
matcher.prepare(client, batch_size=cfg.STEM_BATCH)
logger.info(
"Total nodes is {} and {}.".format(
len(matcher._candidates[0].G),
len(matcher._candidates[1].G),
)
)
for candidate in tqdm(
matcher._candidates, desc="preprocessing", dynamic_ncols=True
):
preprocess(
candidate, args.eps / 10.0, args.min_samples, args.rounds
)
if args.pickle:
logger.info("Creating pickle file...")
with open(
os.path.join(
cfg.OUTPUT_DIR, "matcher-{}.pickle".format(args.pickle)
),
"wb",
) as handle:
pickle.dump(
matcher, handle, protocol=pickle.HIGHEST_PROTOCOL
)
logger.info("Created pickle file.")
logger.info(
"Total nodes is now {} and {}.".format(
len(matcher._candidates[0].G), len(matcher._candidates[1].G)
)
)
matching_futures = matcher.run(client, sim_funcs)
candidate_pair_scatters = {}
for future in matching_futures:
candidate_pair = future.candidate_pair
helper.broadcast_add_and_get(
client, candidate_pair_scatters, candidate_pair
)
# Solve
solution: Solution
solutions = solve(
client,
matching_futures,
solve_funcs,
candidate_pair_scatters,
args.iterations,
)
truth_pairs = client.scatter((truth_pairs,), broadcast=True)[0]
# Submit max stats
max_stats_future = []
with tqdm(
total=len(candidate_pair_scatters),
desc="Submit max stats",
dynamic_ncols=True,
) as pbar:
for (
candiate_pair,
candidate_pair_scatter,
) in candidate_pair_scatters.items():
future = client.submit(
get_max_stats, candidate_pair_scatter, truth_pairs,
)
future._candidate_pair = candiate_pair
max_stats_future.append(future)
pbar.update()
# Draw
draw_future = []
if args.draw:
logger.info("Drawing solutions")
draws = []
for solution in solutions:
draws.append(
dask.delayed(draw_circular)(
solution._final_candidate_pair,
solution.solution_future,
func=solution.solve_func,
prefix="matched_benchmark_{}-draw".format(args.suffix),
)
)
draw_future = client.compute(draws, priority=10)
# Export similarity
export_sim_future = []
if not args.no_export_sim:
logger.info("Exporting similarities")
exports = []
similarities = []
for future in matching_futures:
similarity, name = future.result()
sim_func_name = future.sim_func.__name__
similarities.append((similarity, sim_func_name))
candidate_pair = future.candidate_pair
candidate_pair_scatter = candidate_pair_scatters[candidate_pair]
similarities = client.scatter((similarities,))[0]
exports.append(
dask.delayed(export_similarity)(
candidate_pair_scatter,
similarities,
truth_pairs,
args.suffix,
args.recursive,
)
)
export_sim_future = client.compute(exports, priority=10)
# Export
export_future = []
if args.export:
logger.info("Exporting solutions")
exports = []
for solution in solutions:
exports.append(
dask.delayed(export)(
solution._final_candidate_pair,
solution.solution_future,
args.suffix,
func=solution.solve_func,
)
)
export_future = client.compute(exports, priority=10)
# Submit solution stats (Delayed)
for solution in solutions:
solution.d_stats = get_solution_stats(
solution._final_candidate_pair,
truth_pairs,
solution.solution_future,
args.recursive,
)
d_solution_stats = [solution.d_stats for solution in solutions]
solution_stats_futures = client.compute(d_solution_stats, priority=20)
# Get max stats
max_stats = {}
with tqdm(
total=len(max_stats_future),
desc="Get max stats",
dynamic_ncols=True,
) as pbar:
for future, stats in as_completed(
max_stats_future, with_results=True
):
max_stats[future._candidate_pair] = stats
pbar.update()
# Get solution stats delayed (Delayed)
d_future_soln_map = dict(zip(solution_stats_futures, solutions))
with tqdm(
total=len(solutions), desc="Counting", dynamic_ncols=True
) as pbar:
for future, stats in as_completed(
solution_stats_futures, with_results=True
):
solution = d_future_soln_map[future]
solution.max_matches = max_stats[solution.candidate_pair][0]
solution.max_comparisons = max_stats[solution.candidate_pair][
1
]
for k, v in stats.items():
setattr(solution, k, v)
pbar.update()
printable = []
# Recall result
for solution in tqdm(
solutions, desc="Benchmarking", dynamic_ncols=True
):
solve_args = "-".join(
(
"{}={}".format(param, round(i, 5))
for param, i in solution.solve_func.keywords.items()
)
)
printable.append(
(
solution.candidate_pair[0].name,
solution.candidate_pair[1].name,
solution.tp,
solution.max_matches,
solution.comparisons,
solution.max_comparisons,
solve_args,
solution.sim_func.__name__,
solution.solve_func.__name__,
solution.recall,
solution.precision,
solution.f1,
solution.reduction,
solution.tradeoff,
solution.cpbur,
round(solution.elasped, 5),
solution.clusters,
round(solution.avg_size, 5),
)
)
logger.debug(
"Matched {} to {} with {}: R = {}".format(
solution.candidate_pair[0].name,
solution.candidate_pair[1].name,
solve_args,
solution.recall,
)
)
printable.sort(key=operator.itemgetter(9, 13), reverse=True)
# Write
pd.DataFrame(
printable,
columns=[
"name",
"name",
"matches",
"max matches",
"comparisons",
"max comparisons",
"args",
"sim function",
"solve function",
"recall",
"precision",
"f1",
"reduction",
"tradeoff",
"cpbur",
"wall time",
"clusters",
"avg cardinality",
],
).to_csv(
os.path.join(
cfg.OUTPUT_DIR, "matched_benchmark_{}.csv".format(args.suffix)
)
)
# Wait for draws
logger.info("Waiting for any other tasks to finish...")
wait(draw_future)
wait(export_sim_future)
wait(export_future)
logger.info("Done.")
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"-f",
"--fake",
action="store_true",
default=False,
help="Run against fake data",
)
parser.add_argument(
"-d",
"--draw",
action="store_true",
default=False,
help="Draw solution",
)
parser.add_argument(
"-ns",
"--no_export_sim",
action="store_true",
default=False,
help="Export similarities",
)
parser.add_argument(
"-x",
"--export",
action="store_true",
default=False,
help="Export solution",
)
parser.add_argument(
"-n",
"--name",
dest="suffix",
help="suffix to name of files",
default="",
)
parser.add_argument(
"-e",
"--eps",
type=float,
help="Epsilon value for DBscan multiplied by 10",
default=40.0,
)
parser.add_argument(
"-m",
"--min_samples",
type=int,
help="Number of levels to trim leaf",
default=2,
)
parser.add_argument(
"-r",