Changeset 19900
- Timestamp:
- Oct 5, 2008, 11:37:51 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py
r19832 r19900 35 35 # + Trends with field number, seeing etc. 36 36 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 42 38 43 39 plotcol_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') 46 43 ] 47 48 44 49 45 def compIPPphoto(summaryTable,mode,plotcol_tlist=plotcol_tlist): … … 76 72 chipfile_l,fpObjc_l = makePlan() 77 73 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) 80 76 # Sort res_hash by its column names to make sure order is 81 77 # identical in all rows of the table, and output is more 82 78 # legible 83 vallist,keylist = valuesKeysSortedByKeys( res_hash)79 vallist,keylist = valuesKeysSortedByKeys(stats_hash) 84 80 rowtuple_list.append(vallist) 85 plotStatsOnefile( deltas_hash,matchtable,plotcol_tlist)81 plotStatsOnefile(column_hash,goodrow_hash,matchtable,plotcol_tlist,bandindex) 86 82 newrows = numpy.rec.array(rowtuple_list,names=keylist) 87 83 tabhdu = tabHDUfromRecArray(newrows) … … 90 86 else: 91 87 appendFitsTable(summaryTable,tabhdu) 88 return column_hash,goodrow_hash 92 89 93 90 def getOutnameStatsOnefile(matchtable,kind,col1,col2=None,format='eps'): … … 101 98 return outname 102 99 103 def plotStatsOnefile(deltas_hash,matchtable,plotcol_tlist,format='eps'): 104 """Make diagnostic plots for a single table, based on values in deltas_hash""" 100 def 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""" 105 103 for troika in plotcol_tlist: 106 104 col1name = troika[0] … … 108 106 plottype = troika[2] 109 107 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)] 110 122 smOpenPlot(outname,format=format) 111 123 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) 114 125 smClosePlot() 115 126 … … 132 143 oldtabhdulist.close() 133 144 return newtabhdu.writeto(fitsfile,clobber=True) 134 145 135 146 136 147 def tabHDUfromRecArray(recarr): 137 148 """Generate a table HDU from a record array""" 138 # create column names from recarray. _names149 # create column names from recarray.dtype.names (NumPy style) 139 150 # Assume float as data type 140 151 # copy data field-wise to table HDU. … … 209 220 for f in copyfields_list: 210 221 outhash[f] = h[f] 211 212 222 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)) 213 241 214 242 # These are just the columns; need to get a slice with the correct array index later 215 243 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'] 218 246 ,'d_sky':['sky_sdss','SKY_ps1'] 219 247 ,'d_skyerr':['skyErr','SKY_SIGMA'] … … 222 250 ,'d_magerr':['psfcountserr','PSF_INST_MAG_SIG'] 223 251 } 224 colval_hash = {}225 252 ismag = re.compile('mag') 226 253 iscounts = re.compile('counts') 227 254 outcoll = [] 255 256 228 257 for outcol,list in colname_hash.iteritems(): 229 258 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 237 271 # 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': 239 273 # Create column with SDSS instrumental magnitude that can 240 274 # 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))) 242 277 instmag_col = pyfits.Column(name='psfinstmag_sdss',format='5E',array=instmag_sdss_arr) 243 278 outcoll.append(instmag_col) 279 goodrow_hash[sdsspsfinstmag_colname.lower()] = goodrow_hash[SDSScolname.lower()] 244 280 # Compute array for internal use 245 281 SDSScol_good = -2.5*log10(SDSScol_good) 246 282 247 if PS1colname == 'PSF_INST_MAG_SIG' and SDSScolname.lower() == 'psfcountserr':283 if PS1colname.lower() == 'psf_inst_mag_sig' and SDSScolname.lower() == 'psfcountserr': 248 284 # Create column with SDSS instrumental magnitude error 249 285 # 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) 252 289 outcoll.append(instmagerr_col) 290 goodrow_hash[sdsspsfinstmagerr_colname.lower()] = goodrow_hash['psfcountserr'] & \ 291 goodrow_hash['psfcounts'] 253 292 # 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] 256 298 SDSScol_good = 2.5/log(10.)*SDSScol_good/SDSScounts_good 299 goodrow_hash[outcol] = all3good_bool 257 300 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 259 304 avg = delta.mean() 260 305 lowq,med,upq = stats_med(delta) … … 263 308 outheaderlabel = [operator.add(outcol+' ',itm).upper() for itm in label_l] 264 309 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)]) 266 311 # and in return variable 267 312 for label,value in zip(outtablabel,[avg,rms,med,lowq,upq]): … … 270 315 newtab = pyfits.new_table(table.columns+pyfits.ColDefs(outcoll),header=h) 271 316 newprimhdu = pyfits.PrimaryHDU(header=infile_handle[0].header) 317 infile_handle.close() 272 318 writeTable(tablename,newprimhdu,newtab) 273 infile_handle.close() 274 return outhash,colval_hash 319 return outhash,colval_hash,goodrow_hash 275 320 276 321 def writeTable(filename,primhdu,tabhdu): … … 281 326 hdulst.writeto(filename,clobber=True) 282 327 283 def filterGoodVal(col1,col2): 328 def 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 338 def filterGoodVal2(col1,col2): 284 339 """Return a pair of arrays that contains those items where both 285 340 col1 and col2 have NaNs and SDSS "no-value" and 286 341 "no-error" flags -9999 and -1000 filtered out.""" 287 342 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 294 350 # 295 351 #... so I'm doing this: … … 426 482 f.close() 427 483 428 return outname 484 return outname,sdssbandstr,bandindex 429 485 430 486 def updateHeaderFromHash(header,hash): … … 520 576 sm.histogram(bincenters,histo) 521 577 522 def smLinePlot(x,y,l type=0,xlab=None,ylab=None,xrange=None,yrange=None,\578 def smLinePlot(x,y,logical=None,ltype=0,xlab=None,ylab=None,xrange=None,yrange=None,\ 523 579 box1=None,box2=None,box3=None,box4=None,\ 524 580 append=False): 525 """Make an sm scatterplot on current device. If append=True,581 """Make an sm line plot on current device. If append=True, 526 582 overplot with current limits. Otherwise, draw box box1 box2 box3 box4""" 527 583 import sm … … 533 589 smSetup(x,y,xrange,yrange,xlab,ylab,box1,box2,box3,box4) 534 590 sm.ltype(ltype) 535 sm.connect(x,y )591 sm.connect(x,y,logical) 536 592 except: 537 593 pass 538 594 539 595 540 def smScatterPlot(x,y, ptype=41,xlab=None,ylab=None,xrange=None,yrange=None,\596 def smScatterPlot(x,y,logical=None,ptype=41,xlab=None,ylab=None,xrange=None,yrange=None,\ 541 597 box1=None,box2=None,box3=None,box4=None,\ 542 598 append=False): … … 551 607 smSetup(x,y,xrange,yrange,xlab,ylab,box1,box2,box3,box4) 552 608 sm.ptype(ptype) 553 sm.points(x,y )609 sm.points(x,y,logical) 554 610 except: 555 611 pass
Note:
See TracChangeset
for help on using the changeset viewer.
