From 6e70b6bf92a1d7911cac0a62c9de80871b33aff0 Mon Sep 17 00:00:00 2001 From: Loic Huder <loic.huder@esrf.fr> Date: Thu, 18 Apr 2024 11:02:36 +0200 Subject: [PATCH] Remove spectra_url_dims and other little improvements in XASObject --- src/est/core/types/xasobject.py | 75 +++++++++------------------------ 1 file changed, 21 insertions(+), 54 deletions(-) diff --git a/src/est/core/types/xasobject.py b/src/est/core/types/xasobject.py index 366914f0..c7ffbfb7 100644 --- a/src/est/core/types/xasobject.py +++ b/src/est/core/types/xasobject.py @@ -1,6 +1,6 @@ from __future__ import annotations import copy -from typing import Any, Optional, Tuple, Union +from typing import Any, Sequence import pint import numpy @@ -9,7 +9,7 @@ from silx.io.url import DataUrl from est.io.utils import get_data_from_url from est.units import ur from est.settings import DEFAULT_READ_TIMEOUT -from est.core.types import dimensions as dimensions_mod +from est.core.types.dimensions import STANDARD_DIMENSIONS from .spectra import Spectra from .spectrum import Spectrum @@ -20,43 +20,33 @@ class XASObject: :param spectra: absorbed beam as a list of :class:`.Spectrum` or a numpy.ndarray. If is a numpy array, the axes should have the `STANDARD_DIMENSIONS` order. - :type: Union[numpy.ndarray, list] :param energy: beam energy - :type: numpy.ndarray of one dimension - :param dict configuration: configuration of the different process - :param int dim1: first dimension of the spectra - :param int dim2: second dimension of the spectra - :param str name: name of the object. Will be used for the hdf5 entry + :param configuration: configuration of the different process + :param dim1: first dimension of the spectra + :param dim2: second dimension of the spectra + :param name: name of the object. Will be used for the hdf5 entry :param spectra_url: path to the spectra data if any. Used when serializing the XASObject. Won't read it from it. - :type: Union[None,str] - :param spectra_url_dims: dimensions of the stored spectra. WARNING: - this is different of the spectra dimension - which should be given in the - `STANDARD_DIMENSIONS` order - :type: Union[tuple,None] :param energy_url: path to the energy / channel data if any. Used when serializing the XASObject. Won't read it from it. - :type: Union[None,str] """ def __init__( self, - spectra=None, - energy=None, - configuration: Union[dict, None] = None, - dim1: Union[int, None] = None, - dim2: Union[int, None] = None, + spectra: numpy.ndarray | Sequence[Any] | None = None, + energy: numpy.ndarray | None = None, + configuration: dict | None = None, + dim1: int | None = None, + dim2: int | None = None, name: str = "scan1", - spectra_url: Union[DataUrl, None] = None, - spectra_url_dims: Optional[dimensions_mod.DimensionsType] = None, - energy_url: Union[DataUrl, None] = None, - check_energy: bool = True, + spectra_url: DataUrl | None = None, + energy_url: DataUrl | None = None, ): if isinstance(energy, numpy.ndarray) and not isinstance(energy, pint.Quantity): # automatic energy unit guess. convert energy provided in keV to eV # works because EXAFS data are never longer than 2 keV and a low - # energy XANES is never shorter than 3 eV. See issue 30 + # energy XANES is never shorter than 3 eV. + # See https://gitlab.esrf.fr/workflow/ewoksapps/est/-/issues/30 if (energy.max() - energy.min()) < 3.0: energy = energy * ur.keV else: @@ -65,13 +55,8 @@ class XASObject: if energy is not None: energy = energy.m_as(ur.eV) - self.__channels = None - self.__spectra = Spectra(energy=energy) - self.__dim1 = None - self.__dim2 = None self.__entry_name = name self.__spectra_url = spectra_url - self.__spectra_url_dims = spectra_url_dims self.__energy_url = energy_url self.spectra = (energy, spectra, dim1, dim2) self.configuration = configuration @@ -106,7 +91,7 @@ class XASObject: return self.__spectra @property - def spectra_url(self) -> Union[None, DataUrl]: + def spectra_url(self) -> DataUrl | None: """Url from where the spectra is available. Used for object serialization""" return self.__spectra_url @@ -116,18 +101,7 @@ class XASObject: self.__spectra_url = url @property - def spectra_url_dims( - self, - ) -> dimensions_mod.DimensionsType | Tuple[int, ...] | None: - """used to interpret the spectra_url if any""" - return self.__spectra_url_dims - - @spectra_url_dims.setter - def spectra_url_dims(self, dims: Tuple[int, ...]): - self.__spectra_url_dims = dims - - @property - def energy_url(self) -> Union[DataUrl, None]: + def energy_url(self) -> DataUrl | None: """Url from where the energy is available. Used for object serialization""" return self.__energy_url @@ -185,13 +159,6 @@ class XASObject: """convert the XAS object to a dict By default made to simply import raw data. - - :param with_process_details: used to embed a list of spectrum with - intermediary result instead of only raw mu. - This is needed especially for the - pushworkflow actors to keep a trace of the - processes. - :type: bool """ res = { @@ -200,6 +167,7 @@ class XASObject: "dim2": self.spectra.shape[1], "energy": self.spectra.energy, "spectra": [spectrum.to_dict() for spectrum in self.spectra], + "dimensions": STANDARD_DIMENSIONS, # spectra dimensions are always changed to STANDARD on load } return res @@ -210,10 +178,8 @@ class XASObject: """load XAS values from a dict""" if not isinstance(ddict, dict): raise TypeError(f"ddict is expected to be a dict. Not {type(ddict)}") - # TODO: is this correct? Shouldn't this be `self.spectra_url_dims`? - # We don't have a test for this. - dimensions = dimensions_mod.STANDARD_DIMENSIONS - # default yay of storing data + dimensions = ddict.get("dimensions", STANDARD_DIMENSIONS) + # default way of storing data from est.io import load_data # avoid cyclic import """The dict can be on the scheme of the to_dict function, containing @@ -282,6 +248,7 @@ class XASObject: channel_url=energy_url, config_url=config_url, dimensions=dimensions, + timeout=timeout, ) def dump(self, h5_file: str): -- GitLab