-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongCOVID_Feature_Extraction.py
145 lines (112 loc) · 4.02 KB
/
LongCOVID_Feature_Extraction.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import os
from datetime import *
import math
import sys
from csv import DictWriter
# In[2]:
rootdir = "/labs/mpsnyder/LongCovidEkanath/COVID_Positives/COVID_Positives_Final_data"
idmapping = pd.read_csv("/labs/mpsnyder/long-covid-study-data/additional_src_files/idmapping.csv", dtype='str')
date_format = "%Y-%m-%d"
ID_bottom = []
# In[3]:
def count_len_csv(files, path):
total_count = 0
for f in files:
updated = path + '/' + f
df = pd.read_csv(updated)
total_count += len(df)
return total_count
# In[4]:
def device_length(files, path):
for f in files:
updated = path + '/' + f
df = pd.read_csv(updated)
date = df.loc[:, 'Start_Date']
if not date.empty:
start_date = date[0]
end_date = date[len(df) -1]
try:
formatted_start = datetime.strptime(start_date, date_format)
formatted_end = datetime.strptime(end_date, date_format)
date_diff = str(formatted_end - formatted_start)
day = (date_diff.split())[0]
return int(day)
except:
print("Cannot convert date")
# In[5]:
def gaps_numb(files, path):
count = 0
for f in files:
updated = path + '/' + f
df = pd.read_csv(updated)
date_col = df.loc[:, 'Start_Date']
if not date_col.empty:
prev = datetime.strptime(date_col[0], date_format)
for i in range(1, len(df)):
cur_value = date_col[i]
try:
cur_date = datetime.strptime(date_col[i], date_format)
if cur_date - prev > timedelta(days = 3):
count += 1
prev = cur_date
except:
print("cannot convert date")
ID_bottom.append((os.path.basename(path), f))
return count
# In[17]:
def mean_features(path):
df = pd.read_csv(path)
if not df.empty:
avgs = []
dates = df['Start_Date']
values = df['Value']
cur_date = dates[0]
cur_sum = 0
count_cur_date = 0
for i in range(1, len(df)):
cur_day = dates[i]
if cur_day == cur_date and type(values[i]) != str:
cur_sum += values[i]
count_cur_date += 1
else:
if count_cur_date != 0:
avgs.append(cur_sum / count_cur_date)
cur_date = cur_day
return np.mean(avgs)
# In[9]:
def add_features(participant_id):
path = rootdir + "/" + participant_id
csv_files = os.listdir(path)
headers = ["id", "data_count", "num_gaps", "device_time", "mean_hr", "mean_st"]
values = {}
if len(csv_files) != 0:
values["id"] = participant_id
values["data_count"] = count_len_csv(csv_files, path)
values["num_gaps"] = gaps_numb(csv_files, path)
values["device_time"] = device_length(csv_files, path)
if "hr.csv" in csv_files:
values["mean_hr"] = mean_features(path + "/" + "hr.csv")
if "hr.csv" not in csv_files:
values["mean_hr"] = -1.
if "st.csv" in csv_files:
values["mean_st"] = mean_features(path + "/" + "st.csv")
if "st.csv" not in csv_files:
values["mean_st"] = -1.
# In[ ]:
#Saving to csv file
with open('/labs/mpsnyder/long-covid-study-data/processed_features/processed_features_b1.csv', 'a') as f_object:
dictwriter_object = DictWriter(f_object, fieldnames=headers)
dictwriter_object.writerow(values)
f_object.close()
if __name__ == "__main__":
#### EDIT HERE, DEPENDING ON HOW BASH SCRIPT USES THIS .py FILE ####
participant_id = os.path.basename(sys.argv[1])
print("participant_id: ", participant_id)
### EDIT THE ABOVE IF DIFFERNT FORMAT ###
add_features(participant_id)
print("Done extracting features for participant: ", participant_id)