-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowth_data_parser.py
373 lines (341 loc) · 10.2 KB
/
growth_data_parser.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
"""
Reads growth statistical data (WHO or CDC) from file and writes SQL insert statements
Author: Anton Koba
"""
import getopt
import sys
CDC = 'cdc'
WHO = 'who'
CDC_STATURE = 'cdc-stat'
CDC_ARMC = 'cdc-armc'
WEIGHT_VELOCITY_1MON = 'weight_velocity_1mon'
WEIGHT_VELOCITY_2MON = 'weight_velocity_2mon'
CDC_FIELDS_MAPPING = {
'age': {
'table': 'cdc_by_age',
'fields': {
'SEX': 'sex',
'_AGEMOS1': 'agemos1',
'_LLG1': 'llg1',
'_MLG1': 'mlg1',
'_SLG1': 'slg1',
'_AGEMOS2': 'agemos2',
'_LLG2': 'llg2',
'_MLG2': 'mlg2',
'_SLG2': 'slg2',
'_AGECAT': 'agecat',
'_LHT1': 'lht1',
'_MHT1': 'mht1',
'_SHT1': 'sht1',
'_LHT2': 'lht2',
'_MHT2': 'mht2',
'_SHT2': 'sht2',
'_LWT1': 'lwt1',
'_MWT1': 'mwt1',
'_SWT1': 'swt1',
'_LWT2': 'lwt2',
'_MWT2': 'mwt2',
'_SWT2': 'swt2',
'_LBMI1': 'lbmi1',
'_MBMI1': 'mbmi1',
'_SBMI1': 'sbmi1',
'_LBMI2': 'lbmi2',
'_MBMI2': 'mbmi2',
'_SBMI2': 'sbmi2',
'_LHC1': 'lhc1',
'_MHC1': 'mhc1',
'_SHC1': 'shc1',
'_LHC2': 'lhc2',
'_MHC2': 'mhc2',
'_SHC2': 'shc2',
}
},
'length': {
'table': 'cdc_by_length',
'fields': {
'SEX': 'sex',
'_LG1': 'lg1',
'_LWLG1': 'lwlg1',
'_MWLG1': 'mwlg1',
'_SWLG1': 'swlg1',
'_LG2': 'lg2',
'_LWLG2': 'lwlg2',
'_MWLG2': 'mwlg2',
'_SWLG2': 'swlg2',
'_htcat': 'htcat',
},
},
'height': {
'table': 'cdc_by_height',
'fields': {
'SEX': 'sex',
'_htcat': 'htcat',
'_HT1': 'ht1',
'_LWHT1': 'lwht1',
'_MWHT1': 'mwht1',
'_SWHT1': 'swht1',
'_HT2': 'ht2',
'_LWHT2': 'lwht2',
'_MWHT2': 'mwht2',
'_SWHT2': 'swht2',
}
},
}
WHO_FIELDS_MAPPING = {
'forage': {
'table': 'who_by_age',
'fields': {
'sex': 'sex',
'_agedays': 'agedays',
'_bmi_l': 'bmil',
'_bmi_m': 'bmim',
'_bmi_s': 'bmis',
'_tsf_l': 'tsf_l',
'_tsf_m': 'tsf_m',
'_tsf_s': 'tsf_s',
'_ssf_l': 'ssf_l',
'_ssf_m': 'ssf_m',
'_ssf_s': 'ssf_s',
'_armc_l': 'armc_l',
'_armc_m': 'armc_m',
'_armc_s': 'armc_s',
'_headc_l': 'headc_l',
'_headc_m': 'headc_m',
'_headc_s': 'headc_s',
'_wei_l': 'wei_l',
'_wei_m': 'wei_m',
'_wei_s': 'wei_s',
'_len_l': 'len_l',
'_len_m': 'len_m',
'_len_s': 'len_s',
}
},
'forlen': {
'table': 'who_by_length',
'fields': {
'sex': 'sex',
'_len': 'len',
'_wfl_l': 'wfl_l',
'_wfl_m': 'wfl_m',
'_wfl_s': 'wfl_s',
},
},
}
CDC_STAT_FIELDS_MAPPING = {
'age': {
'table': 'cdc_stat_by_age',
'fields': {
'sex': 'sex',
'agemos': 'agemos',
'L': 'l',
'M': 'm',
'S': 's',
'P3': 'p3',
'P5': 'p5',
'P10': 'p10',
'P25': 'p25',
'P50': 'p50',
'P75': 'p75',
'P90': 'p90',
'P95': 'p95',
'P97': 'p97',
}
},
}
CDC_ARMC_FIELDS_MAPPING = {
'age': {
'table': 'cdc_armc_by_age',
'fields': {
'Sex': 'sex',
'Age': 'age',
'L': 'l',
'M': 'm',
'S': 's',
'P01': 'p01',
'P1': 'p1',
'P3': 'p3',
'P5': 'p5',
'P10': 'p10',
'P25': 'p25',
'P50': 'p50',
'P75': 'p75',
'P90': 'p90',
'P95': 'p95',
'P97': 'p97',
'P99': 'p99',
'P999': 'p999',
}
},
}
WEIGHT_VELOCITY_FIELDS_MAPPING_1MON = {
'age': {
'table': 'who_weight_velocity_1mon_by_age',
'fields': {
'Sex': 'sex',
'Interval': 'age',
'L': 'l',
'M': 'm',
'S': 's',
'Delta': 'delta',
'1st': 'p1',
'3rd': 'p3',
'5th': 'p5',
'15th': 'p15',
'25th': 'p25',
'50th': 'p50',
'75th': 'p75',
'85th': 'p85',
'95th': 'p95',
'97th': 'p97',
'99th': 'p99',
}
},
}
WEIGHT_VELOCITY_FIELDS_MAPPING_2MON = {
'age': {
'table': 'who_weight_velocity_2mon_by_age',
'fields': {
'Sex': 'sex',
'agemos_from': 'agemos_from',
'agemos_to': 'agemos_to',
'L': 'l',
'M': 'm',
'S': 's',
'Delta': 'delta',
'1st': 'p1',
'3rd': 'p3',
'5th': 'p5',
'15th': 'p15',
'25th': 'p25',
'50th': 'p50',
'75th': 'p75',
'85th': 'p85',
'95th': 'p95',
'97th': 'p97',
'99th': 'p99',
}
},
}
TYPES_MAPPING = {
CDC: CDC_FIELDS_MAPPING,
WHO: WHO_FIELDS_MAPPING,
CDC_STATURE: CDC_STAT_FIELDS_MAPPING,
CDC_ARMC: CDC_ARMC_FIELDS_MAPPING,
WEIGHT_VELOCITY_1MON: WEIGHT_VELOCITY_FIELDS_MAPPING_1MON,
WEIGHT_VELOCITY_2MON: WEIGHT_VELOCITY_FIELDS_MAPPING_2MON,
}
def main(argv):
try:
opts, args = getopt.getopt(argv, 't:vh', ['help'])
# print opts
# print args
verbose = False
document_type = None
input_file = None
for option, argument in opts:
if option in ('-h', '--help'):
usage()
sys.exit()
if option == '-v':
verbose = True
if option == '-t':
if argument in (CDC, WHO, CDC_STATURE, CDC_ARMC, WEIGHT_VELOCITY_1MON, WEIGHT_VELOCITY_2MON):
document_type = argument
else:
print('Error: option -t (type) must be specified correctly')
usage()
sys.exit(2)
if not args or len(args) != 1:
print('Error: please specify input filename as last argument')
usage()
sys.exit(2)
else:
input_file = args[0]
process(input_file, document_type, verbose)
except getopt.GetoptError as err:
print('Error:')
print str(err)
usage()
sys.exit(2)
def usage():
print """
Usage:
python growth_data_parser.py -t (who|cdc|cdc-stat|cdc-armc) [-v] inputFile.csv
python growth_data_parser.py -h OR
python growth_data_parser.py --help - print this help message
"""
def process(input_file, document_type, verbose):
if verbose:
print('/* document type is: ' + document_type + ' */')
print('/* reading input file \'' + input_file + '\': */')
try:
with open(input_file) as inp:
element_positions = {}
first_line = True
line_number = 1
for line in inp:
line = line.strip()
line_elements = line.split(',')
if first_line:
element_positions = get_element_positions(line_elements)
else:
required_elements = get_required_elements(element_positions, line_elements, document_type, verbose)
if required_elements:
query = create_query(required_elements, line_number)
print(query)
first_line = False
line_number += 1
except IOError as err:
print('Error while processing file: ')
print(str(err))
def create_query(required_elements, num):
template = 'INSERT INTO {0} ({1}) VALUES ({2}); /* {3} */'
table_name = required_elements['table']
fields = required_elements['fields']
fields_list = []
values_list = []
for field, value in fields.items():
if value:
fields_list.append(field)
values_list.append(value)
return template.format(table_name, ','.join(fields_list), ','.join(values_list), num)
def get_element_positions(line_elements):
result = {}
index = 0
for element in line_elements:
result[element] = index
index += 1
# print(str(result))
return result
def get_required_elements(element_positions, split_elements, document_type, verbose):
result = {}
# fields_denom_map = CDC_FIELDS_MAPPING if CDC == document_type else WHO_FIELDS_MAPPING
fields_denom_map = TYPES_MAPPING.get(document_type)
# print(str(element_positions))
denom = split_elements[element_positions.get('denom', element_positions.get('_denom'))]
# if verbose:
# print('/* processing denom: ' + denom + ' */')
demon_map = fields_denom_map.get(denom)
if demon_map:
table_name = demon_map.get('table')
fields_map = demon_map.get('fields')
result_fields = {}
if fields_map:
for input_field, result_field in fields_map.iteritems():
# print(str(input_field) + ' -> ' + str(result_field))
position = element_positions.get(input_field)
if position >= 0:
value = split_elements[position]
# print('value: ' + str(value))
result_fields[result_field] = value
else:
print('/* cannot find position for element [' + str(input_field) + '] */')
print('/* positions: ' + str(element_positions) + ' */')
result['table'] = table_name
result['fields'] = result_fields
else:
print('/* no fields fields_denom_map for type ' + document_type + ' and denom ' + denom + ' */')
# print(str(result))
return result
if __name__ == '__main__':
main(sys.argv[1:])