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
|
import pytest
import sys
import os
import tempfile
from .checks import (
# check_header,
compare_array,
compare_array_abstol,
)
import numpy as np
from ..fitslib import (
FITS,
read,
write,
)
@pytest.mark.parametrize(
'compress',
[
'rice',
'hcompress',
'plio',
'gzip',
'gzip_2',
'gzip_lossless',
'gzip_2_lossless',
]
)
def test_compressed_write_read(compress):
"""
Test writing and reading a rice compressed image
"""
nrows = 5
ncols = 20
if compress in ['rice', 'hcompress'] or 'gzip' in compress:
dtypes = ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'f4', 'f8']
elif compress == 'plio':
dtypes = ['i1', 'i2', 'i4', 'f4', 'f8']
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')
for ext, dtype in enumerate(dtypes):
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)
csend = compress.replace('_lossless', '')
write(fname, data, compress=csend, qlevel=qlevel)
rdata = read(fname, ext=ext+1)
if 'lossless' in compress or dtype[0] in ['i', 'u']:
compare_array(
data, rdata,
"%s compressed images ('%s')" % (compress, dtype)
)
else:
# lossy floating point
compare_array_abstol(
data,
rdata,
0.2,
"%s compressed images ('%s')" % (compress, dtype),
)
with FITS(fname) as fits:
for ii in range(len(dtypes)):
i = ii + 1
assert fits[i].is_compressed(), "is compressed"
@pytest.mark.parametrize(
'compress',
[
'rice',
'hcompress',
'plio',
'gzip',
'gzip_2',
'gzip_lossless',
'gzip_2_lossless',
]
)
def test_compressed_write_read_fitsobj(compress):
"""
Test writing and reading a rice compressed image
In this version, keep the fits object open
"""
nrows = 5
ncols = 20
if compress in ['rice', 'hcompress'] or 'gzip' in compress:
dtypes = ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'f4', 'f8']
# dtypes = ['u2']
elif compress == 'plio':
dtypes = ['i1', 'i2', 'i4', 'f4', 'f8']
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!
for dtype in dtypes:
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)
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
compare_array(
data, rdata,
"%s compressed images ('%s')" % (compress, dtype)
)
else:
# lossy floating point
compare_array_abstol(
data,
rdata,
0.2,
"%s compressed images ('%s')" % (compress, dtype),
)
with FITS(fname) as fits:
for ii in range(len(dtypes)):
i = ii + 1
assert fits[i].is_compressed(), "is compressed"
@pytest.mark.skipif(sys.version_info < (3, 9),
reason='importlib bug in 3.8')
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")
def test_compress_preserve_zeros():
"""
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
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
@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):
"""
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 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']:
assert np.all(rdata1 == rdata2), mess
else:
assert np.all(rdata1 != rdata2), mess
@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,
)
if __name__ == '__main__':
test_compressed_seed(
compress='rice',
match_seed=False,
use_fits_object=True,
dtype='f4',
)
|