/
hcup.jl
521 lines (471 loc) · 14.4 KB
/
hcup.jl
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
##### Beginning of file
import CSV
import CSVFiles
import DataFrames
import FileIO
# import selected names from PredictMD
import ..convert_value_to_missing!
import ..filename_extension
import ..fix_type
import ..is_nothing
"""
"""
function x_contains_y(
x::AbstractString,
y::AbstractVector{<:AbstractString},
)
if length(y) == 0
return false
end
for i = 1:length(y)
if occursin(y[i], x)
return true
end
end
return false
end
"""
"""
function symbol_begins_with(
x::Symbol,
y::AbstractString
)
if length(y) <= length(string(x)) && string(x)[1:length(y)] == y
return true
else
return false
end
return nothing
end
"""
Given a dataframe, return the column names corresponding to CCS "one-hot"
columns.
# Examples
```julia
import CSVFiles
import FileIO
import PredictMD
df = DataFrames.DataFrame(
FileIO.load(
MY_CSV_FILE_NAME;
type_detect_rows = 30_000,
)
)
@info(PredictMD.Cleaning.ccs_onehot_names(df))
@info(PredictMD.Cleaning.ccs_onehot_names(df, "ccs_onehot_"))
```
"""
function ccs_onehot_names(
df::DataFrames.AbstractDataFrame,
ccs_onehot_prefix::AbstractString = "ccs_onehot_",
)
result = column_names_with_prefix(
df,
ccs_onehot_prefix,
)
return result
end
"""
"""
function column_names_with_prefix(
df::DataFrames.AbstractDataFrame,
prefix::AbstractString,
)
all_names = DataFrames.names(df)
name_begins_with_prefix = Vector{Bool}(length(all_names))
for j = 1:length(all_names)
name_begins_with_prefix[j] = symbol_begins_with(
all_names[j],
prefix,
)
end
vector_of_matching_names = all_names[name_begins_with_prefix]
return vector_of_matching_names
end
"""
Given a single ICD 9 code, import the relevant patients from the
Health Care Utilization Project (HCUP) National Inpatient Sample (NIS)
database.
# Examples:
```julia
import CSVFiles
import FileIO
import PredictMD
icd_code_list = ["8841"]
icd_code_type=:procedure
input_file_name_list = [
"./data/nis_2012_core.csv",
"./data/nis_2013_core.csv",
"./data/nis_2014_core.csv",
]
output_file_name = "./output/hcup_nis_pr_8841.csv"
PredictMD.Cleaning.clean_hcup_nis_csv_icd9(
icd_code_list,
input_file_name_list,
output_file_name;
icd_code_type=icd_code_type,
rows_for_type_detect = 30_000,
)
df = DataFrames.DataFrame(
FileIO.load(
output_file_name;
type_detect_rows = 30_000,
)
)
@info(PredictMD.Cleaning.ccs_onehot_names(df))
```
"""
function clean_hcup_nis_csv_icd9(
icd_code_list::AbstractVector{<:AbstractString},
input_file_name_list::AbstractVector{<:AbstractString},
output_file_name::AbstractString;
header_row::Bool = true,
print_every_n_lines::Integer = 1_000_000,
icd_code_type::Union{Nothing, Symbol} = nothing,
num_dx_columns::Integer = 25,
num_pr_columns::Integer = 15,
ccs_onehot_prefix::AbstractString = "ccs_onehot_",
rows_for_type_detect::Union{Nothing, Integer} = nothing,
)
if is_nothing(rows_for_type_detect)
error("you need to specify rows_for_type_detect")
end
if rows_for_type_detect <= 0
error("rows_for_type_detect must be > 0")
end
if is_nothing(icd_code_type)
error("you need to specify icd_code_type")
end
if icd_code_type==:diagnosis
elseif icd_code_type==:procedure
else
error("\"icd_code_type\" must be one of: :diagnosis, :procedure")
end
if length(input_file_name_list) == 0
error("length(input_file_name_list) == 0")
end
input_file_name_list = strip.(input_file_name_list)
for i = 1:length(input_file_name_list)
if filename_extension(input_file_name_list[i]) != ".csv"
error("all input files must be .csv")
end
end
output_file_name = strip.(output_file_name)
if filename_extension(output_file_name) != ".csv"
error("output file must be .csv")
end
if ispath(output_file_name)
error(
string(
"Output file already exists. ",
"Rename, move, or delete the file, and then try again.",
)
)
end
temp_file_name_vector = Vector{String}(length(input_file_name_list))
for i = 1:length(input_file_name_list)
temp_file_name_vector[i] = string(tempname(), "_", i, ".csv")
end
icd_code_list = strip.(icd_code_list)
for i = 1:length(input_file_name_list)
if ispath(temp_file_name_vector[i])
error("ispath(temp_file_name_vector[i])")
end
@info(
string(
"Starting to read input file ",
i,
" of ",
length(input_file_name_list),
".",
)
)
open(input_file_name_list[i], "r") do f_input
open(temp_file_name_vector[i], "w") do f_temp_output
line_number = 1
for line in eachline(f_input)
if line_number == 1 && header_row
write(f_temp_output, line)
write(f_temp_output, "\n")
else
if x_contains_y(line, icd_code_list)
write(f_temp_output, line)
write(f_temp_output, "\n")
end
end
line_number += 1
if (print_every_n_lines >= 0) &&
(line_number % print_every_n_lines == 0)
@info(
string(
"Input file ",
i,
" of ",
length(input_file_name_list),
". Current line number: ",
line_number,
)
)
end
end
end
end
@info(
string(
"Finished reading input file ",
i,
" of ",
length(input_file_name_list),
".",
)
)
end
df_vector = Vector{DataFrames.DataFrame}(length(input_file_name_list))
for i = 1:length(temp_file_name_vector)
@info(
string(
"Starting to read temporary file ",
i,
" of ",
length(input_file_name_list),
".",
)
)
# df_i = DataFrames.readtable(temp_file_name_vector[i])
# We can't use DataFrames.readtable because it is deprecated.
df_i = DataFrames.DataFrame(
FileIO.load(
temp_file_name_vector[i];
type_detect_rows = rows_for_type_detect,
)
)
df_vector[i] = df_i
@info(
string(
"Finished reading temporary file ",
i,
" of ",
length(input_file_name_list),
".",
)
)
end
for i = 1:length(temp_file_name_vector)
Base.Filesystem.rm(
temp_file_name_vector[i];
force = true,
recursive = true,
)
end
all_column_names_vectors = [
DataFrames.names(df) for df in df_vector
]
shared_column_names = intersect(all_column_names_vectors...)
for i = 1:length(df_vector)
extra_column_names = setdiff(
names(df_vector[i]),
shared_column_names,
)
DataFrames.deletecols!(df_vector[i], extra_column_names,)
end
combined_df = vcat(df_vector...)
for i = 1:length(df_vector)
df_vector[i] = DataFrames.DataFrame()
end
if icd_code_type==:diagnosis
icd_code_column_names = Symbol[
Symbol( string("DX", j) ) for j = 1:num_dx_columns
]
elseif icd_code_type==:procedure
icd_code_column_names = Symbol[
Symbol( string("PR", j) ) for j = 1:num_pr_columns
]
else
error("\"icd_code_type\" must be one of: :diagnosis, :procedure")
end
row_i_has_kth_icd_code_matrix = Matrix{Bool}(
size(combined_df, 1),
length(icd_code_list),
)
for k = 1:length(icd_code_list)
current_icd_code = icd_code_list[k]
row_i_has_current_icd_code_in_col_j_matrix =
Matrix{Bool}(
size(combined_df, 1),
length(icd_code_column_names),
)
for j = 1:length(icd_code_column_names)
@info(
string(
"icd9 code ",
k,
" of ",
length(icd_code_list),
". DX column ",
j,
" of ",
length(icd_code_column_names),
".",
)
)
for i = 1:size(combined_df, 1)
cell_value = combined_df[i, icd_code_column_names[j]]
if DataFrames.ismissing(cell_value)
row_i_has_current_icd_code_in_col_j_matrix[i, j] = false
else
cell_value = strip(string(cell_value))
row_i_has_current_icd_code_in_col_j_matrix[i, j] =
cell_value == current_icd_code
end
end
end
row_i_has_current_icd_code_in_any_icdcode_column = vec(
sum(row_i_has_current_icd_code_in_col_j_matrix, 2) .> 0
)
row_i_has_kth_icd_code_matrix[:, k] =
row_i_has_current_icd_code_in_any_icdcode_column
end
matching_rows =
findall(Bool.(vec(sum(row_i_has_kth_icd_code_matrix, 2).>0)))
num_rows_before = size(combined_df, 1)
combined_df = combined_df[matching_rows, :]
num_rows_after = size(combined_df, 1)
@info(
string(
"I initially identified ",
num_rows_before,
" rows that could possibly have matched your ICD code(s).",
" I checked each row, and ",
num_rows_after,
" of those rows actually matched your ICD code(s).",
"I removed the ",
num_rows_before - num_rows_after,
" rows that did not match.",
)
)
dx_column_names = [Symbol(string("DX", i)) for i = 1:num_dx_columns]
dx_ccs_column_names =
[Symbol(string("DXCCS", i)) for i = 1:num_dx_columns]
index_to_ccs = strip.(
string.(
unique(
DataFrames.skipmissing(
vcat(
[combined_df[:, col] for
col in dx_ccs_column_names]...
)
)
)
)
)
index_to_ccs = index_to_ccs[findall(index_to_ccs .!= "")]
index_to_ccs = unique(index_to_ccs)
index_to_ccs = parse.(Int, index_to_ccs)
sort!(index_to_ccs)
index_to_ccs = string.(index_to_ccs)
ccs_to_index = Dict()
for k = 1:length(index_to_ccs)
ccs_to_index[ index_to_ccs[ k ] ] = k
end
ccs_to_index = fix_type(ccs_to_index)
row_i_has_vcode_dx_in_kth_ccs = Matrix{Bool}(
size(combined_df, 1),
length(index_to_ccs),
)
for j = 1:length(dx_column_names)
@info(
string(
"Processing DXCCS column ",
j,
" of ",
length(dx_column_names),
".",
)
)
jth_dx_col_name = dx_column_names[j]
jth_dx_ccs_col_name = dx_ccs_column_names[j]
for i = 1:size(combined_df, 1)
dx_value = combined_df[i, jth_dx_col_name]
if DataFrames.ismissing(dx_value)
else
dx_value = strip(string(dx_value))
if length(dx_value) == 0
elseif dx_value[1] == 'V' || dx_value[1] == "V"
ccs_value = combined_df[i, jth_dx_ccs_col_name]
if DataFrames.ismissing(ccs_value)
error(
error(
"dx value was not missing but",
"ccs value was missing"
)
)
else
ccs_value = strip(string(ccs_value))
row_i_has_vcode_dx_in_kth_ccs[
i,
ccs_to_index[ccs_value]
] = true
end
end
end
end
end
for k = 1:length(index_to_ccs)
kth_ccs = index_to_ccs[k]
kth_ccs_onehot_column_name = Symbol(
string(
ccs_onehot_prefix,
kth_ccs,
)
)
temporary_column_ints = Int.(row_i_has_vcode_dx_in_kth_ccs[:, k])
if sum(temporary_column_ints) > 0
temporary_column_strings = Vector{String}(size(combined_df, 1))
for i = 1:size(combined_df, 1)
if temporary_column_ints[i] > 0
temporary_column_strings[i] = "Yes"
else
temporary_column_strings[i] = "No"
end
end
combined_df[kth_ccs_onehot_column_name] =
temporary_column_strings
else
end
end
convert_value_to_missing!(
combined_df,
"A",
DataFrames.names(combined_df),
)
convert_value_to_missing!(
combined_df,
"C",
DataFrames.names(combined_df),
)
convert_value_to_missing!(
combined_df,
-99,
DataFrames.names(combined_df),
)
try
mkpath(dirname(output_file_name))
catch
end
@info(string("Attempting to write output file..."))
CSV.write(
output_file_name,
combined_df,
)
@info(
string(
"Wrote ",
size(combined_df, 1),
" rows to output file: \"",
output_file_name,
"\"",
)
)
return output_file_name
end
##### End of file