-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsa_v4.py
358 lines (287 loc) · 13.3 KB
/
lsa_v4.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
# # Learning Standards Analysis
#
# Simeon Wong
# MAT188 2023F at the University of Toronto
# %%
import pandas as pd
import numpy as np
import glob
from itertools import compress
import wwparse_v2
from tqdm import tqdm
import re
import argparse
from Configs import Configs
import os, os.path
import yaml
def extract_tutorial_number(x: str):
rel = re.search(r'TUT(\d{4})', x)
if rel is None:
return pd.NA
else:
return int(rel.group(1))
def grade_by_ls(student: tuple, lsref):
this_student, this_student_scores = student
this_standards_achieved = pd.Series(index=pd.MultiIndex.from_frame(
lsref[['modality', 'standard']].drop_duplicates()),
name=this_student)
for this_idx, this_standard in lsref.iterrows():
question_keys = [x.strip() for x in this_standard['reqs'].split(',')]
if '|' in question_keys[0]:
n_correct_required = int(question_keys[0].split('|')[0])
question_keys[0] = question_keys[0].split('|')[1]
ratio_required = n_correct_required / len(question_keys)
else:
ratio_required = 1
# get correctness for each question
question_isgraded = [
np.any(this_student_scores.loc[x, 'is_graded'] == True)
if x in this_student_scores.index else True for x in question_keys
]
question_iscorrect = [
np.any(this_student_scores.loc[x, 'correct'])
if x in this_student_scores.index else False for x in question_keys
]
# check if the required number of questions are correct
if np.sum(question_isgraded) == 0:
ls_cor = np.nan
else:
corsum = np.nansum(question_iscorrect)
ls_cor = int(
(corsum >= 1)
and (corsum / np.sum(question_isgraded) >= ratio_required))
# if all requirements are met, set this standard to true
this_standards_achieved[tuple(this_standard[['modality',
'standard']])] = ls_cor
return this_standards_achieved
def load_data(config: Configs):
#######################################################################
## Data loading
# import table of learning standards
lsref = pd.read_excel(config.input_paths.standards_lookup_table,
sheet_name='grading',
engine='openpyxl')
lsref = lsref.set_index('standard').stack().reset_index()
lsref.columns = ['standard', 'modality', 'reqs']
# ignore exams
if not config.compute_exams:
lsref = lsref[lsref['modality'] != 'exam']
# load roster
roster = pd.read_csv(config.input_paths.utatg_roster)
gradebook = pd.read_csv(config.input_paths.quercus_gradebook)
gradebook = gradebook[~gradebook['SIS User ID'].isna()]
gradebook = gradebook[['SIS User ID',
'Section']].set_index(['SIS User ID'])
gradebook['tut'] = gradebook['Section'].apply(extract_tutorial_number)
# gradebook['tut_day'] = gradebook['tut'].apply(
# lambda x: tut_day_tbl.loc[x, 'day'])
roster = roster[roster['UTORid'].isin(
gradebook.index)] # drop students who dropped the course
roster.rename(columns={'Student Number': 'SID'}, inplace=True)
roster['SID'] = pd.to_numeric(roster['SID'], errors='coerce')
if config.debug:
roster = pd.concat(
(roster[:20], roster[-20:]
)) # DEBUGGING: only keep first and last 20 students for speed
scores = []
##### WEBWORK #####
for filename in glob.glob(config.input_paths.webwork_glob):
print(f'Loading {filename}...')
this_score = wwparse_v2.parse_csv(filename)
# a question is correct if all parts are correct
this_score['correct'] = this_score['score'] == 1
this_score['is_graded'] = True
scores.append(this_score)
##### TUTORIALS #####
if isinstance(config.input_paths.tutorial_consolidated, str):
config.input_paths.tutorial_consolidated = [
config.input_paths.tutorial_consolidated
]
for this_file in config.input_paths.tutorial_consolidated:
ext = os.path.splitext(this_file)[1]
print(f'Loading {this_file}...')
if ext.lower() == '.csv':
tdc = pd.read_csv(this_file)
elif ext.lower() == '.xlsx':
tdc = pd.read_excel(this_file)
tdc_score_keys = [
x for x in tdc.columns
if re.match(r'^tut\d{1,2}[\.\-]\d[\.\-]\d{1,2}$', x)
]
tdc_scores = tdc.melt(id_vars=['UTORid'],
value_vars=tdc_score_keys,
var_name='score_key',
value_name='correct')
tdc_scores[['tut',
'tut_gradegroup']] = tdc_scores['score_key'].str.extract(
r'tut(\d{1,2})[\.\-](\d)[\.\-]\d{1,2}')
tdc_scores['tut'] = tdc_scores['tut'].astype(int)
tdc_scores['tut_gradegroup'] = tdc_scores['tut_gradegroup'].astype(int)
# normalize score_key
tdc_scores['score_key'] = tdc_scores['score_key'].str.replace('.', '-')
# identify which questions are graded based on listed grading group
tdc_graded_cols = [x for x in tdc.columns if re.match(r'SBG\d{1,2}', x)]
if len(tdc_graded_cols) > 0:
tdc_graded = tdc.melt(id_vars=['UTORid'],
value_vars=tdc_graded_cols,
var_name='tut',
value_name='tut_gradegroup')
tdc_graded['tut'] = tdc_graded['tut'].str.extract(r'SBG(\d{1,2})').astype(
int)
tdc_scores['is_graded'] = tdc_scores.apply(lambda x: (
(x['UTORid'] == tdc_graded['UTORid']) &
(x['tut'] == tdc_graded['tut']) &
(x['tut_gradegroup'] == tdc_graded['tut_gradegroup'])).sum() > 0,
axis=1)
# if graded questions are explicitly given via SBG numbers, then use that
tdc_scores.loc[:,'correct'] = tdc_scores['correct'].fillna(False)
else:
# if SBG numbers aren't given, then questions are graded if not NA
# tdc_scores['is_graded'] = tdc_scores['correct'].notna()
tdc_scores['is_graded'] = tdc_scores['correct'] == True
tdc_scores = tdc_scores.rename(columns={'UTORid': 'login_name'})
tdc_scores = tdc_scores[[
'login_name', 'score_key', 'correct', 'is_graded'
]]
scores.append(tdc_scores)
# load midterm data
if config.compute_exams and config.input_paths.midterm_consolidated_glob:
for file in glob.glob(config.input_paths.midterm_consolidated_glob):
print('Loading ' + file)
midterm_data = pd.read_excel(file)
# merge with roster
midterm_data = midterm_data.merge(roster[['Email', 'UTORid']],
how='left',
on='Email')
midterm_data = midterm_data.rename(
columns={'UTORid': 'login_name'})
# parse score key
ls_cols = [
x for x in midterm_data.columns
if re.match(r'^ex\d\-\d\-\d{1,2}', x)
]
midterm_data = midterm_data[['login_name'] + ls_cols]
midterm_data.columns = ['login_name'] + ls_cols
# stack into long format
midterm_data = midterm_data.melt(id_vars='login_name',
var_name='score_key',
value_name='correct')
midterm_data = midterm_data.dropna(subset=['login_name'])
# check if correct has type string, convert to int
midterm_data['correct'] = midterm_data['correct'].map({
'TRUE': 1,
'FALSE': 0,
True: 1,
False: 0
})
midterm_data['is_graded'] = True
scores.append(midterm_data)
# concatenate all scores
scores = pd.concat(scores, ignore_index=True)
# load manually scored items
manual_scores = pd.read_excel(config.input_paths.manual_scores)
scores_key = list(scores[['login_name',
'score_key']].itertuples(index=False, name=None))
manual_scores_key = list(manual_scores[['login_name', 'score_key'
]].itertuples(index=False,
name=None))
in_manual_scores = [x in manual_scores_key for x in scores_key]
# drop values from scores where login_name and score_key match from manual_scores
scores = scores.iloc[~np.array(in_manual_scores)]
scores = pd.concat([scores, manual_scores], ignore_index=True)
# remove score rows without an associated utorid
scores = scores[scores['login_name'] != ''].dropna(subset=['login_name'])
# save for debugging
scores.to_csv(config.output_dir + '/debug_raw_scores.csv')
return scores, roster, lsref
def run(config: Configs):
scores, roster, lsref = load_data(config)
#######################################################################
# Which learning standards has each student achieved?
# remove requirements that don't have associated questions in our score db
uniq_scorekey = scores['score_key'].unique()
# Save unique score keys for debugging
pd.Series(uniq_scorekey).to_csv(config.output_dir +
'/debug_uniq_scorekey.csv',
index=False)
for this_idx, this_standard in lsref.iterrows():
if (this_standard['reqs'] == '') or pd.isna(this_standard['reqs']):
continue
# parse standards
if '|' in this_standard['reqs']:
n_req, reqstr = this_standard['reqs'].split('|')[0:2]
n_req = int(n_req)
reqs = reqstr.split(',')
else:
reqs = this_standard['reqs'].split(',')
n_req = len(reqs)
in_db = [tr.strip() in uniq_scorekey for tr in reqs]
# only keep standards that have questions in the db
reqs = list(compress(reqs, in_db))
n_req = min(n_req, len(reqs))
# reconstruct reqs string
if n_req > 0:
lsref.loc[this_idx, 'reqs'] = str(n_req) + '|' + (','.join(reqs))
else:
lsref.loc[this_idx, 'reqs'] = pd.NA
# remove empty standards with no associated items
lsref = lsref[~lsref['reqs'].isna()]
standards_achieved = []
for this_student in tqdm(roster['UTORid'].unique(),
desc='Evaluating learning standards by student'):
this_student_scores = scores[scores['login_name'] ==
this_student].set_index('score_key')
standards_achieved.append(
grade_by_ls((this_student, this_student_scores), lsref))
standards_achieved = pd.concat(standards_achieved, axis=1).T
# compute fraction standards achieved across each modality
modalities = lsref['modality'].unique()
for this_modality in modalities:
this_modality_standards = standards_achieved.loc[:, this_modality]
standards_achieved.loc[:, (
'summary',
f'frac_{this_modality}')] = this_modality_standards.mean(
axis=1, skipna=True)
standards_achieved.loc[:, (
'summary',
f'achieved_{this_modality}')] = this_modality_standards.sum(
axis=1, skipna=True)
standards_achieved.loc[:, (
'summary',
f'count_{this_modality}')] = this_modality_standards.count(axis=1)
# Join student names for easy lookup
roster.set_index('UTORid', inplace=True)
standards_achieved = standards_achieved[standards_achieved.index.isin(
roster.index)]
standards_achieved[('student',
'first_name')] = roster.loc[standards_achieved.index,
'First Name']
standards_achieved[('student',
'last_name')] = roster.loc[standards_achieved.index,
'Last Name']
standards_achieved[('student',
'SID')] = roster.loc[standards_achieved.index, 'SID']
standards_achieved.sort_index(axis=0, inplace=True)
standards_achieved.to_csv(config.output_dir + '/standards_achieved.csv')
#%% Main
#######################################################################
# Parse arguments
if __name__ == '__main__':
import sys
parser = argparse.ArgumentParser()
parser.add_argument('config', type=str)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--no-exams', dest='compute_exams', action='store_false')
parser.add_argument('--generate-reports', action='store_true')
args = parser.parse_args(
) if 'ipykernel' not in sys.modules else parser.parse_args(
['--debug', 'config2024.yml'])
with open(args.config, 'r') as f:
config = yaml.safe_load(f)
config = Configs(config | args.__dict__)
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
run(config)
if args.generate_reports:
import make_ls_report_v2
make_ls_report_v2.run(config)