-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
307 lines (259 loc) · 11 KB
/
utils.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
"""Convenience methods for common tasks, such as retrieiving
subsets of events, converting timestamps, getting a term, etc.
To use:
`import utils`
"""
import re
import csv
import copy
import logging
import datetime
from urllib import parse
import numpy as np
import pandas as pd
logging.basicConfig(filename='sensordata-utils.log', filemode='w', level=logging.WARN)
def get_term(timestamp: float) -> str:
"""Returns a term id based on a timestamp in seconds. If the provided
timestamp is in milliseconds this method will truncate the timestamp to seconds.
"""
inmillis = len(str(abs(timestamp))) >= 13
if inmillis:
timestamp = int(timestamp / 1000)
eventtime = datetime.datetime.fromtimestamp(timestamp)
year = eventtime.year
month = eventtime.month
if month >= 8:
return 'fall%d' % year
if month >= 7: # TODO: Deal with summer terms?
return 'summer-1-%d' % year
if month > 5:
return 'summer-2-%d' % year
if month >= 1:
return 'spring%d' % year
return None
#: The typical fieldnames included in sensordata events.
DEFAULT_FIELDNAMES = [
'email',
'CASSIGNMENTNAME',
'time',
'Class-Name',
'Unit-Type',
'Unit-Name',
'From-Unit-Name',
'To-Unit-Name',
'Type',
'Subtype',
'Subsubtype',
'onTestCase',
'Current-Statements',
'Current-Methods',
'Current-Size',
'Current-Test-Assertions',
'ConsoleOutput'
]
def raw_to_csv(inpath: str, outpath: str, fieldnames=None) -> None:
"""
Given a file of newline separated URLs, writes the URL query params as
rows in CSV format to the specified output file.
If your URLs are DevEventTracker posted events, then you probably want
the :attr:`DEFAULT_FIELDNAMES`. These fieldnames can be imported and
modified as needed.
"""
with open(inpath, 'r') as infile, open(outpath, 'w') as outfile:
if not fieldnames:
fieldnames = DEFAULT_FIELDNAMES
writer = csv.DictWriter(outfile, delimiter=',', fieldnames=fieldnames)
writer.writeheader()
for line in infile:
event = processline(line, fieldnames)
if event is not None:
if isinstance(event, list):
for item in event:
writer.writerow(item)
else:
writer.writerow(event)
def processline(url, fieldnames=None, filtertype=None):
"""
Given a URL, returns a dict object containing the key-value
pairs from its query params. Filters for a specific Type if specified.
Args:
fieldnames (list, default=None): The list of fieldnames to capture. If `None`,
uses `DEFAULT_FIELDNAMES`.
filtertype (bool): Only return a dict if the query param for Type == filtertype
Returns:
(dict) containing the key-value pairs from the the url's query params.
"""
if not fieldnames:
fieldnames = DEFAULT_FIELDNAMES
if 'http' in url:
url = url.split(':', 1)[-1]
items = parse.parse_qs(parse.urlparse(url).query)
kvpairs = {}
for key, value in items.items():
if _shouldwritekey(key, fieldnames):
kvpairs[key] = value[0].rstrip('\n\r')
elif key.startswith('name'): # some items are in the form name0=somekey, value0=somevalue
k = value[0] # e.g., "name0=k"
num = re.search(r'(\d+)$', key).group(0)
val = items.get('value{}'.format(num), [''])[0] # e.g., "value0=v", "value0="
if _shouldwritekey(k, fieldnames):
kvpairs[k] = val.rstrip('\n\r')
time = int(float(kvpairs.get('time', 0))) # time is not guaranteed to be present
kvpairs['time'] = time if time != 0 else ''
if filtertype and kvpairs['Type'] != filtertype:
return None
if kvpairs.get('Class-Name', '').endswith('Test') and \
kvpairs.get('Current-Test-Assertions', 0) != 0:
kvpairs['onTestCase'] = 1
return kvpairs
def split_termination_events(df):
"""Typically, Termination events contain results of several test methods being run at
once. This method takes a DataFrame containing such Termination events and returns it
with each one split into its own event (with the same timestamp).
"""
flattened = [
event
for sublist in df.apply(__split_one_termination, axis=1)
for event in sublist
]
return pd.DataFrame(flattened)
def __split_one_termination(t):
try:
t = t.to_dict()
if t['Type'] != 'Termination' or t['Subtype'] != 'Test':
return [t]
try:
tests = t['Unit-Name'].strip('|').split('|')
outcomes = t['Subsubtype'].strip('|').split('|')
expandedevents = []
for test, outcome in zip(tests, outcomes):
newevent = copy.deepcopy(t)
newevent['Unit-Name'] = test
newevent['Subsubtype'] = outcome
newevent['Unit-Type'] = 'Method'
expandedevents.append(newevent)
except AttributeError:
return [t]
except KeyError:
logging.error('Missing some required keys to split termination event. Need \
Type, Subtype, and Subsubtype. Doing nothing.')
return [t]
return expandedevents
def test_outcomes(te):
"""Parse outcomes for the specified test termination."""
outcomes = te['Subsubtype'].strip('|').split('|')
failures = outcomes.count('Failure')
successes = outcomes.count('Success')
errors = len(outcomes) - (failures + successes)
return pd.Series({
'successes': successes,
'failures': failures,
'errors': errors
})
def _shouldwritekey(key, fieldnames):
if not fieldnames:
return True
if key in fieldnames:
return True
return False
def maptouuids(sensordata=None, sdpath=None, uuids=None, uuidpath=None, crnfilter=None,
crncol='crn', usercol='email', assignmentcol='CASSIGNMENTNAME', due_dates=None):
"""Map sensordata to users and assignments based on studentProjectUuids.
Args:
sensordata (pd.DataFrame): A DataFrame containing sensordata
sdpath (str): Path to raw sensordata (CSV). Either this or `sensordata`
must be provided.
uuids (pd.DataFrame): A DataFrame containined uuids
uuidpath (str): Path to UUID file. The file is expected to contain columns
['studentProjectUuid', {crncol}, {usercol}, {assignmentcol}]
at least. Either this or uuids must be provided.
crnfilter (str): A CRN to filter UUIDs on
crncol (str): Name of the column containing course CRNs
assignmentcol (str): Name of the column containing assignment names. Defaults to
'CASSIGNMENTNAME'. This will get renamed to 'assignment'.
usercol (str): Name of the column containing user information. This will get
renamed to userName, and domains will be removed from emails.
due_dates (list): A list of `pd.Timestamp` values indicating due dates of assignments.
Use these timestamps as a heuristic identifier of which assignment
the events are being generated for. If omitted, the resulting
dataframe will have no **assignment** column.
Returns:
A `pd.DataFrame` containing the result of a left join on sensordata and uuids.
"""
# check required params
if sensordata is None and sdpath is None:
raise ValueError('Either sensordata or sdpath must be provided. Got None for both.')
if uuids is None and uuidpath is None:
raise ValueError('Either uuids or uuidpath must be provided. Got None for both.')
# read sensordata
if sensordata is None:
sensordata = pd.read_csv(sdpath, low_memory=False)
# read uuids
cols = ['userUuid', 'studentProjectUuid', assignmentcol, usercol]
if crnfilter:
cols.append(crncol)
if uuids is None:
uuids = pd.read_csv(uuidpath, usecols=cols)
uuids = (
uuids.rename(columns={usercol: 'userName', assignmentcol: 'assignment'})
.sort_values(by=['userName', 'assignment'], ascending=[1, 1])
)
umap = lambda u: u.split('@')[0] if str(u) != 'nan' and u != '' else u
uuids['userName'] = uuids['userName'].apply(umap)
# filter uuids by crn if provided
if crnfilter:
uuids = uuids[(uuids[crncol].notnull()) & (uuids[crncol].str.contains(crnfilter))]
uuids = uuids.drop(columns=[crncol])
# create user oracle
users = (
uuids.loc[:, ['userUuid', 'userName']]
.drop_duplicates(subset=['userUuid', 'userName'])
.set_index('userUuid')
)
# join
merged = sensordata.join(users, on='userUuid')
merged = merged.query('userName.notnull()')
del sensordata
# create assignment oracle
assignments = (
uuids.loc[:, ['studentProjectUuid', 'assignment']]
.drop_duplicates(subset=['studentProjectUuid', 'assignment'])
.set_index('studentProjectUuid')
)
conflicts = uuids.groupby('studentProjectUuid').apply(lambda g: len(g['assignment'].unique()) > 1)
conflicts = list(conflicts[conflicts].index)
with_conflicts = merged.loc[merged['studentProjectUuid'].isin(conflicts)]
without_conflicts = merged.loc[~merged['studentProjectUuid'].isin(conflicts)]
without_conflicts = without_conflicts.join(assignments, on='studentProjectUuid')
# for uuids with conflicting assignments, map based on timestamps
if due_dates:
with_conflicts.loc[:, 'assignment'] = (
with_conflicts.apply(__assignment_from_timestamp, due_dates=due_dates, axis=1)
)
merged = pd.concat([with_conflicts, without_conflicts], ignore_index=True, sort=False) \
.sort_values(by=['userName', 'assignment', 'time'])
return merged
def __assignment_from_timestamp(event, due_dates, offset=None):
offset = pd.Timedelta(1, 'w') if offset is None else offset
try:
t = pd.to_datetime(event['time'], unit='ms')
except ValueError:
t = pd.Timestamp(event['time'])
for idx, dd in enumerate(due_dates, start=1):
dd = pd.to_datetime(dd, unit='ms')
if t < dd + offset:
return 'Project {}'.format(idx)
return None
def with_edit_sizes(df, groupby=['userName', 'Class-Name'], sizecol='Current-Size'):
"""Given a data frame with Edit events containing Current-Sizes, group by
Class-Name and return the dataframe with a column called 'edit_size', which
contains the size of the edit made to the given file.
"""
edits = df[(~df['Class-Name'].isna()) & (df['Type'] == 'Edit')] \
.groupby(groupby) \
.apply(__get_edit_sizes, sizecol=sizecol)
df.loc[df.index.isin(edits.index), 'edit_size'] = edits['edit_size']
return df
def __get_edit_sizes(df, sizecol):
df['edit_size'] = df[sizecol].diff().abs().fillna(0)
return df