ejeschke / ginga

The Ginga astronomical FITS file viewer
BSD 3-Clause "New" or "Revised" License
121 stars 77 forks source link

local variable 'intercept' referenced before assignment #1041

Closed pllim closed 1 year ago

pllim commented 1 year ago

I see this traceback:

  File "ginga\ImageView.py", line 800, in _image_set_cb
    self.apply_profile_or_settings(image)
  File "ginga\ImageView.py", line 876, in apply_profile_or_settings
    self.auto_levels()
  File "ginga\ImageView.py", line 2593, in auto_levels
    loval, hival = autocuts.calc_cut_levels(image)
  File "ginga\AutoCuts.py", line 797, in calc_cut_levels
    loval, hival = self.calc_zscale(data, contrast=self.contrast,
  File "ginga\AutoCuts.py", line 830, in calc_zscale
    loval, hival = zscale.zscale_samples(samples, contrast=contrast)
  File "ginga\util\zscale.py", line 92, in zscale_samples
    ngoodpix, zstart, zslope = zsc_fit_line(samples, npix, KREJ, ngrow,
  File "ginga\util\zscale.py", line 171, in zsc_fit_line
    zstart = intercept - slope

As a result, cuts is set to (0, 0) and I have to manually adjust the cut levels for image to show properly.

My hunch is that the loop run into break at the very first iteration.

https://github.com/ejeschke/ginga/blob/af1756aa0fb9bda485fee2e1706836382d60547b/ginga/util/zscale.py#L128-L129

pllim commented 1 year ago

My data was randomly generated by numpy.random.random so all the values fall between 0 and 1.

ejeschke commented 1 year ago

Do you have some code that generates a repeatable fault image?

pllim commented 1 year ago

Something like this:

import numpy as np
from astropy.io import fits

def image_cube_hdu_obj_microns():
    flux_hdu = fits.ImageHDU(np.random.random((8, 9, 10)).astype(np.float32))
    flux_hdu.name = 'FLUX'

    uncert_hdu = fits.ImageHDU(np.random.random((8, 9, 10)).astype(np.float32))
    uncert_hdu.name = 'ERR'

    mask_hdu = fits.ImageHDU(np.random.randint((8, 9, 10)).astype(np.uint16))
    mask_hdu.name = 'MASK'

    wcs = {
        'WCSAXES': 3, 'CRPIX1': 38.0, 'CRPIX2': 38.0, 'CRPIX3': 1.0,
        'CRVAL1': 205.4384, 'CRVAL2': 27.004754, 'CRVAL3': 4.890499866509344,
        'CTYPE1': 'RA---TAN', 'CTYPE2': 'DEC--TAN', 'CTYPE3': 'WAVE',
        'CUNIT1': 'deg', 'CUNIT2': 'deg', 'CUNIT3': 'um',
        'CDELT1': 3.61111097865634E-05, 'CDELT2': 3.61111097865634E-05, 'CDELT3': 0.001000000047497451,  # noqa
        'PC1_1 ': -1.0, 'PC1_2 ': 0.0, 'PC1_3 ': 0,
        'PC2_1 ': 0.0, 'PC2_2 ': 1.0, 'PC2_3 ': 0,
        'PC3_1 ': 0, 'PC3_2 ': 0, 'PC3_3 ': 1,
        'DISPAXIS': 2, 'VELOSYS': -2538.02,
        'SPECSYS': 'BARYCENT', 'RADESYS': 'ICRS', 'EQUINOX': 2000.0,
        'LONPOLE': 180.0, 'LATPOLE': 27.004754,
        'MJDREFI': 0.0, 'MJDREFF': 0.0, 'DATE-OBS': '2014-03-30'}

    flux_hdu.header.update(wcs)
    flux_hdu.header['BUNIT'] = '1E-17 erg*s^-1*cm^-2*Angstrom^-1'

    uncert_hdu.header['BUNIT'] = '1E-17 erg*s^-1*cm^-2*Angstrom^-1'

    return fits.HDUList([fits.PrimaryHDU(), flux_hdu, uncert_hdu, mask_hdu])

hdulist = image_cube_hdu_obj_microns()
hdulist.writeto('image_cube_hdu_obj_microns.fits', overwrite=True)

xref https://github.com/spacetelescope/jdaviz/issues/1991

ejeschke commented 1 year ago

Hmm, I thought I was able to reproduce this error with dev branch last week, but now I cannot.

ejeschke commented 1 year ago

Hmm, I thought I was able to reproduce this error with dev branch last week, but now I cannot.

Figured that out--I had "Cut New" set to "off".

ejeschke commented 1 year ago

@pllim, I did some digging and found the problem to be that the sample size was too small with small images. I'm still thinking about merging #1042, after a rebase and some more testing to make sure I am getting similar results.

In the meantime, can you check that your problem images are fixed by this update in b75f2f995a6eb4e31394c1ea431f30bcd2548fed ?

pllim commented 1 year ago

See https://github.com/ejeschke/ginga/pull/1042#issuecomment-1440721720 .

I also think #1042 is still a good idea since that would bring Ginga to be more consistent with astropy.visualization. Thanks! 😸

ejeschke commented 1 year ago

on the latest master branch, cut low is 0.03439 and cut high is 0.9939

Excellent, that sounds much better. I'm glad we took a deeper look. I'm going to close this issue, and we can continue the discussion in #1042