-
Notifications
You must be signed in to change notification settings - Fork 4
/
visual_tools.py
197 lines (158 loc) · 7.1 KB
/
visual_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
import numpy as np
from mpl_toolkits.basemap import Basemap
from osgeo import gdal
import georaster
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
import matplotlib.patches as patches
import matplotlib.cm as cm
from math import cos, sin
def plotPathStatHistory(history_df, plotsfile, dpi = 100):
# history_df : dataframe of pathStat history
plt.plot(history_df.index.values, history_df["work"], 'p')
plt.ylabel("Work")
plt.xlabel("Iteration")
plt.title("Work Convergence")
plt.savefig(plotsfile + "_work.eps", bbox_inches = "tight", format = "eps", dpi = dpi)
plt.clf()
plt.plot(history_df.index.values, history_df["waypoints"], 'p')
plt.ylabel("Waypoints")
plt.xlabel("Iteration")
plt.title("Number of Waypoints Convergence")
plt.savefig(plotsfile + "_waypoints.eps", bbox_inches = "tight", format = "eps", dpi = dpi)
plt.clf()
plt.plot(history_df.index.values, history_df["distance"], 'p')
plt.ylabel("Distance")
plt.xlabel("Iteration")
plt.title("Distance Convergence")
plt.savefig(plotsfile + "_distance.eps", bbox_inches = "tight", format = "eps", dpi = dpi)
plt.clf()
plt.plot(history_df.index.values, history_df["avg"], 'p')
plt.ylabel("Average cost2go value")
plt.xlabel("Iteration")
plt.title("Average cost2go Convergence")
plt.savefig(plotsfile + "_avg.eps", bbox_inches = "tight", format = "eps", dpi = dpi)
plt.clf()
plt.plot(history_df.index.values, history_df["avg_diff"], 'p')
plt.ylabel("Average cost2go Change")
plt.xlabel("Iteration")
plt.title("Average cost2go Change Convergence")
plt.savefig(plotsfile + "_avg_diff.eps", bbox_inches = "tight", format = "eps", dpi = dpi)
plt.clf()
def plotRegion(occupancyRasterFile, occupancyGrid, plotsfile = None, width = 10):
occ_raster = gdal.Open(occupancyRasterFile)
cols = occ_raster.RasterXSize
rows = occ_raster.RasterYSize
height = ( float(rows) / float(cols) ) * float(width)
f = plt.figure(figsize = (width, height))
ax = f.add_subplot(111)
region = georaster.SingleBandRaster(occ_raster, load_data=False)
minx, maxx, miny, maxy = region.extent
m = Basemap (projection = 'cyl',
llcrnrlon = minx - .005,
llcrnrlat = miny - .005,
urcrnrlon = maxx + .005,
urcrnrlat = maxy + .005,
resolution = 'h')
image = georaster.SingleBandRaster(occ_raster,
load_data=(minx, maxx, miny, maxy), latlon=True)
ax.imshow(image.r, extent=(0, cols, 0, rows), zorder=0, alpha=1, cmap=cm.Paired)
if plotsfile is not None:
#plt.savefig(plotsfile + "_region.eps", bbox_inches = "tight", format = "eps", dpi = 300)
plt.savefig(plotsfile + "_region.eps", bbox_inches = -2, format = "eps", dpi = 100)
return ax
def plotPath(trace, waypoints, occupancyRasterFile, occupancyGrid, plotsfile = None, width = 10, init = True):
def plotit(trace, waypoints):
ypoints = [occupancyGrid.shape[0] - w[0] - 0.5 for w in waypoints]
xpoints = [w[1] + 0.5 for w in waypoints]
plt.plot(xpoints, ypoints, 'x--', linewidth=6, markersize=15)
plt.scatter([trace[0][1] + 0.5], [occupancyGrid.shape[0] - trace[0][0] - 0.5],
s = 550, marker = "D", color = "#ec73c9")
plt.scatter([trace[len(trace) - 1][1] + 0.5], [occupancyGrid.shape[0] - trace[len(trace) - 1][0] - 0.5],
s = 550, marker = "o", color = "seagreen")
return plt.gca()
if init == True:
plt.close("all")
ax = plotRegion(occupancyRasterFile, occupancyGrid)
plt.xlim(auto=False)
plt.ylim(auto=False)
ax = plotit(trace, waypoints)
if plotsfile is not None:
plt.savefig(plotsfile + "_path.eps", bbox_inches = "tight", format = "eps", dpi = 100)
return ax
def plotVector(ugrids, vgrids, occupancyRasterFile, occupancyGrid, plotsfile = None, width = 10, init = True, sampleInterval = 1, color = "black"):
plt.close("all")
usamples_sum = None
vsamples_sum = None
for i in range(len(ugrids)):
plt.clf()
ax = plotRegion(occupancyRasterFile, occupancyGrid)
plt.xlim(auto=False)
plt.ylim(auto=False)
ugrid = np.flipud(ugrids[i])
vgrid = np.flipud(vgrids[i])
rows = len(occupancyGrid)
cols = len(occupancyGrid[0])
ysamples = []
xsamples = []
usamples = []
vsamples = []
for row in range(0, rows - 1, sampleInterval):
for col in range(0, cols - 1, sampleInterval):
ysamples.append(row - 0.5)
xsamples.append(col + 0.5)
usamples.append(ugrid[row][col])
vsamples.append(vgrid[row][col])
if usamples_sum == None:
usamples_sum = np.array(usamples)
else:
usamples_sum = usamples_sum + usamples
if vsamples_sum == None:
vsamples_sum = np.array(vsamples)
else:
vsamples_sum = vsamples_sum + vsamples
ysamples = [rows - y for y in ysamples]
plt.quiver(xsamples[::], ysamples[::], usamples[::], vsamples[::],
headwidth = 5, color = color)
if plotsfile is not None:
plt.savefig(plotsfile + "_environment_" + str(i) + ".eps", bbox_inches = "tight", format = "eps", dpi = 100)
plt.clf()
ax = plotRegion(occupancyRasterFile, occupancyGrid)
plt.xlim(auto=False)
plt.ylim(auto=False)
plt.quiver(xsamples[::], ysamples[::], usamples_sum[::], vsamples_sum[::], color = color, width = .005)
if plotsfile is not None:
plt.savefig(plotsfile + "_environment" + ".eps", bbox_inches = "tight", format = "eps", dpi = 100)
return ax
def plotActions(actiongrid, action2radians, occupancyRasterFile, occupancyGrid, plotsfile = None,
width = 10, init = True, sampleInterval = 1, magnitude = 4):
def action2uv(action, magnitude, action2radians):
if action == '*' or action == '-' or action == ' ':
magnitude = 0
u = magnitude * cos(action2radians[action])
v = magnitude * sin(action2radians[action])
return (u, v)
rows = len(occupancyGrid)
cols = len(occupancyGrid[0])
#actiongrid = np.flipud(actiongrid)
ysamples = []
xsamples = []
usamples = []
vsamples = []
for row in range(0, rows - 1, sampleInterval):
for col in range(0, cols - 1, sampleInterval):
ysamples.append(row + 0.5)
xsamples.append(col + 0.5)
u, v = action2uv(actiongrid[row][col], magnitude, action2radians)
usamples.append(u)
vsamples.append(v)
plt.close("all")
ax = plotRegion(occupancyRasterFile, occupancyGrid)
plt.xlim(auto = False)
plt.ylim(auto = False)
ysamples = [rows - y for y in ysamples]
plt.quiver(xsamples[::], ysamples[::], usamples[::], vsamples[::], scale=20, scale_units='inches')
if plotsfile is not None:
plt.savefig(plotsfile + "_actions" + ".png", bbox_inches = "tight", format = "png", dpi = 100)
return ax