00001 import os
00002 import sys
00003 import shutil
00004 import commands
00005 import numpy
00006 from __main__ import default
00007 from tasks import *
00008 from taskinit import *
00009 from cleanhelper import *
00010 import unittest
00011
00012 '''
00013 Unit tests for cleanhelper class members.
00014 '''
00015 class cleanhelper_test(unittest.TestCase):
00016
00017
00018
00019 msfile = 'simptsrcs.ms'
00020 outlierfilename='outlier.txt'
00021 newoutlierfilename='newoutlier_v0.txt'
00022 newoutlierreffilename='newoutlier_ref.txt'
00023 boxfilename='cleanhelpertest-sf.box'
00024 boxfilename2='boxf.txt'
00025 rgnfilename='box0.rgn'
00026 rgntextfilename='regmask.txt'
00027 imgname='cleanhelpertest'
00028
00029 imsize=[300,300]
00030 cell='0.1arcsec'
00031 stokes='I'
00032 mode='mfs'
00033 spw=''
00034 nchan=1
00035 start=0
00036 width=0
00037 restfreq=''
00038 field=''
00039 phasecenter="J2000 17:45:40.0 -29.00.28"
00040 refpath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/cleanhelper/reference/'
00041 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/cleanhelper/'
00042 outlierfile=datapath+outlierfilename
00043 newoutlierfile=datapath+newoutlierfilename
00044 newoutlierreffile=refpath+newoutlierreffilename
00045 boxfile=datapath+boxfilename
00046 boxfile2=datapath+boxfilename2
00047 rgnfile=datapath+rgnfilename
00048 rgntextfile=datapath+rgntextfilename
00049 res = None
00050
00051 def setUp(self):
00052 usescratch=False
00053 mosweight=False
00054 if (os.path.exists(self.msfile)):
00055 os.system('rm -rf ' + self.msfile)
00056
00057 shutil.copytree(self.datapath+self.msfile, self.msfile)
00058 self.imset = cleanhelper(im, self.msfile, (usescratch or mosweight))
00059
00060 def tearDown(self):
00061 if (os.path.exists(self.msfile)):
00062 os.system('rm -rf ' + self.msfile)
00063
00064 im.close()
00065 os.system('rm -rf ' + self.imgname+'*')
00066
00067 def getpixval(self,img,pixel):
00068 ia.open(img)
00069 px = ia.pixelvalue(pixel)
00070 ia.close()
00071 return px['value']['value']
00072
00073 def comparemask(self,maskimage,refmask):
00074 """
00075 compare the input image with reference image
00076 return true if pix values are identical
00077
00078 """
00079 ia.open(maskimage)
00080 maskvals = ia.getchunk()
00081 ia.close()
00082 ia.open(refmask)
00083 refvals= ia.getchunk()
00084 ia.close()
00085 diff = refvals - maskvals
00086
00087 return (numpy.all(diff==0))
00088
00089 def testDefineimages(self):
00090 """[Cleanhelper defineimages test]"""
00091 self.imset.defineimages(imsize=self.imsize, cell=self.cell,
00092 stokes=self.stokes, mode=self.mode,
00093 spw=self.spw, nchan=self.nchan,
00094 start=self.start, width=self.width,
00095 restfreq=self.restfreq, field=self.field,
00096 phasecenter=self.phasecenter)
00097
00098 self.res=im.summary()
00099 self.assertTrue(self.res)
00100
00101 def testDefinemultimages(self):
00102 """[Cleanhelper definemultimages test]"""
00103
00104 self.run_defineimages()
00105 self.res=im.summary()
00106 self.assertTrue(self.res)
00107
00108 def run_defineimages(self,sf=False, makelargeim=False):
00109 rootname=self.imgname
00110 makepbim=True
00111 if sf:
00112 if makelargeim:
00113 imsizes=[1000,1000]
00114 else:
00115 imsizes=self.imsize
00116 phasecenters=self.phasecenter
00117 imageids='sf'
00118 else:
00119 imsizes, phasecenters, imageids=self.imset.readoutlier(self.outlierfile)
00120 print "imsizes,imageids=", imsizes,imageids
00121 self.imset.definemultiimages(rootname=rootname, imsizes=imsizes,
00122 cell=self.cell, stokes=self.stokes, mode=self.mode,
00123 spw=self.spw, nchan=self.nchan, start=self.start,
00124 width=self.width, restfreq=self.restfreq,
00125 field=self.field,
00126 phasecenters=phasecenters,
00127 names=imageids, facets=1, makepbim=makepbim)
00128
00129
00130 def testReadoutlier(self):
00131 """[Cleanhelper readoutlier test (reader for older format)]"""
00132 imsizes, phasecenters, imageids=self.imset.readoutlier(self.outlierfile)
00133 f = open(self.outlierfile,'r')
00134 lines=f.readlines()
00135 f.close()
00136 cnt = 0
00137 for elm in lines:
00138 if elm.upper().rfind('C') ==0:
00139 cnt +=1
00140 if len(imsizes) == len(phasecenters) == len(imageids) ==cnt:
00141 self.res=True
00142 else:
00143 self.res=False
00144 self.assertTrue(self.res)
00145
00146 def testNewreadoutlier(self):
00147 """[Cleanhelper newreadoutlier test (reader for new outlier file format)]"""
00148 imageids, imsizes, phasecenters, masks, models, paramdic, newformat=self.imset.newreadoutlier(self.newoutlierfile)
00149
00150 print "Using ", self.newoutlierfile
00151 f = open(self.newoutlierreffile,'r')
00152 lines=f.readlines()
00153 f.close()
00154 cnt = 0
00155 for elm in lines:
00156 if len(elm.split())!=0 and elm.split()[0]!='#' :
00157 cnt +=1
00158 print "N fields=",cnt
00159 print "imsizes=",imsizes," phasecenters=",phasecenters
00160 if len(imsizes) == len(phasecenters) == len(imageids) == len(masks) == len(models) == cnt:
00161 self.res=True
00162 else:
00163 self.res=False
00164 self.assertTrue(self.res)
00165
00166
00167 def testMakemultifieldmaskbox(self):
00168 """[Cleanhelper (new)makemultfieldmask2 test: boxes given in argument]"""
00169
00170 self.imset.maskimages={}
00171 mmask=[[[55,55,65,65],[40,70,50,75]],[20,20,40,40],[5,5,15,10]]
00172 self.run_defineimages()
00173 self.imset.makemultifieldmask2(mmask)
00174
00175 for imgroot,maskimg in self.imset.maskimages.iteritems():
00176 self.assertTrue(os.path.exists(maskimg))
00177 retval=self.comparemask(maskimg, self.refpath+'ref-'+maskimg)
00178 if retval:
00179 self.res=True
00180 else:
00181 self.res=False
00182 self.assertTrue(self.res)
00183 os.system('rm -rf ' + maskimg)
00184
00185 def testMakemultifieldmaskboxfile(self):
00186 """[Cleanhelper (new)makemultfieldmask2 test: boxes given as a AIPS boxfile]"""
00187 self.imset.maskimages={}
00188 self.run_defineimages()
00189
00190 self.imset.makemultifieldmask2(maskobject=self.outlierfile,newformat=False)
00191 for imgroot,maskimg in self.imset.maskimages.iteritems():
00192 self.assertTrue(os.path.exists(maskimg))
00193 retval=self.comparemask(maskimg,self.refpath+'ref-'+maskimg)
00194 if retval:
00195 self.res=True
00196 else:
00197 self.res=False
00198 self.assertTrue(self.res)
00199 os.system('rm -rf ' + maskimg)
00200
00201 def testMakemaskimagebox(self):
00202 """[Cleanhelper makemaskimage test: 2 boxes]"""
00203 self.run_defineimages(sf=True)
00204 print "int boxes"
00205 ibmask=[[100,85,120,95],[145,145,155,155]]
00206 maskimage=self.imset.imagelist[0]+'.0.mask'
00207 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=ibmask)
00208 self.assertTrue(os.path.exists(maskimage)," int box maskimage does not exist")
00209
00210 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00211 self.assertTrue(retval,"test on int box mask failed")
00212 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00213
00214 retval=False
00215 print "float box and int box"
00216 fibmask=[[100.0,85.0,120.0,95.0],[145,145,155,155]]
00217 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=fibmask)
00218 self.assertTrue(os.path.exists(maskimage)," float +int box maskimage does not exist")
00219 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00220 self.assertTrue(retval,"test on float+int boxes failed")
00221 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00222
00223 retval=False
00224 print "numpy.int boxes"
00225 import numpy as np
00226 box1=[np.int_(i) for i in ibmask[0]]
00227 box2=[np.int_(i) for i in ibmask[1]]
00228 numpyintmask=[box1,box2]
00229 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=fibmask)
00230 self.assertTrue(os.path.exists(maskimage)," numpy.int box maskimage does not exist")
00231 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00232 self.assertTrue(retval,"test on numpy.int boxes failed")
00233 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00234
00235 retval=False
00236 print "numpy.float boxes"
00237 box1=[np.float_(i) for i in fibmask[0]]
00238 box2=[np.float_(i) for i in fibmask[1]]
00239 numpyintmask=[box1,box2]
00240 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=fibmask)
00241 self.assertTrue(os.path.exists(maskimage)," numpy.float box maskimage does not exist")
00242 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00243 self.assertTrue(retval,"test on numpy.float boxes failed")
00244 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00245
00246
00247 def testMakemaskimageboxfile(self):
00248 """[Cleanhelper makemaskimage test: make mask from boxfile ("worldbox")]"""
00249 self.run_defineimages(sf=True)
00250 boxfile=self.boxfile
00251 maskimage=self.imset.imagelist[0]+'.mask'
00252 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=boxfile)
00253 self.assertTrue(os.path.exists(maskimage)," boxfile maskimage does not exist")
00254 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00255 self.assertTrue(retval,"test on box mask failed")
00256 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00257
00258 def testMakemaskimagemixed(self):
00259 """[Mixed input to mask parameter (single field)] """
00260
00261
00262
00263 self.run_defineimages(sf=True,makelargeim=True)
00264 maskimage=self.imset.imagelist[0]+'_mixed.mask'
00265 masks = ['circle [ [ 220pix , 650pix] ,100pix ]', self.boxfile2, self.rgnfile,self.rgntextfile]
00266 self.imset.makemaskimage(outputmask=maskimage,imagename=self.imset.imagelist[0],maskobject=masks)
00267 self.assertTrue(os.path.exists(maskimage)," boxfile maskimage does not exist")
00268 retval=self.comparemask(maskimage, self.refpath+'ref-'+maskimage)
00269 self.assertTrue(retval,"test on box mask failed")
00270 os.system('rm -rf ' + self.imset.imagelist[0]+'*')
00271
00272 def testGetOptimumSize(self):
00273 import random
00274 import time
00275
00276 self.assertEqual(cleanhelper.getOptimumSize(1024),1024)
00277 self.assertEqual(cleanhelper.getOptimumSize(59049), 60000)
00278 self.assertEqual(cleanhelper.getOptimumSize(15625), 15680)
00279
00280
00281 self.assertEqual(cleanhelper.getOptimumSize(1375),1400)
00282 self.assertEqual(cleanhelper.getOptimumSize(62354),62500)
00283 self.assertEqual(cleanhelper.getOptimumSize(981),1000)
00284 self.assertEqual(cleanhelper.getOptimumSize(8123), 8192)
00285 self.assertEqual(cleanhelper.getOptimumSize(82863),82944)
00286
00287
00288 random.seed(time.time())
00289 factorList = [2,3,5,7]
00290
00291 for i in xrange(100):
00292 x = random.randint(100,1000000)
00293 y = cleanhelper.getOptimumSize(x)
00294 self.assertTrue(y>=x,"input=%s output=%s" % (x,y))
00295
00296
00297 remainder = float(y)
00298 expList = [0,0,0,0]
00299 for idx in range(len(factorList)):
00300 while remainder > 0 and \
00301 (remainder/factorList[idx] == int(remainder/factorList[idx])):
00302 expList[idx] += 1
00303 remainder /= factorList[idx]
00304
00305
00306 value = 1
00307 for idx in range(len(factorList)):
00308 value *= factorList[idx]**expList[idx]
00309 self.assertEqual(y,value)
00310
00311 self.assertTrue(expList.count(0) < len(expList))
00312
00313
00314
00315 def suite():
00316 return [cleanhelper_test]
00317
00318 if __name__ == '__main__':
00319 testSuite = []
00320 for testClass in suite():
00321 testSuite.append(unittest.makeSuite(testClass,'test'))
00322 allTests = unittest.TestSuite(testSuite)
00323 unittest.TextTestRunner(verbosity=2).run(allTests)