test_exafs.py 3.57 KB
Newer Older
payno's avatar
payno committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2019 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "06/11/2019"


30
import os
payno's avatar
payno committed
31
import unittest
32

33
try:
payno's avatar
PEP8    
payno committed
34
    import PyMca5  # noqa
35
36
37
38
39
except ImportError:
    has_pymca = False
else:
    has_pymca = True
    from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
payno's avatar
payno committed
40
    from est.core.process.pymca.exafs import pymca_exafs
41
    from est.io.utils.spec import read_spectrum
payno's avatar
payno committed
42
    from est.core.process.pymca.normalization import pymca_normalization
43

payno's avatar
payno committed
44
from est.core.types import Spectrum, XASObject
payno's avatar
payno committed
45
46


payno's avatar
payno committed
47
@unittest.skipIf(has_pymca is False, "PyMca5 is not installed")
48
class TestEXAFSSingleSpectrum(unittest.TestCase):
payno's avatar
payno committed
49
    """Make sure the process have valid io"""
payno's avatar
payno committed
50

51
    def setUp(self):
payno's avatar
payno committed
52
        data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
payno's avatar
payno committed
53
54
55
56
57
        energy, mu = read_spectrum(
            data_file,
            energy_col_name="Column 1",
            absorption_col_name="Column 2",
        )
58
        spectrum = Spectrum(energy=energy, mu=mu)
59
        exafs_configuration = {"Knots": {"Values": (1, 2, 5), "Number": 3}}
payno's avatar
payno committed
60
61
62
63
64
65
66
67
        configuration = {"EXAFS": exafs_configuration}
        self.input_ = XASObject(
            energy=energy,
            spectra=(spectrum,),
            dim1=1,
            dim2=1,
            configuration=configuration,
        )
68

69
70
        # first process normalization
        self.preproc_input_ = pymca_normalization(xas_obj=self.input_)
payno's avatar
payno committed
71
72
        assert "NormalizedBackground" in self.preproc_input_.spectra.data.flat[0]
        for spectrum in self.preproc_input_.spectra.data.flat:
payno's avatar
payno committed
73
            assert "NormalizedBackground" in spectrum
74

75
    def testPyMcaXASAsInput(self):
76
        res = pymca_exafs(self.preproc_input_)
77
        self.assertTrue(isinstance(res, XASObject))
payno's avatar
payno committed
78
79
80
        self.assertTrue("EXAFSKValues" in res.spectra.data.flat[0])
        self.assertTrue("EXAFSSignal" in res.spectra.data.flat[0])
        self.assertTrue("PostEdgeB" in res.spectra.data.flat[0])
81
82
83

    def testDictAsInput(self):
        """Test succeed if the input is a dict"""
payno's avatar
payno committed
84
        assert "NormalizedBackground" in self.preproc_input_.spectra.data.flat[0]
85
        res = pymca_exafs(self.preproc_input_)
86
        self.assertTrue(isinstance(res, XASObject))
payno's avatar
payno committed
87
88
89
        self.assertTrue("EXAFSKValues" in res.spectra.data.flat[0])
        self.assertTrue("EXAFSSignal" in res.spectra.data.flat[0])
        self.assertTrue("PostEdgeB" in res.spectra.data.flat[0])