In [3]:
# Numpy & Matplotlib
%pylab inline
In [4]:
import wepy.configuration as cf # our tailor-made configuration file
import json # to read json files
import os # to access filesystem
import re # regular expression
from pprint import pprint # a nice print
import dotmap # to transform dictionaries into dynamic structures (same storage but accessible through . instead of [])
In [7]:
path_trips = cf.PATHS.trips
# Now find the files with answers
list_files = os.listdir(path_trips) # lists files in the data folder
r = re.compile('response.txt') # prepares a regular expression to match files with the indicated pattern
list_files = list(filter(r.search, list_files)) # applies the filter to the files and tranforms the iterator into a list
len(list_files)
Out[7]:
In [8]:
# loads a particular file
data_file = open(path_trips + list_files[0])
# this builds a dictionnary from the json file
data_answer = json.load(data_file)
data_sent = json.load(open(path_trips + str.replace(list_files[0], 'response', 'sent')))
# dotmap transform dictionaries into structure (easier completion)
simple_answer = dotmap.DotMap(data_answer)
simple_sent = dotmap.DotMap(data_sent)
In [12]:
import pandas as pd # dataframes
from tqdm import tqdm as wb # waitbar
n_files = len(list_files)
# create a list of all the file content parsed as dotmap dictionaries
data_store = [None] * n_files # an empty list of size n
for i in wb(range(n_files)):
current_file_name = path_trips + list_files[i]
# departing from the answers ensures that more files shall be OK
data_sent = dotmap.DotMap(json.load(open(str.replace(current_file_name, 'response', 'sent'))))
data_answer = dotmap.DotMap(json.load(open(current_file_name)))
data_store[i] = (data_sent, data_answer)
In [13]:
# a first way to print the content
pprint(data_answer)
# now data is a regular dictionary accessible like this
print(data_answer['fuelEstimation']['co2Emission'])
# the somewhat simpler dotmap access:
print(simple_answer.fuelEstimation.co2Emission)
print(simple_answer.fuelEstimation.fuelVolume)
print(simple_sent.keys())
print(simple_sent.vehicle)
In [14]:
# Plotly
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=False)
In [75]:
current_route = data_store[math.floor(rand()*n_files)][0].route
print(current_route.keys())
data_comp = [
go.Scatter(x = current_route.gpsDate, y = current_route.vehVelocity, name = "velocity", line=dict(dash='dash')),
go.Scatter(x = current_route.gpsDate, y = np.array(current_route.vehEngineSpeed)/100., name = "engine speed", mode='lines'),
]
# go.Scatter(x = current_route.gpsDate, y = current_route.data, name = "data", line = dict(dash='dash')),
layout_comp = go.Layout(
title='A sample route',
xaxis = dict(title='date'), yaxis = dict(title='speed'),
autosize = False, width = 800, height = 400)
iplot(go.Figure(data = data_comp, layout = layout_comp))
In [90]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.ion() # for a non blocking show()
current_route = data_store[math.floor(rand()*n_files)][0].route
plt.plot(current_route.gpsDate, current_route.vehVelocity)
Out[90]:
In [120]:
from bokeh.plotting import figure, output_notebook, show
# output to static HTML file
# output_file("lines.html")
output_notebook()
current_route = data_store[math.floor(rand()*n_files)][0].route
# create a new plot with a title and axis labels
p = figure(title="velocity function of gps date", x_axis_label='time', y_axis_label='speed')
# add a line renderer with legend and line thickness
p.line(current_route.gpsDate, current_route.vehVelocity, legend="velocity", line_width=2)
p.plot_height = 1000
p.plot_height = 400
# show the results
show(p)
In [ ]:
In [ ]: