-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanalysis.py
More file actions
271 lines (204 loc) · 8.24 KB
/
analysis.py
File metadata and controls
271 lines (204 loc) · 8.24 KB
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
import copy
import matplotlib
from matplotlib import pyplot as plt
import json
import os
import pymongo
import numpy as np
import collections
from scipy.stats import norm
import argparse
import pickle
from agent import State, Action
import dateutil
from sklearn.manifold import TSNE
import altair
def load_data(config):
client = pymongo.MongoClient()
sessions = client['tutor'].usersessions.find({})
sessions = [s for s in sessions
if s.get('endTimestamp') is not None and
s.get('survey', {}).get('experience', '') != 'Test']
with open(config['testProblems']) as f:
c = json.load(f)
testProblems = { str(p['id']) : p for p in c['testProblems'] }
return { 'sessions': sessions, 'testProblems': testProblems }
def test_score(testAnswers, problems):
c = []
for a in testAnswers:
c.append(a['answer'] == problems[a['id']]['solution'])
return np.mean(c)
def session_length(session, data):
return (session['endTimestamp'] - session['beginTimestamp']).total_seconds() / 60
def pretest_score(session, data):
return test_score(session['preTestResponses'], data['testProblems'])
def posttest_score(session, data):
return test_score(session['postTestResponses'], data['testProblems'])
def correctness_exercise_phase(session, data):
correct = sum([1 for e in session['exerciseResponses'] if e['response'] == 0])
return correct / len(session['exerciseResponses'])
def compute_statistics(scores):
return { 'raw': np.array(scores),
'mean': np.mean(scores),
'max': np.max(scores),
'min': np.min(scores) }
def aggregate_session_statistic(f, data):
scores = []
for s in data['sessions']:
scores.append(f(s, data))
return compute_statistics(scores)
def exercise_correctness(k, responses, data):
correct = sum([1 for e in responses if e['response'] == 0])
return correct / len(responses)
def number_of_occurrences(k, responses, data):
return len(responses)
def aggregate_exercise_statistic(f, data):
responses_by_exercise = collections.defaultdict(list)
for i, s in enumerate(data['sessions']):
for r in s['exerciseResponses']:
responses_by_exercise[r['id']].append({ **r, 'session': s })
scores = []
for k, v in responses_by_exercise.items():
scores.append(f(k, v, data))
return compute_statistics(scores)
def ith_question(i, question, response, state):
return True, i // 2, state
def ith_question_with_op(op, n=1):
def criterion(i, question, response, state):
if question.count(op) < n:
return False, None, None
return True, i // 2, state
return criterion
def bernoulli_ci(values):
p = np.mean(values)
return (p, norm.ppf(0.975) * np.sqrt(p * (1 - p) / len(values)), len(values))
def analyze_student_success_rate(dataset, criterion):
results = collections.defaultdict(list)
for st in dataset.obs_by_student.values():
student_state = {}
i = 0
for q, r in st:
use, key, student_state = criterion(i, dataset.problems[q], r, student_state)
if use:
i += 1
results[key].append(r)
return { k:bernoulli_ci(v) for k, v in results.items() }
def question_difficulty(q, r):
return True, q
def question_length(q, r):
return True, len(q)
def analyze_question_difficulty(dataset, criterion):
results = collections.defaultdict(list)
for k, v in dataset.obs_by_problem.items():
for st, r in v:
use, key = criterion(k, r)
if use:
results[key].append(r)
return { k:bernoulli_ci(v) for k, v in results.items() }
def compare_learning_algorithms(config):
print('Comparing learning algorithms...')
results = config['results']
output = config['output']
data_points = collections.defaultdict(list)
for path in results:
with open(path, 'rb') as f:
r = pickle.load(f)
for p in r:
algorithm, domain = p['name'], p['domain']
data_points[algorithm, domain].append(p)
algorithms = list(set(k[0] for k in data_points.keys()))
domains = list(set(k[1] for k in data_points.keys()))
success_rate = {}
for a in algorithms:
for d in domains:
if len(data_points[a, d]):
success_rate[a, d] = '{:.3f}'.format(max(map(lambda r: r['success_rate'],
data_points[a, d])))
else:
success_rate[a, d] = 'N/A'
with open(output, 'w') as f:
f.write(f'\\begin{{tabular}}{{| l | {" c" * len(domains)} |}}\n')
f.write('\\hline')
headers = ['Algorithm'] + domains
f.write(' & '.join('\\textbf{{{}}}'.format(c) for c in headers))
f.write('\\\\ \\hline\n')
for a in algorithms:
line = [a]
for d in domains:
line.append(success_rate[a, d])
f.write(' & '.join(line))
f.write('\\\\\n')
f.write('\\end{tabular}\n')
print('Wrote', output)
def compare_agents(config):
if config.get('compare_learning_algorithms'):
compare_learning_algorithms(config['compare_learning_algorithms'])
def analyze_user_study(config):
dump = config['db_dump']
db = json.load(open(dump))
i = 0
for row in db:
if not row.get('endTimestamp'):
continue
i += 1
time_taken = (dateutil.parser.parse(row['endTimestamp']['$date']) -
dateutil.parser.parse(row['beginTimestamp']['$date']))
exercise_responses = row['exerciseResponses']
post_test_responses = row['postTestResponses']
n_exercises = len(exercise_responses)
n_post_test_questions = len(post_test_responses)
exercise_score = sum(1 for e in exercise_responses if e['correct'])
post_test_score = sum(1 for e in post_test_responses if e['correct'])
print(f'Participant #{i}:')
print('Curriculum:', row['curriculum'])
print('Time elapsed:', time_taken)
print('Survey:', row['survey'])
print(f'Exercise phase: {exercise_score}/{n_exercises} correct')
print(f'Post test: {post_test_score}/{n_post_test_questions} correct')
print('Post test responses:', post_test_responses)
print()
def load_run_output(path: str):
with open(path, 'rb') as pkl:
results = pickle.load(pkl)
return [{'algorithm': r['name'],
'run_index': r.get('run_index', 0),
'domain': r['domain'],
'success_rate': r['success_rate'],
'n_steps': r['n_steps'] - (r['n_steps'] % 1000)}
for r in results]
def load_experiment_data(output_root):
data_points = []
for root, dirs, files in os.walk(output_root):
if 'results.pkl' in files:
data_points.extend(load_run_output(os.path.join(root, 'results.pkl')))
return data_points
def make_plot(data: list[dict], plot_id: str):
with open(os.path.join('vega-lite', plot_id + '.json')) as f:
plot_spec = json.load(f)
plot_spec['data'] = {'values': data}
return altair.Chart.from_dict(plot_spec)
def embed_problems_tsne(model_path: str, problems: list[dict]) -> list[(float, float)]:
model = torch.load(model_path)
device = torch.device('cpu')
model.to(device)
embeddings = model.embed_states([State([p['problem']], [], 0.0) for p in problems]).numpy()
tsne = TSNE()
X = tsne.fit_transform(embeddings)
problems = copy.deepcopy(problems)
for i, p in enumerate(problems):
p['x'] = X[i, 0]
p['y'] = X[i, 1]
return problems
if __name__ == '__main__':
parser = argparse.ArgumentParser('Analyze experiments & user study results')
parser.add_argument('--config', required=True, help='Path to config file.')
parser.add_argument('--user-study', help='Analyze data from the tutoring user study.',
action='store_true')
parser.add_argument('--agent-comparison', help='Compare different learning agents.',
action='store_true')
opt = parser.parse_args()
config = json.load(open(opt.config))
if opt.user_study:
analyze_user_study(config)
elif opt.agent_comparison:
compare_agents(config)