test_io.py 5.59 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
import warnings
38
39
40
41
42
43
44
45
46
47
48
49
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
50

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

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

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

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

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

    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
86
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
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
92
        dims = (Dim.DIM_2, Dim.DIM_1, Dim.DIM_0)
93
        with self.assertRaises(ValueError):
payno's avatar
payno committed
94
            XASReader().read_frm_url(
95
96
97
98
99
                InputInformation(
                    spectra_url=spectra_url,
                    channel_url=channel_url,
                    dimensions=dims,
                )
payno's avatar
payno committed
100
            )
101
        dims = (Dim.DIM_0, Dim.DIM_1, Dim.DIM_2)
payno's avatar
payno committed
102
        xas_obj = XASReader().read_frm_url(
103
104
105
106
107
            InputInformation(
                spectra_url=spectra_url,
                channel_url=channel_url,
                dimensions=dims,
            )
payno's avatar
payno committed
108
        )
109
110
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
payno's avatar
payno committed
111
112
113
114
        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)
115
116
117
118
119
120
121

    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
122
        spectra = numpy.arange(x_dim * y_dim * energy_dim).reshape(shape)
123
124
125
126
127
        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
128
        dims = (Dim.DIM_0, Dim.DIM_1, Dim.DIM_2)
payno's avatar
PEP8    
payno committed
129
        with warnings.catch_warnings(record=True):
payno's avatar
payno committed
130
            XASReader().read_frm_url(
131
132
133
134
135
                InputInformation(
                    spectra_url=spectra_url,
                    channel_url=channel_url,
                    dimensions=dims,
                )
payno's avatar
payno committed
136
            )
137

138
        dims = (Dim.DIM_2, Dim.DIM_1, Dim.DIM_0)
payno's avatar
payno committed
139
        xas_obj = XASReader().read_frm_url(
140
141
142
            InputInformation(
                spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
            )
payno's avatar
payno committed
143
        )
144
145
        self.assertTrue(isinstance(xas_obj, XASObject))
        self.assertTrue(xas_obj.n_spectrum == x_dim * y_dim)
payno's avatar
payno committed
146
147
148
149
150
151
        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
        )