Sample file to import and display json files content


In [3]:
# Numpy & Matplotlib
%pylab inline


Populating the interactive namespace from numpy and matplotlib

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 [])

File location is in our config file


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]:
15302

Take the first available file


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)

import all files in a pandas dataframe for study


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)


100%|██████████| 15302/15302 [00:24<00:00, 620.44it/s]

Explore content


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)


{'account': {'account': 'wenow',
             'deviceId': '1',
             'password': 'pwd',
             'requestType': 8,
             'userId': 'wenow.1@gmail.com'},
 'advancedEcoDriving': None,
 'advancedFuelEstimation': None,
 'advancedSafety': None,
 'comment': 'OK',
 'comments': [DotMap(errorCode=0, comment='OK')],
 'ecoDriving': {'energyClass': 1,
                'score': 9.2,
                'scoreAccel': -1.9,
                'scoreDecel': -0.7,
                'scoreMain': 0.4,
                'stdDevAccel': 3.2799246,
                'stdDevDecel': 3.0402534,
                'stdDevMain': 0.832029},
 'errorCode': 0,
 'fuelEstimation': {'co2Emission': 102.0,
                    'co2Mass': 4.384,
                    'coldFuelVolume': 0.014,
                    'engineTempStatus': True,
                    'fuelConsumption': 3.81,
                    'fuelVolume': 1.632,
                    'idleCo2Emission': 0.948,
                    'idleCo2Mass': 0.0,
                    'idleFuelConsumption': 0.353,
                    'idleFuelPercentage': 0.12,
                    'idleFuelVolume': 0.002},
 'itinID': '58d3a354b3cb1f0007e5ad94',
 'itineraryStatistics': {'distance': 42791.0,
                         'drivingDuration': 2035.0,
                         'drivingPercentage': 99.0,
                         'idlingDuration': 18.0,
                         'idlingPercentage': 1.0,
                         'speedMean': 75.0,
                         'subdispNb': 15,
                         'tripDuration': 2053.0},
 'safety': None,
 'status': True}
102.0
161.0
0.968
odict_keys(['account', 'vehicle', 'route'])
DotMap(carTypeIndex=1, carEngineIndex=1, carPower=110, carMass=1400, carGearboxIndex=2, carConsumption='4.80')

Some graphs


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))


odict_keys(['vehVelocity', 'gpsDate', 'vehEngineSpeed'])

The usual matplotlib


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]:
[<matplotlib.lines.Line2D at 0x158eb5080>]

last bokeh


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)


Loading BokehJS ...

In [ ]:


In [ ]: