Repository: gammapy/gammapy

Issue:
Catalog getitem fails with Numpy int index
I just tried to get a catalog source by int index, and my int was a numpy int.

This failed with a ValueError due to this check:
https://github.com/gammapy/gammapy/blob/c77fe799202acdc880db717ffd85106c1d46f326/gammapy/catalog/core.py#L169

To check that this doesn't work as expected:
```python
>>> import six
>>> import numpy as np
>>> isinstance(np.int,  six.integer_types)
False
```

I guess we should find some better incantation to check for int via Google.
(and add a regression test, showing that it works for numpy int)


Discussion:

Resolving patch:
diff --git a/gammapy/catalog/core.py b/gammapy/catalog/core.py
--- a/gammapy/catalog/core.py
+++ b/gammapy/catalog/core.py
@@ -2,6 +2,7 @@
 """Source catalog and object base classes.
 """
 from __future__ import absolute_import, division, print_function, unicode_literals
+from ..utils.array import _is_int
 from collections import OrderedDict
 import sys
 from pprint import pprint
@@ -166,7 +167,7 @@ def __getitem__(self, key):
         """
         if isinstance(key, six.string_types):
             index = self.row_index(key)
-        elif isinstance(key, six.integer_types):
+        elif _is_int(key):
             index = key
         else:
             msg = 'Key must be source name string or row index integer. '
@@ -234,4 +235,4 @@ def __str__(self):
         """
         ss = self.description
         ss += ' with {} objects.'.format(len(self.table))
-        return ss
\ No newline at end of file
+        return ss
diff --git a/gammapy/utils/array.py b/gammapy/utils/array.py
--- a/gammapy/utils/array.py
+++ b/gammapy/utils/array.py
@@ -2,6 +2,7 @@
 """Utility functions to deal with arrays and quantities.
 """
 from __future__ import absolute_import, division, print_function, unicode_literals
+from astropy.extern import six
 import numpy as np
 
 __all__ = ['array_stats_str',
@@ -9,6 +10,7 @@
            'shape_divisible_by',
            'symmetric_crop_pad_width']
 
+all_integer_types = six.integer_types + (np.integer,)
 
 def array_stats_str(x, label=''):
     """Make a string summarising some stats for an array.
@@ -105,4 +107,8 @@ def symmetric_crop_pad_width(shape, new_shape):
 
     ywidth = (ydiff // 2, ydiff // 2)
     xwidth = (xdiff // 2, xdiff // 2)
-    return (ywidth, xwidth)
\ No newline at end of file
+    return (ywidth, xwidth)
+
+
+def _is_int(val):
+    return isinstance(val, all_integer_types)


Test patch:
diff --git a/gammapy/catalog/tests/test_core.py b/gammapy/catalog/tests/test_core.py
--- a/gammapy/catalog/tests/test_core.py
+++ b/gammapy/catalog/tests/test_core.py
@@ -1,6 +1,7 @@
 # Licensed under a 3-clause BSD style license - see LICENSE.rst
 from __future__ import absolute_import, division, print_function, unicode_literals
 from collections import OrderedDict
+import numpy as np
 from numpy.testing import assert_allclose
 from astropy.tests.helper import pytest, assert_quantity_allclose
 from astropy.table import Table, Column
@@ -53,6 +54,9 @@ def test_getitem(self):
         source = self.cat[0]
         assert source.data['Source_Name'] == 'a'
 
+        source = self.cat[np.int(0)]
+        assert source.data['Source_Name'] == 'a'
+
         with pytest.raises(KeyError):
             self.cat['invalid']