File: test_frames.py

package info (click to toggle)
python-fabio 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,092 kB
  • sloc: python: 19,244; ansic: 1,085; makefile: 219; sh: 215
file content (278 lines) | stat: -rw-r--r-- 9,220 bytes parent folder | download | duplicates (4)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Fable Input Output
#             https://github.com/silx-kit/fabio
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Test frame concept of FabioImage

"""

import unittest
import logging
import numpy
import contextlib

logger = logging.getLogger(__name__)

import fabio.fabioimage
import fabio.edfimage
import fabio.file_series
from .utilstest import UtilsTest


class _CommonTestFrames(unittest.TestCase):
    """Test generic tests which could append on frame iteration"""

    @classmethod
    def getMeta(cls):
        return None

    @classmethod
    def setUpClass(cls):
        cls.meta = cls.getMeta()
        if cls.meta is None:
            raise unittest.SkipTest("No data test")

    @contextlib.contextmanager
    def image(self):
        if hasattr(self.meta, "image"):
            image = self.meta.image
            if image is not None:
                yield image
                return

        image = fabio.open(self.meta.filename)
        try:
            yield image
        finally:
            image.close()

    def test_frames_full_iteration(self):
        with self.image() as image:
            cache = {}
            for i, frame in enumerate(image.frames()):
                cache["data %d" % i] = numpy.array(frame.data)
                cache["header %d" % i] = frame.header.copy()
                self.assertEqual(i, frame.index)
            self.assertEqual(i, self.meta.nframes - 1)
            for i, frame in enumerate(image.frames()):
                data = cache.pop("data %d" % i)
                self.assertTrue(numpy.array_equal(data, frame.data))
                header = cache.pop("header %d" % i)
                self.assertEqual(header, frame.header)
                self.assertEqual(i, frame.index)
            self.assertEqual(len(cache), 0)
            self.assertEqual(i, self.meta.nframes - 1)
            self.assertEqual(image.nframes, self.meta.nframes)

    def test_frames_abort_iteration(self):
        with self.image() as image:
            for i, _frame in enumerate(image.frames()):
                if i == 2:
                    break
            for i, _frame in enumerate(image.frames()):
                pass
            self.assertEqual(i, self.meta.nframes - 1)
            self.assertEqual(image.nframes, self.meta.nframes)

    def test_frames_random_access(self):
        with self.image() as image:
            nframes = self.meta.nframes
            self.assertEqual(image.nframes, nframes)

            # before last
            frame2 = image._get_frame(nframes - 2)
            # first
            frame1 = image._get_frame(0)
            # last
            frame3 = image._get_frame(nframes - 1)

            self.assertIsNotNone(frame1)
            self.assertIsNotNone(frame2)
            self.assertIsNotNone(frame3)
            self.assertEqual(frame1.index, 0)
            self.assertEqual(frame2.index, nframes - 2)
            self.assertEqual(frame3.index, nframes - 1)
            self.assertIsNot(frame1, frame2)
            self.assertIsNot(frame2, frame3)
            self.assertIsNot(frame3, frame1)
            self.assertEqual(image.nframes, self.meta.nframes)


class TestVirtualEdf(_CommonTestFrames):

    @classmethod
    def getMeta(cls):
        header1 = {"foo": "bar"}
        data1 = numpy.array([[1, 1], [3, 4]], dtype=numpy.uint16)
        header2 = {"foo": "bar2"}
        data2 = numpy.array([[2, 2], [3, 4]], dtype=numpy.uint16)
        header3 = {"foo": "bar2"}
        data3 = numpy.array([[3, 3], [3, 4]], dtype=numpy.uint16)
        image = fabio.edfimage.EdfImage(data=data1, header=header1)
        image.append_frame(data=data2, header=header2)
        image.append_frame(data=data3, header=header3)
        frames = [(header1, data1), (header2, data2), (header3, data3)]

        class Meta(object):
            pass

        meta = Meta()
        meta.image = image
        meta.nframes = 3
        meta.frames = frames
        return meta

    def test_content(self):
        image = self.meta.image
        frames = self.meta.frames
        for i, frame in enumerate(image.frames()):
            header, data = frames[i]
            if i in [0, 1, 2]:
                self.assertIsInstance(frame, fabio.fabioimage.FabioFrame)
                self.assertIs(frame.file_container, image)
                self.assertEqual(frame.header["foo"], header["foo"])
                self.assertEqual(frame.file_index, i)
                self.assertEqual(frame.shape, data.shape)
                self.assertEqual(frame.dtype, data.dtype)
                self.assertEqual(frame.data[0, 0], i + 1)
                self.assertTrue(numpy.array_equal(frame.data, data))
            else:
                self.fail()


class TestEdf(_CommonTestFrames):

    @classmethod
    def getMeta(cls):
        filename = UtilsTest.getimage("multiframes.edf.bz2")
        filename = filename.replace(".bz2", "")
        image = fabio.open(filename)

        class Meta(object):
            pass

        meta = Meta()
        meta.image = image
        meta.nframes = 8
        return meta

    def test_content(self):
        image = self.meta.image
        self.assertEqual(image.nframes, 8)
        for i, frame in enumerate(image.frames()):
            if 0 <= i < 8:
                self.assertIsInstance(frame, fabio.fabioimage.FabioFrame)
                self.assertIs(frame.file_container, image)
                self.assertEqual(frame.file_index, i)
                self.assertEqual(frame.data[0, 0], i)
                self.assertEqual(frame.shape, (40, 20))
                self.assertEqual(frame.dtype, numpy.dtype("uint16"))
            else:
                self.fail()


class TestTiff(_CommonTestFrames):

    @classmethod
    def getMeta(cls):
        filename = UtilsTest.getimage("multiframes.tif.bz2")
        filename = filename.replace(".bz2", "")
        image = fabio.open(filename)

        class Meta(object):
            pass

        meta = Meta()
        meta.image = image
        meta.nframes = 8
        return meta

    def test_content(self):
        image = self.meta.image
        self.assertEqual(image.nframes, 8)
        for i, frame in enumerate(image.frames()):
            if 0 <= i < 8:
                self.assertIsInstance(frame, fabio.fabioimage.FabioFrame)
                self.assertIs(frame.file_container, image)
                self.assertEqual(frame.file_index, i)
                self.assertEqual(frame.data[0, 0], i)
                self.assertEqual(frame.shape, (40, 20))
                self.assertEqual(frame.dtype, numpy.dtype("uint16"))
            else:
                self.fail()


class TestFabioImage(unittest.TestCase):

    def test_single_frame_iterator(self):
        data = numpy.array([[1, 2], [3, 4]], dtype=numpy.uint16)
        image = fabio.fabioimage.FabioImage(data=data)
        for i, frame in enumerate(image.frames()):
            if i == 0:
                self.assertIsInstance(frame, fabio.fabioimage.FabioFrame)
                self.assertIs(frame.file_container, image)
                self.assertEqual(frame.file_index, 0)
                self.assertEqual(frame.shape, data.shape)
                self.assertEqual(frame.dtype, data.dtype)
                self.assertTrue(numpy.array_equal(frame.data, data))
            else:
                self.fail()


class TestFileSeries(_CommonTestFrames):

    @classmethod
    def getMeta(cls):
        filenames = []
        filename = UtilsTest.getimage("multiframes.tif.bz2")
        filename = filename.replace(".bz2", "")
        filenames.append(filename)
        filename = UtilsTest.getimage("multiframes.edf.bz2")
        filename = filename.replace(".bz2", "")
        filenames.append(filename)
        image = fabio.file_series.FileSeries(filenames)

        class Meta(object):
            pass

        meta = Meta()
        meta.image = image
        meta.nframes = 8 * 2
        return meta


def suite():
    loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
    testsuite = unittest.TestSuite()
    testsuite.addTest(loadTests(TestFabioImage))
    testsuite.addTest(loadTests(TestVirtualEdf))
    testsuite.addTest(loadTests(TestEdf))
    testsuite.addTest(loadTests(TestTiff))
    testsuite.addTest(loadTests(TestFileSeries))
    return testsuite


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