0
# Visualization Tools
1
2
Specialized plotting functions for electron microscopy data with calibrated axes, FFT displays, interactive stack viewing, and scientific visualization tools optimized for microscopy data analysis.
3
4
## Capabilities
5
6
### Core Plotting Functions
7
8
Main plotting interface and basic visualization functions for microscopy data structures.
9
10
```python { .api }
11
def plot(data_dict, **kwargs):
12
"""
13
Easy plotting of data structures from ncempy readers.
14
15
Parameters:
16
- data_dict: dict, data structure from ncempy.io.read()
17
- **kwargs: additional matplotlib arguments
18
19
Returns:
20
matplotlib figure and axes objects
21
"""
22
23
def imsd(image, num_sd=2, **kwargs):
24
"""
25
Display image with standard deviation scaling.
26
27
Parameters:
28
- image: numpy.ndarray, input image
29
- num_sd: float, number of standard deviations for scaling
30
- **kwargs: additional matplotlib arguments
31
32
Returns:
33
matplotlib image object
34
"""
35
```
36
37
### Calibrated Image Display
38
39
Functions for displaying images with proper spatial calibration and units.
40
41
```python { .api }
42
def im_calibrated(image, pixel_size, pixel_unit='nm', **kwargs):
43
"""
44
Display image with calibrated axes showing physical dimensions.
45
46
Parameters:
47
- image: numpy.ndarray, input image
48
- pixel_size: list or float, pixel size for each dimension
49
- pixel_unit: str or list, units for pixel size
50
- **kwargs: additional matplotlib arguments
51
52
Returns:
53
matplotlib image object with calibrated axes
54
"""
55
```
56
57
### FFT and Frequency Domain Visualization
58
59
Specialized functions for displaying Fourier transforms as diffraction patterns.
60
61
```python { .api }
62
def imfft(image, **kwargs):
63
"""
64
Display 2D FFT as diffractogram with proper scaling.
65
66
Parameters:
67
- image: numpy.ndarray, input image
68
- **kwargs: additional matplotlib arguments
69
70
Returns:
71
matplotlib image object showing FFT magnitude
72
"""
73
74
def imrfft(image, **kwargs):
75
"""
76
Display 2D real FFT as diffractogram.
77
78
Parameters:
79
- image: numpy.ndarray, input image
80
- **kwargs: additional matplotlib arguments
81
82
Returns:
83
matplotlib image object showing real FFT
84
"""
85
86
def im_and_fft(image, **kwargs):
87
"""
88
Show image and its FFT side by side.
89
90
Parameters:
91
- image: numpy.ndarray, input image
92
- **kwargs: additional matplotlib arguments
93
94
Returns:
95
matplotlib figure with two subplots
96
"""
97
```
98
99
### Interactive Stack Viewing
100
101
Class for interactive visualization of image stacks with slider controls.
102
103
```python { .api }
104
class stack_view:
105
"""
106
Interactive stack viewing with slider control.
107
108
Attributes:
109
- stack: numpy.ndarray, 3D image stack (frames, y, x)
110
- fig: matplotlib.figure.Figure, figure object
111
- ax: matplotlib.axes.Axes, axes object
112
- slider: matplotlib.widgets.Slider, frame slider
113
"""
114
115
def __init__(self, stack, **kwargs):
116
"""
117
Initialize interactive stack viewer.
118
119
Parameters:
120
- stack: numpy.ndarray, 3D image stack
121
- **kwargs: additional matplotlib arguments
122
"""
123
124
def update_frame(self, frame_number):
125
"""
126
Update displayed frame.
127
128
Parameters:
129
- frame_number: int, frame index to display
130
"""
131
132
def show(self):
133
"""Display the interactive viewer."""
134
```
135
136
### Peak and Point Visualization
137
138
Functions for overlaying detected features on microscopy images.
139
140
```python { .api }
141
def plot_points(image, points, point_size=50, point_color='red', **kwargs):
142
"""
143
Plot detected points overlaid on image.
144
145
Parameters:
146
- image: numpy.ndarray, background image
147
- points: numpy.ndarray, point coordinates (N, 2)
148
- point_size: float, marker size
149
- point_color: str, marker color
150
- **kwargs: additional matplotlib arguments
151
152
Returns:
153
matplotlib figure with points overlaid
154
"""
155
```
156
157
### Polar and Ring Analysis Visualization
158
159
Specialized plotting functions for diffraction ring analysis and polar coordinate data.
160
161
```python { .api }
162
def plot_ringpolar(image, center, points, **kwargs):
163
"""
164
Plot points in polar coordinates relative to center.
165
166
Parameters:
167
- image: numpy.ndarray, background image
168
- center: tuple, center coordinates (y, x)
169
- points: numpy.ndarray, point coordinates
170
- **kwargs: additional matplotlib arguments
171
172
Returns:
173
matplotlib figure with polar coordinate overlay
174
"""
175
176
def plot_distpolar(image, distortion_params, calibration_points, **kwargs):
177
"""
178
Plot distortion fitting results in polar coordinates.
179
180
Parameters:
181
- image: numpy.ndarray, background image
182
- distortion_params: dict, fitted distortion parameters
183
- calibration_points: numpy.ndarray, calibration points
184
- **kwargs: additional matplotlib arguments
185
186
Returns:
187
matplotlib figure showing distortion correction
188
"""
189
```
190
191
### Radial Profile Visualization
192
193
Functions for displaying radial intensity profiles and fitting results.
194
195
```python { .api }
196
def plot_radialprofile(radii, intensities, fit_result=None, **kwargs):
197
"""
198
Plot radial intensity profile.
199
200
Parameters:
201
- radii: numpy.ndarray, radial coordinates
202
- intensities: numpy.ndarray, intensity values
203
- fit_result: dict, optional fitting results to overlay
204
- **kwargs: additional matplotlib arguments
205
206
Returns:
207
matplotlib figure with radial profile
208
"""
209
210
def plot_fit(x_data, y_data, fit_function, fit_params, **kwargs):
211
"""
212
Plot experimental data with fitted function overlay.
213
214
Parameters:
215
- x_data: numpy.ndarray, x coordinates
216
- y_data: numpy.ndarray, y values
217
- fit_function: callable, fitted function
218
- fit_params: dict, fitted parameters
219
- **kwargs: additional matplotlib arguments
220
221
Returns:
222
matplotlib figure with data and fit
223
"""
224
```
225
226
## Usage Examples
227
228
### Basic Data Visualization
229
230
```python
231
import ncempy
232
import ncempy.viz as viz
233
import matplotlib.pyplot as plt
234
235
# Read and plot microscopy data
236
data = ncempy.read('image.dm4')
237
viz.plot(data)
238
plt.title('Electron Microscopy Image')
239
plt.show()
240
241
# Display with calibrated axes
242
viz.im_calibrated(data['data'], data['pixelSize'], data['pixelUnit'])
243
plt.title('Calibrated Image')
244
plt.show()
245
```
246
247
### FFT Visualization
248
249
```python
250
import ncempy.viz as viz
251
import matplotlib.pyplot as plt
252
253
# Show image and its FFT
254
viz.im_and_fft(image_data)
255
plt.show()
256
257
# Display just the diffractogram
258
viz.imfft(image_data, cmap='hot')
259
plt.title('Diffraction Pattern')
260
plt.show()
261
```
262
263
### Interactive Stack Viewing
264
265
```python
266
import ncempy.viz as viz
267
268
# Load image stack (3D array)
269
stack_data = load_image_stack() # shape: (frames, height, width)
270
271
# Create interactive viewer
272
viewer = viz.stack_view(stack_data)
273
viewer.show()
274
```
275
276
### Peak Overlay Visualization
277
278
```python
279
import ncempy.algo as algo
280
import ncempy.viz as viz
281
import matplotlib.pyplot as plt
282
283
# Detect peaks
284
peaks = algo.peakFind2D(diffraction_image)
285
286
# Plot peaks on image
287
viz.plot_points(diffraction_image, peaks,
288
point_size=100, point_color='yellow')
289
plt.title('Detected Diffraction Peaks')
290
plt.show()
291
```
292
293
### Radial Profile Analysis
294
295
```python
296
import ncempy.algo as algo
297
import ncempy.viz as viz
298
import matplotlib.pyplot as plt
299
300
# Calculate radial profile
301
center = (256, 256) # image center
302
radii, intensities = algo.calc_radialprofile(image, center)
303
304
# Fit and plot
305
fit_result = algo.fit_radialprofile(radii, intensities, model_function)
306
viz.plot_radialprofile(radii, intensities, fit_result)
307
plt.title('Radial Intensity Profile')
308
plt.show()
309
```
310
311
### Advanced Visualization with Multiple Features
312
313
```python
314
import ncempy.viz as viz
315
import matplotlib.pyplot as plt
316
317
# Create figure with multiple subplots
318
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
319
320
# Original image
321
axes[0,0].imshow(image_data)
322
axes[0,0].set_title('Original Image')
323
324
# FFT
325
viz.imfft(image_data, ax=axes[0,1])
326
axes[0,1].set_title('FFT')
327
328
# With detected peaks
329
viz.plot_points(image_data, detected_peaks, ax=axes[1,0])
330
axes[1,0].set_title('Detected Peaks')
331
332
# Radial profile
333
viz.plot_radialprofile(radii, intensities, ax=axes[1,1])
334
axes[1,1].set_title('Radial Profile')
335
336
plt.tight_layout()
337
plt.show()
338
```
339
340
### Customization Options
341
342
```python
343
import ncempy.viz as viz
344
import matplotlib.pyplot as plt
345
346
# Custom colormap and scaling
347
viz.imsd(image_data, num_sd=3, cmap='viridis',
348
interpolation='bilinear')
349
350
# Custom point visualization
351
viz.plot_points(image_data, peaks,
352
point_size=75, point_color='cyan',
353
alpha=0.8, marker='x')
354
355
# Custom calibrated display
356
viz.im_calibrated(image_data, pixel_size=[0.1, 0.1],
357
pixel_unit='nm', cmap='gray',
358
origin='lower')
359
plt.xlabel('X Position (nm)')
360
plt.ylabel('Y Position (nm)')
361
plt.show()
362
```