Skip to content

Commit

Permalink
Merge pull request #397 from esheldon/empty-slice
Browse files Browse the repository at this point in the history
FIX empty slice case
  • Loading branch information
esheldon authored Jun 11, 2024
2 parents bf983bb + 5899be0 commit ef68463
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
-------------

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.2'
__version__ = '1.2.3'

from . import fitslib

Expand Down
6 changes: 5 additions & 1 deletion fitsio/hdu/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions fitsio/tests/test_empty_slice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)

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

with FITS(fname) as fits:
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
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.2",
version="1.2.3",
description=description,
long_description=long_description,
long_description_content_type='text/markdown; charset=UTF-8; variant=GFM',
Expand Down

0 comments on commit ef68463

Please sign in to comment.