-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_log.py
119 lines (107 loc) · 3.37 KB
/
parse_log.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
import json
import numpy as np
from ibo2013.question.models import *
from django.contrib.auth.models import User
import datetime
def make_student_json():
students = Student.objects.filter(testbit=False).select_related()
sid = linear_projection(students,k='user_id')
print "studentlen ", len(list(students))
ret = []
for s in students:
details = {
'id':sid[s.user.id],
'real_id':s.user.id,
'fname':s.user.first_name,
'lname':s.user.last_name,
'code':s.individual_id,
'delegation':s.delegation.name
}
ret.append(details)
print json.dumps(ret)
def make_question_json():
questions = ExamQuestion.objects.filter(exam=3).select_related().order_by('question__examquestion__position')
qid = linear_projection(questions)
ret = []
for q in questions:
details = {
'id':qid[q.id],
'real_id':q.id,
'category':q.category.name
}
ret.append(details)
print json.dumps(ret)
def run(start=0,end=1000000,examid=3):
students = Student.objects.filter(testbit=False).select_related()
print "studentlen ", len(list(students))
questions = ExamQuestion.objects.filter(exam=examid).order_by('question__examquestion__position')
log = ExamAjaxLog.objects.filter(question__exam=examid).select_related().order_by('timestamp')[start:end]
sid = linear_projection(students,k='user_id')
qid = linear_projection(questions)
sol = list(ExamAnswers.objects.filter(question__exam=examid,user=303).order_by('question__position'))
solutions = np.zeros((len(qid),4))
for i,s in enumerate(sol):
for j in range(1,5):
solutions[i,j-1] = getattr(s,'answer'+str(j))
scores = np.zeros((len(sid),len(qid),4))#three dim, for each question 4 answers
focus = {}
print solutions
events = []
for item in log:
try:
si = sid[item.user.id]
except:
print "user id {0} not in student list".format(item.user.id)
continue
qi = qid.get(item.question.id,-1)
e = {
'tstamp':item.timestamp.isoformat(),
'sid':si,
'qid':qi,
'prev_qid':None, #used to indicate where focus came from
'score':None,
'prev_score':None, #used to indicate previous score
'focus':None,
'flag':None
}
if item.index == 0:
#flag
e['flag'] = item.data
elif item.index <= 4:
#update score
try:
sc = scores[si,qi]
e['prev_score'] = np.sum(sc)
if(item.data == (solutions[qi,item.index-1] == 1)):
sc[item.index-1] = 1
else:
sc[item.index-1] = 0
e['score'] = np.sum(sc)
except:
print item.index, sc
elif item.index == 10:
#set focus
e['focus'] = True
e['prev_qid'] = focus.get(si,-1)
focus[si] = qi
elif item.index == 11:
#take focus away
e['qid'] = focus.get(si,-1) #beacause otherwise it's always 36
focus[si] = -1
e['focus'] = False
else:
raise Exception("unknown index: " + item.index)
if(e['qid'] >= 0):
events.append(e)
else:
pass
#print "skipping",e
with open("examlog.json","w") as f:
f.write(json.dumps(events))
return True
#returns a projection from id to 1..4
def linear_projection(qset,k='id'):
ret = {}
for i,item in enumerate(qset):
ret[getattr(item,k)] = i
return ret