-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutbreak detection python.py
326 lines (267 loc) · 9.72 KB
/
outbreak detection python.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
# -*- coding: utf-8 -*-
"""
Outbreak Detection Surveillance Adaptation
Created on Mon Feb 17 12:55:50 2025
"""
from transforms.api import transform_df, Input, Output
from pyspark.sql import functions as F
from myproject.datasets import constants
invalid_foods = constants.INVALID_FOOD_ITEMS
# Clean string values
def clean_data(df):
"""
Remove empty strings, double quotes (including smart quotes), and trim white spaces from string columns.
Converts empty strings or strings with only spaces to null.
"""
# Define a pattern to match all types of double quotes
double_quotes_pattern = (
r"[\"\u201C\u201D]" # Matches standard (") and smart quotes (“ and ”)
)
for column_name, dtype in df.dtypes:
if dtype == "string":
# Replace empty or whitespace-only strings with None, trim whitespaces, and remove double quotes
df = df.withColumn(
column_name,
F.when(F.trim(F.col(column_name)) == "", None).otherwise(
F.regexp_replace(
F.trim(F.col(column_name)), double_quotes_pattern, ""
)
),
)
return df
def aggregate_column(
df,
group_col,
agg_col,
alias,
dataset_name=None,
sort=False,
arr_dis=True,
delimiter=";",
):
"""
Aggregates a column by collecting non-null/empty values, optionally sorting,
and concatenating into a single string. Handles 'unknown' for specific datasets.
"""
# Sort dataset
if "CDCID" in df.columns or "OutbreakMainID" in df.columns:
primary_id_col = "CDCID" if "CDCID" in df.columns else "OutbreakMainID"
if "Id" in df.columns:
df = df.orderBy(primary_id_col, "Id")
else:
df = df.orderBy(primary_id_col)
# Collect non-null and non-empty values
expr = F.collect_list(F.when(F.trim(F.col(agg_col)) != "", F.col(agg_col)))
# Sorting, if required
if sort:
expr = F.sort_array(expr)
# Handle 'unknown' values for etiology dataset
if dataset_name == "etiology":
# Normalize 'unknown' variations to 'unknown' using regex
expr = F.transform(
expr,
lambda x: F.when(
F.lower(F.trim(x)).rlike(r"^(unknown|unknown\s+.*)$"), "unknown"
).otherwise(x),
)
# Remove duplicates if arr_dis is True
if arr_dis:
expr = F.array_distinct(expr)
# Check if all values are 'unknown'
is_all_unknown = (
(F.size(expr) > 0)
& (F.size(F.array_distinct(expr)) == 1)
& F.array_contains(expr, "unknown")
)
concatenated = F.when(is_all_unknown, F.lit("unknown")).otherwise(
F.concat_ws(delimiter, expr)
)
else:
# Non-etiology case: Handle array distinct and concatenation
expr = F.array_distinct(expr) if arr_dis else expr
concatenated = F.concat_ws(delimiter, expr)
# Replace empty results or delimiter-only strings with null
result = F.when(F.trim(concatenated) == "", F.lit(None)).otherwise(concatenated)
# Perform the aggregation
return df.groupBy(group_col).agg(result.alias(alias))
@transform_df(
Output("ri.foundry.main.dataset.264feeab-2a02-4389-9616-9cfa648fc542"),
obmain=Input("ri.foundry.main.dataset.390fe829-8b1a-4085-8d7e-603245e9a519"),
setting=Input("ri.foundry.main.dataset.26e4d9e8-136b-4f0e-8244-336afcfc5cb3"),
foodvehicle=Input("ri.foundry.main.dataset.0f0913b3-2886-4da6-bda2-f6f5a570b17c"),
foodingredient=Input(
"ri.foundry.main.dataset.397d7d72-bc8e-4af9-8544-6125a952813a"
),
catassignment=Input("ri.foundry.main.dataset.e40c0107-47d8-4e88-ba1b-7f79572e20a8"),
etiology=Input("ri.foundry.main.dataset.259b50df-b420-4283-8401-a45b163c548f"),
water_exp=Input("ri.foundry.main.dataset.73559d80-b115-4377-9bb0-b1f598049b47"),
# Old datasets
# obmain=Input("ri.foundry.main.dataset.7f526c1c-df76-47b3-9a8b-8d08b285b889"),
# setting=Input("ri.foundry.main.dataset.ef7f5d04-261f-4562-b9c8-7be39d436615"),
# foodvehicle=Input("ri.foundry.main.dataset.027aaff4-32b8-4ef7-8682-29640387bd77"),
# foodingredient=Input(
# "ri.foundry.main.dataset.dc4b0868-ca47-4450-a7b6-b5c84292917f"
# ),
# catassignment=Input("ri.foundry.main.dataset.6f3a724b-9f7a-417e-bb52-ead57e746b0b"),
)
def compute(
obmain, setting, foodvehicle, foodingredient, catassignment, etiology, water_exp
):
# Apply the cleaning function to raw data
setting = clean_data(setting)
foodvehicle = clean_data(foodvehicle)
foodingredient = clean_data(foodingredient)
catassignment = clean_data(catassignment)
etiology = clean_data(etiology)
# Setting
aggregated_setting = aggregate_column(
setting, "OutbreakMainID", "SettingName", "Setting"
)
df = obmain.join(aggregated_setting, on="OutbreakMainID", how="left")
# Food Vehicle
aggregated_foodvehicle = aggregate_column(
foodvehicle, "OutbreakMainID", "FoodName", "FoodVehicle"
)
df = df.join(aggregated_foodvehicle, on="OutbreakMainID", how="left")
# Food Contaminated Ingredient
aggregated_foodingredient = aggregate_column(
foodingredient, "OutbreakMainID", "IngredientName", "FoodContaminatedIngredient"
)
df = df.join(aggregated_foodingredient, on="OutbreakMainID", how="left")
# Water Exposure
aggregated_water_exp = aggregate_column(
water_exp, "CDCID", "WaterExposure", "WaterExposure", sort=True
)
df = df.join(aggregated_water_exp, on="CDCID", how="left")
# Animal Type
animaltype = (
catassignment.filter(F.col("CategoryGroup") == "Animal (Dashboard)")
.select("CDCID", "LVL1")
.groupBy("CDCID")
.agg(
F.concat_ws(
";", F.sort_array(F.array_distinct(F.collect_list("LVL1")))
).alias("AnimalType")
)
)
df = df.join(
animaltype.withColumnRenamed("CDCID", "OutbreakMainID"),
on="OutbreakMainID",
how="left",
)
# Water Type
watertype = (
catassignment.filter(
(F.col("CategoryGroup") == "Implicated Water System")
| (F.col("CategoryGroup") == "Venue Type")
)
.select("CDCID", "LVL1")
.groupBy("CDCID")
.agg(
F.concat_ws(
";", F.sort_array(F.array_distinct(F.collect_list("LVL1")))
).alias("WaterType")
)
)
df = df.join(
watertype.withColumnRenamed("CDCID", "OutbreakMainID"),
on="OutbreakMainID",
how="left",
)
# Update WaterType column based on PrimaryMode
df = df.withColumn(
"WaterType",
F.when(F.col("PrimaryMode") == "Water", df["WaterType"]).otherwise(None),
)
# Etiology
# Genus and Species
genus_species = (
etiology.orderBy("CDCID", "Id")
.withColumn(
"GenusSpecies", F.concat_ws(" ", F.col("GenusName"), F.col("SpeciesName"))
)
.groupBy("CDCID")
.agg(F.concat_ws(";", F.collect_list("GenusSpecies")).alias("Etiology"))
.withColumnRenamed("CDCID", "OutbreakMainID")
)
df = df.join(genus_species, on="OutbreakMainID", how="left")
# Serotype
# Preprocess the SubtypeName column for the etiology dataset
etiology = (
etiology.orderBy("CDCID", "Id")
.withColumn(
"ProcessedSubtypeName",
F.when(
F.col("GenusName").startswith("Norovirus"),
F.concat_ws(
" ",
F.when(
F.col("Polymerase").isNotNull(), F.trim(F.col("Polymerase"))
).otherwise(""),
F.when(
F.col("Capsid").isNotNull(), F.trim(F.col("Capsid"))
).otherwise(""),
),
).otherwise(
F.when(
F.lower(F.trim(F.col("SubtypeName"))) == "unknown", "unknown"
).otherwise(F.col("SubtypeName"))
),
)
.withColumn(
"ProcessedSubtypeName",
F.when(F.trim(F.col("ProcessedSubtypeName")) == "", None).otherwise(
F.col("ProcessedSubtypeName")
),
)
)
serotype = aggregate_column(
etiology,
"CDCID",
"ProcessedSubtypeName",
"SerotypeOrGenotype",
dataset_name="etiology",
arr_dis=False,
)
df = df.join(
serotype.withColumnRenamed("CDCID", "OutbreakMainID"),
on="OutbreakMainID",
how="left",
)
# Confirmed Status
confirmed = aggregate_column(
etiology,
"CDCID",
"Confirmed",
"EtiologyStatus",
dataset_name="etiology",
arr_dis=False,
)
df = df.join(
confirmed.withColumnRenamed("CDCID", "OutbreakMainID"),
on="OutbreakMainID",
how="left",
)
df = df.select(
"OutbreakMainID",
"Year",
"Month",
"State",
"PrimaryMode",
"Etiology",
"SerotypeOrGenotype",
"EtiologyStatus",
"Setting",
"Illnesses",
"Hospitalizations",
"InfoOnHospitalizations",
"Deaths",
"InfoOnDeaths",
"FoodVehicle",
"FoodContaminatedIngredient",
"IFSACCategory",
"WaterExposure",
"WaterType",
"AnimalType",
)
return df