-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytic_final.py
485 lines (295 loc) · 11.8 KB
/
analytic_final.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
339
340
341
# !/usr/bin/env python 3
print("Content-type: text/html\n\n")
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#read data created in survey_prog
df = pd.read_csv('survey_data_codes10.txt',names=
['patid',
'Medical_Home',
'age',
'Sex',
'doctorid',
'Language',
'survey_quarter',
'shift',
'appointment',
'fac_parking',
'fac_uncrowded',
'fac_navigate',
'fac_clean',
'r_welcome',
'r_checkin',
'r_expected',
'r_fees',
'r_atmosphere',
'r_waitingroom',
'r_ontime',
'doctor_focused',
'doctor_supportive',
'doc_answered_questions',
'followup_service_quality',
'followup_carryout',
'recommend',
'reenroll'])
#Make lists of variable names and question titles
vanames=list(df.columns[8:27])
questiontitles=[
'It was easy to make an appointment.',
'Parking was adequate.',
'Elevators and walk-ways had enough space for everyone.',
'It was easy to find my way around.',
'The facility was clean.',
'I felt welcomed by the staff.',
'I did not have to wait long to check in.',
'Staff were expecting me; my appointment was in the system.',
'I understood the reason for any fees and charges',
'I felt relaxed in the waiting room.',
'There was enough space for everyone in the waiting room.',
'My appointment occurred at the expected time.',
'My doctor was focused on me - not his/her computer.',
'My doctor made me feel calm and supported',
"My doctor took time to answer my questions.",
"It was easy to get the prescriptions and services that my doctor ordered.",
"I know exactly how to carry out my doctor's instructions.",
"I'd recommend this center to friends/on social media.",
"I want to re-enroll at my medical center."]
# Recode the Age variable for analysis
def recode(df):
df["intage"]=df['age'].astype(int)
bins = [0,65,100]
names = ['Less than 65', '65+']
df['AgeRange'] = pd.cut(df['intage'], bins, labels=names)
return df['AgeRange']
recode(df)
#Check data with xtabs
try:
pd.crosstab(df['survey_quarter'], df['reenroll'],normalize=True,margins=True)
except:
print("No crosstab produced.")
#Create elements for HTML table: Percent Agree
sumlist=[]
nlist=[]
perclist=[]
def percentagree(df):
for i in df.columns[8:27]:
xagree=(df[i]==5) | (df[i]==4)
xasum=xagree.sum()
xacount=xagree.count()
xaperc=round(100*xasum/xacount,1)
sumlist.append(xasum)
nlist.append(xacount)
perclist.append(xaperc)
return nlist , perclist
percentagree(df)
#Create elements for HTML table: counts
sagreelist=[]
agreelist=[]
neutlist=[]
disagreelist=[]
sdisagreelist=[]
def counts(df,nlist,perclist):
for i in df.columns[8:27]:
sagree=df[i]==5
agree=df[i]==4
neut=df[i]==3
disagree=df[i]==2
sdisagree=df[i]==1
sagree_sum=sagree.sum()
agree_sum=agree.sum()
neut_sum=neut.sum()
disagree_sum=disagree.sum()
sdisagree_sum=sdisagree.sum()
sagreelist.append(sagree_sum)
agreelist.append(agree_sum)
neutlist.append(neut_sum)
disagreelist.append(disagree_sum)
sdisagreelist.append(sdisagree_sum)
listoflist=[questiontitles,nlist,sdisagreelist,disagreelist,neutlist,agreelist,sagreelist,perclist]
tabcells=pd.DataFrame(listoflist)
tabcells_trans=tabcells.transpose()
tabcells_trans.columns=['Question', 'Count', 'SDisagree', 'Disagree','Neut','Agree','Sagree','perc']
tabcells_trans.to_csv('tabelems.csv', index=False)
counts(df,nlist,perclist)
# Create HTML table
filein = open('tabelems.csv',"r")
fileout = open("html-table_surv.html", "w")
tabledata = filein.readlines()
table = "<!DOCTYPE HTML>\n <http>\n"
table = table + "<head>\n <h1>Summary of Responses: Extent of patients' agreement with PX statements</h1>\n"
table = table + "<style>\n"
table = table + "table.center\n"
table = table + "table, td, th {width:100%; font-family:verdana;font-size:9;border: 1px black; border-collapse: collapse;padding: 8px;}\n"
table = table + "td {text-align: center;}\n"
table = table + "td:first-child {text-align: left;}\n"
table = table + "th {width:80px;background-color:#008B8B;color: white;word-wrap: normal;}\n"
table = table + "tr:nth-child(even) {background-color: #FFFFF0;}\n"
table = table + "</style>\n </head>\n <body>\n"
table = table + "<table class='center'>\n"
header=['Question',"Response Count","Strongly Disagree Count","Disagree Count","Neutral Count","Agree Count","Strongly Agree Count","Percent Favorable"]
table = table + " <tr>\n" #adds the first row which is the header
for column in header:
table += "<th>{0}</th>".format(column)
table += " </tr>\n"
for line in tabledata[1::]:
elem = line.split(",")
table =table+ " <tr>"
for column in elem:
table = table + "<td>{0}</td>".format(column)
table += " </tr>\n"
table += "</table>"
table +="</body>"
table += "</html>"
fileout.writelines(table)
fileout.close()
#Create charts for pxcenter.html home page
demovars=['Medical_Home','Sex','Language','AgeRange']
def frontcharts(df):
for i in demovars:
df.pivot_table(df.columns[25], [i], 'survey_quarter',aggfunc='mean',dropna=True).plot.bar()
plt.xticks(rotation=0,fontsize=14)
plt.ylabel('Avg. Agreement \n (1=Lowest, 5=Highest',fontsize=14)
plt.ylim(0,5)
plt.legend(ncol=4,loc=9,title="Survey Quarter",fontsize=12)
plt.title("By {x}: {0}".format(questiontitles[17],x=i),fontsize=16,wrap=True)
plt.savefig("html_charts/"+i+'fig_home.png')
plt.close()
frontcharts(df)
#Create detailed charts for each patient pop group and save for child pages
def charts_by_sex(df):
qindex=0
nobs=str(len(df))
for i in df.columns[8:27]:
df.pivot_table(i, 'Sex', 'survey_quarter',dropna=True,aggfunc='mean').plot.bar()
plt.xticks(rotation=0,fontsize=14)
plt.ylabel('Avg. Agreement \n (1=Lowest, 5=Highest',fontsize=14)
plt.ylim(0,5)
plt.legend(ncol=4,loc=9,title="Survey Quarter",fontsize=12)
plt.title(questiontitles[qindex] + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.savefig("html_charts/fig_sex_breakout"+i)
plt.close()
qindex=qindex+1
charts_by_sex(df)
def charts_by_language(df):
qindex=0
nobs=str(len(df))
for i in df.columns[8:27]:
df.pivot_table(i, 'Language', 'survey_quarter',dropna=True,aggfunc='mean').plot.bar()
plt.xticks(rotation=0,fontsize=14)
plt.ylabel('Avg. Agreement \n (1=Lowest, 5=Highest',fontsize=14)
plt.ylim(0,5)
plt.legend(ncol=4,loc=9,title="Survey Quarter",fontsize=12)
plt.title(questiontitles[qindex] + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.savefig("html_charts/fig_lang_breakout"+i)
plt.close()
qindex=qindex+1
charts_by_language(df)
def charts_by_age(df):
qindex=0
nobs=str(len(df))
for i in df.columns[8:27]:
df.pivot_table(i, 'AgeRange', 'survey_quarter',dropna=True,aggfunc='mean').plot.bar()
plt.xticks(rotation=0,fontsize=14)
plt.ylabel('Avg. Agreement \n (1=Lowest, 5=Highest',fontsize=14)
plt.ylim(0,5)
plt.legend(ncol=4,loc=9,title="Survey Quarter",fontsize=12)
plt.title(questiontitles[qindex] + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.savefig("html_charts/fig_age_breakout"+i)
plt.close()
qindex=qindex+1
charts_by_age(df)
def charts_by_medhome(df):
qindex=0
nobs=str(len(df))
for i in df.columns[8:26]:
df.pivot_table(i, 'Medical_Home', 'survey_quarter',dropna=True,aggfunc='mean').plot.bar()
plt.xticks(rotation=0,fontsize=14)
plt.ylabel('Avg. Agreement \n (1=Lowest, 5=Highest',fontsize=14)
plt.ylim(0,5)
plt.legend(ncol=4,loc=9,title="Survey Quarter",fontsize=12)
plt.title(questiontitles[qindex] + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.savefig("html_charts/fig_medhome_breakout"+i)
plt.close()
qindex=qindex+1
charts_by_medhome(df)
#Create charts relating recommendation and reenrollment responses to other survey answers
#Save for child pages
def recommendation_charts(df):
metric=df.columns[25]
metric_quest=questiontitles[17]
qindex=0
nobs=str(len(df))
for i in df.columns[8:25]:
boxprops = dict(linestyle='-', linewidth=3, color='g')
medianprops= dict(linestyle='--', linewidth=2.5, color='m')
meanpointprops = dict(marker='D', markeredgecolor='black',markerfacecolor='red')
boxplot = df.boxplot(column=[df.columns[25]],by=[i],showmeans=True,meanprops=meanpointprops,boxprops=boxprops,medianprops=medianprops,grid=False)
plt.title(metric_quest + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.xlabel(questiontitles[qindex] + "(1=Lowest, 5=Highest)",wrap=True,fontsize=12)
plt.suptitle("")
plt.subplots_adjust(bottom=0.2)
plt.ylabel('Agreement \n (1=Lowest, 5=Highest',fontsize=12)
plt.xticks(rotation=0)
plt.savefig("html_charts/fig_rec"+i)
plt.close()
qindex=qindex+1
recommendation_charts(df)
def reenrollment_charts(df):
try:
metric=df.columns[26]
metric_quest=questiontitles[18]
qindex=0
nobs=str(len(df))
for i in df.columns[8:25]:
boxprops = dict(linestyle='-', linewidth=3, color='g')
medianprops= dict(linestyle='--', linewidth=2.5, color='m')
meanpointprops = dict(marker='D', markeredgecolor='black',markerfacecolor='red')
boxplot = df.boxplot(column=[df.columns[26]],by=[i],showmeans=True,meanprops=meanpointprops,boxprops=boxprops,medianprops=medianprops,grid=False)
plt.title(metric_quest + " (N=" + nobs+")",fontsize=16,wrap=True)
plt.xlabel(questiontitles[qindex] + "(1=Lowest, 5=Highest)",wrap=True,fontsize=12)
plt.suptitle("")
plt.subplots_adjust(bottom=0.2,left=.2)
plt.ylabel('Agreement \n (1=Lowest, 5=Highest',fontsize=12)
plt.xticks(rotation=0)
plt.savefig("html_charts/fig_reenroll"+i)
plt.close()
qindex=qindex+1
except:
print("No chart produced.")
reenrollment_charts(df)
#Create correlations and correlation tables for recommendation and reenrollment child pages
def reenroll_corrs(df):
try:
metric_quest=questiontitles[18]
varnames=list(df.columns[8:25])
coefflist_reen=[]
for i in df.columns[8:25]:
cf=df['reenroll'].corr(df[i])
coefflist_reen.append(cf)
plt.barh(varnames,coefflist_reen)
plt.xlabel("Correlation Coefficient")
plt.title("Correlations to: "+metric_quest,wrap=True)
plt.savefig("Renroll_corrs.png",bbox_inches='tight')
plt.show()
plt.close()
except:
print("No chart produced.")
reenroll_corrs(df)
def rec_corrs(df):
try:
metric_quest=questiontitles[17]
varnames=list(df.columns[8:25])
coefflist_rec=[]
for i in df.columns[8:25]:
cf=df['recommend'].corr(df[i])
coefflist_rec.append(cf)
plt.barh(varnames,coefflist_rec)
plt.xlabel("Correlation Coefficient")
plt.title("Correlations to: "+metric_quest,wrap=True)
plt.savefig("recommend_corrs.png",bbox_inches='tight')
plt.show()
plt.close()
except:
print("No chart produced.")
rec_corrs(df)
filein.close()