-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport-data.php
300 lines (241 loc) · 6.23 KB
/
import-data.php
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
<?php
require_once 'include.php';
use VAERS\CSVReader;
use VAERS\ESWriter;
$bulk_limit = 1000;
$data_clean = function ($data) {
if (!$data) return $data;
$data = array_filter($data);
$data['x1'] = 1; // 100%
$data = fix_dates($data);
$data = shorten_text($data);
$data['REACTIONS'] = combine_reactions($data) ?: null;
$data = fix_numdays($data);
$data = clean_nullable($data);
$data = has_fields($data);
return $data;
};
$first_run = false;
foreach ($years as $year)
{
echo "\n running year $year\n";
$index = ESWriter::getIndexName($year);
$es_writer = new ESWriter();
$csv_reader = new CSVReader();
$csv_reader->setRowCB($data_clean);
$data_generator = $csv_reader->lineGenerator(DATA_DIR . $year . 'VAERSDATA.csv');
$documents = [];
foreach ($data_generator as $data)
{
if (!$data) continue;
$documents[] = $data;
if (count($documents) >= $bulk_limit)
{
$es_writer->send_bulk($documents, $index, !$first_run);
$documents = [];
}
}
if ($documents)
{
$es_writer->send_bulk($documents, $index,!$first_run);
}
}
function clean_nullable($data): array
{
$fields = [
'ALLERGIES',
'OTHER_MEDS',
'HISTORY',
'LAB_DATA',
'CUR_ILL'
];
static $null_expressions;
if (!$null_expressions)
{
$null_expressions = array_flip([
'unknown',
'unk',
'na',
'no',
'n/a',
'none',
'none;',
'none.',
'none known',
'no known allergies',
'nkda',
'nka',
'no known drug allergy',
'no know drug or food allergies',
'no allergies to medications, food, or other products',
'none reported',
'none on file',
'not stated',
'no relevant history;',
'no relevant history',
'no relevant hx',
'no relevant hx;',
'no relevant hx.',
'no hx of drug allergy'
]);
}
foreach ($fields as $field)
{
if (!array_key_exists($field,$data)) continue;
$value = trim(strtolower($data[$field]));
if (array_key_exists($value, $null_expressions))
{
unset($data[$field]);
}
}
return $data;
}
function has_fields($data): array
{
$possible_fields = [
'OTHER_MEDS',
'CUR_ILL',
'HISTORY',
'ALLERGIES',
'LAB_DATA'
];
$extra_data = [];
foreach ($possible_fields as $possible_field)
{
if (!empty($data[$possible_field]))
{
$extra_data[] = $possible_field;
}
}
if ($extra_data)
{
$data['HAS_DATA'] = $extra_data;
}
return $data;
}
function combine_reactions($data): array
{
//make reactions into array field,
$reactions = [
['L_THREAT','Y'],
['DIED','Y'],
['HOSPITAL','Y'],
['DISABLE','Y'],
['RECOVD','N','!RECOVED'],
['ER_VISIT','Y'],
['ER_ED_VISIT','Y'],
['X_STAY','Y'],
];
$reactions_arr = [];
foreach ($reactions as $rp)
{
//reaction_pieces
$field = $rp[0];
$value = $rp[1];
$replacement = $rp[2] ?? null;
if (isset($data[$field]) && $data[$field] == $value)
{
$reaction_string = $replacement ?: $field;
$reactions_arr[] = $reaction_string;
}
}
return $reactions_arr;
}
/**
* @param $data
* @return mixed
*/
function fix_dates($data)
{
//fix dates -currently in MM/DD/YYYY - should be converted to YYYY-MM-DD
$date_fields = [
'DATEDIED',
'ONSET_DATE',
'RECVDATE',
'RPT_DATE',
'TODAYS_DATE',
'VAX_DATE'
];
foreach ($date_fields as $date_field)
{
if (!empty($data[$date_field]))
{
$date = $data[$date_field];
$date = str_replace('\\', '', $date);
$date_pieces = explode('/', $date);
if (count($date_pieces) !== 3)
{
echo "\ninvalid date format in: {$data['VAERS_ID']} in $date_field";
$data[$date_field] = null;
continue;
}
$day = str_pad($date_pieces[1], 2, '0', STR_PAD_LEFT);
$month = str_pad($date_pieces[0], 2, '0', STR_PAD_LEFT);
$year = $date_pieces[2];
$data[$date_field] = "$year-$month-$day";
}
}
return $data;
}
/**
* @param $data
* @return mixed
*/
function fix_numdays($data)
{
if (!isset($data['NUMDAYS']) && isset($data['VAX_DATE']) && isset($data['ONSET_DATE']))
{
// this doesn't make sense - probably that dates were confused.
if ($data['ONSET_DATE'] < $data['VAX_DATE'])
{
// $data['ADJUSTED'] = true;
$old_vaxdate = $data['VAX_DATE'];
$data['VAX_DATE'] = $data['ONSET_DATE'];
$data['ONSET_DATE'] = $old_vaxdate;
}
$onset = strtotime($data['ONSET_DATE']);
$vax = strtotime($data['VAX_DATE']);
$diff = $onset - $vax;
if ($diff >= 0)
{
$days = (int)floor($diff / (24 * 60 * 60));
$data['NUMDAYS'] = $days;
// $data['ADJUSTED'] = true;
}
else
{
echo "not sure this is supposed to happen\n";
}
}
if (($data['NUMDAYS'] ?? 0) > 10000)
{
unset($data['NUMDAYS']);
}
return $data;
}
function shorten_text($data)
{
$text_fields = [
'SYMPTOM_TEXT',
// 'LAB_DATA',
// 'HISTORY'
];
foreach ($text_fields as $field)
{
if (isset($data[$field]))
{
$text = wordwrap($data[$field]);
$text = keepXLines($text, 10);
$new_field = 'SHORT_'.$field;
$data[$new_field] = $text;
}
}
return $data;
}
function keepXLines($str, $num = 10)
{
$lines = explode("\n", $str);
$firsts = array_slice($lines, 0, $num);
return implode("\n", $firsts);
}
echo "\n finished\n";