Installation¶
PtyLab.py requires Python 3.11 or newer and is distributed on PyPI. Install it in a virtual environment, for example with conda or uv venv.
If you are contributing to the library instead, see the contributing guide.
From PyPI¶
Tip
The commands below use uv, which installs faster. With plain pip, drop the uv prefix.
Optional extras¶
| Extra | Installs | Needed for |
|---|---|---|
gpu |
CuPy (CUDA 12) | GPU reconstruction |
torch |
PyTorch | differentiable ptychography (GradientEngine) |
gui |
pyqtgraph, PySide6 | the interactive Qt viewer |
Verify GPU detection¶
After installing with the gpu extra, run:
It lists the GPUs that CuPy and PyTorch (if installed) find, with their CUDA versions, and fails if a library cannot use the GPU.
PtyLab uses the GPU when CuPy finds one; set params.gpuSwitch = False to run on the CPU. For large datasets, call engine.convert2single() before reconstruct() to use single precision, which halves memory use.
Differentiable ptychography¶
GradientEngine reconstructs with PyTorch gradients, so custom models, losses and constraints can be used. Install it with the torch extra:
If there are installation issues on your system, see the PyTorch installation guide.
Interactive viewer (Qt)¶
With the gui extra, show3Dslider opens a Qt viewer outside a notebook; otherwise it falls back to a matplotlib slider. On a remote machine the window needs a display, such as a remote desktop or ssh -X.
Latest unreleased version¶
To install the latest unreleased changes on main directly from GitHub:
Extras work the same way, for example with GPU support: