From 3e05ca44888a1dff88c0b9fb095f3ac5201af767 Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Mon, 16 Dec 2019 12:26:42 -0500 Subject: [PATCH] Fix crashing bug when slicing NDCube. If an axis after the first axis was sliced with a non-native int type, e.g. numpy.int64, the slixing would crash due to missing_axes not being altered. This commit adds support for nupy.int64 and numpy.int32 types. --- changelog/223.bugfix.rst | 1 + ndcube/utils/wcs.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 changelog/223.bugfix.rst diff --git a/changelog/223.bugfix.rst b/changelog/223.bugfix.rst new file mode 100644 index 000000000..ceb40318e --- /dev/null +++ b/changelog/223.bugfix.rst @@ -0,0 +1 @@ +Fix crashing bug when an NDCube axis after the first is sliced with a numpy.int64. diff --git a/ndcube/utils/wcs.py b/ndcube/utils/wcs.py index 981b8ca0c..9ea9ebf2a 100644 --- a/ndcube/utils/wcs.py +++ b/ndcube/utils/wcs.py @@ -7,6 +7,7 @@ import re from copy import deepcopy from collections import UserDict +import numbers import numpy as np from astropy import wcs @@ -163,7 +164,7 @@ def _wcs_slicer(wcs, missing_axes, item): item_checked.append(slice(0, 1)) new_wcs = wcs.slice(item_checked) # item is int then slicing axis. - elif isinstance(item, int) or isinstance(item, np.int64): + elif isinstance(item, numbers.Integral): # using index to keep track of whether the int(which is converted to # slice(int_value, int_value+1)) is already added or not. It checks # the dead axis i.e missing_axes to check if it is dead than slice(0,1) @@ -207,7 +208,10 @@ def _wcs_slicer(wcs, missing_axes, item): item_ = _slice_list(item_checked) new_wcs = wcs.slice(item_) for i, it in enumerate(item_checked): - if isinstance(it, int): + # If an axis is sliced out, i.e. it's item is an int, + # set missing axis to True. + # numbers.Integral captures all int types, int, np.int64, etc. + if isinstance(it, numbers.Integral): missing_axes[i] = True else: raise NotImplementedError("Slicing FITS-WCS by {} not supported.".format(type(item)))