-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_utils.py
180 lines (149 loc) · 5.7 KB
/
plot_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
###### Plotting Utils #######
# Refer to: https://github.com/imagingofthings/DeepWave/blob/master/datasets/Pyramic/color_plot.py
import collections.abc as abc
import astropy.coordinates as coord
import astropy.units as u
import matplotlib.pyplot as plt
import mpl_toolkits.basemap as basemap
import matplotlib.tri as tri
import numpy as np
from sklearn.cluster import KMeans
import csv
def wrapped_rad2deg(lat_r, lon_r):
"""
Equatorial coordinate [rad] -> [deg] unit conversion.
Output longitude guaranteed to lie in [-180, 180) [deg].
"""
lat_d = coord.Angle(lat_r * u.rad).to_value(u.deg)
lon_d = coord.Angle(lon_r * u.rad).wrap_at(180 * u.deg).to_value(u.deg)
return lat_d, lon_d
def cart2pol(x, y, z):
"""
Cartesian coordinates to Polar coordinates.
"""
cart = coord.CartesianRepresentation(x, y, z)
sph = coord.SphericalRepresentation.from_cartesian(cart)
r = sph.distance.to_value(u.dimensionless_unscaled)
colat = u.Quantity(90 * u.deg - sph.lat).to_value(u.rad)
lon = u.Quantity(sph.lon).to_value(u.rad)
return r, colat, lon
def cart2eq(x, y, z):
"""
Cartesian coordinates to Equatorial coordinates.
"""
r, colat, lon = cart2pol(x, y, z)
lat = (np.pi / 2) - colat
return r, lat, lon
def is_scalar(x):
"""
Return :py:obj:`True` if `x` is a scalar object.
"""
if not isinstance(x, abc.Container):
return True
return False
def eq2cart(r, lat, lon):
"""
Equatorial coordinates to Cartesian coordinates.
"""
r = np.array([r]) if is_scalar(r) else np.array(r, copy=False)
if np.any(r < 0):
raise ValueError("Parameter[r] must be non-negative.")
XYZ = (
coord.SphericalRepresentation(lon * u.rad, lat * u.rad, r)
.to_cartesian()
.xyz.to_value(u.dimensionless_unscaled)
)
return XYZ
def cmap_from_list(name, colors, N=256, gamma=1.0):
"""
Parameters
----------
name : str
colors :
* a list of (value, color) tuples; or
* list of color strings
N : int
Number of RGB quantization levels.
gamma : float
Something?
Returns
-------
cmap : :py:class:`matplotlib.colors.LinearSegmentedColormap`
"""
from collections.abc import Sized
import matplotlib.colors
if not isinstance(colors, abc.Iterable):
raise ValueError('colors must be iterable')
if (isinstance(colors[0], Sized) and
(len(colors[0]) == 2) and
(not isinstance(colors[0], str))): # List of value, color pairs
vals, colors = zip(*colors)
else:
vals = np.linspace(0, 1, len(colors))
cdict = dict(red=[], green=[], blue=[], alpha=[])
for val, color in zip(vals, colors):
r, g, b, a = matplotlib.colors.to_rgba(color)
cdict['red'].append((val, r, r))
cdict['green'].append((val, g, g))
cdict['blue'].append((val, b, b))
cdict['alpha'].append((val, a, a))
return matplotlib.colors.LinearSegmentedColormap(name, cdict, N, gamma)
def draw_map(I, R, lon_ticks, ground_truth_info=None, catalog=None, show_labels=False, show_axis=False):
"""
Parameters
==========
I : :py:class:`~numpy.ndarray`
(3, N_px)
R : :py:class:`~numpy.ndarray`
(3, N_px)
"""
_, R_el, R_az = cart2eq(*R)
R_el, R_az = wrapped_rad2deg(R_el, R_az)
R_el_min, R_el_max = np.around([np.min(R_el), np.max(R_el)])
R_az_min, R_az_max = np.around([np.min(R_az), np.max(R_az)])
fig = plt.figure()
ax = fig.add_subplot(111)
bm = basemap.Basemap(projection='mill',
llcrnrlat=R_el_min, urcrnrlat=R_el_max,
llcrnrlon=R_az_min, urcrnrlon=R_az_max,
resolution='c',
ax=ax)
if show_axis:
bm_labels = [1, 0, 0, 1]
else:
bm_labels = [0, 0, 0, 0]
bm.drawparallels(np.linspace(R_el_min, R_el_max, 5),
color='w', dashes=[1, 0], labels=bm_labels, labelstyle='+/-',
textcolor='#565656', zorder=0, linewidth=2)
bm.drawmeridians(lon_ticks,
color='w', dashes=[1, 0], labels=bm_labels, labelstyle='+/-',
textcolor='#565656', zorder=0, linewidth=2)
if show_labels:
ax.set_xlabel('Azimuth (degrees)', labelpad=20)
ax.set_ylabel('Elevation (degrees)', labelpad=40)
ax.set_title('Sound Events (SELDNet Prediction)', pad=20)
R_x, R_y = bm(R_az, R_el)
triangulation = tri.Triangulation(R_x, R_y)
N_px = I.shape[1]
mycmap = cmap_from_list('mycmap', I.T, N=N_px)
colors_cmap = np.arange(N_px)
ax.tripcolor(triangulation, colors_cmap, cmap=mycmap,
shading='gouraud', alpha=0.9, edgecolors='w', linewidth=0.1)
# Npts = 12
# I_s = np.square(I).sum(axis=0)
# max_idx = I_s.argsort()[-Npts:][::-1]
# x_y = np.column_stack((R_x[max_idx], R_y[max_idx]))
# km_res = KMeans(n_clusters=1).fit(x_y)
# clusters = km_res.cluster_centers_
if ground_truth_info and len(ground_truth_info["gt"]) != 0:
for i in range(len(ground_truth_info["gt"])):
ground_truth = ground_truth_info["gt"][i]
gt_num = ground_truth_info["num"][i]
gt_color = ground_truth_info["color"][i]
gt_x, gt_y = bm(ground_truth[0], ground_truth[1])
ax.scatter(gt_x, gt_y, s=500, alpha=0.3, c=gt_color)
ax.text(gt_x, gt_y, gt_num, ha='center', va='center', color='white')
# # Note, the cases where the intensity is at 180 deg gives a funny error with kmeans.
# # Essentially it finds the centroid right at 0 deg since the intensity is split
# # Maybe think on how to solve this @Sivan
return fig, ax, triangulation