USRA-STI / gdt-fermi

Gamma-ray Data Tools - Fermi mission components
Apache License 2.0
2 stars 3 forks source link

`AttributeError` when calling `GbmHealpix._repr_html()` #15

Closed derekocallaghan closed 4 months ago

derekocallaghan commented 8 months ago

The following occurs in a notebook:

from gdt.missions.fermi.gbm.localization import GbmHealPix

gbm_healpix = GbmHealPix.open("./glg_healpix_all_bn180112842_v00.fit")
gbm_healpix

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/anaconda3/envs/gifts/lib/python3.10/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/gitrepos/gifts-6u/gifts-software/gdt/core/file.py:217, in FitsFileContextManager._repr_html_(self)
    215 s += '<table>'
    216 s += '<tr><th>No.</th><th>Name</th><th>Ver</th><th>Type</th><th>Cards</th><th>Dimensions</th></tr>'
--> 217 for row in self._hdulist.info(False):
    218     s += f'<tr><td>{row[0]}</td><td>{row[1]}</td><td>{row[2]}</td><td>{row[3]}</td><td>{row[4]}</td>' \
    219          f'<td>{row[5]}</td></tr>'
    220 s += '</table>'

AttributeError: 'NoneType' object has no attribute 'info'

_hdulist is instantiated in FitsFileContextManager.open():

    def open(cls, file_path: Union[str, Path], mode: str = 'readonly', memmap: bool = None):
        """Open a FITS file.

        Args:
            file_path (str): The file path
            mode (str): The file access mode
            memmap (bool): If True, memory map when reading the file

        Returns:
            (:class:`FitsFileContextManager`)
        """
        path = Path(file_path)
        obj = cls()
        obj._hdulist = fits.open(path, mode=mode, memmap=memmap)

which is called by GbmHealpix.open():

obj = super().open(file_path, **kwargs)

However, the object is recreated in GbmHealpix.from_data(), without _hdulist:

obj = super().from_data(prob_arr, trigtime=trigtime, filename=filename)

This local FitsFileContextManager._repr_html() fix resolves the issue, if it's acceptable I'll create a PR:

diff --git a/src/gdt/core/file.py b/src/gdt/core/file.py
index c35202c..1f8f2cd 100644
--- a/src/gdt/core/file.py
+++ b/src/gdt/core/file.py
@@ -214,7 +214,7 @@ class FitsFileContextManager(AbstractContextManager):
         s = f'<p>&lt{self.__class__.__name__}(filename=<b>"{self.filename}"</b>) at {hex(id(self))}&gt</p>'
         s += '<table>'
         s += '<tr><th>No.</th><th>Name</th><th>Ver</th><th>Type</th><th>Cards</th><th>Dimensions</th></tr>'
-        for row in self._hdulist.info(False):
+        for row in self.hdulist.info(False):
             s += f'<tr><td>{row[0]}</td><td>{row[1]}</td><td>{row[2]}</td><td>{row[3]}</td><td>{row[4]}</td>' \
                  f'<td>{row[5]}</td></tr>'
         s += '</table>'
derekocallaghan commented 4 months ago

Hi @AdamGoldstein-USRA,

Just wanted to check whether the suggested fix is okay with you? If so, I'll create a PR.

Thanks

BillCleveland-USRA commented 4 months ago

I ran into the same issue and was going to perform the same fix. I'll let you create the PR.

derekocallaghan commented 4 months ago

Thanks @BillCleveland-USRA, I'll create a PR this week including a unit test.

derekocallaghan commented 4 months ago

Fixed by https://github.com/USRA-STI/gdt-core/pull/37