-
Notifications
You must be signed in to change notification settings - Fork 6
/
gaussian_hill.py
213 lines (183 loc) · 7.53 KB
/
gaussian_hill.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
import numpy as np
from matplotlib import pyplot as pl
def gaussian_hill_elevation(n, b = 2.5):
x, y = np.meshgrid(np.linspace(-b,b,n),
np.linspace(-b,b,n))
z = np.exp(-x*x-y*y)
return (x, y, z)
def gaussian_hill_slope(n, b = 2.5):
x, y = np.meshgrid(np.linspace(-b,b,n),
np.linspace(-b,b,n))
r = np.sqrt(x*x+y*y)
return 2*r*np.exp(-r*r)
def gaussian_hill_curvature(n, b = 2.5):
x, y = np.meshgrid(np.linspace(-b,b,n),
np.linspace(-b,b,n))
r = np.sqrt(x*x+y*y)
return (1 - 2*r*r)*2*np.exp(-r*r)
def np_slope(x, y, z):
d = y[1,0] - y[0,0]
dy, dx = np.gradient(z, d)
return np.sqrt(dx*dx+dy*dy)
def np_abs_curvature(x, y, z):
d = y[1,0] - y[0,0]
dy, dx = np.gradient(z, d)
dz = np.sqrt(dx*dx+dy*dy)
dy, dx = np.gradient(dz, d)
return np.sqrt(dx*dx+dy*dy)
#Plot Analytical Solution of Gaussian Hill Elevation, Slope, and Curvature
n = 111
fg, ax = pl.subplots(1, 3)
x, y, z = gaussian_hill_elevation(n)
im = ax[0].imshow(z, cmap = pl.cm.cividis_r)
cb = fg.colorbar(im, ax = ax[0], orientation = 'horizontal')
cb.set_label('Elevation')
im = ax[1].imshow(gaussian_hill_slope(n), cmap = pl.cm.magma_r)
cb = fg.colorbar(im, ax = ax[1], orientation = 'horizontal')
cb.set_label('Slope')
v = gaussian_hill_curvature(n)
vmax = v.max()
im = ax[2].imshow(v, cmap = pl.cm.seismic, vmin = -vmax, vmax = vmax)
cb = fg.colorbar(im, ax = ax[2], orientation = 'horizontal')
cb.set_label('Curvature')
pl.show()
# Plot 3D view using elevation as color code
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x.ravel(), y.ravel(), z.ravel(), s=5, c=z.ravel(), cmap='viridis', marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Plot 3D view using slope as color code
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x.ravel(), y.ravel(), z.ravel(), s=5, c=gaussian_hill_slope(n).ravel(), cmap='magma_r', marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Plot Analytical and numerical Slope analysis
fg, ax = pl.subplots(1, 3)
im = ax[0].imshow(gaussian_hill_slope(n), cmap = pl.cm.magma_r)
cb = fg.colorbar(im, ax = ax[0], orientation = 'horizontal')
cb.set_label('Slope (analytic)')
im = ax[1].imshow(np_slope(x, y, z), cmap = pl.cm.magma_r)
cb = fg.colorbar(im, ax = ax[1], orientation = 'horizontal')
cb.set_label('Slope (numeric)')
v = gaussian_hill_slope(n) - np_slope(x, y, z)
vmax = v.max()
im = ax[2].imshow(v, cmap = pl.cm.seismic, vmin = -vmax, vmax = vmax)
cb = fg.colorbar(im, ax = ax[2], orientation = 'horizontal')
cb.set_label('Absolute difference (analytic - numeric)')
pl.show()
# Plot Analytical and numerical Curvature analysis
fg, ax = pl.subplots(1, 3)
v = gaussian_hill_curvature(n)
vmax = v.max()
im = ax[0].imshow(v, cmap = pl.cm.seismic, vmin = -vmax, vmax = vmax)
cb = fg.colorbar(im, ax = ax[0], orientation = 'horizontal')
cb.set_label('Curvature (analytic)')
v = np_abs_curvature(x, y, z)
vmax = v.max()
im = ax[1].imshow(v, cmap = pl.cm.seismic, vmin = -vmax, vmax = vmax)
cb = fg.colorbar(im, ax = ax[1], orientation = 'horizontal')
cb.set_label('Curvature (numeric)')
v = np.abs(gaussian_hill_curvature(n)) - np_abs_curvature(x, y, z)
vmax = np.percentile(v, 99)
im = ax[2].imshow(v, cmap = pl.cm.seismic, vmin = -vmax, vmax = vmax)
cb = fg.colorbar(im, ax = ax[2], orientation = 'horizontal')
cb.set_label('Absolute difference (analytic - numeric)')
pl.show()
# Slope Distributions
n=11
x, y, z = gaussian_hill_elevation(n)
G_grad = gaussian_hill_slope(n)
G_numerical_grad = np_slope(x, y, z)
G_grad_p = np.percentile(G_grad.ravel(), [5,10,25,50,75,90,95])
G_numerical_grad_p = np.percentile(G_numerical_grad.ravel(), [5,10,25,50,75,90,95])
G_grad_p-G_numerical_grad_p
fg, ax = pl.subplots(1, 1)
ax.hist(G_grad.ravel(), bins=10, color='b', histtype='step', label='Gaussian H. analytical gradient')
ax.set_xlabel('Slope', fontsize=16)
ax.set_ylabel('#', fontsize=16)
ax.set_xlim((0, 1))
ax.hist(G_numerical_grad.ravel(), bins=100, color='r', histtype='step', label='Gaussian H. numerical gradient')
ax.legend()
ax.set_title('Gaussian Hill with %d x %d elements'%(n,n), fontsize=24 )
n=111
x, y, z = gaussian_hill_elevation(n)
G_grad = gaussian_hill_slope(n)
G_numerical_grad = np_slope(x, y, z)
G_grad_p = np.percentile(G_grad.ravel(), [5,10,25,50,75,90,95])
G_numerical_grad_p = np.percentile(G_numerical_grad.ravel(), [5,10,25,50,75,90,95])
G_grad_p-G_numerical_grad_p
fg, ax = pl.subplots(1, 1)
ax.hist(G_grad.ravel(), bins=100, color='b', histtype='step', label='Gaussian H. analytical gradient')
ax.set_xlabel('Slope', fontsize=16)
ax.set_ylabel('#', fontsize=16)
ax.set_xlim((0, 1))
ax.hist(G_numerical_grad.ravel(), bins=100, color='r', histtype='step', label='Gaussian H. numerical gradient')
ax.legend()
ax.set_title('Gaussian Hill with %d x %d elements'%(n,n), fontsize=24 )
n=1111
x, y, z = gaussian_hill_elevation(n)
G_grad = gaussian_hill_slope(n)
G_numerical_grad = np_slope(x, y, z)
G_grad_p = np.percentile(G_grad.ravel(), [5,10,25,50,75,90,95])
G_numerical_grad_p = np.percentile(G_numerical_grad.ravel(), [5,10,25,50,75,90,95])
G_grad_p-G_numerical_grad_p
fg, ax = pl.subplots(1, 1)
ax.hist(G_grad.ravel(), bins=1000, color='b', histtype='step', label='Gaussian H. analytical gradient')
ax.set_xlabel('Slope', fontsize=16)
ax.set_ylabel('#', fontsize=16)
ax.set_xlim((0, 1))
ax.hist(G_numerical_grad.ravel(), bins=100, color='r', histtype='step', label='Gaussian H. numerical gradient')
ax.legend()
ax.set_title('Gaussian Hill with %d x %d elements'%(n,n), fontsize=24 )
#Subsampling/Resampling
n=111
x, y, z = gaussian_hill_elevation(n)
G_grad = gaussian_hill_slope(n)
xs = x[::2, ::2]
ys = y[::2, ::2]
zs = z[::2, ::2]
# Plot 3D view using elevation as color code and subsampled data
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x.ravel(), y.ravel(), z.ravel(), s=25, c=z.ravel(), cmap='viridis', marker='o')
ax.scatter(xs.ravel(), ys.ravel(), zs.ravel(), s=15, c='k', marker='x')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
#Interpolate to surface with higher density
#scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)[source]
from scipy.interpolate import interp2d
b=2.5
x_elementsi = np.linspace(-b,b,n)[::2]
y_elementsi = np.linspace(-b,b,n)[::2]
zsi = interp2d(x_elementsi, y_elementsi, zs, kind='linear')
x_elementsi = np.linspace(-b,b,n)
y_elementsi = np.linspace(-b,b,n)
zi = zsi(x_elementsi, y_elementsi)
fg, ax = pl.subplots(2, 2)
vmin = 0
vmax = 1
im = ax[0,0].imshow(z, cmap = pl.cm.viridis, vmin = vmin, vmax = vmax)
ax[0,0].set_title('Full-resolution Gaussian Hill', fontsize=21)
cb = fg.colorbar(im, ax = ax[0,0], orientation = 'horizontal')
cb.set_label('Elevation')
im = ax[0,1].imshow(zs, cmap = pl.cm.viridis, vmin = vmin, vmax = vmax)
ax[0,1].set_title('Subsampled Gaussian Hill', fontsize=21)
cb = fg.colorbar(im, ax = ax[0,1], orientation = 'horizontal')
cb.set_label('Elevation')
im = ax[1,0].imshow(zi, cmap = pl.cm.viridis, vmin = vmin, vmax = vmax)
ax[1,0].set_title('Resampled Gaussian Hill', fontsize=21)
cb = fg.colorbar(im, ax = ax[1,0], orientation = 'horizontal')
cb.set_label('Elevation')
dh = z-zi
im = ax[1,1].imshow(dh, cmap = pl.cm.Spectral, vmin = np.percentile(dh.ravel(),5), vmax = np.percentile(dh.ravel(),95))
ax[1,1].set_title('dH of full-resolution and resampled Gaussian Hill', fontsize=21)
cb = fg.colorbar(im, ax = ax[1,1], orientation = 'horizontal')
cb.set_label('dH')