-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbc_visualization.py
258 lines (240 loc) · 8.67 KB
/
rbc_visualization.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
"""
Module to convert RBC frames (npy files) to input required by Perseus software (for computing topological persistence)
created by kel 6/5/2012
New Cells: 110125, 130125, 140125, 40125, 50125
Old Cells: 100125, 120125, 50125, 90125
"""
import numpy
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools as it
import re
import scipy.io
import os
import cPickle as pkl
import rbc_processing as rp
def plot_frame( frame, outname=None ):
"""
Use pylab.imshow() to plot a frame (npy array).
Note: The boundary should be removed, thus there will not be a halo
"""
data = numpy.load(frame)
fig = plt.figure()
#plt.title("RBC frame")
plt.imshow(data)
plt.colorbar()
#plt.show()
if outname:
fig.savefig( outname )
def plot_block_frame( frame, ind ):
"""
Use pylab.imshow() to plot a frame (npy array).
Note: The boundary should be removed, thus there will not be a halo
"""
data = numpy.load(frame)[:,:,ind]
fig = plt.figure()
plt.title("RBC frame")
plt.imshow(data)
plt.colorbar()
plt.show()
def plot_frame_std( frame ):
"""
Use pylab.imshow() to plot a frame (npy array).
Note: The boundary should be removed, thus there will not be a halo
"""
data = numpy.load(frame)
fig = plt.figure()
plt.title("RBC frame")
plt.imshow(data,vmin=0,vmax=3750)
plt.colorbar()
plt.show()
def plot_block( files, m, n ):
"""
- files is directory for cell
- m starting index, n ending index
Use pylab.imshow() to plot a frame (npy array).
Note: The boundary should be removed, thus there will not be a halo
"""
#Get all frames
if os.path.isdir (files):
fdir = files + '/'
dlist = os.listdir(fdir)
frames = []
for f in dlist:
if f.endswith('npy') and not os.path.isdir(fdir+f):
frames.append(f)
else:
print 'Error - input is not a directory'
return
#Sort frames
frames . sort(key=natural_key)
stack = []
#load npy arrays
for ind in xrange(len(frames)):
if ind >= m and ind <= n:
stack.append(numpy.load(files + frames[ind]))
#Create 3d array
d = numpy.dstack(stack)
d = numpy.rollaxis(d,-1)
fig = plt.figure()
plt.title("RBC Stack")
plt.imshow(d[1])
plt.colorbar()
plt.show()
def plot_sublevels (file, persFile, output, interval, mask=False):
"""
plots sublevel sets for specified numpy file
from the height values in associated persFile
output is directory for output files (indv. names come from file + height)
interval provides info on what frames to save
(i.e. interval==2 means every other frame )
if mask==True all sublevel values are set to 1
"""
if not output.endswith('/'):
output+='/'
with open(persFile, 'r') as f:
s = f.read()
f.close()
data = numpy.load(file)
heightList = [x[1:-1] for x in re.findall('\[[^\]]*\]',s)]
#d = numpy.where(data>int(heightList[100]))
#data[d] = 0
#rp . save_npy_as_png(data,output+file.split('/')[-1].rstrip('.npy'))
for ind, h in enumerate(heightList):
if ind % interval == 0:
temp = data.copy()
temp[numpy.where(temp>int(h))] = 0
if mask:
temp[numpy.where(temp!=0)] = 1
outName = file.split('/')[-1].rstrip('.npy') + '_' + h
rp . save_npy_as_png ( temp, output + outName)
def plot_sublevels_comp (file, slice, output, interval, persFile='NULL', mask=False):
"""
plots sublevel sets for specified numpy file
from the height values in associated persFile
output is directory for output files (indv. names come from file + height)
interval provides info on what frames to save
(i.e. interval==2 means every other frame )
if mask==True all sublevel values are set to 1
"""
if not output.endswith('/'):
output+='/'
data = numpy.load(file)[slice]
if persFile != 'NULL':
with open(persFile, 'r') as f:
s = f.read()
f.close()
heightList = [x[1:-1] for x in re.findall('\[[^\]]*\]',s)]
else:
tempMin = data[numpy.where(data>data.min())].min()
avg = (data.max() - tempMin) / 50
heightList = numpy.arange(tempMin,data.max(), avg)
#d = numpy.where(data>int(heightList[100]))
#data[d] = 0
#rp . save_npy_as_png(data,output+file.split('/')[-1].rstrip('.npy'))
for ind, h in enumerate(heightList):
if ind % interval == 0:
temp = data.copy()
temp[numpy.where(temp>int(h))] = 0
if mask:
temp[numpy.where(temp!=0)] = 1
outName = file.split('/')[-1].rstrip('.npy') + '_' + str(slice) +'_' + str(h)
rp . save_npy_as_png ( temp, output + outName +'.png')
def plot_interlevels (file, persFile, output, interval, r, mask=False):
"""
plots interlevels sets for specified numpy file
from the height values in associated persFile (USE .BETTI FILE)
output is directory for output files (indv. names come from file + height)
interval provides info on what frames to save
(i.e. interval==2 means every other frame )
r perburation to height function (use half of desired window)
Use half interval to capture every height value (if all height values present)
if mask==True all sublevel values are set to 1
"""
if not output.endswith('/'):
output+='/'
with open(persFile, 'r') as f:
s = f.read()
f.close()
data = numpy.load(file)
heightList = [x[1:-1] for x in re.findall('\[[^\]]*\]',s)]
#d = numpy.where(data>int(heightList[100]))
#data[d] = 0
#rp . save_npy_as_png(data,output+file.split('/')[-1].rstrip('.npy'))
for ind, h in enumerate(heightList):
if ind % interval == 0:
temp = data.copy()
lower = int(h) - r
upper = int(h) + r
temp[(temp>upper)|(temp<lower)] = 0
if mask:
temp[numpy.where(temp!=0)] = 1
outName = file.split('/')[-1].rstrip('.npy') + '_'+h+'-'+str(lower)+'-'+str(upper)
rp . save_npy_as_png ( temp, output + outName + '.png')
def create_3d_sublevels( folder, file, frameList, heightList, output, ext='npy' ):
"""
This function creates .mats to view with Matlab or .npy
- folder is directory to cells, ends in '/'
- file is category of cell, ex. new_110125
- frameList is numbers of frames to calculate on
- heightList is list of heights for sublevel sets
- output is output file for .mat files
- ext is extension '.npy' or 'm'
"""
#correcting data
if folder.endswith('/') == False:
folder = folder + '/'
#processing variables
interm = '-concatenated-ASCII_'
# for each height
for h in heightList:
#stack frames
stack = []
for f in frameList:
#frame processing
dir = folder + file + '/'
frame = dir + file + interm + str(f) + '.npy'
data = numpy.load(frame)
#find superlevel set and set to 0
d = numpy.where(data>h)
data[d] = 0
#place on stack
stack . append(data)
sublevel = numpy.dstack(stack)
#setting dimensions
sublevel = numpy.rollaxis(sublevel,-1)
#save for visualization in sage or matlab
if ext == 'npy':
numpy.save(output + '_height_' + str(h), sublevel)
elif ext == 'pkl':
with open( output + '_height_' + str(h), 'w') as fh:
pkl.dump( sublevel, fh )
else:
scipy.io.savemat(output + '.mat', mdict={'arr':sublevel})
def plot_frame_3d ( folder, file, list, output):
"""
Use pylab.imshow() to plot a wire frame of npy array,
where height > 0 in 3 dimensions.
"""
interm = '-concatenated-ASCII_'
if not file.endswith('/'):
file+=('/')
for l in list:
fig = plt.figure()
data = numpy.load(folder+file+interm+str(l)+'.npy')
d = numpy.where(data>0)
xlist = d[0]
ylist = d[1]
z = data[d]
fig = plt.figure()
ax = Axes3D(fig)
ax.title('RBC Frame 3d ' + frame)
ax.plot(xlist,ylist,z)
plt.savefig(output + file + '_' + str(l))
def natural_key(string_):
"""
Use with frames.sort(key=natural_key)
"""
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]