File: test_image_compression.py

package info (click to toggle)
python-fitsio 1.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,020 kB
  • sloc: python: 7,963; ansic: 3,962; makefile: 10
file content (670 lines) | stat: -rw-r--r-- 18,263 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import pytest
import sys
import os
import tempfile
from .checks import (
    # check_header,
    compare_array,
)
import numpy as np
from ..fitslib import (
    FITS,
    read,
    write,
    RICE_1,
    SUBTRACTIVE_DITHER_1,
    GZIP_1,
    GZIP_2,
    PLIO_1,
    HCOMPRESS_1,
)
from ..util import cfitsio_is_bundled, cfitsio_version

CFITSIO_VERSION = cfitsio_version(asfloat=True)


@pytest.mark.parametrize("with_nan", [False, True])
@pytest.mark.parametrize(
    'compress',
    [
        'rice',
        'hcompress',
        'plio',
        'gzip',
        'gzip_2',
        'gzip_lossless',
        'gzip_2_lossless',
    ],
)
@pytest.mark.parametrize(
    'dtype', ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'f4', 'f8']
)
def test_compressed_write_read(compress, dtype, with_nan):
    """
    Test writing and reading a rice compressed image
    """
    nrows = 5
    ncols = 20
    if compress in ['rice', 'hcompress'] or 'gzip' in compress:
        pass
    elif compress == 'plio':
        if dtype not in ['i1', 'i2', 'i4', 'f4', 'f8']:
            return
    else:
        raise ValueError('unexpected compress %s' % compress)

    if 'lossless' in compress:
        qlevel = None
    else:
        qlevel = 16

    seed = 1919
    rng = np.random.RandomState(seed)

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        if dtype[0] == 'f':
            data = rng.normal(size=(nrows, ncols))
            if compress == 'plio':
                data = data.clip(min=0)
            data = data.astype(dtype)
        else:
            data = np.arange(
                nrows * ncols,
                dtype=dtype,
            ).reshape(nrows, ncols)

        if "f" in dtype and with_nan and compress != "plio":
            data[3, 11] = np.nan

        csend = compress.replace('_lossless', '')
        write(fname, data, compress=csend, qlevel=qlevel)
        rdata = read(fname, ext=1)

        if 'lossless' in compress or dtype[0] in ['i', 'u']:
            np.testing.assert_array_equal(
                data,
                rdata,
                err_msg="%s compressed images ('%s')" % (compress, dtype),
            )
        else:
            # lossy floating point
            np.testing.assert_allclose(
                data,
                rdata,
                rtol=0,
                atol=0.2,
                err_msg="%s compressed images ('%s')" % (compress, dtype),
            )

        with FITS(fname) as fits:
            assert fits[1].is_compressed(), "is compressed"


@pytest.mark.parametrize("with_nan", [False, True])
@pytest.mark.parametrize(
    'compress',
    [
        'rice',
        'hcompress',
        'plio',
        'gzip',
        'gzip_2',
        'gzip_lossless',
        'gzip_2_lossless',
    ],
)
@pytest.mark.parametrize(
    'dtype', ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'f4', 'f8']
)
def test_compressed_write_read_fitsobj(compress, dtype, with_nan):
    """
    Test writing and reading a rice compressed image

    In this version, keep the fits object open
    """

    if (
        "gzip" in compress
        and dtype in ["u2", "i2", "u4", "i4"]
        and not cfitsio_is_bundled()
    ):
        pytest.xfail(
            reason=(
                "Non-bundled cfitsio libraries have a bug. "
                "See https://github.com/HEASARC/cfitsio/pull/97."
            )
        )

    nrows = 5
    ncols = 20
    if compress in ['rice', 'hcompress'] or 'gzip' in compress:
        pass
    elif compress == 'plio':
        if dtype not in ['i1', 'i2', 'i4', 'f4', 'f8']:
            return
    else:
        raise ValueError('unexpected compress %s' % compress)

    if 'lossless' in compress:
        qlevel = None
        # qlevel = 9999
    else:
        qlevel = 16

    seed = 1919
    rng = np.random.RandomState(seed)

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            # note i8 not supported for compressed!

            if dtype[0] == 'f':
                data = rng.normal(size=(nrows, ncols))
                if compress == 'plio':
                    data = data.clip(min=0)
                data = data.astype(dtype)
            else:
                data = np.arange(
                    nrows * ncols,
                    dtype=dtype,
                ).reshape(nrows, ncols)

            if "f" in dtype and with_nan and compress != "plio":
                data[3, 11] = np.nan

            csend = compress.replace('_lossless', '')
            fits.write_image(data, compress=csend, qlevel=qlevel)
            rdata = fits[-1].read()

            if 'lossless' in compress or dtype[0] in ['i', 'u']:
                # for integers we have chosen a wide range of values, so
                # there will be no quantization and we expect no
                # information loss
                np.testing.assert_array_equal(
                    data,
                    rdata,
                    "%s compressed images ('%s')" % (compress, dtype),
                )
            else:
                # lossy floating point
                np.testing.assert_allclose(
                    data,
                    rdata,
                    rtol=0,
                    atol=0.2,
                    err_msg="%s compressed images ('%s')" % (compress, dtype),
                )

        with FITS(fname) as fits:
            assert fits[1].is_compressed(), "is compressed"


@pytest.mark.skipif(sys.version_info < (3, 9), reason='importlib bug in 3.8')
@pytest.mark.skipif(CFITSIO_VERSION < 3.49, reason='bug in cfitsio < 3.49')
def test_gzip_tile_compressed_read_lossless_astropy():
    """
    Test reading an image gzip compressed by astropy (fixed by cfitsio 3.49)
    """
    import importlib.resources

    ref = (
        importlib.resources.files("fitsio")
        / 'test_images'
        / 'test_gzip_compressed_image.fits.fz'
    )  # noqa
    with importlib.resources.as_file(ref) as gzip_file:
        data = read(gzip_file)

    compare_array(data, data * 0.0, "astropy lossless compressed image")


@pytest.mark.parametrize("with_nan", [False, True])
def test_compress_preserve_zeros(with_nan):
    """
    Test writing and reading gzip compressed image
    """

    zinds = [
        (1, 3),
        (2, 9),
    ]

    dtypes = ['f4', 'f8']

    seed = 2020
    rng = np.random.RandomState(seed)

    # Do not test hcompress as it doesn't support SUBTRACTIVE_DITHER_2
    for compress in ['gzip', 'gzip_2', 'rice']:
        with tempfile.TemporaryDirectory() as tmpdir:
            fname = os.path.join(tmpdir, 'test.fits')

            with FITS(fname, 'rw') as fits:
                for dtype in dtypes:
                    data = rng.normal(size=5 * 20).reshape(5, 20).astype(dtype)
                    for zind in zinds:
                        data[zind[0], zind[1]] = 0.0
                    if with_nan:
                        data[3, 15] = np.nan

                    fits.write_image(
                        data,
                        compress=compress,
                        qlevel=16,
                        qmethod='SUBTRACTIVE_DITHER_2',
                    )
                    rdata = fits[-1].read()

                    for zind in zinds:
                        assert rdata[zind[0], zind[1]] == 0.0
                    if with_nan:
                        assert np.isnan(rdata[3, 15])


@pytest.mark.parametrize("with_nan", [False, True])
@pytest.mark.parametrize(
    'compress',
    [
        'rice',
        'hcompress',
        'plio',
    ],
)
@pytest.mark.parametrize(
    'seed_type',
    ['matched', 'unmatched', 'checksum', 'checksum_int'],
)
@pytest.mark.parametrize(
    'use_fits_object',
    [False, True],
)
@pytest.mark.parametrize(
    'dtype',
    ['f4', 'f8'],
)
def test_compressed_seed(
    compress, seed_type, use_fits_object, dtype, with_nan
):
    """
    Test writing and reading a rice compressed image
    """
    nrows = 5
    ncols = 20

    qlevel = 16

    seed = 1919
    rng = np.random.RandomState(seed)

    if seed_type == 'matched':
        # dither_seed = 9881
        dither_seed1 = 9881
        dither_seed2 = 9881
    elif seed_type == 'unmatched':
        # dither_seed = None
        dither_seed1 = 3
        dither_seed2 = 4
    elif seed_type == 'checksum':
        dither_seed1 = 'checksum'
        dither_seed2 = b'checksum'
    elif seed_type == 'checksum_int':
        dither_seed1 = -1
        # any negative means use checksum
        dither_seed2 = -3

    with tempfile.TemporaryDirectory() as tmpdir:
        fname1 = os.path.join(tmpdir, 'test1.fits')
        fname2 = os.path.join(tmpdir, 'test2.fits')

        data = rng.normal(size=(nrows, ncols))
        if compress == 'plio':
            data = data.clip(min=0)
        data = data.astype(dtype)

        if "f" in dtype and with_nan and compress != "plio":
            data[3, 11] = np.nan

        if use_fits_object:
            with FITS(fname1, 'rw') as fits1:
                fits1.write(
                    data,
                    compress=compress,
                    qlevel=qlevel,
                    # dither_seed=dither_seed,
                    dither_seed=dither_seed1,
                )
                rdata1 = fits1[-1].read()

            with FITS(fname2, 'rw') as fits2:
                fits2.write(
                    data,
                    compress=compress,
                    qlevel=qlevel,
                    # dither_seed=dither_seed,
                    dither_seed=dither_seed2,
                )
                rdata2 = fits2[-1].read()
        else:
            write(
                fname1,
                data,
                compress=compress,
                qlevel=qlevel,
                # dither_seed=dither_seed,
                dither_seed=dither_seed1,
            )
            rdata1 = read(fname1)

            write(
                fname2,
                data,
                compress=compress,
                qlevel=qlevel,
                # dither_seed=dither_seed,
                dither_seed=dither_seed2,
            )
            rdata2 = read(fname2)

        mess = "%s compressed images ('%s')" % (compress, dtype)

        if seed_type in ['checksum', 'checksum_int', 'matched']:
            np.testing.assert_array_equal(rdata1, rdata2, mess)
        else:
            with pytest.raises(AssertionError):
                np.testing.assert_array_equal(rdata1, rdata2, mess)

        if "f" in dtype and with_nan and compress != "plio":
            assert np.isnan(rdata1[3, 11])
            assert np.isnan(rdata2[3, 11])
        else:
            assert np.all(np.isfinite(rdata1))
            assert np.all(np.isfinite(rdata2))


@pytest.mark.parametrize(
    'dither_seed',
    ['blah', 10_001],
)
def test_compressed_seed_bad(dither_seed):
    """
    Test writing and reading a rice compressed image
    """
    compress = 'rice'
    dtype = 'f4'
    nrows = 5
    ncols = 20

    qlevel = 16

    seed = 1919
    rng = np.random.RandomState(seed)

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        data = rng.normal(size=(nrows, ncols))
        data = data.astype(dtype)

        with pytest.raises(ValueError):
            write(
                fname,
                data,
                compress=compress,
                qlevel=qlevel,
                dither_seed=dither_seed,
            )


def test_memory_compressed_seed():
    import fitsio

    dtype = 'f4'
    nrows = 300
    ncols = 500

    seed = 1919
    rng = np.random.RandomState(seed)

    with tempfile.TemporaryDirectory() as tmpdir:
        fname1 = os.path.join(tmpdir, 'test1.fits')
        fname2 = os.path.join(tmpdir, 'test2.fits')

        data = rng.normal(size=(nrows, ncols))
        data = data.astype(dtype)

        fitsio.write(
            fname1,
            data.copy(),
            dither_seed='checksum',
            compress='RICE',
            qlevel=1e-4,
            tile_dims=(100, 100),
            clobber=True,
        )
        hdr = fitsio.read_header(fname1, ext=1)
        dither1 = hdr['ZDITHER0']
        assert dither1 == 8269

        fits = fitsio.FITS('mem://[compress R 100,100; qz -1e-4]', 'rw')
        fits.write(data.copy(), dither_seed='checksum')
        data = fits.read_raw()
        fits.close()
        f = open(fname2, 'wb')
        f.write(data)
        f.close()
        hdr = fitsio.read_header(fname2, ext=1)
        dither2 = hdr['ZDITHER0']
        assert dither1 == dither2


def test_image_compression_inmem_subdither2():
    H, W = 100, 100
    rng = np.random.RandomState(seed=10)
    img = rng.normal(size=(H, W))
    img[40:50, :] = 0.0
    with FITS('mem://[compress G 100,100; qz 0]', 'rw') as F:
        F.write(img)
        rawdata = F.read_raw()

    with tempfile.TemporaryDirectory() as tmpdir:
        pth = os.path.join(tmpdir, 'out.fits')
        with open(pth, 'wb') as f:
            f.write(rawdata)
        im2 = read(pth)
        z = im2[40:50, :]

    minval = z.min()
    assert minval == 0


@pytest.mark.parametrize(
    "kw,val",
    [
        ("compress", RICE_1),
        ("tile_dims", (10, 2)),
        ("tile_dims", np.array([10, 2])),
        ("tile_dims", [10, 2]),
        ("qlevel", 10.0),
        ("qmethod", SUBTRACTIVE_DITHER_1),
        ("hcomp_scale", 10.0),
        ("hcomp_smooth", True),
    ],
)
@pytest.mark.parametrize("set_val_to_none", [False, True])
def test_image_compression_raises_on_python_set(kw, val, set_val_to_none):
    H, W = 100, 100
    rng = np.random.RandomState(seed=10)
    img = rng.normal(size=(H, W))
    if set_val_to_none:
        kws = {kw: None}
    else:
        kws = {kw: val}

    with FITS('mem://[compress G 100,100; qz 0]', 'rw') as F:
        with pytest.raises(ValueError):
            F.write(img, **kws)

    with FITS('mem://[compress G 100,100; qz 4.0]', 'rw') as F:
        F.write(img, dither_seed=10)


@pytest.mark.parametrize(
    "compress",
    [
        RICE_1,
        GZIP_1,
        GZIP_2,
        PLIO_1,
        HCOMPRESS_1,
    ],
)
@pytest.mark.parametrize(
    "dtype",
    [
        np.uint8,
        np.int8,
        np.uint16,
        np.int16,
        np.uint32,
        np.int32,
    ],
)
@pytest.mark.parametrize("fname", ["mem://", "test.fits"])
def test_image_compression_inmem_lossess_int(compress, dtype, fname):
    if not cfitsio_is_bundled():
        pytest.xfail(
            reason=(
                "Non-bundled cfitsio libraries have a bug. "
                "See https://github.com/HEASARC/cfitsio/pull/97 "
                "and https://github.com/HEASARC/cfitsio/pull/99."
            ),
        )
    if compress == PLIO_1 and dtype in [np.int16, np.uint32, np.int32]:
        pytest.skip(
            reason="PLIO lossless compression of int16, uint32, and "
            "int32 types is not supported by cfitsio",
        )
    rng = np.random.RandomState(seed=10)
    img = rng.normal(size=(300, 300))
    if dtype in [
        np.uint8,
        np.uint16,
        np.uint32,
    ]:
        img = np.abs(img)
    img = img.astype(dtype)
    with tempfile.TemporaryDirectory() as tmpdir:
        if "mem://" not in fname:
            fpth = os.path.join(tmpdir, fname)
        else:
            fpth = fname

        with FITS(fpth, 'rw') as F:
            F.write(img, compress=compress, qlevel=0)
            rimg = F[-1].read()
            assert rimg is not None
            assert np.array_equal(rimg, img)


def test_image_compression_inmem_lossessgzip_int_zeros():
    img = np.zeros((300, 300)).astype(np.int32)
    with FITS('mem://', 'rw') as F:
        F.write(img, compress='GZIP', qlevel=0)
        rimg = F[-1].read()
        assert rimg is not None
        assert np.array_equal(rimg, img)


def test_image_compression_inmem_lossessgzip_float():
    rng = np.random.RandomState(seed=10)
    img = rng.normal(size=(300, 300))
    with FITS('mem://', 'rw') as F:
        F.write(img, compress='GZIP', qlevel=0)
        rimg = F[-1].read()
        assert rimg is not None
        assert np.array_equal(rimg, img)


def test_image_mem_reopen_noop():
    rng = np.random.RandomState(seed=10)
    img = rng.normal(size=(300, 300))
    with FITS('mem://', 'rw') as F:
        F.write(img)
        rimg = F[-1].read()
        assert rimg is not None
        assert np.array_equal(rimg, img)
        F.reopen()
        rimg = F[-1].read()
        assert rimg is not None
        assert np.array_equal(rimg, img)

    with FITS('mem://', 'rw') as F:
        F.write(img)
        F.reopen()
        rimg = F[-1].read()
        assert rimg is not None
        assert np.array_equal(rimg, img)


@pytest.mark.parametrize("nan_value", [np.nan, np.inf, -np.inf])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize(
    "fname",
    [
        "test.fits",
        "mem://",
    ],
)
def test_image_compression_nulls(fname, dtype, nan_value):
    data = np.arange(36).reshape((6, 6)).astype(dtype)
    data[1, 1] = nan_value

    # everything comes back as nan
    if nan_value is not np.nan:
        msk = ~np.isfinite(data)
        cdata = data.copy()
        cdata[msk] = np.nan
    else:
        cdata = data

    with tempfile.TemporaryDirectory() as tmpdir:
        if "mem://" not in fname:
            fpth = os.path.join(tmpdir, fname)
        else:
            fpth = fname

        with FITS(fpth, "rw") as fits:
            fits.write(
                data,
                compress='RICE_1',
                tile_dims=(3, 3),
                dither_seed=10,
                qlevel=2,
            )
            read_data = fits[1].read()

            np.testing.assert_allclose(
                read_data,
                cdata,
            )

        if "mem://" not in fpth:
            with FITS(fpth, "r") as fits:
                read_data = fits[1].read()
                np.testing.assert_allclose(
                    read_data,
                    cdata,
                )


if __name__ == '__main__':
    test_compressed_seed(
        compress='rice',
        match_seed=False,
        use_fits_object=True,
        dtype='f4',
    )