-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurate_covid_positive.py
338 lines (271 loc) · 11.1 KB
/
curate_covid_positive.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
"""
Module name: curate_covid_positive.py
Description:
This module provides functions to curate and consolidate COVID-19 positive case records
from multiple data sources. It extracts relevant data from various sources and saves a
consolidated table. The supported data sources include SGSS, Pillar 2 antigen testing,
GDPPR, HES-APC (diagnosis), and CHESS.
"""
from pyspark.sql import functions as f, DataFrame
from functools import reduce
from typing import List
from .environment_utils import get_spark_session
from .table_management import load_table, save_table
from .csv_utils import read_csv_file
def create_covid_positive_table(table_key: str = 'covid_positive', extraction_methods: List[str] = None) -> None:
"""
Create a consolidated DataFrame containing positive COVID-19 records from multiple sources and save it to a table.
Args:
table_key (str, optional): The table key of the table to save the consolidated data.
Defaults to 'covid_positive'.
extraction_methods (List[str], optional): List of methods for extracting positive COVID-19 records.
Defaults to None.
Allowed values are: 'sgss', 'pillar_2', 'gdppr', 'hes_apc_any_diagnosis', 'hes_apc_primary_diagnosis', 'chess'.
Returns:
None
"""
if extraction_methods is None:
extraction_methods = ['sgss', 'pillar_2', 'gdppr', 'hes_apc_any_diagnosis', 'hes_apc_primary_diagnosis', 'chess']
# Get spark session
spark = get_spark_session()
# Extract COVID-19 records from multiple sources
covid_positive_from_sources = [extract_covid_positive_records(method) for method in extraction_methods]
covid_positive = reduce(DataFrame.unionByName, covid_positive_from_sources)
# Save the consolidated data to a table
save_table(covid_positive, table_key)
# Clear cache
spark.catalog.clearCache()
def extract_covid_positive_records(extract_method: str) -> DataFrame:
"""
Extract covid positive cases based on the specified data source.
Args:
extract_method (str): The data source to extract sex data from.
Allowed values are: 'sgss', 'pillar_2', 'gdppr', 'hes_apc_any_diagnosis', 'hes_apc_primary_diagnosis', 'chess'.
Returns:
DataFrame: DataFrame containing the extracted COVID-19 positive records.
The DataFrame includes columns for person ID, record date, code, covid status and data source.
"""
extraction_methods = {
'sgss': {
'extraction_function': covid_positive_from_sgss,
'data_source': 'sgss',
'load_method': 'sgss'
},
'pillar_2': {
'extraction_function': covid_positive_from_pillar_2,
'data_source': 'pillar_2',
'load_method': 'pillar_2'
},
'gdppr': {
'extraction_function': covid_positive_from_gdppr,
'data_source': 'gdppr',
'load_method': 'gdppr'
},
'hes_apc_any_diagnosis': {
'extraction_function': covid_positive_from_hes_apc_any_diagnosis,
'data_source': 'hes_apc_diagnosis',
'load_method': None
},
'hes_apc_primary_diagnosis': {
'extraction_function': covid_positive_from_hes_apc_primary_diagnosis,
'data_source': 'hes_apc_diagnosis',
'load_method': None
},
'chess': {
'extraction_function': covid_positive_from_chess,
'data_source': 'chess',
'load_method': 'chess'
}
}
if extract_method not in extraction_methods:
raise ValueError(
f"Invalid extract_method: {extract_method}. Allowed values are: 'sgss', 'pillar_2', 'gdppr', "
"'hes_apc_any_diagnosis', 'hes_apc_primary_diagnosis', 'chess'."
)
return extraction_methods[extract_method]['extraction_function'](
load_table(
table=extraction_methods[extract_method]['data_source'],
method=extraction_methods[extract_method]['load_method']
)
)
def covid_positive_from_sgss(sgss: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the SGSS table, ensuring distinct records.
Args:
sgss (DataFrame): DataFrame containing SGSS data.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_positive_sgss = (
sgss
.select('person_id', 'specimen_date', 'lab_report_date')
.withColumn(
'date',
f.when(
f.col('specimen_date').isNotNull(), f.col('specimen_date')
)
.otherwise(f.col('lab_report_date'))
)
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL)")
.select('person_id', 'date')
.distinct()
.withColumn('code', f.lit(None))
.withColumn('description', f.lit(None))
.withColumn('covid_status', f.lit('confirmed'))
.withColumn('data_source', f.lit('sgss'))
)
return covid_positive_sgss
def covid_positive_from_pillar_2(pillar_2: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the COVID-19 antigen testing (Pillar 2) table, ensuring distinct records.
Args:
pillar_2 (DataFrame): DataFrame containing COVID-19 antigen testing (Pillar 2) data.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_19_antigen_testing_snomed = (
read_csv_file('./codelists/covid_19_antigen_testing_snomed.csv')
.select('code', 'description', 'test_result')
)
covid_positive_pillar_2 = (
pillar_2
.select('person_id', f.to_date('teststartdate').alias('date'), f.col('testresult').alias('test_result_original'))
.withColumn(
'code',
f.when(
f.col('test_result_original').rlike("SCT:\\d+"),
f.regexp_extract('test_result_original', "SCT:(\\d+)", 1)
)
.otherwise(None)
)
.join(
f.broadcast(covid_19_antigen_testing_snomed),
on = 'code', how = 'left'
)
)
covid_positive_pillar_2.cache()
covid_positive_pillar_2 = (
covid_positive_pillar_2
.withColumn(
'test_result',
f.when(f.col('test_result').isNull(), f.col('test_result_original'))
.otherwise(f.col('test_result'))
)
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL) AND (test_result = 'Positive')")
.select('person_id', 'date', 'code', 'description')
.distinct()
.withColumn('covid_status', f.lit('confirmed'))
.withColumn('data_source', f.lit('pillar_2'))
)
return covid_positive_pillar_2
def covid_positive_from_gdppr(gdppr: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the GDPPR table, ensuring distinct records.
Args:
gdppr (DataFrame): DataFrame containing GDPPR demographics data.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_19_infection_snomed_codelist = (
read_csv_file("./codelists/covid_19_infection_snomed.csv")
.select('code', 'description', 'covid_status')
)
covid_positive_gdppr = (
gdppr
.select('person_id', 'date', 'record_date', 'code')
.withColumn('date', f.when(f.col('date').isNull(), f.col('record_date')).otherwise(f.col('date')))
.join(
f.broadcast(covid_19_infection_snomed_codelist),
on = 'code', how = 'inner'
)
)
covid_positive_gdppr.cache()
covid_positive_gdppr = (
covid_positive_gdppr
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL)")
.select('person_id', 'date', 'code', 'description', 'covid_status')
.distinct()
.withColumn('data_source', f.lit('gdppr'))
)
return covid_positive_gdppr
def covid_positive_from_hes_apc_any_diagnosis(hes_apc_diagnosis: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the HES-APC diagnosis table, accepting any diagnosis position,
and ensuring distinct records.
Args:
hes_apc_diagnosis (DataFrame): DataFrame containing HES-APC diagnosis table.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_19_infection_icd10_codelist = (
read_csv_file("./codelists/covid_19_infection_icd10.csv")
.select('code', 'description', 'covid_status')
)
covid_positive_hes_apc_matched = (
hes_apc_diagnosis
.select('person_id', f.col('epistart').alias('date'), 'code')
.join(
f.broadcast(covid_19_infection_icd10_codelist),
on = 'code', how = 'inner'
)
)
covid_positive_hes_apc_matched.cache()
covid_positive_hes_apc_any_diagnosis = (
covid_positive_hes_apc_matched
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL)")
.select('person_id', 'date', 'code', 'description', 'covid_status')
.distinct()
.withColumn('data_source', f.lit('hes_apc_any_diagnosis'))
)
return covid_positive_hes_apc_any_diagnosis
def covid_positive_from_hes_apc_primary_diagnosis(hes_apc_diagnosis: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the HES-APC diagnosis table, restricting to primary diagnosis,
and ensuring distinct records.
Args:
hes_apc_diagnosis (DataFrame): DataFrame containing HES-APC diagnosis table.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_19_infection_icd10_codelist = (
read_csv_file("./codelists/covid_19_infection_icd10.csv")
.select('code', 'description', 'covid_status')
)
covid_positive_hes_apc_matched = (
hes_apc_diagnosis
.select('person_id', f.col('epistart').alias('date'), 'code', 'diag_position')
.filter("diag_position = 1")
.join(
f.broadcast(covid_19_infection_icd10_codelist),
on = 'code', how = 'inner'
)
)
covid_positive_hes_apc_matched.cache()
covid_positive_hes_apc_primary_diagnosis = (
covid_positive_hes_apc_matched
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL)")
.select('person_id', 'date', 'code', 'description', 'covid_status')
.distinct()
.withColumn('data_source', f.lit('hes_apc_primary_diagnosis'))
)
return covid_positive_hes_apc_primary_diagnosis
def covid_positive_from_chess(chess: DataFrame) -> DataFrame:
"""
Extract COVID-19 positive records from the CHESS table, ensuring distinct records.
Args:
chess (DataFrame): DataFrame containing HES-APC diagnosis data.
Returns:
DataFrame: Processed DataFrame with metadata added.
"""
covid_positive_chess = (
chess
.select('person_id', f.col('hospitaladmissiondate').alias('date'), 'covid19')
.filter("(person_id IS NOT NULL) AND (date IS NOT NULL) AND (covid19 = 'Yes')")
.select('person_id', 'date')
.distinct()
.withColumn('code', f.lit(None))
.withColumn('description', f.lit(None))
.withColumn('covid_status', f.lit('confirmed'))
.withColumn('data_source', f.lit('chess'))
)
return covid_positive_chess