Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug not writing qlevel #386

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Changes
errors in a FITS file. Currently this only raises when extensions
are not properly marked with XTENSION but we can expand this over time

Bug Fixes

- Bug not writing compression qlevel when it is set to None/0.0
This was preventing lossless gzip compression

version 1.2.0
--------------
Expand Down
2 changes: 1 addition & 1 deletion fitsio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
usage.
"""

__version__ = '1.2.0'
__version__ = '1.2.1'

from . import fitslib

Expand Down
6 changes: 2 additions & 4 deletions fitsio/fitsio_pywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1485,10 +1485,8 @@ PyFITSObject_create_image_hdu(struct PyFITSObject* self, PyObject* args, PyObjec
goto create_image_hdu_cleanup;
}

if (qlevel > 0) {
if (fits_set_quantize_level(self->fits, qlevel, &status)) {
goto create_image_hdu_cleanup;
}
if (fits_set_quantize_level(self->fits, qlevel, &status)) {
goto create_image_hdu_cleanup;
}

if (fits_set_quantize_method(self->fits, qmethod, &status)) {
Expand Down
100 changes: 92 additions & 8 deletions fitsio/tests/test_image_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..fitslib import (
FITS,
read,
write,
)


Expand All @@ -32,6 +33,80 @@ def test_compressed_write_read(compress):
"""
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

This one fails because the compressed data do not seem to be
finalized
"""
nrows = 5
ncols = 20
if compress in ['rice', 'hcompress'] or 'gzip' in compress:
dtypes = ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'f4', 'f8']
# dtypes = ['u2']
Expand Down Expand Up @@ -69,21 +144,30 @@ def test_compressed_write_read(compress):
fits.write_image(data, compress=csend, qlevel=qlevel)
rdata = fits[-1].read()

if dtype[0] == 'f':
compare_array_abstol(
data,
rdata,
0.2,
"%s compressed images ('%s')" % (compress, dtype),
)
else:
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),
)
# else:
# # 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)
# )

with FITS(fname) as fits:
for ii in range(len(dtypes)):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def check_system_cfitsio_objects(self, obj_name):

setup(
name="fitsio",
version="1.2.0",
version="1.2.1",
description=description,
long_description=long_description,
long_description_content_type='text/markdown; charset=UTF-8; variant=GFM',
Expand Down
Loading