In [1]:
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style('white')
In [2]:
from hybridpy.learning import dynamicprogramming, ensemblepredictor
from hybridpy.dataset import triploader
import osmapping
import glob
import math
import numpy as np
In [3]:
dname = '/Users/astyler/projects/ChargeCarData/illah/'
trips = []
fnames = glob.glob(dname+'*.csv')
for fname in fnames:
trips.append(triploader.load(fname))
len(trips)
Out[3]:
In [4]:
buffr = 0.01
mins=[(min(trip.Longitude) -buffr,min(trip.Latitude)-buffr) for trip in trips]
maxs=[(max(trip.Longitude) + buffr,max(trip.Latitude)+buffr) for trip in trips]
ll = map(min,zip(*mins))
ur = map(max,zip(*maxs))
print ll
print ur
In [5]:
ll = [-80.0, 40.40]
ur = [-79.87, 40.48]
mymap = osmapping.MLMap(ll,ur)
In [6]:
for trip in trips:
trip['x'], trip['y'] = mymap.convert_coordinates(trip[['Longitude','Latitude']].values).T
In [7]:
mymap.load_shape_file('./shapefiles/pittsburgh/line.shp')
mymap.load_shape_file('./shapefiles/pittsburgh/polygon.shp')
In [8]:
mymap.clear_selected_shapes()
road = {'edgecolor':'white','lw':3, 'facecolor':'none','zorder':6};
mymap.select_shape('highway','motorway',**road)
mymap.select_shape('highway','trunk',**road)
mymap.select_shape('highway','primary',**road)
mymap.select_shape('highway','secondary',**road)
mymap.select_shape('highway','tertiary',**road)
mymap.select_shape('highway','residential',**road)
mymap.select_shape('leisure','park',facecolor='#BBDDBB',edgecolor='none',zorder=4)
mymap.select_shape('waterway','riverbank',facecolor='#CCCCEE', edgecolor='none', zorder=5)
mymap.select_shape('natural','water',facecolor='#CCCCEE', edgecolor='none', zorder=5)
In [9]:
training = trips[0:4]+trips[5:]
test = trips[4]
In [10]:
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111)
mymap.draw_map(ax, map_fill='#eeeeee')
for (idx,trip) in enumerate(training):
ax.plot(trip.x, trip.y, lw=2, alpha=0.2, c='red', zorder=99)
ax.plot(test.x, test.y, lw=2, alpha=1, c='blue', zorder=100)
Out[10]:
In [11]:
test.head()
Out[11]:
In [32]:
features = ['Latitude', 'Longitude', 'HeadingCosF', 'HeadingSinF', 'SpeedFilt', 'Acceleration', 'Power', 'TotalEnergyUsed']
feature_weights = [1,1,1,1,0.1,0.1,0,2]
ep = ensemblepredictor.EnsemblePredictor(training, features=features, feature_weights=feature_weights)
In [33]:
results[10]
Out[33]:
In [46]:
pv = [r[1] for r in results]
In [53]:
max(np.array(pv)/sum(pv))
Out[53]:
In [43]:
np.linalg.norm([1,1,1,1,1,1,1])
Out[43]:
In [55]:
index = 300
results = ep.predict(test.iloc[index], 1e-2)
#best_weight = max(results)[0]
#results = [(w/best_weight, i) for (w,i) in results]
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(24,12))
gs = gridspec.GridSpec(1, 2, width_ratios=[10,7], height_ratios=[10,7])
gs.update(wspace=-0.15)
ax = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
mymap.draw_map(ax, map_fill='#eeeeee')
normalizer = sum([r[1] for r in results])
for (r, trip) in zip(results, training):
idx = r[2]
alph = r[1]/normalizer
ax.plot(trip.x.iloc[idx:], trip.y.iloc[idx:], lw=2, alpha=alph, c='red', zorder=99)
ax.plot(trip.x.iloc[idx], trip.y.iloc[idx], 'gD', alpha=alph, markersize=3, zorder=101)
ax2.plot(trip.TotalEnergyUsed.iloc[idx:], lw=2, alpha=alph, c='red')
ax.plot(test.x.iloc[index], test.y.iloc[index], 'bD', markersize=5, zorder=100)
gtEnergy = test.TotalEnergyUsed.iloc[index:]
ax2.plot(gtEnergy, lw=2, alpha=1, c='blue')
ax2.axis(xmin=0, xmax=2*len(test.TotalEnergyUsed), ymin=0, ymax=2*gtEnergy.values[-1])
ax2.set_ymargin(0.5)
In [66]:
gtEnergy.values[-1]
Out[66]:
In [22]:
results[0:10]
Out[22]:
In [108]:
In [24]:
ep = ensemblepredictor.EnsemblePredictor(training, features=features, feature_weights=feature_weights)
fig = plt.figure(figsize=(24,12))
gs = gridspec.GridSpec(1, 2, width_ratios=[10,7], height_ratios=[10,7])
gs.update(wspace=-0.15)
ax = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
ax2.set_xlim(0, 2*len(test.TotalEnergyUsed))
ax2.set_ylim(0, 2*test.TotalEnergyUsed.values[-1])
mymap.draw_map(ax, map_fill='#eeeeee')
weights = []
for index in range(5,len(test),5):
results = ep.predict(test.iloc[index], sigma=1e-2)
#results = [(norm(r[0], 1e-4), r[1]) for r in ep.predict(test.iloc[index])]
#results = [(1.0/r[0], r[1]) for r in ep.predict(test.iloc[index])]
#best_match = max(results)
#results = [(w/best_match[0], i) for (w,i) in results]
lines = []
for (r, trip) in zip(results, training):
idx = r[2]
lines.append(ax.plot(trip.x.iloc[idx:], trip.y.iloc[idx:], lw=2, alpha=r[1], c='red', zorder=99)[0])
energy = trip.TotalEnergyUsed.iloc[idx:]
energy = energy - energy.values[0]
lines.append(ax2.plot(energy, lw=2, alpha=r[1], c='red')[0])
pt = ax.plot(test.x.iloc[index], test.y.iloc[index], 'bD', markersize=5, zorder=101)[0]
gtEnergy = test.TotalEnergyUsed.iloc[index:]
gtEnergy = gtEnergy - gtEnergy.values[0]
line = ax2.plot(gtEnergy, lw=2, alpha=1, c='blue')
lines.append(line[0])
plt.savefig('video/ne_frame'+format(index/5, '03')+'.png')
for line in lines:
line.remove()
del line
pt.remove()
In [ ]: