From d621316d6b1fe40b08c5036bc8f99263a62bdcd1 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Tue, 11 Jun 2024 10:35:58 -0400 Subject: [PATCH 1/4] FIX empty slice case --- fitsio/hdu/image.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fitsio/hdu/image.py b/fitsio/hdu/image.py index a2d16c8d..d8b2965b 100644 --- a/fitsio/hdu/image.py +++ b/fitsio/hdu/image.py @@ -278,8 +278,11 @@ def _read_image_slice(self, arg): step = -1 # Sanity checks for proper syntax. - if (step > 0 and stop < start) or (step < 0 and start < stop): + if ((step > 0 and stop < start) + or (step < 0 and start < stop) + or (start == stop)): return numpy.empty(0, dtype=npy_dtype) + if start < 0: start = dims[dim] + start if start < 0: @@ -293,6 +296,7 @@ def _read_image_slice(self, arg): if stop > dims[dim]: stop = dims[dim] + if stop < start: # A little black magic here. The stop is offset by 2 to # accommodate the 1-offset of CFITSIO, and to move past the end From 8ccb4db6646cfbb1690c4e418e54ba25dfa68be8 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Tue, 11 Jun 2024 10:36:59 -0400 Subject: [PATCH 2/4] add test case fitsio/tests/test_empty_slice.py --- fitsio/tests/test_empty_slice.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 fitsio/tests/test_empty_slice.py diff --git a/fitsio/tests/test_empty_slice.py b/fitsio/tests/test_empty_slice.py new file mode 100644 index 00000000..32c780b1 --- /dev/null +++ b/fitsio/tests/test_empty_slice.py @@ -0,0 +1,20 @@ +import tempfile +import os +import numpy as np +from ..fitslib import write, FITS + + +def test_empty_image_slice(): + shape = (10, 10) + data = np.arange(shape[0] * shape[1]).reshape(shape) + outfile = 'test_image.fits' + with tempfile.TemporaryDirectory() as tmpdir: + fname = os.path.join(tmpdir, 'test.fits') + write(fname, data, clobber=True) + + # third, test slices using fitsio + with FITS(outfile) as fits: + # first, passing the slices directly + # overlap = f[0][xslice, yslice] + overlap = fits[0][0:8, 0:0] + assert overlap.size == 0 From e05f3b6a686f862eb493544428271db4bd1b6e14 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Tue, 11 Jun 2024 11:47:21 -0400 Subject: [PATCH 3/4] BUG wrong image file path --- fitsio/tests/test_empty_slice.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fitsio/tests/test_empty_slice.py b/fitsio/tests/test_empty_slice.py index 32c780b1..bcb20a57 100644 --- a/fitsio/tests/test_empty_slice.py +++ b/fitsio/tests/test_empty_slice.py @@ -7,13 +7,12 @@ def test_empty_image_slice(): shape = (10, 10) data = np.arange(shape[0] * shape[1]).reshape(shape) - outfile = 'test_image.fits' with tempfile.TemporaryDirectory() as tmpdir: fname = os.path.join(tmpdir, 'test.fits') write(fname, data, clobber=True) # third, test slices using fitsio - with FITS(outfile) as fits: + with FITS(fname) as fits: # first, passing the slices directly # overlap = f[0][xslice, yslice] overlap = fits[0][0:8, 0:0] From 5899be0b4eec20147adca709150207b4613fa52e Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Tue, 11 Jun 2024 11:55:24 -0400 Subject: [PATCH 4/4] more empty slice tests, update change log --- CHANGES.md | 7 +++++++ fitsio/__init__.py | 2 +- fitsio/tests/test_empty_slice.py | 12 +++++++----- setup.py | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 97a59efb..902c14c3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +version 1.2.3 (not yet released) +------------- + +Bug Fixes + + - Reading images with empty slices was returning data + version 1.2.2 ------------- diff --git a/fitsio/__init__.py b/fitsio/__init__.py index 956510d0..6cb6eef1 100644 --- a/fitsio/__init__.py +++ b/fitsio/__init__.py @@ -5,7 +5,7 @@ usage. """ -__version__ = '1.2.2' +__version__ = '1.2.3' from . import fitslib diff --git a/fitsio/tests/test_empty_slice.py b/fitsio/tests/test_empty_slice.py index bcb20a57..a39355bf 100644 --- a/fitsio/tests/test_empty_slice.py +++ b/fitsio/tests/test_empty_slice.py @@ -5,15 +5,17 @@ def test_empty_image_slice(): + shape = (10, 10) data = np.arange(shape[0] * shape[1]).reshape(shape) + with tempfile.TemporaryDirectory() as tmpdir: fname = os.path.join(tmpdir, 'test.fits') write(fname, data, clobber=True) - # third, test slices using fitsio with FITS(fname) as fits: - # first, passing the slices directly - # overlap = f[0][xslice, yslice] - overlap = fits[0][0:8, 0:0] - assert overlap.size == 0 + assert fits[0][0:0, 0:0].size == 0 + + assert fits[0][0:8, 0:0].size == 0 + + assert fits[0][0:0, 0:8].size == 0 diff --git a/setup.py b/setup.py index e1cce1ba..d6282856 100644 --- a/setup.py +++ b/setup.py @@ -318,7 +318,7 @@ def check_system_cfitsio_objects(self, obj_name): setup( name="fitsio", - version="1.2.2", + version="1.2.3", description=description, long_description=long_description, long_description_content_type='text/markdown; charset=UTF-8; variant=GFM',