-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.py
144 lines (121 loc) · 5.92 KB
/
alerts.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
from datetime import datetime
import pandas as pd
import params
def pregmrs_alert(project):
"""
:param project_key: Project ID
:return:
"""
print("\n[{}] Getting records from the PREG-MRS REDCap project:".format(datetime.now()))
df = project.export_records(format_type='df', fields=params.LOGIC_FIELDS)
newborns = df[df['newborn_date'].notna()]
postpartum_woman = df[df['pmrs_study_group']==2]
df_records = postpartum_woman.index.get_level_values('record_id').difference(newborns.index.get_level_values('record_id'))
#print(df_records)
#print(postpartum_woman)
ppw_res = postpartum_woman.reset_index()
to_be_alert = ppw_res[ppw_res['record_id'].isin(df_records)][['record_id','study_number','pmrs_date']]
to_be_alert[['pmrs_date']] = to_be_alert[['pmrs_date']].apply(pd.to_datetime)
try:
alerts1 = to_be_alert[((datetime.today() - to_be_alert['pmrs_date']).dt.days <=4)]
except:
alerts1 = pd.DataFrame(columns=['record_id','study_number','pmrs_date'])
try:
alerts2 = to_be_alert[((datetime.today() - to_be_alert['pmrs_date']).dt.days <=9)&((datetime.today() - to_be_alert['pmrs_date']).dt.days >4)]
except:
alerts2 = pd.DataFrame(columns=['record_id','study_number','pmrs_date'])
try:
alerts3 = to_be_alert[((datetime.today() - to_be_alert['pmrs_date']).dt.days >9)]
except:
alerts3 = pd.DataFrame(columns=['record_id','study_number','pmrs_date'])
all_records = df.index.get_level_values('record_id')
#print(all_records)
completed_records = all_records.difference(alerts1['record_id'])
completed_records = completed_records.difference(alerts2['record_id'])
completed_records = completed_records.difference(alerts3['record_id'])
to_import_df = build_pregmrs_alert(df,completed_records,alerts1['record_id'].values,alerts2['record_id'].values,alerts3['record_id'].values)
to_import_dict = [{'record_id': rec_id, 'fu_status': participant.fu_status}
for rec_id, participant in to_import_df.iterrows()]
#print(to_import_dict)
response = project.import_records((to_import_dict))
print("[PREG-MRS] Alerts setup: {}".format(response.get('count')))
def build_pregmrs_alert(df, completed_records,alerts1, alerts2, alerts3):
dfres = df.reset_index()
data_to_import = pd.DataFrame(columns=['fu_status'])
for el in completed_records:
#print(el)
sn = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['study_number'].values[0]
ty = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['pmrs_study_group'].values[0]
#print(sn,ty)
type = params.type_dict[int(ty)]
if type == 'PPM':
nb_date = dfres[(dfres['record_id'] == el) & (dfres['redcap_event_name'] == 'newborn_arm_1')]['newborn_date'].values[0]
if nb_date != '':
status = '- COMPLETED'
else:
status = ''
else:
status = '- COMPLETED'
final_status = str(type)+ " "+ status
data_to_import.loc[el] = final_status
for el in alerts1:
sn = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['study_number'].values[0]
ty = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['pmrs_study_group'].values[0]
type = params.type_dict[int(ty)]
if type == 'PPM':
status = '- APPOINTMENT'
else:
print('WHAT?')
status = '???'
final_status = str(type)+ " "+ status
data_to_import.loc[el] = final_status
for el in alerts2:
sn = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['study_number'].values[0]
ty = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['pmrs_study_group'].values[0]
type = params.type_dict[int(ty)]
if type == 'PPM':
status = '- TO BE VISITED'
else:
print('WHAT?')
status = '???'
final_status = str(type)+ " "+ status
data_to_import.loc[el] = final_status
for el in alerts3:
sn = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['study_number'].values[0]
ty = dfres[(dfres['record_id']==el)&(dfres['redcap_event_name']=='pregmrs_arm_1')]['pmrs_study_group'].values[0]
type = params.type_dict[int(ty)]
if type == 'PPM':
status = '- UNREACH'
else:
print('WHAT?')
status = '???'
final_status = str(type)+ " "+ status
data_to_import.loc[el] = final_status
#print(data_to_import)
return data_to_import
def sn_cleaning(project, field_='study_number',event='pregmrs_arm_1'):
df_all = project.export_records(format_type='df', fields=params.LOGIC_FIELDS)
df = df_all.xs(event,level=1,drop_level=False)
#print(df[[field_]])
data_to_import = pd.DataFrame(columns=[field_])
for k,el in df[[field_]].T.items():
if str(el[field_])[0] == '':
#print(el[0][1:])
data_to_import.loc[k[0]] = el[0][1:]
#else:
#print(k[0],str(el[field_])+"!!!!!!!!!!!!!!!!!!!!")
to_import_dict = [{'record_id': rec_id, field_: participant[field_]}
for rec_id, participant in data_to_import.iterrows()]
response = project.import_records((to_import_dict))
print("[PREG-MRS] "+field_ +" CORRECTIONS: {}".format(response.get('count')))
def duplicates(project):
df_all = project.export_records(format_type='df', fields=params.LOGIC_FIELDS)
df = df_all.xs('pregmrs_arm_1',level=1,drop_level=False)
repeated = df.groupby('study_number').count()[['pmrs_date']].sort_values('pmrs_date')
s = 0
for k,el in repeated.T.items():
if el[0] != 1:
s+= 1
#print(k,el[0])
if s == 0:
print("END: There is no repeated participant to clean.")