Skip to content

Instantly share code, notes, and snippets.

@cbare
Created November 15, 2015 23:19
Show Gist options
  • Select an option

  • Save cbare/48de7da55b4683793b12 to your computer and use it in GitHub Desktop.

Select an option

Save cbare/48de7da55b4683793b12 to your computer and use it in GitHub Desktop.
import json
import pandas as pd
import matplotlib.pyplot as plt
import synapseclient
from collections import OrderedDict
syn = synapseclient.Synapse()
syn.login()
results = syn.tableQuery('SELECT count(*) FROM syn5299184')
print "Total number of records:", results.asInteger()
## Get a limited number of records
results = syn.tableQuery('SELECT * FROM syn5299184 LIMIT 20')
file_map = syn.downloadTableColumns(results, ["deviceMotion_walking_outbound.json.items"])
## if we download all the data, we'll see that one is missing. This shows us why:
results = syn.tableQuery('SELECT * FROM syn5299184 where "deviceMotion_walking_outbound.json.items" IS NULL')
for result in results:
print result
## load the json files
data = OrderedDict()
for file_handle, path in file_map.iteritems():
with open(path) as f:
data[file_handle] = json.loads(f.read())
## Each json file is a list of maps whose keys are:
## * userAcceleration
## * timestamp
## * magneticField
## * gravity
## * rotationRate
## * attitude
## data[data.keys()[0]][0].keys()
## Here are the data types
names = ['attitude',
'userAcceleration',
'rotationRate',
'gravity',
'magneticField']
## build dataframes for all data types except timestamp, which
## becomes the index for the others.
records = OrderedDict()
for file_handle, rs in data.iteritems():
series = OrderedDict()
timestamps = [r['timestamp'] for r in rs]
t0 = min(timestamps)
timestamps = [t-t0 for t in timestamps]
for name in names:
series[name] = pd.DataFrame([r[name] for r in rs], index=timestamps)
records[file_handle] = series
## graph the gravity data type for one record
g = records[records.keys()[0]]['gravity']
plt.plot(g.index, g.x, 'r', g.index, g.y, 'g', g.index, g.z, 'b')
plt.show()
## graph the gravity data type for a sequence of records
for i,key in enumerate(records.keys()):
g = records[key]['gravity']
plt.plot(g.index, g.x, 'r', g.index, g.y, 'g', g.index, g.z, 'b')
plt.show()
if i > 10: break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment