summaryrefslogtreecommitdiff
path: root/silx/io/test/test_spectoh5.py
blob: 903a62c957e588370d7ebf1ea2a0d5df92e87b26 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-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.
#
# ############################################################################*/
"""Tests for SpecFile to HDF5 converter"""

from numpy import array_equal
import os
import sys
import tempfile
import unittest

import h5py

from ..spech5 import SpecH5, SpecH5Group
from ..convert import convert, write_to_h5
from ..utils import h5py_read_dataset

__authors__ = ["P. Knobel"]
__license__ = "MIT"
__date__ = "12/02/2018"


sfdata = b"""#F /tmp/sf.dat
#E 1455180875
#D Thu Feb 11 09:54:35 2016
#C imaging  User = opid17
#O0 Pslit HGap  MRTSlit UP  MRTSlit DOWN
#O1 Sslit1 VOff  Sslit1 HOff  Sslit1 VGap
#o0 pshg mrtu mrtd
#o2 ss1vo ss1ho ss1vg

#J0 Seconds  IA  ion.mono  Current
#J1 xbpmc2  idgap1  Inorm

#S 1  ascan  ss1vo -4.55687 -0.556875  40 0.2
#D Thu Feb 11 09:55:20 2016
#T 0.2  (Seconds)
#P0 180.005 -0.66875 0.87125
#P1 14.74255 16.197579 12.238283
#N 4
#L MRTSlit UP  second column  3rd_col
-1.23 5.89  8
8.478100E+01  5 1.56
3.14 2.73 -3.14
1.2 2.3 3.4

#S 1 aaaaaa
#D Thu Feb 11 10:00:32 2016
#@MCADEV 1
#@MCA %16C
#@CHANN 3 0 2 1
#@CALIB 1 2 3
#N 3
#L uno  duo
1 2
@A 0 1 2
@A 10 9 8
3 4
@A 3.1 4 5
@A 7 6 5
5 6
@A 6 7.7 8
@A 4 3 2
"""


class TestConvertSpecHDF5(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        fd, cls.spec_fname = tempfile.mkstemp(prefix="TestConvertSpecHDF5")
        os.write(fd, sfdata)
        os.close(fd)

        fd, cls.h5_fname = tempfile.mkstemp(prefix="TestConvertSpecHDF5")
        # Close and delete (we just need the name)
        os.close(fd)
        os.unlink(cls.h5_fname)

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.spec_fname)

    def setUp(self):
        convert(self.spec_fname, self.h5_fname)

        self.sfh5 = SpecH5(self.spec_fname)
        self.h5f = h5py.File(self.h5_fname, "a")

    def tearDown(self):
        self.h5f.close()
        self.sfh5.close()
        os.unlink(self.h5_fname)

    def testAppendToHDF5(self):
        write_to_h5(self.sfh5, self.h5f, h5path="/foo/bar/spam")
        self.assertTrue(
            array_equal(self.h5f["/1.2/measurement/mca_1/data"],
                        self.h5f["/foo/bar/spam/1.2/measurement/mca_1/data"])
        )

    def testWriteSpecH5Group(self):
        """Test passing a SpecH5Group as parameter, instead of a Spec filename
        or a SpecH5."""
        g = self.sfh5["1.1/instrument"]
        self.assertIsInstance(g, SpecH5Group)        # let's be paranoid
        write_to_h5(g, self.h5f, h5path="my instruments")

        self.assertAlmostEqual(self.h5f["my instruments/positioners/Sslit1 HOff"][tuple()],
                               16.197579, places=4)

    def testTitle(self):
        """Test the value of a dataset"""
        title12 = h5py_read_dataset(self.h5f["/1.2/title"])
        self.assertEqual(title12,
                         u"aaaaaa")

    def testAttrs(self):
        # Test root group (file) attributes
        self.assertEqual(self.h5f.attrs["NX_class"],
                         u"NXroot")
        # Test dataset attributes
        ds = self.h5f["/1.2/instrument/mca_1/data"]
        self.assertTrue("interpretation" in ds.attrs)
        self.assertEqual(list(ds.attrs.values()),
                         [u"spectrum"])
        # Test group attributes
        grp = self.h5f["1.1"]
        self.assertEqual(grp.attrs["NX_class"],
                         u"NXentry")
        self.assertEqual(len(list(grp.attrs.keys())),
                         1)

    def testHdf5HasSameMembers(self):
        spec_member_list = []

        def append_spec_members(name):
            spec_member_list.append(name)
        self.sfh5.visit(append_spec_members)

        hdf5_member_list = []

        def append_hdf5_members(name):
            hdf5_member_list.append(name)
        self.h5f.visit(append_hdf5_members)

        # 1. For some reason, h5py visit method doesn't include the leading
        # "/" character when it passes the member name to the function,
        # even though an explicit the .name attribute of a member will
        # have a leading "/"
        spec_member_list = [m.lstrip("/") for m in spec_member_list]

        self.assertEqual(set(hdf5_member_list),
                         set(spec_member_list))

    def testLinks(self):
        self.assertTrue(
            array_equal(self.sfh5["/1.2/measurement/mca_0/data"],
                        self.h5f["/1.2/measurement/mca_0/data"])
        )
        self.assertTrue(
            array_equal(self.h5f["/1.2/instrument/mca_1/channels"],
                        self.h5f["/1.2/measurement/mca_1/info/channels"])
        )


def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(
        unittest.defaultTestLoader.loadTestsFromTestCase(TestConvertSpecHDF5))
    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest="suite")