-
Notifications
You must be signed in to change notification settings - Fork 2
/
schedule_grading.py
303 lines (243 loc) · 9.44 KB
/
schedule_grading.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
import os
import random
import sys
try:
import colorama
colorama.init()
from colorama import Fore
except ImportError:
print('For extra useless functionality, install colorama.')
colorama = None
from assignment import Assignment
from grader import Grader
from student import Student
error_occured = False
def load_graders(filename):
graders = []
with open(filename, 'r') as gfile:
for line in gfile:
line = line.strip()
if line != '':
graders.append(Grader(line))
return graders
def load_students(filename):
students = []
with open(filename, 'r') as sfile:
for line in sfile:
line = line.strip()
if line != '':
students.append(Student(line))
return students
def load_assignments(filename):
assignments = []
with open(filename, 'r') as afile:
for line in afile:
line = line.strip()
assignments.append(Assignment(line))
return assignments
def load_weights(filename, graders, students):
weights = {}
for grader in graders:
weights[grader] = {}
try:
with open(filename, 'r') as wfile:
for line in wfile:
line = line.split(';')
grader_name = line[0], line[1]
student_name = line[2], line[3]
weight = float(line[4])
grader = None
student = None
for g in graders:
if g.name == grader_name:
grader = g
for s in students:
if s.name == student_name:
student = s
if not grader:
error('Could not match grader {} in {}.'.format(grader_name, filename))
error('A former grader should be simply given a limit of 0 to grade.')
if not student:
error('Could not match student {} in {}.'.format(student_name, filename))
if grader and student:
weights[grader][student] = weight
except FileNotFoundError:
warning('No {} file found.'.format(filename))
return weights
def load_history(assignments, graders, students):
data = {}
for assignment in assignments:
filename = assignment.id + '.csv'
if os.path.isfile(filename):
data[assignment.id] = load_history_file(filename, graders, students)
else:
break
print('Loaded history for first {} assignments.'.format(len(data.keys())))
return data
def load_history_file(filename, graders, students):
data = {}
for grader in graders:
data[grader] = []
with open(filename, 'r') as history_file:
for line in history_file:
line = line.strip().split(';')
if len(line) != 4 and line != []:
error('Invalid line in {}: {}'.format(filename, line))
grader_name = line[0], line[1]
student_name = line[2], line[3]
grader = None
student = None
for g in graders:
if g.name == grader_name:
grader = g
for s in students:
if s.name == student_name:
student = s
if not grader:
error('Could not match grader {} in {}.'.format(grader_name, filename))
error('A former grader should be simply given a limit of 0 to grade.')
if not student:
warning('Could not match student {} in {}.'.format(student_name, filename))
if grader and student:
data[grader].append(student)
return data
#tslg: time since last graded
def calculate_tslg(assignments, history, graders, students):
counter = 1
tslg = {}
for grader in graders:
tslg[grader] = {}
assignments = assignments[::-1]
for assignment in assignments:
assignment = assignment.id
data = history[assignment]
for grader in data:
for student in data[grader]:
if student not in tslg[grader]:
tslg[grader][student] = counter
counter += 1
for grader in graders:
for student in students:
if student not in tslg[grader].keys():
tslg[grader][student] = counter + 1
return tslg
def apply_weights(tslg, weights):
for grader in weights:
for student in weights[grader]:
tslg[grader][student] *= weights[grader][student]
return tslg
def get_max_tslg(tslg, student):
return max(map(lambda g: tslg[g][student] if g.hours else 0, tslg))
def split_students_by_tslg(tslg, students):
split_students = {}
for student in students:
max_tslg = get_max_tslg(tslg, student)
if max_tslg not in split_students:
split_students[max_tslg] = []
split_students[max_tslg].append(student)
return split_students
def split_graders_by_tslg(tslg, graders, student):
split_graders = {}
for grader in graders:
time = tslg[grader][student]
if time not in split_graders:
split_graders[time] = []
split_graders[time].append(grader)
return split_graders
def make_grader_list(tslg, graders, students):
#sorts students so that those with the longest tslg are first
#sorted_students = sorted(students, key=lambda s: get_max_tslg(tslg, s), reverse=True)
grader_list = {}
overflow_graders = list(filter(lambda g: g.overflow > 0, graders))
for grader in graders:
grader_list[grader] = []
split_students = split_students_by_tslg(tslg, students)
for group in sorted(split_students.keys(), reverse=True):
random.shuffle(split_students[group])
print('Group {}: {}'.format(group, split_students[group]))
for student in split_students[group]:
split_graders = split_graders_by_tslg(tslg, graders, student)
assigned = False
for grader_group in sorted(split_graders.keys(), reverse=True):
random.shuffle(split_graders[grader_group])
for grader in split_graders[grader_group]:
if len(grader_list[grader]) < grader.hours and grader_group > 1:
assigned = True
grader_list[grader].append(student)
break
if assigned:
break
if not assigned:
#print(overflow_graders, student)
overflow_selection = sorted(overflow_graders,\
key=lambda g: tslg[g][student], reverse=True)
overflow_selection = list(filter(lambda g:\
len(grader_list[g]) < g.hours + g.overflow, overflow_selection))
overflow_selection = overflow_selection[0] if overflow_selection else None
#print()
#print(student)
#print(split_graders)
#print(grader_list)
#print()
if not overflow_selection or tslg[overflow_selection][student] <= 1:
error('{} was not assigned a grader.'.format(student))
else:
assigned = True
grader_list[overflow_selection].append(student)
warning('{} was assigned to an overflow grader.'.format(student))
#print(grader_list)
return grader_list
def write_to_csv(grader_list, assignment):
lines = []
for grader in grader_list:
for student in grader_list[grader]:
line = ';'.join(grader.name + student.name) + '\n'
lines.append(line)
with open(assignment.id + '.csv', 'w') as csv_file:
csv_file.writelines(lines)
def error(msg):
global error_occured
error_occured = True
if colorama:
print('{} ERROR: {}{}'.format(Fore.RED, msg, Fore.RESET))
else:
print(' ERROR: {}'.format(msg))
def warning(msg):
if colorama:
print('{} WARNING: {}{}'.format(Fore.YELLOW, msg, Fore.RESET))
else:
print(' WARNING: {}'.format(msg))
def main():
global error_occured
seed = random.randint(0, 2**20)
print('Using random seed {}.'.format(seed))
random.seed(seed)
graders = load_graders('graders.csv')
students = load_students('students.csv')
assignments = load_assignments('assignment_list.txt')
weights = load_weights('weights.csv', graders, students)
graders.sort(key=lambda g:g.hours + 0.5 if g.overflow else 0)
students.sort(key=lambda s:(s.name[1], s.name[0]))
#print(graders)
#print(students)
#print(assignments)
history = load_history(assignments, graders, students)
graded_assignments = assignments[:len(history.keys())]
if len(graded_assignments) == len(assignments):
warning('All assignments have been graded.')
sys.exit(0)
next_assignment = assignments[len(history.keys())]
tslg = calculate_tslg(graded_assignments, history, graders, students)
tslg = apply_weights(tslg, weights)
#to be used to specify which graders shouldn't grade certain students, for example
#history = adjust_with_weights(history)
next_list = make_grader_list(tslg, graders, students)
if not error_occured:
write_to_csv(next_list, next_assignment)
print('Wrote to {}.csv.'.format(next_assignment.id))
sys.exit(0)
else:
error('Did not write CSV due to error.')
sys.exit(-1)
if __name__ == '__main__':
main()