test_workflow.py 4.86 KB
Newer Older
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
# 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"
27
__date__ = "07/08/2019"
28

29
import unittest
30
31
32
import os
import tempfile
import shutil
payno's avatar
payno committed
33
from est.core.types import Spectrum, XASObject
payno's avatar
payno committed
34
from est.core.io import XASWriter
payno's avatar
payno committed
35

36
try:
payno's avatar
PEP8    
payno committed
37
    import larch  # noqa
38
39
40
41
except ImportError:
    has_larch = False
else:
    has_larch = True
payno's avatar
payno committed
42
43
44
    from est.core.process.larch.autobk import Larch_autobk
    from est.core.process.larch.pre_edge import Larch_pre_edge
    from est.core.process.larch.xftf import Larch_xftf
45
try:
payno's avatar
PEP8    
payno committed
46
    import PyMca5  # noqa
47
48
49
50
51
except ImportError:
    has_pymca = False
else:
    has_pymca = True
    from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
52
    from est.io.utils.spec import read_spectrum
payno's avatar
payno committed
53
54
55
56
    from est.core.process.pymca.normalization import PyMca_normalization
    from est.core.process.pymca.exafs import PyMca_exafs
    from est.core.process.pymca.ft import PyMca_ft
    from est.core.process.pymca.k_weight import PyMca_k_weight
57

58

payno's avatar
payno committed
59
@unittest.skipIf(not has_pymca or not has_larch, "pymca and/or larch not installed")
60
61
62
63
class TestMeldWorkflow(unittest.TestCase):
    """
    Test the following workflow: read_xmu -> pymca normalize ->
    """
payno's avatar
payno committed
64

65
66
    def setUp(self):
        data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
payno's avatar
payno committed
67
68
69
70
71
        energy, mu = read_spectrum(
            data_file,
            energy_col_name="Column 1",
            absorption_col_name="Column 2",
        )
72
        self.spectrum = Spectrum(energy=energy, mu=mu)
payno's avatar
payno committed
73
74
75
        self.xas_obj = XASObject(
            energy=energy, spectra=(self.spectrum,), dim1=1, dim2=1
        )
76
77
        assert self.xas_obj.linked_h5_file is not None
        self.output_dir = tempfile.mkdtemp()
payno's avatar
payno committed
78
        self.h5_file = os.path.join(self.output_dir, "output_file.h5")
79
        self.configuration_exafs = {
payno's avatar
payno committed
80
81
82
            "Knots": {"Values": (1, 2, 5), "Number": 3, "Orders": [3, 3, 3]},
            "KMin": 0,
            "KMax": 2.3,
83
        }
payno's avatar
payno committed
84

85
86
87
88
89
90
91
    def tearDown(self):
        shutil.rmtree(self.output_dir)

    def test_workflow_normalize_autobk_kweight_ft(self):
        """Test the following workflow:
        pymca normalize -> larch autobk -> kweight -> pymca ft
        """
payno's avatar
payno committed
92
        self.xas_obj.configuration = {"EXAFS": self.configuration_exafs}
93
        # pymca normalization
payno's avatar
payno committed
94
        out = PyMca_normalization(inputs={"xas_obj": self.xas_obj})()
95
        # larch autobk
payno's avatar
payno committed
96
        out = Larch_autobk(inputs={"xas_obj": out}).run()
97
        # k weight
payno's avatar
payno committed
98
        kweight_process = PyMca_k_weight(inputs={"xas_obj": out})
payno's avatar
payno committed
99
        kweight_process.setConfiguration({"k_weight": 0})
payno's avatar
payno committed
100
        out = kweight_process()
101
        # pymca ft
payno's avatar
payno committed
102
        out = PyMca_ft(inputs={"xas_obj": out})()
103

payno's avatar
payno committed
104
105
        self.assertTrue(out.spectra.data.flat[0].ft.intensity is not None)
        self.assertTrue(len(out.spectra.data.flat[0].ft.intensity) > 1)
106
107
108
109
110
111

    def test_workflow_preedge_exafs_xftf(self):
        """Test the following workflow:
        larch pre_edge -> pymca exafs -> larch xftf
        """
        # larch pre edge
payno's avatar
payno committed
112
        out = Larch_pre_edge(inputs={"xas_obj": self.xas_obj}).run()
113
        # pymca exafs
payno's avatar
payno committed
114
        exafs = PyMca_exafs(inputs={"xas_obj": out})
115
116

        exafs.setConfiguration(self.configuration_exafs)
payno's avatar
payno committed
117
        out = exafs()
118
        # for now we cannot link xftf because chi is not set by pymca exafs
payno's avatar
payno committed
119
120
121
122
123
        spectrum_0 = out.spectra.data.flat[0]
        self.assertFalse(spectrum_0["EXAFSSignal"] is None)
        self.assertFalse(spectrum_0["EXAFSKValues"] is None)
        self.assertFalse(spectrum_0.chi is None)
        self.assertFalse(spectrum_0.k is None)
124
        # larch xftf
payno's avatar
payno committed
125
        out = Larch_xftf(inputs={"xas_obj": out})()
126
127
128
129

        writer = XASWriter()
        writer.output_file = self.h5_file
        writer(out)