-
Notifications
You must be signed in to change notification settings - Fork 12
/
viz_utils.py
126 lines (108 loc) · 2.99 KB
/
viz_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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
_COLOR_MAP_LOSS = np.array([
[192, 192, 255],
[ 96, 192, 96],
[192, 255, 192],
[192, 192, 192],
[ 0, 0, 0] # nodata
]) / 255.0
_COLOR_MAP_GAIN = np.array([
[ 0, 0, 255],
[ 0, 128, 0],
[128, 255, 128],
[128, 128, 128],
[ 0, 0, 0] # nodata
]) / 255.0
_COLOR_MAP_LC4 = np.array([
[ 0, 0, 255],
[ 0, 128, 0],
[128, 255, 128],
[128, 96, 96],
[ 0, 0, 0] # nodata
]) / 255.0
_COLOR_MAP_NLCD16 = np.array([
[ 0, 0, 0], # nodata
[ 70, 107, 159],
[209, 222, 248],
[222, 197, 197],
[217, 146, 130],
[235, 0, 0],
[171, 0, 0],
[179, 172, 159],
[104, 171, 95],
[ 28, 95, 44],
[181, 197, 143],
[204, 184, 121],
[223, 223, 194],
[220, 217, 57],
[171, 108, 40],
[184, 217, 235],
[108, 159, 184],
]) / 255.0
CMAP_LOSS = matplotlib.colors.ListedColormap(_COLOR_MAP_LOSS)
CMAP_GAIN = matplotlib.colors.ListedColormap(_COLOR_MAP_GAIN)
CMAP_LC = matplotlib.colors.ListedColormap(_COLOR_MAP_LC4)
CMAP_NLCD = matplotlib.colors.ListedColormap(_COLOR_MAP_NLCD16)
def show_legend(patches, labels):
fig = plt.figure(figsize=(4,2))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.axis("off")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.legend(patches, labels, loc='center', fontsize=17, frameon=False)
plt.show()
plt.close()
def show_loss_legend():
labels = [
'Water loss',
'Tree canopy loss',
'Low vegetation loss',
'Impervious surface loss',
'No change'
]
patches = [
matplotlib.patches.Patch(facecolor=CMAP_LOSS(i), edgecolor='k')
for i in range(5)
]
show_legend(patches, labels)
def show_gain_legend():
labels = [
'Water gain',
'Tree canopy gain',
'Low vegetation gain',
'Impervious surface gain',
'No change'
]
patches = [
matplotlib.patches.Patch(facecolor=CMAP_GAIN(i), edgecolor='k')
for i in range(5)
]
show_legend(patches, labels)
def show_img(img, cmap, vmin, vmax, title=None):
fig = plt.figure(figsize=(10,10))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.axis('off')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if cmap is not None:
ax.imshow(img, cmap=cmap, vmin=vmin, vmax=vmax, interpolation="none")
else:
ax.imshow(img)
if title is not None:
plt.title(title, fontsize=20)
plt.show()
plt.close()
def show_loss(predictions, title=None):
img = predictions.copy()
zero_mask = img == 0
img = img // 4
img[zero_mask] = 4
show_img(img, CMAP_LOSS, 0, len(_COLOR_MAP_LOSS) - 1, title)
def show_gain(predictions, title=None):
img = predictions.copy()
zero_mask = img == 0
img = img % 4
img[zero_mask] = 4
show_img(img, CMAP_GAIN, 0, len(_COLOR_MAP_GAIN) - 1, title)