forked from ilyakava/pyfst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_plot.py
289 lines (242 loc) · 9.8 KB
/
window_plot.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
287
288
289
"""Plotting code for cube shaped objects.
Hyperspectral data and 3D filters
"""
import numpy as np
import plotly.offline as py
from plotly.graph_objs import *
import scipy.misc
import pdb
def get_the_slice(x,y,z, surfacecolor, colorscale='Hot', showscale=False):
# Greys,YlGnBu,Greens,YlOrRd,Bluered,RdBu,Reds,Blues,Picnic,Rainbow,Portland,Jet,Hot,Blackbody,Earth,Electric,Viridis,CividisGreys,YlGnBu,Greens,YlOrRd,Bluered,RdBu,Reds,Blues,Picnic,Rainbow,Portland,Jet,Hot,Blackbody,Earth,Electric,Viridis,Cividis
return Surface(x=x,# https://plot.ly/python/reference/#surface
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 str_to_filename(mystr):
return "".join([c for c in mystr if c.isalpha() or c.isdigit() or c==' ']).rstrip()
def pyplot_slices(surfcolor_z, surfcolor_y,surfcolor_x,title=None,resample_factor=50):
"""
Args:
surfcolor_z: img that has x on vertical, y on horiz
surfcolor_y: img that has x on vertical, z on horiz
surfcolor_x: img that has y on vertical, z on horiz
"""
surfcolor_z = scipy.misc.imresize(surfcolor_z, resample_factor*np.array(surfcolor_z.shape), interp='nearest')
surfcolor_y= scipy.misc.imresize(surfcolor_y, resample_factor*np.array(surfcolor_y.shape), interp='nearest')
surfcolor_x = scipy.misc.imresize(surfcolor_x, resample_factor*np.array(surfcolor_x.shape), interp='nearest')
x=np.linspace(-1,1, surfcolor_z.shape[0])
y=np.linspace(-1,1, surfcolor_z.shape[1])
y,x=np.meshgrid(y,x)
z=np.zeros(x.shape)
slice_z=get_the_slice(x,y,z, surfcolor_z)
x=np.linspace(-1,1, surfcolor_y.shape[0])
z=np.linspace(-1,1, surfcolor_y.shape[1])
z,x=np.meshgrid(z,x)
y=np.zeros(x.shape)
slice_y=get_the_slice(x,y,z, surfcolor_y)
y=np.linspace(-1,1, surfcolor_x.shape[0])
z=np.linspace(-1,1, surfcolor_x.shape[1])
z,y=np.meshgrid(z,y)
x=np.zeros(z.shape)
slice_x=get_the_slice(x,y,z, surfcolor_x)
# sminz, smaxz=get_lims_colors(surfcolor_z)
# sminy, smaxy=get_lims_colors(surfcolor_y)
# sminx, smaxx=get_lims_colors(surfcolor_x)
# vmin=min([sminz, sminy, sminx])
# vmax=max([smaxz, smaxy, smaxx])
# slice_z.update(cmin=vmin, cmax=vmax)
# slice_y.update(cmin=vmin, cmax=vmax)
# slice_x.update(cmin=vmin, cmax=vmax, showscale=True)
slice_x.update(showscale=True)
axis = dict(showbackground=True,
backgroundcolor="rgb(230, 230,230)",
gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
)
title = title or 'Slices in volumetric data'
layout = Layout(
title=title,
width=700,
height=700,
scene=Scene(xaxis=XAxis(axis),
yaxis=YAxis(axis),
zaxis=ZAxis(axis),
aspectratio=dict(x=1,
y=1,
z=1
),
)
)
fig=Figure(data=Data([slice_z,slice_y,slice_x]), layout=layout)
py.plot(fig, filename=str_to_filename(title)+'.html', auto_open=False)
def pyplot_cube(surfcolor_z, surfcolor_y,surfcolor_x,title=None,resample_factor=50):
"""
Args:
surfcolor_z: img that has x on vertical, y on horiz
surfcolor_y: img that has x on vertical, z on horiz
surfcolor_x: img that has y on vertical, z on horiz
"""
if resample_factor != 1:
surfcolor_z = scipy.misc.imresize(surfcolor_z, resample_factor*np.array(surfcolor_z.shape), interp='nearest')
surfcolor_y= scipy.misc.imresize(surfcolor_y, resample_factor*np.array(surfcolor_y.shape), interp='nearest')
surfcolor_x = scipy.misc.imresize(surfcolor_x, resample_factor*np.array(surfcolor_x.shape), interp='nearest')
x=np.linspace(-1,1, surfcolor_z.shape[0])
y=np.linspace(-1,1, surfcolor_z.shape[1])
y,x=np.meshgrid(y,x)
z=np.ones(x.shape)
slice_z=get_the_slice(x,y,z, surfcolor_z)
x=np.linspace(-1,1, surfcolor_y.shape[0])
z=np.linspace(-1,1, surfcolor_y.shape[1])
z,x=np.meshgrid(z,x)
y=np.ones(x.shape)
slice_y=get_the_slice(x,y,z, surfcolor_y)
y=np.linspace(-1,1, surfcolor_x.shape[0])
z=np.linspace(-1,1, surfcolor_x.shape[1])
z,y=np.meshgrid(z,y)
x=np.ones(z.shape)
slice_x=get_the_slice(x,y,z, surfcolor_x)
# sminz, smaxz=get_lims_colors(surfcolor_z)
# sminy, smaxy=get_lims_colors(surfcolor_y)
# sminx, smaxx=get_lims_colors(surfcolor_x)
# vmin=min([sminz, sminy, sminx])
# vmax=max([smaxz, smaxy, smaxx])
# slice_z.update(cmin=vmin, cmax=vmax)
# slice_y.update(cmin=vmin, cmax=vmax)
# slice_x.update(cmin=vmin, cmax=vmax, showscale=True)
slice_x.update(showscale=True)
axis = dict(showbackground=True,
backgroundcolor="rgb(230, 230,230)",
gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
)
title = title or 'Slices in volumetric data'
layout = Layout(
title=title,
width=700,
height=700,
scene=Scene(xaxis=XAxis(axis),
yaxis=YAxis(axis),
zaxis=ZAxis(axis),
aspectratio=dict(x=1,
y=1,
z=1
),
)
)
fig=Figure(data=Data([slice_z,slice_y,slice_x]), layout=layout)
py.plot(fig, filename=str_to_filename(title)+'.html', auto_open=False)
def make_slice_plots():
for scale in [0,1,2]:
for nu in [0,1,2]:
for kappa in [0,1,2]:
cube = tang_psi_window_3D(scale, nu*np.pi/3, kappa*np.pi/3, [7,7,7])
cube = np.imag(cube)
title = 'j=%d, nu=%d, kappa=%d' % (scale, nu, kappa)
pyplot_slices(cube[:,:,3], cube[:,3,:], cube[3,:,:], title=title)
def pyplot_3dscatter(vals, locs, title=None):
"""
Example usage:
[vals, locs] = tang_psi_window_3D_flat(1, 1*np.pi/3, 1*np.pi/3, [7,7,7])
vals = np.imag(vals)
pyplot_3dscatter(vals, locs)
"""
trace1 = Scatter3d(
x=locs[:,0],
y=locs[:,1],
z=locs[:,2],
mode='markers',
marker=dict(
size=90*np.abs(vals)/vals.max(),
color=vals,
colorscale='Hot',
opacity=0.9
)
)
data = [trace1]
axis = dict(showbackground=True,
backgroundcolor="rgb(230, 230,230)",
gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
)
title = title or '3D Scatter Plot with Colorscaling'
layout = Layout(
title=title,
width=700,
height=700,
# margin=dict(
# l=0,
# r=0,
# b=0,
# t=0
# ),
scene=Scene(xaxis=XAxis(axis),
yaxis=YAxis(axis),
zaxis=ZAxis(axis),
)
)
fig = Figure(data=data, layout=layout)
py.plot(fig, filename=str_to_filename(title)+'.html', auto_open=False)
def make_3dscatter_plots():
for scale in [0,1,2]:
for nu in [0,1,2]:
for kappa in [0,1,2]:
[vals, locs] = tang_psi_window_3D_flat(scale, nu*np.pi/3, kappa*np.pi/3, [7,7,7])
vals = np.imag(vals)
pyplot_3dscatter(vals, locs)
title = 'j=%d, nu=%d, kappa=%d' % (scale, nu, kappa)
pyplot_3dscatter(vals, locs, title=title)
class ScrollThruPlot(object):
"""For scrolling through layers of 3d vis
https://matplotlib.org/2.1.2/gallery/animation/image_slices_viewer.html
Example usage:
import matplotlib.pyplot as plt
X = np.real(cube)
fig, ax = plt.subplots(1, 1)
tracker = ScrollThruPlot(ax, X)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()
"""
def __init__(self, ax, X, fig):
self.fig = fig
self.ax = ax
self.ax.set_title('Use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = 0
# if you init with a blank images, all images will be blank
init_img = np.random.rand(self.X.shape[0], self.X.shape[1])
self.im = ax.imshow(init_img, cmap='gray')
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
# self.im.cla()
self.im.set_data(self.X[:, :, self.ind] / np.abs(self.X[:, :, self.ind]).max())
# self.fig.colorbar(self.im)
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
import os
import scipy.io as sio
DATASET_PATH = '/scratch0/ilya/locDoc/data/hyperspec/datasets'
def make_dataset_cube_plot():
mat_contents = sio.loadmat(os.path.join(DATASET_PATH, 'Pavia_center_right.mat'))
data = mat_contents['Pavia_center_right'].astype(np.float32)
data /= np.max(np.abs(data))
pyplot_cube(data[:,-1,:], data[:,:,51], np.rot90(data[0,:,:]), title='title',resample_factor=1)
# top, right, left
import windows as win
def display_3dfilter():
cube = win.fst3d_psi_window_3D(0, 0, 1/7., [7,7,7])
cube = np.imag(cube)
pyplot_slices(cube[:,:,3], cube[:,3,:], cube[3,:,:])
if __name__ == '__main__':
make_dataset_slice_plots()