-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
488 lines (399 loc) · 15.9 KB
/
test.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import re
import db
from student import Student
from question import Question
import process_utils
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from flask import session
# Represents an entire test.
class Test:
def __init__(self, students, versions, discarded = [], boundaries = None, texts = None, subboundaries = None, question_discriminations = None, question_weights = None, student_locations = None):
# {!Array<!Student>} An array of students.
self.students = students
# {Object<string, !Array<number>>} Dictionary mapping the version names to their answer keys.
self.versions = versions
# Metadata.
self.student_versions = set()
if students and versions:
self.score_all()
if students:
self.test_length = len(self.students[0].scores)
# Store a reference to this test in each student object.
for i in range(len(self.students)):
self.students[i].test = self
if (self.students[i].version != None):
# Append versions.
self.student_versions.add(self.students[i].version)
# Also try to set the location.
if student_locations and (len(student_locations) - 1) >= i:
self.students[i].location = student_locations[i]
else:
self.test_length = 0
# {!Array<!Question>} The array of questions within the test.
self.questions = self.init_questions(texts, discarded, question_discriminations, question_weights)
# {!Array<number>} Question indexes that are discarded.
self.discarded = discarded or []
# {number} The length of the test.
self.test_length = len(self.questions)
# {!Array<number>} The default grade boundaries.
self.boundaries = boundaries or [0.5, -0.5, -2]
# Any set subboundaries.
self.subboundaries = subboundaries or None
# Score all the students.
def score_all(self):
for student in self.students:
version = self.get_version(student.version)
answer_key = self.versions[version]
student.score(answer_key)
# Find the difficulty of a question given its index.
def get_percentage_correct(self, index):
if index >= self.test_length:
return 0
# Number of students that were right for this question..
right = 0
# Query each student to see if they were right for this question.
for student in self.students:
if student.is_right(index):
right += 1
# Return the percentage of students right.
if len(self.students) == 0:
return 0
else:
# Force the answer to be floating point.
return right / (len(self.students) * 1.0) * 100
# Initializes the array of question objects.
def init_questions(self, text_array, discard_array, question_discriminations, question_weights):
questions = []
for i in range(self.test_length):
# The percentage of students that got this question correct.
percentage = self.get_percentage_correct(i)
# The text for the question, if it exists.
text = text_array[i] if text_array and len(text_array) - 1 >= i else ''
# If this question was marked as to discard or not.
discard = ((i + 1) in discard_array) if discard_array else False
# If a question discrimination was saved.
discrimination = question_discriminations[i] if question_discriminations and len(question_discriminations) - 1 >= i else None
# If a question weight was saved.
weight = question_weights[i] if question_weights and len(question_weights) - 1 >= i else None
# Create the question.
question = Question(self, percentage_correct = percentage, text = text, discard = discard, discrimination = discrimination, weight = weight)
questions.append(question)
return questions
# Given a string, guess which version it is most likely to be.
def get_version(self, version_string):
# Full version, including course code.
given_version_int_full = int(version_string)
# Without the course code.
given_version_int_without_course_code = int(version_string[3:])
# Last character only.
given_version_int_last_char = int(version_string[-1:])
to_search = [given_version_int_full, given_version_int_without_course_code, given_version_int_last_char]
# Compare from the fullest version to the most abbreviated version.
for given_comparable in to_search:
# Search through the keys we have.
for key_name in self.versions.keys():
# Strip the non-numeric characters from the keys we have.
stripped_key_name = re.sub("\D", "", key_name)
stripped_key_name_int = int(stripped_key_name)
# Compare it to the integer value of the version given.
if stripped_key_name_int == given_comparable:
return key_name
# Return the first key as a default.
return self.versions.keys()[0]
# Outputs student score data in a CSV-like format, comma-delineated.
def to_student_score_csv(self):
if (len(self.students) == 0):
return ''
# Full array of student data.
students = []
# Grade boundary key.
min_location = self.min_student_location()
max_location = self.max_student_location()
grade_boundaries = self.get_grade_boundaries()
# Go through all the students and retrieve information about each one.
for i in range (0, len(self.students)):
student = self.students[i]
student_id = student.id
# Create the student array.
student_array = student.scores
student_array = [int(val) for val in student_array]
student_array.insert(0, student.id)
# Convert to string.
student_array = list(map(str, student_array))
students.append(','.join(student_array))
# Return the joined string.
return '\n'.join(students)
# Outputs question data in a CSV-like format, comma-delineated.
def to_question_csv(self, encode = False, zero_indexed = True):
# Full array of question data.
questions = []
# Go through all the questions and retrieve information about each one.
for i in range(self.test_length):
question = self.questions[i]
text = question.text if not encode else process_utils.html_encode(question.text)
percentage = question.percentage_correct
discrimination = question.get_discrimination()
weight = question.get_item_weight()
kept = not question.discard
# Create the question array.
question_array = []
question_array.append(i if zero_indexed else i + 1)
question_array.append(text)
question_array.append(percentage)
question_array.append(discrimination)
question_array.append(weight)
question_array.append(kept)
# Convert to string.
question_array = list(map(str, question_array))
questions.append(','.join(question_array))
# Return the joined string.
return '\n'.join(questions)
def process_raw_question_stats(self, response_r_object):
features = []
j = 0
# Store inside the questions.
for i in range(len(self.questions)):
if not self.questions[i].discard:
# Cache the feature.
# R vectors are 1-indexed.
feature = response_r_object.rx2(j + 1)
features.append(feature[0])
j += 1
else:
features.append(0)
return features
# Sets item weights and discriminations for the questions.
def calculate_question_stats(self, response_matrix = None, return_values = False, store = True):
response_matrix = response_matrix or self.get_response_matrix()
# Assign to a symbol.
robjects.globalenv['response_matrix'] = response_matrix
# Use mirt.
importr('mirt')
# Create the model.
twopl_mod = "ability = 1 - " + str(len(response_matrix))
# Fit the model.
robjects.r('twopl_fit <- mirt(data = response_matrix, model = "' + twopl_mod + '", itemtype = "2PL", SE = TRUE)')
# Process parameters.
robjects.r('twopl_params <- coef(twopl_fit, IRTpars = TRUE, simplify = TRUE)')
robjects.r('twopl_items <- twopl_params$items')
# Store a row of discrimination coefficients.
discriminations = self.process_raw_question_stats(robjects.r('twopl_items[,1]'))
# Store a row of item weights.
item_weights = self.process_raw_question_stats(robjects.r('twopl_items[,2]'))
# Create a list to store the discriminations for saving into the database.
discriminations_to_save = []
# Create a list to store the weights for saving into the database.
weights_to_save = []
# Store inside the questions.
for i in range(len(self.questions)):
question = self.questions[i]
# Cache the discrimination.
# R vectors are 1-indexed.
question.discrimination = discriminations[i]
# Cache the item weight.
# R vectors are 1-indexed.
question.item_weight = item_weights[i]
discriminations_to_save.append(question.discrimination)
weights_to_save.append(question.item_weight)
if store:
# Set up a database object.
database_manager = db.Database()
database_manager.insert_or_update_from('question_discriminations', session['id'], discriminations_to_save)
database_manager.insert_or_update_from('question_weights', session['id'], weights_to_save)
# Sets locations for the students.
def calculate_student_stats(self, response_matrix = None, return_values = False, store = True):
response_matrix = response_matrix or self.get_response_matrix()
# Assign to a symbol.
robjects.globalenv['response_matrix'] = response_matrix
# Use mirt.
importr('mirt')
# robjects.r('tmp <- tempfile(pattern=paste("foo", Sys.getpid(), sep=""))')
# Create the model.
twopl_mod = "ability = 1 - " + str(len(response_matrix))
# Fit the model.
robjects.r('twopl_fit <- mirt(data = response_matrix, model = "' + twopl_mod + '", itemtype = "2PL", SE = TRUE)')
# Fit the student model.
robjects.r('student_fit <- fscores(twopl_fit, method = "EAP", full.scores = TRUE, full.scores.SE = TRUE)')
locations = robjects.r('student_fit[,1]')
# Create a list to store the locations for saving into the database.
locations_to_save = []
# Store inside the questions.
for i in range(len(self.students)):
student = self.students[i]
# Cache the discrimination.
# R vectors are 1-indexed.
student.location = locations.rx(i + 1)[0]
locations_to_save.append(student.location)
if store:
# Set up a database object.
database_manager = db.Database()
database_manager.insert_or_update_from('student_locations', session['id'], locations_to_save)
# Returns the matrix of responses in a dataframe / ltm-usable format.
def get_response_matrix(self):
matrix = {}
matrix_index = 0
# For each question:
for question_index in range(self.test_length):
question = self.questions[question_index]
# Cannot have questions where either 100% or 0% were correct, as ltm will crash.
# This also excludes questions the user has opted to discard.
if not question.discard:
# Header value.
question_response_vector = []
# Retrieve all the responses for each student.
for j in range(len(self.students)):
# question_response_vector.append(self.students[j].is_right(question_index))
question_response_vector.append(int(self.students[j].is_right(question_index)))
# question_response_vector.append(1)
matrix_index += 1
else:
# Ignore the exclusions.
pass
# Convert to a vector.
# matrix[question_index + 1] = robjects.BoolVector(question_response_vector)
matrix[matrix_index] = robjects.IntVector(question_response_vector)
# Convert the dictionary of vectors to a dataframe.
response_matrix = robjects.DataFrame(matrix)
return response_matrix
def min_student_location(self):
min_score = 100000
for i in range (len(self.students)):
location = self.students[i].get_location()
if location < min_score and not location == None:
min_score = location
return min_score
def max_student_location(self):
max_score = -100000
for i in range (len(self.students)):
location = self.students[i].get_location()
if location > max_score and not location == None:
max_score = location
return max_score
# Converts the A/B, B/C, and C/D grade boundaries to find a grade boundary key in item score terms.
def calculate_subboundaries(self, boundaries):
ab_lowest = boundaries[0]
bc_lowest = boundaries[1]
cd_lowest = boundaries[2]
min_score = min(self.min_student_location(), cd_lowest)
max_score = max(self.max_student_location(), ab_lowest)
# Get the range of each 'section'.
range_a = max(max_score - ab_lowest, 0)
range_b = max(ab_lowest - bc_lowest, 0)
range_c = max(bc_lowest - cd_lowest, 0)
range_d = max(cd_lowest - min_score, 0)
# Check for ranges of 0.
subboundaries = [
{
'grade': 'A+',
# This is set to the top 50% of scores in the A range.
'value': ab_lowest + (range_a / 4) * 2
},
{
'grade': 'A',
# This is set to the middle 25% of scores in the A range.
'value': ab_lowest + (range_a / 4) * 1
},
{
'grade': 'A-',
# This is set to the bottom 25% of scores in the A range.
'value': ab_lowest + (range_a / 4) * 0
},
{
'grade': 'B+',
# This is set to the top 33% of scores in the B range.
'value': bc_lowest + (range_b / 3) * 2
},
{
'grade': 'B',
# This is set to the middle 33% of scores in the B range.
'value': bc_lowest + (range_b / 3) * 1
},
{
'grade': 'B-',
# This is set to the bottom 33% of scores in the B range.
'value': bc_lowest + (range_b / 3) * 0
},
{
'grade': 'C+',
# This is set to the top 33% of scores in the C range.
'value': cd_lowest + (range_c / 3) * 2
},
{
'grade': 'C',
# This is set to the middle 33% of scores in the C range.
'value': cd_lowest + (range_c / 3) * 1
},
{
'grade': 'C-',
# This is set to the bottom 33% of scores in the C range.
'value': cd_lowest + (range_c / 3) * 0
},
{
'grade': 'D+',
# This is set to the top 33% of scores in the D range.
'value': min_score + (range_d / 3) * 2
},
{
'grade': 'D',
# This is set to the middle 33% of scores in the D range.
'value': min_score + (range_d / 3) * 1
},
{
'grade': 'D-',
'value': min_score
},
]
return subboundaries
# Calculates subboundaries if subboundaries are not set.
def get_grade_boundaries(self):
if not self.subboundaries:
return self.calculate_subboundaries(self.boundaries)
# Use the existing grade boundaries if they exist.
grade_boundaries = self.calculate_subboundaries(self.boundaries)
grade_boundaries[0]['value'] = self.subboundaries[0]
grade_boundaries[1]['value'] = self.subboundaries[1]
grade_boundaries[3]['value'] = self.subboundaries[2]
grade_boundaries[4]['value'] = self.subboundaries[3]
grade_boundaries[6]['value'] = self.subboundaries[4]
grade_boundaries[7]['value'] = self.subboundaries[5]
grade_boundaries[9]['value'] = self.subboundaries[6]
grade_boundaries[10]['value'] = self.subboundaries[7]
return grade_boundaries
# Outputs question data in a CSV-like format, comma-delineated.
def to_student_csv(self):
if (len(self.students) == 0):
return ''
# Full array of student data.
students = []
# Grade boundary key.
min_location = self.min_student_location()
max_location = self.max_student_location()
grade_boundaries = self.get_grade_boundaries()
# Go through all the students and retrieve information about each one.
for i in range (0, len(self.students)):
student = self.students[i]
student_id = student.id
raw_percentage = student.raw_percentage()
raw_grade = student.grade_key_to_grade(raw_percentage)
analyzed_percentage = student.raw_percentage(discarded = self.discarded)
analyzed_score = student.get_location()
recommended_percentage = student.grade_key_to_percentage(student.get_location(), min_location, max_location, grade_boundaries = grade_boundaries)
recommended_grade = student.grade_key_to_grade(student.get_location(), grade_boundaries = grade_boundaries)
# Create the question array.
student_array = []
student_array.append(student_id)
student_array.append(raw_percentage)
student_array.append(raw_grade)
student_array.append(analyzed_percentage)
student_array.append(analyzed_score)
student_array.append(recommended_percentage)
student_array.append(recommended_grade)
# student_array = student.scores
# student_array = [int(val) for val in student_array]
# student_array.insert(0, student.id)
# Convert to string.
student_array = list(map(str, student_array))
students.append(','.join(student_array))
# Return the joined string.
return '\n'.join(students)