In [1]:
import bson.json_util as bju
import json

In [2]:
%matplotlib inline

In [3]:
import pandas as pd
import numpy as np
import matplotlib.pylab as pl

In [4]:
n = 10
x = pd.date_range(start='2015-07-13 9:00:00', freq='S', periods=n)
y = np.vstack([range(n), range(n+1,1+2*n), range(1,2*n+1,2)]).T
df = pd.DataFrame(y, index=x)

In [5]:
df.plot()


Out[5]:
<matplotlib.axes.AxesSubplot at 0x1059688d0>

In [6]:
def PandasResample(df, length):
    td = (df.index[-1] - df.index[0]) / (length-1)
    return df.resample(td, how='mean').interpolate() # Handle NaNs when upsampling

In [7]:
df


Out[7]:
0 1 2
2015-07-13 09:00:00 0 11 1
2015-07-13 09:00:01 1 12 3
2015-07-13 09:00:02 2 13 5
2015-07-13 09:00:03 3 14 7
2015-07-13 09:00:04 4 15 9
2015-07-13 09:00:05 5 16 11
2015-07-13 09:00:06 6 17 13
2015-07-13 09:00:07 7 18 15
2015-07-13 09:00:08 8 19 17
2015-07-13 09:00:09 9 20 19

In [8]:
df.resample('500ms', fill_method='bfill')


Out[8]:
0 1 2
2015-07-13 09:00:00.000 0 11 1
2015-07-13 09:00:00.500 1 12 3
2015-07-13 09:00:01.000 1 12 3
2015-07-13 09:00:01.500 2 13 5
2015-07-13 09:00:02.000 2 13 5
2015-07-13 09:00:02.500 3 14 7
2015-07-13 09:00:03.000 3 14 7
2015-07-13 09:00:03.500 4 15 9
2015-07-13 09:00:04.000 4 15 9
2015-07-13 09:00:04.500 5 16 11
2015-07-13 09:00:05.000 5 16 11
2015-07-13 09:00:05.500 6 17 13
2015-07-13 09:00:06.000 6 17 13
2015-07-13 09:00:06.500 7 18 15
2015-07-13 09:00:07.000 7 18 15
2015-07-13 09:00:07.500 8 19 17
2015-07-13 09:00:08.000 8 19 17
2015-07-13 09:00:08.500 9 20 19
2015-07-13 09:00:09.000 9 20 19

In [9]:
new_index = pd.date_range(start=df.index[0], end=df.index[-1], freq='500ms')

In [10]:
df.reindex(new_index).interpolate().plot()


Out[10]:
<matplotlib.axes.AxesSubplot at 0x108bf03d0>

In [11]:
entries_oct_20 = json.load(open("power_drain/data/location/shankari_2015-10-20"), object_hook=bju.object_hook)
entries_oct_21 = json.load(open("power_drain/data/location/shankari_2015-10-21"), object_hook=bju.object_hook)
entries = []
entries.extend(entries_oct_20)
entries.extend(entries_oct_21)

In [12]:
locations = [entry for entry in entries if entry["metadata"]["key"] == "background/location"]

In [13]:
def expand_vals(location):
    ret_dict = {}
    ret_dict.update(location["metadata"])
    ret_dict.update(location["data"])
    return ret_dict

In [14]:
locations_df = pd.DataFrame([expand_vals(loc) for loc in locations]).set_index("local_dt")

In [15]:
locations_df.head()


Out[15]:
accuracy altitude elapsedRealtimeNanos filter fmt_time heading key latitude loc longitude platform read_ts sensed_speed time_zone ts type write_fmt_time write_local_dt write_ts
local_dt
2015-10-20 08:16:10+00:00 67.5 0 866237042000000 time 2015-10-20T08:16:10-07:00 0 background/location 37.392983 {u'type': u'Point', u'coordinates': [-122.0847... -122.084706 android 1.445357e+09 0 America/Los_Angeles 1445354170 sensor-data 2015-10-20T08:16:14.022000-07:00 2015-10-20 08:16:14.022000+00:00 1.445354e+09
2015-10-20 08:16:40+00:00 48.0 0 866267089000000 time 2015-10-20T08:16:40-07:00 0 background/location 37.393059 {u'type': u'Point', u'coordinates': [-122.0849... -122.084977 android 0.000000e+00 0 America/Los_Angeles 1445354200 sensor-data 2015-10-20T08:16:40.422000-07:00 2015-10-20 08:16:40.422000+00:00 1.445354e+09
2015-10-20 08:17:10+00:00 45.0 0 866297195000000 time 2015-10-20T08:17:10-07:00 0 background/location 37.393240 {u'type': u'Point', u'coordinates': [-122.0848... -122.084846 android 0.000000e+00 0 America/Los_Angeles 1445354230 sensor-data 2015-10-20T08:17:10.534000-07:00 2015-10-20 08:17:10.534000+00:00 1.445354e+09
2015-10-20 08:17:40+00:00 64.5 0 866327199000000 time 2015-10-20T08:17:40-07:00 0 background/location 37.394942 {u'type': u'Point', u'coordinates': [-122.0836... -122.083605 android 0.000000e+00 0 America/Los_Angeles 1445354260 sensor-data 2015-10-20T08:17:40.514000-07:00 2015-10-20 08:17:40.514000+00:00 1.445354e+09
2015-10-20 08:18:10+00:00 40.5 0 866357266000000 time 2015-10-20T08:18:10-07:00 0 background/location 37.396167 {u'type': u'Point', u'coordinates': [-122.0827... -122.082705 android 0.000000e+00 0 America/Los_Angeles 1445354290 sensor-data 2015-10-20T08:18:12.777000-07:00 2015-10-20 08:18:12.777000+00:00 1.445354e+09

In [16]:
locations_df.latitude.plot()


Out[16]:
<matplotlib.axes.AxesSubplot at 0x10dafd4d0>

In [41]:
import datetime as pydt
import pytz

In [62]:
to_local_dt = lambda(ts): pydt.datetime.fromtimestamp(ts).replace(tzinfo=locations_df.index[0].tz)

In [63]:
new_index_oct20_first = pd.date_range(start=to_local_dt(1445349600), end=to_local_dt(1445371200), freq='1S')

In [64]:
new_index_oct20_second = pd.date_range(start=to_local_dt(1445373000), end=to_local_dt(1445394600), freq='1S')

In [65]:
len(locations_df.index.unique())


Out[65]:
7207

In [66]:
len(locations_df)


Out[66]:
7207

In [67]:
val_counts = locations_df.index.value_counts()

In [68]:
val_counts


Out[68]:
2015-10-21 16:19:28+00:00    1
2015-10-20 22:48:47+00:00    1
2015-10-21 01:49:42+00:00    1
2015-10-20 15:38:37+00:00    1
2015-10-21 02:39:18+00:00    1
2015-10-21 00:12:56+00:00    1
2015-10-20 19:20:04+00:00    1
2015-10-21 08:46:31+00:00    1
2015-10-20 08:20:35+00:00    1
2015-10-20 21:47:14+00:00    1
2015-10-21 02:40:54+00:00    1
2015-10-21 00:14:32+00:00    1
2015-10-20 23:01:35+00:00    1
2015-10-20 20:35:29+00:00    1
2015-10-21 01:28:53+00:00    1
2015-10-21 01:18:26+00:00    1
2015-10-20 08:23:15+00:00    1
2015-10-20 21:49:54+00:00    1
2015-10-20 08:23:47+00:00    1
2015-10-20 21:54:36+00:00    1
2015-10-20 21:51:30+00:00    1
2015-10-21 01:31:33+00:00    1
2015-10-21 02:45:10+00:00    1
2015-10-20 21:52:34+00:00    1
2015-10-21 00:39:03+00:00    1
2015-10-20 18:12:59+00:00    1
2015-10-20 21:53:06+00:00    1
2015-10-21 01:24:37+00:00    1
2015-10-20 20:31:13+00:00    1
2015-10-20 22:57:19+00:00    1
                            ..
2015-10-20 22:40:36+00:00    1
2015-10-20 22:32:04+00:00    1
2015-10-20 14:22:35+00:00    1
2015-10-20 22:20:52+00:00    1
2015-10-21 02:05:17+00:00    1
2015-10-20 21:54:21+00:00    1
2015-10-21 00:48:30+00:00    1
2015-10-21 17:56:38+00:00    1
2015-10-20 22:22:28+00:00    1
2015-10-20 21:09:37+00:00    1
2015-10-21 19:09:15+00:00    1
2015-10-21 03:16:32+00:00    1
2015-10-20 18:44:31+00:00    1
2015-10-21 00:51:10+00:00    1
2015-10-20 23:38:19+00:00    1
2015-10-20 22:25:08+00:00    1
2015-10-21 03:19:12+00:00    1
2015-10-20 21:21:06+00:00    1
2015-10-20 21:24:27+00:00    1
2015-10-20 21:13:53+00:00    1
2015-10-21 03:20:48+00:00    1
2015-10-20 22:27:48+00:00    1
2015-10-20 15:08:06+00:00    1
2015-10-21 08:35:02+00:00    1
2015-10-20 21:15:29+00:00    1
2015-10-20 23:33:58+00:00    1
2015-10-20 23:42:35+00:00    1
2015-10-21 02:09:33+00:00    1
2015-10-21 03:23:28+00:00    1
2015-10-21 02:53:21+00:00    1
Name: local_dt, dtype: int64

In [69]:
locations_df = locations_df.drop_duplicates(['ts'])

In [70]:
locations_df.head()


Out[70]:
accuracy altitude elapsedRealtimeNanos filter fmt_time heading key latitude loc longitude platform read_ts sensed_speed time_zone ts type write_fmt_time write_local_dt write_ts
local_dt
2015-10-20 08:16:10+00:00 67.5 0 866237042000000 time 2015-10-20T08:16:10-07:00 0 background/location 37.392983 {u'type': u'Point', u'coordinates': [-122.0847... -122.084706 android 1.445357e+09 0 America/Los_Angeles 1445354170 sensor-data 2015-10-20T08:16:14.022000-07:00 2015-10-20 08:16:14.022000+00:00 1.445354e+09
2015-10-20 08:16:40+00:00 48.0 0 866267089000000 time 2015-10-20T08:16:40-07:00 0 background/location 37.393059 {u'type': u'Point', u'coordinates': [-122.0849... -122.084977 android 0.000000e+00 0 America/Los_Angeles 1445354200 sensor-data 2015-10-20T08:16:40.422000-07:00 2015-10-20 08:16:40.422000+00:00 1.445354e+09
2015-10-20 08:17:10+00:00 45.0 0 866297195000000 time 2015-10-20T08:17:10-07:00 0 background/location 37.393240 {u'type': u'Point', u'coordinates': [-122.0848... -122.084846 android 0.000000e+00 0 America/Los_Angeles 1445354230 sensor-data 2015-10-20T08:17:10.534000-07:00 2015-10-20 08:17:10.534000+00:00 1.445354e+09
2015-10-20 08:17:40+00:00 64.5 0 866327199000000 time 2015-10-20T08:17:40-07:00 0 background/location 37.394942 {u'type': u'Point', u'coordinates': [-122.0836... -122.083605 android 0.000000e+00 0 America/Los_Angeles 1445354260 sensor-data 2015-10-20T08:17:40.514000-07:00 2015-10-20 08:17:40.514000+00:00 1.445354e+09
2015-10-20 08:18:10+00:00 40.5 0 866357266000000 time 2015-10-20T08:18:10-07:00 0 background/location 37.396167 {u'type': u'Point', u'coordinates': [-122.0827... -122.082705 android 0.000000e+00 0 America/Los_Angeles 1445354290 sensor-data 2015-10-20T08:18:12.777000-07:00 2015-10-20 08:18:12.777000+00:00 1.445354e+09

In [89]:
first_dc = locations_df.reindex(new_index_oct20_first, method='nearest').interpolate().reset_index()

In [86]:
second_dc = locations_df.reindex(new_index_oct20_second, method='nearest').interpolate().reset_index()

In [94]:
(first_dc.latitude - second_dc.latitude).plot()


Out[94]:
<matplotlib.axes.AxesSubplot at 0x111c45e90>

In [ ]: