File: test_image_compression_defaults.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 (287 lines) | stat: -rw-r--r-- 9,459 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
import os
import tempfile
import numpy as np
import fitsio


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

        img = np.ones((20, 20))
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(img)
        with fitsio.FITS(fn) as fits:
            assert len(fits) == 1


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

        img = np.ones((20, 20))
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(
                img,
                compress='RICE',
                tile_dims=(10, 5),
                qlevel=7.0,
                qmethod='SUBTRACTIVE_DITHER_2',
                dither_seed=42,
            )
        with fitsio.FITS(fn) as fits:
            assert len(fits) == 2
        hdr = fitsio.read_header(fn, ext=1)
        for key, val in [
            ('ZTILE1', 5),
            ('ZTILE2', 10),
            ('ZQUANTIZ', 'SUBTRACTIVE_DITHER_2'),
            ('ZDITHER0', 42),
            ('ZCMPTYPE', 'RICE_ONE'),
        ]:
            assert hdr[key] == val


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

        img = np.ones((20, 20))
        with fitsio.FITS(fn + '[compress G]', 'rw', clobber=True) as fits:
            fits.write(img)
        hdr = fitsio.read_header(fn, ext=1)
        for key, val in [
            ('ZTILE1', 20),
            ('ZTILE2', 1),
            ('ZQUANTIZ', 'SUBTRACTIVE_DITHER_1'),
            ('ZCMPTYPE', 'GZIP_1'),
        ]:
            assert hdr[key] == val


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

        img = np.ones((20, 20))
        with fitsio.FITS(
            fn + '[compress G 5 10; qz 8.0]', 'rw', clobber=True
        ) as fits:
            fits.write(img, dither_seed=42)
        hdr = fitsio.read_header(fn, ext=1)
        for key, val in [
            ('ZTILE1', 5),
            ('ZTILE2', 10),
            ('ZQUANTIZ', 'SUBTRACTIVE_DITHER_2'),
            ('ZCMPTYPE', 'GZIP_1'),
            ('ZDITHER0', 42),
        ]:
            assert hdr[key] == val


def test_compression_qlevels_none_zero():
    default_kws = {
        "compress": fitsio.GZIP_2,
        "tile_dims": np.array([100, 100]),
        "qmethod": fitsio.SUBTRACTIVE_DITHER_2,
    }
    with tempfile.TemporaryDirectory() as tmpdir:
        fnpat = os.path.join(tmpdir, 'test-%i.fits')

        H, W = 200, 200
        bigimg = np.random.uniform(size=(H, W))
        results = []
        # None: don't even use compression at all
        # 0: lossless gzip
        for i, qlevel in enumerate([None, 0, 16, 4, 1]):
            fn = fnpat % i
            ql = qlevel
            kw = {}
            kw.update(default_kws)
            if ql is None:
                kw.update(compress=0)
                ql = 0
            kw["qlevel"] = ql
            with fitsio.FITS(fn, 'rw', clobber=True) as fits:
                fits.write(bigimg, dither_seed=42, **kw)
            filesize = os.stat(fn).st_size
            img2 = fitsio.read(fn)
            rms = np.sqrt(np.mean((img2 - bigimg) ** 2))
            results.append((qlevel, filesize, rms))
        # No compression
        q, sz, rms = results[0]
        assert sz == 2880 * (1 + int(np.ceil(H * W * 8 / 2880.0)))
        assert rms == 0.0
        # GZIP lossless
        q, sz, rms = results[1]
        assert rms == 0.0
        # Decreasing file size
        for r1, r2 in zip(results, results[1:]):
            q1, sz1, rms1 = r1
            q2, sz2, rms2 = r2
            assert sz1 > sz2
            assert rms1 <= rms2


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

        img = np.ones((20, 20))
        with fitsio.FITS(
            fn + '[compress HS 10 10; s 2.0]', 'rw', clobber=True
        ) as fits:
            fits.write(img, dither_seed=42)
        hdr = fitsio.read_header(fn, ext=1)
        for key, val in [
            ('ZTILE1', 10),
            ('ZTILE2', 10),
            ('ZQUANTIZ', 'SUBTRACTIVE_DITHER_1'),
            ('ZCMPTYPE', 'HCOMPRESS_1'),
            ('ZDITHER0', 42),
            ('ZNAME1', 'SCALE'),
            ('ZVAL1', 2.0),
            ('ZNAME2', 'SMOOTH'),
            ('ZVAL2', 1),
        ]:
            assert hdr[key] == val


def test_compression_qlevel_default():
    # Check that if not specified, qlevel defaults to 4.
    with tempfile.TemporaryDirectory() as tmpdir:
        fn = os.path.join(tmpdir, 'test.fits')

        H, W = 200, 200
        bigimg = np.random.uniform(size=(H, W))
        # Default qlevel
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(bigimg, compress='GZIP')
        size_def = os.stat(fn).st_size
        hdr = fitsio.read_header(fn, ext=1)
        print(hdr)
        for key, val in [
            ('ZQUANTIZ', 'SUBTRACTIVE_DITHER_1'),
            ('ZCMPTYPE', 'GZIP_1'),
        ]:
            assert hdr[key] == val
        # qlevel=0
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(bigimg, compress='GZIP', qlevel=0)
        size_0 = os.stat(fn).st_size
        # qlevel=4
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(bigimg, compress='GZIP', qlevel=4)
        size_4 = os.stat(fn).st_size
        # qlevel=16
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            fits.write(bigimg, compress='GZIP', qlevel=16)
        size_16 = os.stat(fn).st_size
        # zero means NO COMPRESSION
        assert size_0 > size_4
        # heh, lower values mean MORE COMPRESSION
        assert size_4 < size_16
        assert size_def == size_4


def test_compression_multihdu_diskfile():
    # Check multi-HDU case with a normal file
    with tempfile.TemporaryDirectory() as tmpdir:
        fn = os.path.join(tmpdir, 'test.fits')

        img = np.ones((20, 20))
        with fitsio.FITS(fn, 'rw', clobber=True) as fits:
            # A
            fits.write(img, extname='A')
            # B
            fits.write(img, extname='B', compress='GZIP')
            # C
            fits.write(
                img,
                extname='C',
                compress='GZIP',
                qmethod='SUBTRACTIVE_DITHER_2',
            )
            # D
            fits.write(img, extname='D')
            # E
            fits.write(img, extname='E', compress='GZIP')
            # F
            fits.write(img, extname='F', compress=None)
        with fitsio.FITS(fn) as fits:
            assert len(fits) == 6
            hdrA = fits['A'].read_header()
            hdrB = fits['B'].read_header()
            hdrC = fits['C'].read_header()
            hdrD = fits['D'].read_header()
            hdrE = fits['E'].read_header()
            hdrF = fits['F'].read_header()
        # A is uncompressed
        assert 'ZCMPTYPE' not in hdrA
        # B is gzip
        assert hdrB['ZCMPTYPE'] == 'GZIP_1'
        assert hdrB['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_1'
        # C is gzip with SD2
        assert hdrC['ZCMPTYPE'] == 'GZIP_1'
        assert hdrC['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_2'
        # D is not compressed
        assert 'ZCMPTYPE' not in hdrD
        # E is GZIP again
        assert hdrE['ZCMPTYPE'] == 'GZIP_1'
        assert hdrE['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_1'
        # F is not compressed
        assert 'ZCMPTYPE' not in hdrF


def test_compression_multihdu_memfile():
    # Check multi-HDU case with a normal file
    with tempfile.TemporaryDirectory() as tmpdir:
        fn = os.path.join(tmpdir, 'test.fits')

        img = np.ones((20, 20))
        with fitsio.FITS("mem://", 'rw', clobber=True) as fits:
            # A
            fits.write(img, extname='A')
            # B
            fits.write(img, extname='B', compress='GZIP')
            # C
            fits.write(
                img,
                extname='C',
                compress='GZIP',
                qmethod='SUBTRACTIVE_DITHER_2',
            )
            # D
            fits.write(img, extname='D')
            # E
            fits.write(img, extname='E', compress='GZIP')
            # F
            fits.write(img, extname='F', compress=None)

            data = fits.read_raw()
            with open(fn, 'wb') as f:
                f.write(data)

        with fitsio.FITS(fn) as fits:
            assert len(fits) == 6
            hdrA = fits['A'].read_header()
            hdrB = fits['B'].read_header()
            hdrC = fits['C'].read_header()
            hdrD = fits['D'].read_header()
            hdrE = fits['E'].read_header()
            hdrF = fits['F'].read_header()
        # A is uncompressed
        assert 'ZCMPTYPE' not in hdrA
        # B is gzip
        assert hdrB['ZCMPTYPE'] == 'GZIP_1'
        assert hdrB['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_1'
        # C is gzip with SD2
        assert hdrC['ZCMPTYPE'] == 'GZIP_1'
        assert hdrC['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_2'
        # D is not compressed
        assert 'ZCMPTYPE' not in hdrD
        # E is GZIP again
        assert hdrE['ZCMPTYPE'] == 'GZIP_1'
        assert hdrE['ZQUANTIZ'] == 'SUBTRACTIVE_DITHER_1'
        # F is not compressed
        assert 'ZCMPTYPE' not in hdrF