forked from jilljenn/qna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irt.py
executable file
·135 lines (117 loc) · 6.13 KB
/
irt.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
import rpy2.robjects as robjects
from rpy2.robjects.functions import SignatureTranslatedFunction
from rpy2.robjects.packages import importr
from calc import logloss, compute_mean_entropy
from rpyinterface import RPyInterface
from my_io import say
from functools import reduce
import random
import pickle
import numpy as np
import os.path
import json
r = robjects.r
ltm = importr('ltm')
cat = importr('catR')
class IRT(RPyInterface):
def __init__(self, Q=None, slip=None, guess=None, prior=None, criterion='MFI'):
self.name = 'IRT'
self.criterion = criterion
self.data = None
self.nb_students = None
self.nb_questions = None
self.coeff = None
self.r_scores = None
self.scores = None
self.validation_question_set = None
def compute_all_predictions(self):
return np.array([self.predict_performance(theta=self.r_scores[i]) for i in range(self.nb_students)]) # ! Discrimination parameter
# def compute_all_errors(self, mask):
# print('Train RMSE:', ((((p - self.data) * mask) ** 2).sum() / mask.sum()) ** 0.5)
# print('Train NLL:', -np.log(1 - abs(p - self.data) * mask).mean())
# print('Train accuracy:', (np.round(p) == self.data).mean())
def training_step(self, train=None, opt_Q=True, opt_sg=True):
# self.nb_students = len(train)
# self.nb_questions = len(train[0])
# raw_data = list(map(int, reduce(lambda x, y: x + y, train)))
# self.data = r.matrix(robjects.IntVector(raw_data), nrow=self.nb_students, byrow=True)
if os.path.isfile(self.get_backup_path()):
print('Cool, already found!', self.checksum)
r.load(self.get_backup_path())
#self.r_scores = r('scores')
self.load()
else:
model = ltm.rasch(self.r_data)
self.coeff = ltm.coef_rasch(model)
ltm.factor_scores = SignatureTranslatedFunction(ltm.factor_scores, init_prm_translate={'resp_patterns': 'resp.patterns'}) # Mais dans quel monde vivons-nous ma p'tite dame
self.r_scores = ltm.factor_scores(model, resp_patterns=self.r_data).rx('score.dat')[0].rx('z1')[0]
robjects.globalenv['scores'] = self.r_scores
self.scores = np.array(self.r_scores)
r('data <- %s' % self.r_data.r_repr())
#r('data[1][1] <- NA')
r('coeff <- coef(rasch(data))')
r('one <- rep(1, %d)' % self.nb_questions)
r('itembank <- cbind(coeff[,2:1], 1 - one, one)')
self.itembank = np.array(r('itembank'))
# self.compute_all_errors()
r('save(itembank, scores, file="{:s}")'.format(self.get_backup_path()))
self.save() # To pickle file
def load(self):
with open('backup/' + self.checksum + '.pickle', 'rb') as f:
backup = pickle.load(f)
self.r_scores = backup.r_scores
self.scores = backup.scores
self.itembank = backup.itembank
def init_test(self, validation_question_set):
self.validation_question_set = validation_question_set
r('theta <- 0')
def next_item(self, replied_so_far, results_so_far):
if self.criterion == 'MFI':
available_questions = ['1'] * self.nb_questions
for question_id in self.validation_question_set:
available_questions[question_id] = '0'
say('nextItem(itembank, NULL, theta, nAvailable=c({}), out = c({}), criterion = "{}")$item'.format(','.join(available_questions), ','.join(map(lambda x: str(x + 1), replied_so_far)), self.criterion))
best_question = r('nextItem(itembank, NULL, theta, nAvailable=c({}), out = c({}), criterion = "{}")$item'.format(','.join(available_questions), ','.join(map(lambda x: str(x + 1), replied_so_far)), self.criterion))[0]
# raise Exception
return best_question - 1
# next = random.choice(list(set(range(nb_questions)) - set(replied_so_far)))
# min_entropy = None
max_info = None
best_question = None
for question_id in range(self.nb_questions):
if question_id in replied_so_far:
continue
p_answering = self.predict_performance()[question_id]
info = p_answering * (1 - p_answering)
# self.estimate_parameters(replied_so_far + [question_id], results_so_far + [True], '1')
# self.estimate_parameters(replied_so_far + [question_id], results_so_far + [False], '0')
# performance_if_correct = self.predict_performance('1')
# performance_if_incorrect = self.predict_performance('0')
# mean_entropy = compute_mean_entropy(p_answering, performance_if_correct, performance_if_incorrect, replied_so_far + [question_id])
# if not min_entropy or mean_entropy < min_entropy:
if not max_info or info > max_info:
# min_entropy = mean_entropy
max_info = info
best_question = question_id
return best_question
def estimate_parameters(self, replied_so_far, results_so_far, var_id=''):
scores_so_far = map(int, results_so_far)
pattern = ['NA'] * self.nb_questions
for i, pos in enumerate(replied_so_far):
pattern[pos] = str(int(results_so_far[i]))
say('theta{} <- thetaEst(itembank, c({}))'.format(var_id, ','.join(pattern)))
r('theta{} <- thetaEst(itembank, c({}))'.format(var_id, ','.join(pattern))) # , method="ML"
# r('theta{} <- thetaEst(itembank[c({}),], c({}))'.format(var_id, ','.join(map(lambda x: str(x + 1), replied_so_far)), ','.join(map(str, scores_so_far))))
say('Thêta du candidat :', r('theta')[0])
# pm = r('semTheta(theta, itembank[c({}),])'.format(','.join(map(str, replied_so_far))))
def predict_performance(self, var_id='', theta=None):
if theta is None:
return tuple(r('Pi(theta{}, itembank)$Pi'.format(var_id)))
else:
return tuple(r('Pi({}, itembank)$Pi'.format(theta)))
def get_prefix(self):
return 'irt' if self.criterion == 'MFI' else 'mepv-irt'
def get_backup_path(self):
return 'backup/%s.rdata' % self.checksum
def get_dim(self):
return 1