spacetelescope / synphot_refactor

Synthetic photometry using Astropy
http://synphot.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
39 stars 25 forks source link

catalog.py "bug" using custom catalog grid #56

Closed pllim closed 10 years ago

pllim commented 10 years ago

See FootPrints Ticket 4589.

"Bug" is in quotes because it was reported so by the user, who was using a catalog grid that STScI does not support. She provided her fix but it changes the behavior of pysynphot. It might suit her but does not reflect what STScI wants.

The method in question is _breakList() at http://trac.assembla.com/astrolib/browser/trunk/pysynphot/pysynphot/catalog.py

Original Code

def _breakList(self, inList, index, parameter):
    par = float(parameter)

    array = [parameters[index] for parameters in inList]
    array = N.array(array, dtype=N.float64)

    upperArray = array[array >= par]
    lowerArray = array[array <= par]

    if upperArray.size == 0:
        maxAllowed = array.max()
        s = "Parameter '%s' exceeds data. Max allowed=%f, entered=%f."
        s = s % (self.parameter_names[index], maxAllowed, parameter)
        raise exceptions.ParameterOutOfBounds(s)

    elif lowerArray.size == 0:
        minAllowed = array.min()
        s = "Parameter '%s' exceeds data. Min allowed=%f, entered=%f."
        s = s % (self.parameter_names[index], minAllowed, parameter)
        raise exceptions.ParameterOutOfBounds(s)

    upper = upperArray.min()
    lower = lowerArray.max()

    upperList = []
    lowerList = []

    for i,parameters in enumerate(inList):
        if array[i] >= par and array[i] <= upper:
            upperList.append(parameters)
        if array[i] >= lower and array[i] <= par:
            lowerList.append(parameters)

    return upperList, lowerList

From User

Awhile ago, I found a bug in catalog.py within pysynphot. I have lost my test case; but I have the fix. I only just noticed that I never sent the fix along (when I upgraded my OS, reinstalled pysynphot, and it broke again).

Within catalog.py, I modifed the _breakList function.

I called

catalog.Icat('nextgen', 4159.10610494, 0, 3.8294)

But I believe the nextgen catalog was something I created from a library of atmospheres. Let me know if you want it. I have attached the error below the new (fixed) code:

def _breakList(self, inList, index, parameter):
    par = float(parameter)

    array = [parameters[index] for parameters in inList]
    array = N.array(array, dtype=N.float64)

    upperArray = array[array >= par]
    lowerArray = array[array <= par]

    upperList = []
    lowerList = []

    warnings = ''

    if upperArray.size > 0:
        upper = upperArray.min()
        for i,parameters in enumerate(inList):
            if array[i] >= par and array[i] <= upper:
                upperList.append(parameters)
            else:
                maxAllowed = array.max()
                warnings += "Parameter '%s' exceeds data. Max allowed=%f, entered=%f.\n" % (self.parameter_names[index], maxAllowed, parameter)

    if lowerArray.size > 0:
        lower = lowerArray.max()
        for i,parameters in enumerate(inList):
            if array[i] >= lower and array[i] <= par:
                lowerList.append(parameters)
            else:
                minAllowed = array.min()
                warnings += "Parameter '%s' exceeds data. Min allowed=%f, entered=%f." % (self.parameter_names[index], minAllowed, parameter)

    if len(upperList) == 0 and len(lowerList) == 0:
        raise exceptions.ParameterOutOfBounds(warnings)

    # If only one side is empty, just replicate
    if len(upperList) == 0:
        upperList = lowerList
    if len(lowerList) == 0:
        lowerList = upperList

    #if upperArray.size == 0:
    #    maxAllowed = array.max()
    #    s = "Parameter '%s' exceeds data. Max allowed=%f, entered=%f."
    #    s = s % (self.parameter_names[index], maxAllowed, parameter)
    #    raise exceptions.ParameterOutOfBounds(s)

    #elif lowerArray.size == 0:
    #    minAllowed = array.min()
    #    s = "Parameter '%s' exceeds data. Min allowed=%f, entered=%f."
    #    s = s % (self.parameter_names[index], minAllowed, parameter)
    #    raise exceptions.ParameterOutOfBounds(s)

    #upper = upperArray.min()
    #lower = lowerArray.max()

    #upperList = []
    #lowerList = []

    #for i,parameters in enumerate(inList):
    #    if array[i] >= par and array[i] <= upper:
    #        upperList.append(parameters)
    #    if array[i] >= lower and array[i] <= par:
    #        lowerList.append(parameters)

    return upperList, lowerList

Traceback info:

ERROR
---> 68 sp = pysynphot.Icat('nextgen', temperature, metallicity, gravity)

.../ureka/v1.0_beta5/Ureka/python/lib/python2.7/site-packages/pysynphot/catalog.pyc in __init__(self, catdir, Teff, metallicity, log_g)
80
81 list2,list3 = self._breakList(list0, 1, metallicity)
---> 82 list4,list5 = self._breakList(list1, 1, metallicity)
83
84 list6,list7 = self._breakList(list2, 2, log_g)

.../ureka/v1.0_beta5/Ureka/python/lib/python2.7/site-packages/pysynphot/catalog.pyc in _breakList(self, inList, index, parameter)
137 s = "Parameter '%s' exceeds data. Max allowed=%f, entered=%f."
138 s = s % (self.parameter_names[index], maxAllowed, parameter)
--> 139 raise exceptions.ParameterOutOfBounds(s)
140
141 elif lowerArray.size == 0:

ParameterOutOfBounds: Parameter 'metallicity' exceeds data. Max allowed=-0.500000, entered=0.000000.
pllim commented 10 years ago

I am closing this, but it may be reopened if we get more complains about _breakList().