ibr_aic package

Submodules

ibr_aic.cli module

ibr_aic.ibr_aic module

class ibr_aic.ibr_aic.HasEnergyMu(*args, **kwargs)[source]

Bases: Protocol

Protocol to define the class that has energy and mu

This class is used for type hinting. Group class from xraylarch also has a energy and mu attributes, and can be used as an input for IbrAic class.

energy: ndarray
mu: ndarray
class ibr_aic.ibr_aic.IbrAic(energy_list: list[ndarray] | None = None, mu_list: list[ndarray] | None = None, group_list: Sequence[HasEnergyMu] | None = None, file_list=None)[source]

Bases: object

IbrAic: A class to remove Bragg peaks from XAS spectra

Usage:

Following is an example usage of the class.

  1. Prepare list of energy, and mu. The energy and mu has to be in the format of list of np.ndarray.

  2. Create an instance of the class.

  3. Call the calc_bragg_iter method to iteratively calculate the Bragg peaks.

  4. Call the save_dat method to save the spectra.

from ibr_aic import IbrAic

energy_list, mu_list, file_list = prepare_spectra_from_qas(file_path)

ix = IbrAic(energy_list, mu_list, file_list)
ix.calc_bragg_iter().save_dat()
Parameters:
  • energy_list (list[np.ndarray]) – List of the energy

  • mu_list (list[np.ndarray]) – List of the mu

  • group_list (list[HasEnergyMu]) – List of the group

  • file_list (list[str]) – List of the file path

calc_bragg(energy_range: list[float] | None = None, scale_range: list[float] | None = None) Self[source]

Function to caculate the Bragg peaks

The caulculation of the Bragg peaks is done in iterative manner and this function corresponds to 1 iteration.

  1. Choose a reference spectrum where the Bragg peaks will be calculated.

  2. Calaculate the scaling factor for the other spectra to match the reference spectrum. The loss fuction is the mean square root error, to reduce the effect of the Bragg peaks.

  3. Subtract the scaled spectra from the reference spectrum. This difference spectrum will be corresponding to the Bragg peaks, but it is not remove completely.

  4. Take an average of the difference spectra, add it to the reference spectrum, and go back to step 2.

  5. Repeat step 2 to 4 until the difference spectrum converges.

Parameters:
  • energy_range (list[float], optional) – Energy range to be used for the calculation of the loss function. Defaults to [-np.inf, np.inf].

  • scale_range (list[float], optional) – Range of the scaling factor. Defaults to [0.5, 1.5].

Returns:

Reference to the class itself

Return type:

self

calc_bragg_iter(energy_range: list[float] | None = None, scale_range: list[float] | None = None, criteria: float = 1e-10, max_iter=200) Self[source]

Iterative calculation of the Bragg peaks

Calculate the Bragg peaks in iterative manner. The calculation will be stopped when the difference between the previous and current update is lower than the criteria. The amount of update is defined by MAE of the spectra, and has a same unit as absorption.

Parameters:
  • energy_range (list[float], optional) – Energy range to be used for the calculation of the loss function. Defaults to [-np.inf, np.inf].

  • scale_range (list[float], optional) – Range of the scaling factor. Please change this value if if the absolute intensity changes significantly larger than 1. Defaults to [0.5, 1.5].

  • criteria (float, optional) – The criteria to stop the calculation. Defaults to 1e-5.

  • max_iter (int, optional) – Maximum number of iteration. Defaults to 200.

Returns:

Reference to the class itself

Return type:

self

energy_list: list[ndarray]
file_list: list[str] | None
group_list: Sequence[HasEnergyMu] | None
ibr_loss: float
interpolate() Self[source]
loss_func(x: float, spectrum: ndarray, reference: ndarray, index: ndarray | tuple, weight: str = 'MSRE') ndarray[source]

Mean square root error function for the optimization of the scaling factor

The mean square is used for calculation of the loss function. If the mean square root or the mean absolute error is used, the optimization will be biased to the Bragg peaks, and there will be a large error around the Bragg peaks. The use of square root will enhance the possibility of fitting to the baseline spectrum.

Parameters:
  • x (float) – scaling factor

  • spectrum (np.ndarray) – spectrum to be scaled

  • reference (np.ndarray) – reference spectrum

  • index (np.ndarray) – index of the energy range to be used for the calculation of the loss function

  • weight (str) – weighting ot calculate the scale. default is mean square root error (MSRE). Other option is mean absolute error (MAE), and mean squared error (MSE).

Returns:

mean square root error

Return type:

np.ndarray

loss_spectrum(spectrum_list: list[ndarray], reference: ndarray, ref_index: int, energy_range: list[float] | None = None, scale_range: list[float] | None = None, weight: str = 'MSRE') ndarray[source]
min_mu_list: list[ndarray]
mu_list: list[ndarray]
save_dat(file_list: list[str] | None = None, output_dir: str = './output/') Self[source]

Save dat

Save the spectra to the dat file. The file name will be the basename of the file_list.

Parameters:
  • file_list (list[str], optional) – List of the file names. If none, the file_name will be taken from the __init__ arguments of the class. Defaults to None.

  • output_dir (str, optional) – The directory to save the ouput dat files. Defaults to “./output/”

Returns:

Self

scale_list: list[ndarray]
ibr_aic.ibr_aic.prepare_group_from_QAS(file_path: str, fluorescence: bool = True) tuple[list[HasEnergyMu], list[str]][source]

Example of preparing an input(group) for IbrAic

Please pip install xraylarch if you want to use this function.

Parameters:
  • file_path (str) – Path to the XAS file. The file_path has to be in the format of glob.

  • fluorescence (bool, optional) – If True, the fluorescence XAS will be used. If False, the absorption XAS will be used. Defaults to True.

Returns:

List of the group file_list(list[str]): List of the file path

Return type:

group_list(list[Group])

ibr_aic.ibr_aic.prepare_spectra_from_QAS(file_path: str, fluorescence: bool = True) tuple[list[ndarray], list[ndarray], list[str]][source]

Example of preparing a input for IbrAic class

Parameters:
  • file_path (str) – Path to the XAS file. The file_path has to be in the format of glob.

  • fluorescence (bool, optional) – If True, the fluorescence XAS will be used. If False, the absorption XAS will be used. Defaults to True.

  • file_path – Path to the XAS file. The file_path has to be in the format of glob.

Returns:

List of the energy mu_list(list[np.ndarray]): List of the mu file_list(list[str]): List of the file path

Return type:

energy_list(list[np.ndarray])

Module contents

Top-level package for IBR-AIC