forked from dnguyengithub/MultitaskAIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
234 lines (206 loc) · 8.68 KB
/
utils.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# coding: utf-8
# MIT License
#
# Copyright (c) 2018 Duong Nguyen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ==============================================================================
"""
Utils for MultitaskAIS.
"""
import numpy as np
from math import radians, cos, sin, asin, sqrt
import sys
sys.path.append('..')
sys.path.append('Data')
#import shapefile
import time
from pyproj import Geod
geod = Geod(ellps='WGS84')
#import dataset
AVG_EARTH_RADIUS = 6378.137 # in km
SPEED_MAX = 30 # knot
LAT, LON, SOG, COG, HEADING, ROT, NAV_STT, TIMESTAMP, MMSI = list(range(9))
def trackOutlier(A):
"""
Koyak algorithm to perform outlier identification
Our approach to outlier detection is to begin by evaluating the expression
“observation r is anomalous with respect to observation s ” with respect to
every pair of measurements in a track. We address anomaly criteria below;
assume for now that a criterion has been adopted and that the anomaly
relationship is symmetric. More precisely, let a(r,s) = 1 if r and s are
anomalous and a(r,s) = 0 otherwise; symmetry implies that a(r,s) = a(s,r).
If a(r,s) = 1 either one or both of observations are potential outliers,
but which of the two should be treated as such cannot be resolved using
this information alone.
Let A denote the matrix of anomaly indicators a(r, s) and let b denote
the vector of its row sums. Suppose that observation r is an outlier and
that is the only one present in the track. Because we expect it to be
anomalous with respect to many if not all of the other observations b(r)
should be large, while b(s) = 1 for all s ≠ r . Similarly, if there are
multiple outliers the values of b(r) should be large for those observations
and small for the non-outliers.
Source: "Predicting vessel trajectories from AIS data using R", Brian L
Young, 2017
INPUT:
A : (nxn) symmatic matrix of anomaly indicators
OUTPUT:
o : n-vector outlier indicators
# FOR TEST
A = np.zeros((5,5))
idx = np.array([[0,2],[1,2],[1,3],[0,3],[2,4],[3,4]])
A[idx[:,0], idx[:,1]] = 1
A[idx[:,1], idx[:,0]] = 1 sampling_track = np.empty((0, 9))
for t in range(int(v[0,TIMESTAMP]), int(v[-1,TIMESTAMP]), 300): # 5 min
tmp = utils.interpolate(t,v)
if tmp is not None:
sampling_track = np.vstack([sampling_track, tmp])
else:
sampling_track = None
break
"""
assert (A.transpose() == A).all(), "A must be a symatric matrix"
assert ((A==0) | (A==1)).all(), "A must be a binary matrix"
# Initialization
n = A.shape[0]
b = np.sum(A, axis = 1)
o = np.zeros(n)
while(np.max(b) > 0):
r = np.argmax(b)
o[r] = 1
b[r] = 0
for j in range(n):
if (o[j] == 0):
b[j] -= A[r,j]
return o.astype(bool)
def detectOutlier(track, speed_max = SPEED_MAX):
"""
removeOutlier() removes anomalus AIS messages from AIS track.
An AIS message is considered as beging anomalous if the speed is
infeasible (> speed_max). There are two types of anomalous messages:
- The reported speed is infeasible
- The calculated speed (distance/time) is infeasible
INPUT:
track : a (nxd) matrix. Each row is an AIS message. The structure
must follow: [Timestamp, Lat, Lon, Speed]
speed_max : knot
OUTPUT:
o : n-vector outlier indicators
"""
# Remove anomalous reported speed
o_report = track[:,3] > speed_max # Speed in track is in knot
if o_report.all():
return o_report, None
track = track[np.invert(o_report)]
# Calculate speed base on (lon, lat) and time
N = track.shape[0]
# Anomoly indicator matrix
A = np.zeros(shape = (N,N))
# Anomalous calculated-speed
for i in range(1,5):
# the ith diagonal
_, _, d = geod.inv(track[:N-i,2],track[:N-i,1],
track[i:,2],track[i:,1])
delta_t = track[i:,0] - track[:N-i,0].astype(np.float)
cond = np.logical_and(delta_t > 2,d/delta_t > (speed_max*0.514444))
abnormal_idx = np.nonzero(cond)[0]
A[abnormal_idx, abnormal_idx + i] = 1
A[abnormal_idx + i, abnormal_idx] = 1
o_calcul = trackOutlier(A)
return o_report, o_calcul
# Creating shape file
def createShapefile(shp_fname, Vs):
"""
Creating AIS shape files
INPUT:
shp_fname : name of the shapefile
Vs : AIS data, each element of the dictionary is an AIS track
whose structure is:
[Timestamp, MMSI, Lat, Lon, SOG, COG, Heading, ROT, NAV_STT]
"""
shp = shapefile.Writer(shapefile.POINT)
shp.field('MMSI', 'N', 10)
shp.field('TIMESTAMP', 'N', 12)
shp.field('DATETIME', 'C', 20)
shp.field('LAT','N',10,5)
shp.field('LON','N',10,5)
shp.field('SOG','N', 10,5)
shp.field('COG', 'N', 10,5)
shp.field('HEADING', 'N', 10,5)
shp.field('ROT', 'N', 5)
shp.field('NAV_STT', 'N', 2)
for mmsi in list(Vs.keys()):
for p in Vs[mmsi]:
shp.point(p[LON],p[LAT])
shp.record(p[MMSI],
p[TIMESTAMP],
time.strftime('%H:%M:%S %d-%m-%Y', time.gmtime(p[TIMESTAMP])),
p[LAT],
p[LON],
p[SOG],
p[COG],
p[HEADING],
p[ROT],
p[NAV_STT])
shp.save(shp_fname)
def interpolate(t, track):
"""
Interpolating the AIS message of vessel at a specific "t".
INPUT:
- t :
- track : AIS track, whose structure is
[LAT, LON, SOG, COG, HEADING, ROT, NAV_STT, TIMESTAMP, MMSI]
OUTPUT:
- [LAT, LON, SOG, COG, HEADING, ROT, NAV_STT, TIMESTAMP, MMSI]
"""
before_p = np.nonzero(t >= track[:,TIMESTAMP])[0]
after_p = np.nonzero(t < track[:,TIMESTAMP])[0]
if (len(before_p) > 0) and (len(after_p) > 0):
apos = after_p[0]
bpos = before_p[-1]
# Interpolation
dt_full = float(track[apos,TIMESTAMP] - track[bpos,TIMESTAMP])
if (abs(dt_full) > 2*3600):
return None
dt_interp = float(t - track[bpos,TIMESTAMP])
try:
az, _, dist = geod.inv(track[bpos,LON],
track[bpos,LAT],
track[apos,LON],
track[apos,LAT])
dist_interp = dist*(dt_interp/dt_full)
lon_interp, lat_interp, _ = geod.fwd(track[bpos,LON], track[bpos,LAT],
az, dist_interp)
speed_interp = (track[apos,SOG] - track[bpos,SOG])*(dt_interp/dt_full) + track[bpos,SOG]
course_interp = (track[apos,COG] - track[bpos,COG] )*(dt_interp/dt_full) + track[bpos,COG]
heading_interp = (track[apos,HEADING] - track[bpos,HEADING])*(dt_interp/dt_full) + track[bpos,HEADING]
rot_interp = (track[apos,ROT] - track[bpos,ROT])*(dt_interp/dt_full) + track[bpos,ROT]
if dt_interp > (dt_full/2):
nav_interp = track[apos,NAV_STT]
else:
nav_interp = track[bpos,NAV_STT]
except:
return None
return np.array([lat_interp, lon_interp,
speed_interp, course_interp,
heading_interp, rot_interp,
nav_interp,t,
track[0,MMSI]])
else:
return None