-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathearly_often.py
executable file
·388 lines (328 loc) · 15.9 KB
/
early_often.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
#!/usr/bin/env python3
"""Calculates incremental development metrics in the form
of Early/Often indices.
Simply put, an early often index
is the mean amount of code written on a given day leading
up to the project due date. We calculate similar metrics
for [solution/test] code writing and [solution/test] launches.
To use:
from early_often import earlyoften, or
./early_often.py <input file> <web-cat submissions file> <duedates file> <output file> on the command line
"""
from utils import get_term
from load_datasets import load_submission_data
import sys
import datetime
import re
import json
import pandas as pd
import numpy as np
def userearlyoften(usergroup, due_date_data, submissions, usercol='userId', lognosubs=False):
"""
This function acts on data for one student's sensordata.
Generally, it is invoked by earlyoften in a split-apply-combine procedure.
Args:
usergroup (DataFrame): Event stream for a student working on a single project
due_date_data (dict): Dictionary containing due dates in millisecond timestamps
submissions (DataFrame): Last submission from each student
usercol (str): Name of the column identifying the user (default "userId")
lognosubs (bool): Print a message for users for whom submissions were not found?
Returns:
A *DataFrame* containing the early often measurements for the user on a given assignment.
"""
prev_row = None
total_weighted_edits_bytes = []
total_edits_bytes = []
total_weighted_edits_stmts = []
total_edits_stmts = []
total_weighted_solution_bytes = []
total_solution_bytes = []
total_weighted_solution_stmts = []
total_solution_stmts = []
total_weighted_test_bytes = []
total_test_bytes = []
total_weighted_test_stmts = []
total_test_stmts = []
total_weighted_solution_methods = []
total_solution_methods = []
total_weighted_test_methods = []
total_test_methods = []
total_weighted_launches = []
total_weighted_test_launches = []
total_weighted_normal_launches = []
total_test_assertions = []
total_weighted_test_assertions = []
total_weighted_debug_sessions = []
curr_sizes_bytes = {}
curr_sizes_stmts = {}
curr_sizes_methods = {}
curr_test_assertions = {} # for each file
user_id, assignment = usergroup.name # returns a tuple
# get the term and assignment due date
first = usergroup.iloc[0]
first_time = first['time']
epoch = datetime.datetime.utcfromtimestamp(0) # seconds
first_time = (first_time - epoch).total_seconds()
term = get_term(first_time)
assignment_number = int(re.search(r'\d', assignment).group())
due_time = due_date_data[term]['assignment%d' % (assignment_number)]['dueTime']
due_time = int(due_time)
due_date = datetime.date.fromtimestamp(due_time / 1000)
if usercol == 'email':
user_id = user_id.split('@')[0]
try:
lastsubmissiontime = submissions.loc[user_id, 'Project {}'.format(assignment_number)]['submissionTimeRaw']
except KeyError:
# Either the user or the assignment is not present in the submission list.
# Extract the information manually.
if lognosubs:
print('Cannot find final submission for {} on Project {}'.format(user_id, assignment_number))
return None
for index, row in usergroup.iterrows():
prev_row = row if prev_row is None else prev_row
time = row['time']
date = time.date()
days_to_deadline = (due_date - date).days
if time > lastsubmissiontime or days_to_deadline < -4:
prev_row = row
continue
if repr(row['Type']) == repr('Edit') and len(row['Class-Name']) > 0:
class_name = repr(row['Class-Name'])
curr_bytes = int(row['Current-Size'])
curr_stmts = int(row['Current-Statements'])
curr_methods = int(row['Current-Methods'])
prev_bytes = curr_sizes_bytes.get(class_name, 0)
prev_stmts = curr_sizes_stmts.get(class_name, 0)
prev_methods = curr_sizes_methods.get(class_name, 0)
byte_edit_size = abs(prev_bytes - curr_bytes)
stmt_edit_size = abs(prev_stmts - curr_stmts)
method_edit_size = abs(prev_methods - curr_methods)
assertion_change_size = 0
if row['Current-Test-Assertions'] != '':
curr_assertions = int(row['Current-Test-Assertions']) # for the current file
prev_assertions = curr_test_assertions.get(class_name, 0)
assertion_change_size = abs(prev_assertions - curr_assertions)
curr_test_assertions[class_name] = curr_assertions
on_test_case = int(row['onTestCase']) == 1
if byte_edit_size > 0:
total_weighted_edits_bytes.append(byte_edit_size * days_to_deadline)
total_edits_bytes.append(byte_edit_size)
if on_test_case:
total_weighted_test_bytes.append(byte_edit_size * days_to_deadline)
total_test_bytes.append(byte_edit_size)
else:
total_weighted_solution_bytes.append(byte_edit_size * days_to_deadline)
total_solution_bytes.append(byte_edit_size)
if stmt_edit_size > 0:
total_weighted_edits_stmts.append(stmt_edit_size * days_to_deadline)
total_edits_stmts.append(stmt_edit_size)
if on_test_case:
total_weighted_test_stmts.append(stmt_edit_size * days_to_deadline)
total_test_stmts.append(stmt_edit_size)
else:
total_weighted_solution_stmts.append(stmt_edit_size * days_to_deadline)
total_solution_stmts.append(stmt_edit_size)
if method_edit_size > 0:
if on_test_case:
total_weighted_test_methods.append(method_edit_size * days_to_deadline)
total_test_methods.append(method_edit_size)
else:
total_weighted_solution_methods.append(method_edit_size * days_to_deadline)
total_solution_methods.append(method_edit_size)
if assertion_change_size > 0:
total_weighted_test_assertions.append(assertion_change_size * days_to_deadline)
total_test_assertions.append(assertion_change_size)
curr_sizes_stmts[class_name] = curr_stmts
curr_sizes_methods[class_name] = curr_methods
curr_sizes_bytes[class_name] = curr_bytes
elif (repr(row['Type']) == repr('Launch')):
total_weighted_launches.append(days_to_deadline)
if (repr(row['Subtype']) == repr('Test')):
total_weighted_test_launches.append(days_to_deadline)
elif (repr(row['Subtype']) == repr('Normal')):
total_weighted_normal_launches.append(days_to_deadline)
elif (repr(row['Type']) == repr('DebugSession')):
length = float(row['length'])
if length > 30:
total_weighted_debug_sessions.append(days_to_deadline)
prev_row = row
byte_early_often_index = np.sum(total_weighted_edits_bytes) / np.sum(total_edits_bytes)
stmt_early_often_index = np.sum(total_weighted_edits_stmts) / np.sum(total_edits_stmts)
solution_byte_early_often_index = np.sum(total_weighted_solution_bytes) / np.sum(total_solution_bytes)
solution_stmt_early_often_index = np.sum(total_weighted_solution_stmts) / np.sum(total_solution_stmts)
solution_meth_early_often_index = np.sum(total_weighted_solution_methods) / np.sum(total_solution_methods)
test_byte_early_often_index = np.sum(total_weighted_test_bytes) / np.sum(total_test_bytes)
test_stmt_early_often_index = np.sum(total_weighted_test_stmts) / np.sum(total_test_stmts)
test_meth_early_often_index = np.sum(total_weighted_test_methods) / np.sum(total_test_methods)
test_assrt_early_often_index = np.sum(total_weighted_test_assertions) / np.sum(total_test_assertions)
launch_early_often = np.mean(total_weighted_launches)
launch_median = np.median(total_weighted_launches)
launch_sd = np.std(total_weighted_launches)
test_launch_early_often = np.mean(total_weighted_test_launches)
test_launch_median = np.median(total_weighted_test_launches)
test_launch_sd = np.std(total_weighted_test_launches)
normal_launch_early_often = np.mean(total_weighted_normal_launches)
normal_launch_median = np.median(total_weighted_normal_launches)
normal_launch_sd = np.std(total_weighted_normal_launches)
debug_session_early_often = np.mean(total_weighted_debug_sessions)
debug_session_median = np.median(total_weighted_debug_sessions)
debug_session_sd = np.std(total_weighted_debug_sessions)
stretched_bytes = []
for weighted, unweighted in zip(total_weighted_edits_bytes, total_edits_bytes):
relative_time = weighted / unweighted
for i in range(weighted):
stretched_bytes.append(relative_time)
byte_edit_median = np.median(stretched_bytes)
byte_edit_sd = np.std(stretched_bytes)
stretched_solution_bytes = []
for weighted, unweighted in zip(total_weighted_solution_bytes, total_solution_bytes):
relative_time = weighted / unweighted
for i in range(weighted):
stretched_solution_bytes.append(relative_time)
solution_byte_edit_median = np.median(stretched_solution_bytes)
solution_byte_edit_sd = np.std(stretched_solution_bytes)
stretched_test_bytes = []
for weighted, unweighted in zip(total_weighted_test_bytes, total_test_bytes):
relative_time = weighted / unweighted
for i in range(weighted):
stretched_test_bytes.append(relative_time)
test_byte_edit_median = np.median(stretched_test_bytes)
test_byte_edit_sd = np.std(stretched_test_bytes)
stretched_stmts = []
for weighted, unweighted in zip(total_weighted_edits_stmts, total_edits_stmts):
relative_time = weighted / unweighted
for i in range(weighted):
stretched_stmts.append(relative_time)
stmt_edit_median = np.median(stretched_stmts)
stmt_edit_sd = np.std(stretched_stmts)
stretched_assertions = []
for weighted, unweighted in zip(total_weighted_test_assertions, total_test_assertions):
relative_time = weighted / unweighted
for i in range(weighted):
stretched_assertions.append(relative_time)
test_assertion_median = np.median(stretched_assertions)
test_assertion_sd = np.std(stretched_assertions)
to_write = {
'byteEarlyOftenIndex': byte_early_often_index,
'byteEditMedian': byte_edit_median,
'byteEditSd': byte_edit_sd,
'stmtEarlyOftenIndex': stmt_early_often_index,
'stmtEditMedian': stmt_edit_median,
'stmtEditSd': stmt_edit_sd,
'solutionByteEarlyOftenIndex': solution_byte_early_often_index,
'solutionByteEditMedian': solution_byte_edit_median,
'solutionByteEditSd': solution_byte_edit_sd,
'solutionStmtEarlyOftenIndex': solution_stmt_early_often_index,
'solutionMethodsEarlyOftenIndex': solution_meth_early_often_index,
'testByteEarlyOftenIndex': test_byte_early_often_index,
'testByteEditMedian': test_byte_edit_median,
'testByteEditSd': test_byte_edit_sd,
'testStmtsEarlyOftenIndex': test_stmt_early_often_index,
'testMethodsEarlyOftenIndex': test_meth_early_often_index,
'assertionsEarlyOftenIndex': test_assrt_early_often_index,
'assertionsMedian': test_assertion_median,
'assertionSd': test_assertion_sd,
'launchEarlyOften': launch_early_often,
'launchMedian': launch_median,
'launchSd': launch_sd,
'testLaunchEarlyOften': test_launch_early_often,
'testLaunchMedian': test_launch_median,
'testLaunchSd': test_launch_sd,
'normalLaunchEarlyOften': normal_launch_early_often,
'normalLaunchMedian': normal_launch_median,
'normalLaunchSd': normal_launch_sd,
'debugSessionEarlyOften': debug_session_early_often,
'debugSessionMedian': debug_session_median,
'debugSessionSd': debug_session_sd
}
return pd.Series(to_write)
def earlyoften(infile, submissionpath, duetimepath, outfile=None, dtypes=None, date_parser=None):
"""Calculate Early/Often indices for developers based on IDE events.
Early/Often refers to the mean time of a certain type of event, in terms of
"days until the deadline". Applying the same concept, we also calculate
medians and standard deviations.
Args:
infile (str): Path to a file containing raw SensorData
outfile (str, optional): Path to a file where combined early often metrics should be written (optional).
If *None*, output is written to a Pandas DataFrame.
submissionpath (str): Path to Web-CAT submissions. Used only to determine the time of the final submission.
duetimepath (str): Path to a JSON file containing due date data for assignments in different terms.
dtypes (dict, optional, no-CLI): Column data types (also only reads the specified columns)
date_parser (func, optional, no-CLI): A function or lambda to parse timestamps.
Returns:
A *DataFrame* if no *outfile* is specified. *None* otherwise.
"""
submissions = load_submission_data(submissionpath)
print('0. Finished reading submission data.')
# Import event stream for all students
if not dtypes:
dtypes = {
'userId': str,
'projectId': str,
'email': str,
'CASSIGNMENTNAME': str,
'time': str,
'Class-Name': object,
'Unit-Type': object,
'Type': object,
'Subtype': object,
'Subsubtype': object,
'onTestCase': object,
'Current-Statements': object,
'Current-Methods': object,
'Current-Size': object,
'Current-Test-Assertions': object
}
if not date_parser:
# assume timestamps are in milliseconds since the epoch unless specified
date_parser = lambda d: datetime.datetime.fromtimestamp(int(d) / 1000)
df = pd.read_csv(infile, dtype=dtypes, na_values=[], low_memory=False, usecols=list(dtypes.keys()),
date_parser=date_parser, parse_dates=['time']) \
.sort_values(by=['time'], ascending=[1]) \
.fillna('')
print('1. Finished reading raw sensordata.')
# Group data by student and project
if 'userId' in df.columns:
user_id = 'userId'
elif 'email' in df.columns:
user_id = 'email'
else:
user_id = 'userName'
assignmentcol = 'assignment'
if 'cleaned_assignment' in df.columns:
assignmentcol = 'cleaned_assignment'
elif 'CASSIGNMENTNAME' in df.columns:
assignmentcol = 'CASSIGNMENTNAME'
grouped = df.groupby([user_id, assignmentcol])
print('2. Finished grouping data. Calculating measures now. This could take some time...')
due_date_data = None
with open(duetimepath) as data_file:
due_date_data = json.load(data_file)
results = grouped.apply(userearlyoften,
due_date_data=due_date_data,
submissions=submissions,
usercol=user_id)
# Write out
if outfile:
results.to_csv(outfile)
else:
return results
def main(args):
"""Parses CLI arguments and begins execution."""
infile = args[0]
submissionpath = args[1]
duetimepath = args[2]
outfile = args[3]
if args.length > 4:
dateformat = args[4]
else:
dateformat = None
try:
results = earlyoften(infile=infile, submissionpath=submissionpath, duetimepath=duetimepath, outfile=outfile, dateformat=dateformat) # If outfile is None, stores results in memory
except FileNotFoundError:
print("Error! File '%s' does not exist." % infile)
if __name__ == '__main__':
if len(sys.argv) < 5:
print(__doc__)
sys.exit()
main(sys.argv[1:])