Source code for est.tests.test_xas_object

import numpy

from ..core.io.read_xas import read_from_url
from ..core.types.xasobject import XASObject


[docs] def test_create_from_single_spectrum(spectrum_cu_from_pymca): """Check that we can create a XASObject from a pymca .dat file""" configuration = { "FT": {"KWeight": 1}, "EXAFS": {"EXAFSNormalized": numpy.array([1, 2, 3])}, } xas_obj = XASObject( spectra=(spectrum_cu_from_pymca,), energy=spectrum_cu_from_pymca.energy, configuration=configuration, dim1=1, dim2=1, ) assert xas_obj.n_spectrum == 1 # Check dict round-trip xas_obj2 = XASObject.from_dict(xas_obj.to_dict()) assert xas_obj2 == xas_obj
[docs] def test_create_from_several_spectra(xas_image_data): """Check that we can create a XASObject from numpy arrays""" spectra_url, channel_url, raw_energy, raw_spectra = xas_image_data xas_obj = read_from_url( spectra_url=spectra_url, channel_url=channel_url, dimensions=(2, 1, 0) ) numpy.testing.assert_array_equal(xas_obj.energy, raw_energy) numpy.testing.assert_array_equal(xas_obj.spectra.as_ndarray("mu"), raw_spectra) assert xas_obj.n_spectrum == raw_spectra.size // len(raw_spectra) # Check dict round-trip xas_obj2 = XASObject.from_dict(xas_obj.to_dict()) assert xas_obj2.n_spectrum == xas_obj.n_spectrum numpy.testing.assert_array_equal(xas_obj2.energy, raw_energy) numpy.testing.assert_array_equal(xas_obj2.spectra.as_ndarray("mu"), raw_spectra) assert xas_obj2 == xas_obj
[docs] def test_repr_single_spectrum(spectrum_cu_from_pymca): """Check the string representation of one spectrum.""" configuration = { "FT": {"KWeight": 1}, "EXAFS": {"EXAFSNormalized": numpy.array([1, 2, 3])}, } xas_obj = XASObject( spectra=(spectrum_cu_from_pymca,), energy=spectrum_cu_from_pymca.energy, configuration=configuration, dim1=1, dim2=1, ) lines = repr(xas_obj) assert str(xas_obj) == lines assert "XASObject Configuration\n" in lines assert " KWeight: 1\n" in lines assert " EXAFSNormalized: shape=(3,) min=1 max=3\n" in lines assert "Spectrum\n" in lines assert " energy: shape=(1461,) min=8e+03 max=9.98e+03\n" in lines assert " mu: shape=(1461,) min=0.386 max=3.22\n" in lines
[docs] def test_repr_several_spectra(xas_image_data): """Check the string representation of several spectra.""" spectra_url, channel_url, _, __loader__ = xas_image_data xas_obj = read_from_url( spectra_url=spectra_url, channel_url=channel_url, dimensions=(2, 1, 0) ) lines = repr(xas_obj) assert str(xas_obj) == lines assert "XASObject Configuration\n" in lines assert "First Spectrum\n" in lines assert " energy: shape=(256,) min=1.08e+04 max=1.12e+04" assert " mu: shape=(256,) min=0 max=1.64" assert "Last Spectrum\n" in lines assert " energy: shape=(256,) min=1.08e+04 max=1.12e+04" assert " mu: shape=(256,) min=0 max=1.64"