/
examples.py
286 lines (252 loc) · 8.44 KB
/
examples.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
286
"""Some examples for quick testing/demonstrations.
All function accept `show` and `draw` arguments
* If `draw` is `True` it will return the widgets (Scatter, Volume, Mesh)
* If `draw` is `False`, it will return the data
* if `show` is `False`, `ipv.show()` will not be called.
"""
import warnings
import numpy as np
from numpy import cos, sin, pi
try:
import scipy.ndimage
import scipy.special
except:
pass # it's ok, it's not crucial
# __all__ = ["example_ylm"]
def xyz(shape=128, limits=[-3, 3], spherical=False, sparse=True, centers=False):
dim = 3
try:
shape[0]
except:
shape = [shape] * dim
try:
limits[0][0] # pylint: disable=unsubscriptable-object
except:
limits = [limits] * dim
if centers:
v = [
slice(vmin + (vmax - vmin) / float(N) / 2, vmax - (vmax - vmin) / float(N) / 4, (vmax - vmin) / float(N))
for (vmin, vmax), N in zip(limits, shape)
]
else:
v = [
slice(vmin, vmax + (vmax - vmin) / float(N) / 2, (vmax - vmin) / float(N - 1))
for (vmin, vmax), N in zip(limits, shape)
]
if sparse:
x, y, z = np.ogrid.__getitem__(v)
else:
x, y, z = np.mgrid.__getitem__(v)
if spherical:
r = np.linalg.norm([x, y, z])
theta = np.arctan2(y, x)
phi = np.arccos(z / r)
return x, y, z, r, theta, phi
else:
return x, y, z
def example_ylm(m=0, n=2, shape=128, limits=[-4, 4], draw=True, show=True, **kwargs):
"""Show a spherical harmonic."""
import ipyvolume.pylab as p3
__, __, __, r, theta, phi = xyz(shape=shape, limits=limits, spherical=True)
radial = np.exp(-(r - 2) ** 2)
data = np.abs(scipy.special.sph_harm(m, n, theta, phi) ** 2) * radial # pylint: disable=no-member
if draw:
vol = p3.volshow(data=data, **kwargs)
if show:
p3.show()
return vol
else:
return data
# return ipyvolume.volshow(data=data.T, **kwargs)
def ball(rmax=3, rmin=0, shape=128, limits=[-4, 4], draw=True, show=True, **kwargs):
"""Show a ball."""
import ipyvolume.pylab as p3
__, __, __, r, _theta, _phi = xyz(shape=shape, limits=limits, spherical=True)
data = r * 0
data[(r < rmax) & (r >= rmin)] = 0.5
if "data_min" not in kwargs:
kwargs["data_min"] = 0
if "data_max" not in kwargs:
kwargs["data_max"] = 1
data = data.T
if draw:
vol = p3.volshow(data=data, **kwargs)
if show:
p3.show()
return vol
else:
return data
# http://graphics.stanford.edu/data/voldata/
def klein_bottle(
draw=True,
show=True,
figure8=False,
endpoint=True,
uv=True,
wireframe=False,
texture=None,
both=False,
interval=1000,
):
"""Show one or two Klein bottles."""
import ipyvolume.pylab as p3
# http://paulbourke.net/geometry/klein/
u = np.linspace(0, 2 * pi, num=40, endpoint=endpoint)
v = np.linspace(0, 2 * pi, num=40, endpoint=endpoint)
u, v = np.meshgrid(u, v)
if both:
x1, y1, z1, _u1, _v1 = klein_bottle(endpoint=endpoint, draw=False, show=False)
x2, y2, z2, _u2, _v2 = klein_bottle(endpoint=endpoint, draw=False, show=False, figure8=True)
x = [x1, x2]
y = [y1, y2]
z = [z1, z2]
else:
if figure8:
# u -= np.pi
# v -= np.pi
a = 2
s = 5
x = s * (a + cos(u / 2) * sin(v) - sin(u / 2) * sin(2 * v) / 2) * cos(u)
y = s * (a + cos(u / 2) * sin(v) - sin(u / 2) * sin(2 * v) / 2) * sin(u)
z = s * (sin(u / 2) * sin(v) + cos(u / 2) * sin(2 * v) / 2)
else:
r = 4 * (1 - cos(u) / 2)
x = 6 * cos(u) * (1 + sin(u)) + r * cos(u) * cos(v) * (u < pi) + r * cos(v + pi) * (u >= pi)
y = 16 * sin(u) + r * sin(u) * cos(v) * (u < pi)
z = r * sin(v)
if draw:
if texture:
uv = True
if uv:
mesh = p3.plot_mesh(
x,
y,
z,
wrapx=not endpoint,
wrapy=not endpoint,
u=u / (2 * np.pi),
v=v / (2 * np.pi),
wireframe=wireframe,
texture=texture,
)
else:
mesh = p3.plot_mesh(x, y, z, wrapx=not endpoint, wrapy=not endpoint, wireframe=wireframe, texture=texture)
if show:
if both:
p3.animation_control(mesh, interval=interval)
p3.squarelim()
p3.show()
return mesh
else:
return x, y, z, u, v
def brain(
draw=True, show=True, fiducial=True, flat=True, inflated=True, subject='S1', interval=1000, uv=True, color=None
):
"""Show a human brain model.
Requirement:
$ pip install https://github.com/gallantlab/pycortex
"""
import ipyvolume as ipv
try:
import cortex
except:
warnings.warn("it seems pycortex is not installed, which is needed for this example")
raise
xlist, ylist, zlist = [], [], []
polys_list = []
def add(pts, polys):
xlist.append(pts[:, 0])
ylist.append(pts[:, 1])
zlist.append(pts[:, 2])
polys_list.append(polys)
def n(x):
return (x - x.min()) / x.ptp()
if fiducial or color is True:
pts, polys = cortex.db.get_surf('S1', 'fiducial', merge=True)
x, y, z = pts.T
r = n(x)
g = n(y)
b = n(z)
if color is True:
color = np.array([r, g, b]).T.copy()
else:
color = None
if fiducial:
add(pts, polys)
else:
if color is False:
color = None
if inflated:
add(*cortex.db.get_surf('S1', 'inflated', merge=True, nudge=True))
u = v = None
if flat or uv:
pts, polys = cortex.db.get_surf('S1', 'flat', merge=True, nudge=True)
x, y, z = pts.T
u = n(x)
v = n(y)
if flat:
add(pts, polys)
polys_list.sort(key=lambda x: len(x))
polys = polys_list[0]
if draw:
if color is None:
mesh = ipv.plot_trisurf(xlist, ylist, zlist, polys, u=u, v=v)
else:
mesh = ipv.plot_trisurf(xlist, ylist, zlist, polys, color=color, u=u, v=v)
if show:
if len(x) > 1:
ipv.animation_control(mesh, interval=interval)
ipv.squarelim()
ipv.show()
return mesh
else:
return xlist, ylist, zlist, polys
def head(draw=True, show=True, max_shape=256):
"""Show a volumetric rendering of a human male head."""
# inspired by http://graphicsrunner.blogspot.com/2009/01/volume-rendering-102-transfer-functions.html
import ipyvolume as ipv
from scipy.interpolate import interp1d
# First part is a simpler version of setting up the transfer function. Interpolation with higher order
# splines does not work well, the original must do sth different
colors = [[0.91, 0.7, 0.61, 0.0], [0.91, 0.7, 0.61, 80.0], [1.0, 1.0, 0.85, 82.0], [1.0, 1.0, 0.85, 256]]
x = np.array([k[-1] for k in colors])
rgb = np.array([k[:3] for k in colors])
N = 256
xnew = np.linspace(0, 256, N)
tf_data = np.zeros((N, 4))
kind = 'linear'
for channel in range(3):
f = interp1d(x, rgb[:, channel], kind=kind)
ynew = f(xnew)
tf_data[:, channel] = ynew
alphas = [[0, 0], [0, 40], [0.2, 60], [0.05, 63], [0, 80], [0.9, 82], [1.0, 256]]
x = np.array([k[1] * 1.0 for k in alphas])
y = np.array([k[0] * 1.0 for k in alphas])
f = interp1d(x, y, kind=kind)
ynew = f(xnew)
tf_data[:, 3] = ynew
tf = ipv.TransferFunction(rgba=tf_data.astype(np.float32))
head_data = ipv.datasets.head.fetch().data
if draw:
vol = ipv.volshow(head_data, tf=tf, max_shape=max_shape)
if show:
ipv.show()
return vol
else:
return head_data
def gaussian(N=1000, draw=True, show=True, seed=42, color=None, marker='sphere'):
"""Show N random gaussian distributed points using a scatter plot."""
import ipyvolume as ipv
rng = np.random.RandomState(seed) # pylint: disable=no-member
x, y, z = rng.normal(size=(3, N))
if draw:
if color:
mesh = ipv.scatter(x, y, z, marker=marker, color=color)
else:
mesh = ipv.scatter(x, y, z, marker=marker)
if show:
# ipv.squarelim()
ipv.show()
return mesh
else:
return x, y, z