This repository was archived by the owner on Dec 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslice3d.py
executable file
·102 lines (80 loc) · 2.69 KB
/
slice3d.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
#!/usr/bin/env python
"""
slice plot
https://nbviewer.jupyter.org/github/empet/Plotly-plots/blob/master/Plotly-Slice-in-volumetric-data.ipynb
"""
import plotly
import plotly.graph_objs as pgo
import numpy as np
pl_BrBG = [
[0.0, "rgb(84, 48, 5)"],
[0.1, "rgb(138, 80, 9)"],
[0.2, "rgb(191, 129, 45)"],
[0.3, "rgb(222, 192, 123)"],
[0.4, "rgb(246, 232, 195)"],
[0.5, "rgb(244, 244, 244)"],
[0.6, "rgb(199, 234, 229)"],
[0.7, "rgb(126, 203, 192)"],
[0.8, "rgb(53, 151, 143)"],
[0.9, "rgb(0, 101, 93)"],
[1.0, "rgb(0, 60, 48)"],
]
def get_the_slice(x, y, z, surfacecolor, colorscale=pl_BrBG, showscale=False):
"""https://plot.ly/python/reference/#surface"""
return pgo.Surface(
x=x, y=y, z=z, surfacecolor=surfacecolor, colorscale=colorscale, showscale=showscale
)
def get_lims_colors(surfacecolor): # color limits for a slice
return np.min(surfacecolor), np.max(surfacecolor)
def main():
alpha = np.pi / 5
x = np.linspace(-2, 2, 50)
y = np.linspace(-2, 2, 50)
x, y = np.meshgrid(x, y)
z = -x * np.tan(alpha)
def volume(x, y, z):
return x * np.exp(-x ** 2 - y ** 2 - z ** 2)
x = np.linspace(-2, 2, 50)
y = np.linspace(-2, 2, 50)
x, y = np.meshgrid(x, y)
z = np.zeros(x.shape)
surfcolor_z = volume(x, y, z)
sminz, smaxz = get_lims_colors(surfcolor_z)
slice_z = get_the_slice(x, y, z, surfcolor_z)
x = np.linspace(-2, 2, 50)
z = np.linspace(-2, 2, 50)
x, z = np.meshgrid(x, y)
y = -0.5 * np.ones(x.shape)
surfcolor_y = volume(x, y, z)
sminy, smaxy = get_lims_colors(surfcolor_y)
vmin = min([sminz, sminy])
vmax = max([smaxz, smaxy])
# slice_y = get_the_slice(x, y, z, surfcolor_y)
axis = dict(
showbackground=True,
backgroundcolor="rgb(230, 230,230)",
gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
)
layout = dict(
title="Slices in volumetric data",
width=700,
height=700,
scene=dict(
xaxis=pgo.layout.scene.XAxis(axis),
yaxis=pgo.layout.scene.YAxis(axis),
zaxis=pgo.layout.scene.ZAxis(axis, range=[-2, 2]),
aspectratio=dict(x=1, y=1, z=1),
),
)
surfcolor_obl = volume(x, y, z)
smino, smaxo = get_lims_colors(surfcolor_obl)
vmin = min([sminz, smino])
vmax = max([smaxz, smaxo])
slice_obl = get_the_slice(x, y, z, surfcolor_obl)
slice_obl.update(cmin=vmin, cmax=vmax, showscale=True)
slice_z.update(cmin=vmin, cmax=vmax)
fig = pgo.Figure(data=[slice_z, slice_obl], layout=layout)
plotly.offline.plot(fig, filename="Slice-volumetric-2.html")
if __name__ == "__main__":
main()