Skip to content

Commit

Permalink
Fix sampled_tracks parsing issue closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
giginet committed Jan 29, 2017
1 parent 6122cd2 commit c9455a2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion beddit/session.py
Expand Up @@ -13,6 +13,7 @@ class SampledTrack(object):
def __init__(self, obj):
self.samples_per_frame = obj['samples_per_frame']
self.data_url = obj.get('data_url', None)
self.data_type = obj['data_type']


class Session(object):
Expand All @@ -27,7 +28,7 @@ def __init__(self, response_object):
self.software = response_object["software"]
self.frame_length = response_object.get("frame_length", None)
self.error_code = response_object.get("error_code", None)
self.sampled_tracks = [SampledTrack(obj) for obj in response_object.get('sampled_tracks', [])]
self.sampled_tracks = {key: SampledTrack(value) for key, value in response_object.get('sampled_tracks', {}).items()}

time_value_tracks = response_object['time_value_tracks']

Expand Down
54 changes: 54 additions & 0 deletions tests/fixtures/session_sample.json
@@ -0,0 +1,54 @@
{
"id" : 1431835725,
"start_timestamp" : 1431835725.618702,
"end_timestamp" : 1431865087.218702,
"timezone" : "America/New_York",
"hardware" : "1.0/39bc22e bl:1.1 hw:1",
"software" : "1.11.0 (101)",
"frame_length" : 0.2,
"error_code" : 0,
"sampled_tracks" : {
"normal" : {
"samples_per_frame" : 28,
"data_url" : "https://bedditcloud-sleepdata...",
"data_type" : "uint16"
},
"noise" : {
"samples_per_frame" : 2,
"data_url" : "https://bedditcloud-sleepdata...",
"data_type" : "float32"
}
},
"time_value_tracks" : {
"actigram" : {
"value_data_type" : "float32",
"items" : [
[0, 29973.82]
]
},
"snoring_events" : {
"value_data_type" : "float32",
"items" : []
},
"respiration_cycles" : {
"value_data_type" : "float32",
"items" : [
[40.3, 4.1]
]
},
"heart_rate" : {
"value_data_type" : "float32",
"items" : [
[60, 64.59961]
]
},
"sensor_status" : {
"value_data_type" : "uint8",
"items" : [
[58.4, 1],
[29316.8, 1]
]
}
},
"updated" : 1371482503.646541
}
15 changes: 14 additions & 1 deletion tests/test_session.py
Expand Up @@ -26,7 +26,7 @@ def test_session(self):
self.assertEqual(session.hardware, raw['hardware'])
self.assertEqual(session.frame_length, raw['frame_length'])
self.assertEqual(session.error_code, raw['error_code'])
self.assertEqual(session.sampled_tracks, [])
self.assertEqual(session.sampled_tracks, {})

self.assertEqual(len(session.respiration_cycle_amplitudes), 7976)
self.assertEqual(len(session.heartbeat), 5963)
Expand All @@ -40,3 +40,16 @@ def test_session(self):
self.assertEqual(len(session.activity_segment_length), 303)
self.assertEqual(len(session.high_activity_intervals), 113)
self.assertEqual(len(session.activity_segment_variation), 303)

def test_sampled_tracks(self):
response = json.load(open(os.path.join(BASE_DIR, 'fixtures/session_sample.json')))
session = Session(response)
tracks = session.sampled_tracks
self.assertEqual(tracks['normal'].samples_per_frame, 28)
self.assertEqual(tracks['normal'].data_url, 'https://bedditcloud-sleepdata...')
self.assertEqual(tracks['normal'].data_type, 'uint16')

self.assertEqual(tracks['noise'].samples_per_frame, 2)
self.assertEqual(tracks['noise'].data_url, 'https://bedditcloud-sleepdata...')
self.assertEqual(tracks['noise'].data_type, 'float32')

0 comments on commit c9455a2

Please sign in to comment.