/
module_evaluation_analysis.py
382 lines (296 loc) · 17.7 KB
/
module_evaluation_analysis.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
import json
import pandas
import shutil
import argparse
import subprocess
from tqdm import tqdm
from weasyprint import HTML
from jinja2 import Environment, FileSystemLoader
from module_evaluation.config import *
from module_evaluation.io_utils import *
from module_evaluation.data_transform import *
from module_evaluation.extract_subset_data import *
from module_evaluation.extract_module_data import *
from module_evaluation.extract_lecturer_data import *
TEMPLATE_PATH = os.path.join(os.getcwd(), 'templates')
TEMPLATE_ENVIRONMENT = Environment(autoescape=False, loader=FileSystemLoader(TEMPLATE_PATH), trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def initialise_directories():
# Create all the directories we'll need for output
for directory in ['lecturers', 'modules', 'subsets']:
for datatype in ['csv', 'pdf']:
to_create = os.path.join(OUTPUT_DIRECTORY, directory, datatype)
if not os.path.exists(to_create):
os.makedirs(to_create)
def read_input_files(input_directory):
# Read in all the data we have
dataframes = []
print('\nreading input data')
dataframes.extend(read_input_dataframes(input_directory))
return dataframes
def construct_module_templates(dataframes, label):
# get the list of modules we have data for
modules = get_module_list(dataframes)
print('\n\nWriting Module templates')
for module in tqdm(modules):
module_data = pandas.DataFrame()
counts = pandas.DataFrame()
subsets = pandas.DataFrame()
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename(module, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename(module, label=label))) as input_file:
module_data = pandas.read_csv(input_file, index_col=0)
if 'Agree' in module_data.columns:
module_data.sort_values('Agree', inplace=True, ascending=False)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Count', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Count', label=label))) as input_file:
counts = pandas.read_csv(input_file, index_col=0)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Year and Subset Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Year and Subset Comparison', label=label))) as input_file:
subsets = pandas.read_csv(input_file, index_col=0)
subsets.index.name = 'question'
context = {}
context['module'] = module
context['data'] = {}
context['data']['overall'] = module_data.to_csv()
context['label'] = label
context['count'] = float(counts.ix[module])
if 'Agree' in module_data.columns:
highlights = module_data.head(3)
context['data']['highlights'] = highlights.to_csv()
lowlights = module_data.tail(3)
context['data']['lowlights'] = lowlights.to_csv()
subsets_needed = set()
subsets_needed.add('All Modules')
for subset in SUBSETS:
if module in subset['subset']:
subsets_needed.add(subset['title'])
meta_data = {}
meta_data['label'] = label
meta_data['subsets'] = list(subsets_needed)
meta_data['count'] = float(counts.ix[module])
context['meta_json'] = json.dumps(meta_data)
context['data']['subsets'] = subsets[list(subsets_needed)].to_csv()
fpath = os.path.join(BUILD_DIR, '%s Module Report - (%s).html' % (module, label))
with open(fpath, 'w') as f:
html = render_template('module_evaluation_analysis.html', context)
f.write(html)
def construct_lecturer_templates(dataframes, label):
# figure out which lecturer data we have
lecturers = get_lecturer_list(dataframes)
lecturer2modules = {}
# figure out which lecturer goes with which module data
for lecturer in lecturers:
lecturer_data = extract_lecturer_data(dataframes, lecturer)
lecturer_modules = get_module_list([lecturer_data])
lecturer2modules[lecturer] = lecturer_modules
print('\n\nWriting Lecturer templates')
for lecturer, modules in tqdm(lecturer2modules.items()):
context = {}
context['data'] = {}
context['lecturer'] = lecturer
context['label'] = label
context['modules'] = modules
subsets = pandas.DataFrame()
overall = pandas.DataFrame()
counts = pandas.DataFrame()
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename(lecturer, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename(lecturer, label=label))) as input_file:
overall = pandas.read_csv(input_file, index_col=0)
overall.sort_index(inplace=True, ascending=False)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('%s Counts' % lecturer, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('%s Counts' % lecturer, label=label))) as input_file:
counts = pandas.read_csv(input_file, index_col=0)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Comparison', label=label))) as input_file:
lecturer_comparison = pandas.read_csv(input_file, index_col=0)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Year and Subset Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Year and Subset Comparison', label=label))) as input_file:
subsets = pandas.read_csv(input_file, index_col=0)
context['total'] = float(counts.ix['All'])
context['data']['overall'] = overall.to_csv()
context['data']['counts'] = counts.to_csv()
context['question_titles'] = list(subsets.index)
all_subsets = set()
all_subsets.add('All Lecturers')
context['comparisons'] = []
for j, q in enumerate(lecturer_comparison.index):
data = lecturer_comparison.ix[q].T
data.index.name = 'lecturer'
new_index = [lecturer if l == lecturer else '...' for l in data.index]
data.index = new_index
data.rename(columns = ['Agree'], inplace=True)
context['comparisons'].append({'q': q, 'id': j, 'data': data.to_csv()})
context['data']['modules'] = []
for module in modules:
module_subsets = ['All Lecturers']
for subset in SUBSETS:
if module in subset['subset']:
module_subsets.append(subset['title'])
all_subsets.add(subset['title'])
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename(lecturer, module, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename(lecturer, module, label=label))) as input_file:
df = pandas.read_csv(input_file, index_col=0)
if 'Agree' in df.columns:
df.sort_index(inplace=True, ascending=False)
module_data = {}
module_data['meta'] = {}
module_data['meta']['code'] = module
module_data['data'] = df.to_csv()
module_data['meta']['count'] = float(counts.ix[module])
module_data['meta']['subsets'] = module_subsets
module_data['meta_json'] = json.dumps(module_data['meta'])
context['data']['modules'].append(module_data)
context['data']['subsets'] = subsets[list(all_subsets)].to_csv()
fpath = os.path.join(BUILD_DIR, '%s Lecturer Report - (%s).html' % (lecturer, label))
with open(fpath, 'w') as f:
html = render_template('lecturer_evaluation_analysis.html', context)
f.write(html)
def construct_subset_comparison_templates(dataframes, label):
print('\n\nWriting Subset templates')
for subset in tqdm(SUBSETS):
context = {}
subset_data = pandas.DataFrame()
counts = pandas.DataFrame()
comparison_data = pandas.DataFrame()
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'subsets', 'csv', construct_csv_filename(subset['title'], label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'subsets', 'csv', construct_csv_filename(subset['title'], label=label))) as input_file:
subset_data = pandas.read_csv(input_file, index_col=0)
subset_data.sort_values('Agree', inplace=True, ascending=False)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Year and Subset Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Year and Subset Comparison', label=label))) as input_file:
comparison_data = pandas.read_csv(input_file, index_col=0)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Count', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename('Module Count', label=label))) as input_file:
counts = pandas.read_csv(input_file, index_col=0)
context['title'] = subset['title']
context['modules'] = []
context['label'] = label
context['data'] = {}
context['data']['overall'] = subset_data.to_csv()
context['data']['average'] = comparison_data['All Modules'].to_csv(header=['Agree'], index_label='question')
if 'Agree' in subset_data.columns:
highlights = subset_data.head(3)
context['data']['highlights'] = highlights.to_csv()
lowlights = subset_data.tail(3)
context['data']['lowlights'] = lowlights.to_csv()
context['data']['modules'] = []
for module in subset['subset']:
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename(module, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'modules', 'csv', construct_csv_filename(module, label=label))) as input_file:
df = pandas.read_csv(input_file, index_col=0)
if 'Agree' in df.columns:
df.sort_values('Agree', inplace=True, ascending=False)
module_data = {}
module_data['meta'] = {}
module_data['meta']['code'] = module
module_data['data'] = df.to_csv()
module_data['meta']['count'] = float(counts.ix[module])
module_data['meta_json'] = json.dumps(module_data['meta'])
if 'Agree' in df.columns:
highlights = df.head(3)
module_data['highlights'] = highlights.to_csv()
lowlights = df.tail(3)
module_data['lowlights'] = lowlights.to_csv()
context['data']['modules'].append(module_data)
context['modules'].append(module)
fpath = os.path.join(BUILD_DIR, '%s Subset Evaluation Analysis Report - (%s).html' % (subset['title'], label))
with open(fpath, 'w') as f:
html = render_template('module_subset_evaluation_analysis.html', context)
f.write(html)
def construct_lecturer_comparison_template(dataframes, label):
print('\n\nWriting Lecturer Comparison Template')
lecturer_comparison = pandas.DataFrame()
subsets = pandas.DataFrame()
counts = pandas.DataFrame()
counts.index.name = 'Lecturer'
for i in tqdm(range(1)):
lecturers = get_lecturer_list(dataframes)
for lecturer in lecturers:
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('%s Counts' % lecturer, label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('%s Counts' % lecturer, label=label))) as input_file:
lecturer_count = pandas.read_csv(input_file, index_col=0)
counts.at[lecturer, 'Count'] = int(lecturer_count.ix['All']['Count'])
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Comparison', label=label))) as input_file:
lecturer_comparison = pandas.read_csv(input_file, index_col=0)
if os.path.exists(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Year and Subset Comparison', label=label))):
with open(os.path.join(OUTPUT_DIRECTORY, 'lecturers', 'csv', construct_csv_filename('Lecturer Year and Subset Comparison', label=label))) as input_file:
subsets = pandas.read_csv(input_file, index_col=0)
context = {}
context['year'] = label
context['data'] = {}
context['data']['counts'] = counts.to_csv()
context['data']['subsets'] = subsets.to_csv()
context['questions'] = []
context['question_titles'] = list(subsets.index)
for i, q in enumerate(lecturer_comparison.index):
data = lecturer_comparison.ix[q].T
data.index.name = 'lecturer'
data.rename(columns = ['Agree'], inplace=True)
context['questions'].append({'q': q, 'id': i, 'data': data.to_csv()})
fpath = os.path.join(BUILD_DIR, 'Lecturer Evaluation Analysis Report - (%s).html' % (label))
with open(fpath, 'w') as f:
html = render_template('overall_lecturer_evaluation_analysis.html', context)
f.write(html)
def create_pdfs():
print('\n\nCreating Lecturer PDF reports')
lecturer_templates = [f for f in os.listdir(BUILD_DIR) if f.find('Lecturer Report') != -1]
for lecturer in tqdm(lecturer_templates):
name = lecturer[:lecturer.find('.html')]
template_file = "file://%s" % os.path.join(BUILD_DIR, lecturer)
output_file = os.path.join("output", "lecturers", "pdf", "%s.pdf" % name)
args = ['node', 'utils/generate_landscape_pdf.js', template_file, output_file]
subprocess.call(args)
print('\n\nCreating Module PDF reports')
module_templates = [f for f in os.listdir(BUILD_DIR) if f.find('Module Report') != -1]
for module in tqdm(module_templates):
name = module[:module.find('.html')]
template_file = "file://%s" % os.path.join(BUILD_DIR, module)
output_file = os.path.join("output", "modules", "pdf", "%s.pdf" % name)
args = ['node', 'utils/generate_landscape_pdf.js', template_file, output_file]
subprocess.call(args)
print('\n\nCreating Subset PDF reports')
subset_templates = [f for f in os.listdir(BUILD_DIR) if f.find('Subset Evaluation Analysis Report') != -1]
for subset in tqdm(subset_templates):
name = subset[:subset.find('.html')]
template_file = "file://%s" % os.path.join(BUILD_DIR, subset)
output_file = os.path.join("output", "subsets", "pdf", "%s.pdf" % name)
args = ['node', 'utils/generate_landscape_pdf.js', template_file, output_file]
subprocess.call(args)
print('\n\nCreating Overall Lecturer Analysis PDF reports')
lecturer_templates = [f for f in os.listdir(BUILD_DIR) if f.find('Lecturer Evaluation Analysis Report') != -1]
for lecturer in tqdm(lecturer_templates):
name = lecturer[:lecturer.find('.html')]
template_file = "file://%s" % os.path.join(BUILD_DIR, lecturer)
output_file = os.path.join("output", "lecturers", "pdf", "%s.pdf" % name)
args = ['node', 'utils/generate_landscape_pdf.js', template_file, output_file]
subprocess.call(args)
def copy_template_files():
template_files = os.listdir(TEMPLATE_PATH)
template_files = [f for f in template_files if f.endswith('.css') or f.endswith('.js')]
print('\n\nCopying template files')
for f in tqdm(template_files):
shutil.copy(os.path.join(TEMPLATE_PATH, f), BUILD_DIR)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Analysing Module Evaluation Feedback')
parser.add_argument('-i', '--input', help='Input directory with evaluation data', required=True, action='store')
parser.add_argument('-l', '--label', help='Label to add to output filenames', required=False, action='store', default='')
args = parser.parse_args()
initialise_directories()
input_directory = os.path.join(os.getcwd(), args.input)
label = args.label
dataframes = read_input_files(input_directory)
extract_and_write_module_data(dataframes, label)
extract_and_write_lecturer_data(dataframes, label)
extract_and_write_year_and_subset_data(dataframes, label)
#
construct_lecturer_templates(dataframes, label)
construct_module_templates(dataframes, label)
construct_subset_comparison_templates(dataframes, label)
construct_lecturer_comparison_template(dataframes, label)
#
copy_template_files()
#
create_pdfs()