test_pymca_normalization.py 4.78 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
# 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/11/2019"


import os
31
import shutil
32
33
import tempfile
import unittest
34
import h5py
35
from silx.io.url import DataUrl
payno's avatar
payno committed
36
37
from est.core.io import read as read_xas
from est.core.types import Spectrum, XASObject
payno's avatar
payno committed
38
from est.core.types import Spectra
payno's avatar
payno committed
39
from est.core.utils import spectra as spectra_utils
40
from est.core.types import Dim
payno's avatar
payno committed
41

42
try:
payno's avatar
PEP8    
payno committed
43
    import PyMca5  # noqa
44
45
46
except ImportError:
    has_pymca = False
else:
payno's avatar
payno committed
47
    has_pymca = True
48
    from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
payno's avatar
payno committed
49
    from est.core.process.pymca.normalization import pymca_normalization
50
    from est.io.utils.spec import read_spectrum
payno's avatar
payno committed
51
52


payno's avatar
payno committed
53
@unittest.skipIf(has_pymca is False, "PyMca5 is not installed")
54
class TestNormalizationSingleSpectrum(unittest.TestCase):
55
    """Make sure the process is processing correctly on a spectrum"""
payno's avatar
payno committed
56

57
    def setUp(self):
payno's avatar
payno committed
58
        data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
payno's avatar
payno committed
59
60
61
62
63
        energy, mu = read_spectrum(
            data_file,
            energy_col_name="Column 1",
            absorption_col_name="Column 2",
        )
64
        self.spectrum = Spectrum(energy=energy, mu=mu)
payno's avatar
payno committed
65
66
67
        self.xas_obj = XASObject(
            spectra=(self.spectrum,), energy=energy, dim1=1, dim2=1
        )
68
69
70

    def testWithXASObjAsInput(self):
        res = pymca_normalization(xas_obj=self.xas_obj)
71
        self.assertTrue(isinstance(res, XASObject))
payno's avatar
payno committed
72
73
        self.assertTrue(isinstance(res.spectra, (Spectra)))
        res_spectrum = res.spectra.data.flat[0]
74
        self.assertTrue(isinstance(res_spectrum, Spectrum))
payno's avatar
payno committed
75
76
77
        self.assertTrue("NormalizedMu" in res_spectrum)
        self.assertTrue("NormalizedEnergy" in res_spectrum)
        self.assertTrue("NormalizedSignal" in res_spectrum)
78
79
80

    def testWithDictAsInput(self):
        res = pymca_normalization(xas_obj=self.xas_obj.to_dict())
81
        self.assertTrue(isinstance(res, XASObject))
payno's avatar
payno committed
82
83
84
        self.assertTrue("NormalizedMu" in res.spectra.data.flat[0])
        self.assertTrue("NormalizedEnergy" in res.spectra.data.flat[0])
        self.assertTrue("NormalizedSignal" in res.spectra.data.flat[0])
payno's avatar
payno committed
85
86


payno's avatar
payno committed
87
@unittest.skipIf(has_pymca is False, "PyMca5 is not installed")
88
class TestNormalizationMultipleSpectrum(unittest.TestCase):
89
    """Make sure the process is processing correctly on a XASObject spectra"""
payno's avatar
payno committed
90

91
    def setUp(self):
92
        self.energy, self.spectra = spectra_utils.create_dataset(shape=(256, 20, 10))
93
        self.output_dir = tempfile.mkdtemp()
payno's avatar
payno committed
94
95
96
97
        spectra_path = "/data/NXdata/data"
        channel_path = "/data/NXdata/Channel"
        filename = os.path.join(self.output_dir, "myfile.h5")
        with h5py.File(filename, "a") as f:
98
99
100
            f[spectra_path] = self.spectra
            f[channel_path] = self.energy

payno's avatar
payno committed
101
102
103
104
105
106
107
        self.xas_obj = read_xas(
            spectra_url=DataUrl(
                file_path=filename, data_path=spectra_path, scheme="silx"
            ),
            channel_url=DataUrl(
                file_path=filename, data_path=channel_path, scheme="silx"
            ),
108
            dimensions=(Dim.DIM_2, Dim.DIM_1, Dim.DIM_0),
payno's avatar
payno committed
109
        )
110
111
112

    def tearDown(self):
        shutil.rmtree(path=self.output_dir)
113
114
115
116

    def testWithXASObjAsInput(self):
        res = pymca_normalization(xas_obj=self.xas_obj)
        self.assertTrue(isinstance(res, XASObject))
payno's avatar
payno committed
117
        for spectrum in self.xas_obj.spectra.data.flat:
payno's avatar
payno committed
118
119
120
            self.assertTrue("NormalizedMu" in spectrum)
            self.assertTrue("NormalizedEnergy" in spectrum)
            self.assertTrue("NormalizedSignal" in spectrum)
121
122
123

    def testWithDictAsInput(self):
        pass