Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
Prototype binning
Browse files Browse the repository at this point in the history
Doesn't yet deal with the idea that anomolies will be different in
different directions
  • Loading branch information
danni committed Dec 26, 2012
1 parent a5872b2 commit 6c90ab0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
64 changes: 64 additions & 0 deletions binning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Danielle Madeley <danielle@madeley.id.au>

import numpy as np

class Grid(np.ndarray):

@classmethod
def calculate_bounds(cls, tracks):
bounds = np.rec.array([(min(t.lat), max(t.lat), min(t.lon), max(t.lon))
for t in tracks],
names=('minlat', 'maxlat', 'minlon', 'maxlon'))

return min(bounds.minlat), max(bounds.maxlat), \
min(bounds.minlon), max(bounds.maxlon)

def __new__(cls, tracks, xnum=50, ynum=50):

minlat, maxlat, minlon, maxlon = cls.calculate_bounds(tracks)

# initialise ourselves
self = np.zeros((xnum, ynum)).view(Grid)

self._elems_total = np.zeros((xnum, ynum))
self._nelems = np.zeros((xnum, ynum), dtype=int)

self.x = np.linspace(minlon, maxlon, num=xnum)
self.y = np.linspace(minlat, maxlat, num=ynum)

for track in tracks:
self.add_track(track, recalculate=False)

self._recalculate()

return self

def add_track(self, track, recalculate=True):
vels = track.calculate_vels()
xbins = np.digitize(vels.lon, self.x) - 1
ybins = np.digitize(vels.lat, self.y) - 1

for (x, y, anom) in zip(xbins, ybins, vels.anom):
self._elems_total[x, y] += anom
self._nelems[x, y] += 1

if recalculate:
self._recalculate()

def _recalculate(self):
valid = self._nelems.nonzero()

self[valid] = self._elems_total[valid] / self._nelems[valid]
52 changes: 52 additions & 0 deletions tests/test_binning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from glob import glob

from matplotlib import pyplot as plt

from track import RKJSON, BadInputException
from binning import Grid

def test_binning_one():
filename = glob('tracks/*.json')[0]

with open(filename) as f:
track = RKJSON(f)
grid = Grid([track])

assert grid.x.shape == (50,)
assert grid.y.shape == (50,)
assert grid.shape == (50, 50)

fig, ax = plt.subplots(1)

cs = ax.pcolor(grid.x, grid.y, grid,
cmap=plt.get_cmap('RdBu_r'))
cs.set_clim(-1, 1)

fig.colorbar(cs)

plt.show()

def test_binning_all():

def generate_tracks():
for fn in glob('tracks/*.json'):
with open(fn) as f:
try:
yield RKJSON(f)
except BadInputException:
pass

grid = Grid(list(generate_tracks()))

assert grid.x.shape == (50,)
assert grid.y.shape == (50,)
assert grid.shape == (50, 50)

fig, ax = plt.subplots(1)
ax.set_title("Speed Anomaly")
cs = ax.contourf(grid.x, grid.y, grid, cmap=plt.get_cmap('RdBu_r'))
cs.set_clim(-1, 1)
ax.contour(grid.x, grid.y, grid)
fig.colorbar(cs)

plt.show()
2 changes: 1 addition & 1 deletion track.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def calculate_vels(self, smooth_vels=False):

# calculate the velocities
# remove any nans by making them zero
vels = np.nan_to_num(dist / times) * 3.6 # m/s to km/h
vels = (dist / times) * 3.6 # m/s to km/h

# the idea here is to generate a long term average for the cycle
# which we can consider the cyclists quiescent speed for the journey -
Expand Down

0 comments on commit 6c90ab0

Please sign in to comment.