-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.py
1160 lines (998 loc) · 48.2 KB
/
match.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
#As a work of the United States government, this project is in the public
#domain within the United States. Additionally, we waive copyright and related
#rights in the work worldwide through the CC0 1.0 Universal public domain
#dedication (https://creativecommons.org/publicdomain/zero/1.0/)
"""
.. module:: matcher
:platform: Unix
:synopsis: class to run match
.. moduleauthor:: Goldschlag
Part of the replication archive for The U.S. Census Bureau's Ex Post
Confidentiality Analysis of the 2010 Census Data Publications
(https://github.com/uscensusbureau/recon_replication)
"""
import time
import multiprocessing as mp
import os
import sys
import logging
import pandas as pd
import numpy as np
import scipy.spatial.distance as spdist
class Matcher(object):
"""Class to organize tabulations of county-level datasets.
Args:
lock (mp.Lock()): lock used to modify shared number of tracts processed.
params (dict): dictionary of params (from json)
"""
def __init__(self,lock,params,leftarg,rightarg):
self.lock = lock
self.m = mp.Manager()
# validation queues
self.countyQueue = self.m.Queue()
self.workQueue = self.m.Queue()
self.doneStatQueue = self.m.Queue()
self.doneMatchQueue = self.m.Queue()
self.paramDict = params
self.leftarg = leftarg
self.rightarg = rightarg
self.t0 = time.time()
self.valTractsPrcd = mp.Value('i',0)
self.putTractsPrcd = mp.Value('i',0)
self.confTractsPrcd = mp.Value('i',0)
self.matchType = ''
def logTime(self):
"""Write time lapsed to log file.
"""
t = time.time() - self.t0
logging.info('Minutes lapsed: {0}'.format(t/60))
def binAge(self,series):
"""Translates single-year age value into bins consistent with tabulations.
Args:
series (list):
"""
if series <= 21 :
return series
elif 22 <= series <= 24:
return 22
elif 25 <= series <= 29:
return 25
elif 30 <= series <= 34:
return 30
elif 35 <= series <= 39:
return 35
elif 40 <= series <= 44:
return 40
elif 45 <= series <= 49:
return 45
elif 50 <= series <= 54:
return 50
elif 55 <= series <= 59:
return 55
elif 60 <= series <= 61:
return 60
elif 62 <= series <= 64:
return 62
elif 65 <= series <= 66:
return 65
elif 67 <= series <= 69:
return 67
elif 70 <= series <= 74:
return 70
elif 75 <= series <= 79:
return 75
elif 80 <= series <= 84:
return 80
elif series >= 85:
return 85
def getSourceData(self,county,source):
"""Loads data to memory from county-level files.
Args:
county (string): 5-digit county FIPS code, 2-digit state and 3-digit
county codes
Returns:
df (pd.DataFrame): pandas dataframe with data for county
"""
# file
# the vanilla cef, hdf, or cmrcl data
if source in ['cef','hdf','cmrcl']:
file = self.paramDict[source+'dir'] + source + county + '.csv'
# the vanilla guesser data
elif source in ['modalGsr','propGsr']:
file = self.paramDict['simuldir'] + '/' + source + county + '.csv'
# the plus guesser data
elif 'plus' in source and ('modalGsr' in source or 'propGsr' in source) and (source[0]!='r'):
file = self.paramDict['simuldir'] + '/' + source + county + '.csv'
# the cef or hdf plus data
elif 'plus' in source and ('cef' in source or 'hdf' in source) and (source[0]!='r'):
stem = source[0:3]
file = self.paramDict[stem+'dir'] + source + county + '.csv'
# rhdf plus data
elif 'plus' and source[0]=='r':
stem = source.replace('plus','').replace('cef','').replace('cmrcl','').replace('binage','').replace('fzyage','')
file = self.paramDict['rhdfbasedir'] + '/' + stem + '/' + source.replace('fzyage','') + county + '.csv'
# the vanilla rhdf data
else:
file = self.paramDict['rhdfbasedir'] + '/' + source + '/' + source + county + '.csv'
# load county file
try:
inDF = pd.read_csv(file,dtype=str,engine='python')
if 'plus' in source:
inDF['geoid_block'] = inDF.county + inDF.tract + inDF.block
inDF['geoid_tract'] = inDF.geoid_block.str.slice(start=0,stop=11)
inDF['county'] = inDF.geoid_block.str.slice(start=0,stop=5)
inDF['tract'] = inDF.geoid_block.str.slice(start=5,stop=11)
inDF['block'] = inDF.geoid_block.str.slice(start=11,stop=15)
inDF['blockid'] = inDF.tract + inDF.block
# vanilla modal guesser data may be missing race-eth because no mode exists
if source == 'modalGsr':
inDF = inDF.drop(inDF[inDF.white.isna()].index)
for v in ['geoid_block']:
inDF[v] = inDF[v].astype(str)
if source == 'cmrcl':
inDF['r'] = inDF['r'].astype(float)
for v in ['sex','age']:
inDF[v] = inDF[v].astype(float)
inDF=inDF[(inDF.tract!='')&(inDF.block!='')&(inDF.pik!='')&(inDF.sex.isnull()==False)&(inDF.age.isnull()==False)]
elif source == 'cef':
#inDF = inDF.drop(['r'],1)
inDF['r'] = inDF['r'].astype(float)
for v in ['keep','sex','age']+self.paramDict['raceethvars']:
inDF[v] = inDF[v].astype(int)
inDF['pik'] = inDF['pik'].astype(str)
inDF.loc[inDF.pik=='nan','pik']=''
elif 'plus' in source:
for v in ['sex','age']+self.paramDict['raceethvars']:
inDF[v] = inDF[v].astype(int)
inDF['pik'] = inDF['pik'].astype(str)
inDF.loc[inDF.pik=='nan','pik']=''
else:
for v in ['sex','age']+self.paramDict['raceethvars']:
inDF[v] = inDF[v].astype(int)
if source == 'cef':
inDF= inDF.sort_values(by=['county','tract','block','sex','age','white','black','aian','asian','nhopi','sor','hisp'])
elif source == 'cmrcl':
inDF= inDF.sort_values(by=['county','tract','block','sex','age','r'])
elif 'plus' in source:
inDF= inDF.sort_values(by=['county','tract','block','sex','age','pik'])
else:
inDF= inDF.sort_values(by=['county','tract','block','sex','age','white','black','aian','asian','nhopi','sor','hisp'])
inDF['agebin'] = inDF['age'].apply(self.binAge)
return inDF
except:
logging.info('failed file: {0}'.format(file))
return pd.DataFrame()
def fillCountyQueue(self,counties,q):
"""Fills loadQueue with counties that need to be loaded to work queue.
Args:
counties (list): list of counties to add to queue
q (mp.Manager().Queue()):queue that the counties will be loaded to
"""
# put fips codes into the working queue
for c in counties:
q.put(c)
def loadData(self,county):
"""Puts tuple of left and right tract-level data into workValQueue.
Args:
county (string): 5-digit county FIPS code, 2-digit state and 3-digit
county codes
"""
# add the county
l = self.getSourceData(county,self.leftarg).to_dict(orient='records')
if self.matchType=='validate' and self.rightarg == 'cef':
r = self.getSourceData(county,self.rightarg)
r.age = pd.to_numeric(r.age,downcast='float')
r = r.to_dict(orient='records')
elif self.matchType=='putative' and self.rightarg == 'cef':
r = self.getSourceData(county,self.rightarg).drop(self.paramDict['raceethvars'],1)
r.age = pd.to_numeric(r.age,downcast='float')
r = r.to_dict(orient='records')
#elif self.rightarg == 'cef':
# r = self.getSourceData(county,self.rightarg)
# r.age = pd.to_numeric(r.age,downcast='float')
# r = r.to_dict(orient='records')
else:
r = self.getSourceData(county,self.rightarg).to_dict(orient='records')
# break into tracts and load to queue
tracts = list(set([x['geoid_tract'] for x in l]) & set([x['geoid_tract'] for x in r]))
# validate tract set
if len(tracts) < len(set([x['geoid_tract'] for x in l])) or len(tracts) < len(set([x['geoid_tract'] for x in r])):
logging.warning('MISSING TRACTS IN COUNTY: {0}'.format(county))
for t in tracts:
l2Load = [x for x in l if x['geoid_tract']==t]
r2Load = [x for x in r if x['geoid_tract']==t]
self.workQueue.put((l2Load,r2Load))
def loadDataWorker(self):
"""Load worker thread function, churning through counties in inQ,
adding tract-level data from a given county via a load function.
Args:
inQ (mp.Manager().Queue()): queue from which counties will be pulled
workQ (string): string that captures what load function to use
('val', 'atch', or 'cnfrm')
"""
# what each worker does, pulling a code and running the match
churn = True
while churn:
# Pull the next record for comparison
try:
c = self.countyQueue.get(timeout = 15)
except:
# If no work can be pulled from queue, break
logging.info('Load worker queue timeout---------------')
logging.info('load queue size: {0}'.format(self.countyQueue.qsize()))
logging.info('---------------Load worker queue timeout')
break
# put data for c in workqueue via loadfunc
self.loadData(c)
def matchWorker(self, inQ, counter, matchFunc, cols2Match):
"""Match worker function, churning through data in inQ and sending them
through the match functioned.
Args:
inQ (mp.Manager().Queue()): queue from which data will be pulled
counter (mp.Value('i',0)): counter that captures the number of
tracts processed
matchFunc (string): string that captures what match function to use
('val', 'atch', or 'cnfrm')
cols2Match (list): list of strings with labels of the variables that
will be matched
"""
# what each worker does, pulling data and running the match
churn = True
while churn:
# Pull the next record for comparison
try:
t = inQ.get(timeout = 15)
except:
# If no work can be pulled from queue, break
logging.info('Worker queue timeout---------------')
logging.info('inQ size: {0}'.format(inQ.qsize()))
logging.info('Number of tracts processed: {0}'.format(counter.value))
break
# Keep track of the number of records processed
with self.lock:
counter.value += 1
# Compare record for match against all searchTerm
if matchFunc == 'val':
self.validate(t,cols2Match)
elif matchFunc == 'atch':
self.attachPIK(t,cols2Match)
elif matchFunc == 'cnfrm':
self.confirmPIK(t,cols2Match)
def match(self,match2Do, cols2Match, leftBlk, rightBlk, colsFromLeft,
colsFromRight, matchDF, matchFlagName, compareResidual):
"""Match function that iterates over left and right, returning match
counts and matched records.
Args:
match2Do (dictioanry): keys are match types ('exact', 'fzyage',
'oneoff') with values True or False that determine what matches will
be executed
cols2Match (list): list of strings with labels of the variables that
will be matched
leftBlk (list): list of record dictionaries for left data
rightBlk (list): list of record dictionaries for right data
colsFromLeft (list): list of variable labels that will be recovered
from the left data for the matched DF
colsFromRight (list): list of variable labels that will be recovered
from the right data for the matched DF
matchDF (pd.DataFrame): dataframe into which matched records will be
stored
matchFlagName (string): label to be used for the flag that captures
what type of match was made in the match DF
compareResidual (true/false): whether to compare residual unmatched blocks
Returns:
exactCount (int): count of exact matches
fzyAgeCount (int): count of fuzzy age matches
binAgeCount (int): count of age bin matches
oneOffCount (int): count of one off matches
matchDF (pd.DataFrame): dataframe into which matched records were
stored
js (float): jensen-shannon comparison of residuals, or -1 if compareResidual==False
"""
# initialize counters
leftCount = len(leftBlk)
rightCount = len(rightBlk)
exactCount = 0
fzyAgeCount = 0
binAgeCount = 0
oneOffCount = 0
# sort the lists of dicts
leftBlk, rightBlk = self.sortMatchDicts(match2Do, cols2Match, leftBlk, rightBlk)
#logging.debug('colsFromRight: {0}'.format(colsFromRight))
# exact comparisons
if match2Do['exact'] == True:
# search for all exact matches first, all left records have a
# chance of finding an exact match before we look for fuzzy
# matches
for i in range(len(leftBlk)-1,-1,-1):
l = leftBlk[i]
# compare to each record on the right until we find a match,
# if we do remove both
for j, r in enumerate(rightBlk):
# exact match?
if {k: l[k] for k in cols2Match}=={k: r[k] for k in cols2Match}:
exactCount += 1
# save match records
newRec = {}
newRec.update({k: l[k] for k in colsFromLeft})
# rename out dict if same vars coming from right if necessary
if 'cef_white' in colsFromRight:
nr = self.renameMatchDict(r, 'cef')
newRec.update({k: nr[k] for k in colsFromRight})
elif 'syn_white' in colsFromRight:
nr = self.renameMatchDict(r, 'syn')
newRec.update({k: nr[k] for k in colsFromRight})
elif any('mdf' in x for x in colsFromRight):
nr = self.renameMatchDict(r, 'mdf')
newRec.update({k: nr[k] for k in colsFromRight})
elif any(self.rightarg[0:3] in x for x in colsFromRight):
nr = self.renameMatchDict(r, self.rightarg[0:3])
newRec.update({k: nr[k] for k in colsFromRight})
else:
newRec.update({k: r[k] for k in colsFromRight})
newRec.update({matchFlagName:1})
matchDF = matchDF.append(newRec,ignore_index=True)
del leftBlk[i]
del rightBlk[j]
break
# fuzzy age comparison
if match2Do['fzyage'] == True:
# search for all fuzzy age matches in residual
for i in range(len(leftBlk)-1,-1,-1):
l = leftBlk[i]
# compare to each record on the right
for j, r in enumerate(rightBlk):
diffs = []
if 'sex' in cols2Match:
diffs.append(l['sex']!=r['sex'])
if 'pik' in cols2Match:
diffs.append(l['pik']!=r['pik'])
if 'white' in cols2Match:
diffs.append(len({k: l[k] for k in self.paramDict['raceethvars'] if l[k]!=r[k]}))
ageDiff = abs(l['age'] - r['age'])>self.paramDict['agefuzdiff']
if ageDiff==0 and sum(diffs)==0:
fzyAgeCount += 1
newRec = {}
newRec.update({k: l[k] for k in colsFromLeft})
# rename out dict if same vars coming from left and right
if 'cef_white' in colsFromRight:
nr = self.renameMatchDict(r, 'cef')
newRec.update({k: nr[k] for k in colsFromRight})
elif 'syn_white' in colsFromRight:
nr = self.renameMatchDict(r, 'syn')
newRec.update({k: nr[k] for k in colsFromRight})
elif any('mdf' in x for x in colsFromRight):
nr = self.renameMatchDict(r, 'mdf')
newRec.update({k: nr[k] for k in colsFromRight})
elif any(self.rightarg[0:3] in x for x in colsFromRight):
nr = self.renameMatchDict(r, self.rightarg[0:3])
newRec.update({k: nr[k] for k in colsFromRight})
else:
newRec.update({k: r[k] for k in colsFromRight})
newRec.update({matchFlagName:2})
matchDF = matchDF.append(newRec,ignore_index=True)
del leftBlk[i]
del rightBlk[j]
break
# bin age comparison
if match2Do['binage'] == True:
# search for all age bin matches that did not receive an exact match
# or a fuzzy age match
for i in range(len(leftBlk)-1,-1,-1):
l = leftBlk[i]
# compare to each record on the right
for j, r in enumerate(rightBlk):
diffs = []
if 'sex' in cols2Match:
diffs.append(l['sex']!=r['sex'])
if 'pik' in cols2Match:
diffs.append(l['pik']!=r['pik'])
if 'white' in cols2Match:
diffs.append(len({k: l[k] for k in self.paramDict['raceethvars'] if l[k]!=r[k]}))
# Age bin match
if l['agebin'] == r['agebin'] and sum(diffs) == 0:
binAgeCount += 1
# save match records
newRec = {}
newRec.update({k: l[k] for k in colsFromLeft})
# rename out dict if same vars coming from right if necessary
if 'cef_white' in colsFromRight:
nr = self.renameMatchDict(r, 'cef')
newRec.update({k: nr[k] for k in colsFromRight})
elif 'syn_white' in colsFromRight:
nr = self.renameMatchDict(r, 'syn')
newRec.update({k: nr[k] for k in colsFromRight})
elif any('mdf' in x for x in colsFromRight):
nr = self.renameMatchDict(r, 'mdf')
newRec.update({k: nr[k] for k in colsFromRight})
elif any(self.rightarg[0:3] in x for x in colsFromRight):
nr = self.renameMatchDict(r, self.rightarg[0:3])
newRec.update({k: nr[k] for k in colsFromRight})
else:
newRec.update({k: r[k] for k in colsFromRight})
newRec.update({matchFlagName:3})
matchDF = matchDF.append(newRec,ignore_index=True)
del leftBlk[i]
del rightBlk[j]
break
# compare residual after exact and fzyage, we don't want to remove oneoffs for this
if compareResidual == True and leftBlk:
js = self.histDiff(leftBlk, rightBlk, cols2Match)
else:
js = -1
# one off comparison
if match2Do['oneoff'] == True:
# search for all fuzzy age matches in residual
for i in range(len(leftBlk)-1,-1,-1):
l = leftBlk[i]
# compare to each record on the right
for j, r in enumerate(rightBlk):
diffs = []
if 'sex' in cols2Match:
diffs.append(l['sex']!=r['sex'])
if 'pik' in cols2Match:
diffs.append(l['pik']!=r['pik'])
if 'white' in cols2Match:
diffs.append(len({k: l[k] for k in self.paramDict['raceethvars'] if l[k]!=r[k]}))
ageDiff = abs(l['age'] - r['age'])>self.paramDict['agefuzdiff']
if ageDiff==0 and sum(diffs) <= self.paramDict['fuzdiff']:
oneOffCount += 1
newRec = {}
newRec.update({k: l[k] for k in colsFromLeft})
# rename out dict if same vars coming from left and right
if 'cef_white' in colsFromRight:
nr = self.renameMatchDict(r, 'cef')
newRec.update({k: nr[k] for k in colsFromRight})
elif 'syn_white' in colsFromRight:
nr = self.renameMatchDict(r, 'syn')
newRec.update({k: nr[k] for k in colsFromRight})
elif any('mdf' in x for x in colsFromRight):
nr = self.renameMatchDict(r, 'mdf')
newRec.update({k: nr[k] for k in colsFromRight})
elif any(self.rightarg[0:3] in x for x in colsFromRight):
nr = self.renameMatchDict(r, self.rightarg[0:3])
newRec.update({k: nr[k] for k in colsFromRight})
newRec.update({matchFlagName:4})
matchDF = matchDF.append(newRec,ignore_index=True)
del leftBlk[i]
del rightBlk[j]
break
return exactCount, fzyAgeCount, binAgeCount, oneOffCount, matchDF, js
def validate(self,t,cols2Match):
"""Match function that performs the validation match. Puts matched
statistics into doneValQueue.
Args:
t (tuple): left and right tracts to be matched, (left, right)
cols2Match (list): list of strings with labels of the variables that
will be matched
"""
# set of matching variables
left = t[0]
right = t[1]
# get county, tract
if len(left) > 0:
county = left[0]['county']
tract = left[0]['tract']
else:
county = right[0]['county']
tract = right[0]['tract']
#logging.debug('validate(), {0}, {1}'.format(county, tract))
# initialize stats frame
blockStats = pd.DataFrame(columns=['county','tract','block',self.leftarg,
self.rightarg,'exact','fzyage','binage','oneoff','jsresid'])
# initialize val match frame
matchDFHeader = ['county',
'tract',
'block',
'sex',
'age',
'white',
'black',
'aian',
'asian',
'nhopi',
'sor',
'hisp',
self.rightarg[0:3]+'_sex',
self.rightarg[0:3]+'_age',
self.rightarg[0:3]+'_white',
self.rightarg[0:3]+'_black',
self.rightarg[0:3]+'_aian',
self.rightarg[0:3]+'_asian',
self.rightarg[0:3]+'_nhopi',
self.rightarg[0:3]+'_sor',
self.rightarg[0:3]+'_hisp',
'val_matchflag']
colsFromLeft = ['county','tract','block','sex','age','white','black',
'aian','asian','nhopi','sor','hisp']
colsFromRight = [self.rightarg[0:3]+'_sex',
self.rightarg[0:3]+'_age',
self.rightarg[0:3]+'_white',
self.rightarg[0:3]+'_black',
self.rightarg[0:3]+'_aian',
self.rightarg[0:3]+'_asian',
self.rightarg[0:3]+'_nhopi',
self.rightarg[0:3]+'_sor',
self.rightarg[0:3]+'_hisp']
matchDF = pd.DataFrame(columns=matchDFHeader)
# match vars
match2Do = {'exact':False,'fzyage':False,'binage':False,'oneoff':False}
for m in self.paramDict['validatematchestodo']:
match2Do[m] = True
# do match if left and right exist
if len(left) > 0 and len(right) > 0:
# validate array
test = [left[0], right[0]]
for tc in test:
if 'blockid' not in tc:
logging.info('EXIT: NO blockid')
logging.info('{0}'.format(tc))
sys.exit()
for p in cols2Match:
if p not in tc:
logging.info('EXIT: NO {0}'.format(p))
logging.info('{0}'.format(tc))
sys.exit()
# identify blocks as unit of work
bothIDs = list(set([x['blockid'] for x in left]) & set([x['blockid'] for x in right]))
onlyLeft = list(set([x['blockid'] for x in left]) -set([x['blockid'] for x in right]))
onlyRight = list(set([x['blockid'] for x in right]) -set([x['blockid'] for x in left]))
for b in onlyLeft:
stats = {'county':county,
'tract':b[0:6],
'block':b[6:],
self.leftarg:len([x for x in left if x['blockid']==b]),
self.rightarg:np.nan,
'exact':np.nan,
'fzyage':np.nan,
'binage':np.nan,
'oneoff':np.nan,
'jsresid':np.nan}
blockStats = blockStats.append(stats,ignore_index=True)
for b in onlyRight:
stats = {'county':county,
'tract':b[0:6],
'block':b[6:],
self.leftarg:np.nan,
self.rightarg:len([x for x in right if x['blockid']==b]),
'exact':np.nan,
'fzyage':np.nan,
'binage':np.nan,
'oneoff':np.nan,
'jsresid':np.nan}
blockStats = blockStats.append(stats,ignore_index=True)
# go block by block
for b in bothIDs:
# subset to target block
leftBlk = [x for x in left if x['blockid']==b]
rightBlk = [x for x in right if x['blockid']==b]
#logging.debug('rightBlk[0]: {0}'.format(rightBlk[0]))
# initialize counters
leftCount = len(leftBlk)
rightCount = len(rightBlk)
# do match
exactCount, fzyAgeCount, binAgeCount, oneOffCount, matchDF, js = self.match(
match2Do, cols2Match, leftBlk, rightBlk, colsFromLeft,
colsFromRight, matchDF,'val_matchflag', False)
# capture stats for block
stats = {'county':county,
'tract':b[0:6],
'block':b[6:],
self.leftarg:leftCount,
self.rightarg:rightCount,
'exact':exactCount,
'fzyage':fzyAgeCount,
'binage':binAgeCount,
'oneoff':oneOffCount,
'jsresid':js}
blockStats = blockStats.append(stats,ignore_index=True)
self.doneStatQueue.put(blockStats)
self.doneMatchQueue.put(matchDF)
# if both frames are empty, return NaN series for results
else:
stats = {'county':county,
'tract':tract,
'block':np.nan,
self.leftarg:np.nan,
self.rightarg:np.nan,
'exact':np.nan,
'fzyage':np.nan,
'binage':np.nan,
'oneoff':np.nan,
'jsresid':np.nan}
self.doneValQueue.put(blockStats)
def sortMatchDicts(self, match2Do, cols2Match, leftBlk, rightBlk):
"""Sort left and right data (list of dictionaries) by the variables to
be matched.
Args:
cols2Match (list): list of strings with labels of the variables that
will be matched
leftBlk (list): list of record dictionaries for left data
rightBlk (list): list of record dictionaries for right data
Returns:
leftBlk (list): sorted list of record dictionaries for left data
rightBlk (list): sorted list of record dictionaries for right
data
"""
# sort the lists of dicts
# if we are doing a putative match, sex and age, then sort by sex,
# either age or binage then, for left and for right also by random
# uniform
if cols2Match == ['sex','age']:
if match2Do['binage'] == True & match2Do['exact'] == False:
leftBlk = sorted(leftBlk, key=lambda k: '%s %02d' % (k['sex'],k['agebin']))
rightBlk = sorted(rightBlk, key=lambda k: '%s %02d %.12f' % (k['sex'],k['agebin'],k['r']))
else:
leftBlk = sorted(leftBlk, key=lambda k: '%s %03d' % (k['sex'],k['age']))
rightBlk = sorted(rightBlk, key=lambda k: '%s %03d %.12f' % (k['sex'],k['age'],k['r']))
if cols2Match == ['sex','age','pik']:
leftBlk = sorted(leftBlk, key=lambda k: '%s %03d %s' % (k['sex'],k['age'],k['pik']))
rightBlk = sorted(rightBlk, key=lambda k: '%s %03d %s' % (k['sex'],k['age'],k['pik']))
if cols2Match == ['sex','age','white','black','aian','asian','nhopi','sor','hisp']:
leftBlk = sorted(leftBlk, key=lambda k: '%s %03d %s %s %s %s %s %s %s' %
(k['sex'],
k['age'],
k['white'],
k['black'],
k['aian'],
k['asian'],
k['nhopi'],
k['sor'],
k['hisp']))
rightBlk = sorted(rightBlk, key=lambda k: '%s %03d %s %s %s %s %s %s %s' %
(k['sex'],
k['age'],
k['white'],
k['black'],
k['aian'],
k['asian'],
k['nhopi'],
k['sor'],
k['hisp']))
if cols2Match == ['sex','age','pik','white','black','aian','asian','nhopi','sor','hisp']:
leftBlk = sorted(leftBlk, key=lambda k: '%s %03d %s %s %s %s %s %s %s %s' %
(k['sex'],
k['age'],
k['pik'],
k['white'],
k['black'],
k['aian'],
k['asian'],
k['nhopi'],
k['sor'],
k['hisp']))
rightBlk = sorted(rightBlk, key=lambda k: '%s %03d %s %s %s %s %s %s %s %s' %
(k['sex'],
k['age'],
k['pik'],
k['white'],
k['black'],
k['aian'],
k['asian'],
k['nhopi'],
k['sor'],
k['hisp']))
return leftBlk, rightBlk
def renameMatchDict(self,d,prfx):
"""Rename keys in data (d). Necessary when the output data would
otherwise have variables with duplicate names.
Args:
d (dictionary): record dictionary
prfx (string): string to signify how variables will be renamed
Returns:
nr (dictionary): record dictionary with renamed keys
"""
if prfx == 'cef':
rename = {'age':'cef_age',
'sex':'cef_sex',
'white':'cef_white',
'black':'cef_black',
'aian':'cef_aian',
'asian':'cef_asian',
'nhopi':'cef_nhopi',
'sor':'cef_sor',
'hisp':'cef_hisp'}
if prfx == 'hdf':
rename = {'age':'hdf_age',
'sex':'hdf_sex',
'white':'hdf_white',
'black':'hdf_black',
'aian':'hdf_aian',
'asian':'hdf_asian',
'nhopi':'hdf_nhopi',
'sor':'hdf_sor',
'hisp':'hdf_hisp'}
if prfx == 'syn':
rename = {'age':'syn_age',
'sex':'syn_sex',
'white':'syn_white',
'black':'syn_black',
'aian':'syn_aian',
'asian':'syn_asian',
'nhopi':'syn_nhopi',
'sor':'syn_sor',
'hisp':'syn_hisp'}
if prfx == 'mdf':
rename = {'age':self.leftarg+'_age',
'sex':self.leftarg+'_sex',
'white':self.leftarg+'_white',
'black':self.leftarg+'_black',
'aian':self.leftarg+'_aian',
'asian':self.leftarg+'_asian',
'nhopi':self.leftarg+'_nhopi',
'sor':self.leftarg+'_sor',
'hisp':self.leftarg+'_hisp'}
if prfx == self.rightarg[0:3]:
rename = {'age':self.rightarg[0:3]+'_age',
'sex':self.rightarg[0:3]+'_sex',
'white':self.rightarg[0:3]+'_white',
'black':self.rightarg[0:3]+'_black',
'aian':self.rightarg[0:3]+'_aian',
'asian':self.rightarg[0:3]+'_asian',
'nhopi':self.rightarg[0:3]+'_nhopi',
'sor':self.rightarg[0:3]+'_sor',
'hisp':self.rightarg[0:3]+'_hisp'}
nr = {}
# split dict by whether it needs to be renamed
d1 = {k: d[k] for k in d if k in rename.keys()}
d2 = {k: d[k] for k in d if k not in rename.keys()}
# rename
d1 = dict((rename[k],v) for (k,v) in d1.items() if k in rename.keys())
# combine
nr.update(d1)
nr.update(d2)
return nr
def histDiff(self,l,r,cols2Match):
"""Compute the difference in histograms of l and r. Uses jensen-shannon.
Args:
l (list): list of dictionaries
r (list): list of dictionaries
cols2Match (list): list of strings with labels of the variables that
will be matched
Returns:
js (float): jensen-shannon measure between l and r
"""
#logging.debug('{0}'.format(l[0]))
for d in [l,r]:
df = pd.DataFrame(d)
gdf = df.groupby(cols2Match).size().reset_index().rename(columns={0:'count'})
gdf['sum'] = gdf['count'].sum()
gdf['freq'] = gdf['count']/gdf['sum']
if d == l:
lhist = gdf[cols2Match+['freq']]
if d == r:
rhist = gdf[cols2Match+['freq']]
#logging.debug('lhist: {0}'.format(lhist))
#logging.debug('rhist: {0}'.format(rhist))
join = lhist.merge(rhist, on=cols2Match, how='outer', suffixes=['_l','_r'])
join['freq_l'] = join['freq_l'].fillna(0)
join['freq_r'] = join['freq_r'].fillna(0)
#innerJoinDebug = lhist.merge(rhist, on=cols2Match, how='inner', suffixes=['_l','_r'])
#innerJoinDebug['freq_l'] = innerJoinDebug['freq_l'].fillna(0)
#innerJoinDebug['freq_r'] = innerJoinDebug['freq_r'].fillna(0)
#logging.debug('join: {0}'.format(join))
#logging.debug('geoid: {0} {1} {2}'.format(l[0]['tract'],l[0]['block'],join))
#logging.debug('innerJoinDebug: {0}'.format(innerJoinDebug))
js = spdist.jensenshannon(join['freq_l'],join['freq_r'])
#logging.debug('js: {0}'.format(js))
return js
def attachPIK(self,t,cols2Match):
"""Match function that performs the putative match. Puts matched
statistics into doneAtchPIKQueue and pik agumented reconstructed data in donePlusQueue.
Args:
t (tuple): left and right tracts to be matched, (left, right)
cols2Match (list): list of strings with labels of the variables that
will be matched
"""
# set of matching variables
left = t[0]
right = t[1]
# get county, tract
if len(left) > 0:
county = left[0]['county']
tract = left[0]['tract']
else:
county = right[0]['county']
tract = right[0]['tract']
# initialize stats frame
blockStats = pd.DataFrame(columns=['county','tract','block',self.leftarg,
self.rightarg,'exact','fzyage','binage'])
# initialize plus frame
matchDFHeader = ['county','tract','block','sex','age','white','black',
'aian','asian','nhopi','sor','hisp','pik','put_matchflag']
colsFromLeft = ['county','tract','block','sex','age','white','black',
'aian','asian','nhopi','sor','hisp']
colsFromRight = ['pik']
matchDF = pd.DataFrame(columns=matchDFHeader)
# match vars
match2Do = {'exact':False,'fzyage':False,'binage':False,'oneoff':False}
for m in self.paramDict['putativematchestodo']:
match2Do[m] = True
# exclude records from right that are missing piks
#right = [x for x in right if x['pik']!='']
# use keep flag on cef instead, this allows us to use only one block-pik combo
if self.rightarg == 'cef':
right = [x for x in right if x['keep']==1]
elif self.rightarg == 'cmrcl':
right = [x for x in right if x['pik']!='']
# do match if left and right exist
if len(left) > 0 and len(right) > 0:
# validate array
test = [left[0], right[0]]
for tc in test:
if 'blockid' not in tc:
logging.info('EXIT: NO blockid')
logging.info('{0}'.format(tc))
sys.exit()
for p in cols2Match:
if p not in tc:
logging.info('EXIT: NO {0}'.format(p))
logging.info('{0}'.format(tc))
sys.exit()
# identify blocks as unit of work
bothIDs = list(set([x['blockid'] for x in left]) & set([x['blockid'] for x in right]))
onlyLeft = list(set([x['blockid'] for x in left]) -set([x['blockid'] for x in right]))
onlyRight = list(set([x['blockid'] for x in right]) -set([x['blockid'] for x in left]))
for b in onlyLeft:
stats = {'county':county,
'tract':b[0:6],
'block':b[6:],
self.leftarg:len([x for x in left if x['blockid']==b]),
self.rightarg:np.nan,
'exact':np.nan,
'fzyage':np.nan,
'binage':np.nan}
blockStats = blockStats.append(stats,ignore_index=True)
for b in onlyRight:
stats = {'county':county,
'tract':b[0:6],
'block':b[6:],
self.leftarg:np.nan,
self.rightarg:len([x for x in right if x['blockid']==b]),
'exact':np.nan,
'fzyage':np.nan,
'binage':np.nan}
blockStats = blockStats.append(stats,ignore_index=True)
# go block by block
for b in bothIDs:
# subset to target block, slice mergeVars
leftBlk = [x for x in left if x['blockid']==b]
rightBlk = [x for x in right if x['blockid']==b]
leftCount = len(leftBlk)
rightCount = len(rightBlk)
exactCount, fzyAgeCount, binAgeCount, oneOffCount, matchDF, js = self.match(match2Do, cols2Match,
leftBlk, rightBlk, colsFromLeft, colsFromRight, matchDF,'put_matchflag',False)
# capture stats for block
stats = {'county':county,