Commit 7e6f6cce authored by payno's avatar payno

[core][types] add Spectrum position and rework a bit the 'force' dimensions

- add `x` and `y` attributes to the Spectrum
- remove forceDim1 and forceDim2 for force_dims which take the two dimensions as input and recompute the Spectrums indices.
warning: this could effect the display of Spectra in the future. So this should be defined before tretatment and keep for the life of the object.
note: the roi process is creating a new Spectra so will not be an issue
parent ce62c780
......@@ -160,8 +160,7 @@ class ROIProcess(Process):
xas_obj._setSpectra(spectra=new_spectra)
# update the dimensions
xas_obj.forceDim1(roi.size[1])
xas_obj.forceDim2(roi.size[0])
xas_obj.force_dims(dim1=roi.size[1], dim2=roi.size[0])
return xas_obj
def process(self, xas_obj):
......
......@@ -101,8 +101,8 @@ class XASObject(object):
self.__channels = None
self.__spectra = []
self.__energy = None
self.__dim1 = 0
self.__dim2 = 0
self.__dim1 = None
self.__dim2 = None
self.__processing_index = 0
self.__h5_file = None
self.__entry_name = name
......@@ -164,12 +164,13 @@ class XASObject(object):
assert isinstance(spectra, (list, tuple, numpy.ndarray))
if isinstance(spectra, numpy.ndarray):
assert spectra.ndim is 3
self.__dim1 = spectra.shape[1]
self.__dim2 = spectra.shape[2]
for y_i_spectrum in range(spectra.shape[1]):
for x_i_spectrum in range(spectra.shape[2]):
self.add_spectrum(Spectrum(energy=energy,
mu=spectra[:, y_i_spectrum, x_i_spectrum]))
# spectra dimensions should be channel, y, x
self.__dim1 = spectra.shape[1]
self.__dim2 = spectra.shape[2]
else:
if dim1 is None or dim2 is None:
raise ValueError(
......@@ -181,6 +182,7 @@ class XASObject(object):
for spectrum in spectra:
assert isinstance(spectrum, Spectrum)
self.add_spectrum(spectrum)
self._updateSpectraIndexes()
self.energy = energy
def _setSpectra(self, spectra):
......@@ -202,13 +204,24 @@ class XASObject(object):
def dim1(self):
return self.__dim1
def forceDim1(self, value):
assert type(value) is int
self.__dim1 = value
def force_dims(self, dim1: int, dim2: int):
"""Force"""
assert self.n_spectrum == (dim1*dim2)
assert type(dim1) is int
assert type(dim2) is int
self.__dim1 = dim1
self.__dim2 = dim2
self._updateSpectraIndexes()
def forceDim2(self, value):
assert type(value) is int
self.__dim2 = value
def _updateSpectraIndexes(self):
"""Update spectrum indexes from dim1 and dim2 definition.
note: indexes are relative to a spectra. So start at 0, 0 and
go up to dim1, dim2.
"""
for y in range(self.dim1):
for x in range(self.dim2):
self.get_spectrum(dim1_idx=y, dim2_idx=x)._force_indexes(x=x, y=y)
@property
def dim2(self):
......@@ -640,6 +653,8 @@ class Spectrum(_Spectrum_Base):
:param numpy.ndarray (1D) energy: beam energy
:param numpy.ndarray (1D) mu: beam absorption
:param int x: x index on the spectra
:param int y: y index on the spectra
"""
_MU_KEY = 'Mu'
......@@ -661,11 +676,18 @@ class Spectrum(_Spectrum_Base):
_NORMALIZED_BACKGROUND_KEY = 'NormalizedBackground'
def __init__(self, energy=None, mu=None):
_X_POS_KEY = 'XPos'
_Y_POS_KEY = 'YPos'
def __init__(self, energy=None, mu=None, x=None, y=None):
_Spectrum_Base.__init__(self)
if energy is not None:
assert isinstance(energy, numpy.ndarray)
self.__x = x
self.__y = y
# properties
self.energy = energy
self.mu = mu
......@@ -713,6 +735,14 @@ class Spectrum(_Spectrum_Base):
assert isinstance(mu, numpy.ndarray) or mu is None
self.__mu = mu
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
@property
def chi(self):
return self.__chi
......@@ -873,11 +903,19 @@ class Spectrum(_Spectrum_Base):
@staticmethod
def from_dict(ddict):
spectrum = Spectrum()
x_pos = None
y_pos = None
if Spectrum._X_POS_KEY in ddict:
x_pos = ddict[Spectrum._X_POS_KEY]
if Spectrum._Y_POS_KEY in ddict:
y_pos = ddict[Spectrum._Y_POS_KEY]
spectrum = Spectrum(x=x_pos, y=y_pos)
return spectrum.load_frm_dict(ddict=ddict)
def to_dict(self):
res = {
self._X_POS_KEY: self.x,
self._Y_POS_KEY: self.y,
self._MU_KEY: self.mu,
self._ENERGY_KEY: self.energy,
self._FT_KEY: self.ft.to_dict(),
......@@ -957,6 +995,13 @@ class Spectrum(_Spectrum_Base):
def copy(self):
return Spectrum().load_frm_dict(self.to_dict())
def _force_indexes(self, x, y):
"""This is protected because it might change display and
the indexes should be defined during Spectra or Spectrum construction
once for all"""
self.__x = x
self.__y = y
class _FT(object):
......
Markdown is supported
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