PyNX: Python tools for Nano-structures Xtallography


PyNX stands for Python tools for Nano-structures Xtallography. It can be used for:

  • Coherent X-ray imaging simulation and analysis: coherent diffraction imaging (CDI), Ptychography, Wavefront propagation, near field and far field techniques…
  • Fast scattering calculations from large number of atoms and reciprocal space positions.

PyNX is fully optimised to use Graphical Processing Units, using either CUDA or OpenCL, to provide fast calculations with 1 to 3 orders of magnitude speedup compared to standard processor calculations.

PyNX scripts

PyNX can be used simply with command-line scripts for some applications (2D/3D CDI and 2D Ptychography). These can take generic files as input, such as CXI files (, or can analyse data directly from beamlines.

PyNX as a python toolkit

PyNX can be used as a python library with the following main modules:

  1. pynx.scattering: X-ray scattering computing using graphical processing units, allowing up to 2.5x10^11 reflections/atoms/seconds (single nVidia Titan X). The sub-module``pynx.scattering.gid`` can be used for Grazing Incidence Diffraction calculations, using the Distorted Wave Born Approximation
  2. pynx.ptycho : simulation and analysis of experiments using the ptychography technique, using GPU (OpenCL). Examples are available in the pynx/Examples directory. Scripts for analysis of raw data from beamlines are also available, as well as using or producing ptychography data sets in CXI (Coherent X-ray Imaging) format.
  3. pynx.wavefront: X-ray wavefront propagation in the near, far field, or continuous (examples available at the end of Also provided are sub-modules for Fresnel propagation and simulation of the illumination from a Fresnel Zone Plate, both using OpenCL for high performance computing.
  4. pynx.cdi: Coherent Diffraction Imaging reconstruction algorithms using GPU for Coherent Diffraction Imaging, in 2D or 3D, for small-angle or Bragg diffraction data. This uses either CUDA or OpenCL, but CUDA is strongly recommended for 3D data (significant speedup).


PyNX is available from:


Version 2018.X (2018-XX-XX)

  • CDI: id01 and id10 scripts will now print the algorithm chain used, when it is not user-supplied
  • Ptycho: enable CUDA operators, 2x speed improvements
  • Ptycho: Correct probe and object orientation and axis in plots, so that both are seen from the source
  • Ptycho: better handling of plots for near field Ptycho
  • [BUG] CDI: correct handling of smooth parameter in OpenCL SupportUpdate() operator
  • [BUG] CDI: correct handling of masked pixels when using auto-correlation to init the support (OpenCL)

Version 2018.2.0 (2018-07-17)

  • CDI: enable using partial coherence (GPU-optimised)
  • CDI runner: use algorithm steps based on operators, e.g. algorithm=’ER**50,(Sup*ER**5*HIO**50)**10’
  • CDI id01 runner: allow batch processing data from a spec file + scan numbers
  • CDI runner: use the scan number to save CXI files (data and output)
  • Ptycho: switch completely to the new operator-based API
  • Ptycho: switch scripts output to hdf5/CXI file format
  • Ptycho: add id16A runner (lambda detector)
  • Ptycho runner: add ability to substract a dark image
  • Ptycho runner: add orientation_round_robin option
  • Ptycho runner: use ‘mask=’ instead of ‘loadmask=’
  • Ptycho CXI runner: use ‘data=’ instead of ‘cxifile=’
  • Ptycho CXI runner: allow analysing several CXI data files using a generic manne: ‘data=data%05d.cxi scan=13,67,89’
  • Ptycho: improve display of phase
  • Ptycho API: add AnalyseProbe and OrthoProbe operators
  • Ptycho: plot ‘up’ correctly (flip up/down plotting with respect with previous version)

Version 3.6.3 (2018-03-21)

  • CDI: sample name, instrument and a note can be saved to CXI files
  • CDI: change FFT-scaling approach (lower noise from masked high-frequency pixels ?)
  • Ptycho id01 runner: read detector distance from UDETCALIB if available
  • [BUG] Ptycho: correct reading mask from hdf5
  • Wavefront: default to filling the wavefront with 1 instead of 0.
  • Wavefront: Add ability to start from a photo/image from scipy or skimage
  • Add benchmark module (pynx.test.speed)

Version 3.6.2 (2018-01-25)

  • Ptycho: id01 runner: add ‘livescan’ option to search for new data when analysing a given spec data file.
  • Ptycho runner: data2cxi will now export raw data, unless data2cxi=crop was used (corrected bug)
  • Use PYNX_PU environment variable to set language (CUDA/OpenCL/CPU) and/or gpu name and/or gpu rank
  • Ptycho and CDI: add CPU API (not yest accessible for ptycho runner scripts, only with new python API)

Version 3.6.1 (2017-12-19)

  • CDI runner: add roi= keyword to manually supply the region-of-interest.
  • CDI: add option to update the support based on the maximum value, instead of the average
  • CDI runner: add ‘support_post_expand’ keyword to shrink and/or expand the support by a few pixels after update
  • CDI: handle <0 observed intensities during initial scaling of object
  • CDI runner scripts: report poisson, gaussian and euclidian llk
  • CDI id01 runner script: add support for the Eiger detector
  • CDI: update examples
  • CDI runner: correctly take into account output_format keyword
  • CDI: correct some bugs with the OpenCL implementation
  • Ptycho: add operator-based python API (not yet used for command-line scripts)
  • Ptycho: add operator-based near field ptychography
  • Processing Unit API: allow to centrally select a GPU language and/or a device
  • Remove official support for Python 2.7. Now supporting Python>=3.4

See the full Changelog

Citation & Bibliography

If you use PyNX for scientific work, please consider including a citation:

  • If you use PyNX for coherent X-ray Imaging including CDI and ptychography:
  • If you use PyNX for GPU scattering calculations:
  • Cite the first PyNX article: J. Appl. Cryst. 44(2011), 635-640. A preprint version is also available on ArXiv:1010.2641
  • Give a link to the project:


The PyNX library is distributed with a CeCILL-B license (an open-source license similar to the FreeBSD one). See

Note that CPU computing of the pynx.scattering module uses the sse_mathfun.h header, which is distributed under the zlib license. See

See for more details about the license, copyright, as well as other possible issues regarding ptychography.


How to install PyNX (preferably using a python virtual environment)

Command-line scripts

Scripts Reference
Documentation of scripts included in PyNX

API Documentation

API Reference
Documentation of modules included in PyNX

Indices and tables