Created
August 25, 2016 15:25
-
-
Save jarek/23d059d693edbb197366fc6b0a053d76 to your computer and use it in GitHub Desktop.
parking and trip charting/plotting code 2016-08-25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# coding=utf-8 | |
from __future__ import print_function | |
import sys | |
import simplejson as json | |
import datetime | |
import numpy as np | |
import matplotlib.pyplot as plt | |
sys.path.insert(0, '/home/jarek/projects/electric2go') | |
from electric2go.systems.car2go import city | |
from electric2go.analysis import graph | |
def json_deserializer(obj): | |
# parse datetimes from JSON we wrote | |
for (key, value) in obj.items(): | |
if isinstance(value, basestring) and key.endswith('time'): | |
try: | |
# this is the format that isoformat outputs | |
obj[key] = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S") | |
except: | |
pass | |
return obj | |
def adjust_tz(t, tz): | |
return (t - tz) if t > tz else (t + 24 - tz) | |
def get_time(t, tz): | |
hours_plus_minutes = t.hour + (t.minute/60.0) | |
return adjust_tz(hours_plus_minutes, tz) | |
result_dict = json.load(fp=sys.stdin, object_hook=json_deserializer) | |
system = result_dict['metadata']['system'] | |
tz = 2 # Berlin in summertime | |
all_parkings = [parking for vin in result_dict['finished_parkings'] for parking in result_dict['finished_parkings'][vin] | |
if parking['starting_time'] != result_dict['metadata']['starting_time']] | |
# don't include parkings that are detected to have started on the very first data point - likely that they've actually started earlier | |
# and it would be a misleading data point | |
all_trips = [trip for vin in result_dict['finished_trips'] for trip in result_dict['finished_trips'][vin]] | |
def parking_cleaner(parking): | |
return True | |
# or, for instance, use `parking['duration'] < 60*60*24` to only show things lasting less than a day - use when we want to eliminate outliers | |
all_parking_durations = [p['duration']/60 for p in all_parkings if parking_cleaner(p)] | |
all_parking_starting_hour = [get_time(p['starting_time'], tz) for p in all_parkings if parking_cleaner(p)] | |
all_parking_ending_hour = [get_time(p['ending_time'], tz) for p in all_parkings if parking_cleaner(p)] | |
all_parking_fuel = [p['fuel'] for p in all_parkings if parking_cleaner(p)] | |
fig = plt.figure() | |
ax = fig.add_subplot(3,1,1) | |
plt.hist([p for p in all_parking_durations if p < 400], bins=400, histtype='bar') | |
plt.title(system + " parking period duration (minutes, < 400 min)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(3,1,2) | |
plt.hist(all_parking_ending_hour, bins=288, histtype='bar') | |
plt.title(system + " parking ending time of day") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(3,1,3) | |
plt.hist(all_parking_starting_hour, bins=288, histtype='bar') | |
plt.title(system + " parking starting time of day") | |
plt.ylabel("frequency") | |
plt.show() | |
fig = plt.figure() | |
plt.plot(all_parking_fuel, all_parking_durations, '.') | |
plt.title(system + " parking period duration vs reported fuel level") | |
plt.xlabel("reported fuel level (%)") | |
plt.ylabel("parking duration (minutes)") | |
plt.show() | |
fig = plt.figure() | |
plt.plot(all_parking_starting_hour, all_parking_durations, '.') | |
plt.title(system + " parking period duration vs starting time of the parking period") | |
plt.xlabel("time the parking period started (time of day)") | |
plt.ylabel("parking duration (minutes)") | |
plt.show() | |
fig = plt.figure() | |
plt.plot(all_parking_ending_hour, all_parking_durations, '.') | |
plt.title(system + " parking period duration vs ending time of the parking period") | |
plt.xlabel("time the parking period ended (time of day)") | |
plt.ylabel("parking duration (minutes)") | |
plt.show() | |
max_minutes = 60*6 | |
def filterer(trip): | |
return trip['duration'] < 60*max_minutes #and trip['starting_time'].hour == 10 | |
all_trip_lengths = [trip['duration']/60 for trip in all_trips if filterer(trip) ] | |
all_trip_speeds = [trip['speed'] for trip in all_trips if filterer(trip) and trip['speed'] < 50 ] | |
all_trip_distances = [trip['distance'] for trip in all_trips if filterer(trip) ] | |
all_trip_fuels = [trip['fuel_use'] for trip in all_trips if filterer(trip) ]#and trip['fuel_use'] > -20 ] | |
fig = plt.figure() | |
plt.plot(all_trip_lengths, all_trip_distances, '.') | |
plt.title(system + " trip duration vs distance") | |
plt.xlabel("duration (minutes)") | |
plt.ylabel("distance") | |
# also works as: | |
#all_trip_duration_vs_distance = [(trip['duration']/60, trip['distance']) for trip in all_trips if trip['duration'] < 60*max_minutes ] | |
#dur,dist = zip(*all_trip_duration_vs_distance) | |
plt.show() | |
fig = plt.figure() | |
plt.plot(all_trip_lengths, all_trip_fuels, '.') | |
plt.title(system + " trip duration vs fuel use") | |
plt.xlabel("duration (minutes)") | |
plt.ylabel("fuel use") | |
plt.show() | |
fig = plt.figure() | |
ax = fig.add_subplot(4,1,1) | |
plt.hist(all_trip_lengths, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " trip durations (minutes)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,2) | |
plt.hist(all_trip_distances, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " trip distances (km)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,3) | |
plt.hist(all_trip_speeds, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " trip speeds (km/h)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,4) | |
plt.hist(all_trip_fuels, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " fuel usage (%)") | |
plt.ylabel("frequency") | |
plt.show() | |
def non_zero_distance_filterer(trip): | |
return filterer(trip) and trip['distance'] > 0 | |
filtered_trip_lengths = [trip['duration']/60 for trip in all_trips if non_zero_distance_filterer(trip) ] | |
filtered_trip_speeds = [trip['speed'] for trip in all_trips if non_zero_distance_filterer(trip) and trip['speed'] < 50 ] | |
filtered_trip_distances = [trip['distance'] for trip in all_trips if non_zero_distance_filterer(trip) ] | |
filtered_trip_fuels = [trip['fuel_use'] for trip in all_trips if non_zero_distance_filterer(trip) and trip['fuel_use'] > -20 ] | |
fig = plt.figure() | |
ax = fig.add_subplot(4,1,1) | |
plt.hist(filtered_trip_lengths, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " non-zero distance trip durations (minutes)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,2) | |
plt.hist(filtered_trip_speeds, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " non-zero trip distances (km)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,3) | |
plt.hist(filtered_trip_distances, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " non-zero trip speeds (km/h)") | |
plt.ylabel("frequency") | |
ax = fig.add_subplot(4,1,4) | |
plt.hist(filtered_trip_fuels, bins=max_minutes-1, histtype='bar') | |
plt.title(system + " non-zero distance fuel usage (%)") | |
plt.ylabel("frequency") | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment