test_pre_edge.py 4.49 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
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

payno's avatar
payno committed
36
try:
payno's avatar
PEP8    
payno committed
37
    import larch  # noqa
payno's avatar
payno committed
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.pre_edge import larch_pre_edge, process_spectr_pre_edge
    from est.io.utils.larch import read_ascii
payno's avatar
payno committed
44
45


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

payno's avatar
payno committed
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")
payno's avatar
payno committed
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:
payno's avatar
payno committed
58
59
60
61
62
63
64
65
66
67
68
69
            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)
        self.configuration = {}

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

    def testProcess(self):
70
71
        self.assertTrue(self.spectrum.pre_edge is None)
        self.assertTrue(self.spectrum.e0 is None)
payno's avatar
payno committed
72
        conf, res_spectrum = process_spectr_pre_edge(self.spectrum, self.configuration)
73
74
        self.assertTrue(self.spectrum.pre_edge is not None)
        self.assertTrue(self.spectrum.e0 is not None)
payno's avatar
payno committed
75
76


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

payno's avatar
payno committed
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:
payno's avatar
payno committed
89
90
91
92
93
94
            data = response.read()  # a `bytes` object
            out_file.write(data)
        assert os.path.exists(self.data_file)
        # define the xas object
        self.configuration = {}
        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,
        )
payno's avatar
payno committed
102
103
104
105
106
107

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

    def testProcessXASObject(self):
        res = larch_pre_edge(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, "pre_edge"))
        self.assertTrue(hasattr(spectrum0, "edge"))
payno's avatar
payno committed
112
113
114

    def testProcessAsDict(self):
        res = larch_pre_edge(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, "pre_edge"))
        self.assertTrue(hasattr(spectrum0, "edge"))