IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 19900


Ignore:
Timestamp:
Oct 5, 2008, 11:37:51 PM (18 years ago)
Author:
Sebastian Jester
Message:

Use logical vectors as index to numpy arrays for selecting goodrows; allow logical vectors in plots for plotting only 'good' values; pass around all columns, computed differences and their associated logical vectors for plotting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py

    r19832 r19900  
    3535#     + Trends with field number, seeing etc.
    3636
    37 # Big (BIG) XXX: For plotting, need to make sure that ALL columns have
    38 # "good" data in exactly the same rows, otherwise lose correspondence
    39 # between them. E.g.: for every column, create a logical vector saying
    40 # whether it's 'good' or not, and then plot things only for those rows
    41 # where both are good
     37# XXX move computation of new columns into matching script
    4238
    4339plotcol_tlist = [
    44     ('d_mag','d_sky','scatter'),
    45     ('d_x','d_y','scatter')
     40    ('d_mag','d_sky','scatter')
     41    ,('d_x','d_y','scatter')
     42    ,('sky_ps1','d_mag','scatter')
    4643    ]
    47 
    4844
    4945def compIPPphoto(summaryTable,mode,plotcol_tlist=plotcol_tlist):
     
    7672    chipfile_l,fpObjc_l = makePlan()
    7773    for chipfile,fpObjc in zip(chipfile_l,fpObjc_l):
    78         matchtable = matchSdssPs1(fpObjc,chipfile)
    79         res_hash, deltas_hash = computeStatistics(matchtable)
     74        matchtable,filter_name,bandindex = matchSdssPs1(fpObjc,chipfile)
     75        stats_hash, column_hash, goodrow_hash = computeStatistics(matchtable)
    8076        # Sort res_hash by its column names to make sure order is
    8177        # identical in all rows of the table, and output is more
    8278        # legible
    83         vallist,keylist = valuesKeysSortedByKeys(res_hash)
     79        vallist,keylist = valuesKeysSortedByKeys(stats_hash)
    8480        rowtuple_list.append(vallist)
    85         plotStatsOnefile(deltas_hash,matchtable,plotcol_tlist)
     81        plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_tlist,bandindex)
    8682    newrows = numpy.rec.array(rowtuple_list,names=keylist)
    8783    tabhdu = tabHDUfromRecArray(newrows)
     
    9086    else:
    9187        appendFitsTable(summaryTable,tabhdu)
     88    return column_hash,goodrow_hash
    9289
    9390def getOutnameStatsOnefile(matchtable,kind,col1,col2=None,format='eps'):
     
    10198    return outname
    10299   
    103 def plotStatsOnefile(deltas_hash,matchtable,plotcol_tlist,format='eps'):
    104     """Make diagnostic plots for a single table, based on values in deltas_hash"""
     100def plotStatsOnefile(values_hash,goodrow_hash,matchtable,plotcol_tlist,bandindex,format='eps'):
     101    """Make diagnostic plots for a single table, based on values
     102    in values_hash"""
    105103    for troika in plotcol_tlist:
    106104        col1name = troika[0]
     
    108106        plottype = troika[2]
    109107        outname = getOutnameStatsOnefile(matchtable,plottype,col1name,col2name,format=format)
     108        values1 = values_hash[col1name]
     109        goodrows1 = goodrow_hash[col1name]
     110        values2 = values_hash[col2name]
     111        goodrows2 = goodrow_hash[col2name]
     112        # Slice out depth for current filter if necessary
     113        if len(values1.shape) > 1:
     114            values1 = values1[:,bandindex]
     115            goodrows1 = goodrows1[:,bandindex]
     116        if len(values2.shape) > 1:
     117            values2 = values2[:,bandindex]
     118            goodrows2 = goodrows2[:,bandindex]
     119        goodrows = goodrows1 & goodrows2
     120        # print col1name, col2name, sum(goodrows), sum(values1 > 1e3)
     121        # print values1[goodrows & (values1 > 1e3)]
    110122        smOpenPlot(outname,format=format)
    111123        if plottype == 'scatter':
    112             smScatterPlot(deltas_hash[col1name],deltas_hash[col2name],\
    113                               xlab=col1name,ylab=col2name)
     124            smScatterPlot(values1,values2,logical=goodrows,xlab=col1name,ylab=col2name)
    114125        smClosePlot()
    115126
     
    132143    oldtabhdulist.close()
    133144    return newtabhdu.writeto(fitsfile,clobber=True)
    134    
     145
    135146       
    136147def tabHDUfromRecArray(recarr):
    137148    """Generate a table HDU from a record array"""
    138     # create column names from recarray._names
     149    # create column names from recarray.dtype.names (NumPy style)
    139150    # Assume float as data type
    140151    # copy data field-wise to table HDU.
     
    209220    for f in copyfields_list:
    210221        outhash[f] = h[f]
    211 
    212222    filtname = outhash['FILTER']
     223
     224    goodrow_hash = {}
     225    colval_hash = {}
     226    # Find out which entries are "good" in every column
     227    for column in table.columns.names:
     228        coldata = table_data.field(column)
     229        # Check column dimensions:
     230        # If 3D, it's one of the profile columns, and we can't plot those anyway, so just skip them.
     231        if len(coldata.shape) > 2:
     232            continue
     233        # If 2D, it's an array, and we need to slice out the right
     234        # depth
     235        elif len(coldata.shape) == 2:
     236            goodrow_hash[column.lower()] = goodValBool(table_data.field(column))[:,filterID[filtname]]
     237            colval_hash[column.lower()] = array(table_data.field(column))[:,filterID[filtname]]
     238        else:
     239            goodrow_hash[column.lower()] = goodValBool(table_data.field(column))
     240            colval_hash[column.lower()] = array(table_data.field(column))
    213241
    214242    # These are just the columns; need to get a slice with the correct array index later
    215243    colname_hash = {
    216         'd_x':['colc','x_psf']
    217         ,'d_y':['rowc','y_psf']
     244        'd_x':['colc','X_PSF']
     245        ,'d_y':['rowc','Y_PSF']
    218246        ,'d_sky':['sky_sdss','SKY_ps1']
    219247        ,'d_skyerr':['skyErr','SKY_SIGMA']
     
    222250        ,'d_magerr':['psfcountserr','PSF_INST_MAG_SIG']
    223251        }
    224     colval_hash = {}
    225252    ismag = re.compile('mag')
    226253    iscounts = re.compile('counts')
    227254    outcoll = []
     255
     256   
    228257    for outcol,list in colname_hash.iteritems():
    229258        SDSScolname,PS1colname=list
    230         # Slice out the proper filter - may need to add another
    231         # boolean dictionary that says whether or not this is
    232         # necessary for a given column (e.g. for objc_colc it won't
    233         # be...)
    234         SDSScol = table_data.field(SDSScolname)[:,filterID[filtname]]
    235         PS1col = table_data.field(PS1colname)
    236         SDSScol_good,PS1col_good = filterGoodVal(SDSScol,PS1col)
     259        # Slice out the proper filter if necessary. This and other
     260        # array() calls are to make sure that we are working with
     261        # numpy.ndarrays, not with numarray's arrays, which pyfits
     262        # returns.
     263        SDSScol = array(table_data.field(SDSScolname))
     264        if len(SDSScol.shape) == 2:
     265            SDSScol = SDSScol[:,filterID[filtname]]
     266        PS1col = array(table_data.field(PS1colname))
     267        bothgood_bool = goodrow_hash[SDSScolname.lower()] & goodrow_hash[PS1colname.lower()]
     268        SDSScol_good = SDSScol[bothgood_bool]
     269        PS1col_good = PS1col[bothgood_bool]
     270        goodrow_hash[outcol] = bothgood_bool
    237271        # Compute SDSS instrumental magnitude if necessary
    238         if PS1colname == 'PSF_INST_MAG' and SDSScolname.lower() == 'psfcounts':
     272        if PS1colname.lower() == 'psf_inst_mag' and SDSScolname.lower() == 'psfcounts':
    239273            # Create column with SDSS instrumental magnitude that can
    240274            # be written to table for diagnostic use
    241             instmag_sdss_arr = -2.5*log10(table_data.field(SDSScolname))
     275            sdsspsfinstmag_colname = 'psfinstmag_sdss'
     276            instmag_sdss_arr = -2.5*log10(array(table_data.field(SDSScolname)))
    242277            instmag_col = pyfits.Column(name='psfinstmag_sdss',format='5E',array=instmag_sdss_arr)
    243278            outcoll.append(instmag_col)
     279            goodrow_hash[sdsspsfinstmag_colname.lower()] = goodrow_hash[SDSScolname.lower()]
    244280            # Compute array for internal use
    245281            SDSScol_good = -2.5*log10(SDSScol_good)
    246282
    247         if PS1colname == 'PSF_INST_MAG_SIG' and SDSScolname.lower() == 'psfcountserr':
     283        if PS1colname.lower() == 'psf_inst_mag_sig' and SDSScolname.lower() == 'psfcountserr':
    248284            # Create column with SDSS instrumental magnitude error
    249285            # that can be written to table for diagnostic use
    250             instmagerr_sdss_arr = 2.5/log(10.)*table_data.field('psfcountserr')/table_data.field('psfcounts')
    251             instmagerr_col = pyfits.Column(name='psfinstmagerr_sdss',format='5E',array=instmagerr_sdss_arr)
     286            sdsspsfinstmagerr_colname = 'psfinstmagerr_sdss'
     287            instmagerr_sdss_arr = array(2.5/log(10.)*table_data.field('psfcountserr')/table_data.field('psfcounts'))
     288            instmagerr_col = pyfits.Column(name=sdsspsfinstmagerr_colname,format='5E',array=instmagerr_sdss_arr)
    252289            outcoll.append(instmagerr_col)
     290            goodrow_hash[sdsspsfinstmagerr_colname.lower()] = goodrow_hash['psfcountserr'] & \
     291                goodrow_hash['psfcounts']
    253292            # Compute array for internal use
    254             SDSScounts = table_data.field('psfcounts')[:,filterID[filtname]]
    255             SDSScol_good,PS1col_good,SDSScounts_good = filterGoodVal3(SDSScol,PS1col,SDSScounts)
     293            SDSScounts = array(table_data.field('psfcounts')[:,filterID[filtname]])
     294            all3good_bool = bothgood_bool & goodrow_hash[sdsspsfinstmagerr_colname.lower()]
     295            SDSScol_good = SDSScol[all3good_bool]
     296            PS1col_good = PS1col[all3good_bool]
     297            SDSScounts_good = SDSScounts[all3good_bool]
    256298            SDSScol_good = 2.5/log(10.)*SDSScol_good/SDSScounts_good
     299            goodrow_hash[outcol] = all3good_bool
    257300        delta = SDSScol_good - PS1col_good
    258         colval_hash[outcol] = delta
     301        # Store *all* values in return hash for later plotting; good
     302        # values will be filtered out during plotting
     303        colval_hash[outcol] = SDSScol - PS1col
    259304        avg = delta.mean()
    260305        lowq,med,upq = stats_med(delta)
     
    263308        outheaderlabel = [operator.add(outcol+' ',itm).upper() for itm in label_l]
    264309        outtablabel = [operator.add(outcol+'_',itm).upper() for itm in label_l]
    265         saveHierarchHeaderList(h,outheaderlabel,[avg,rms,med,lowq,upq])
     310        saveHierarchHeaderList(h,outheaderlabel,[float(avg),float(rms),float(med),float(lowq),float(upq)])
    266311        # and in return variable
    267312        for label,value in zip(outtablabel,[avg,rms,med,lowq,upq]):
     
    270315    newtab = pyfits.new_table(table.columns+pyfits.ColDefs(outcoll),header=h)
    271316    newprimhdu = pyfits.PrimaryHDU(header=infile_handle[0].header)
     317    infile_handle.close()
    272318    writeTable(tablename,newprimhdu,newtab)
    273     infile_handle.close()
    274     return outhash,colval_hash
     319    return outhash,colval_hash,goodrow_hash
    275320
    276321def writeTable(filename,primhdu,tabhdu):
     
    281326    hdulst.writeto(filename,clobber=True)
    282327
    283 def filterGoodVal(col1,col2):
     328def goodValBool(col):
     329    """Return a bool array that contains True where
     330    col1 doesn't have  NaNs nor SDSS "no-value" and
     331    "no-error" flags -9999 and -1000."""
     332    from numpy import isfinite,array
     333    goodcondition = isfinite(col)
     334    for val in [-9999,-1000]:
     335        goodcondition &= (col != val)
     336    return goodcondition
     337
     338def filterGoodVal2(col1,col2):
    284339    """Return a pair of arrays that contains those items where both
    285340    col1 and col2 have  NaNs and SDSS "no-value" and
    286341    "no-error" flags -9999 and -1000 filtered out."""
    287342    from numpy import isfinite,logical_and,array
    288     # I don't understand why this doesn't work...
    289     #
    290     #goodcondition = logical_and(isfinite(col1),isfinite(col2))
    291     #col1 = col1[goodcondition]
    292     #col2_good = col2[goodcondition]
    293     #return col1,col2
     343    goodcondition = isfinite(col1) & isfinite(col2)
     344    for val in [-9999,-1000]:
     345        goodcondition &= (col1 != val)
     346        goodcondition &= (col2 != val)
     347    col1 = col1[goodcondition]
     348    col2_good = col2[goodcondition]
     349    return col1,col2
    294350    #
    295351    #... so I'm doing this:
     
    426482    f.close()
    427483   
    428     return outname
     484    return outname,sdssbandstr,bandindex
    429485
    430486def updateHeaderFromHash(header,hash):
     
    520576    sm.histogram(bincenters,histo)
    521577
    522 def smLinePlot(x,y,ltype=0,xlab=None,ylab=None,xrange=None,yrange=None,\
     578def smLinePlot(x,y,logical=None,ltype=0,xlab=None,ylab=None,xrange=None,yrange=None,\
    523579                    box1=None,box2=None,box3=None,box4=None,\
    524580                    append=False):
    525     """Make an sm scatter plot on current device. If append=True,
     581    """Make an sm line plot on current device. If append=True,
    526582    overplot with current limits. Otherwise, draw box box1 box2 box3 box4"""
    527583    import sm
     
    533589            smSetup(x,y,xrange,yrange,xlab,ylab,box1,box2,box3,box4)
    534590        sm.ltype(ltype)
    535         sm.connect(x,y)
     591        sm.connect(x,y,logical)
    536592    except:
    537593        pass
    538594
    539595
    540 def smScatterPlot(x,y,ptype=41,xlab=None,ylab=None,xrange=None,yrange=None,\
     596def smScatterPlot(x,y,logical=None,ptype=41,xlab=None,ylab=None,xrange=None,yrange=None,\
    541597                    box1=None,box2=None,box3=None,box4=None,\
    542598                    append=False):
     
    551607            smSetup(x,y,xrange,yrange,xlab,ylab,box1,box2,box3,box4)
    552608        sm.ptype(ptype)
    553         sm.points(x,y)
     609        sm.points(x,y,logical)
    554610    except:
    555611        pass
Note: See TracChangeset for help on using the changeset viewer.