casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdtpimaging_regression.py
Go to the documentation of this file.
00001 ################################################################################
00002 # 
00003 # Regression test for Moon raster scan imaging 
00004 # using sdtpimaging task 
00005 # 
00006 # global parameter:
00007 # benchmarking (if not defined, set to True, to report benchmark results)
00008 # initial,  if True, it save the stats results as the referece data
00009 # then the data is copied to datapath.
00010 #
00011 # some preset parameters:
00012 # testdata: test data name
00013 # ref_data: the file name of the reference output results of this regression 
00014 #           script to compare with (assumed to store at 'datapath' if not 
00015 #           available create it and copied to 'datapath')
00016 # datapath: location of the input data (MS and reference data for
00017 #           comparison of the results) 
00018 #           default datapath is currently set to casapath+'/data/alma/atf/sd/'
00019 # testdir: directory of the regression test to be performed 
00020 #
00021 # output: (in testdir)
00022 # regression log, images in CASA image format, 
00023 # summary.html(images and image statistics summary in HTML)
00024 #
00025 # last modified 2011-06-29 W. Kawasaki
00026 # modified 2008-10-08 T. Tsutsumi
00027 ########################################################################
00028 import os
00029 import sys
00030 import time
00031 import regression_utility as regutl
00032 import cPickle
00033 from numpy import *
00034 asap_init()
00035 
00036 scriptname='sdtpimaging'
00037 # benchmarking?
00038 # default is True
00039 try:
00040     benchmarking
00041 except NameError:
00042     benchmarking=None
00043 
00044 if benchmarking is None: 
00045     benchmarking = True
00046 
00047 # set initial = True to create reference data for comparison
00048 #initial = True
00049 initial = False 
00050 
00051 #log file name
00052 logfilename='sdtpimaging_regression.log'
00053 #test directory
00054 testdir='sdtpimaging_test'
00055 #
00056 testdata='uid___X1e1_X3197_X1.ms'
00057 ref_data='sdtpimaging_refdata.txt'
00058 dataname=testdata.rstrip('.ms')
00059 antids=['0','1']
00060 tol=0.05
00061 description='Description\nProcess ATF raster scan data, %s \n step1: run sdtpimaging to do baseline subtraction only\n separate run for each antenna.\n step2: run sdtpimaging to do imaging only for each antenna\n' % testdata
00062 ##############################################
00063 def compare(thisStatslist, logfile=None, outfiles=[], tol=0.05):
00064     refstats=[]
00065     statslist=['rms', 'max']
00066     diff_max=[]
00067     nodiff_maxpos=[]
00068     diff_rms=[] 
00069     failcnt=0
00070     
00071     if logfile is None:
00072         logfile = sys.stdout
00073            
00074     f = open(ref_data)
00075     refstats.append(cPickle.load(f))
00076     refstats.append(cPickle.load(f))
00077     f.close()
00078      
00079     for i in range(len(thisStatslist)):
00080         diff_max.append(refstats[i]['max']-thisStatslist[i]['max'])
00081         nodiff_maxpos.append(all(refstats[i]['maxpos']==thisStatslist[i]['maxpos']))
00082         diff_rms.append(refstats[i]['rms']-thisStatslist[i]['rms'])
00083  
00084         if (diff_max[i]<tol and nodiff_maxpos[i]):
00085             passfailstr = 'PASS'
00086         else:
00087             passfailstr = 'FAIL'
00088             failcnt+=1
00089         print >>logfile, 'image max test for antid=%d image %s' % (i, passfailstr)
00090         print >>logfile, '--- image max %s at %s' % (str(thisStatslist[i]['max']), str(thisStatslist[i]['maxpos'])) 
00091         if (diff_rms[i]<tol):
00092             passfailstr = 'PASS'
00093         else:
00094             passfailstr = 'FAIL'
00095             failcnt+=1
00096         print >>logfile, 'image rms test for antid=%d image %s' % (i, passfailstr)           
00097         print >>logfile, '--- image rms %s' % str(thisStatslist[i]['rms'])
00098 
00099 
00100     # difference, fidelity like measure..
00101     #if len(outfiles) > 0:
00102     #   default(immath)
00103     #   outfile='diffim'
00104     #   mode='evalexpr'
00105     #   expr='"%s"-"%s"' % (refim[0],outfiles[0])    
00106     #   immath()  
00107     #   imagename=outfile
00108     #   stats=imstats()
00109     #   if stats['rms'][0]!=0.0:
00110     #       outfile='fid'
00111     #       expr='abs(refim1/diffim)'
00112     #       immath()
00113 
00114     if failcnt:
00115         print >>logfile, '***SDTPIMAGING regression test FAILED***'
00116         return True
00117     else:
00118         print >>logfile, '***SDTPIMAGING regression test PASSED***'
00119         return False 
00120 ################################################################################ 
00121 try: 
00122     outfiles=[]
00123     for i in antids:
00124         outfiles.append(dataname+'Ant'+i+'.im')
00125 
00126     casapath=os.environ['CASAPATH'].split()[0]
00127     #host=os.environ['CASAPATH'].split()[3]
00128     import socket
00129     host=socket.gethostname()
00130     if host == "minor":
00131         datapath='/export/home/minor/alma/casatest/ATF/rastermaps/'
00132         print "host is %s, datapath is %s" % (host, datapath)
00133     else:
00134         datapath=casapath+'/data/regression/alma-sd/'
00135     #datapath=datapath+testdata
00136 
00137     # setup test directory
00138     curdir=os.getcwd()
00139     regutl.maketestdir(testdir)
00140     os.system("cp -r "+datapath+testdata+" "+testdir+"/.")
00141     if os.path.isfile(datapath+ref_data):
00142         os.system("cp -r "+datapath+ref_data+" "+testdir+"/.")
00143     else:
00144         initial = True
00145     os.chdir(testdir)
00146    
00147 
00148     # start regression 
00149     print "sdtpimaging regression start"
00150     startTime = time.time();
00151     startProc = time.clock();
00152 
00153     # Do baseline subtraction first
00154     default(sdtpimaging)
00155     infile=testdata
00156     calmode='baseline'
00157     stokes='XX'
00158     createimage=False
00159     masklist=[50,50]
00160     bpoly=1
00161     for i in antids:
00162        antenna=i
00163        sdtpimaging()
00164     baseline2time=time.time()
00165 
00166     #imaging
00167     #skip baseline subtraction
00168     default(sdtpimaging)
00169     infile=testdata
00170     calmode='none' 
00171     createimage=True
00172     imsize=[200,200]
00173     cell=['0.2arcmin','0.2arcmin']
00174     phasecenter="AZEL 187d54m22s 41d03m0s"
00175     ephemsrcname='Moon'
00176     pointingcolumn='direction'
00177     # didnot work well if gridfunction='BOX' or 'PB'
00178     # is chosen.
00179     gridfunction='SF'
00180     for i in antids:
00181         print "create an image for", i
00182         outfile=outfiles[int(i)]
00183         antenna=i
00184         sdtpimaging()
00185 
00186     endTime = time.time();
00187     endProc = time.clock();
00188     # main process end here
00189 
00190     #analyze output image
00191     logfile=open(logfilename, 'w')
00192     casaver=casalog.version()
00193     import datetime
00194     datestring=datetime.datetime.isoformat(datetime.datetime.today())
00195     print >>logfile, '%s running %s on %s (casaroot at %s)' % (scriptname, casaver, host, casapath)
00196     print >>logfile, 'at ', datestring
00197     print >>logfile, ' ' 
00198     print >>logfile, description
00199     print >>logfile, ' '
00200 
00201     thisStats=[]
00202     for i in antids:
00203         imagename=outfiles[int(i)]
00204         stats=imstat()
00205         #stats=ia.statistics()
00206         thisStats.append(stats)
00207  
00208     # summary page 
00209     tb.open(testdata+'/ANTENNA')
00210     antnames=tb.getcol('NAME')
00211     tb.close()
00212     htmlfile=open('summary.html','w')
00213     htmlhead='<html><head><title>sdtpimaging summary</title></head>\n'
00214     htmlbody='<body><h2>sdtpimaging regression summary</h2>'
00215     htmlbody+='<body><h3>data set:%s (ATF total power raster scans of Moon)</h3>' % testdata
00216     htmlbody+='<body><h4>regression run at %s</h4>' % datestring
00217     for i in range(len(outfiles)):
00218         ia.open(outfiles[i])
00219         data=ia.getchunk([-1,-1,1,1],[-1,-1,1,1],1,-1,True,True,False)
00220         ia.close()
00221         pngimg=outfiles[i]+'.png'
00222         tdata=data.transpose()
00223         tdatalist=tdata.tolist()
00224         tdatalist.reverse()
00225         pl.ioff()
00226         pl.imshow(tdatalist, interpolation='bilinear',cmap=pl.cm.hot,extent=(0,200,0,200)) 
00227         pl.savefig(pngimg)
00228         htmlbody+='<img src=%s />\n' % pngimg
00229         htmlbody+='<table><tr><th colspan=2>%s (antenna:%s)</th><tr>' % (outfiles[i], antnames[i])
00230         htmlbody+='<td><tr><td>max</td><td>%s</td><tr>' % thisStats[i]['max'][0] 
00231         htmlbody+='<tr><td>maxpos</td><td>%s</td><tr>' % thisStats[i]['maxpos'].tolist()
00232         htmlbody+='<tr><td>min</td><td>%s</td><tr>' % thisStats[i]['min'][0]
00233         htmlbody+='<tr><td>minpos</td><td>%s</td><tr>' % thisStats[i]['minpos'].tolist()
00234         htmlbody+='<tr><td>rms</td><td>%s</td><tr>' % thisStats[i]['rms'][0]
00235         htmlbody+='</table><hr>\n' 
00236     htmlclose = '</body></html>'  
00237     htmlfile.writelines(htmlhead)
00238     htmlfile.writelines(htmlbody)
00239     htmlfile.writelines(htmlclose)
00240     htmlfile.close() 
00241    
00242     if initial:
00243         file = open('sdtpimaging_refdata.txt','w') 
00244         for i in antids:
00245             cPickle.dump(thisStats[int(i)],file)    
00246         file.close() 
00247         os.system('cp sdtpimaging_refdata.txt '+datapath)
00248     else:
00249         compare(thisStats,logfile)
00250 
00251     if benchmarking:
00252         print >>logfile, ' '
00253         print >>logfile, '******** Benchmarking ******************' 
00254         print >>logfile, '*                                      *'
00255         print >>logfile,'Total wall clock time was:%s s ' % str(endTime - startTime)
00256         print >>logfile,'      baseline subtraction(%s independent data sets):%s s' % (len(antids), str(baseline2time - startTime))
00257         print >>logfile,'      imaging (%s images):%s s'% (len(antids), str(endTime - baseline2time))
00258         print >>logfile,'Total CPU        time was:%s s'% str(endProc - startProc)
00259 
00260     logfile.close()
00261 except Exception, instance:
00262     print "###Error in sdtpimaging regression: ", instance;
00263 finally:
00264     if logfile and (type(logfile) == file):
00265         logfile.close()
00266     os.chdir(curdir)