test_io.py 5.45 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
payno's avatar
payno committed
35
from est.units import ur
36
37
38
39
40
41
42
43
44
45
46
47
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
48

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

    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
61
        with h5py.File(self.filename, "a") as f:
62
63
            f[self.spectra_path] = spectra

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

    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
71
        with h5py.File(self.filename, "a") as f:
72
73
            f[self.channel_path] = channel

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

    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
84
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
85
86
87
88
89
90
91
        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
        dims = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
        with self.assertRaises(ValueError):
payno's avatar
payno committed
92
93
94
            XASReader().read_frm_url(
                spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
            )
95
96

        dims = (Dim.X_DIM, Dim.Y_DIM, Dim.CHANNEL_ENERGY_DIM)
payno's avatar
payno committed
97
98
99
        xas_obj = XASReader().read_frm_url(
            spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
        )
100
101
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
payno's avatar
payno committed
102
        numpy.testing.assert_array_equal(xas_obj.spectra[1].mu, spectra[1, 0, :])
103
104
105
106
107
108
109
110
        numpy.testing.assert_array_equal(xas_obj.spectra[2].energy, channel)

    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
111
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
112
113
114
115
116
117
118
        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
        dims = (Dim.X_DIM, Dim.Y_DIM, Dim.CHANNEL_ENERGY_DIM)
        with self.assertRaises(ValueError):
payno's avatar
payno committed
119
120
121
            XASReader().read_frm_url(
                spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
            )
122
123

        dims = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
payno's avatar
payno committed
124
125
126
        xas_obj = XASReader().read_frm_url(
            spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
        )
127
128
129
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
        numpy.testing.assert_array_equal(xas_obj.spectra[1].mu, spectra[:, 0, 1])
payno's avatar
payno committed
130
131
        numpy.testing.assert_array_equal(xas_obj.spectra[2].energy.m,
                                         (channel * ur.eV).m)
132
133
134
135


def suite():
    test_suite = unittest.TestSuite()
payno's avatar
payno committed
136
    for ui in (TestSpectraDimensions,):
137
138
139
140
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


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