-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.py
285 lines (266 loc) · 9.61 KB
/
tools.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
def rec2ann(xr, yr, r, R, dim):
import numpy as np
rho = float(yr) + r
phi = float(xr)/dim[1]*2*np.pi + np.pi/2
ya = rho*np.cos(phi) + R
xa = rho*np.sin(phi) + R
return [round(xa), round(ya)]
def ann2rec(xa, ya, r, R, dim):
import numpy as np
rho = np.sqrt((xa-R)**2 + (ya-R)**2)
phi = np.arctan2(xa-R, ya-R) - np.pi/2
xr = dim[1]*phi*rho / (2*np.pi*rho)
if xr < 0:
xr += dim[1]
yr = rho - r
return [round(xr), round(yr)]
def find_center(cluster):
import numpy as np
return [np.median(cluster[:,0]), np.median(cluster[:,1])]
def find_box(cluster):
import numpy as np
cluster = np.asarray(cluster)
return [min(cluster[:,0]), min(cluster[:,1]),
max(cluster[:,0]), max(cluster[:,1])]
def in_hull(points, hull):
#print "Deciding if points are inside the hull..."
import matplotlib.path as path
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
edges = []
codes = []
prev = hull[0][0]
next = hull[0][1]
edges.append(tuple(prev))
codes.append(path.Path.MOVETO)
edges.append(tuple(next))
codes.append(path.Path.LINETO)
idx = 2
while idx < len(hull)+1:
for edge in hull:
pt1 = edge[0]
pt2 = edge[1]
if (pt1[0] == next[0] and
pt1[1] == next[1] and
(pt2[0] != prev[0] or
pt2[1] != prev[1])):
prev = pt1
next = pt2
edges.append(tuple(next))
codes.append(path.Path.LINETO)
#print "break on pt2"
break
elif (pt2[0] == next[0] and
pt2[1] == next[1] and
(pt1[0] != prev[0] or
pt1[1] != prev[1])):
prev = pt2
next = pt1
edges.append(tuple(next))
codes.append(path.Path.LINETO)
#print "break on pt1"
break
idx += 1
edges.append(hull[0][0])
codes.append(path.Path.CLOSEPOLY)
polygon = path.Path(edges,codes)
mask = polygon.contains_points(points)
if False:
mask = mask.reshape(4040,80).T
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
patch = patches.PathPatch(polygon, facecolor="orange", lw=2)
#ax.add_patch(patch)
ax.imshow(mask)
ax.set_xlim(0,4040)
ax.set_ylim(0,80)
ax.axis("equal")
ax.invert_yaxis
plt.show()
return mask
def find_mask(hull, box):
import numpy as np
dim = [box[2]-box[0], box[3]-box[1]]
mask = np.asarray([[x, y] for x in np.arange(box[0], box[2])
for y in np.arange(box[1], box[3])])
mask = np.transpose(in_hull(mask, hull).reshape(dim[0], dim[1]))
return mask
def unwrap(wrapped):
import numpy as np
import copy
duration = len(wrapped)
unwrapped = copy.deepcopy(wrapped)
tmp = np.array(wrapped)
for idx in range(duration-1):
if tmp[idx+1] - tmp[idx] > 3840/2:
tmp = tmp - 3840
unwrapped[idx+1] = tmp[idx+1]
for t in np.arange(2,30*3):
try:
if tmp[idx+t] - tmp[idx+1] < -3840/2:
tmp[idx+t] += 3840
except:
pass
else:
unwrapped[idx+1] = tmp[idx+1]
return unwrapped
def contains_list(original_list, sublist):
for item in original_list:
if (sublist[0] == item[0] and
sublist[1] == item[1]):
return True
return False
def ConcaveHull(points):
#print "Finding concave hull..."
from scipy.spatial import Delaunay
import numpy as np
import collections
import matplotlib.pyplot as plt
delaunay = Delaunay(points)
simplices = delaunay.simplices
edge_list = []
for simplex in simplices:
delta = points[simplex]
perimeter = np.sqrt((delta[0,0] - delta[1,0])**2 +
(delta[0,1] - delta[1,1])**2)
perimeter += np.sqrt((delta[1,0] - delta[2,0])**2 +
(delta[1,1] - delta[2,1])**2)
perimeter += np.sqrt((delta[0,0] - delta[2,0])**2 +
(delta[0,1] - delta[2,1])**2)
if perimeter < 30:
delta = delta.tolist()
delta = [(pt[0],pt[1]) for pt in delta]
edge = [delta[0], delta[1]]
edge.sort()
edge_list.append(tuple(edge))
edge = [delta[1], delta[2]]
edge.sort()
edge_list.append(tuple(edge))
edge = [delta[2], delta[0]]
edge.sort()
edge_list.append(tuple(edge))
counter = collections.Counter(edge_list)
hull = np.asarray([key for (key, value)
in counter.items() if value == 1])
return hull
def remove_padding(data):
import numpy as np
for idx, pos in enumerate(data):
if pos < 100:
data[idx] = 3940 - pos
elif pos >= 3940:
data[idx] = pos - 3940
else:
data[idx] -= 100
data = unwrap(data)
return np.asarray(data)
def match_histogram(source, template):
# Credit to ali_m from:
# http://stackoverflow.com/questions/32655686/histogram-matching-of-two-images-in-python-2-x
import numpy as np
oldshape = source.shape
source = source.ravel()
template = template.ravel()
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape).astype(np.uint8)
# Smooth data using a 4th orderivatives spline smoother.
def spline_smoothing(data, deg=4, tol=14, detect_outliers=True):
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("seaborn-white")
import matplotlib
matplotlib.rc("font", family="FreeSans", size=18)
from scipy.interpolate import UnivariateSpline
import statsmodels.api as sm
import warnings
warnings.filterwarnings("ignore")
data = np.pad(data, ((150,150),(0,0)), mode="edge")
dim = data.shape
time = np.arange(dim[0])/30.
# Some possible preprocessing hacks here
#data = savgol_filter(data, 125, 4, axis=0)
#for idx in range(dim[1]):
# data[:,idx] = sm.nonparametric.lowess(data[:,idx], time,
# frac=30.0/dim[0],
# return_sorted=False)
splines = []
for idx in range(dim[1]):
x = time
y = data[:,idx]
splines.append(UnivariateSpline(x, y, k=deg, s=tol))
derivatives = np.asarray([[spline.derivatives(t) for spline in splines] for t in time])
if False:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.plot(derivatives[150:-150,:,0])
ax2 = fig.add_subplot(2,2,2)
ax2.plot(derivatives[150:-150,:,1])
ax3 = fig.add_subplot(2,2,3)
ax3.plot(derivatives[150:-150,:,2])
ax4 = fig.add_subplot(2,2,4)
ax4.plot(derivatives[150:-150,:,3])
plt.show(block=False)
if detect_outliers: # weight-based implementation with bbox
splines = []
for idx in range(dim[1]):
x = time
y = data[:,idx]
accs = derivatives[:,idx,2]
weights = []
for acc in accs:
if abs(acc) < 2: # outlier detection
weights.append(1)
elif abs(acc) < 3:
weights.append(0.1)
else:
weights.append(0)
weights = np.asarray(weights)
splines.append(UnivariateSpline(x, y, w=weights, k=deg, s=tol))
derivatives = np.asarray([[spline.derivatives(t) for spline in splines] for t in time])
if False:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.plot(derivatives[150:-150,:,0])
ax2 = fig.add_subplot(2,2,2)
ax2.plot(derivatives[150:-150,:,1])
ax3 = fig.add_subplot(2,2,3)
ax3.plot(derivatives[150:-150,:,2])
ax4 = fig.add_subplot(2,2,4)
ax4.plot(derivatives[150:-150,:,3])
plt.show(block=False)
splines = []
for idx in range(dim[1]):
x = time
y = derivatives[:,idx,0]
jerks = derivatives[:,idx,3]
weights = []
for jerk in jerks:
if abs(jerk) < 3: # outlier detection
weights.append(1)
elif abs(jerk) < 5:
weights.append(0.1)
else:
weights.append(0)
weights = np.asarray(weights)
splines.append(UnivariateSpline(x, y, w=weights, k=deg, s=tol))
derivatives = np.asarray([[spline.derivatives(t) for spline in splines] for t in time])
if False:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.plot(derivatives[150:-150,:,0])
ax2 = fig.add_subplot(2,2,2)
ax2.plot(derivatives[150:-150,:,1])
ax3 = fig.add_subplot(2,2,3)
ax3.plot(derivatives[150:-150,:,2])
ax4 = fig.add_subplot(2,2,4)
ax4.plot(derivatives[150:-150,:,3])
plt.show()
return derivatives[150:-150,:,:]