stemtool.nbed package

Submodules

stemtool.nbed.nbed_strain module

stemtool.nbed.nbed_strain.ROI_from_image(image, med_val, style='over', showfig=True)[source]
stemtool.nbed.nbed_strain.ROI_strain_map(strain_ROI, ROI)[source]

Convert the strain in the ROI array to a strain map

stemtool.nbed.nbed_strain.aperture_image(data4D, center, radius)[source]

Generate Virtual DF image for a given aperture

Parameters:
  • data4D (ndarray of shape (4,4)) – the first two dimensions are Fourier space, while the next two dimensions are real space
  • center (ndarray of shape (1,2)) – Center of the circular aperture
  • radius (float) – Radius of the circular aperture
Returns:

df_image – Generated virtual dark field image from the aperture and 4D data

Return type:

ndarray of shape (2,2)

Notes

We generate the aperture first, and then make copies of the aperture to generate a 4D dataset of the same size as the 4D data. Then we do an element wise multiplication of this aperture 4D data with the 4D data and then sum it along the two Fourier directions.

stemtool.nbed.nbed_strain.bin4D_dask(data4D, bin_factor, workers=4)[source]

Bin 4D data in spectral dimensions, implemented in dask

Parameters:
  • data4D (ndarray of shape (4,4)) – the first two dimensions are Fourier space, while the next two dimensions are real space
  • bin_factor (int) – Value by which to bin data
  • workers (int, optional) – Number of dask workers. Default is 4.
Returns:

binned_data – Data binned in the spectral dimensions

Return type:

ndarray of shape (4,4)

Notes

The data is binned in the first two dimensions - which are the Fourier dimensions using the internal numba functions resizer2D_numbaopt and resizer1D_numbaopt

See also

resizer1D()

stemtool.nbed.nbed_strain.bin_scan(data4D, bin_factor)[source]

Bin the data in the scan dimensions

Parameters:
  • data4D (ndarray) – This is a 4D dataset where the first two dimensions are the dffraction dimensions and the next two dimensions are the scan dimensions
  • bin_factor (int or tuple) – Binning factor for scan dimensions
Returns:

binned_4D – The data binned in the scanned dimensions.

Return type:

ndarray

Notes

You can specify the bin factor to be either an integer or a tuple. If you specify an integer the same binning will be used in both the scan X and scan Y dimensions, while if you specify a tuple then different binning factors for each dimensions.

Examples

Run as:

>>> binned_4D = bin_scan(data4D, 4)

This will bin the scan dimensions by 4. This is functionally identical to:

>>> binned_4D = bin_scan(data4D, (4, 4))
stemtool.nbed.nbed_strain.bin_scan_test(big4D, bin_factor)[source]
stemtool.nbed.nbed_strain.cbed_filter(image, beam_rad, med_val=50, sec_med=True, hybridizer=0.25, bit_depth=32)[source]

Generate the filtered cross-correlated image for locating disk positions

Parameters:
  • image (ndarray) – The image to be filtered
  • beam_rad (float) – Radius of the circle. The circle used for cross-correlating is always centered at the image center.
  • med_val (float, optional) – Deviation from median value to accept in the Sobel filtered image. Default is 50
  • sec_med (bool, Optional) – Tamps out deviation from median values in the Sobel filtered image too if True
  • hybridizer (float, optional) – The value to use for hybrid cross-correlation. Default is 0.25. 0 gives pure cross correlation, while 1 gives pure phase correlation
  • bit_depth (int, optional) – Maximum power of 2 to be used for scaling the image when taking logarithms. Default is 32
Returns:

  • slm_image (ndarray) – The filtered image.
  • lsc_image (ndarray) – The filtered image cross-correlated with the circle edge

Notes

We first generate the circle centered at the X and Y co-ordinates, with the radius given inside the circ_vals tuple. This generated circle is the Sobel filtered to generate an edge of the circle.

Often due to detector issues, or stray muons a single pixel may be much brighter. Also dead pixels can cause individual pixels to be much darker. To remove such errors, and values in the image, we take the median value of the image and then throw any values that are med_val times larger or med_val times smaller than the median. Then we normalize the image from 1 to the 2^bit_depth and then take the log of that image. This generates an image whose scale is between 0 and the bit_depth. To further decrease detector noise, this scaled image is then Gaussian filtered with a single pixel blur, and then finally Sobel filtered. This Sobel filtered image is then cross-correlated with the Sobel filtered circle edge.

If there are disks in the image whose size is close to the radius of the circle, then the locations of them now become 2D peaks. If the circle radius is however too small/large rather than 2D peaks at diffraction disk locations, we will observe circles.

Examples

This is extremely useful for locating NBED diffraction positions. If you know the size and of the central disk you use the on the Mean_CBED to calculate the disk positions from:

>>> slm_reference, lsc_reference = st.nbed.cbed_filter(Mean_CBED, beam_r)
stemtool.nbed.nbed_strain.custom_detector(data4D, inner, outer=0, center=(0, 0), mrad_calib=0, get_detector=False)[source]

Return STEM image from detector values

Parameters:
  • data4D (ndarray) – the first two dimensions are Fourier space, while the next two dimensions are real space
  • inner (float) – The inner collection angle in Fourier space in pixels
  • outer (float, optional) – The inner collection angle in Fourier space in pixels. Default is 0
  • center (tuple, optional) – The center of the 4D-STEM pattern in Fourier space. Default is (0, 0)
  • mrad_calib (float, optional) – Calibration of the Fourier space. Default is 0.
  • get_detector (bool, optional) – Get the detector array if set to True. Default is False
Returns:

  • data_det (ndarray) – The STEM image from the detector region chosen
  • detector (ndarray, optional) – The boolean ndarray if get_detector is True

Notes

Based on the inner and outer collection angle the a STEM image is generated from the 4D-STEM dataset. We assume that the first two dimensions are the Fourier space, while the next two dimensions are real space scanning dimensions.

stemtool.nbed.nbed_strain.data4Dto2D(data4D)[source]

Convert 4D data to 2D data

Parameters:data4D (ndarray of shape (4,4)) – the first two dimensions are Fourier space, while the next two dimensions are real space
Returns:data2D – Raveled 2D data where the first two dimensions are positions while the next two dimensions are spectra
Return type:ndarray of shape (2,2)
stemtool.nbed.nbed_strain.get_radius(cbed_image, ubound=0.2, tol=0.0001)[source]

Find the size of the central disk from diffraction patterns.

Parameters:
  • cbed_image (ndarray) – The CBED image to be used for calculating the central convergence angle size from
  • ubound (float, optional) – The ratio of the size of the image to be used as the upper bound for the radius size. Default is 0.2. The lower bound is always 1 pixels
  • tol (float, optional) – The tolerance of the scipy.optimize fitting function Default is 0.01
Returns:

rad – The radius in pixels of the convergence aperture

Return type:

float

Notes

This is based on the idea that if a Sobel filtered CBED pattern is cross-correlated with a circle edge, a sharp peak is obtained only if the radius of that circle is the same as the radius in pixels of the CBED pattern. If the circle is a lot smaller or larger, then a donut like ring is formed at the center of the cross-correlated pattern. If the radius is a bit off - then the peak is a bit diffuse. Thus the correct radius in pixels will give the sharpest peak, which is how we find the radius by using scipy.optimize.minimize_scalar

See also

cbed_filter()

stemtool.nbed.nbed_strain.get_strain_plot(volume, roi, precision=(0.2, 0.001), upsampling=6)[source]

Get strain maps from the volume calculation and region of interest

Parameters:
  • volume (ndarray, float) – Calculated volume from strain
  • roi (ndarray, bool) – Region where the particle lies
  • precision (tuple) – The number of significant digits in each axes
  • upsampling (int) – Upsampling factor for the region before calculation
Returns:

  • rawvals (ndarray, float) – The raw calculated strain values from the region of interest
  • strain (ndarray, float) – Strain (second column) as a function of distance from particle surface (first column), with standard error (third column) and standard deviation (fourth column)
  • maxdist (float) – Maximum distance from surface

Notes

The ROI is upsampled by the upsampled by the upsampling factor, and the cartesian distances inside that ROI from the nearest edge point are calculated. That’s your x values, which is the distance from the surface, while the y values are the calculated volume strain at that position

stemtool.nbed.nbed_strain.peak_prominence(peak_pos, peak_im, fit_radius)[source]
stemtool.nbed.nbed_strain.resizer1D(data, N)[source]
stemtool.nbed.nbed_strain.resizer2D(data2D, sampling)[source]
stemtool.nbed.nbed_strain.rotate_and_center_ROI(data4D_ROI, rotangle, xcenter, ycenter)[source]

Rotation Corrector

Parameters:
  • data4D_ROI (ndarray) – Region of interest of the 4D-STEM dataset in the form of ROI pixels (scanning), CBED_Y, CBED_x
  • rotangle (float) – angle in counter-clockwise direction to rotate individual CBED patterns
  • xcenter (float) – X pixel co-ordinate of center of mean pattern
  • ycenter (float) – Y pixel co-ordinate of center of mean pattern
Returns:

corrected_ROI – Each CBED pattern from the region of interest first centered and then rotated along the center

Return type:

ndarray

Notes

We start by centering each 4D-STEM CBED pattern and then rotating the patterns with respect to the pattern center

stemtool.nbed.nbed_strain.rotation_finder(image_orig, axis=0)[source]

Angle Finder

Parameters:
  • image_orig ((2,2) shape ndarray) – Input Image
  • axis (int, optional) – Axis along which to perform sum
Returns:

min_x – Angle by which if the image is rotated by, the sum of the image along the axis specified is maximum

Return type:

float

Notes

Uses an internal angle_fun to sum the intensity along a particular axis. This will give the angle at which that sum is highest along the axis specified.

stemtool.nbed.nbed_strain.sobel_filter(image, med_filter=50)[source]
stemtool.nbed.nbed_strain.sort_edges(edge_map, edge_distance=5)[source]
stemtool.nbed.nbed_strain.spectra_finder(data4D, yvals, xvals)[source]
stemtool.nbed.nbed_strain.strain4D_general(data4D, disk_radius, ROI=0, prom_val=0, disk_center=<Mock name='mock.nan' id='140615505145040'>, max_radius=0, rotangle=0, med_factor=30, gauss_val=3, hybrid_cc=0.2, gblur=True, max_strain=0.1, take_median=True)[source]

Get strain from a ROI without the need for specifying Miller indices of diffraction spots

Parameters:
  • data4D (ndarray) – This is a 4D dataset where the first two dimensions are the diffraction dimensions and the next two dimensions are the scan dimensions
  • disk_radius (float) – Radius in pixels of the diffraction disks
  • ROI (ndarray, optional) – Region of interest. If no ROI is passed then the entire scan region is the ROI
  • prom_val (float, optional) – Minimum prominence value to use to use the peak for strain mapping. Default is 0
  • disk_center (tuple, optional) – Location of the center of the diffraction disk - closest to the <000> undiffracted beam
  • max_radius (float, optional) – Maximum distance from the center to use for calculation. Default is 0, when all distances are considered.
  • rotangle (float, optional) – Angle of rotation of the CBED with respect to the optic axis This must be in degrees
  • med_factor (float, optional) – Due to detector noise, some stray pixels may often be brighter than the background. This is used for damping any such pixels. Default is 30
  • gauss_val (float, optional) – The standard deviation of the Gaussian filter applied to the logarithm of the CBED pattern. Default is 3
  • hybrid_cc (float, optional) – Hybridization parameter to be used for cross-correlation. Default is 0.1
  • gblur (bool, optional) – If gblur is on, the strain maps are blurred by a single pixel. Default is True.
  • max_strain (float, optional) – Tamp strain value above this value. Default is 0.1
  • take_median (bool, optional) – If True, the Mean_CBED is the median diffraction pattern in the ROI. If False, it is the mean diffraction patter. Default is True
Returns:

  • e_xx_map (ndarray) – Strain in the xx direction in the region of interest
  • e_xy_map (ndarray) – Strain in the xy direction in the region of interest
  • e_th_map (ndarray) – Angular strain in the region of interest
  • e_yy_map (ndarray) – Strain in the yy direction in the region of interest
  • newROI (ndarray) – New reduced ROI where the prominence of the higher order diffraction disks is above prom_val
  • new_pos (ndarray) – List of all the higher order peak positions with respect to the central disk for all positions in the ROI
  • distances (ndarray) – List of distances in the newly calculated ROI from the particle edge.

Notes

We first of all calculate the preconditioned data (log + Sobel filtered) for every CBED pattern in the ROI. Then the mean preconditioned pattern is calculated and cross-correlated with the Sobel template. The disk positions are as peaks in this cross-correlated pattern, with the central disk the one closest to the center of the CBED pattern. Using that insight the distances of the higher order diffraction disks are calculated with respect to the central transmitted beam. This is then performed for all other CBED patterns. The calculated higher order disk locations are then compared to the higher order disk locations for the median pattern to generate strain maps. However, sometimes the ROI may contain points where there is no diffraction pattern actually. To prevent picking such points and generating erroneous results, we calculate the peak prominence of every higher order diffraction spot, and only if they are more prominent than prom_val they will be chosen. If prom_val is zero, then all peaks are chosen.

stemtool.nbed.nbed_strain.strain_figure(exx, exy, eth, eyy, ROI, vm=0, scale=0, scale_unit='nm', figsize=(22, 21))[source]

Plot the strain maps from a given set of strains, where the strain is mapped only in the region of interest, while anything outside the region of interest is black.

Parameters:
  • exx (ndarray) – 2D array of e_xx strains
  • eyy (ndarray) – 2D array of e_yy strains
  • eth (ndarray) – 2D array of e_theta strains
  • eyy – 2D array of e_yy strains
  • ROI (ndarray of type bool) – Region of interest
  • vm (float, optional) – Maximum value of strain map Default is 0, and when it’s zero, it will calculate the maximum absolute strain value and use that.
  • scale (float, optional) – Pixel size. Default is 0
  • scale_unit (char, optional) – Units of the calibration. Default is ‘nm’
  • figsize (tuple, optional) – Size of the final figure

Notes

This is basically a nice function for plotting calculated strain maps from a region of interest. We use the RdBu_r, which is the RdBu reversed color scheme, where 0 is white, negative values are increasingly blue the more negative they are, while positive values are increasingly red the more positive they are. Anything outside the region of interest is black, and thus to an observer the ROI can be visualized clearly. Optionally, a scalebar can be assigned too.

stemtool.nbed.nbed_strain.test_aperture(pattern, center, radius, showfig=True)[source]

Test an aperture position for Virtual DF image

Parameters:
  • pattern (ndarray of shape (2,2)) – Diffraction pattern, preferably the mean diffraction pattern for testing out the aperture location
  • center (ndarray of shape (1,2)) – Center of the circular aperture
  • radius (float) – Radius of the circular aperture
  • showfig (bool, optional) – If showfig is True, then the image is displayed with the aperture overlaid
Returns:

aperture – A matrix of the same size of the input image with zeros everywhere and ones where the aperture is supposed to be

Return type:

ndarray of shape (2,2)

Notes

Use the showfig option to visually test out the aperture location with varying parameters