ptypy.engines package¶
Submodules¶
ptypy.engines.projectional module¶
Difference Map reconstruction engine.
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- class ptypy.engines.projectional.DM(ptycho_parent, pars=None)¶
Bases:
ptypy.engines.projectional._ProjectionEngine
,ptypy.engines.projectional.DMMixin
A full-fledged Difference Map engine.
Difference map reconstruction engine.
- DEFAULT = {'alpha': 1.0, 'clip_object': None, 'compute_log_likelihood': True, 'fourier_power_bound': None, 'fourier_relax_factor': 0.05, 'name': 'DM', 'numiter': 20, 'numiter_contiguous': 1, 'obj_smooth_std': None, 'object_inertia': 0.0001, 'overlap_converge_factor': 0.05, 'overlap_max_iterations': 10, 'position_refinement': {'amplitude': 1e-06, 'amplitude_decay': True, 'interval': 1, 'max_shift': 2e-06, 'method': 'Annealing', 'metric': 'fourier', 'nshifts': 4, 'record': False, 'start': None, 'stop': None}, 'probe_center_tol': None, 'probe_fourier_support': None, 'probe_inertia': 1e-09, 'probe_support': 0.7, 'probe_update_start': 2, 'record_local_error': False, 'subpix': 'linear', 'subpix_start': 0, 'update_object_first': True}¶
- class ptypy.engines.projectional.RAAR(ptycho_parent, pars=None)¶
Bases:
ptypy.engines.projectional._ProjectionEngine
,ptypy.engines.projectional.RAARMixin
A RAAR engine.
Difference map reconstruction engine.
- DEFAULT = {'beta': 0.75, 'clip_object': None, 'compute_log_likelihood': True, 'fourier_power_bound': None, 'fourier_relax_factor': 0.05, 'name': 'RAAR', 'numiter': 20, 'numiter_contiguous': 1, 'obj_smooth_std': None, 'object_inertia': 0.0001, 'overlap_converge_factor': 0.05, 'overlap_max_iterations': 10, 'position_refinement': {'amplitude': 1e-06, 'amplitude_decay': True, 'interval': 1, 'max_shift': 2e-06, 'method': 'Annealing', 'metric': 'fourier', 'nshifts': 4, 'record': False, 'start': None, 'stop': None}, 'probe_center_tol': None, 'probe_fourier_support': None, 'probe_inertia': 1e-09, 'probe_support': 0.7, 'probe_update_start': 2, 'record_local_error': False, 'subpix': 'linear', 'subpix_start': 0, 'update_object_first': True}¶
ptypy.engines.ML module¶
Maximum Likelihood reconstruction engine.
TODO.
Implement other regularizers
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- class ptypy.engines.ML.ML(ptycho_parent, pars=None)¶
Bases:
ptypy.engines.base.PositionCorrectionEngine
Maximum likelihood reconstruction engine.
Maximum likelihood reconstruction engine.
- DEFAULT = {'ML_type': 'gaussian', 'floating_intensities': False, 'intensity_renormalization': 1.0, 'name': 'ML', 'numiter': 20, 'numiter_contiguous': 1, 'poly_line_coeffs': 'quadratic', 'position_refinement': {'amplitude': 1e-06, 'amplitude_decay': True, 'interval': 1, 'max_shift': 2e-06, 'method': 'Annealing', 'metric': 'fourier', 'nshifts': 4, 'record': False, 'start': None, 'stop': None}, 'probe_fourier_support': None, 'probe_support': 0.7, 'probe_update_start': 2, 'record_local_error': False, 'reg_del2': False, 'reg_del2_amplitude': 0.01, 'scale_precond': False, 'scale_probe_object': 1.0, 'smooth_gradient': 0.0, 'smooth_gradient_decay': 0.0}¶
- SUPPORTED_MODELS = [<class 'ptypy.core.manager.Full'>, <class 'ptypy.core.manager.Vanilla'>, <class 'ptypy.core.manager.Bragg3dModel'>, <class 'ptypy.core.manager.BlockVanilla'>, <class 'ptypy.core.manager.BlockFull'>, <class 'ptypy.core.manager.GradFull'>, <class 'ptypy.core.manager.BlockGradFull'>]¶
- engine_finalize()¶
Delete temporary containers.
- engine_initialize()¶
Prepare for ML reconstruction.
- engine_iterate(num=1)¶
Compute num iterations.
- engine_prepare()¶
Last minute initialization, everything, that needs to be recalculated, when new data arrives.
ptypy.engines.stochastic module¶
Stochastic reconstruction engine.
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- class ptypy.engines.stochastic.EPIE(ptycho_parent, pars=None)¶
Bases:
ptypy.engines.stochastic._StochasticEngine
,ptypy.engines.stochastic.EPIEMixin
The ePIE algorithm.
Stochastic Douglas-Rachford reconstruction engine.
- DEFAULT = {'alpha': 1.0, 'beta': 1.0, 'compute_log_likelihood': True, 'name': 'EPIE', 'numiter': 20, 'numiter_contiguous': 1, 'object_norm_is_global': False, 'position_refinement': {'amplitude': 1e-06, 'amplitude_decay': True, 'interval': 1, 'max_shift': 2e-06, 'method': 'Annealing', 'metric': 'fourier', 'nshifts': 4, 'record': False, 'start': None, 'stop': None}, 'probe_center_tol': None, 'probe_fourier_support': None, 'probe_support': 0.7, 'probe_update_start': 0, 'record_local_error': False}¶
- class ptypy.engines.stochastic.SDR(ptycho_parent, pars=None)¶
Bases:
ptypy.engines.stochastic._StochasticEngine
,ptypy.engines.stochastic.SDRMixin
The stochastic Douglas-Rachford algorithm.
Stochastic Douglas-Rachford reconstruction engine.
- DEFAULT = {'beta_object': 0.9, 'beta_probe': 0.1, 'compute_log_likelihood': True, 'name': 'SDR', 'numiter': 20, 'numiter_contiguous': 1, 'position_refinement': {'amplitude': 1e-06, 'amplitude_decay': True, 'interval': 1, 'max_shift': 2e-06, 'method': 'Annealing', 'metric': 'fourier', 'nshifts': 4, 'record': False, 'start': None, 'stop': None}, 'probe_center_tol': None, 'probe_fourier_support': None, 'probe_support': 0.7, 'probe_update_start': 0, 'record_local_error': False, 'sigma': 1, 'tau': 1}¶
ptypy.engines.base module¶
Base engine. Used to define reconstruction parameters that are shared by all engines.
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- class ptypy.engines.base.Base3dBraggEngine(ptycho, pars=None)¶
Bases:
ptypy.engines.base.BaseEngine
3d Bragg engines need a slightly different prepare() method, because a 2d probe support makes no sense (at least not yet…)
Defaults:
[probe_support] default = None
Base reconstruction engine. :param ptycho: The parent
Ptycho
object. :type ptycho: Ptycho :param pars: Initialization parameters :type pars: Param or dict- prepare()¶
Last-minute preparation before iterating.
- class ptypy.engines.base.BaseEngine(ptycho, pars=None)¶
Bases:
object
Base reconstruction engine. In child classes, overwrite the following methods for custom behavior : engine_initialize engine_prepare engine_iterate engine_finalize
Defaults:
[numiter] default = 20 type = int lowlim = 1 help = Total number of iterations doc = For on-the-fly (live) processing, the reconstruction engine will iterate at least this many times after all data has been loaded.
[numiter_contiguous] default = 1 type = int lowlim = 1 help = Number of iterations without interruption doc = The engine will not return control to the caller until this number of iterations is completed (not processing server requests, I/O operations, …).
[probe_support] default = 0.7 type = float, None lowlim = 0.0 help = Valid probe area as fraction of the probe frame doc = Defines a circular area centered on the probe frame, in which the probe is allowed to be nonzero.
[probe_fourier_support] default = None type = float, None lowlim = 0.0 help = Valid probe area in frequency domain as fraction of the probe frame doc = Defines a circular area centered on the probe frame (in frequency domain), in which the probe is allowed to be nonzero.
[record_local_error] default = False type = bool help = If True, save the local map of errors into the runtime dictionary. userlevel = 2
Base reconstruction engine. :param ptycho: The parent
Ptycho
object. :type ptycho: Ptycho :param pars: Initialization parameters :type pars: Param or dict- COMPATIBLE_MODELS = []¶
- engine_finalize()¶
Engine-specific finalization. Used to wrap-up engine-specific stuff. Called at the end of self.finalize()
- engine_initialize()¶
Engine-specific initialization. Called at the end of self.initialize().
- engine_iterate(num)¶
Engine single-step iteration. All book-keeping is done in self.iterate(), so this routine only needs to implement the “core” actions.
- engine_prepare()¶
Engine-specific preparation. Last-minute initialization providing up-to-date information for reconstruction. Called at the end of self.prepare()
- finalize()¶
Clean up after iterations are done.
- initialize()¶
Prepare for reconstruction.
- iterate(num=None)¶
Compute one or several iterations.
- numNone, int number of iterations.
If None or num<1, a single iteration is performed.
- prepare()¶
Last-minute preparation before iterating.
- support_constraint(storage=None)¶
Enforces 2D support contraint on probe.
- class ptypy.engines.base.PositionCorrectionEngine(ptycho_parent, pars)¶
Bases:
ptypy.engines.base.BaseEngine
A sub class engine that supports position correction
Defaults:
[position_refinement] default = False type = Param, bool help = If True refine scan positions
[position_refinement.method] default = Annealing type = str help = Annealing or GridSearch
[position_refinement.start] default = None type = int help = Number of iterations until position refinement starts doc = If None, position refinement starts at first iteration
[position_refinement.stop] default = None type = int help = Number of iterations after which positon refinement stops doc = If None, position refinement stops after last iteration
[position_refinement.interval] default = 1 type = int help = Frequency of position refinement
[position_refinement.nshifts] default = 4 type = int help = Number of random shifts calculated in each position refinement step (has to be multiple of 4)
[position_refinement.amplitude] default = 0.000001 type = float help = Distance from original position per random shift [m]
[position_refinement.amplitude_decay] default = True type = bool help = After each interation, multiply amplitude by factor (stop - iteration) / (stop - start)
[position_refinement.max_shift] default = 0.000002 type = float help = Maximum distance from original position [m]
[position_refinement.metric] default = “fourier” type = str help = Error metric, can choose between “fourier” and “photon” choices = [“fourier”, “photon”]
[position_refinement.record] default = False type = bool help = record movement of positions
Position Correction engine.
- POSREF_ENGINES = {'Annealing': <class 'ptypy.engines.posref.AnnealingRefine'>, 'GridSearch': <class 'ptypy.engines.posref.GridSearchRefine'>}¶
- engine_finalize()¶
Synchronize positions
- engine_initialize()¶
Prepare the position refinement object for use further down the line.
- position_update()¶
Position refinement update.
ptypy.engines.utils module¶
Engine-specific utilities. This could be compiled, or GPU accelerated.
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- ptypy.engines.utils.Cdot(c1, c2)¶
Compute the dot product on two containers c1 and c2. No check is made to ensure they are of the same kind.
- Parameters
c2 (Container c1,) – Input
- Returns
The dot product (scalar)
- ptypy.engines.utils.Cnorm2(c)¶
Computes a norm2 on whole container c.
- Parameters
c (Container) – Input
- Returns
The norm2 (scalar)
See also
- ptypy.engines.utils.basic_fourier_update(diff_view, pbound=None, alpha=1.0, LL_error=True)¶
* DEPRECATED * Backwards compatible function, for reference only. Contains LL error. Please replace with log_likelihood and projection_update_DM_AP
See also
- ptypy.engines.utils.basic_fourier_update_LEGACY(diff_view, pbound=None, alpha=1.0, LL_error=True)¶
* DEPRECATED * Fourier update a single view using its associated pods. Updates on all pods’ exit waves.
- Parameters
diff_view (View) – View to diffraction data
alpha (float, optional) – Mixing between old and new exit wave. Valid interval
[0, 1]
pbound (float, optional) – Power bound. Fourier update is bypassed if the quadratic deviation between diffraction data and diff_view is below this value. If
None
, fourier update always happens.LL_error (bool) – If
True
, calculates log-likelihood and puts it in the last entry of the returned error vector, else puts in0.0
- Returns
error – 1d array,
error = np.array([err_fmag, err_phot, err_exit])
.err_fmag, Fourier magnitude error; quadratic deviation from root of experimental data
err_phot, quadratic deviation from experimental data (photons)
err_exit, quadratic deviation of exit waves before and after Fourier iteration
- Return type
ndarray
- ptypy.engines.utils.dynamic_load(path, baselist, fail_silently=True)¶
Load an derivatives of baselist dynamically from .py files in the given path.
- Parameters
path – Path to Python files.
- ptypy.engines.utils.log_likelihood(diff_view)¶
Calculates the log-likelihood for a diffraction view.
- Parameters
diff_view (View) – View to diffraction data
- Returns
ll_error – Log-likelihood error
- Return type
float
- ptypy.engines.utils.projection_update_DM_AP(diff_view, alpha=1.0, pbound=None)¶
Linear interpolation between Difference Map algorithm (a,b,c = -1,1,2) and Alternating Projections algorithm (a,b,c = 0,1,1) with coefficients a = -alpha, b = 1, c = 1 + alpha. Alpha = 1.0 corresponds to DM and alpha = 0.0 to AP.
- Parameters
diff_view (View) – View to diffraction data
alpha (float, optional) – Blend between AP (alpha=0.0 and DM (alpha=1.0) . Valid interval
[0, 1]
pbound (float, optional) – Power bound. Fourier update is bypassed if the quadratic deviation between diffraction data and diff_view is below this value. If
None
, fourier update always happens.
- Returns
err_fmag, err_exit –
err_fmag, Fourier magnitude error; quadratic deviation from root of experimental data
err_exit, quadratic deviation between exit waves before and after projection
- Return type
float
- ptypy.engines.utils.projection_update_generalized(diff_view, a, b, c, pbound=None)¶
Generalized projection update of a single view using its associated pods. Updates on all pods’ exit waves. We assume here that the current state is held in pod.exit, while the product of pod.probe & pod.object hold the state after overlap constraint has been applied. With O() denoting the overlap constraint and F() denoting the Data/Fourier constraint, the general projection update can be expressed with four coefficients
\[\psi^{j+1} = [x 1 + a O + b F (c O + y 1)](\psi^{j})\]However, the coefficients aren’t all independent as the sum of x+a+b and d+y must be 1, thus we choose
\[x = 1 - a - b\]and
\[y = 1 - c\]The choice of a,b,c should enable a wide range of projection based algorithms.
For memory efficiency, this projection update includes the Fourier update which is why the power bound mechanism is included but deactivated by default.
- Parameters
diff_view (View) – View to diffraction data
a (float) – Coefficients for Overlap, Fourier and Fourier * Overlap constraints, respectively
b (float) – Coefficients for Overlap, Fourier and Fourier * Overlap constraints, respectively
c (float) – Coefficients for Overlap, Fourier and Fourier * Overlap constraints, respectively
pbound (float, optional) – Power bound. Fourier update is bypassed if the quadratic deviation between diffraction data and diff_view is below this value. If
None
, fourier update always happens.
- Returns
err_fmag, err_exit –
err_fmag, Fourier magnitude error; quadratic deviation from root of experimental data
err_exit, quadratic deviation between exit waves before and after projection
- Return type
float
- ptypy.engines.utils.reduce_dimension(a, dim, local_indices=None)¶
Apply a low-rank approximation on a.
- Parameters
a (ndarray) – 3D numpy array
dim (int) – The number of dimensions to retain. The case dim=0 (which would just reduce all layers to a mean) is not implemented.
local_indices – Used for Containers distributed across nodes. Local indices of the current node.
- Returns
- where:
reduced array is the result of dimensionality reduction (same shape as a)
modes: 3D array of length dim containing eigenmodes (aka singular vectors)
coefficients: 2D matrix representing the decomposition of a.
- Return type
reduced array, modes, coefficients
Module contents¶
Engines module.
Implements the difference map (DM) and maximum likelihood (ML) reconstruction algorithms for ptychography.
This file is part of the PTYPY package.
- copyright
Copyright 2014 by the PTYPY team, see AUTHORS.
- license
see LICENSE for details.
- ptypy.engines.by_name(name)¶
- ptypy.engines.register(name=None)¶
Engine registration decorator