test_reprocessing.py 5.72 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/26/2019"


30
31
32
33
34
import os
import shutil
import tempfile
import unittest
import numpy
payno's avatar
payno committed
35
from est.core.io import XASWriter
payno's avatar
payno committed
36
from est.core.types import XASObject
37
import urllib.request
payno's avatar
payno committed
38
from est.core.types import Spectrum
payno's avatar
payno committed
39
from ..reprocessing import exec_
40
from est.core.types import Dim
payno's avatar
payno committed
41

42
43
44
45
46
47
try:
    import PyMca5
except ImportError:
    has_pymca = False
else:
    has_pymca = True
payno's avatar
payno committed
48
49
50
51
    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
    from est.core.process.pymca.normalization import PyMca_normalization
52
    from est.io.utils.spec import read_spectrum
53
54
55
56
57
58
59
    from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
try:
    import larch
except ImportError:
    has_larch = False
else:
    has_larch = True
payno's avatar
payno committed
60
61
62
63
    from est.core.process.larch.pre_edge import Larch_pre_edge
    from est.core.process.larch.xftf import Larch_xftf
    from est.core.process.larch.autobk import Larch_autobk
    from est.io.utils.larch import read_ascii
payno's avatar
payno committed
64
65


payno's avatar
payno committed
66
@unittest.skipIf(has_pymca is False, "PyMca5 is not installed")
67
68
class TestReprocessingPyMca(unittest.TestCase):
    """test reprocessing work for some pymca process"""
payno's avatar
payno committed
69

payno's avatar
payno committed
70
71
    def setUp(self):
        self.output_dir = tempfile.mkdtemp()
72
        data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
payno's avatar
payno committed
73
74
75
76
77
        energy, mu = read_spectrum(
            data_file,
            energy_col_name="Column 1",
            absorption_col_name="Column 2",
        )
78
        self.spectrum = Spectrum(energy=energy, mu=mu)
payno's avatar
payno committed
79
80
81
82
        self.xas_obj_ref = XASObject(
            spectra=(self.spectrum,), energy=energy, dim1=1, dim2=1
        )
        self.h5_file = os.path.join(self.output_dir, "output_file.h5")
payno's avatar
payno committed
83
84
85
86
87
88
89
90
91
        out = PyMca_normalization()(xas_obj=self.xas_obj_ref)
        out = PyMca_exafs()(xas_obj=out)
        out = PyMca_k_weight()(xas_obj=out)
        out = PyMca_ft()(xas_obj=out)
        out = PyMca_normalization()(xas_obj=out)

        writer = XASWriter()
        writer.output_file = self.h5_file
        writer(out)
payno's avatar
payno committed
92
        self.assertTrue(out.spectra.data.flat[0].ft.intensity is not None)
payno's avatar
payno committed
93
        self.assertEqual(len(out.get_process_flow()), 5)
payno's avatar
payno committed
94
95
96
97
98
99
100

    def tearDown(self):
        shutil.rmtree(self.output_dir)

    def test(self):
        res_xas_obj = exec_(self.h5_file)
        self.assertEqual(res_xas_obj, self.xas_obj_ref)
payno's avatar
payno committed
101
        numpy.testing.assert_allclose(
payno's avatar
payno committed
102
103
            res_xas_obj.spectra.data.flat[0].ft.intensity,
            self.xas_obj_ref.spectra.data.flat[0].ft.intensity,
payno's avatar
payno committed
104
        )
105
106


payno's avatar
payno committed
107
@unittest.skipIf(has_larch is False, "Larch is not installed")
108
109
class TestReprocessingLarch(unittest.TestCase):
    """test reprocessing work for some larch process"""
payno's avatar
payno committed
110

111
112
113
    def setUp(self):
        self.output_dir = tempfile.mkdtemp()
        xmu_url = "https://raw.githubusercontent.com/xraypy/xraylarch/master/examples/xafs/cu_rt01.xmu"
payno's avatar
payno committed
114
        self.data_file = os.path.join(self.output_dir, "cu_rt01.xmu")
115

payno's avatar
payno committed
116
117
118
        with urllib.request.urlopen(xmu_url) as response, open(
            self.data_file, "wb"
        ) as out_file:
119
120
121
122
123
124
            data = response.read()  # a `bytes` object
            out_file.write(data)

        assert os.path.exists(self.data_file)
        energy, mu = read_ascii(self.data_file)
        self.spectrum = Spectrum(energy=energy, mu=mu)
payno's avatar
payno committed
125
126
127
        spectra = (self.spectrum,)
        self.xas_obj_ref = XASObject(spectra=spectra, energy=energy, dim1=1, dim2=1)
        self.h5_file = os.path.join(self.output_dir, "output_file.h5")
128
129

        pre_edge_process = Larch_pre_edge()
130
        pre_edge_process.set_properties({"rbkg": 1.0, "kweight": 2})
131
        autobk_process = Larch_autobk()
132
        autobk_process.set_properties(
payno's avatar
payno committed
133
134
            {"kmin": 2, "kmax": 16, "dk": 3, "window": "hanning", "kweight": 2}
        )
135
        xftf_process = Larch_xftf()
136
        xftf_process.set_properties({"kweight": 2})
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

        out = pre_edge_process(xas_obj=self.xas_obj_ref)
        out = autobk_process(xas_obj=out)
        out = xftf_process(xas_obj=out)

        writer = XASWriter()
        writer.output_file = self.h5_file
        writer(out)
        assert len(out.get_process_flow()) is 3

    def tearDown(self):
        shutil.rmtree(self.output_dir)

    def test(self):
        res_xas_obj = exec_(self.h5_file)
        self.assertEqual(res_xas_obj, self.xas_obj_ref)
payno's avatar
payno committed
153
        numpy.testing.assert_allclose(
payno's avatar
payno committed
154
155
            res_xas_obj.spectra.data.flat[0].chir_mag,
            self.xas_obj_ref.spectra.data.flat[0].chir_mag,
payno's avatar
payno committed
156
        )