Skip to content
Browse files

model is working now, stoked

  • Loading branch information...
1 parent 78bc89b commit ff050c32d8cb083e43b3e9a21b29edba781c74c6 @chelm committed
Showing with 48 additions and 44 deletions.
  1. +2 −0 README.md
  2. BIN all.tsv.gz
  3. +36 −36 model.py
  4. +10 −8 task1_2.py
View
2 README.md
@@ -20,6 +20,8 @@ One thing to note is the structure of the nd-array. Its a 1000 x 1000 x 7 grid o
3. A grid based solution works, but a node graph solution would probably be better. Id like to experiment with using a graph database as the adaptation model, would be interesting for sure.
+4. The training is slow, the time diff fn is to blame for this. If I create a time diff that was avoided using the time_delta fn in datetime itd be faster.
+
### Running:
> python task1_2.py
View
BIN all.tsv.gz
Binary file not shown.
View
72 model.py
@@ -1,13 +1,12 @@
import numpy as np
import math
-from datetime import datetime
-from datetime import date
+from datetime import datetime, date
from PIL import Image
import os.path
class Model:
- def __init__(self, bbox, matrix_file=None, frames=None):
+ def __init__(self, bbox, width=None, height=None, matrix_file=None, frames=None):
""" A simple matrix based prediction grid
a time weighted grid for determining travel times
@@ -25,10 +24,12 @@ def __init__(self, bbox, matrix_file=None, frames=None):
self.frames = self.matrix.shape[2]
else:
- self.width = 1000
- self.height = 1000
+ self.width = width
+ self.height = height
self.frames = frames
- self.matrix = np.zeros((self.width, self.height, self.frames))
+ self.matrix = np.zeros(( self.height, self.width, self.frames, 2))
+
+ print self.matrix.shape
def save(self, file):
@@ -44,8 +45,6 @@ def day(self, time):
>>> m.day("2007-01-07T10:54:50+00:00")
6
"""
- #return datetime.date.weekday(self.time(time))
- #return datetime.date.weekday(datetime.datetime.now())
return date.weekday(self.time(time))
@@ -70,7 +69,7 @@ def time_diff(self, t1, t2):
280
"""
- diff = self.time(t2) - self.time(t1)
+ diff = abs(self.time(t2) - self.time(t1))
return diff.seconds
@@ -99,11 +98,11 @@ def dist(self, p1, p2):
0.0
"""
- return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
+ return int( math.sqrt(((p2[0] - p1[0]) ** 2) + ((p2[1] - p1[1]) ** 2)) )
- def train(self, p1, p2):
+ def train(self, p1, p2, day):
""" Trains the model with two points
uses the time diff and delta xy to linearly
@@ -114,7 +113,7 @@ def train(self, p1, p2):
xy1 = self.yx( p1[2], p1[3] )
xy2 = self.yx( p2[2], p2[3] )
- day = self.day( p2[1] )
+ #day = self.day( p2[1] )
delta = self.time_diff( p1[1], p2[1])
# update the matrix model
@@ -147,25 +146,20 @@ def learn(self, p1, p2, t_index, delta):
>>> m = Model( [-122, 38, -121, 37], './time_travel.npy' )
"""
distance = self.dist(p1, p2)
-
+
if distance == 0:
- # update the p2 cell to avoid a double count
x, y = p2[1], p2[0]
- val = self.matrix[y, x, t_index]
- incr = delta if val == 0 else delta / 2
-
- self.matrix[y, x, t_index] = delta if val == 0 else val + incr
-
else:
- t_per_cell = distance / delta if delta else 0
- x, y = self.vector(p1, p2)
- try:
- val = self.matrix[y, x, t_index]
- incr = delta if val == 0 else t_per_cell
- self.matrix[y, x, t_index] += incr
- except:
- pass
-
+ x, y = self.vector( p1, p2 )
+ delta = delta / (len(x)*len(y)) if delta else 0
+
+ try:
+ for px in x:
+ for py in y:
+ self.matrix[ py, px, t_index, 0] += delta
+ self.matrix[ py, px, t_index, 1] += 1
+ except:
+ pass
def predict(self, loc1, loc2, time=None):
@@ -179,13 +173,19 @@ def predict(self, loc1, loc2, time=None):
p2 = self.yx(loc2[0], loc2[1])
x, y = self.vector(p1, p2)
+ travel_time = 0
+ count = 0
if time:
- t_index = self.day(time) - 1
- travel_time = sum( self.matrix[y, x, t_index])
+ t_index = self.day(time)
+ for px in x:
+ for py in y:
+ travel_time += self.matrix[ py, px, t_index, 0] if ( self.matrix[ py, px, t_index, 1] == 0 ) else self.matrix[ py, px, t_index, 0]/self.matrix[ py, px, t_index, 1]
else:
- travel_time = sum( sum( self.matrix[y, x, :])) / self.frames
-
+ for px in x:
+ for py in y:
+ travel_time += sum(self.matrix[ py, px, :, 0]) / sum(self.matrix[ py, px, :, 1])
+
return self.timeFormat(int( travel_time ))
@@ -208,16 +208,16 @@ def visualize(self, index=None):
""" create a 3D view of the matrix for each time
"""
+ self.matrix.dtype = 'uint8'
if index:
- self.matrix.dtype = 'uint8'
I = self.matrix[:, :, index]
else:
I = np.sum( self.matrix, axis=2) / self.frames
+ print I.shape
I.dtype = 'uint8'
- print I.shape, I.dtype
- I.reshape(8000,1000)
+ #I.reshape(2000,4000)
index = 'all'
- resultImage = Image.fromarray(I)
+ resultImage = Image.fromarray(I).convert('RGB')
resultImage.save('uber.' + str(index) + '.png')
pass
View
18 task1_2.py
@@ -6,36 +6,38 @@
file = './time_travel.npy'
bbox = [-122.5, 37.5, -122, 38]
nhours = 7
+width, height = 1000, 1000
# create and train a model if the file isnt there
if not os.path.exists(file):
- m = Model( bbox, None, nhours )
+ m = Model( bbox, width, height, None, nhours )
gps = gzip.open('all.tsv.gz', 'r').readlines()
# loop over the points
for i in xrange(1, len(gps)):
- p2 = gps[i].split('\t')
- p1 = gps[i-1].split('\t')
+ p2 = gps[i].strip().split('\t')
+ p1 = gps[i-1].strip().split('\t')
print p1
# only train the model with points on the same track (ID)
if p1[0] == p2[0]:
- m.train( p1, p2 )
+ m.train( p1, p2, int( p1[1][8:10] )-1 )
print 'Finished training...now save'
#m.visualize()
m.save(file)
+ #print 'expect 128 sec'
print m.predict( (37.795521, -122.419347), (37.795679, -122.409885))
print m.predict( (37.782551, -122.445368), (37.786956, -122.440279))
print m.predict( (37.800224, -122.433520), (37.800066, -122.436167))
else:
- m = Model( bbox, file )
- m.visualize(4)
- m.visualize(5)
- m.visualize(6)
+ m = Model( bbox, width, height, file )
+ #m.visualize()
+ print '\t', m.predict( (37.782551, -122.445368), (37.786956, -122.440279), '2007-01-07T10:56:58+00:00')
+ print '\t', m.predict( (37.782551, -122.445368), (37.786956, -122.440279))
print 'Task 1 (w/o times):'
print '\t', m.predict( (37.795521, -122.419347), (37.795679, -122.409885))
print '\t', m.predict( (37.782551, -122.445368), (37.786956, -122.440279))

0 comments on commit ff050c3

Please sign in to comment.
Something went wrong with that request. Please try again.