Monitoring¶
PtyLab.Monitor
¶
Monitor
¶
AbstractMonitor
¶
Base class of the reconstruction monitors; on its own it shows nothing.
The engines call update with one MonitorFrame per update. A custom monitor
can either override update and read the frame directly, or override the
per-quantity hooks below (updateObjectProbeErrorMonitor, update_encoder,
...), which the default update calls. Every hook is a no-op here, so a
subclass only implements the ones it uses.
Source code in PtyLab/Monitor/Monitor.py
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 | |
update(frame)
¶
Show one snapshot of the reconstruction.
The default forwards the frame to the per-quantity hooks, so monitors
written against those hooks keep working. Fields the engine left empty
(None) are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
MonitorFrame
|
Current reconstruction state. |
required |
Source code in PtyLab/Monitor/Monitor.py
initializeMonitors()
¶
saveFigures(fileName='recent', dpi=150)
¶
Save the monitor figures as PNG files. This monitor has none.
Returns:
| Type | Description |
|---|---|
list[Path]
|
Empty, as nothing is saved. |
describe_parameters(params)
¶
Record the reconstruction parameters, e.g. as text in a log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Params
|
Parameters of the reconstruction. |
required |
update_focusing_metric(TV_value, AOI_image, metric_name, allmerits=None)
¶
Show the result of one TV autofocus step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
TV_value
|
float
|
Focus metric at the current distance. |
required |
AOI_image
|
ndarray
|
Propagated field in the area of interest. |
required |
metric_name
|
str
|
Name of the focus metric, e.g. |
required |
allmerits
|
ndarray
|
Focus metric at every distance that was tried. Defaults to None. |
None
|
Source code in PtyLab/Monitor/Monitor.py
writeEngineName(name)
¶
Record which engine produced the current update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Engine name. |
required |
update_encoder(corrected_positions, original_positions, *args, **kwargs)
¶
Show the scan positions before and after position correction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corrected_positions
|
ndarray
|
Corrected encoder positions in m. |
required |
original_positions
|
ndarray
|
Measured encoder positions in m. |
required |
Source code in PtyLab/Monitor/Monitor.py
update_overlap(overlap_area, linear_overlap)
¶
Show the estimated probe overlap between neighbouring scan positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overlap_area
|
float
|
Area overlap, between 0 and 1. |
required |
linear_overlap
|
float
|
Linear overlap, between 0 and 1. |
required |
Source code in PtyLab/Monitor/Monitor.py
updateObjectProbeErrorMonitor(error, object_estimate, probe_estimate, zo=None, purity_probe=None, purity_object=None, encoder_positions=None)
¶
Show the object and probe estimates and the error metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
array - like
|
Error metric of every iteration so far. |
required |
object_estimate
|
ndarray
|
Object inside the display region. |
required |
probe_estimate
|
ndarray
|
Probe inside the display region. |
required |
zo
|
float
|
Sample-detector distance in m. Defaults to None. |
None
|
purity_probe
|
float
|
Probe purity. Defaults to None. |
None
|
purity_object
|
float
|
Object purity. Defaults to None. |
None
|
encoder_positions
|
ndarray
|
Scan positions in object pixels. Defaults to None. |
None
|
Source code in PtyLab/Monitor/Monitor.py
updateBeamWidth(beamwidth_y, beamwidth_x)
¶
Show the probe width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beamwidth_y
|
float
|
FWHM along y in m. |
required |
beamwidth_x
|
float
|
FWHM along x in m. |
required |
updateDiffractionDataMonitor(Iestimated, Imeasured)
¶
Show the estimated and measured intensity at the last scan position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iestimated
|
ndarray
|
Estimated detector intensity. |
required |
Imeasured
|
ndarray
|
Measured detector intensity. |
required |
Source code in PtyLab/Monitor/Monitor.py
Monitor
¶
Bases: AbstractMonitor
Live matplotlib monitor, in GUI windows or in Jupyter cell outputs.
Shows up to three figures:
- object, probe and error metric (
ObjectProbeErrorPlot), always - estimated vs measured intensity (
DiffractionDataPlot), forverboseLevel = "high" - zo, theta, purity and scan positions (
ParameterHistoryPlot), once the engine changes one of them and whileshowParameterHistoryis True
Attributes:
| Name | Type | Description |
|---|---|---|
figureUpdateFrequency |
int
|
Update the figures every this many iterations. |
verboseLevel |
str
|
|
objectPlot |
str
|
|
objectZoom |
float or None
|
Shrinks the shown object region around the
scanned area; |
probeZoom |
float or None
|
Same for the probe, around the pupil size. |
objectPlotContrast |
float
|
Brightness scaling of the complex object plot. |
probePlotContrast |
float
|
Brightness scaling of the complex probe plot. |
showParameterHistory |
bool
|
Whether to show the third figure. |
screenshot_directory |
str or None
|
If set, the first figure is saved
there after every update as |
Source code in PtyLab/Monitor/Monitor.py
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
initializeMonitors()
¶
Create the figures; a figure that already exists is reused.
Source code in PtyLab/Monitor/Monitor.py
update(frame)
¶
Draw the object, probe and error panels, plus the diffraction data when shown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
MonitorFrame
|
Current reconstruction state. |
required |
Source code in PtyLab/Monitor/Monitor.py
saveFigures(fileName='recent', dpi=150)
¶
Save the current monitor figures as PNG files.
Call it after engine.reconstruct() to keep the final state, or let
reconstruction.saveResults(fileName, snapshot=monitor) call it next to
the HDF5 file. Each figure that exists is written as
<fileName>_<figure>.png:
reconstruction: object, probe and error metricdiffraction: estimated vs measured intensity (verboseLevel = "high")history: zo, theta, purity and scan positions, if any of them changed- any window an engine registered in
extraFigures, under its key (e.g.purity_scanfrom purityPIE)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fileName
|
str or Path
|
Path prefix of the PNG files; missing
parent directories are created. Defaults to |
'recent'
|
dpi
|
int
|
Resolution of the PNG files. Defaults to 150. |
150
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
The files written. |
Source code in PtyLab/Monitor/Monitor.py
DummyMonitor
¶
Bases: AbstractMonitor
Monitor that shows nothing, for runs where plotting would only cost time.
Inherits every no-op hook from AbstractMonitor, so any hook an engine calls
(e.g. update_focusing_metric when params.TV_autofocus is on) exists here too.
Source code in PtyLab/Monitor/Monitor.py
NapariMonitor
¶
Bases: DummyMonitor
Shows the object, probe and diffraction data as layers in a napari viewer.
Needs napari, which is not a PtyLab dependency.
Source code in PtyLab/Monitor/Monitor.py
initializeVisualisation()
¶
Open the viewer and create its image layers.
Source code in PtyLab/Monitor/Monitor.py
tracked_values(frame)
¶
Scalars besides object and probe that an engine may change, in display units.
Quantities the frame does not carry (None) or that are undefined for this
reconstruction (purity of a single mode) are left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
MonitorFrame
|
Current reconstruction state. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, float]]
|
Panel title -> line label -> value. |
Source code in PtyLab/Monitor/Monitor.py
Plots
¶
ObjectProbeErrorPlot
¶
Bases: _LiveFigure
Figure with the object estimate, the probe estimate and the error metric.
Call updateObject, updateProbe and updateError with the current state,
then drawNow to show it. The first round of updates creates the images; later
ones only replace their data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figNum
|
int
|
pyplot figure number. Defaults to 1. |
1
|
Source code in PtyLab/Monitor/Plots.py
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 | |
updateObject(object_estimate, objectPlot, amplitudeScalingFactor=1, purity=None, **kwargs)
¶
Show the object estimate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_estimate
|
ndarray
|
Object, |
required |
objectPlot
|
str
|
|
required |
amplitudeScalingFactor
|
float
|
Brightness scaling of the complex plot. Defaults to 1. |
1
|
purity
|
float
|
Object purity, shown in the title; |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Source code in PtyLab/Monitor/Plots.py
updateProbe(probe_estimate, amplitudeScalingFactor=1, label='Probe estimate', purity=None, **kwargs)
¶
Show the probe (or, for FPM, pupil) estimate as a complex plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probe_estimate
|
ndarray
|
Probe, |
required |
amplitudeScalingFactor
|
float
|
Brightness scaling of the complex plot. Defaults to 1. |
1
|
label
|
str
|
Panel title. Defaults to |
'Probe estimate'
|
purity
|
float
|
Probe purity, shown in the title; |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Source code in PtyLab/Monitor/Plots.py
updateError(error_estimate)
¶
Plot the error metric of every iteration so far on log-log axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_estimate
|
array - like
|
One error value per completed iteration. |
required |
Source code in PtyLab/Monitor/Plots.py
DiffractionDataPlot
¶
Bases: _LiveFigure
Figure with the estimated and measured diffraction intensity, side by side.
Both are shown as \(\log_{10}(I + 1)\), with the colour limits of the measured intensity applied to the estimate so the two can be compared directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figNum
|
int
|
pyplot figure number. Defaults to 2. |
2
|
Source code in PtyLab/Monitor/Plots.py
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 313 | |
updateIestimated(Iestimate, cmap='gray', **kwargs)
¶
Show the estimated intensity; its colour limits are set by update_view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iestimate
|
ndarray
|
Estimated detector intensity, CPU or GPU. |
required |
cmap
|
str or Colormap
|
Colour map. Defaults to |
'gray'
|
Source code in PtyLab/Monitor/Plots.py
updateImeasured(Imeasured, cmap='gray', **kwargs)
¶
Show the measured intensity, with colour limits fitted to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Imeasured
|
ndarray
|
Measured detector intensity, CPU or GPU. |
required |
cmap
|
str or Colormap
|
Colour map. Defaults to |
'gray'
|
Source code in PtyLab/Monitor/Plots.py
update_view(Iestimated, Imeasured, cmap)
¶
Show both intensities with the colour limits of the measured one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iestimated
|
ndarray
|
Estimated detector intensity. |
required |
Imeasured
|
ndarray
|
Measured detector intensity. |
required |
cmap
|
str or Colormap
|
Colour map of both panels. |
required |
Source code in PtyLab/Monitor/Plots.py
ParameterHistoryPlot
¶
Bases: _LiveFigure
Traces of the quantities an engine changes besides object and probe.
Each update records one value per line, grouped into panels (e.g. the panel "purity [%]" with lines "object" and "probe"). A panel is only drawn once one of its lines has changed from its first value, so quantities the engine never touches (a fixed zo, a single-mode purity) stay hidden. The scan-position panel appears once position correction has moved a position.
The figure is created on the first draw that has something to show, so no window opens for a reconstruction that changes nothing.
Source code in PtyLab/Monitor/Plots.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
record(iteration, values)
¶
Append one value per line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iteration
|
int
|
Iteration the values belong to. |
required |
values
|
dict[str, dict[str, float]]
|
Panel title -> line label -> value. |
required |
Source code in PtyLab/Monitor/Plots.py
record_positions(measured, corrected)
¶
Keep the scan positions if position correction has moved any of them.
Source code in PtyLab/Monitor/Plots.py
active_panels()
¶
Titles of the panels whose values have changed, in recording order.
Source code in PtyLab/Monitor/Plots.py
draw()
¶
Redraw every active panel; does nothing while no panel is active.
Source code in PtyLab/Monitor/Plots.py
is_inline()
¶
Whether matplotlib draws inline, as in a Jupyter notebook.
Returns:
| Type | Description |
|---|---|
bool
|
True for an inline backend, False for a GUI or file backend. |
TensorboardMonitor
¶
TensorboardMonitor
¶
Bases: AbstractMonitor
Source code in PtyLab/Monitor/TensorboardMonitor.py
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
update_encoder(corrected_positions, original_positions, scaling=1.0, beamwidth=None)
¶
Update the stage position images. :param corrected_positions: :param original_positions: :param scaling: :return:
Source code in PtyLab/Monitor/TensorboardMonitor.py
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 | |
__smart_upload_image_couldbecomplex(name, data, step, max_outputs=3, description=None, center_phase=False)
¶
Safely upload an image that could be complex. If it is, cast it to colour before uploading.
Source code in PtyLab/Monitor/TensorboardMonitor.py
frame
¶
MonitorFrame
dataclass
¶
One snapshot of the reconstruction state, handed to a monitor.
The engine builds one frame per monitor update and passes it to
AbstractMonitor.update. The monitor decides what to show from it.
The array fields are references to the live reconstruction arrays (CPU or GPU),
not copies, so building a frame is cheap even when the monitor ignores it. They
are only valid during the update call; a monitor that keeps data for later
must copy it, e.g. through object_view and probe_view.
Attributes:
| Name | Type | Description |
|---|---|---|
error |
ndarray
|
Error metric of every iteration so far. |
object |
Any
|
|
probe |
Any
|
|
operation_mode |
str
|
|
object_roi |
tuple[slice, slice]
|
Region of the object to display. |
probe_roi |
tuple[slice, slice]
|
Region of the probe to display. |
object_pixel_size |
float
|
Axis step of the object panel in meters. |
probe_pixel_size |
float
|
Axis step of the probe panel, in meters for CPM and in 1/m for FPM. |
probe_axis_unit |
str
|
Unit of the probe panel axes. |
probe_label |
str
|
Title of the probe panel. |
nosm |
int
|
Number of object state mixtures. |
npsm |
int
|
Number of probe state mixtures. |
purity_object |
Any
|
Object purity, meaningful only when |
purity_probe |
Any
|
Probe purity, meaningful only when |
zo |
Any
|
Sample-detector distance in meters. |
theta |
Any
|
Reflection angle in degrees (aPIE), |
positions |
ndarray | None
|
Scan positions in object pixels. |
engine_name |
str | None
|
Name of the engine that produced the frame. |
encoder_original |
ndarray | None
|
Measured encoder positions. |
encoder_corrected |
ndarray | None
|
Encoder positions after position correction. |
beam_width |
tuple[float, float] | None
|
|
I_estimated |
ndarray | None
|
Estimated detector intensity at the
last scan position, only filled for |
I_measured |
ndarray | None
|
Measured detector intensity at the same position. |
area_overlap |
float | None
|
Estimated probe area overlap. |
linear_overlap |
float | None
|
Estimated probe linear overlap. |
Source code in PtyLab/Monitor/frame.py
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 | |
iteration
property
¶
Number of completed iterations.
from_reconstruction(reconstruction, **fields)
classmethod
¶
Fill the fields that follow from the reconstruction state.
The panel sampling comes from panel_geometry. The engine must have
initialised reconstruction.error first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconstruction
|
Reconstruction
|
Current reconstruction state. |
required |
**fields
|
Any
|
Further |
{}
|
Returns:
| Type | Description |
|---|---|
MonitorFrame
|
The snapshot. |
Source code in PtyLab/Monitor/frame.py
object_view()
¶
Real-space object inside object_roi, as a squeezed NumPy array.
Leading singleton axes of the 6D array (nlambda, nosm, 1, nslice, No, No)
are dropped, so a single-mode object comes back as (Ny, Nx).
Source code in PtyLab/Monitor/frame.py
probe_view()
¶
Probe inside probe_roi, as a squeezed NumPy array.
panel_geometry(reconstruction)
¶
Axis steps, units and labels of the object and probe panels.
FPM reconstructs the object spectrum and a pupil, so the panels use the FPM
object sampling dxo_fpm and the pupil-plane step dfp (a spatial frequency)
instead of dxo and dxp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconstruction
|
Reconstruction
|
Reconstruction whose sampling is shown. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The |