test_io.py 5.83 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
34
# 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/26/2019"


import unittest
from est.core.types import Dim
from est.core.io import XASReader
from silx.io.url import DataUrl
from est.core.types import XASObject
35
from est.io.utils.information import InputInformation
payno's avatar
payno committed
36
from est.units import ur
37
38
39
40
41
42
43
44
45
46
47
48
import numpy
import os
import tempfile
import h5py
import shutil


class TestSpectraDimensions(unittest.TestCase):
    """
    Test reading spectra with different dimensions organisation
    (X, Y, Channels), (Channels, Y, X), (Y, Channels, X)
    """
payno's avatar
payno committed
49

50
    def setUp(self) -> None:
payno's avatar
payno committed
51
52
        self.spectra_path = "/data/NXdata/data"
        self.channel_path = "/data/NXdata/Channel"
53
        self.output_dir = tempfile.mkdtemp()
payno's avatar
payno committed
54
        self.filename = os.path.join(self.output_dir, "myfile.h5")
55
56
57
58
59
60
61

    def tearDown(self) -> None:
        shutil.rmtree(self.output_dir)

    def saveSpectra(self, spectra):
        """Save the spectra to the spectra file defined in setup and return the
        associated silx url"""
payno's avatar
payno committed
62
        with h5py.File(self.filename, "a") as f:
63
64
            f[self.spectra_path] = spectra

payno's avatar
payno committed
65
66
67
        return DataUrl(
            file_path=self.filename, data_path=self.spectra_path, scheme="silx"
        )
68
69
70
71

    def saveChannel(self, channel):
        """Save the energy to the spectra file defined in setup and return the
        associated silx url"""
payno's avatar
payno committed
72
        with h5py.File(self.filename, "a") as f:
73
74
            f[self.channel_path] = channel

payno's avatar
payno committed
75
76
77
        return DataUrl(
            file_path=self.filename, data_path=self.channel_path, scheme="silx"
        )
78
79
80
81
82
83
84

    def testDimensionsXYEnergy(self):
        """Test that spectra stored as X, Y Energy can be read"""
        x_dim = 4
        y_dim = 2
        energy_dim = 3
        shape = (x_dim, y_dim, energy_dim)
payno's avatar
payno committed
85
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
86
87
88
89
90
        channel = numpy.linspace(0, 1, energy_dim)
        spectra_url = self.saveSpectra(spectra)
        channel_url = self.saveChannel(channel=channel)

        # if dims are incoherent with energy, should raise an error
91
        dims = (Dim.DIM_2, Dim.DIM_1, Dim.DIM_0)
92
        with self.assertRaises(ValueError):
payno's avatar
payno committed
93
            XASReader().read_frm_url(
94
95
96
97
98
                InputInformation(
                    spectra_url=spectra_url,
                    channel_url=channel_url,
                    dimensions=dims,
                )
payno's avatar
payno committed
99
            )
100
        dims = (Dim.DIM_0, Dim.DIM_1, Dim.DIM_2)
payno's avatar
payno committed
101
        xas_obj = XASReader().read_frm_url(
102
103
104
105
106
            InputInformation(
                spectra_url=spectra_url,
                channel_url=channel_url,
                dimensions=dims,
            )
payno's avatar
payno committed
107
        )
108
109
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
payno's avatar
payno committed
110
111
112
113
        numpy.testing.assert_array_equal(
            xas_obj.spectra.data.flat[1].mu, spectra[1, 0, :]
        )
        numpy.testing.assert_array_equal(xas_obj.spectra.data.flat[2].energy, channel)
114
115
116
117
118
119
120

    def testDimensionsChannelYX(self):
        """Test that spectra stored as Channel, Y, X can be read"""
        x_dim = 10
        y_dim = 5
        energy_dim = 30
        shape = (energy_dim, y_dim, x_dim)
payno's avatar
payno committed
121
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
122
123
124
125
126
        channel = numpy.linspace(0, 100, energy_dim)
        spectra_url = self.saveSpectra(spectra)
        channel_url = self.saveChannel(channel=channel)

        # if dims are incoherent with energy, should raise an error
127
        dims = (Dim.DIM_0, Dim.DIM_1, Dim.DIM_2)
128
        with self.assertRaises(ValueError):
payno's avatar
payno committed
129
            XASReader().read_frm_url(
130
131
132
133
134
                InputInformation(
                    spectra_url=spectra_url,
                    channel_url=channel_url,
                    dimensions=dims,
                )
payno's avatar
payno committed
135
            )
136

137
        dims = (Dim.DIM_2, Dim.DIM_1, Dim.DIM_0)
payno's avatar
payno committed
138
        xas_obj = XASReader().read_frm_url(
139
140
141
            InputInformation(
                spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
            )
payno's avatar
payno committed
142
        )
143
144
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
payno's avatar
payno committed
145
146
147
148
149
150
        numpy.testing.assert_array_equal(
            xas_obj.spectra.data.flat[1].mu, spectra[:, 0, 1]
        )
        numpy.testing.assert_array_equal(
            xas_obj.spectra.data.flat[2].energy, (channel * ur.eV).m
        )
151
152
153
154


def suite():
    test_suite = unittest.TestSuite()
payno's avatar
payno committed
155
    for ui in (TestSpectraDimensions,):
156
157
158
159
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


payno's avatar
payno committed
160
if __name__ == "__main__":
161
    unittest.main(defaultTest="suite")