epsic_tools.toolbox package

Submodules

epsic_tools.toolbox.define_probe_function module

epsic_tools.toolbox.define_probe_function.FuncAberrUV(u, v, aberrcoeff)[source]
epsic_tools.toolbox.define_probe_function.define_probe_function(V, alpha, px_cal, array_px, aberr_input, dose=1, plot_me=False)[source]
epsic_tools.toolbox.define_probe_function.rad_array(arr_len)[source]

epsic_tools.toolbox.make_merlin_mask module

epsic_tools.toolbox.make_merlin_mask.add_to_mask(mask, pix_list, save_path=None)[source]

Adds an arbitrary list of pixels to a mask. Parameters __________ mask: pyxem ElectronDiffraction2D

mask as pyxem object

pix_list: list of tuples of ints

list of pixels expressed as tuples to be added to a mask

save_path: str

default None. If desired to save this new version, full path of h5 file to be provided here.

mask_new: pyxem ElectronDiffraction2D

epsic_tools.toolbox.make_merlin_mask.make_mask(flat_field, hot_pix_factor, mask_cross=False, show_mask=True, dest_h5_path=None, show_hist=False)[source]

Creates mask for Merlin Medipix Parameters: _____________ flat_field: str

full path of the flat-field mib data

hot_pix_factor: float

intensity factor above average intensity to determine hot pixels

mask_cross: bool

Default False. Mask out the central cross on quad chip

show_mask: bool

Default True. For plotting the mask.

dest_h5_path: str

default None. full path of the h5 file to be saved. key for mask: ‘merlin_mask’. A pyxem version of the mask is also saved in the same destination.

show_hist: bool

If passed as True a histogram of the flat-feild data is shown (averaged if the data is a stack)

mask: pyxem ElectronDiffraction2D

mask pyxem object

epsic_tools.toolbox.make_merlin_mask_test module

epsic_tools.toolbox.make_merlin_mask_test.add_to_mask(mask, pix_list, save_path=None)[source]

Adds an arbitrary list of pixels to a mask. Parameters __________ mask: pyxem ElectronDiffraction2D

mask as pyxem object

pix_list: list of tuples of ints

list of pixels expressed as tuples to be added to a mask

save_path: str

default None. If desired to save this new version, full path of h5 file to be provided here.

mask_new: pyxem ElectronDiffraction2D

epsic_tools.toolbox.make_merlin_mask_test.make_mask(flat_field, hot_pix_factor, show_mask=True, dest_h5_path=None, show_hist=False)[source]

Creates mask for Merlin Medipix Parameters: _____________ flat_field: str

full path of the flat-field mib data

hot_pix_factor: float

intensity factor above average intensity to determine hot pixels

show_mask: bool

Default True. For plotting the mask.

dest_h5_path: str

default None. full path of the h5 file to be saved. key for mask: ‘merlin_mask’. A pyxem version of the mask is also saved in the same destination.

show_hist: bool

If passed as True a histogram of the flat-feild data is shown (averaged if the data is a stack)

mask: pyxem ElectronDiffraction2D

mask pyxem object

epsic_tools.toolbox.ptycho_utils module

Created on Fri May 15 22:52:15 2020

@author: eha56862

epsic_tools.toolbox.ptycho_utils.crop_recon_obj(json_file)[source]
epsic_tools.toolbox.ptycho_utils.duplicate_json(source_json_path, new_json_path, param_to_change=None)[source]

This gets a source json file and duplicates it, changing a parameter if declared Parameters ____________ source_json_path: str

full path of the starting json file

new_json_path: str

full path of the new json file

param_to_change: tuple of (str, new_val)

default None - key of the parameter to be changed as first el of tuple and the new value as the second el.

data_dict: dict

The new dict if anything changed.

TODO: Fix the ugly limited dict depth implementation!

epsic_tools.toolbox.ptycho_utils.e_lambda(e_0)[source]

relativistic electron wavelength

Parameters

e_0 – int accelerating voltage in volts

Returns

e_lambda: float

wavelength in meters

epsic_tools.toolbox.ptycho_utils.get_RMSE(dist_list)[source]

Input: dist_list: list of floats

atomic distances

returns

RMSE of the list elements

epsic_tools.toolbox.ptycho_utils.get_error(file_path)[source]
file_path: str

full path of the recon file. It check if it is frtom ptypy or ptyREX

errors: np.array

error as numpy array

epsic_tools.toolbox.ptycho_utils.get_fft(obj_arr, crop=None, apply_hann=False)[source]

obj_arr: numpy.ndarray crop: float

fraction of FOV to crop before fft. default None

apply_hann: bool

If True, hanning window applied before fft

obj_fft: numpy.ndarray

abs array of the fft

epsic_tools.toolbox.ptycho_utils.get_hdf5_complex_probe(h5_file)[source]
epsic_tools.toolbox.ptycho_utils.get_hdf5_error(h5_file)[source]
epsic_tools.toolbox.ptycho_utils.get_hdf5_object_modulus(h5_file, params)[source]
epsic_tools.toolbox.ptycho_utils.get_hdf5_object_phase(h5_file, params)[source]
epsic_tools.toolbox.ptycho_utils.get_hdf5_probe_modulus(h5_file)[source]
epsic_tools.toolbox.ptycho_utils.get_hdf5_probe_phase(h5_file)[source]
epsic_tools.toolbox.ptycho_utils.get_json_params(h5_file)[source]
epsic_tools.toolbox.ptycho_utils.get_json_pixelSize(json_file)[source]
json_file: str

full path of the json file.

Returns pixelSize: float

reconstruction pixel size in (m)

epsic_tools.toolbox.ptycho_utils.get_obj_array(file_path)[source]
file_path: str

full path of the recon file. It check if it is frtom ptypy or ptyREX

data_arr: np.array

complex object numpy array

TODO: if input is json check if there is a similarly named hdf file in the same folder

epsic_tools.toolbox.ptycho_utils.get_probe_array(file_path)[source]
file_path: str

full path of the recon file. It check if it is frtom ptypy or ptyREX

probe_data_arr: np.array

complex probe numpy array

epsic_tools.toolbox.ptycho_utils.get_sampling_factor(x, d, n, dr)[source]
x: float

pixelSize * number of pixels in probe

d: float

probe diameter in m

n: int

number of probe positions

dr: float

step size

s: float

sampling factor

Ref: Darren Batey PhD Thesis, Page 68.

epsic_tools.toolbox.ptycho_utils.json_to_dict(json_path)[source]
json_path: str

full path of the json file.

json_dict: dictionary

epsic_tools.toolbox.ptycho_utils.kdtree_NN(experiment, truth, search_rad)[source]

Runs sklearn KDTree proximity algorithm on the data Parameters ___________ experiment: list

list of atomic position coordinates in the experimental data

truth: list

list of atomic position coordinates in the ground truth

search_rad: float

radius to search for nearest neighbour

nn_results: dict
dict containing the following keys:

TP list - as paired atoms coordinates and there distances FP list FN list Precision Recall RMSE of distances

epsic_tools.toolbox.ptycho_utils.load_dict_from_hdf5(filename)[source]

recursively loads an hdf5 file to a nested dict

epsic_tools.toolbox.ptycho_utils.load_recon(fn)[source]
epsic_tools.toolbox.ptycho_utils.load_series(pn, crop_to, sort_by='rot', blur=0, verbose=False)[source]

loads all ptycho reconstructions in a folder and sorts by a parameter

pn : String pathname of folder

sort_by: ‘rot’ or ‘step’ parameter by which to sort the data

crop_to: Int size of cropped object reconstruction

blur: Int to pass as sigma to gaussian blur object phase. 0 = no blur

verbose: Bool to print detailed output

d_s: hyperspy singnal2D object function

p_s: hyperspy singnal2D probe function

d_s_fft: hyperspy singnal2D fourier transfor of object function

rad_fft: hyperspy singnal1D object radial profile of d_s_fft

r_s: hyperspy signal1D object scan rotation

s_s: hyperspy signal1D object probe step size

e_s: hyperspy signal1D object final error value

from epsic_tools.toolbox.ptychography.load_pycho_series import load_series pn = r’Y:‚0cm26481-1processingMerlin‚00130_80kV_graphene_600C_ptyclusterprocessingpycho’ d_s, p_s, d_s_fft, rad_fft, r_s, s_s, e_s = load_series(pn,sort_by = ‘rot’, crop_to = 80) hs.plot.plot_signals([d_s,p_s,d_s_fft, rad_fft], navigator_list=[r_s,s_s, e_s,None])

Break loading from hdf file into seperate functions

epsic_tools.toolbox.ptycho_utils.plot_ptyREX_output(json_path, save_fig=None, crop=False)[source]

To save the ptyREX recon output Parameters ———- json_path: str

full path of the json file. This figure output will be saved in this folder.

save_fig: str, default None

In case we want the figure to be saved the full path should be given here keyword argument.

crop: Bool

default False

epsic_tools.toolbox.ptycho_utils.plot_ptyr(filename)[source]

Plots the real and imaginary parts of a ptypy recon file

epsic_tools.toolbox.ptycho_utils.save_dict_to_hdf5(dic, filename, overwrite=True)[source]

recursively saves a nested dict into an hdf5 file

epsic_tools.toolbox.radial_profile module

Created on Thu Feb 13 15:37:27 2020

@author: gys37319

epsic_tools.toolbox.radial_profile.radial_profile(data, center, nRad=1)[source]

calculate radial profile for a 2D array

data : 2D numpy array

center: tuple of x,y center positions

nRad: integer number of radial slices Returns ——-

radialprofile : nD numpy array corresponding to nRad

epsic_tools.toolbox.radial_profile.radial_profile_stack(hs_obj, center=None)[source]

calculate radial profile from a hyperspy Signal2D object

hs_obj : hyperspy signal2D object

center: tuple of x,y center positions

radial_profiles : hyperspy signal1D object

epsic_tools.toolbox.radial_profile.sector_mask(shape, centre, radius, angle_range)[source]

Return a boolean mask for a circular sector. The start/stop angles in angle_range should be given in clockwise order.

epsic_tools.toolbox.sim_utils module

Created on Fri Mar 13 09:31:39 2020

@author: eha56862 These functions are utilities for determining the parameters for the simulation matrix TODO:

get_probe get_sim_params

class epsic_tools.toolbox.sim_utils.NestedDefaultDict(*args, **kwargs)[source]

Bases: collections.defaultdict

epsic_tools.toolbox.sim_utils.add_dose_noise(file_path, stack_path, dose, add_noise=True)[source]
file_path: str

full path and name of the sim h5 file

stack_path:

full path for the stack h5 file

dose: int

target sum intensity of the entire 4DSTEM data

add_noise: boolean

if True it also adds posson noise to the diffraction patterns

epsic_tools.toolbox.sim_utils.calc_camera_length(data_hs, bf_rad, angle, pixel_size)[source]

it returns the camera length based on the detector pixel size and a known value in the diffraction plane - it plots the sum dp with the known value marked with circle roi input:

data_hs: 4D-STEM hypespy object bf_rad: int

bf radius in pixels

angle: float

known angle in diff plane in rad

pixel_size: float

physcial size of detector pix in m

returns
CL: float

camera length in meters

epsic_tools.toolbox.sim_utils.calc_pixelSize(acc_voltage, pixel_array, det_pixelSize, camera_length)[source]

Calculates the pixelSize in ptycho recon

acc_voltage: int

accelerating voltage in V

pixel_array: int

number of pixels in detector in x or y (assumed square)

det_pixelSize: float

detector physical pixel size in m

camera_length: float

camera length

pixelSize: float

recon pixel size in m

epsic_tools.toolbox.sim_utils.calc_probe_size(pixelSize, imageSize, _lambda, probe_def, probe_semiAngle, method='80pctInt', plot_probe=True, return_probeArr=False)[source]

this function is for giving an estimate of the probe size to set up the sim ptycho data accordingly. :param pixelSize: float

pixel size in (m)

Parameters
  • imageSize – list of ints image size in (pixels)

  • _lambda – float in (m) - electron wavelength

  • probe_def – float probe defocus in m

  • probe_semiAngle – float (rad) probe semi-angle

  • plot_probe – boolean default to True - to plot the probe imag / real

  • return_probeArr – boolean default to False - if the probe array is needed as output

Returns

  • plots probe in the real and fourier space - if plot_probe is True

probe_rad: float

in (m) probe radius if return_probeArr set to True:

[probe_rad, psiShift]: with psiShift: np.array

probe in real space

epsic_tools.toolbox.sim_utils.fft(ar)[source]
epsic_tools.toolbox.sim_utils.find_nearest(array, value)[source]
epsic_tools.toolbox.sim_utils.genAp(*args)[source]

### Generate a circular aperture ### out:

ap - Aperture

in:

shape - Array size r - Radius of aperture

epsic_tools.toolbox.sim_utils.genStop(*args)[source]

### Generate a circular aperture ### out:

ap - Aperture

in:

shape - Array size r - Radius of aperture

epsic_tools.toolbox.sim_utils.get_adf(data_hs, bf_rad)[source]

provides an adf image - as outer angle it defaults to centre pixel position plus the bf disc radius and as inner it uses the bf disc radius :param data_hs: hyperspy Signal2D object

Parameters

bf_rad – int radius of the bright field disc in pixels

Returns

adf: hyperspy Signal2D object

epsic_tools.toolbox.sim_utils.get_bf_disc(data_hs)[source]

Interactively gets the radius and centre position of the bright filed disc :param data_hs: hyperspy Signal2D object

Returns

circ_roi: hs roi object

roi object with centre and radius

epsic_tools.toolbox.sim_utils.get_disc_overlap(rad, dist, print_output=False)[source]

More suitable for Wigner - as it aims to minimse triple overlap regions for ptyREX will be using the simpler function get_overlap to calculate disc over lap in image or diff plane

rad: float

radius of probe or probe semi-angle

dist: float

step distance or the angle to the reflection of interest

returns

Percentage_overlap: float

epsic_tools.toolbox.sim_utils.get_figs(sim_matrix_path)[source]

returns the list of the png figure of the final recon

epsic_tools.toolbox.sim_utils.get_frc(ar1, ar2, dx, norm=False, plot=False)[source]
epsic_tools.toolbox.sim_utils.get_overlap(probe_rad, step_size)[source]
probe_rad: float

probe radius in m (or rad)

step_size: float

scan step size in m (or known diffraction disc position in rad)

Returns probe_overlap: float

percentage probe overlap

epsic_tools.toolbox.sim_utils.get_potential(sim_file_path)[source]

gets the pyprismatic h5 file and outputs the potential - in V.Angstroms

epsic_tools.toolbox.sim_utils.get_ptyREX_ready(sim_matrix_path)[source]

checks for the folders that have ptyREX json file

Returns ptyREX_dirs: list

list of dirs

epsic_tools.toolbox.sim_utils.get_ptyREX_recon_list(sim_matrix_path, run_id=None)[source]
sim_matrix_path: str

full path of the simulation matrix

run_id: str

default None. If provided the json files with run_id in their names will be returned.

common_dirs: list

List of json files that have identically named hdf5 file in the same folder

epsic_tools.toolbox.sim_utils.get_raw_dir_list(sim_matrix_path, get_all=False)[source]

checks for the folders with only two files and identify them as raw get_all set to True returns all the folders

sim_matrix_path: str

full path holding the sim matrix

get_all: bool

Default False. Set to True if all folders needed

raw_dirs: list

list of directories

epsic_tools.toolbox.sim_utils.get_sim_data(sim_h5_file)[source]
epsic_tools.toolbox.sim_utils.get_sim_params(sim_h5_file)[source]
epsic_tools.toolbox.sim_utils.get_sim_probe(sim_h5_file, real_space=True)[source]
epsic_tools.toolbox.sim_utils.get_sim_probe_for_ptyrex(sim_h5_file, probe_path, pixel_size)[source]

gets the sim probe, bins it by 2 and saves it into an h5 file readable by ptyrex.

epsic_tools.toolbox.sim_utils.get_step_size(probe_rad, target_overlap)[source]

knowing the probe radius and the target overlap percentage this function returns the suitable step size. Parameters ___________ probe_rad: float

probe radius in m

target_overlap: float

overlap fraction

step_size: float

the step size in m needed to get the target overlap

epsic_tools.toolbox.sim_utils.ifft(ar)[source]
epsic_tools.toolbox.sim_utils.json_to_dict_sim(json_path)[source]
epsic_tools.toolbox.sim_utils.makeFourierCoordinates(N, pixelSize)[source]

this function creates a set of coordinates in the Fourier space Input ________ N: np.array of int

image size in pixels. np.array((Nx,Ny))

pixelSize: float

pixel size in A

qx: np.array qy: np.array

fourier coordinates

epsic_tools.toolbox.sim_utils.max_defocus(pixelSize, imageSize, _lambda, probe_semiAngle)[source]

this function returns the max defocus to be used for ptycho sim given by the defocus resulting in probe diameter of quarter of the reconstruction array size.

pixelSize: float

pixel size in (m)

imageSize: list of ints

image size in (pixels)

_lambda: float
  1. electron wavelength

probe_semiAngle: float

(rad) probe semi-angle

max_def: float

max defocus in (m)

epsic_tools.toolbox.sim_utils.parse_params_file(params_file, h5_file, drop_unneeded=True)[source]

Reads the parameters text file into a dict to be fed into ptypy / pycho recons

epsic_tools.toolbox.sim_utils.setPower(ar, power)[source]
epsic_tools.toolbox.sim_utils.shift_probe(X, dx, dy)[source]
epsic_tools.toolbox.sim_utils.sim_to_hs(sim_h5_file, h5_key='hdose_noisy_data')[source]

reads simulated 4DSTEM file into hs object Parameters __________ sim_h5_file: str

full path of the simulated 4DSTEM dataset h5 file

h5_key: str

the h5 key of the dataset - default is ‘hdose_noisy_data’ if h5_key is passed as ‘skip’: for skipped probe data if h5_key is passed as ‘raw’: as-output sim - each dp sums to ~1 intensity if another h5_key is provided that location is looked up for data

data_hs: hyperspy Signal2D object

epsic_tools.toolbox.sim_utils.write_ptyrex_json(exp_dict, iter_num)[source]

epsic_tools.toolbox.warp_3d module

Created on Fri Jul 12 14:15:48 2019

@author: gys37319

Functions to fit and warp 4DSTEM diffraction data

epsic_tools.toolbox.warp_3d.compute_transform(data, params)[source]

compute the tranform from fit parameters

data : Input image, 2D numpy array params : center [x0,y0], phi (in rad), axes [major,minor]

transformation

epsic_tools.toolbox.warp_3d.ellipse_angle_of_rotation(a)[source]

Calculate ellipse angle of rotation from fit params

a : fit parameters

angle in radians

epsic_tools.toolbox.warp_3d.ellipse_axis_length(a)[source]

Calculate ellipse major / minor axis length from fit params

a : fit parameters

len1 , len2 as a numpy array

epsic_tools.toolbox.warp_3d.ellipse_center(a)[source]

Calculate ellipse center from fit params

a : fit parameters

x0, y0 as a numpy array

epsic_tools.toolbox.warp_3d.fitEllipse(x, y)[source]

fit ellipse to x,y co-ordinates

x : 1D numpy array y :1D numpy array

a : fit parameters to be fed into functions

epsic_tools.toolbox.warp_3d.fit_ellipse(data, threshold=5, sigma=0, plot_me=False)[source]

function to fit ellipse to a single image

data : Input image, 2D numpy array thershold : threshold for binarising image, integer plot_me : boolean to plot results

center, phi, axes

epsic_tools.toolbox.warp_3d.get_coords_4d(coords, shape_4d)[source]

build the 4d transformation co-ordinates

coords : 2D coordinates shape_4d : shape of the 4d data set to be transformed

co_ords4d : 4d coordinates for the transform

epsic_tools.toolbox.warp_3d.plot_ellipse(data, params, line_width=2)[source]

function to plot ellipse fits

data : Input image, 2D numpy array params : center [x0,y0], phi (in rad), axes [major,minor] line_width : thickness of plot line

epsic_tools.toolbox.warp_3d.remove_cross(data)[source]

normalize bright px and add data to zero px cross in quad chip data

data : 2D numpy array with bright and zero px cross

data : 2D numpy array with normalised / filled cross

epsic_tools.toolbox.warp_3d.warp_all(data, coords, dat_type, plot_me=False)[source]

NOT CURRENTLY USED

apply transformation to full 4d data set

data : inpud 4d data set (dask array) coords : 4D coordinates plot_me : boolean

dat_temp : warped 4D data

epsic_tools.toolbox.warp_3d.warp_all_np(data, coords, order=1, preserve_range=True, plot_me=False)[source]

apply transformation to full 4d data set

data : inpud 4d data set (dask array) coords : 4D coordinates plot_me : boolean

dat_temp : warped 4D data

Module contents