Skip to content

Commit 9fec28b

Browse files
committed
Get rid of a couple warts--the NumCode and ImgCode class attributes on _ImageBaseHDU. In addition to being non-pep-8, they are not very meaningfully named, and apply more broadly than to just image HDUs (though in fairness they apply *mostly* to image HDUs).
1 parent 3dd2fc5 commit 9fec28b

6 files changed

Lines changed: 50 additions & 33 deletions

File tree

‎astropy/io/fits/hdu/__init__.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see PYFITS.rst
22

3-
from .base import register_hdu, unregister_hdu, DELAYED
3+
from .base import (register_hdu, unregister_hdu, DELAYED, BITPIX2DTYPE,
4+
DTYPE2BITPIX)
45
from .compressed import CompImageHDU
56
from .groups import GroupsHDU, GroupData, Group
67
from .hdulist import HDUList
@@ -11,4 +12,5 @@
1112

1213
__all__ = ['HDUList', 'PrimaryHDU', 'ImageHDU', 'TableHDU', 'BinTableHDU',
1314
'GroupsHDU', 'GroupData', 'Group', 'CompImageHDU', 'FitsHDU',
14-
'StreamingHDU', 'register_hdu', 'unregister_hdu', 'DELAYED']
15+
'StreamingHDU', 'register_hdu', 'unregister_hdu', 'DELAYED',
16+
'BITPIX2DTYPE', 'DTYPE2BITPIX']

‎astropy/io/fits/hdu/base.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ class _Delayed(object):
2929
DELAYED = _Delayed()
3030

3131

32+
BITPIX2DTYPE = {8: 'uint8', 16: 'int16', 32: 'int32', 64: 'int64',
33+
-32: 'float32', -64: 'float64'}
34+
"""Maps FITS BITPIX values to Numpy dtype names."""
35+
36+
DTYPE2BITPIX = {'uint8': 8, 'int16': 16, 'uint16': 16, 'int32': 32,
37+
'uint32': 32, 'int64': 64, 'uint64': 64, 'float32': -32,
38+
'float64': -64}
39+
"""
40+
Maps Numpy dtype names to FITS BITPIX values (this includes unsigned
41+
integers, with the assumption that the pseudo-unsigned integer convention
42+
will be used in this case.
43+
"""
44+
45+
3246
class InvalidHDUException(Exception):
3347
"""
3448
A custom exception class used mainly to signal to _BaseHDU.__new__ that

‎astropy/io/fits/hdu/compressed.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import numpy as np
1111

12-
from .base import DELAYED, ExtensionHDU
13-
from .image import _ImageBaseHDU, ImageHDU
12+
from .base import DELAYED, ExtensionHDU, BITPIX2DTYPE, DTYPE2BITPIX
13+
from .image import ImageHDU
1414
from .table import BinTableHDU
1515
from ..card import Card
1616
from ..column import Column, ColDefs, TDEF_RE
@@ -1621,7 +1621,7 @@ def _summary(self):
16211621
for idx in range(self.header['NAXIS']):
16221622
_shape += (self.header['NAXIS' + str(idx + 1)],)
16231623

1624-
_format = _ImageBaseHDU.NumCode[self.header['BITPIX']]
1624+
_format = BITPIX2DTYPE[self.header['BITPIX']]
16251625

16261626
return (self.name, class_name, len(self.header), _shape,
16271627
_format)
@@ -1632,7 +1632,7 @@ def _update_compressed_data(self):
16321632
"""
16331633

16341634
# Check to see that the image_header matches the image data
1635-
image_bitpix = _ImageBaseHDU.ImgCode[self.data.dtype.name]
1635+
image_bitpix = DTYPE2BITPIX[self.data.dtype.name]
16361636

16371637
if image_bitpix != self._orig_bitpix or self.data.shape != self.shape:
16381638
self._update_header_data(self.header)
@@ -1734,7 +1734,7 @@ def scale(self, type=None, option='old', bscale=1, bzero=0):
17341734

17351735
# Determine the destination (numpy) data type
17361736
if type is None:
1737-
type = _ImageBaseHDU.NumCode[self._bitpix]
1737+
type = BITPIX2DTYPE[self._bitpix]
17381738
_type = getattr(np, type)
17391739

17401740
# Determine how to scale the data
@@ -1785,7 +1785,7 @@ def scale(self, type=None, option='old', bscale=1, bzero=0):
17851785
self.data = np.array(np.around(self.data), dtype=_type) # 0.7.7.1
17861786

17871787
# Update the BITPIX Card to match the data
1788-
self._bitpix = _ImageBaseHDU.ImgCode[self.data.dtype.name]
1788+
self._bitpix = DTYPE2BITPIX[self.data.dtype.name]
17891789
self._bzero = self.header.get('BZERO', 0)
17901790
self._bscale = self.header.get('BSCALE', 1)
17911791
# Update BITPIX for the image header specifically
@@ -1806,7 +1806,7 @@ def scale(self, type=None, option='old', bscale=1, bzero=0):
18061806

18071807
def _prewriteto(self, checksum=False, inplace=False):
18081808
if self._scale_back:
1809-
self.scale(_ImageBaseHDU.NumCode[self._orig_bitpix])
1809+
self.scale(BITPIX2DTYPE[self._orig_bitpix])
18101810

18111811
if self._has_data:
18121812
self._update_compressed_data()
@@ -1917,7 +1917,7 @@ def _update_header_scale_info(self, dtype=None):
19171917
if dtype is None:
19181918
dtype = self._dtype_for_bitpix()
19191919
if dtype is not None:
1920-
self.header['BITPIX'] = _ImageBaseHDU.ImgCode[dtype.name]
1920+
self.header['BITPIX'] = DTYPE2BITPIX[dtype.name]
19211921

19221922
self._bzero = 0
19231923
self._bscale = 1

‎astropy/io/fits/hdu/groups.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import sys
44
import numpy as np
55

6-
from .image import _ImageBaseHDU, PrimaryHDU
6+
from .base import DTYPE2BITPIX
7+
from .image import PrimaryHDU
78
from .table import _TableLikeHDU
89
from ..column import Column, ColDefs, FITS2NUMPY
910
from ..fitsrec import FITS_rec, FITS_record
@@ -148,7 +149,7 @@ def __new__(cls, input=None, bitpix=None, pardata=None, parnames=[],
148149
unique_parnames = _unique_parnames(parnames + ['DATA'])
149150

150151
if bitpix is None:
151-
bitpix = _ImageBaseHDU.ImgCode[input.dtype.name]
152+
bitpix = DTYPE2BITPIX[input.dtype.name]
152153

153154
fits_fmt = GroupsHDU._bitpix2tform[bitpix] # -32 -> 'E'
154155
format = FITS2NUMPY[fits_fmt] # 'E' -> 'f4'
@@ -391,7 +392,7 @@ def update_header(self):
391392
else:
392393
raise ValueError('incorrect array type')
393394

394-
self._header['BITPIX'] = _ImageBaseHDU.ImgCode[field0_code]
395+
self._header['BITPIX'] = DTYPE2BITPIX[field0_code]
395396

396397
self._header['NAXIS'] = len(self._axes)
397398

‎astropy/io/fits/hdu/image.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
import numpy as np
77

8-
from .base import DELAYED, _ValidHDU, ExtensionHDU
8+
from .base import DELAYED, _ValidHDU, ExtensionHDU, BITPIX2DTYPE, DTYPE2BITPIX
99
from ..header import Header
1010
from ..util import _is_pseudo_unsigned, _unsigned_zero, _is_int
1111
from ..verify import VerifyWarning
1212

1313
from ....extern.six import string_types
14-
from ....utils import isiterable, lazyproperty
14+
from ....utils import isiterable, lazyproperty, deprecated
1515

1616

1717
class _ImageBaseHDU(_ValidHDU):
@@ -26,14 +26,6 @@ class _ImageBaseHDU(_ValidHDU):
2626
image data
2727
"""
2828

29-
# mappings between FITS and numpy typecodes
30-
# TODO: Maybe make these module-level constants instead...
31-
NumCode = {8: 'uint8', 16: 'int16', 32: 'int32', 64: 'int64',
32-
-32: 'float32', -64: 'float64'}
33-
ImgCode = {'uint8': 8, 'int16': 16, 'uint16': 16, 'int32': 32,
34-
'uint32': 32, 'int64': 64, 'uint64': 64, 'float32': -32,
35-
'float64': -64}
36-
3729
standard_keyword_comments = {
3830
'SIMPLE': 'conforms to FITS standard',
3931
'XTENSION': 'Image extension',
@@ -264,7 +256,7 @@ def data(self, data):
264256
self._modified = True
265257

266258
if isinstance(data, np.ndarray):
267-
self._bitpix = _ImageBaseHDU.ImgCode[data.dtype.name]
259+
self._bitpix = DTYPE2BITPIX[data.dtype.name]
268260
self._orig_bitpix = self._bitpix
269261
self._orig_bscale = 1
270262
self._orig_bzero = 0
@@ -371,7 +363,7 @@ def _update_header_scale_info(self, dtype=None):
371363
if dtype is None:
372364
dtype = self._dtype_for_bitpix()
373365
if dtype is not None:
374-
self._header['BITPIX'] = _ImageBaseHDU.ImgCode[dtype.name]
366+
self._header['BITPIX'] = DTYPE2BITPIX[dtype.name]
375367

376368
self._bzero = 0
377369
self._bscale = 1
@@ -409,7 +401,7 @@ def scale(self, type=None, option='old', bscale=1, bzero=0):
409401

410402
# Determine the destination (numpy) data type
411403
if type is None:
412-
type = self.NumCode[self._bitpix]
404+
type = BITPIX2DTYPE[self._bitpix]
413405
_type = getattr(np, type)
414406

415407
# Determine how to scale the data
@@ -464,7 +456,7 @@ def scale(self, type=None, option='old', bscale=1, bzero=0):
464456
self.data = np.array(np.around(self.data), dtype=_type)
465457

466458
# Update the BITPIX Card to match the data
467-
self._bitpix = _ImageBaseHDU.ImgCode[self.data.dtype.name]
459+
self._bitpix = DTYPE2BITPIX[self.data.dtype.name]
468460
self._bzero = self._header.get('BZERO', 0)
469461
self._bscale = self._header.get('BSCALE', 1)
470462
self._header['BITPIX'] = self._bitpix
@@ -485,7 +477,7 @@ def _verify(self, option='warn'):
485477

486478
def _prewriteto(self, checksum=False, inplace=False):
487479
if self._scale_back:
488-
self.scale(self.NumCode[self._orig_bitpix])
480+
self.scale(BITPIX2DTYPE[self._orig_bitpix])
489481

490482
self.update_header()
491483
if not inplace and not self._has_data:
@@ -580,7 +572,7 @@ def _get_scaled_image_data(self, offset, shape):
580572
supports alternate offset/shape for Section support.
581573
"""
582574

583-
code = _ImageBaseHDU.NumCode[self._orig_bitpix]
575+
code = BITPIX2DTYPE[self._orig_bitpix]
584576

585577
raw_data = self._get_raw_data(shape, code, offset)
586578
raw_data.dtype = raw_data.dtype.newbyteorder('>')
@@ -662,7 +654,7 @@ def _summary(self):
662654
if self.shape and all(self.shape):
663655
# Only show the format if all the dimensions are non-zero
664656
# if data is not touched yet, use header info.
665-
format = self.NumCode[self._bitpix]
657+
format = BITPIX2DTYPE[self._bitpix]
666658
else:
667659
format = ''
668660

@@ -713,6 +705,14 @@ def _calculate_datasum(self, blocking):
713705
return super(_ImageBaseHDU, self)._calculate_datasum(
714706
blocking=blocking)
715707

708+
@deprecated('1.1.0', alternative='the module level constant BITPIX2DTYPE')
709+
def NumCode(self):
710+
return BITPIX2DTYPE
711+
712+
@deprecated('1.1.0', alternative='the module level constant DTYPE2BITPIX')
713+
def ImgCode(self):
714+
return DTYPE2BITPIX
715+
716716

717717
class Section(object):
718718
"""

‎astropy/io/fits/hdu/streaming.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from ..file import _File
77
from ..header import _pad_length
8-
from .base import _BaseHDU
8+
from .base import _BaseHDU, BITPIX2DTYPE
99
from .hdulist import HDUList
10-
from .image import PrimaryHDU, _ImageBaseHDU
10+
from .image import PrimaryHDU
1111
from ..util import fileobj_name
1212

1313

@@ -171,7 +171,7 @@ def write(self, data):
171171
raise IOError('Attempt to write more data to the stream than the '
172172
'header specified.')
173173

174-
if _ImageBaseHDU.NumCode[self._header['BITPIX']] != data.dtype.name:
174+
if BITPIX2DTYPE[self._header['BITPIX']] != data.dtype.name:
175175
raise TypeError('Supplied data does not match the type specified '
176176
'in the header.')
177177

0 commit comments

Comments
 (0)