test_workflow.py 4.2 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
27
28
29
30
31
32
33
34
# 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__ = "07/25/2019"

import os
import unittest
import tempfile
import shutil
import urllib.request
import numpy
payno's avatar
payno committed
35
from est.core.types import Spectrum
payno's avatar
payno committed
36

37
try:
payno's avatar
PEP8    
payno committed
38
    import larch  # noqa
39
40
41
42
except ImportError:
    has_larch = False
else:
    has_larch = True
payno's avatar
PEP8    
payno committed
43
    from est.core.process.larch.xftf import process_spectr_xftf as xftf
payno's avatar
payno committed
44
45
46
    from est.core.process.larch.autobk import process_spectr_autobk as autobk
    from est.core.process.larch.pre_edge import process_spectr_pre_edge as pre_edge
    from est.io.utils.larch import read_ascii
47
48


payno's avatar
payno committed
49
@unittest.skipIf(has_larch is False, "xraylarch not installed")
50
51
class TestLarchWorkflows(unittest.TestCase):
    """Test several larch workflow"""
payno's avatar
payno committed
52

53
54
55
    def setUp(self):
        self.outputdir = tempfile.mkdtemp()
        xmu_url = "https://raw.githubusercontent.com/xraypy/xraylarch/master/examples/xafs/cu_rt01.xmu"
payno's avatar
payno committed
56
        self.data_file = os.path.join(self.outputdir, "cu_rt01.xmu")
57

payno's avatar
payno committed
58
59
60
        with urllib.request.urlopen(xmu_url) as response, open(
            self.data_file, "wb"
        ) as out_file:
61
62
63
64
65
66
67
68
69
70
71
72
            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)

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

    def testAutobk(self):
        """equivalent treatment as the 'xafs.autobk.par script from larch'"""
payno's avatar
payno committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        pre_edge(
            spectrum=self.spectrum,
            overwrite=True,
            configuration={"rbkg": 1.0, "kweight": 2},
        )
        autobk(
            spectrum=self.spectrum,
            overwrite=True,
            configuration={
                "kmin": 2,
                "kmax": 16,
                "dk": 3,
                "window": "hanning",
                "kweight": 2,
            },
        )
        xftf(spectrum=self.spectrum, overwrite=True, configuration={"kweight": 2})
90
91
92

    def testXafsft1(self):
        """equivalent treatment as the 'xafs.doc_xafs1.par script from larch'"""
payno's avatar
payno committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        autobk(
            spectrum=self.spectrum,
            overwrite=True,
            configuration={"rbkg": 1.0, "kweight": 2, "clamp+hi": 10},
        )
        conf, spec_dk1 = xftf(
            spectrum=self.spectrum,
            overwrite=False,
            configuration={
                "kweight": 2,
                "kmin": 3,
                "kmax": 13,
                "window": "hanning",
                "dk": 1,
            },
        )
        conf, spec_dk2 = xftf(
            spectrum=self.spectrum,
            overwrite=False,
            configuration={
                "kweight": 2,
                "kmin": 3,
                "kmax": 13,
                "window": "hanning",
                "dk": 5,
            },
        )
120
121
        self.assertNotEqual(spec_dk1, spec_dk2)
        self.assertFalse(numpy.array_equal(spec_dk1.chir_re, spec_dk2.chir_re))