xasanalysis package
Submodules
xasanalysis.xasanalysis module
- class xasanalysis.xasanalysis.XASAnalysis(groups: dict[str, Group] | None = None, e0: float | None = None, pre_edge_kws: dict | None = None, autobk_kws: dict | None = None, xftf_kws: dict | None = None, reference: Group | None = None, groups_ref: dict[str, Group] | None = None)[source]
Bases:
objectClass to analyze the XAS data
This class aims to provide a simple interface to analyze the XAS data. The class is based on the larch library, which is a python library for X-ray absorption spectroscopy. It is a abstraction of my workflow to analyze the XAS data, which includes the following steps:
Read tha data (This will be done by different functions)
Read the reference data (This will done using the xasref module). I am planning to replace the xasref to a better module in the future with the help of beamline scientists.
Align the data to the reference data. (The energy alignment is usually done using the first derivative of the absorption coefficients. This module uses spdist metric and mean absolute error as the metric to align the data)
Set the e0, pre-edge, autobk, xftf parameters. (This step is not nessaary, but it is useful to use the same parameters for all the groups to get the consistent results)
Normalization using the pre-edge function of xraylarch
Background removal using the autobk function of xraylarch
Fourier transform using the xftf function of xraylarch
Plotting the results (This class provides the simple interface to plot the results)
- groups
dictionary of the groups
- Type:
dict
- e0
e0 of the spectrum
- Type:
float
- pre_edge_kws
keywords for the pre_edge function
- Type:
dict
- autobk_kws
keywords for the autobk function
- Type:
dict
- xftf_kws
keywords for the xftf function
- Type:
dict
- reference
reference spectrum
- Type:
Group
- groups_ref
dictionary of the reference groups
- Type:
dict
- Parameters:
groups (dict) – dictionary of the groups. Default is None.
e0 (float) – e0 of the spectrum. Default is None, which allows a automatic detection.
pre_edge_kws (dict) – keywords for the pre_edge function. Default is None, which allows a automatic detection.
autobk_kws (dict) – keywords for the autobk function. Default is None, which allows a automatic detection.
xftf_kws (dict) – keywords for the xftf function. Default is None, which allows a automatic detection.
reference (Group) – reference spectrum. Default is None.
groups_ref (dict) – dictionary of the references in each group. Default is None.
Examples
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> e0 = 8345 >>> pre_edge_kws: dict = { "e0": None, "step": None, "nnorm": 3, "nvict": 0, "pre1": -150, "pre2": -45, "norm1": 70, "norm2": None, } >>> autobk_kws: dict = {"rbkg": 1.0, "kmax": None, "kweight": 2} >>> xftf_kws: dict = {"kmin": 2, "kmax": 8, "dk": 2, "window": "hanning"} >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> group1 = read_xmu("example1.xmu", "example1") >>> group1_ref = read_xmu("example1_ref.xmu", "example1_ref") >>> xa.add_group(group1, "group1", align_ref=group1_ref) >>> xa.plot_exkr( ref=True, plot_erange="full", plot_krange=[0, 15], plot_rrange=[0, 8], save_path="./out/Ni_exkr.png", )
- add_group(group: Group, name: str, align_ref: Group | None = None) Self[source]
Add the group to the class
This function adds the group to the groups dictionary. If the align_ref is provided, it will align the align_ref to the reference spectra that is registered. This function will raise an exception if the align_ref is provided but the reference is not set.
- Parameters:
group (Group) – group object
name (str) – name of the group
align_ref (Group) – reference group to align the group. Default is None.
- Returns:
self. This method can be chained.
- Return type:
Self
Examples
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> xa.set_reference_from_db("Ni K", label="Ni foil") >>> group1 = read_xmu("example1.xmu", "example1") >>> group1_ref = read_xmu("example1_ref.xmu", "example1_ref") >>> xa.add_group(group1, "group1", align_ref=group1_ref)
- add_merge_group(groups: list[Group], name: str, align_ref: Group | list[Group] | None = None) Self[source]
Add the group to the class. The list of group will be merged prior to the addition
This function adds the group to the groups dictionary. This function will merge the list of the groups before adding to the class.
If the align_ref is provided, it will align the align_ref to the reference spectra that is registered. This function will raise an exception if the align_ref is provided but the reference is not set. The align_ref can also be a list of groups, which will be merged before aligning the group.
- Parameters:
groups (list[Group]) – list of group objects
name (str) – name of the group
align_ref (Group | list[Group]) – reference group to align the group. Default is None.
- Returns:
self. This method can be chained.
- Return type:
Self
- autobk(skip_pre_edge=True) Self[source]
Background removal of the groups
This method will calculate the background removal of the groups. It is highly recommended to set the e0 and the autobk_kws before running this method. If it is not set, it will automatically detect the parameters for each group, and the parameters will not be consitent between the groups.
- Parameters:
skip_pre_edge (bool) – skip the pre-edge calculation. Default is True.
- Returns:
self. This method can be chained.
- Return type:
Self
- autobk_kws: dict
- calc_feff8l_from_structure(structure: str, absorber: str, dir='./feff/', edge=None, cluster_size=8.0, absorber_site=1, site_index=None, extra_titles=None, with_h=False, version8=True, fmt='cif', rng_seed=None, verbose: bool = True) Self[source]
- calc_shift(group: Group, fit_range: list[float] | None = None, max_shift: float = 20.0) float[source]
Calculate the shift of the spectrum, with respect to the reference spectrum using spdist+MAE as the metric
This method calculates the shift of the spectrum, with respect to the reference spectrum using spdist+MAE as the metric. The main strategy of calculating the metric is the following:
Interpolate the spectrum to the energy grid (for example, 0.5eV spacing, this ensures that each energy contribution will be uniformly weighted)
Calculate the flattened spectrum using the pre_edge function of xraylarch.
Interpolate the the flattened spectrum to the energy grid
Calculate the spdist metric between the spectrum and the reference spectrum.
Calculate the mean absolute error between the spectrum and the reference spectrum
Return the average of the spdist metric and the mean absolute error
The spdist is a average of the minimum distance between the two spectra, and the mean absolute error is the average of the absolute vertical distance between the two spectra. spdist metric is more sensitive towards the distance in the diagnal direnction of the two spectra. This is useful to align the spectra with similar features.
- Parameters:
group (Group) – group of the spectrum
fit_range (list) – the fitting range for the metric calculation. The default is None, which will use the e0 - 20eV to e0 + 80eV.
max_shift (float) – maximum shift of the spectrum. The default is 20.0. Please increase the value if the spectrum is totally misaligned.
- Returns:
shift of the spectrum. The calibrated energy is energy + shift.
- Return type:
shift(float)
Examples
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> xa.set_reference_from_db("Ni K", label="Ni foil") >>> group1_ref = read_xmu("example1_ref.xmu", "example1_ref") >>> shift = xa.calc_shift(group1_ref, fit_range = [e0 - 20, e0 + 80], max_shift = 20.0)
- e0: float | None
- find_e0_from_derivative(index: int = 0) float[source]
Find the e0 of the group using the first derivative of the absorption coefficients
This method will find the e0 of the group using the first derivative of the absorption coefficients. This method is useful to find which e0 to use for all of the spectra.
- Returns:
e0 of the group
- Return type:
float
- get_e0() float[source]
Get the e0 of the class
This method will return the self.e0 if it is set. If it is not set, it will return the e0 of the first group in the groups dictionary.
- Returns:
e0 of the class
- Return type:
float
- get_kweight() int[source]
Get the kweight of the class
This method will return the kweight of the xftf_kws if it is set. If it is not set, it will return 2. This will be used for the plotting and the Fourier transform.
- Returns:
kweight of the class
- Return type:
int
- groups: dict[str, Group]
- groups_ref: dict[str, Group]
- has_chi(groups_name: list[str] | None = None) bool[source]
Check if the groups have the chi attribute
This mehtod will check if the groups have the chi attribute. The check will only be done for the groups that are in the groups_name list. If the groups_name is None, it will check all the groups.
- Returns:
True if all the groups have the chi attribute, False otherwise.
- Return type:
bool
- has_chir(groups_name: list[str] | None = None) bool[source]
Check if the groups have the chir attribute
This method will check if the groups have the chir attribute. The check will only be done for the groups that are in the groups_name list. If the groups_name is None, it will check all the groups.
- Returns:
True if all the groups have the chir attribute, False otherwise.
- Return type:
bool
- has_flat(groups_name: list[str] | None = None) bool[source]
Check if the groups have the flat attribute
This method will check if the groups have the flat attribute. The check will only be done for the groups that are in the groups_name list. If the groups_name is None, it will check all the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None.
- Returns:
True if all the groups have the flat attribute, False otherwise.
- Return type:
bool
- has_flat_refs(groups_name: list[str] | None = None) bool[source]
Check if the reference groups have the flat attribute
This method will check if the reference groups have the flat attribute. The check will only be done for the groups that are in the groups_name list. If the groups_name is None, it will check all the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None.
- Returns:
True if all the reference groups have the flat attribute, False otherwise.
- Return type:
bool
- order_groups(order: list[str]) Self[source]
Reorder the groups dictionary
- Parameters:
order (list) – order of the groups
- Returns:
self. This method can be chained.
- Return type:
Self
- plot_ek(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_erange: str | tuple[float, float] | list[float] | None = 'full', plot_krange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the energy and k spectra
This method will plot the energy and k spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_erange (str | tuple | list) – plot range of the energy spectra. Default is “full”.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_ek( ref=True, plot_erange="full", plot_krange=[0, 15], plot_legend="e", save_path="./out/Ni_ek.png", )
- plot_ekr(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_erange: str | tuple[float, float] | list[float] | None = 'full', plot_krange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the energy, k, and R spectra
This method will plot the energy, k, and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_erange (str | tuple | list) – plot range of the energy spectra. Default is “full”.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_ekr( ref=True, plot_erange="full", plot_krange=[0, 15], plot_rrange=[0, 8], plot_legend="e", save_path="./out/Ni_ekr.png", )
- plot_er(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_erange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the energy and R spectra
This method will plot the energy and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_erange (str | tuple | list) – plot range of the energy spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_er( ref=True, plot_erange="full", plot_rrange=[0, 8], save_path="./out/Ni_er.png", )
- plot_exkr(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_erange: str | tuple[float, float] | list[float] | None = 'full', plot_krange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the energy, xanes, k, and R spectra
This method will plot the energy, xanes, k, and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_erange (str | tuple | list) – plot range of the energy spectra. Default is “full”.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_exkr( ref=True, plot_erange="full", plot_krange=[0, 15], plot_rrange=[0, 8], save_path="./out/Ni_exkr.png", )
- plot_flat(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_range: str | tuple[float, float] | list[float] | None = 'full', plot_range_y: tuple[float, float] | list[float] | None = None, ref: bool = False, plot_legend: bool = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Axes | None = None, fig: Figure | None = None) Axes[source]
Plot the flattened spectra
This method will plot the flattend spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_range (str | tuple | list) – plot range of the spectra. Default is “full”.
plot_range_y (tuple | list) – plot range of the y-axis. Default is None.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool) – plot the legend. Default is True.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Axes) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
axes of the plot
- Return type:
Axes
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_flat( ref=True, plot_range="xanes", save_path="./out/Ni_xanes.png", )
- plot_flat_refs(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_range: str | tuple[float, float] | list[float] | None = 'xanes', plot_range_y: tuple[float, float] | list[float] | None = None, ref: bool = True, plot_legend: bool = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Axes | None = None, fig: Figure | None = None) Axes[source]
Plot the flattened reference spectra
This method is used to check the alignment of the reference spectra. It will plot the flattened reference spectra of the groups. The plotting range is set to “xanes” by default to plot the xanes region of the spectra.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_range (str | tuple | list) – plot range of the spectra. Default is “xanes”.
plot_range_y (tuple | list) – plot range of the y-axis. Default is None.
ref (bool) – plot the reference spectrum. Default is True.
plot_legend (bool) – plot the legend. Default is True.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Axes) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
matplotlib.axes.Axes of the plot
- Return type:
Axes
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_flat_refs( plot_range="xanes", save_path="./out/Ni_xanes_ref.png", )
- plot_k(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_range: str | tuple[float, float] | list[float] | None = 'full', plot_range_y: tuple[float, float] | list[float] | None = None, ref: bool = False, plot_legend: bool = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Axes | None = None, fig: Figure | None = None) Axes[source]
Plot the chi(k) spectra
This method will plot the chi(k) spectra of the groups. The plotting k-weight is set to the k-weight define by the xftf_kws. This is to prevent the inconsistency of the plotting and the fast Fourier transform.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_range (str | tuple | list) – plot range of the spectra. Default is “full”.
plot_range_y (tuple | list) – plot range of the y-axis. Default is None.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool) – plot the legend. Default is True.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Axes) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
matplotlib.axes.Axes of the plot
- Return type:
Axes
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_k( ref=True, plot_range=[0, 15], save_path="./out/Ni_k.png", legend_kws={ "bbox_to_anchor": (1.05, 1), "loc": "upper left", }, )
- plot_kr(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_krange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the k and R spectra
This method will plot the k and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_kr( ref=True, plot_krange=[0, 15], plot_rrange=[0, 8], save_path="./out/Ni_kr.png", )
- plot_multi(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_erange: str | tuple[float, float] | list[float] | None = 'full', plot_krange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Axes | Sequence[Axes] | None = None, fig: Figure | None = None, plot_figures: str | list[str] | None = None) Axes | Sequence[Axes][source]
general method for plotting multiple figures.
This method is the general method for plotting multiple figures. Please use following functions for the specific plots:
plot_flat: plot the flattened spectra
plot_flat_refs: plot the flattened reference spectra
plot_k: plot the chi(k) spectra
plot_r: plot the chi(R) spectra
plot_ekr: plot the energy, k, and R spectra
plot_ek: plot the energy and k spectra
plot_er: plot the energy and R spectra
plot_kr: plot the k and R spectra
plot_exkr: plot the energy, k, and R spectra
plot_xkr: plot the xanes, k, and R spectra
plot_xk: plot the xanes and k spectra
plot_xr: plot the xanes and R spectra
This method is a recursive method depending on the instance of the plot_figures. If the plot_figures is a list, it will call the plot_multi for each of the figures. If the plot_figures is a string, it will call the specific plot method for the figure.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_erange (str | tuple | list) – plot range of the energy spectra. Default is “full”.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Axes | Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
plot_figures (str | list[str] | None) – plot figures. Default is None, which will plot all the figures. The options are “e”, “x”, “k”, and “r”. It can also be passed as a list. If it is a None it will plot “e”, “k”, and “r” the figures.
- Returns:
matplotlib.axes.Axes of the plot or a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Axes | Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters. >>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws)
.set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db(“Ni K”, label=”Ni foil”))
>>> xa.add_group(group1, "group1") >>> xa.plot_multi( ref=True, plot_erange="full", plot_krange=[0, 15], plot_rrange=[0, 8], save_path="./out/Ni_ekr.png", plot_legend="e", plot_figures=["e", "x", "k", "r"], )
- plot_r(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_range: str | tuple[float, float] | list[float] | None = 'full', plot_range_y: tuple[float, float] | list[float] | None = None, ref: bool = False, plot_legend: bool = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Axes | None = None, fig: Figure | None = None) Axes[source]
Plot the chi(R) spectra
This method will plot the chi(R) spectra of the groups. The units will be set based on the k-weight of the Fourier transform.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_range (str | tuple | list) – plot range of the spectra. Default is “full”.
plot_range_y (tuple | list) – plot range of the y-axis. Default is None.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool) – plot the legend. Default is True.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Axes) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
matplotlib.axes.Axes of the plot
- Return type:
Axes
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_r( ref=True, plot_range=[0, 8], save_path="./out/Ni_r.png", legend_kws={ "bbox_to_anchor": (1.05, 1), "loc": "upper left", }, )
- plot_range_yaxis(x: ndarray, y: ndarray, plot_range: list[float] | None = None) list[float][source]
- plot_xk(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_krange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the xanes and k spectra
This method will plot the xanes and k spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_xk( ref=True, plot_krange=[0, 15], save_path="./out/Ni_xk.png", )
- plot_xkr(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_krange: str | tuple[float, float] | list[float] | None = 'full', plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the xanes, k, and R spectra
This method will plot the xanes, k, and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_krange (str | tuple | list) – plot range of the k spectra. Default is “full”.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_xkr( ref=True, plot_krange=[0, 15], plot_rrange=[0, 8], save_path="./out/Ni_xkr.png", )
- plot_xr(groups_name: list[str] | None = None, ignore_kws: list[str] | None = None, plot_rrange: str | tuple[float, float] | list[float] | None = 'full', ref: bool = False, plot_legend: bool | list[int] | str = True, legend_kws: dict | None = None, save_path: str | None = None, ax: Sequence[Axes] | None = None, fig: Figure | None = None) Sequence[Axes][source]
Plot the xanes and R spectra
This method will plot the xanes and R spectra of the groups.
- Parameters:
groups_name (list) – list of the group names. Default is None, which will plot all the groups.
ignore_kws (list) – list of the keywords to ignore in the group names. Default is None.
plot_rrange (str | tuple | list) – plot range of the R spectra. Default is “full”.
ref (bool) – plot the reference spectrum. Default is False.
plot_legend (bool | list | str) – plot the legend. Default is True. The options are True, False, “all”, “e”, “x”, “k”, “r”, and “left”. The “left” will plot the legend on the left side of the plot.
legend_kws (dict) – legend keywords. Default is None.
save_path (str) – save path of the figure. Default is None.
ax (Sequence[Axes]) – axes of the plot. Default is None.
fig (Figure) – figure of the plot. Default is None. This will only matter if the ax is provided.
- Returns:
a ndarray of the matplotlib.axes.Axes of the plot
- Return type:
Sequence[Axes]
Examples
Following example is a simple example with missing values of the parameters.
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> (xa.set_pre_edge_kws(pre_edge_kws) .set_autobk_kws(autobk_kws) .set_xftf_kws(xftf_kws) .set_reference_from_db("Ni K", label="Ni foil")) >>> xa.add_group(group1, "group1") >>> xa.plot_xr( ref=True, plot_rrange=[0, 8], save_path="./out/Ni_xr.png", )
- pre_edge(calc_group: bool = True, calc_reference: bool = False) Self[source]
Pre-edge normalization of the groups
This method will calculate the pre-edge of the groups. It is highly recommended to set the e0 and the pre_edge_kws before running this method. If it is not set, it will automatically detect the parameters for each group, and the parameters will not be consitent between the groups.
- Parameters:
calc_group (bool) – calculate the pre-edge of the groups. Default is True.
calc_reference (bool) – calculate the pre-edge of the reference. Default is False.
- Returns:
self. This method can be chained.
- Return type:
Self
- pre_edge_kws: dict
- reference: Group
- remove_group(name: str) Self[source]
Remove the group from the class using the name
- Parameters:
name (str) – name of the group
- Returns:
self. This method can be chained.
- Return type:
Self
- set_autobk_kws(kws) Self[source]
Set the autobk keywords
- Parameters:
kws (dict) – autobk keywords
- Returns:
self. This method can be chained.
- Return type:
Self
- set_e0(e0: float) Self[source]
Set the e0 of the class
- Parameters:
e0 (float) – e0 of the class
- Returns:
self. This method can be chained.
- Return type:
Self
- set_pre_edge_kws(kws) Self[source]
Set the pre_edge keywords
- Parameters:
kws (dict) – pre_edge keywords
- Returns:
self. This method can be chained.
- Return type:
Self
- set_reference(group: Group, ref_name: str | None) Self[source]
Register the reference spectrum to the class
This method will register the reference spectrum to the class. The reference spectrum will be used to align the spectra to the reference spectrum. It can also be used for plotting the reference spectrum in the plotting methods.
- Parameters:
group (Group) – reference group
ref_name (str) – name of the reference spectrum
- Retruns:
Self: self. This method can be chained.
- set_reference_from_db(ref_name: str, element: str | None = None, label: str | None = None) Self[source]
Register the reference spectrum to the class using the xasref module
This method will registere the reference spectrum to the class using the xasref module. xasref is a curated list of reference spectrum that is aligned by myself, using the first derivative of the absorption coefficients. This module is planned to be replaced by a better module in the future, with the help of beamline scientists. The reference spectrum will be used to align the spectra to the reference spectrum.
- Parameters:
ref_name (str) – name of the reference spectrum in the xasref module
element (str) – element of the reference spectrum. This is only used for efficient loading of the dictionary and it is not nessesary.
label (str) – label of the reference spectrum. Default is None, which will use the ref_name.
- Returns:
self. This
- Return type:
Self
Examples
>>> from xasanalysis import XASAnalysis >>> xa = XASAnalysis() >>> xa.set_reference_from_db("Ni K", label="Ni foil")
- set_xftf_kws(kws) Self[source]
Set the xftf keywords
- Parameters:
kws (dict) – xftf keywords
- Returns:
self. This method can be chained.
- Return type:
Self
- xftf(skip_autobk=True) Self[source]
Fourier transform of the groups
This method will calculate the Fourier transform of the groups. It is highly recommended to set the e0 and the xftf_kws before running this method. If it is not set, it will automatically detect the parameters for each group, and the parameters will not be consitent between the groups.
- Parameters:
skip_autobk (bool) – skip the autobk calculation. Default is True.
- Returns:
self. This method can be chained.
- Return type:
Self
- xftf_kws: dict
- xasanalysis.xasanalysis.calc_shift(energy_grid: ndarray, group: Group, reference: Group, pre_edge_kws: dict, fit_range: list[float] | None = None, max_shift: float = 20.0)[source]
Calculate the shift of the spectrum, with respect to the reference spectrum using spdist+MAE as the metric
The main strategy of calculating the metric is the following:
Interpolate the spectrum to the energy grid (for example, 0.5eV spacing, this ensures that each energy contribution will be uniformly weighted)
Calculate the flattened spectrum using the pre_edge function of xraylarch.
Interpolate the the flattened spectrum to the energy grid
Calculate the spdist metric between the spectrum and the reference spectrum.
Calculate the mean absolute error between the spectrum and the reference spectrum
Return the average of the spdist metric and the mean absolute error
The spdist is a average of the minimum distance between the two spectra, and the mean absolute error is the average of the absolute vertical distance between the two spectra. spdist metric is more sensitive towards the distance in the diagnal direnction of the two spectra. This is useful to align the spectra with similar features.
- Parameters:
energy_grid – energy grid for the metric calculation. This will be used for the interpolation of the spectrum
group (Group) – group of the spectrum
reference (Group) – group of the reference spectrum
pre_edge_kws (dict) – pre_edge keywords that will be passed to the pre_edge function
fit_range (list) – the fitting range for the metric calculation
max_shift (float) – maximum shift of the spectrum
- Returns:
shift of the spectrum loss(float): loss of the spectrum
- Return type:
shift(float)
Examples
>>> from xasanalysis import calc_shift >>> group = read_xmu("example.xmu", "example") >>> group2 = deepcopy(group) >>> group2.energy = group2.energy + 10.0 >>> group2.mu = group2.mu*0.5 >>> e0 = find_e0(group) >>> energy_grid = np.linspace(e0 - 20, e0 + 80, 200) >>> fit_range = [e0 - 20, e0 + 80] >>> pre_edge_kws = { "e0": None, "step": None, "nnorm": 3, "nvict": 0, "pre1": -150, "pre2": -45, "norm1": 70, "norm2": None, } >>> shift, loss = calc_shift(energy_grid, group2, group, pre_edge_kws, fit_range, max_shift = 20.0)
- xasanalysis.xasanalysis.f(dfnum, dfden, size=None)
Draw samples from an F distribution.
Samples are drawn from an F distribution with specified parameters, dfnum (degrees of freedom in numerator) and dfden (degrees of freedom in denominator), where both parameters must be greater than zero.
The random variate of the F distribution (also known as the Fisher distribution) is a continuous probability distribution that arises in ANOVA tests, and is the ratio of two chi-square variates.
Note
New code should use the ~numpy.random.Generator.f method of a ~numpy.random.Generator instance instead; please see the random-quick-start.
- Parameters:
dfnum (float or array_like of floats) – Degrees of freedom in numerator, must be > 0.
dfden (float or array_like of float) – Degrees of freedom in denominator, must be > 0.
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn. If size isNone(default), a single value is returned ifdfnumanddfdenare both scalars. Otherwise,np.broadcast(dfnum, dfden).sizesamples are drawn.
- Returns:
out – Drawn samples from the parameterized Fisher distribution.
- Return type:
ndarray or scalar
See also
scipy.stats.fprobability density function, distribution or cumulative density function, etc.
random.Generator.fwhich should be used for new code.
Notes
The F statistic is used to compare in-group variances to between-group variances. Calculating the distribution depends on the sampling, and so it is a function of the respective degrees of freedom in the problem. The variable dfnum is the number of samples minus one, the between-groups degrees of freedom, while dfden is the within-groups degrees of freedom, the sum of the number of samples in each group minus the number of groups.
References
Examples
An example from Glantz[1], pp 47-40:
Two groups, children of diabetics (25 people) and children from people without diabetes (25 controls). Fasting blood glucose was measured, case group had a mean value of 86.1, controls had a mean value of 82.2. Standard deviations were 2.09 and 2.49 respectively. Are these data consistent with the null hypothesis that the parents diabetic status does not affect their children’s blood glucose levels? Calculating the F statistic from the data gives a value of 36.01.
Draw samples from the distribution:
>>> dfnum = 1. # between group degrees of freedom >>> dfden = 48. # within groups degrees of freedom >>> s = np.random.f(dfnum, dfden, 1000)
The lower bound for the top 1% of the samples is :
>>> np.sort(s)[-10] 7.61988120985 # random
So there is about a 1% chance that the F statistic will exceed 7.62, the measured value is 36, so the null hypothesis is rejected at the 1% level.
- xasanalysis.xasanalysis.read_QAS_SDD(file_path: str, name: str, roi: int = 1, channels: list[int] | None = None, energy_col: int = 0, i0_col: int = 1, use_glob=False)[source]
Read the data collected from QAS(Silicon Drifts Detector mode) and return a larch Group object or a list of larch Group objects
The group will be sorted by the file name when using the glob option
- Parameters:
file_path (str) – path to the file
name (str) – name of the group
roi (int) – roi of the spectrum
channels (list[int]) – channels used to summed up the spectrum
energy_col (int) – column of the energy
i0_col (int) – column of the i0
use_glob (bool) – use glob to read multiple files
- Returns:
larch Group object or a list of larch Group objects(if use_glob=True)
- Return type:
Group | list[Group]
Examples
Example1: >>> from xasanalysis import read_QAS_SDD >>> group = read_QAS_SDD(“example.dat”, “example”, roi = 1, channels = [1, 2, 3])
Example2: >>> from xasanalysis import read_QAS_SDD >>> groups = read_QAS_SDD(“example*.dat”, “example”, roi = 1, channels = [1, 2, 3], use_glob=True)
- xasanalysis.xasanalysis.read_QAS_fluorescence(file_path: str, name: str, energy_col: int = 0, i0_col: int = 1, iff_col: int = 4, use_glob=False) Group | list[Group][source]
Read the data collected from QAS(fluorescence mode) and return a larch Group object or a list of larch Group objects
The group will be sorted by the file name when using the glob option
- Parameters:
file_path (str) – path to the file
name (str) – name of the group
energy_col (int) – column of the energy
i0_col (int) – column of the i0
iff_col (int) – column of the iff
use_glob (bool) – use glob to read multiple files
- Returns:
larch Group object or a list of larch Group objects(if use_glob=True)
- Return type:
Group | list[Group]
Examples
Example1: >>> from xasanalysis import read_QAS_fluorescence >>> group = read_QAS_fluorescence(“example.dat”, “example”)
Example2: >>> from xasanalysis import read_QAS_fluorescence >>> groups = read_QAS_fluorescence(“example*.dat”, “example”, use_glob=True)
- xasanalysis.xasanalysis.read_QAS_ref(file_path: str, name: str, energy_col: int = 0, it_col: int = 2, ir_col: int = 3, use_glob=False) Group | list[Group][source]
Read the data collected from QAS(reference reference foil) and return a larch Group object or a list of larch Group objects
The group will be sorted by the file name when using the glob option
- Parameters:
file_path (str) – path to the file
name (str) – name of the group
energy_col (int) – column of the energy
it_col (int) – column of the it
ir_col (int) – column of the ir
use_glob (bool) – use glob to read multiple files
- Returns:
larch Group object or a list of larch Group objects(if use_glob=True)
- Return type:
Group | list[Group]
Examples
Example1: >>> from xasanalysis import read_QAS_ref >>> group = read_QAS_ref(“example.dat”, “example”)
Example2: >>> from xasanalysis import read_QAS_ref >>> groups = read_QAS_ref(“example*.dat”, “example”, use_glob=True)
- xasanalysis.xasanalysis.read_QAS_transmission(file_path: str, name: str, energy_col: int = 0, i0_col: int = 1, it_col: int = 2, use_glob=False) Group | list[Group][source]
Read the data collected from QAS(transmission mode) and return a larch Group object or a list of larch Group objects
The group will be sorted by the file name when using the glob option
- Parameters:
file_path (str) – path to the file
name (str) – name of the group
energy_col (int) – column of the energy
i0_col (int) – column of the i0
it_col (int) – column of the it
use_glob (bool) – use glob to read multiple files
- Returns:
larch Group object or a list of larch Group objects(if use_glob = True)
- Return type:
Group | list[Group]
Examples
Example1: >>> from xasanalysis import read_QAS_transmission >>> group = read_QAS_transmission(“example.dat”, “example”)
Example2: >>> from xasanalysis import read_QAS_transmission >>> groups = read_QAS_transmission(“example*.dat”, “example”, use_glob=True)
- xasanalysis.xasanalysis.read_xmu(file_path: str, name: str, energy_col: int = 0, mu_col: int = 1) Group[source]
Read the xmu file gereated by Athena and return a larh Group object
- Parameters:
file_path (str) – path to the xmu file
name (str) – name of the group
energy_col (int) – column of the energy
mu_col (int) – column of the mu
- Returns:
larch Group object
- Return type:
group(Group)
Examples
>>> from xasanalysis import read_xmu >>> group = read_xmu("example.xmu", "example")
Module contents
Top-level package for XASAnalysis.