Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/xas_toolbox/utils/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Any

from numpy import complex128, dtype, float64, int_, ndarray, uint

# AliasTypes for 1d and nd arrays of float/ints.

_Array1dFloat = ndarray[tuple[Any], dtype[float64]]
_Array1dInt = ndarray[tuple[Any], dtype[uint | int_]]
_Array1dComplex = ndarray[tuple[Any], dtype[complex128]]

_Array1d = _Array1dFloat | _Array1dInt | _Array1dComplex

_ArrayNdFloat = ndarray[tuple[Any, ...], dtype[float64]]
_ArrayNdInt = ndarray[tuple[Any, ...], dtype[uint | int_]]
_ArrayNdComplex = ndarray[tuple[Any, ...], dtype[complex128]]

_ArrayNd = _ArrayNdFloat | _ArrayNdInt | _ArrayNdComplex
17 changes: 17 additions & 0 deletions src/xas_toolbox/xas/alignment/align_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from xas_toolbox.io import XasMeasurement
from xas_toolbox.utils.maths.interpolate import interpolate_data
from xas_toolbox.utils.typing import _Array1dFloat, _ArrayNdFloat

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,3 +106,19 @@ def align_stack(scan: XasMeasurement):
scan_out.energy = energy_out
scan_out.mu = mu_out
return scan_out


def shift_to_iref(
x: _Array1dFloat | _ArrayNdFloat | list[_Array1dFloat], delta_e: float | np.ndarray
) -> _Array1dFloat | _ArrayNdFloat | list[_Array1dFloat]:
"""
Shift `x` array/list of arrays by `delta_e` values.
"""
if isinstance(x, np.ndarray):
out = np.empty_like(x)
out = x + delta_e
else:
out = []
for i in range(len(x)):
out.append(x[i] + delta_e[i])
return out
75 changes: 70 additions & 5 deletions src/xas_toolbox/xas/edges/find_e0.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from xraylib import EdgeEnergy, SymbolToAtomicNumber

from xas_toolbox.utils.maths.start_stop import _mean_start_stop
from xas_toolbox.utils.typing import _Array1dFloat, _ArrayNdFloat
from xas_toolbox.utils.xray import _edges

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -58,7 +59,63 @@ def calc_e0(x: np.ndarray, y: np.ndarray, sigma: float = 5):
return edge_energy, np.array(edge_coords, dtype=int), edge_number


def edge_from_symbol(x: np.ndarray, y: np.ndarray, symbol: str) -> str:
def get_edge_symbol(
x: _ArrayNdFloat | _Array1dFloat | list[_Array1dFloat],
y: _Array1dFloat | _ArrayNdFloat,
symbol: str | None,
):
"""
Get absorption edge and atom labels for x, y data with optional absorbing atom
input.

Arguments:
x: Energy array/list
y: Absorption array/list
symbol (Optional): Absorbing atom.

Returns:
out: List/single value for predicted absorption edge + absorbing atom
(if symbol=`None`).
"""
if symbol is not None:
out = edge_from_symbol(x, y, symbol)
else:
if isinstance(x, np.ndarray) and x.ndim == 1:
e0, _, _ = calc_e0(x, y)
if isinstance(e0, list):
out = guess_edge(e0[0])
else:
out = guess_edge(e0)
else:
out = []
for i in range(x.shape[0]):
e0, idx, no = calc_e0(x[i, :], y[i, :])
if isinstance(e0, list):
out.append(guess_edge(e0[0]))
else:
out.append(guess_edge(e0))
return out


def edge_from_symbol(
x: _Array1dFloat | _ArrayNdFloat | list[_Array1dFloat],
y: _Array1dFloat | _ArrayNdFloat | list[_Array1dFloat],
symbol: str,
) -> str | list[str]:
if x.ndim > 1:
for i in range(x.shape[0]):
edge = edge_from_symbol(x[i, :], y[i, :], symbol=symbol)
else:
edge = edge_from_symbol(x, y, symbol)
edge = list(set(edge))
if len(edge) == 1:
edge = edge[0]
return edge


def edge_from_symbol_1d(
x: _Array1dFloat, y: _Array1dFloat | _ArrayNdFloat, symbol: str
) -> str:
"""
Get absorption edge label for a sample with unkown edge but
known absorbing atom.
Expand All @@ -73,14 +130,21 @@ def edge_from_symbol(x: np.ndarray, y: np.ndarray, symbol: str) -> str:
"""
Z = SymbolToAtomicNumber(symbol) # noqa: N806
e0, ec, eno = calc_e0(x, y)
e0s = np.array([EdgeEnergy(Z, e.upper()) for e in _edges])
diffs = np.abs(e0s - e0)
e0s = np.array([EdgeEnergy(Z, e.upper()) for e in _edges[5:]])
if isinstance(e0, float):
diffs = np.abs(e0s - e0)
else:
diffs = np.abs(e0s - e0[0])

edge = _edges[np.where(diffs == diffs.min())[0][0]]
return edge


def compare_e0s(
x: np.ndarray, y: np.ndarray, symbol: str | None = None, edge: str | None = None
x: _Array1dFloat,
y: _Array1dFloat | _ArrayNdFloat,
symbol: str | None = None,
edge: str | None = None,
):
"""
Get a tabulated e0 value from either sample metadata for absorbing atom + edge
Expand All @@ -98,7 +162,8 @@ def compare_e0s(
e0_calc (float): Calcuated e0 from `x` and `y`.
"""
e0_calc, idx, no = calc_e0(x, y)
e0_calc = e0_calc[0]
if isinstance(e0_calc, list):
e0_calc = e0_calc[0]
if edge is None:
if symbol is None:
symbol, edge = guess_edge(e0_calc)
Expand Down
Loading