Skip to content

Slicing GroupsHDU #6688

Closed
Closed

Description

@MSeifert04

Discovered in #6664 it seems like when creating a GroupsHDU from scratch one cannot slice it:

import numpy as np
imdata = np.arange(150.0).reshape(10, 1, 1, 3, 5)
pdata1 = np.arange(10) + 0.1
pdata2 = 42
x = fits.GroupData(imdata, bitpix=-32,
                   parnames=['abc', 'xyz', 'xyz'],
                   pardata=[pdata1, pdata2, pdata2])
hdu = fits.GroupsHDU(x)
hdu.data[:2]

throws a TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      9 hdu = fits.GroupsHDU(x)
     10 hdu.writeto(tempfile.TemporaryFile(mode='wb'))
---> 11 hdu.data[:2]

\astropy\io\fits\hdu\groups.py in __getitem__(self, key)
    210 
    211     def __getitem__(self, key):
--> 212         out = super().__getitem__(key)
    213         if isinstance(out, GroupData):
    214             out.parnames = self.parnames

\astropy\io\fits\fitsrec.py in __getitem__(self, key)
    494                 # Store the new arrays for the _coldefs object
    495                 #
--> 496                 arrays.append(self._coldefs._arrays[idx][key])
    497 
    498                 # Ensure that the sliced FITS_rec will view the same scaled

TypeError: 'NoneType' object is not subscriptable

But it works if one writes it to a file and then slices the read file:

hdu.writeto('tmp_groups.fits')

with fits.open('tmp_groups.fits') as hdul:
    hdul[0].data[:2]

Activity

  1. pllim commented on Oct 9, 2017

    @pllim
    Member

    Looks like a bug to me. Also, when I append hdu to a HDUList without writing to a file, the info() is different from the one read in from a file:

    Filename: (No file associated with this HDUList)
    No.    Name      Ver    Type      Cards   Dimensions   Format
      0  PRIMARY       1 GroupsHDU       15   (5, 3, 1, 1)   float32   1 Groups  0 Parameters
    
    Filename: tmp_groups.fits
    No.    Name      Ver    Type      Cards   Dimensions   Format
      0  PRIMARY       1 GroupsHDU       15   (5, 3, 1, 1)   float32   10 Groups  3 Parameters
    
  2. saimn commented on Dec 12, 2017

    @saimn
    Contributor

    Yep, looks real, the code probably needs to be updated to cope with the ColDefs -> _arrays references and follow what has been done for hdu/table.py.

    In [26]: hdu.data.columns
    Out[26]: 
    ColDefs(
        name = 'abc'; format = 'E'
        name = 'xyz'; format = 'E'
        name = '_xyz'; format = 'E'
        name = 'DATA'; format = 'E'
    )
    
    In [27]: hdu.data.columns._arrays
    Out[27]: [None, None, None, None]
    
    In [28]: hdu.data.columns[0]
    Out[28]: name = 'abc'; format = 'E'
    
    In [29]: hdu.data.columns[0].array
    
    In [30]: hdu.data.columns[0].array is None
    Out[30]: True
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions