-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualize.py
312 lines (217 loc) · 9.37 KB
/
visualize.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
"""Functions to create and/or save plots of images and fit results.
"""
import numpy as np
import scipy as sp
import matplotlib as mpl
import pylab
from imageprocess import calc_absimage
def show_fitresult(rcoord, od_prof, fit_prof, T, N, figname=None, showfig=False):
"""Plot the result of fitting an image.
The figure that is generated contains a single plot of the profile of the
optical density plus the fit. Also displayed are the temperature and
number of atoms as determined by the fit and a legend.
**Inputs**
* rcoord: 1D array containing the radial coordinate
* od_prof: 1D array containing the radially averaged optical density
profile
* fit_prof: 1D array containing the fit to the optical density
* T: temperature as determined by the fit
* N: number of atoms as determined by the fit
**Outputs**
* fig: matplotlib figure instance
**Optional inputs**
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
"""
fig = pylab.figure()
ax1 = fig.add_subplot(111)
ax1.plot(rcoord, od_prof, 'b-', label=r'data')
ax1.plot(rcoord, fit_prof, 'r-', label=r'$n_{2D}(r)$ fit')
ax1.set_xlabel(r'$r$ [pix]')
ax1.set_ylabel(r'$OD$')
ax1.legend()
ax1.text(0.7,0.5, r'$T/T_F=$ %1.2f'%T, color='r', transform=ax1.transAxes)
ax1.text(0.7, 0.4, r'$N=$ %1.1f $\cdot10^6$'%(N*1e-6), color='r', \
transform=ax1.transAxes)
_save_or_show(figname=figname, showfig=showfig)
return fig
def show_fitresult_errorbars(rcoord, od_prof, fit_profs, T, N, linestyles=None,
Terr=[0.03], figname=None, showfig=False):
"""Plot the result of fitting an image.
The figure that is generated contains a single plot of the profile of the
optical density plus the fit. Also displayed are the temperature and
number of atoms as determined by the fit and a legend.
**Inputs**
* rcoord: 1D array containing the radial coordinate
* od_prof: 1D array containing the radially averaged optical density
profile
* fit_profs: list of 1D arrays, each 1D array is a fit to the optical
density, the first one the optimal fit, the next ones
give (graphical) error bars by fitting with fixed T/T_F
* T: temperature as determined by the fit
* N: number of atoms as determined by the fit
**Outputs**
* fig: matplotlib figure instance
**Optional inputs**
* linestyles: list of str, list of the same length as `fit_profs`, with
each string a plot style for the line
(i.e. 'r-' for a red solid line)
* Terr: list of floats, one value for each two profiles in fit_profs
that give the width of the error lines.
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
"""
if not linestyles:
linestyles = ['r-']
for i in range(len(fit_profs)-1):
linestyles.append('r--')
proflabels = []
for num in Terr:
proflabels.append(r'$T/T_F\pm%s$'%str(num))
proflabels.append(None)
print proflabels
fig = pylab.figure()
ax1 = fig.add_subplot(111)
ax1.plot(rcoord, od_prof, 'b-', lw=1.5, label=r'data')
ax1.plot(rcoord, fit_profs[0], linestyles[0], label=r'$n_{2D}(r)$ fit')
for prof, lstyle, label in zip(fit_profs[1:], linestyles[1:], proflabels):
ax1.plot(rcoord, prof, lstyle, label=label)
ax1.set_xlabel(r'$r$ [pix]')
ax1.set_ylabel(r'$OD$')
ax1.legend()
ax1.text(0.7,0.5, r'$T/T_F=$ %1.2f'%T, color='r', transform=ax1.transAxes)
ax1.text(0.7, 0.4, r'$N=$ %1.1f $\cdot10^6$'%(N*1e-6), color='r', \
transform=ax1.transAxes)
_save_or_show(figname=figname, showfig=showfig)
return fig
def show_rawframes(rawdata, figname=None, showfig=False):
"""Plots the transmission image together with the 3 raw images
**Inputs**
* rawdata: 3D array, containing pwa, pwoa, df. Last dimension is frame
index
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
**Outputs**
* fig: matplotlib figure instance
"""
transimg, odimg = calc_absimage(rawdata)
aspect = transimg.shape[0]/float(transimg.shape[1])
fig = pylab.figure(figsize=(8, 8*aspect))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.imshow(transimg, cmap=pylab.cm.gray, vmin=0, vmax=1.35)
ax2.imshow(rawdata[:, :, 0], cmap=pylab.cm.gray)
ax3.imshow(rawdata[:, :, 1], cmap=pylab.cm.gray)
ax4.imshow(rawdata[:, :, 2], cmap=pylab.cm.gray)
for ax in [ax1, ax2, ax3, ax4]:
ax.set_xticks([])
ax.set_yticks([])
_save_or_show(figname=figname, showfig=showfig)
return fig
def show_transimg(img, vmin=0, vmax=1.35, cmap=None, com=None, colorbar=False,
figname=None, showfig=False):
"""Show a single transmission image
**Inputs**
* img: 2D array, the image data
* vmin: float, the minimum value of the colormap
* vmax: float, the maximum value of the colormap
* cmap: instance, a matplotlib colormap instance. The default is
a gray colormap.
* com: 1D array, containing the coordinates of the center of mass. If
com is supplied it is plotted as a red cross.
* colorbar: bool, if True a color scale is displayed
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
**Outputs**
* fig: matplotlib figure instance
"""
if not cmap:
cmap = pylab.cm.gray
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.imshow(img, cmap=cmap, vmin=vmin, vmax=vmax)
ax.set_xticks([])
ax.set_yticks([])
if com:
ax.plot([com[1]], [com[0]], 'w+', ms=10, zorder=5)
if colorbar:
fig.colorbar(img)
_save_or_show(figname=figname, showfig=showfig)
return fig
def contourplot(img, numlines=100, filter=None, figname=None, showfig=False):
"""Aply a Gaussian filter and show the image with contour lines
**Inputs**
* img: 2D array, containing the image
* numlines: int, the number of contour lines
* filter: float, size of Gaussian filter in pixels. For more details,
see the scipy.ndimage docs.
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
**Outputs**
* fig: matplotlib figure instance, the contour plot
"""
if filter:
img = sp.ndimage.gaussian_filter(img, filter)
aspect = img.shape[1]/float(img.shape[0])
fig = pylab.figure(figsize=(12, 12*aspect))
ax = fig.add_subplot(111)
ax.contourf(img, numlines, interpolation='nearest', cmap=pylab.cm.hot)
ax.contour(img, numlines, interpolation='nearest', cmap=pylab.cm.Accent)
_save_or_show(figname=figname, showfig=showfig)
return fig
def show_img_and_com(img, com, cmap=pylab.cm.gray, figname=None, showfig=False):
"""Show the image and mark the center of mass with a cross
**Inputs**
* img: 2D array, containing the image
* com: sequence, containing the two coordinates of the center of mass
* cmap: colormap, a valid colormap from the matplotlib.cm module
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
**Outputs**
* fig: matplotlib figure instance, the contour plot
"""
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot([com[1]], [com[0]], 'r+', ms=15, zorder=5)
ax.imshow(img, cmap=cmap)
_save_or_show(figname=figname, showfig=showfig)
return fig
def show_residuals(rcoord, od_prof, fit_prof, figname=None, showfig=False):
"""Plots the fit residuals
**Inputs**
* rcoord: 1D array, containing the radial coordinate
* od_prof: 1D array, containing the radially averaged optical density
profile
* fit_prof: 1D array, containing the fit to the optical density
**Outputs**
* fig: matplotlib figure instance
**Optional inputs**
* figname: str, if not None the figure is saved with this filename
* showfig: bool, if True pop up a figure with pylab.show()
"""
fig = pylab.figure(figsize=(12,4))
ax1 = fig.add_subplot(121)
ax1.plot(rcoord, od_prof - fit_prof, 'b-')
ax1.axhline(0, color='k', lw=0.5)
ax1.set_xlabel(r'$r$ [pix]')
ax1.set_ylabel(r'$OD_{data}-OD_{fit}$')
od_prof = np.where(od_prof<0, 1e-10, od_prof)
ax2 = fig.add_subplot(122)
ax2.semilogy(rcoord**2, od_prof, 'b-', label=r'data')
ax2.semilogy(rcoord**2, fit_prof, 'r-', label=r'$n_{2D}(r)$ fit')
ax2.set_ylim(1e-5, fit_prof.max()*2)
ax2.set_xlabel(r'$r^2$ [pix]')
ax2.set_ylabel(r'$OD$')
ax2.legend()
_save_or_show(figname=figname, showfig=showfig)
return fig
def _save_or_show(figname=None, showfig=False):
"""Save and/or show figure depending on inputs"""
if figname:
pylab.savefig(''.join([figname, '.png']))
pylab.close()
if showfig:
pylab.show()