Skip to content

Commit

Permalink
Merge pull request #7234 from mhvk/angle-str-numpy-dev-issue
Browse files Browse the repository at this point in the history
Ensure Angle.__str__ does not assume array2print passes subclasses.
  • Loading branch information
bsipocz committed Mar 2, 2018
2 parents e18445a + 50b0d97 commit 0361a2b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ astropy.coordinates

- Add a workaround for a bug in the einsum function in Numpy 1.14.0. [#7187]

- Fix problems with printing ``Angle`` instances under numpy 1.14.1. [#7234]

astropy.cosmology
^^^^^^^^^^^^^^^^^

Expand Down
26 changes: 17 additions & 9 deletions astropy/coordinates/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from . import angle_utilities as util
from .. import units as u
from ..utils import isiterable
from ..utils.compat import NUMPY_LT_1_14_1

__all__ = ['Angle', 'Latitude', 'Longitude']

Expand Down Expand Up @@ -426,19 +427,26 @@ def is_within_bounds(self, lower=None, upper=None):
ok &= np.all(self < Angle(upper))
return bool(ok)

def __str__(self):
def _str_helper(self, format=None):
if self.isscalar:
return str(self.to_string())
return self.to_string(format=format)

if NUMPY_LT_1_14_1:
def formatter(x):
return x.to_string(format=format)
else:
return np.array2string(self, formatter={'all': lambda x: x.to_string()})
# In numpy 1.14.1, array2print formatters get passed plain numpy scalars instead
# of subclass array scalars, so we need to recreate an array scalar.
def formatter(x):
return self._new_view(x).to_string(format=format)

return np.array2string(self, formatter={'all': formatter})

def __str__(self):
return self._str_helper()

def _repr_latex_(self):
if self.isscalar:
return self.to_string(format='latex')
else:
# Need to do a magic incantation to convert to str. Regular str
# or array2string causes all backslashes to get doubled.
return np.array2string(self, formatter={'all': lambda x: x.to_string(format='latex')})
return self._str_helper(format='latex')


def _no_angle_subclass(obj):
Expand Down
3 changes: 3 additions & 0 deletions astropy/io/fits/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ....io import fits
from ....tests.helper import catch_warnings, ignore_warnings
from ....utils.compat import NUMPY_LT_1_14_1
from ....utils.exceptions import AstropyDeprecationWarning

from ..column import Delayed, NUMPY2FITS
Expand Down Expand Up @@ -844,6 +845,8 @@ def test_modify_column_attributes(self):
assert header['TNULL2'] == 'b'
assert header['TNULL3'] == 2.3

@pytest.mark.xfail(not NUMPY_LT_1_14_1,
reason="See https://github.com/astropy/astropy/issues/7214")
def test_mask_array(self):
t = fits.open(self.data('table.fits'))
tbdata = t[1].data
Expand Down
6 changes: 4 additions & 2 deletions astropy/utils/compat/numpycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


__all__ = ['NUMPY_LT_1_10_4', 'NUMPY_LT_1_11', 'NUMPY_LT_1_11_2',
'NUMPY_LT_1_12', 'NUMPY_LT_1_13', 'NUMPY_LT_1_14']
'NUMPY_LT_1_12', 'NUMPY_LT_1_13', 'NUMPY_LT_1_14',
'NUMPY_LT_1_14_1']

# TODO: It might also be nice to have aliases to these named for specific
# features/bugs we're checking for (ex:
Expand All @@ -17,4 +18,5 @@
NUMPY_LT_1_11_2 = not minversion('numpy', '1.11.2')
NUMPY_LT_1_12 = not minversion('numpy', '1.12')
NUMPY_LT_1_13 = not minversion('numpy', '1.13')
NUMPY_LT_1_14 = not minversion('numpy', '1.14dev')
NUMPY_LT_1_14 = not minversion('numpy', '1.14')
NUMPY_LT_1_14_1 = not minversion('numpy', '1.14.1')
14 changes: 14 additions & 0 deletions docs/io/fits/usage/unfamiliar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ table, Astropy will automatically detect what kind of table it is.
>>> from astropy.io import fits
>>> filename = fits.util.get_testdata_filepath('ascii.fits')
>>> hdul = fits.open(filename)

..
This needs to be skipped for numpy-1.14.1. It should be removed when 1.14.2
is available. See https://github.com/astropy/astropy/issues/7214.
.. doctest-skip::

>>> hdul[1].data[:1] # doctest: +FLOAT_CMP
FITS_rec([(10.123, 37)],
dtype=(numpy.record, {'names':['a','b'], 'formats':['S10','S5'], 'offsets':[0,11], 'itemsize':16}))
Expand Down Expand Up @@ -92,6 +99,13 @@ The other difference is the need to specify the table type when using the
... bzero=0.6, ascii=True)
>>> col3 = fits.Column(name='t1', format='I', array=[91, 92, 93], ascii=True)
>>> hdu = fits.TableHDU.from_columns([col1, col2, col3])

..
This needs to be skipped for numpy-1.14.1. It should be removed when 1.14.2
is available. See https://github.com/astropy/astropy/issues/7214.
.. doctest-skip::

>>> hdu.data
FITS_rec([('abc', 11.0, 91), ('def', 12.0, 92), ('', 0.0, 93)],
dtype=(numpy.record, [('abc', 'S3'), ('def', 'S15'), ('t1', 'S10')]))
Expand Down

0 comments on commit 0361a2b

Please sign in to comment.