Skip to content

Commit

Permalink
Fix ellipsis in ACAImage and squash warning about integer arrays (#167)
Browse files Browse the repository at this point in the history
* Fix TypeError: unsupported operand type(s) for +: 'int' and 'ellipsis'
* Fix DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays
  • Loading branch information
javierggt authored Feb 2, 2024
1 parent 2d6e1ab commit 6312f8b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
8 changes: 5 additions & 3 deletions chandra_aca/aca_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def _adjust_item(self, item):
)
aca_coords = True

out_rc = [None, None] # New [row0, col0]
# These are new [row0, col0] values for the __getitem__ output. If either is left at None
# then the downstream code uses the original row0 or col0 value, respectively.
out_rc = [None, None]

if isinstance(item, (int, np.integer)):
item = (item,)
Expand All @@ -219,7 +221,7 @@ def _adjust_item(self, item):
else np.clip(it.stop - rc0, 0, shape[i])
)
item[i] = slice(start, stop, it.step)
else:
elif it is not ...:
item[i] = it - rc0
if np.any(item[i] < 0) or np.any(item[i] >= shape[i]):
raise IndexError(
Expand All @@ -234,7 +236,7 @@ def _adjust_item(self, item):
if it.start is not None:
rc_off = it.start if it.start >= 0 else shape[i] + it.start
out_rc[i] = rc0 + rc_off
else:
elif it is not ...:
it = np.array(it)
rc_off = np.where(it >= 0, it, shape[i] + it)
out_rc[i] = rc0 + rc_off
Expand Down
24 changes: 12 additions & 12 deletions chandra_aca/maude_decom.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def _aca_header_1(bits):
-------
dict
"""
bits = np.unpackbits(np.array(_unpack("BBBbbBB", bits), dtype=np.uint8))
bits = np.unpackbits(np.array(_unpack("BBBBBBB", bits), dtype=np.uint8))
return {
"IMGFID": bool(bits[0]),
"IMGNUM": _packbits(bits[1:4]),
Expand Down Expand Up @@ -428,20 +428,20 @@ def _aca_header_2(bits):
-------
dict
"""
bits = _unpack("BbbbbBB", bits)
c = np.unpackbits(np.array(bits[:2], dtype=np.uint8))
values = _unpack("BBbbbbB", bits)
c = np.unpackbits(np.array(values[:2], dtype=np.uint8))
return {
# do we want these?
# 'FID2': bool(bits[0]),
# 'IMGNUM2': _packbits(bits[1:4]),
# 'IMGFUNC2': _packbits(bits[4:6]),
# 'FID2': bool(values[0]),
# 'IMGNUM2': _packbits(values[1:4]),
# 'IMGFUNC2': _packbits(values[4:6]),
"BGDRMS": _packbits(c[6:16]),
"TEMPCCD": bits[2],
"TEMPHOUS": bits[3],
"TEMPPRIM": bits[4],
"TEMPSEC": bits[5],
"BGDSTAT": bits[6],
"BGDSTAT_PIXELS": np.unpackbits(np.array(bits[-1:], dtype=np.uint8)[-1:]),
"TEMPCCD": values[2],
"TEMPHOUS": values[3],
"TEMPPRIM": values[4],
"TEMPSEC": values[5],
"BGDSTAT": values[6],
"BGDSTAT_PIXELS": np.unpackbits(np.array(values[-1:], dtype=np.uint8)[-1:]),
}

@staticmethod
Expand Down
47 changes: 47 additions & 0 deletions chandra_aca/tests/test_aca_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,53 @@ def test_slice():
assert np.all(a2 == im80)


def test_ellipsis():
from chandra_aca import aca_image

row0 = 11
col0 = 22
inp = np.arange(64).reshape(8, 8)
img = aca_image.ACAImage(inp, row0=row0, col0=col0)

assert np.all(img[...] == inp)
assert img[...].row0 == row0
assert img[...].col0 == col0

img2 = img[-1, ...]
assert np.all(img2 == inp[-1, ...])
# assert img2.row0 == img[size - 1 , ...].row0 # Fails independently of this PR
assert img2.col0 == col0

img2 = img[1, ...]
assert np.all(img2 == inp[1, ...])
assert img2.row0 == row0 + 1
assert img2.col0 == col0

img2 = img[..., -2]
assert np.all(img2 == inp[..., -2])
assert img2.row0 == row0
# assert img2.col0 == img[..., size - 2] # fails independently of this PR

img2 = img[..., 2]
assert np.all(img[..., 2] == inp[..., 2])
assert img[..., 2].row0 == row0
assert img[..., 2].col0 == col0 + 2

img2 = img.aca[row0 + 1, ...]
assert np.all(img2 == inp[1, ...])
assert img2.row0 == row0 + 1
assert img2.col0 == col0

img2 = img.aca[..., col0 + 2]
assert np.all(img2 == inp[..., 2])
assert img2.row0 == row0
assert img2.col0 == col0 + 2

with pytest.raises(IndexError):
# an index can only have a single ellipsis
img[..., ...]


def test_slice_list():
a = ACAImage(im6, row0=1, col0=2)
r = [1, 2, 3]
Expand Down

0 comments on commit 6312f8b

Please sign in to comment.