ryanhausen / fitsmap

FitsMap: A Simple, Lightweight Tool For Displaying Interactive Astronomical Image and Catalog Data
MIT License
48 stars 8 forks source link

Map Creation Error when norm_kwargs dictionary is sparse. #84

Open TheSkyentist opened 10 months ago

TheSkyentist commented 10 months ago

There is some unpredictable behavior with a per-file norm_kwargs dictionary.

I have a directory with the following structure:

 fitsmap/
├─ F200W.fits
├─ F200W-307.0.fits
├─ RGB.png
├─ Segmentation.png
/

The following code snippet results in the following error:

# Normalization kwargs
norm_kwargs = {
    'F200W.fits':dict(stretch='log',min_percent=30, max_percent=99.9),
    'Segmentation.png':dict(stretch='log',min_percent=10, max_percent=99.9)
}

# Make Map
files = ['RGB.png','F200W.fits','F200W-307.0.fits','Segmentation.png']
convert.files_to_map(
    files,out_dir='test',
    norm_kwargs=norm_kwargs
    )
  File "████/fitsmap/fitsmap/convert.py", line 1196, in files_to_map
    any(map(f, tasks))
  File "████/fitsmap/fitsmap/convert.py", line 1194, in f
    func_args[0](**func_args[1])
  File "████/fitsmap/fitsmap/convert.py", line 461, in tile_img
    mpl_norm, mpl_cmap = build_mpl_objects(array.array, image_norm)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "████/fitsmap/fitsmap/convert.py", line 408, in build_mpl_objects
    mpl_norm = simple_norm(array, **norm_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: simple_norm() got an unexpected keyword argument 'F200W.fits'

The error occurs after converting the F200W.fits file and the index.html file is not created. Behaviour is a little unpredictable, depending on which files are called as input, their order, and how many are used in the norm_kwargs file.

ryanhausen commented 10 months ago

Thanks for reporting. I will look into this.

ryanhausen commented 10 months ago

@TheSkyentist

It looks the norm_kwargs are trying to map to the png image:

# Normalization kwargs
norm_kwargs = {
    'F200W.fits':dict(stretch='log',min_percent=30, max_percent=99.9),
    'Segmentation.png':dict(stretch='log',min_percent=10, max_percent=99.9) # this image shouldn't have any norm kwargs 
}

Do you get the same error if you give a norm kwargs map for just the fits files?

The code doesn't currently validate the norm_kwargs and because you don't provide any norm_kwargs for one of your fits files the whole dict is being passed in.

I will add some norm_kwargs validation. In the mean time, you need to provide either:

  1. a single norm_kwargs dict for all the fits files

or

  1. a dictionary with a norm_kwargs with an entry for each of the fits files.
TheSkyentist commented 10 months ago

It does not give me an error when I only include the FITS files. However, it also does not give me an error when I include some of the PNGs and FITS files, only certain combinations cause errors. For example:

norm_kwargs={
        'RGB.png':dict(stretch="log",min_percent=30,max_percent=99.9),
        'F200W.fits':dict(stretch="log",min_percent=30,max_percent=99.9),
        'F200W-307.0.fits':dict(stretch="log",min_percent=30,max_percent=99.9),
        'Segmentation.png':dict(stretch="log",min_percent=30,max_percent=99.9)
    }

causes no errors.

ryanhausen commented 10 months ago

@TheSkyentist

Thanks for checking. It will only give the error when there is a FITS file that doesn't contain an entry in the dictionary. The offending code is here:

    image_engine = IMG_ENGINE_MPL if file_location.endswith(".fits") else IMG_ENGINE_PIL
    if image_engine == IMG_ENGINE_MPL:
        image_norm = norm_kwargs.get(os.path.basename(file_location), norm_kwargs)
        mpl_norm, mpl_cmap = build_mpl_objects(array.array, image_norm)

If you put a PNG in there it won't ever get checked because the code only uses the norm_kwargs for FITS image files.

I need to add some code to validate the norm_kwargs, but for now doing the above will avoid errors.

Thanks again for reporting!

TheSkyentist commented 10 months ago

Ah okay I understand now, thanks for checking!