Commit f26ab044 authored by payno's avatar payno
Browse files

[type] clean up API of Xas_object

parent a85b5a76
Pipeline #39094 failed with stages
in 6 minutes and 37 seconds
......@@ -138,8 +138,8 @@ class TestStreamSingleSpectrum(unittest.TestCase):
out = pymca_k_weight(xas_obj=out)
out = pymca_ft(xas_obj=out)
assert isinstance(out, XASObject)
self.assertEqual(out.dim1, 1)
self.assertEqual(out.dim2, 5)
self.assertEqual(out.spectra.shape[0], 1)
self.assertEqual(out.spectra.shape[1], 5)
self.assertTrue(out.spectra.data.flat[0].ft is not None)
self.assertTrue(len(out.spectra.data.flat[0].ft.intensity) > 0)
......
......@@ -111,9 +111,9 @@ class ROIProcess(Process):
xmin, xmax = roi.origin[0], roi.origin[0] + roi.size[0]
# clip roi
ymin = max(ymin, 0)
ymax = min(ymax, xas_obj.dim1)
ymax = min(ymax, xas_obj.spectra.shape[0])
xmin = max(xmin, 0)
xmax = min(xmax, xas_obj.dim2)
xmax = min(xmax, xas_obj.spectra.shape[1])
assert type(ymin) is int
assert type(ymax) is int
assert type(xmin) is int
......@@ -168,7 +168,7 @@ class ROIProcess(Process):
xas_obj.spectra = Spectra(energy=_energies[:, 0], spectra=new_spectra)
# update the dimensions
xas_obj.force_dims(dim1=roi.size[1], dim2=roi.size[0])
xas_obj.spectra.reshape((roi.size[1], roi.size[0]))
return xas_obj
def process(self, xas_obj: Union[dict, XASObject]):
......
......@@ -129,8 +129,8 @@ class TestXASObject(unittest.TestCase):
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
)
self.assertEqual(self.xas_obj.dim1, 20)
self.assertEqual(self.xas_obj.dim2, 10)
self.assertEqual(self.xas_obj.spectra.shape[0], 20)
self.assertEqual(self.xas_obj.spectra.shape[1], 10)
self.assertEqual(self.xas_obj.n_spectrum, 20 * 10)
ddict = self.xas_obj.to_dict(with_process_details=False)
original_spectra = silx.io.utils.get_data(
......
......@@ -50,7 +50,10 @@ _logger = logging.getLogger(__name__)
class Spectra:
"""
A map of spectrum
A map of spectrum.
- data: numpy array of Spectrum. Expected to be 2D.
- energy: set of energy for each position (x,y)
"""
def __init__(self, energy, spectra: Union[Iterable, None] = None):
......@@ -102,6 +105,7 @@ class Spectra:
def __getitem__(self, item):
return self.__data[item]
@property
def shape(self):
return self.__data.shape
......@@ -136,6 +140,10 @@ class Spectra:
_logger.warning("spectra and energy have incoherent dimension")
def map_to(self, key: str, relative_to: str = "energy"):
"""
Create a map a specific key of this spectra.
"""
def get_param(object, name):
if hasattr(object, name):
return getattr(object, name)
......@@ -187,7 +195,10 @@ class Spectra:
shape.insert(0, -1)
return array.reshape(shape)
def keys(self):
def keys(self) -> tuple:
"""
return the tuple of keys contained in the spectra
"""
if len(self.data.flat) > 0:
assert isinstance(self.data.flat[0], Spectrum)
return tuple(self.data.flat[0].keys())
......@@ -195,7 +206,7 @@ class Spectra:
def __eq__(self, other):
if not isinstance(other, Spectra):
return False
if len(self.data) != len(other):
if len(self.data) != len(other.data):
return False
else:
return numpy.array_equal(self.data.flat, other.data.flat)
......@@ -205,6 +216,10 @@ class Spectra:
@staticmethod
def frm_dict(ddict, dimensions):
"""
:param dict ddict: dict containing the data to be loaded
:param tuple dimensions: information regarding spectra dimensions
"""
from est.io import load_data # avoid cyclic import
# if spectra is given from an url
......
......@@ -102,8 +102,8 @@ class TestXASObject(unittest.TestCase):
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
)
self.assertEqual(self.xas_obj.dim1, 20)
self.assertEqual(self.xas_obj.dim2, 10)
self.assertEqual(self.xas_obj.spectra.shape[0], 20)
self.assertEqual(self.xas_obj.spectra.shape[1], 10)
self.assertEqual(self.xas_obj.n_spectrum, 20 * 10)
ddict = self.xas_obj.to_dict(with_process_details=False)
original_spectra = silx.io.utils.get_data(
......
......@@ -136,7 +136,6 @@ class XASObject(object):
def entry(self) -> str:
return self.__entry_name
# TODO: fix me: should return a tuple
@property
def spectra(self):
return self.__spectra
......@@ -187,26 +186,6 @@ class XASObject(object):
"""Util function to access the spectrum at dim1_idx, dim2_idx"""
return self.spectra[dim1_idx, dim2_idx]
# # TODO: should disappear
# def add_spectrum(self, spectrum):
# self.__spectra.append(spectrum)
# TODO: should disappear
@property
def dim1(self) -> int:
return self.spectra.shape()[0]
# TODO: should disappear
def force_dims(self, dim1: int, dim2: int):
"""Force"""
if dim1 is not None and dim2 is not None:
self.spectra.reshape((dim1, dim2))
# TODO: should disappear
@property
def dim2(self) -> int:
return self.spectra.shape()[1]
@property
def energy(self) -> numpy.ndarray:
"""energy as a numpy array in eV.
......@@ -303,8 +282,8 @@ class XASObject(object):
"configuration": self.configuration,
"spectra": spectra_,
"energy": get_energy(),
"dim1": self.dim1,
"dim2": self.dim2,
"dim1": self.spectra.shape[0],
"dim2": self.spectra.shape[1],
}
if with_process_details is True:
res["linked_h5_file"] = self.linked_h5_file
......@@ -362,9 +341,6 @@ class XASObject(object):
self.link_to_h5(ddict["linked_h5_file"])
self.__processing_index = ddict["current_processing_index"]
self.spectra.reshape((dim1, dim2))
# TODO: clean me if ok
# if not contains_config_spectrum:
# self.spectrum = ddict
return self
@staticmethod
......@@ -441,8 +417,6 @@ class XASObject(object):
(self.energy is None and other.energy is None)
or numpy.array_equal(self.energy.m, other.energy.m) # noqa
) # noqa
and self.dim1 == other.dim1 # noqa
and self.dim2 == other.dim2 # noqa
and self.configuration == other.configuration # noqa
and self.spectra,
other.spectra, # noqa
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment