Create a gist now

Instantly share code, notes, and snippets.

@nvbn /
Last active May 17, 2018

What would you like to do?
import json
from datetime import datetime
from collections import namedtuple
from itertools import groupby, takewhile
from statistics import median, mean
from matplotlib import pyplot
import numpy as np
from geopy.distance import geodesic
Point = namedtuple('Point', 'latitude, longitude, datetime')
def read_points():
with open('data.json') as f:
data = json.load(f)
for point in data['locations']:
yield Point(
point['latitudeE7'] / 10 ** 7,
point['longitudeE7'] / 10 ** 7,
datetime.fromtimestamp(int(point['timestampMs']) / 1000)
home = (52.350879, 4.893817)
work = (52.3657573, 4.8980648)
max_distance = 0.050
from_date = datetime(2017, 11, 1)
from_hour = 7
to_hour = 12
points = read_points()
after_move = takewhile(lambda point: point.datetime >= from_date, points)
work_days = (point for point in after_move
if point.datetime.weekday() < 5)
commute_time = (point for point in work_days
if from_hour <= point.datetime.hour < to_hour)
by_days = groupby(commute_time, key=lambda point:
def last_at_home(points):
result = None
for point in points:
if geodesic(home, point[:2]).km <= max_distance:
result = point
return result
def first_at_work(points, after):
for point in points:
if point.datetime > after.datetime and geodesic(work, point[:2]).km <= max_distance:
return point
Commute = namedtuple('Commute', 'day, start, end, took')
def get_commute():
for day, points in by_days:
points = [*points][::-1]
start = last_at_home(points)
if start is None:
end = first_at_work(points, start)
if end is None:
yield Commute(
day, start.datetime, end.datetime, end.datetime - start.datetime,
commutes = [*get_commute()][::-1]
fig, ax = pyplot.subplots()
ax.plot([ for commute in commutes],
[commute.took.total_seconds() / 60 for commute in commutes])
ax.set(xlabel='day', ylabel='commute (minutes)',
title='Daily commute')
from dateutil.parser import parse
Weather = namedtuple('Weather', 'windspeed, temperature, wind_direction')
def read_weather():
result = {}
with open('weather.txt') as f:
for line in f.readlines():
if not line.startswith(' '):
data = [part.strip() for part in line.split(',')]
result[parse(data[1]).date()] = Weather(
int(data[4]) / 10,
int(data[11]) / 10,
return result
weather = read_weather()
normalized = [commute for commute in commutes
if commute.took.total_seconds() < 60 * 20]
fig, ax = pyplot.subplots()
ax.scatter([commute.took.total_seconds() / 60 for commute in normalized],
[weather[].windspeed for commute in normalized])
ax.set(xlabel='Commute time', ylabel='Wind speed',
title='Commute and wind')
from mpl_toolkits.mplot3d import Axes3D
fig, ax = pyplot.subplots(subplot_kw={'projection': '3d'})
ax.scatter([weather[].temperature for commute in normalized],
[weather[].windspeed for commute in normalized],
[commute.took.total_seconds() / 60 for commute in normalized])
ax.set(xlabel='Wind speed', ylabel='Temperature', zlabel='Commute time',
title='Commute and weather')
from matplotlib import cm
colors = iter(cm.Reds(np.linspace(0, 1, len(normalized))))
fig, ax = pyplot.subplots()
for commute in sorted(normalized, key=lambda commute: commute.took.total_seconds() / 60):
ax.set(xlabel='Wind speed', ylabel='Wind direction',
title='Commute and wind')
from matplotlib import cm
colors = iter(cm.Reds(np.linspace(0, 1, len(normalized))))
fig, ax = pyplot.subplots(subplot_kw={'projection': 'polar'})
for commute in sorted(normalized, key=lambda commute: commute.took.total_seconds() / 60):
ax.set(title='Commute and wind')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment