-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataparser.py
More file actions
69 lines (54 loc) · 2.28 KB
/
dataparser.py
File metadata and controls
69 lines (54 loc) · 2.28 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
__author__ = 'alexander'
from sample import Sample
class Session:
def __init__(self):
self.samples = [ ]
class DataParser:
def __init__(self):
# Sessions is a map: activityCode -> List<Session>
self.sessions = { }
# Parse a single data file.
# Parsing proceeds as follows:
# Iterate over the rows. Skip rows until a valid activityID is found.
# session <- new empty list of samples.
# with activityId:
# Skip until valid HR.
# Consume row, parse as new sample. Add to list.
# Go to next row and repeat
#
#
# Returns a set< (classification, list<sample>) >
def parseFile(self, path):
with open(path, 'rU') as f:
currentActivity = None
currentHr = None
currentSession = None
for line in f:
tokens = line.split()
assert len(tokens) == 54
# Skip the line if no current session and cannot adopt activity from row
if (currentSession == None and not tokens[1].isdigit()):
continue
# Check if activity has changed
if currentActivity != int(tokens[1]):
print "Parsing new activity: " + tokens[1]
currentActivity = int(tokens[1])
currentSession = self.startNewSession(currentActivity)
currentHr = None
# Skip the line if no current heart rate and cannot adopt hr from row
if currentHr == None and (tokens[2] == "NaN" or not tokens[2].isdigit()):
continue
# Adopt the current heart rate
if tokens[2].isdigit(): currentHr = int(tokens[2])
try:
nextSample = Sample(tokens, currentHr)
currentSession.samples.append(nextSample)
except Exception:
print "Failed to parse sample. Igoring"
# Create and return a new session
def startNewSession(self, activityCode):
newSession = Session()
if not self.sessions.has_key(activityCode):
self.sessions[activityCode] = [ ]
self.sessions[activityCode].append(newSession)
return newSession