PyNX: Python tools for Nano-structures Xtallography

NEWS

  • 2020/02/02: version 2020.1 is out ! See the full Changelog
  • 2019/06/19: version 2019.2.6 is out. Note that 2019.2.x (x<6) versions had incorrect Ptycho CUDA scaling, preventing correct ML minimisation
  • 2018/06: all coherent imaging modules have now been converted to use an operator-based API
  • 2016/09: overhaul of the library structure - pynx.gpu has now become pynx.scattering, with pynx.scattering.gid as a sub-module.

Introduction

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 (http://cxidb.org), 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 wavefront.py). 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).

Download

PyNX is available from:

Changelog

Version 2020.1 (2020-02-02)

  • CDI runner: enable combining several masks and interpolating gap maxipix masks
  • CDI runner: enable setting initial support based on command-line equation
  • CDI: Faster cdi array matching and pynx-cdi-analysis using OpenCL
  • CDI: add phase retrieval transfer function (PRTF) plotting code
  • CDI & ptychography: more automatic tests
  • Ptycho: enable position/translation corrections
  • [BUG] Ptycho: correct gradient calculation for maximum likelihood/conjugate gradient algorithm
  • [BUG] Ptycho runner: correctly reshape and rescale probe as needed when loading a previous probe
  • All: use safe import for matplotlib.pyplot in case tk is not available, switching backend to agg
  • pynx-test.py: add option for automatic tests & email reporting
  • More efficient memory usage, especially for tests
  • [INCOMPATIBLE CHANGE] Scattering: change sign in Fhkl_thread, now computing F(hkl)=SUM_i exp(-2j*pi*(h*x_i + k*y_i + l*z_i)) instead of F(hkl)=SUM_i exp(+2j*pi*(h*x_i + k*y_i + l*z_i))

Version 2019.2.6 (2019-06-19)

  • [BUG] Ptycho: Correct CUDA ML operator, which prevented correct minimisation
  • CDI: keep the free pixel mask during successive runs (nb_run=N)

Version 2019.2.5 (2019-06-02)

  • Ptycho: large speedup when using CUDA by increasing stack size (needed for fast, recent GPU) and atomic operations
  • Ptycho: store history of figures of merit and cycle parameters. Export to CXI file.
  • Ptycho: add pynx-simulationpy runner for tests
  • Ptycho: add nanoscopium runner script
  • Ptycho: add dm_loop_obj_probe parameter to control looping over object+probe update
  • Improve pynx-test.py output
  • Improve documentation
  • Improve pynx.test.speed to test for large pinned memory allocations.
  • [BUG] CDI: fix CDI Calc2Obs operator
  • [BUG] CDI & Ptycho: correct nps file import
  • [BUG] Ptycho: correct wavelength calculation for CXI export
  • [BUG] Ptycho CXI runner: correct xyrange parameter interpretation

Version 2019.2 (2019-05-20)

  • CDI & Ptychography: CXI output files follow the NeXus standard, allowing direct display when opened with silx view.
  • CDI: record history of indicators (log-likelihood, support size and levels, …) in CXI output
  • CDI runner: add save=all option to save several steps in the algorithm chain
  • CDI: support update has been improved to avoid diverging, affecting threshold levels to be used.
  • CDI: allow updating support only around the border of the support (support_update_border_n)
  • CDI: add GPS operator
  • CDI: export a more complete set of configuration parameters to CXI files
  • CDI: correct scaling (ML operator, initial scaling)
  • CDI: correct examples
  • CDI runner: add save=all options to export solved object after each step
  • Ptychography: CUDA operators are now preferred to OpenCL (significantly faster for large frame sizes)
  • Ptychography: improve near field algorithm, allowing to specify mask with a zero-phase restraint (vacuum)
  • Ptycho runner: allow to roll (circular shift) data instead of cropping
  • Ptycho: add NanoMAX (MaxIV) runner script
  • Utils: add phase retrieval transfer function estimation (for CDI)

Version 2019.1 (2019-02-07)

  • CDI: add ‘free’ log-likelihood figure-of-merit.
  • CDI: allow to give a range for the support threshold, when performing multiple runs.
  • CDI: allow to keep only the best solutions when performing multiple runs
  • CDI: id01 and id10 scripts will now print the algorithm chain used, when it is not user-supplied
  • CDI: add pynx-cdi-analysis script to analyse proposed solutions.
  • Ptycho: enable CUDA operators, 2x speed improvements, especially for large frame sizes
  • Ptycho: correct probe and object orientation and axis in plots, so that both are seen from the source
  • Ptycho: auto-correct probe centering when necessary (DM)
  • Ptycho: better handling of plots for near field Ptycho
  • Ptycho scripts: add ability to create a movie of the scan
  • CDI & Ptycho: improved speed of calculations from GPU profiling.
  • Add test suite
  • Support for Python 3.7
  • Python 3.4 is deprecated
  • [Incompatible change] Ptycho: now all API functions using x,y(,z) coordinates as input or output will use them in alphabetical order. The inverse order is only used for shapes e.g. (ny, nx). This affects notably declaration of PtychoData, as well as get_view_coord(), calc_obj_shape(), Simulation.scan.values
  • [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)
  • [BUG] Ptycho: correct taking into account of mask when using a command-lien script
  • [BUG] Ptycho script: correct taking into account mask in some circumstances

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: http://ftp.esrf.fr/pub/scisoft/PyNX/
PyNX re-uses or was inspired by features described in the following articles and open-source software packages:
  • PtyPy: 1. B. Enders and P. Thibault, “A computational framework for ptychographic reconstructions”, Proc Math Phys Eng Sci 472(2196), (2016).
  • M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, “Iterative least-squares solver for generalized maximum-likelihood ptychography,” Optics Express 26(3), 3108 (2018).
  • S. Marchesini, A. Schirotzek, C. Yang, H. Wu, and F. Maia, “Augmented projections for ptychographic imaging,” Inverse Problems 29(11), 115009 (2013).
    1. Thibault and A. Menzel, “Reconstructing state mixtures from diffraction measurements,” Nature 494(7435), 68–71 (2013).
    1. Thibault and M. Guizar-Sicairos, “Maximum-likelihood refinement for coherent diffractive imaging,” New J. Phys. 14(6), 063004 (2012).
  • J. N. Clark, X. Huang, R. Harder, and I. K. Robinson, “High-resolution three-dimensional partially coherent diffraction imaging,” Nat Commun 3, 993 (2012).
  • S. Marchesini, “A unified evaluation of iterative projection algorithms for phase retrieval,” Review of Scientific Instruments 78(1), 011301 (2007).

License

The PyNX library is distributed with a CeCILL-B license (an open-source license similar to the FreeBSD one). See http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html

Note that CPU computing of the pynx.scattering module uses the sse_mathfun.h header, which is distributed under the zlib license. See http://gruntthepeon.free.fr/ssemath/

See http://ftp.esrf.fr/pub/scisoft/PyNX/README.txt for more details about the license, copyright, as well as other possible issues regarding ptychography.

Installation

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

Automated testing

To automatically test PyNX after installation, you can run the pynx-test.py script, which will run a series of tests and can help diagnose issues specific to GPU languages (OpenCL, CUDA), dependencies or applications (CDI, Ptycho..).

Beginner tutorials

To begin using PyNX, you can read the following tutorials:

  • Use command-line-scripts for data analysis:
  • Use the Python API for:
    • Ptychography
    • Coherent Diffraction Imaging
    • Wavefront propagation
    • Scattering calculations

Command-line scripts

Scripts Reference
Documentation of scripts included in PyNX

API Documentation

API Reference
Documentation of modules included in PyNX

Indices and tables