xpdtools package

Subpackages

Submodules

xpdtools.calib module

Tools for running pyFAI calibraion

xpdtools.calib.img_calibration(img, wavelength, calibrant='Ni', detector='perkin_elmer', calib_ref_fp=None, **kwargs)[source]

Function to calibrate experimental geometry for an image

Parameters:
  • img (ndarray) – 2D powder diffraction image from calibrant

  • wavelength (float) – x-ray wavelength in angstrom.

  • calibrant (str, optional) – calibrant being used, default is ‘Ni’. input could be “full file path” to customized d-spacing file with “.D” extension or one of pre-defined calibrant names. List of pre-defined calibrant names: [‘NaCl’, ‘AgBh’, ‘quartz’, ‘Si_SRM640’, ‘Ni’, ‘Si_SRM640d’, ‘Si_SRM640a’, ‘alpha_Al2O3’, ‘LaB6_SRM660b’, ‘TiO2’, ‘CrOx’, ‘LaB6_SRM660c’, ‘CeO2’, ‘Si_SRM640c’, ‘CuO’, ‘Si_SRM640e’, ‘PBBA’, ‘ZnO’, ‘Si’, ‘C14H30O’, ‘cristobaltite’, ‘LaB6_SRM660a’, ‘Au’, ‘Cr2O3’, ‘Si_SRM640b’, ‘LaB6’, ‘Al’, ‘mock’]

  • detector (str or pyFAI.detector.Detector instance, optional.) – detector used to collect data. default value is ‘perkin-elmer’. other allowed values are in pyFAI documentation.

  • calib_ref_fp (str, optional) – full file path to where the native pyFAI calibration information will be saved. Default to current working directory.

  • kwargs – Additional keyword argument for calibration. please refer to pyFAI documentation for all options.

Returns:

ai – instance of AzimuthalIntegrator. Can be used to integrate 2D images directly.

Return type:

pyFAI.AzimuthalIntegrator

Examples

calib Ni image with pyFAI default Ni.D d-spacing with wavlength 0.1823 angstrom

>>> import tifffile as tif
>>> ni_img = tif.imread('<path_to_img_file>')
>>> ai = img_calibration(ni_img, 0.1823)

calib Ni image with pyFAI customized myNi.D d-spacing with wavlength 0.1823 angstrom

>>> import tifffile as tif
>>> ni_img = tif.imread('<path_to_img_file>')
>>> ai = img_calibration(ni_img, 0.1823, 'path/to/myNi.D')

integrate image right after calibration

>>> import matplotlib.pyplot as plt
>>> npt = 1482 # just a number for demonstration
>>> q, Iq = ai.integrate1d(ni_img, npt, unit="q_nm^-1")
>>> plt.plot(q, Iq)

References

pyFAI documentation: http://pyfai.readthedocs.io/en/latest/

xpdtools.dev_utils module

Utilities for making the developer’s lives easier

xpdtools.shim module

Shims

class xpdtools.shim.PDFGetterShim[source]

Bases: object

Shim for PDFGetX3

xpdtools.tools module

Tools for x-ray scattering data processing

xpdtools.tools.binned_outlier(img, binner, alpha=3, tmsk=None, mask_method='median', pool=None)[source]

Sigma Clipping based masking

Parameters:
  • img (np.ndarray) – The image

  • binner (BinnedStatistic1D instance) – The binned statistics information

  • alpha (float, optional) – The number of standard deviations to clip, defaults to 3

  • tmsk (np.ndarray, optional) – Prior mask. If None don’t use a prior mask, defaults to None.

  • mask_method ({‘median’, ‘mean’}, optional) – The method to use for creating the mask, median is faster, mean is more accurate. Defaults to median.

  • pool (Executor instance) – A pool against which jobs can be submitted for parallel processing

Returns:

The mask

Return type:

np.ndarray

xpdtools.tools.call_stream_element(callable_item, *args, **kwargs)[source]

Call callable_item on the args and kwargs

xpdtools.tools.check_in(x, k)[source]
xpdtools.tools.check_kwargs(x, k, v, **kwargs)[source]
xpdtools.tools.decomp(data, n_components=0.9, model=<class 'sklearn.decomposition._pca.PCA'>, **kwargs)[source]

Perform a decomposision of the data

Parameters:
  • data (list of ndarray) – The list of data to be decomposed

  • n_components (int, flot, None or str) – The number of components passed to the model initiaion

  • model (class) – The model class, must take in n_components, have a fit method and a components_ attribute

  • kwargs (dict) – The kwargs to pass to the model creation

Returns:

  • eigen (ndarray) – The eigenvectors

  • scores (ndarray) – The scores for each component

xpdtools.tools.fq_getter(x, y, composition, **kwargs)[source]

Process the data to F(Q)

Parameters:
  • x (ndarray) – The q or tth values

  • y (ndarray) – The scattered intensity

  • composition (str) – The composition

  • kwargs (dict) – Additional kwargs for PDFGetter

Returns:

  • q (ndarray) – The radial values

  • fq (ndarray) – The reduced structure function

  • config (dict) – The PDFGetter config

xpdtools.tools.generate_binner(geo, img_shape, mask=None)[source]

Create a pixel resolution BinnedStats1D instance

Parameters:
  • geo (pyFAI.geometry.Geometry instance) – The calibrated geometry

  • img_shape (tuple, optional) – The shape of the image, if None pull from the mask. Defaults to None.

  • mask (np.ndarray, optional) – The mask to be applied, if None no mask is applied. Defaults to None.

Returns:

The configured instance of the binner.

Return type:

BinnedStatistic1D

xpdtools.tools.generate_map_bin(geo, img_shape)[source]

Create a q map and the pixel resolution bins

Parameters:
  • geo (pyFAI.geometry.Geometry instance) – The calibrated geometry

  • img_shape (tuple, optional) – The shape of the image, if None pull from the mask. Defaults to None.

Returns:

  • q (ndarray) – The q map

  • qbin (ndarray) – The pixel resolution bins

xpdtools.tools.ignore_streamz_input(func)[source]
xpdtools.tools.load_geo(cal_params)[source]

Load a pyFAI geometry from a dict of calibration parameters

Parameters:

cal_params (dict) – The calibration parameters

Returns:

The calibrate azimuthal integrator (which inherits from the geometry)

Return type:

AzimuthalIntegrator

xpdtools.tools.map_to_binner(pixel_map, bins, mask=None)[source]

Transforms pixel map and bins into a binner

Parameters:
  • pixel_map (np.ndarray) – The map between pixels and values

  • bins (np.ndarray) – The bins to use in the binner

  • mask (np.ndarray, optional) – The mask for the pixel map

Returns:

The binner

Return type:

BinnedStatistic1D

xpdtools.tools.mask_img(img, binner, edge=30, lower_thresh=0.0, upper_thresh=None, alpha=3, auto_type='median', tmsk=None, pool=None)[source]

Mask an image based off of various methods

Parameters:
  • img (np.ndarray) – The image to be masked

  • binner (BinnedStatistic1D instance) – The binned statistics information

  • edge (int, optional) – The number of edge pixels to mask. Defaults to 30. If None, no edge mask is applied

  • lower_thresh (float, optional) – Pixels with values less than or equal to this threshold will be masked. Defaults to 0.0. If None, no lower threshold mask is applied

  • upper_thresh (float, optional) – Pixels with values greater than or equal to this threshold will be masked. Defaults to None. If None, no upper threshold mask is applied.

  • alpha (float, optional) – Then number of acceptable standard deviations, if tuple then we use a linear distribution of alphas from alpha[0] to alpha[1], if array then we just use that as the distribution of alphas. Defaults to 3. If None, no outlier masking applied.

  • auto_type ({‘median’, ‘mean’}, optional) – The type of binned outlier masking to be done, ‘median’ is faster, where ‘mean’ is more accurate, defaults to ‘median’.

  • tmsk (np.ndarray, optional) – The starting mask to be compounded on. Defaults to None. If None mask generated from scratch.

  • pool (Executor instance) – A pool against which jobs can be submitted for parallel processing

Returns:

tmsk – The mask as a boolean array. True pixels are good pixels, False pixels are masked out.

Return type:

np.ndarray

xpdtools.tools.move_center(motors, geometry)[source]

Move the PONI for pyFAI based off of a diffx/diffy motor move

Parameters:
  • motors (tuple) – X, Y diff from geometry position

  • geometry (pyFAI.geometry.Geometry)

xpdtools.tools.nu_fq_getter(q, iq, composition, **kwargs)[source]

Process the data to F(Q) on a non uniform grid

Parameters:
  • q (ndarray) – The q or tth values

  • iq (ndarray) – The scattered intensity

  • composition (str) – The composition

  • kwargs (dict) – Additional kwargs for PDFGetter

Returns:

  • q (ndarray) – The radial values

  • fq (ndarray) – The reduced structure function

  • config (dict) – The PDFGetter config

xpdtools.tools.nu_pdf_getter(q, fq)[source]

Process a non uniform F(Q) to the PDF

Parameters:
  • q (ndarray) – The q or tth values

  • fq (ndarray) – The reduced structure funciton

Returns:

  • r (ndarray) – The radial values

  • gr (ndarray) – The PDF

xpdtools.tools.overlay_mask(img, mask)[source]

Overlay mask on image, masked pixels are np.nan

xpdtools.tools.pdf_getter(x, y, composition, **kwargs)[source]

Process the data to the PDF

Parameters:
  • x (ndarray) – The q or tth values

  • y (ndarray) – The scattered intensity

  • composition (str) – The composition

  • kwargs (dict) – Additional kwargs for PDFGetter

Returns:

  • r (ndarray) – The radial values

  • gr (ndarray) – The PDF

  • config (dict) – The PDFGetter config

xpdtools.tools.pluck_check(t, position, eq='~~pluck_check_null~~')[source]

Check if a position in an iterable is truthy or equal to eq

xpdtools.tools.polarization_correction(img, geo, polarization_factor=0.99)[source]

Perform polarization correction on an image

Parameters:
  • img (ndarray) – The image

  • geo (pyFAI.geometry.Geometry instance) – The calibrated geometry

  • polarization_factor (float) – The polarization factor to apply

Returns:

The corrected image

Return type:

ndarray

xpdtools.tools.progress_decorator(func, progress=None)[source]
xpdtools.tools.splay_tuple(iter_of_tuple)[source]

Splay all tuples in an iterable

Parameters:

iter_of_tuple (iterable) – An iterable which may contain tuples

Returns:

The splayed output

Return type:

tuple

xpdtools.tools.sq_getter(x, y, composition, **kwargs)[source]

Process the data to F(Q)

Parameters:
  • x (ndarray) – The q or tth values

  • y (ndarray) – The scattered intensity

  • composition (str) – The composition

  • kwargs (dict) – Additional kwargs for PDFGetter

Returns:

  • q (ndarray) – The radial values

  • fq (ndarray) – The reduced structure function

  • config (dict) – The PDFGetter config

xpdtools.tools.z_score_image(img, binner)[source]

Z score an image according to the azimuthal average

Parameters:
  • img (ndarray) – The image

  • binner (BinnedStatistic1D instance) – The binner

Returns:

The z scored image

Return type:

ndarray

Module contents