File: test_format.py

package info (click to toggle)
python-imageio 2.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,016 kB
  • sloc: python: 26,044; makefile: 138
file content (423 lines) | stat: -rw-r--r-- 12,438 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import gc
import shutil
from pathlib import Path
import sys
from typing import List

import imageio
import imageio as iio
import numpy as np
import pytest
from imageio.core import Format, FormatManager, Request
from pytest import raises

from conftest import deprecated_test


@pytest.fixture(scope="module", autouse=True)
@deprecated_test
def resort():
    imageio.formats.sort()
    yield
    imageio.formats.sort()


class MyFormat(Format):
    """TEST DOCS"""

    _closed: List[int] = []

    def _can_read(self, request):
        return request.filename.lower().endswith(self.extensions + (".haha",))

    def _can_write(self, request):
        return request.filename.lower().endswith(self.extensions + (".haha",))

    class Reader(Format.Reader):
        _failmode = False
        _stream_mode = False

        def _open(self):
            self._read_frames = 0

        def _close(self):
            self.format._closed.append(id(self))

        def _get_length(self):
            if self._stream_mode:
                return np.inf
            return 3

        def _get_data(self, index):
            if self._failmode == 2:
                raise IndexError()
            elif self._failmode:
                return "not an array", {}
            elif self._stream_mode and self._read_frames >= 5:
                raise IndexError()  # Mark end of stream
            else:
                self._read_frames += 1
                return np.ones((10, 10)) * index, self._get_meta_data(index)

        def _get_meta_data(self, index):
            if self._failmode:
                return "not a dict"
            return {"index": index}

    class Writer(Format.Writer):
        def _open(self):
            self._written_data = []
            self._written_meta = []
            self._meta = None

        def _close(self):
            self.format._closed.append(id(self))

        def _append_data(self, im, meta):
            self._written_data.append(im)
            self._written_meta.append(meta)

        def _set_meta_data(self, meta):
            self._meta = meta


@deprecated_test
def test_format(test_images, tmp_path):
    """Test the working of the Format class"""

    filename1 = test_images / "chelsea.png"
    filename2 = tmp_path / "chelsea.out"

    # Test basic format creation
    F = Format("testname", "test description", "foo bar spam")
    assert F.name == "TESTNAME"
    assert F.description == "test description"
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == {".foo", ".bar", ".spam"}

    # Test setting extensions
    F1 = Format("test", "", "foo bar spam")
    F2 = Format("test", "", "foo, bar,spam")
    F3 = Format("test", "", ["foo", "bar", "spam"])
    F4 = Format("test", "", ".foo .bar .spam")
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == {".foo", ".bar", ".spam"}
    # Fail
    raises(ValueError, Format, "test", "", 3)  # not valid ext
    raises(ValueError, Format, "test", "", "", 3)  # not valid mode
    raises(ValueError, Format, "test", "", "", "x")  # not valid mode

    # Test subclassing
    F = MyFormat("test", "", modes="i")
    assert "TEST DOCS" in F.doc

    # Get and check reader and write classes
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert Path(R.request.filename) == filename1
    assert Path(W.request.filename) == filename2
    # Fail
    raises(RuntimeError, F.get_reader, Request(filename1, "rI"))
    raises(RuntimeError, F.get_writer, Request(filename2, "wI"))

    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))

    # Test __del__
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)


@deprecated_test
def test_reader_and_writer(test_images, tmp_path):
    # Prepare
    filename1 = test_images / "chelsea.png"
    filename2 = tmp_path / "chelsea.out"
    F = MyFormat("test", "", modes="i")

    # Test using reader
    n = 3
    R = F.get_reader(Request(filename1, "ri"))
    assert len(R) == n
    ims = [im for im in R]
    assert len(ims) == n
    for i in range(3):
        assert ims[i][0, 0] == i
        assert ims[i].meta["index"] == i
    for i in range(3):
        assert R.get_meta_data(i)["index"] == i
    # Read next
    assert R.get_data(0)[0, 0] == 0
    assert R.get_next_data()[0, 0] == 1
    assert R.get_next_data()[0, 0] == 2
    # Fail
    R._failmode = 1
    raises(ValueError, R.get_data, 0)
    raises(ValueError, R.get_meta_data, 0)
    R._failmode = 2
    with raises(IndexError):
        [im for im in R]

    # Test streaming reader
    R = F.get_reader(Request(filename1, "ri"))
    R._stream_mode = True
    assert R.get_length() == np.inf
    ims = [im for im in R]
    assert len(ims) == 5

    # Test using writer
    im1 = np.zeros((10, 10))
    im2 = imageio.core.Image(im1, {"foo": 1})
    W = F.get_writer(Request(filename2, "wi"))
    W.append_data(im1)
    W.append_data(im2)
    W.append_data(im1, {"bar": 1})
    W.append_data(im2, {"bar": 1})
    # Test that no data is copies (but may be different views)
    assert len(W._written_data) == 4
    for im in W._written_data:
        assert (im == im1).all()
    im1[2, 2] == 99
    for im in W._written_data:
        assert (im == im1).all()
    # Test meta
    assert W._written_meta[0] == {}
    assert W._written_meta[1] == {"foo": 1}
    assert W._written_meta[2] == {"bar": 1}
    assert W._written_meta[3] == {"foo": 1, "bar": 1}
    #
    W.set_meta_data({"spam": 1})
    assert W._meta == {"spam": 1}
    # Fail
    raises(ValueError, W.append_data, "not an array")
    raises(ValueError, W.append_data, im, "not a dict")
    raises(ValueError, W.set_meta_data, "not a dict")


@deprecated_test
def test_default_can_read_and_can_write(tmp_path):
    F = imageio.plugins.example.DummyFormat("test", "", "foo bar", "v")

    # Prepare files
    filename1 = str(tmp_path / "test")
    open(filename1 + ".foo", "wb")
    open(filename1 + ".bar", "wb")
    open(filename1 + ".spam", "wb")

    # Test _can_read()
    assert F.can_read(Request(filename1 + ".foo", "rv"))
    assert F.can_read(Request(filename1 + ".bar", "r?"))
    assert not F.can_read(Request(filename1 + ".spam", "r?"))

    # Test _can_write()
    assert F.can_write(Request(filename1 + ".foo", "wv"))
    assert F.can_write(Request(filename1 + ".bar", "w?"))
    assert not F.can_write(Request(filename1 + ".spam", "w?"))


# Format manager


@deprecated_test
def test_format_manager(test_images, tmp_path):
    """Test working of the format manager"""

    formats = imageio.formats

    # Test basics of FormatManager
    assert isinstance(formats, FormatManager)
    assert len(formats) > 0
    assert "FormatManager" in repr(formats)

    # Get docs
    smalldocs = str(formats)
    # fulldocs = formats.create_docs_for_all_formats()

    # Check each format ...
    for format in formats:
        #  That each format is indeed a Format
        assert isinstance(format, Format)
        # That they are mentioned
        assert format.name in smalldocs
        # assert format.name in fulldocs

    fname = test_images / "chelsea.png"
    fname2 = tmp_path / "chelsea.noext"
    shutil.copy(fname, fname2)

    # Check getting
    F1 = formats["PNG"]
    F2 = formats[".png"]
    F3 = formats[fname2.as_posix()]  # will look in file itself
    assert type(F1) is type(F2)
    assert type(F1) is type(F3)
    # Check getting
    F1 = formats["DICOM"]
    F2 = formats[".dcm"]
    F3 = formats["dcm"]  # If omitting dot, format is smart enough to try with
    assert type(F1) is type(F2)
    assert type(F1) is type(F3)
    # Fail
    raises(ValueError, formats.__getitem__, 678)  # must be str
    raises(IndexError, formats.__getitem__, ".nonexistentformat")

    # Adding a format
    myformat = Format("test", "test description", "testext1 testext2")
    formats.add_format(myformat)
    assert type(myformat) in [type(f) for f in formats]
    assert type(formats["testext1"]) is type(myformat)
    assert type(formats["testext2"]) is type(myformat)
    # Fail
    raises(ValueError, formats.add_format, 678)  # must be Format
    raises(ValueError, formats.add_format, myformat)  # cannot add twice

    # Adding a format with the same name
    myformat2 = Format("test", "other description", "foo bar")
    raises(ValueError, formats.add_format, myformat2)  # same name
    formats.add_format(myformat2, True)  # overwrite
    assert formats["test"].name is not myformat.name
    assert type(formats["test"]) is type(myformat2)

    # Test show (we assume it shows correctly)
    formats.show()


#   # Potential
#   bytes = b'x' * 300
#   F = formats.search_read_format(Request(bytes, 'r?', dummy_potential=1))
#   assert F is formats['DUMMY']


@deprecated_test
def test_sorting_errors():
    with raises(TypeError):
        imageio.formats.sort(3)
    with raises(ValueError):
        imageio.formats.sort("foo,bar")
    with raises(ValueError):
        imageio.formats.sort("foo.png")


@deprecated_test
def test_default_order():
    assert imageio.formats[".tiff"].name == "TIFF"
    assert imageio.formats[".png"].name == "PNG-PIL"
    assert imageio.formats[".pfm"].name == "PFM-FI"


@deprecated_test
def test_preferring_fi():
    # Prefer FI all the way
    imageio.formats.sort("-FI")

    assert imageio.formats[".tiff"].name == "TIFF-FI"
    assert imageio.formats[".png"].name == "PNG-FI"
    assert imageio.formats[".pfm"].name == "PFM-FI"

    # This would be better
    imageio.formats.sort("TIFF", "-FI")
    assert imageio.formats[".tiff"].name == "TIFF"
    assert imageio.formats[".png"].name == "PNG-FI"
    assert imageio.formats[".pfm"].name == "PFM-FI"


@deprecated_test
def test_preferring_arbitrary():
    # Normally, these exotic formats are somewhere in the back
    imageio.formats.sort()
    names = [f.name for f in imageio.formats]
    assert "DICOM" not in names[:10]
    assert "FFMPEG" not in names[:10]
    assert "NPZ" not in names[:10]

    # But we can move them forward
    imageio.formats.sort("DICOM", "NPZ")
    names = [f.name for f in imageio.formats]
    assert names[0] == "DICOM"
    assert names[1] == "NPZ"

    # And back to normal ..
    imageio.formats.sort()
    names = [f.name for f in imageio.formats]
    assert "DICOM" not in names[:10]
    assert "FFMPEG" not in names[:10]
    assert "NPZ" not in names[:10]


@deprecated_test
def test_bad_formats(tmp_path):
    # test looking up a read format from file
    bogus_file = tmp_path / "bogus.fil"
    bogus_file.write_text("abcdefg")

    with pytest.raises(IndexError):
        iio.formats[str(bogus_file)]

    # test empty format
    with pytest.raises(ValueError):
        iio.formats[""]


@deprecated_test
def test_write_format_search_fail(tmp_path):
    req = iio.core.Request(tmp_path / "foo.bogus", "w")
    assert iio.formats.search_write_format(req) is None


@deprecated_test
def test_format_by_filename():
    iio.formats["test.jpg"]


@pytest.fixture()
def missing_ffmpeg():
    old_ffmpeg = sys.modules.get("imageio_ffmpeg")
    old_plugin = sys.modules.get("imageio.plugins.ffmpeg")
    sys.modules["imageio_ffmpeg"] = None
    sys.modules.pop("imageio.plugins.ffmpeg")

    yield

    sys.modules["imageio_ffmpeg"] = old_ffmpeg
    sys.modules["imageio.plugins.ffmpeg"] = old_plugin


@pytest.mark.skip
def test_missing_format(missing_ffmpeg):
    # regression test for
    # https://github.com/imageio/imageio/issues/887

    for format in imageio.formats:
        assert format.name != "FFMPEG"


def test_touch_warnings(test_images, tmp_path):
    with pytest.deprecated_call():
        imageio.formats.search_read_format(Request(test_images / "chelsea.png", "r"))

    with pytest.deprecated_call():
        imageio.formats.search_write_format(Request(tmp_path / "chelsea.png", "w"))