File: test_utils_header.py

package info (click to toggle)
pyfai 0.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,460 kB
  • sloc: python: 49,743; lisp: 7,059; sh: 225; ansic: 165; makefile: 119
file content (196 lines) | stat: -rw-r--r-- 8,044 bytes parent folder | download
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
195
196
#!/usr/bin/env python
# coding: utf-8
#
#    Project: Azimuthal integration
#             https://github.com/silx-kit/pyFAI
#
#    Copyright (C) 2015-2018 European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# 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.

"""Test suite for math utilities library"""

__author__ = "Jérôme Kieffer"
__contact__ = "Jerome.Kieffer@ESRF.eu"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "16/10/2020"

import unittest
import numpy
import logging
import shutil
import os
import fabio
import h5py

logger = logging.getLogger(__name__)

from .utilstest import UtilsTest
from ..utils import header_utils


class TestEdfMonitor(unittest.TestCase):

    def setUp(self):
        header = {
            "mon1": "100",
            "bad": "foo",
            "counter_pos": "12 13 14 foo",
            "counter_mne": "mon2 mon3 mon4 mon5",
            "bad_size_pos": "foo foo foo",
            "bad_size_mne": "mon2 mon3 mon4 mon5",
            "mne_not_exists_pos": "12 13 14 foo",
            "pos_not_exists_mne": "mon2 mon3 mon4 mon5",
        }
        self.image = fabio.numpyimage.numpyimage(numpy.array([]), header)

    def test_monitor(self):
        result = header_utils._get_monitor_value_from_edf(self.image, "mon1")
        self.assertEquals(100, result)

    def test_monitor_in_counter(self):
        result = header_utils._get_monitor_value_from_edf(self.image, "counter/mon3")
        self.assertEquals(13, result)

    def test_bad_monitor(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "bad")

    def test_bad_monitor_in_counter(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "counter/mon5")

    def test_bad_counter_syntax(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "counter/mon5/1")

    def test_missing_monitor(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "not_exists")

    def test_missing_counter(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "not_exists/mon")

    def test_missing_counter_monitor(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "counter/not_exists")

    def test_missing_counter_mne(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "mne_not_exists/mon")

    def test_missing_counter_pos(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "pos_not_exists/mon")

    def test_missing_counter_pos_element(self):
        self.assertRaises(header_utils.MonitorNotFound, header_utils._get_monitor_value_from_edf, self.image, "bad_size/mon")

    def test_edf_file_motor(self):
        image = fabio.open(UtilsTest.getimage("Pilatus1M.edf"))
        result = header_utils._get_monitor_value_from_edf(image, "motor/lx")
        self.assertEqual(result, -0.2)

    def test_edf_file_key(self):
        image = fabio.open(UtilsTest.getimage("Pilatus1M.edf"))
        result = header_utils._get_monitor_value_from_edf(image, "scan_no")
        self.assertEqual(result, 19)


class TestHdf5Monitor(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestHdf5Monitor, cls).setUpClass()
        cls.tempDir = os.path.join(UtilsTest.tempdir, cls.__name__)
        os.makedirs(cls.tempDir)
        cls.file = os.path.join(cls.tempDir, "file.h5")

        h5 = h5py.File(cls.file, mode="w")
        data = numpy.array([1.0, 1.2, 1.3, 1.4]) + numpy.array([0, 0, 0]).reshape(-1, 1)
        h5["images"] = data.reshape(-1, 2, 2)
        h5["header/bar/vector"] = numpy.array([1.0, 1.2, 1.3])
        h5["header/bar/const"] = 1.5
        h5["header/bar/bad_type"] = numpy.array([1.0, 1.2j, 1.3]).reshape(1, 3, 1)
        h5["header/bar/bad_shape"] = numpy.array([1.0, 1.2, 1.3]).reshape(1, 3, 1)
        h5["header/bar/bad_size"] = numpy.array([1.0, 1.2])
        h5.close()

    @classmethod
    def tearDownClass(cls):
        super(TestHdf5Monitor, cls).tearDownClass()
        shutil.rmtree(cls.tempDir)
        cls.tempDir = None

    def test_vector_monitor(self):
        pass

    def test_const_monitor(self):
        monitor_key = "/header/bar/const"
        with fabio.open(self.file + "::/images") as image:
            for iframe in range(image.nframes):
                frame = image.getframe(iframe)
                result = header_utils.get_monitor_value(frame, monitor_key)
                self.assertEquals(1.5, result)

    def test_missing_monitor(self):
        monitor_key = "/header/bar/vector"
        expected_values = [1.0, 1.2, 1.3]
        with fabio.open(self.file + "::/images") as image:
            for iframe in range(image.nframes):
                frame = image.getframe(iframe)
                result = header_utils.get_monitor_value(frame, monitor_key)
                self.assertAlmostEqual(result, expected_values[iframe])

    def test_bad_type_monitor(self):
        monitor_key = "/header/bar/bad_type"
        with fabio.open(self.file + "::/images") as image:
            frame = image.getframe(0)
            with self.assertRaises(header_utils.MonitorNotFound):
                header_utils.get_monitor_value(frame, monitor_key)

    def test_bad_shape_monitor(self):
        monitor_key = "/header/bar/bad_shape"
        with fabio.open(self.file + "::/images") as image:
            frame = image.getframe(0)
            with self.assertRaises(header_utils.MonitorNotFound):
                header_utils.get_monitor_value(frame, monitor_key)

    def test_bad_size_monitor(self):
        monitor_key = "/header/bar/bad_size"
        expected_values = [1.0, 1.2, header_utils.MonitorNotFound]
        with fabio.open(self.file + "::/images") as image:
            for iframe in range(image.nframes):
                frame = image.getframe(iframe)
                expected_value = expected_values[iframe]
                if isinstance(expected_value, type(Exception)) and issubclass(expected_value, Exception):
                    with self.assertRaises(expected_value):
                        header_utils.get_monitor_value(frame, monitor_key)
                else:
                    result = header_utils.get_monitor_value(frame, monitor_key)
                    self.assertAlmostEqual(result, expected_value)


def suite():
    loader = unittest.defaultTestLoader.loadTestsFromTestCase
    testsuite = unittest.TestSuite()
    testsuite.addTest(loader(TestEdfMonitor))
    return testsuite


if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite())
    UtilsTest.clean_up()