-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawn_repeted_trip.py
141 lines (134 loc) · 4.54 KB
/
drawn_repeted_trip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
########################
# developed by rcarson
# aixueer4ever@gmail.com
########################
import os
import numpy as np
import pandas as pd
from random import random,randrange
import matplotlib.pyplot as plt
from math import pi
import sys
class draw_repeated_trip:
def __init__(self,dataPath,driver=None,threshold=0.05):
self.dataPath=dataPath
self.driver=driver
self.threshold=threshold
# flip a trip
def flip(self,x):
if x[x[:,1]>0,1].shape[0]>x.shape[0]/2:
x[:,1]=x[:,1]*(-1)
return x
# get the degree to rotate
def getrw(self,a):
(d,w)=self.getwd(a)
return -w
def rotate_one_point(self,a,w0):
(d,w)=self.getwd(a)
return self.getxy((d,w+w0))
def rotate_trip(self,trip):
xx=trip.shape[0]
a=[i for i in trip.xs(xx-1)]
w0=self.getrw(a)
xx=[]
for j in range(trip.shape[0]):
a=[i for i in trip.xs(j)]
xx.append(self.rotate_one_point(a,w0))
return np.array(xx)
# get x,y coordinate
def getxy(self,a):
(d,w)=a
return d*np.cos(w),d*np.sin(w)
# get r,w coordinate
def getwd(self,a):
[x,y]=a
if x>0:
return (x**2+y**2)**0.5,np.arctan(y/x)
elif x<0:
if y>0:
return (x**2+y**2)**0.5,pi+np.arctan(y/x)
elif y<0:
return (x**2+y**2)**0.5,-pi+np.arctan(y/x)
else:
return x,pi
else:
if y>0:
return y,pi/2
elif y<0:
return y,-pi/2
else:
return 0,0
def update_trips(self,tripl,trip,tripname,tripcounter,tripother):
if len(tripl)==0:
tripl[tripname]=trip
tripcounter[tripname]=0
tripother[tripname]=[]
else:
for t in tripl:
if self.sametrip(tripl[t],trip):
tripcounter[t]+=1
tripother[t].append(tripname)
for xx in tripother[t]:
tripcounter[xx]=tripcounter[t]
return tripl,tripcounter,tripother
tripl[tripname]=trip
tripcounter[tripname]=0
tripother[tripname]=[]
return tripl,tripcounter,tripother
def getdd(self,tx,ty):
while tx.shape[0]>ty.shape[0]:
ty=np.vstack((ty,ty[-1,:]))
while tx.shape[0]<ty.shape[0]:
tx=np.vstack((tx,tx[-1,:]))
dd=tx-ty
return np.sum((dd[:,0]**2+dd[:,1]**2)**0.5)
def sametrip(self,tx,ty):
mm=int(tx.shape[0]*self.threshold)
txx=np.vstack((tx[mm:,:],tx[-mm:,:]))
dd=self.getdd(tx,txx)
return self.getdd(tx,ty)<=dd
def check_trips(self,tripcounter,tripname):
return tripcounter[tripname]>2
def draw(self):
path=self.dataPath
drivers=os.listdir(path)
if self.driver!='-1':
driver=self.driver
else:
driver=drivers[randrange(len(drivers))]
print 'driver',driver
trips=os.listdir(path+'/'+driver)
tripl={} # trip name -> trip data
tripc={} # trip name -> number of same trips
tripo={} # trip name -> list of names of other same trips
for trip in trips:
if '.csv' in trip:
t1=pd.read_csv(path+'/'+driver+'/'+trip)
t1r=self.flip(self.rotate_trip(t1))
tripl,tripc,tripo=self.update_trips(tripl,t1r,trip,tripc,tripo)
c=0
for trip in trips:
if '.csv' in trip:
t1=pd.read_csv(path+'/'+driver+'/'+trip)
plt.subplot(1,3,1)
plt.plot(t1['x'],t1['y'])
plt.title('all trips of driver '+driver)
t1r=self.flip(self.rotate_trip(t1))
if self.check_trips(tripc,trip):
plt.subplot(1,3,2)
plt.plot(t1['x'],t1['y'])
plt.title('repeated trips of driver '+driver)
plt.subplot(1,3,3)
plt.plot(t1r[:,0],t1r[:,1])
plt.title('repeated trips of driver '+driver+' after rotation')
c+=1
print c, 'repeated trips'
plt.show()
if __name__ == "__main__":
path=sys.argv[1]
driver=sys.argv[2]
if len(sys.argv)==4:
dd=draw_repeated_trip(dataPath=path,driver=driver,threshold=float(sys.argv[3]))
else:
dd=draw_repeated_trip(dataPath=path,driver=driver)
dd.draw()