forked from ryanGT/teaching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb_survey_analyzer.py
293 lines (206 loc) · 7.93 KB
/
bb_survey_analyzer.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
import txt_mixin
import txt_database
import copy, os
import numpy as np
def load_bb_csv(filename_in, enc='latin-1'):
myfile = open(filename_in,'r', encoding=enc)
mylist = myfile.readlines()
return mylist
def clean_one_string(str_in):
elim_list = ['<span>','</span>','<p>','</p>','<br>']
replace_dict = {'Ê':' ',' ':' '}#,'"':'"'} adding quotes
#messes up csv readers
strout = str_in
for e in elim_list:
strout = strout.replace(e,'')
for key, val in replace_dict.items():
strout = strout.replace(key,val)
return strout
def clean_bb_csv_list(listin):
listout = copy.copy(listin)
for i, row in enumerate(listout):
strout = clean_one_string(row)
listout[i] = strout
return listout
def get_unique_answers(listin):
listout = []
for item in listin:
if item not in listout:
listout.append(item)
return listout
class bb_survey_bad_download(txt_mixin.delimited_txt_file):
"""I created this class initially to fix a survey sent to me by a
colleague while working on a paper regarding EGR 345/346. This
person had downloaded blackboard survey results in such a way that
everything was in one column and each repondent had as many rows
as there where questions. After the number of rows for respondent
1, it started over for respondent 2.
For whatever reason, I am leaving this as the base class."""
def __init__(self, *args, **kwargs):
txt_mixin.delimited_txt_file.__init__(self, *args, **kwargs)
self.data = self.break_list()
def get_answers_to_one_question(self, qnum):
label = 'Question ID %i' % qnum
answers = []
N = len(self.data)
for i in range(1,N):
curlabel = self.data[i][0]
if curlabel == label:
answers.append(self.data[i][2])
return answers
def get_answers(self):
answers = {}
for i in range(1,7):
curanswers = self.get_answers_to_one_question(i)
answers[i] = curanswers
self.answers = answers
def quantify_answers(self):
numeric_answers = {}
for i in range(2,7):
txt_answers = self.answers[i]
ans_dict = answer_dicts.nested_dict[i]
inv_map = {v: k for k, v in ans_dict.items()}
N = len(txt_answers)
ans_array = np.zeros(N,dtype=float)
for j, item in enumerate(txt_answers):
nv = inv_map[item]
ans_array[j] = nv
numeric_answers[i] = ans_array
self.numeric_answers = numeric_answers
#self.numeric_answers = ans_num_list
def calc_averages(self):
averages = {}
for i in range(2,7):
cur_data = self.numeric_answers[i]
ave = cur_data.mean()
averages[i] = ave
self.averages = averages
def calc_one_hist(self, qnum):
hist = {}
cur_data = self.numeric_answers[qnum]
for i in range(6):
sum = 0
for item in cur_data:
if item == i:
sum += 1
hist[i] = sum
return hist
def calc_histogram(self):
hist = {}
for i in range(2,7):
cur_hist = self.calc_one_hist(i)
hist[i] = cur_hist
self.hist = hist
def percent_hist(self):
N = len(self.answers[3])
phist = {}
for i in range(2,7):
cur_hist = self.hist[i]
cur_p = {}
for key, val in cur_hist.items():
pval = val/N
cur_p[key] = pval
phist[i] = cur_p
self.phist = phist
class bb_survey(bb_survey_bad_download):
"""This is probably the real base class that assumes the data is
downloaded such that each respondent gets one row and each
question is in its own column."""
def __init__(self, filename):
self.filename = filename
listin = load_bb_csv(filename)
fno, ext = os.path.splitext(filename)
outname = fno + '_clean' + ext
clean_list = clean_bb_csv_list(listin)
txt_mixin.dump(outname, clean_list)
self.db = txt_database.db_from_file(outname)
def find_question_ids(self):
row0 = self.db.data[0]
ids = []
N = len(row0)
pat = 'Question ID'
for i in range(N):
curlabel = row0[i]
if curlabel.find(pat) == 0:
ids.append(curlabel)
self.question_ids = ids
self.nquestions = len(ids)
return ids
def get_answers(self):
if not hasattr(self, 'nquestions'):
self.find_question_ids()
answers = {}
for i in range(1,self.nquestions+1):
attr = 'Answer_%i' % i
ans = getattr(self.db, attr)
answers[i] = ans.tolist()
self.answers = answers
def get_unique_answers_one_question(self, qnum):
"""Find the unique answers to a question. This would mainly
be helpful when getting ready to tabulate a Likert question"""
return get_unique_answers(self.answers[qnum])
def tabulate_likert(self, qnum, answers):
"""Answers is a list of possible Likert answers. The list
would need to be generated from either knowing the questions
ahead of time by copying them from the survey or using the
get_unique_answers_one_question method above. Note that the
get_unique_answers_one_question cannot possibly know how to
sort the answers if you later want to assign numerica values
to the answers. The tabulated results will be in the same
order as answers."""
responses = self.answers[qnum]
table_out = [None]*len(answers)
for i, ans in enumerate(answers):
cur_total = 0
for resp in self.answers[qnum]:
if resp == ans:
cur_total += 1
table_out[i] = cur_total
return table_out
def likert_to_latex_table(self, qnum, answers, hline=True):
tabulated = self.tabulate_likert(qnum, answers)
latexout = [r'Likert Option & Frequency \\']
pat = r'%s & %s \\'
for ans, freq in zip(answers, tabulated):
curline = pat % (ans, freq)
if hline:
latexout.append(r'\hline')
latexout.append(curline)
return latexout
def find_one_question(self, qnum):
"""Find 'Question ID N' in the first row of data. The text
for the question should be in the next spot"""
row0 = self.db.data[0,:].tolist()
label = 'Question ID %i' % qnum
ind = row0.index(label)
raw_question = row0[ind+1]
clean_question = clean_one_string(raw_question)
return clean_question
def find_questions(self):
questions = []
for i in range(self.nquestions):
qnum = i + 1
curq = self.find_one_question(qnum)
questions.append(curq)
self.questions = questions
return questions
def build_csv_data(self):
columns = []
for i in range(self.nquestions):
qnum = i + 1
curq = self.questions[i]
q_label = "Question %i: " % qnum
q_out = q_label + curq
curans = self.answers[qnum]
curcol = [q_out] + curans
columns.append(curcol)
self.csv_data = np.column_stack(columns)
return self.csv_data
def save_csv(self, delim="\t"):
if not hasattr(self, "csv_data"):
self.build_csv_data()
fno, ext = os.path.splitext(self.filename)
outname = fno + '_clean_out.csv'
txt_mixin.dump_delimited(outname, \
self.csv_data, \
delim=delim)