/
analyze_survey.py
290 lines (274 loc) · 10.4 KB
/
analyze_survey.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
from target_questions import Target
from survey_data import Survey
from response_data import Response
from datetime import datetime
import plotly
from plotly import tools
from plotly.graph_objs import Scatter, Layout, Bar
survey_data = Survey.survey_data
target_surveys = Target.target_info
response_data = Response.response_data
def get_questions(survey_id, target_survey, question_types):
id_info = {}
target_questions = target_survey["questions"]
id_info["survey_id"] = survey_id
for question_type in question_types:
id_info[question_type] = {}
id_info[question_type]["page_id"] = target_questions[question_type]["page_id"]
id_info[question_type]["question_id"] = target_questions[question_type]["question_id"]
return id_info
def initialize_answer_array(question_type, question_info):
survey_id, page_id, question_id = get_ids(question_info)
answer_array = {}
for survey in survey_data:
if survey['id'] == survey_id:
page_index, question_index = get_indexes(survey, question_info)
if question_type == "nps":
choices = survey["pages"][page_index]["questions"][question_index]["answers"]["choices"]
for choice in choices:
answer_array[choice["id"]] = 0
elif question_type == "components":
rows = survey["pages"][page_index]["questions"][question_index]["answers"]["rows"]
choices = survey["pages"][page_index]["questions"][question_index]["answers"]["choices"]
for row in rows:
answer_array[row["id"]] = {}
for choice in choices:
answer_array[row["id"]][choice["id"]] = 0
return answer_array
def get_ids(question_info):
survey_id = question_info["survey_id"]
page_id = question_info[question_type]["page_id"]
question_id = question_info[question_type]["question_id"]
return survey_id, page_id, question_id
def get_nps_responses(question_info):
survey_id, page_id, question_id = get_ids(question_info)
answer_array = initialize_answer_array(question_type, question_info)
for response_id, response in response_data[survey_id].items():
for page in response["pages"]:
if page["id"] == page_id:
for question in page["questions"]:
if question["id"] == question_id:
answer_id = question["answers"][0]["choice_id"]
answer_array[answer_id] += 1
return answer_array
def get_component_responses(question_info):
survey_id, page_id, question_id = get_ids(question_info)
answer_array = initialize_answer_array(question_type, question_info)
for response_id, response in response_data[survey_id].items():
for page in response["pages"]:
if page["id"] == page_id:
for question in page["questions"]:
if question["id"] == question_id:
for answer in question["answers"]:
row_id = answer["row_id"]
choice_id = answer["choice_id"]
answer_array[row_id][choice_id] += 1
return answer_array
def get_index(content_list, content_id):
return content_list.index(item)
def get_indexes(survey, question_info):
survey_id, page_id, question_id = get_ids(question_info)
page_index = None
question_index = None
for page in survey["pages"]:
if page["id"] == page_id:
page_index = survey["pages"].index(page)
for question in survey["pages"][page_index]["questions"]:
if question["id"] == question_id:
question_index = survey["pages"][page_index]["questions"].index(question)
return page_index, question_index
def match_answers(question_type, question_info, answer_array):
survey_id, page_id, question_id = get_ids(question_info)
for survey in survey_data:
if survey['id'] == survey_id:
page_index, question_index = get_indexes(survey, question_info)
choices = survey["pages"][page_index]["questions"][question_index]["answers"]["choices"]
if question_type == "nps":
for choice in choices:
answer_array[choice["text"]] = answer_array.pop(choice["id"])
elif question_type == "components":
rows = survey["pages"][page_index]["questions"][question_index]["answers"]["rows"]
for row in rows:
answer_array[row["text"]] = answer_array.pop(row["id"])
for choice in choices:
answer_array[row["text"]][choice["text"]] = answer_array[row["text"]].pop(choice["id"])
return answer_array
def calculate_nps(matched_answers):
zero_key = None
one_key = None
ten_key = None
if "0" not in matched_answers:
matched_answers["0"] = 0
for key, value in matched_answers.items():
if key.startswith("10 "):
ten_key = key
elif key.startswith("Extremely likely"):
ten_key = key
if key.startswith("1 ") or key.startswith("1<"):
zero_key = "0"
one_key = key
elif key == "Not at all likely - 0":
zero_key = key
one_key = "1"
promoters = (matched_answers[ten_key] + matched_answers["9"])
passives = (matched_answers["8"] + matched_answers["7"])
detractors = (matched_answers["6"] + matched_answers["5"] + matched_answers["4"] + matched_answers["3"] + matched_answers["2"] + matched_answers[one_key] + matched_answers[zero_key])
total = promoters + passives + detractors
nps = round((promoters - detractors)/total*100)
print ("Total respondants: " + str(total))
print ("NPS: " + str(nps))
return nps
def calculate_averages(matched_answers):
averages_dict = {}
for key, value in matched_answers.items():
total = 0
e = value["Excellent"] * 5
g = value["Good"] * 4
f = value["Fair"] * 3
p = value["Poor"] * 2
vp = value["Very Poor"] * 1
for description, count in value.items():
total += count
weighted_average = round(((e + g + f + p + vp)/total), 2)
averages_dict[key] = weighted_average
return averages_dict
def prepare_chart_data(chart_data):
x_axis = []
y_axis = []
chart_data.pop("Group Discussions", None)
chart_data.pop("Short Takes / TAD Talks", None)
chart_data.pop("Think Tanks", None)
for k, v in chart_data.items():
x_axis.append(k)
y_axis.append(v)
return x_axis, y_axis
def create_nps_chart(nps_chart_data):
final_data = []
for event_type, nps_data in nps_chart_data.items():
x_axis, y_axis = prepare_chart_data(nps_data)
if event_type == "SourceCon":
event_marker = dict(
size = 10,
color = 'rgb(103, 174, 68)'
)
event_textfont = dict(
color = 'rgb(103, 174, 68)'
)
elif event_type == "ERE Conference":
event_marker = dict(
size = 10,
color = 'rgb(22, 98, 133)'
)
event_textfont = dict(
color = 'rgb(22, 98, 133)'
)
chart_title = "Event NPS Scores"
final_data.append(Scatter(
x = x_axis,
y = y_axis,
mode='lines+text',
textposition='top left',
name = event_type,
marker = event_marker,
textfont = event_textfont,
text = y_axis
))
chart_layout = Layout(
margin = dict(
r = 150,
b = 200,
),
title = chart_title
)
plotly.offline.plot({
"data": final_data,
"layout": chart_layout
},
filename = "charts/nps_chart.html"
)
def get_component_averages(event_type, data):
component_averages = {}
for event_name, averages in data.items():
if (event_type == ("SourceCon") and event_name.startswith("Source")):
for component_name, average in averages.items():
if component_name not in component_averages.keys():
component_averages[component_name] = []
component_averages[component_name].append(average)
elif (event_type == ("ERE Conference") and event_name.startswith("ERE")):
for component_name, average in averages.items():
if component_name not in component_averages.keys():
component_averages[component_name] = []
component_averages[component_name].append(average)
for component_name, scores in component_averages.items():
component_averages[component_name] = round(sum(component_averages[component_name]) / len(component_averages[component_name]), 2)
return component_averages
def create_component_charts(component_chart_data):
for event_type, event in component_chart_data.items():
final_data = []
component_averages = get_component_averages(event_type, event)
event_filename = "charts/" + event_type.lower().replace(" ", "_")+ "_component_chart.html"
for event_name, averages in event.items():
bar_x_axis, bar_y_axis = prepare_chart_data(averages)
scatter_x_axis, scatter_y_axis = prepare_chart_data(component_averages)
chart_title = event_type + " Component Scores"
final_data.append(Bar(
x = bar_x_axis,
y = bar_y_axis,
textposition='top left',
name = event_name,
text = event_name
)
)
chart_layout = Layout(
margin = dict(
r = 150,
b = 200,
),
title = chart_title
)
final_data.append(Scatter(
x = scatter_x_axis,
y = scatter_y_axis,
mode = 'markers',
name = "Average Score",
text = "Average Score",
marker = dict(
symbol = 'line-ns-open',
color = 'rgb(0, 0, 0)',
size = 1,
line = dict(
width = 75
)
)
)
)
plotly.offline.plot({
"data": final_data,
"layout": chart_layout
},
filename = event_filename
)
question_types = ["nps", "components"]
nps_chart_data = {}
component_chart_data = {}
for event_type, event_data in target_surveys.items():
nps_chart_data[event_type] = {}
component_chart_data[event_type] = {}
for survey_id, data in event_data.items():
print (data['title'])
question_info = get_questions(survey_id, data, question_types)
for question_type in question_types:
if question_type == "nps":
answers = get_nps_responses (question_info)
matched_answers = match_answers(question_type, question_info, answers)
nps = calculate_nps(matched_answers)
event_name = data["season"] + " " + data["date_created"][:4]
nps_chart_data[event_type][event_name] = nps
elif question_type == "components":
answers = get_component_responses(question_info)
matched_answers = match_answers(question_type, question_info, answers)
averages = calculate_averages(matched_answers)
component_chart_data[event_type][data["title"]] = averages
print ()
create_nps_chart(nps_chart_data)
create_component_charts(component_chart_data)