Breakdown of Examples for Iterative Ptychography¶
This section provides a detailed breakdown of an iterative ptychography reconstruction using PyPty. To get started, create a Python script and import the necessary libraries:
import numpy as np
import pypty
# NumPy is used for minor preprocessing, while all major operations are handled by PyPty.
print("PyPty version: ", pypty._version_)
We will need numpy to do some small preprocessing outside of pypty, but the major steps will be done with pypty functions.
Below I will go through the steps required to do a ptychographic reconstruction from normal 4D-STEM data (far-field).
Creation of Calibrated Data¶
In a first step we want to create and center a 4D-STEM dataset (in h5 format). PyPty can also accept raw numpy-arrays, but it's better to do this preprocessing step.
path = path_to_your_data
name = name of your dataset
path_raw = path+name+".npy"
path_h5 = path+name+".h5"
pypty.initialize.create_pypty_data(path_raw, path_h5, swap_axes=False,
flip_ky=0,flip_kx=0, comcalc_len=200*200,
comx=0, comy=0, bin=1, crop_left=None,
crop_right=None, crop_top=None, crop_bottom=None, normalize=False, exist_ok=1)
Experimental parameters¶
PyPty accepts .json metadata from Nion microscopes, but you can also specify all entries yourself. Here I create a dictionary experimental_params with relevant parameters. See the experiment description for further details.
path_json="" ## I will leave this string like this and specify everything by hand.
output_folder=your_output_folder
experimental_params={
'output_folder': output_folder,
'path_json': path_json,
'data_path': path_h5,
### You can specify one of the following three parameters
'rez_pixel_size_A': None,
'rez_pixel_size_mrad': None,
'conv_semiangle_mrad': 33.8,
'scan_size': [256,256], ## this is number of scan points along slow and fast axes
### You can specify one of the following two parameters
'scan_step_A': 0.18, # scan step in Angstrom
'fov_nm': None,
'acc_voltage': 60, ## kV
'aberrations': [200,0,0],## Angstrom [C10, C12a, C12b], can be longer
'PLRotation_deg': 4.65, # deg
'bright_threshold': 0.2, ## this parameter will be used to estimate the aperture
'total_thickness': 6, ## Angstrom, thickness of the sample
'num_slices': 1, ## number of slices in your reconstruction
'plot': True, ## pypty will plot a few things
'print_flag': 3, ## max verbosity
'data_pad': None, ## None will add 1/4 of pixels to each side. This is done to prevent aliasing. You can do any other positive int.
## now there are 2 nexus tags you can specify. This will help to identify your reconstruction in the Future.
'chemical_formula': YOUR_Formula,
'sample_name': NAME_of_YOUR_Sample,
}
Reconstruction without an extra h5 file¶
If you do not want to create an extra .h5 file (not recommended), you can specify the 4D-STEM data directly in the experimental parameters (before you join them with pypty_params).
data_path will be ignored and you can leave this field empty. The dataset itself can be either 4D or 3D (with 2 scan axes merged into one).
Loading a reconstruction preset¶
Parameters for iterative ptychography are stored in a dictionary typically named pypty_params.
1) Such presets are typically stored as .pkl data and you can load them via a function in utils module
2) Alternatively, you can load them from a nexus data of another reconstruction
3) A third option is to construct your custom preset as a dictionary from scratch (Please see the guide ) 4) Finally, you can specify it asNone, then you will have a standard preset
Joining a preset with experimental parameters¶
Once you have a preset, you can add experimental data to it.
Starting Ptychography¶
This is done with a single function and single argument, pypty_params:
Saving your Results as one Nexus file¶
Once your reconstruction is done, you will find a bunch of files in the output_folder. You can stack them into one big nexus file via a single function. You can also select where the .nxs file will be saved via
path_to_your_nexus_file