test_io_with_pymca.py 3.86 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
# 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"

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

41
try:
payno's avatar
PEP8    
payno committed
42
    import PyMca5  # noqa
43
44
45
46
47
except ImportError:
    has_pymca = False
else:
    has_pymca = True
    from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
48
49


50
class TestReadWrite(unittest.TestCase):
51
    """Test read function for spectra and configuration"""
payno's avatar
payno committed
52

53
54
    def setUp(self):
        self.outputdir = tempfile.mkdtemp()
payno's avatar
payno committed
55
        self.outputfile = os.path.join(self.outputdir, "output_file.h5")
56
57
58
59

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

payno's avatar
payno committed
60
    @unittest.skipIf(has_pymca is False, "PyMca5 is not installed")
61
62
    def testReadSpectrum(self):
        data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
payno's avatar
payno committed
63
        res = read_xas(
payno's avatar
payno committed
64
65
66
67
68
69
            spectra_url=DataUrl(
                file_path=data_file, scheme="PyMca", data_path="Column 1"
            ),
            channel_url=DataUrl(
                file_path=data_file, scheme="PyMca", data_path="Column 2"
            ),
payno's avatar
payno committed
70
        )
71
        self.assertTrue(isinstance(res, XASObject))
72
        self.assertEqual(res.n_spectrum, 1)
payno's avatar
payno committed
73
74
        self.assertTrue("Mu" in res.spectra.data.flat[0])
        self.assertTrue("Energy" in res.spectra.data.flat[0])
75

76

77
78
class TestNxWriting(unittest.TestCase):
    """Test that the nx process is correctly store ad the output data"""
payno's avatar
payno committed
79

80
81
    def setUp(self):
        self.output_dir = tempfile.mkdtemp()
82
        energy, spectra = spectra_utils.create_dataset(shape=(256, 20, 10))
payno's avatar
payno committed
83
84
        self.xas_obj = XASObject(spectra=spectra, energy=energy, dim1=20, dim2=10)
        self.h5_file = os.path.join(self.output_dir, "output_file.h5")
85
86
87
88
89
90
91
92

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

    def testWriteRead(self):
        writer = XASWriter()
        writer.output_file = self.h5_file
        writer(self.xas_obj)
payno's avatar
payno committed
93
94
95
96
97
98
99
        with h5py.File(self.h5_file, "r") as hdf:
            self.assertTrue("scan1" in hdf.keys())
            self.assertTrue("data" in hdf["scan1"].keys())
            self.assertTrue("absorbed_beam" in hdf["scan1"].keys())
            self.assertTrue("monochromator" in hdf["scan1"].keys())

        loaded_xas_obj = XASObject.from_file(self.h5_file, configuration_path=None)
100
        numpy.testing.assert_allclose(loaded_xas_obj.energy, self.xas_obj.energy)
payno's avatar
payno committed
101
102
103
        numpy.testing.assert_allclose(
            loaded_xas_obj.absorbed_beam(), self.xas_obj.absorbed_beam()
        )