test_mback_norm.py 4.93 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
44
45
    from est.core.process.larch.mback_norm import (
        larch_mback_norm,
        process_spectr_mback_norm,
    )
payno's avatar
payno committed
46
47
    from est.core.process.larch.pre_edge import process_spectr_pre_edge
    from est.io.utils.larch import read_ascii
48
49


payno's avatar
payno committed
50
@unittest.skipIf(has_larch is False, "xraylarch not installed")
51
52
class TestLarchSpectrum(unittest.TestCase):
    """Make sure computation on one spectrum is valid"""
payno's avatar
payno committed
53

54
55
56
    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
57
        self.data_file = os.path.join(self.outputdir, "cu_rt01.xmu")
58

payno's avatar
payno committed
59
60
61
        with urllib.request.urlopen(xmu_url) as response, open(
            self.data_file, "wb"
        ) as out_file:
62
63
64
65
66
67
            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
68
69
70
71
72
        process_spectr_pre_edge(
            spectrum=self.spectrum, overwrite=True, configuration={}
        )
        assert hasattr(self.spectrum, "norm")
        self.configuration = {"z": 29}
73
74
75
76
77

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

    def testProcess(self):
payno's avatar
payno committed
78
79
        self.assertFalse(hasattr(self.spectrum, "mback_mu"))
        self.assertFalse(hasattr(self.spectrum, "norm_mback"))
80
        process_spectr_mback_norm(self.spectrum, self.configuration)
payno's avatar
payno committed
81
82
        self.assertTrue(hasattr(self.spectrum, "mback_mu"))
        self.assertTrue(hasattr(self.spectrum, "norm_mback"))
83
84


payno's avatar
payno committed
85
@unittest.skipIf(has_larch is False, "xraylarch not installed")
86
87
class TestLarchSpectra(unittest.TestCase):
    """Make sure computation on spectra is valid (n spectrum)"""
payno's avatar
payno committed
88

89
90
91
92
    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
93
94
95
96
        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:
97
98
99
100
            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
101
        self.configuration = {"z": 29}
102
103
        energy, mu = read_ascii(self.data_file)
        spectrum = Spectrum(energy=energy, mu=mu)
payno's avatar
payno committed
104
105
106
107
108
109
110
111
112
        process_spectr_pre_edge(spectrum=spectrum, overwrite=True, configuration={})
        assert hasattr(spectrum, "norm")
        self.xas_object = XASObject(
            spectra=(spectrum,),
            energy=energy,
            dim1=1,
            dim2=1,
            configuration=self.configuration,
        )
113
114
115
116
117
118
119

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

    def testProcessXASObject(self):
        res = larch_mback_norm(self.xas_object)
        assert isinstance(res, XASObject)
payno's avatar
payno committed
120
        spectrum0 = res.spectra.data.flat[0]
payno's avatar
payno committed
121
122
        self.assertTrue(hasattr(spectrum0, "mback_mu"))
        self.assertTrue(hasattr(spectrum0, "norm_mback"))
123
124
125
126

    def testProcessAsDict(self):
        res = larch_mback_norm(self.xas_object.to_dict())
        assert isinstance(res, XASObject)
payno's avatar
payno committed
127
        spectrum0 = res.spectra.data.flat[0]
payno's avatar
payno committed
128
129
        self.assertTrue(hasattr(spectrum0, "mback_mu"))
        self.assertTrue(hasattr(spectrum0, "norm_mback"))