dendrograms / astrodendro

Generate a dendrogram from a dataset
https://dendrograms.readthedocs.io/
Other
37 stars 36 forks source link

updates to hdf5 break code #177

Closed vtcloud closed 1 year ago

vtcloud commented 2 years ago

Hi,

I am running the dendrogram code on a iMac using anaconda with the following packages: h5py 3.5.0 py38h8f61f66_100 hdf5 1.10.6 hdbbcd12_0 anaconda

Unfortunately, the use of these package versions break the loading of saved dendrograms. Quick test with (the fits file is in the astrodendro/tests directory):

` from astrodendro import Dendrogram from astropy.io import fits

image = fits.getdata('PerA_Extn2MASS_F_Gal.fits') d = Dendrogram.compute(image, min_value=2.0, min_delta=1., min_npix=10, verbose=True)

d.save_to('my_dendrogram.hdf5') d = Dendrogram.load_from('my_dendrogram.hdf5') `

In order to get it running again, I made the following changes to the code:

Old file hdf5.py: `def dendro_import_hdf5(filename): """Import 'filename' and construct a dendrogram from it""" import h5py from astropy.wcs.wcs import WCS

log.debug('Loading HDF5 file from disk...')
with h5py.File(filename, 'r') as h5f:
    newick = h5f['newick'].value
    data = h5f['data'].value
    index_map = h5f['index_map'].value

`

Modification:

newick = h5f['newick'][()] if not isinstance(newick, str): newick = newick.decode() data = h5f['data'][()] index_map = h5f['index_map'][()]

It seems to work again. Hope it helps others.

vtcloud commented 2 years ago

Sorry, I missed one change in hdf5.py:

' def dendro_import_hdf5(filename): """Import 'filename' and construct a dendrogram from it""" import h5py from astropy.wcs.wcs import WCS

log.debug('Loading HDF5 file from disk...')
with h5py.File(filename, 'r') as h5f:
    newick = h5f['newick'][()]
    if not isinstance(newick, str):
            newick = newick.decode()
    data = h5f['data'][()]
    index_map = h5f['index_map'][()]

    params = {}
    if 'min_value' in h5f.attrs:
        params['min_value'] = h5f.attrs['min_value']
        params['min_delta'] = h5f.attrs['min_delta']
        params['min_npix'] = h5f.attrs['min_npix']

    try:
        wcs = WCS(h5f['wcs_header'][()])

' The change also has to be applied to the last line in the code snippet (the insert above shows the changed code).

astrofrog commented 1 year ago

I think this should now be fixed, but feel free to re-open if not