test_autobk.py 4.46 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
# 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
payno's avatar
payno committed
34
from est.core.types import Spectrum, XASObject
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
    from est.core.process.larch.autobk import larch_autobk, process_spectr_autobk
    from est.io.utils.larch import read_ascii
44
45


payno's avatar
payno committed
46
@unittest.skipIf(has_larch is False, "xraylarch not installed")
47
48
class TestLarchSpectrum(unittest.TestCase):
    """Make sure computation on one spectrum is valid"""
payno's avatar
payno committed
49

50
51
52
    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
53
        self.data_file = os.path.join(self.outputdir, "cu_rt01.xmu")
54

payno's avatar
payno committed
55
56
57
        with urllib.request.urlopen(xmu_url) as response, open(
            self.data_file, "wb"
        ) as out_file:
58
59
60
61
62
63
            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
64
        self.configuration = {"kweight": 1}
65
66
67
68
69

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

    def testProcess(self):
70
71
        self.assertTrue(self.spectrum.k is None)
        self.assertTrue(self.spectrum.chi is None)
payno's avatar
payno committed
72
        conf, res_spectrum = process_spectr_autobk(self.spectrum, self.configuration)
73
74
        self.assertFalse(self.spectrum.k is None)
        self.assertFalse(self.spectrum.chi is None)
75
76


payno's avatar
payno committed
77
@unittest.skipIf(has_larch is False, "xraylarch not installed")
78
79
class TestLarchSpectra(unittest.TestCase):
    """Make sure computation on spectra is valid (n spectrum)"""
payno's avatar
payno committed
80

81
82
83
84
    def setUp(self):
        self.outputdir = tempfile.mkdtemp()
        # Download test file from xraylarch
        xmu_url = "https://raw.githubusercontent.com/xraypy/xraylarch/master/examples/xafs/cu_rt01.xmu"
payno's avatar
payno committed
85
86
87
88
        self.data_file = os.path.join(self.outputdir, "cu_rt01.xmu")
        with urllib.request.urlopen(xmu_url) as response, open(
            self.data_file, "wb"
        ) as out_file:
89
90
91
92
            data = response.read()  # a `bytes` object
            out_file.write(data)
        assert os.path.exists(self.data_file)
        # define the xas object
payno's avatar
payno committed
93
        self.configuration = {"kweight": 1}
94
        energy, mu = read_ascii(self.data_file)
payno's avatar
payno committed
95
96
97
98
99
100
101
        self.xas_object = XASObject(
            spectra=(Spectrum(energy=energy, mu=mu),),
            energy=energy,
            dim1=1,
            dim2=1,
            configuration=self.configuration,
        )
102
103
104
105
106
107

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

    def testProcessXASObject(self):
        res = larch_autobk(self.xas_object)
108
        assert isinstance(res, XASObject)
payno's avatar
payno committed
109
        spectrum0 = res.spectra.data.flat[0]
payno's avatar
payno committed
110
111
        self.assertTrue(hasattr(spectrum0, "k"))
        self.assertTrue(hasattr(spectrum0, "chi"))
112
113
114

    def testProcessAsDict(self):
        res = larch_autobk(self.xas_object.to_dict())
115
        assert isinstance(res, XASObject)
payno's avatar
payno committed
116
        spectrum0 = res.spectra.data.flat[0]
payno's avatar
payno committed
117
118
        self.assertTrue(hasattr(spectrum0, "k"))
        self.assertTrue(hasattr(spectrum0, "chi"))