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 import unittest
00010
00011 '''
00012 Unit tests for task clean. It tests the following parameters:
00013 vis: wrong and correct values
00014 imagename: if output exists
00015 field: wrong field type; non-default value
00016 spw: wrong value; non-default value
00017 mode: empty value; non-default values
00018 gridmode: unsupported value; non-default values
00019 niter: wrong type; non-default values
00020 psfmode: unsupported value; non-default values
00021 imagermode: unsupported value; non-default values
00022 imsize: zero value; non-default value
00023 stokes: unsupported value; non-default value
00024 weighting: unsupported value; non-default values
00025 selectdata: True; subparameters:
00026 timerange: non-default value
00027 antenna: unsupported value; non-default value
00028
00029 Other tests: check the value of a pixel.
00030 '''
00031 class clean_test1(unittest.TestCase):
00032
00033
00034
00035 msfile = 'ngc7538_ut1.ms'
00036 res = None
00037 img = 'cleantest1'
00038 img2 = '0-cleantest1'
00039 msk = 'cleantest1.in.mask'
00040 boxmsk = 'cleantest1.box'
00041
00042 def setUp(self):
00043 self.res = None
00044 default(clean)
00045 if (os.path.exists(self.msfile)):
00046 os.system('rm -rf ' + self.msfile)
00047
00048 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00049 shutil.copytree(datapath+self.msfile, self.msfile)
00050
00051 def tearDown(self):
00052 if (os.path.exists(self.msfile)):
00053 os.system('rm -rf ' + self.msfile)
00054
00055 os.system('rm -rf ' + self.img+'*')
00056
00057 def getpixval(self,img,pixel):
00058 ia.open(img)
00059 px = ia.pixelvalue(pixel)
00060 ia.close()
00061 return px['value']['value']
00062
00063 def compareimages(self,inimage,refimage):
00064 """
00065 compare the input image with reference image
00066 return true if pix values are identical
00067 usually useful for mask images
00068 """
00069 ia.open(inimage)
00070 invals = ia.getchunk()
00071 ia.close()
00072 ia.open(refimage)
00073 refvals= ia.getchunk()
00074 ia.close()
00075 diff = refvals - invals
00076 return (numpy.all(diff==0))
00077
00078 def test1(self):
00079 '''Clean 1: Default values'''
00080 self.res = clean()
00081 self.assertFalse(self.res)
00082
00083 def test2(self):
00084 """Clean 2: Wrong input should return False"""
00085 msfile = 'badfilename'
00086 self.res = clean(vis=msfile, imagename=self.img, imagermode='')
00087 self.assertFalse(self.res)
00088
00089 def test3(self):
00090 """Clean 3: Good input should return None"""
00091 self.res = clean(vis=self.msfile,imagename=self.img, imagermode='')
00092 self.assertEqual(self.res,None)
00093
00094 def test4(self):
00095 """Clean 4: Check if output exists"""
00096 self.res = clean(vis=self.msfile,imagename=self.img)
00097 self.assertTrue(os.path.exists(self.img+'.image'))
00098
00099 def test5(self):
00100 """Clean 5: Wrong field type"""
00101 self.res = clean(vis=self.msfile,imagename=self.img,field=0)
00102 self.assertFalse(self.res)
00103
00104 def test6(self):
00105 """Clean 6: Non-default field value"""
00106 self.res = clean(vis=self.msfile,imagename=self.img,field='0~1', imagermode='')
00107 self.assertEqual(self.res, None)
00108 self.assertTrue(os.path.exists(self.img+'.image'))
00109
00110 def test7(self):
00111 """Clean 7: Wrong spw value"""
00112 self.res = clean(vis=self.msfile,imagename=self.img,spw='10')
00113 self.assertFalse(os.path.exists(self.img+'.image'))
00114
00115 def test8(self):
00116 """Clean 8: Non-default spw value"""
00117 self.res = clean(vis=self.msfile,imagename=self.img,spw='0', imagermode='')
00118 self.assertTrue(os.path.exists(self.img+'.image'))
00119
00120 def test9(self):
00121 """Clean 9: Empty mode value"""
00122 self.res = clean(vis=self.msfile,imagename=self.img,mode='')
00123 self.assertFalse(self.res)
00124
00125
00126 def test10(self):
00127 """Clean 10: Non-default mode channel"""
00128 self.res = clean(vis=self.msfile,imagename=self.img,mode='channel',imagermode='')
00129 self.assertEqual(self.res,None)
00130 self.assertTrue(os.path.exists(self.img+'.image'),'Image %s does not exist'%self.img)
00131
00132 def test11(self):
00133 """Clean 11: Non-default mode velocity"""
00134 retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00135
00136
00137
00138
00139
00140 res = clean(vis=self.msfile,imagename=self.img,mode='velocity',restfreq='23600MHz', interpolation='nearest',imagermode='')
00141 if(res != None):
00142 retValue['success']=False
00143 retValue['error_msgs']=retValue['error_msgs']\
00144 +"\nError: Failed to run in velocity mode."
00145 if(not os.path.exists(self.img+'.image')):
00146 retValue['success']=False
00147 retValue['error_msgs']=retValue['error_msgs']\
00148 +"\nError: Failed to create output image."
00149
00150
00151 vals = imval(self.img+'.image')
00152 size = len(vals['data'])
00153
00154 if (vals['data'][0]==0 or vals['data'][size-1]==0):
00155 retValue['success']=False
00156 retValue['error_msgs']=retValue['error_msgs']\
00157 +"\nError: There are blank planes in the edges of the image."
00158
00159
00160 self.assertTrue(retValue['success'],retValue['error_msgs'])
00161
00162 def test12(self):
00163 """Clean 12: Non-default mode frequency"""
00164 self.res = clean(vis=self.msfile,imagename=self.img,mode='frequency',imagermode='')
00165 self.assertEqual(self.res,None)
00166 self.assertTrue(os.path.exists(self.img+'.image'),'Image %s does not exist'%self.img)
00167
00168 def test13(self):
00169 """Clean 13: Unsupported gridmode"""
00170 self.res = clean(vis=self.msfile,imagename=self.img,gridmode='grid')
00171 self.assertFalse(self.res)
00172
00173 def test14(self):
00174 """Clean 14: Non-default gridmode widefield"""
00175 self.res = clean(vis=self.msfile,imagename=self.img,gridmode='widefield',imsize=[20],imagermode='')
00176 self.assertEqual(self.res, None)
00177 self.assertTrue(os.path.exists(self.img+'.image'),'Image %s does not exist'%self.img)
00178
00179
00180
00181
00182
00183
00184
00185
00186 def test15(self):
00187 """Clean 15: Wrong niter type"""
00188 self.res = clean(vis=self.msfile,imagename=self.img,niter='1')
00189 self.assertFalse(self.res)
00190
00191 def test16(self):
00192 """Clean 16: Non-default niter values"""
00193 for n in range(10,400,50):
00194 self.res = clean(vis=self.msfile,imagename=self.img,niter=n, imagermode='')
00195 self.assertEqual(self.res,None,'Failed for niter = %s' %n)
00196
00197 def test17(self):
00198 """Clean 17: Unsupported psfmode"""
00199 self.res = clean(vis=self.msfile,imagename=self.img,psfmode='psf')
00200 self.assertFalse(self.res)
00201
00202 def test18(self):
00203 """Clean 18: Non-default psfmode hogbom"""
00204 self.res = clean(vis=self.msfile,imagename=self.img,psfmode='hogbom',imagermode='')
00205 self.assertEqual(self.res, None)
00206 self.assertTrue(os.path.exists(self.img+'.image'))
00207
00208 def test19(self):
00209 """Clean 19: Non-default psfmode clarkstokes"""
00210 self.res = clean(vis=self.msfile,imagename=self.img,psfmode='clarkstokes',imagermode='')
00211 self.assertEqual(self.res, None)
00212 self.assertTrue(os.path.exists(self.img+'.image'))
00213
00214 def test20(self):
00215 """Clean 20: Unsupported imagermode"""
00216 self.res = clean(vis=self.msfile,imagename=self.img,imagermode='clark')
00217 self.assertFalse(self.res)
00218
00219 def test21(self):
00220 '''Clean 21: Non-default imagermode csclean'''
00221
00222 self.res = clean(vis=self.msfile,imagename=self.img,imagermode='csclean')
00223 self.assertEqual(self.res, None)
00224 self.assertTrue(os.path.exists(self.img+'.image'))
00225
00226 def test22(self):
00227 '''Clean 22: Non-default imagermode mosaic'''
00228 self.res = clean(vis=self.msfile,imagename=self.img,imagermode='mosaic',imsize=[20])
00229 self.assertEqual(self.res, None)
00230 self.assertTrue(os.path.exists(self.img+'.image'))
00231
00232 def test23(self):
00233 """Clean 23: Zero value of imsize"""
00234 self.res = clean(vis=self.msfile,imagename=self.img,imsize=0)
00235 self.assertFalse(os.path.exists(self.img+'.image'))
00236
00237 def test24(self):
00238 '''Clean 24: Non-default imsize values'''
00239 self.res = clean(vis=self.msfile,imagename=self.img,imsize=[80,80], imagermode='')
00240 self.assertEqual(self.res,None)
00241 self.assertTrue(os.path.exists(self.img+'.image'),'Image %s does not exist' %self.img)
00242
00243 def test25(self):
00244 """Clean 25: Non-default cell values"""
00245 self.res = clean(vis=self.msfile,imagename=self.img, cell=12.5, imagermode='')
00246 self.assertEqual(self.res, None)
00247 self.assertTrue(os.path.exists(self.img+'.image'))
00248
00249 def test26(self):
00250 """Clean 26: Unsupported Stokes parameter"""
00251 self.res = clean(vis=self.msfile,imagename=self.img, stokes='V')
00252 self.assertFalse(self.res)
00253
00254 def test27(self):
00255 """Clean 27: Non-default Stokes parameter"""
00256 self.res = clean(vis=self.msfile,imagename=self.img, stokes='RR', imagermode='')
00257 self.assertEqual(self.res, None)
00258 self.assertTrue(os.path.exists(self.img+'.image'))
00259
00260 def test28(self):
00261 '''Clean 28: Unsupported weighting mode'''
00262 self.res = clean(vis=self.msfile,imagename=self.img, weighting='median')
00263 self.assertFalse(self.res)
00264
00265 def test29(self):
00266 '''Clean 29: Non-default weighting uniform'''
00267 self.res = clean(vis=self.msfile,imagename=self.img, weighting='uniform', imagermode='')
00268 self.assertEqual(self.res, None)
00269 self.assertTrue(os.path.exists(self.img+'.image'))
00270
00271 def test30(self):
00272 '''Clean 30: Non-default weighting briggs'''
00273 self.res = clean(vis=self.msfile,imagename=self.img, weighting='briggs', imagermode='')
00274 self.assertEqual(self.res, None)
00275 self.assertTrue(os.path.exists(self.img+'.image'))
00276
00277 def test31(self):
00278 '''Clean 31: Non-default weighting radial'''
00279 self.res = clean(vis=self.msfile,imagename=self.img, weighting='radial', imagermode='')
00280 self.assertEqual(self.res, None)
00281 self.assertTrue(os.path.exists(self.img+'.image'))
00282
00283 def test32(self):
00284 '''Clean 32: Non-default subparameters of selectdata'''
00285
00286
00287 self.res = clean(vis=self.msfile,imagename=self.img,selectdata=True,
00288 timerange='>11:30:00',antenna='VA01', imagermode='')
00289 self.assertEqual(self.res, None)
00290 self.assertTrue(os.path.exists(self.img+'.image'))
00291
00292 def test33(self):
00293 '''Clean 33: Wrong antenna subparameter of selectdata'''
00294 self.res = clean(vis=self.msfile,imagename=self.img,selectdata=True,
00295 antenna='88')
00296 self.assertFalse(os.path.exists(self.img+'.image'))
00297
00298 def test34(self):
00299 '''Clean 34: Verify the value of pixel 50'''
00300
00301 self.res = clean(vis=self.msfile,imagename=self.img,selectdata=True,
00302 timerange='>11:28:00',field='0~2',imsize=[100,100],niter=10,imagermode='')
00303
00304 os.system('cp -r ' + self.img + '.image' + ' myimage.im')
00305 self.assertEqual(self.res, None)
00306 self.assertTrue(os.path.exists(self.img + '.image'))
00307
00308 ref = 0.011824539862573147
00309
00310 ref = 0.011824540793895721
00311 value = self.getpixval(self.img+'.image', 50)
00312 diff = abs(ref - value)
00313 self.assertTrue(diff < 10e-4,
00314 'Something changed the pixel brightness. ref_val=%s, new_val=%s'
00315 %(ref,value))
00316
00317 def test35(self):
00318 '''Clean 35: Wrong type of phasecenter'''
00319 self.res=clean(vis=self.msfile,imagename=self.img,phasecenter=4.5)
00320 self.assertFalse(os.path.exists(self.img+'.image'))
00321
00322 def test36(self):
00323 '''Clean 36: Non-default value of phasecenter'''
00324 self.res=clean(vis=self.msfile,imagename=self.img,phasecenter=2,imagermode='')
00325 self.assertTrue(os.path.exists(self.img+'.image'))
00326
00327 def test37(self):
00328 '''Clean 37: Test box mask'''
00329 self.res=clean(vis=self.msfile,imagename=self.img,mask=[115,115,145,145],imagermode='')
00330 self.assertTrue(os.path.exists(self.img+'.image') and
00331 os.path.exists(self.img+'.mask'))
00332
00333 def test38(self):
00334 '''Clean 38: Test freeing of resource for mask (checks CAS-954)'''
00335 self.res=clean(vis=self.msfile,imagename=self.img,mask=[115,115,145,145],imagermode='')
00336 cmd='/usr/sbin/lsof|grep %s' % self.img+'.mask'
00337 output=commands.getoutput(cmd)
00338 ret=output.find(self.img+'.mask')
00339 self.assertTrue(ret==-1)
00340
00341 def test39(self):
00342 '''Clean 39: Input mask image specified'''
00343 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00344 shutil.copytree(datapath+self.msk, '1_'+self.msk)
00345 self.res=clean(vis=self.msfile,imagename=self.img2,mask='1_'+self.msk, imagermode='')
00346 self.assertEqual(self.res, None)
00347 self.assertTrue(os.path.exists(self.img2+'.image'))
00348
00349 os.system('rm -rf ' + '1_'+self.msk+ ' ' + self.img2+'*')
00350
00351 def test40(self):
00352 '''Clean 40: Test chaniter=T clean with flagged channels'''
00353
00354 flagdata(vis=self.msfile,mode='manualflag',spw='0:0~0')
00355 self.res=clean(vis=self.msfile,imagename=self.img,mode='channel',chaniter=True, spw='0', imagermode='')
00356 self.assertEqual(self.res, None)
00357 self.assertTrue(os.path.exists(self.img+'.image'))
00358
00359 def test41(self):
00360 '''Clean 41: Test nterms=2 and ref-freq > maximum-frequency'''
00361
00362 self.res=clean(vis=self.msfile,imagename=self.img,nterms=2,reffreq='25.0GHz',niter=5,imagermode='');
00363 self.assertEqual(self.res,None);
00364
00365 def test42(self):
00366 '''Clean42: Test nterms=2, with only one channel whose frequency is same as ref-freq'''
00367
00368 self.res=clean(vis=self.msfile,imagename=self.img,nterms=2,reffreq='23691.4682MHz',spw='0:0',imagermode='');
00369 self.assertFalse(self.res);
00370
00371 def test43(self):
00372 '''Clean 43: Test user input mask from a boxfile'''
00373 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00374 refpath=datapath+'reference/'
00375 shutil.copyfile(datapath+self.boxmsk, self.boxmsk)
00376 self.res=clean(vis=self.msfile,imagename=self.img,mode='channel', mask=self.boxmsk,imagermode='')
00377 self.assertEqual(self.res, None)
00378 self.assertTrue(os.path.exists(self.img+'.image'))
00379 self.assertTrue(self.compareimages(self.img+'.mask', refpath+'ref_cleantest1boxfile.mask'))
00380
00381 def test44(self):
00382 '''Clean 44: Test input mask image of different channel ranges'''
00383 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00384 refpath=datapath+'reference/'
00385 shutil.copyfile(datapath+self.boxmsk, self.boxmsk)
00386
00387 self.res=clean(vis=self.msfile,imagename=self.img,mode='channel', mask=[115,115,145,145], niter=10,imagermode='')
00388 self.assertEqual(self.res, None)
00389
00390 self.res=clean(vis=self.msfile,imagename=self.img+'.narrow',mode='channel',
00391 nchan=3, start=2, mask=self.img+'.mask', niter=10,imagermode='')
00392 self.assertEqual(self.res, None)
00393
00394 self.res=clean(vis=self.msfile,imagename=self.img+'.wide',mode='channel',mask=self.img+'.narrow.mask', niter=10,imagermode='')
00395
00396
00397 ia.open(self.img+'.mask')
00398 r1=rg.box([0,0,0,2],[256,256,0,4])
00399 sbim=ia.subimage(outfile=self.img+'.subim.mask', region=r1)
00400 ia.close()
00401 sbim.close()
00402
00403 os.system('cp -r '+self.img+'.mask '+self.img+'.ref.mask')
00404 ia.open(self.img+'.narrow.mask')
00405
00406
00407 pixs = ia.getchunk(blc=[0,0,0,0],trc=[256,256,0,1])
00408 r1=rg.box([0,0,0,0],[256,256,0,1])
00409 ia.close()
00410 ia.open(self.img+'.ref.mask')
00411 ia.set(pixelmask=False)
00412 ia.set(pixelmask=True, region=r1)
00413 ia.putchunk(pixels=pixs,blc=[0,0,0,2])
00414 ia.close()
00415
00416 self.assertTrue(os.path.exists(self.img+'.narrow.image'))
00417 self.assertTrue(os.path.exists(self.img+'.wide.image'))
00418 self.assertTrue(self.compareimages(self.img+'.narrow.mask', self.img+'.subim.mask'),
00419 "mask mismatch for applying a wider chan. range mask to narrower chan. range clean")
00420 self.assertTrue(self.compareimages(self.img+'.wide.mask', self.img+'.ref.mask'),
00421 "mask mismatch for applying a narrower chan. range mask to wider chan. range clean")
00422
00423 def test45(self):
00424 """Clean 45: Test selection of obs ID 0 (present)"""
00425 self.res = clean(vis=self.msfile, imagename=self.img + '0',
00426 selectdata=True, observation='0', niter=10, imagermode='')
00427 self.assertTrue(os.path.exists(self.img + '0.image'))
00428
00429 def test46(self):
00430 """Clean 46: Test selection of obs ID 2 (absent)"""
00431 self.res = clean(vis=self.msfile, imagename=self.img + '2',
00432 selectdata=True, observation='2', niter=10)
00433 self.assertFalse(os.path.exists(self.img + '2.image'))
00434
00435
00436 class clean_test2(unittest.TestCase):
00437
00438
00439 msfile = 'split1scan.ms'
00440 res = None
00441 img = 'cleantest2'
00442
00443 def setUp(self):
00444 self.res = None
00445 default(clean)
00446 if (os.path.exists(self.msfile)):
00447 os.system('rm -rf ' + self.msfile)
00448
00449 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00450 shutil.copytree(datapath+self.msfile, self.msfile)
00451
00452 def tearDown(self):
00453 if (os.path.exists(self.msfile)):
00454 os.system('rm -rf ' + self.msfile)
00455
00456 def test1a(self):
00457 """Clean 1a: Non-default mode velocity"""
00458 retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00459 res = clean(vis=self.msfile,imagename=self.img,mode='velocity',restfreq='231901MHz')
00460 if(res != None):
00461 retValue['success']=False
00462 retValue['error_msgs']=retValue['error_msgs']\
00463 +"\nError: Failed to run in velocity mode."
00464 if(not os.path.exists(self.img+'.image')):
00465 retValue['success']=False
00466 retValue['error_msgs']=retValue['error_msgs']\
00467 +"\nError: Failed to create output image."
00468
00469
00470 vals = imval(self.img+'.image')
00471 size = len(vals['data'])
00472 if (vals['data'][0]==0.0 or vals['data'][size-1]==0.0):
00473 retValue['success']=False
00474 retValue['error_msgs']=retValue['error_msgs']\
00475 +"\nError: There are blank planes in the edges of the image."
00476
00477
00478 self.assertTrue(retValue['success'],retValue['error_msgs'])
00479
00480 class clean_multifield_test(unittest.TestCase):
00481
00482
00483
00484 msfile = 'outlier_ut.ms'
00485 res = None
00486 img = ['cleantest3a','cleantest3b']
00487 newoutlierfile='newoutlier4cleantest.txt'
00488 inmodel = 'cleantest3in.model'
00489
00490 def setUp(self):
00491 self.res = None
00492 default(clean)
00493 if (os.path.exists(self.msfile)):
00494 os.system('rm -rf ' + self.msfile)
00495
00496 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00497 shutil.copytree(datapath+self.msfile, self.msfile)
00498
00499 def tearDown(self):
00500 if (os.path.exists(self.msfile)):
00501 os.system('rm -rf ' + self.msfile)
00502
00503
00504
00505 for imext in ['.image','.model','.residual','.psf']:
00506 if (os.path.exists(self.img[0]+imext)):
00507 shutil.rmtree(self.img[0]+imext)
00508 if (os.path.exists(self.img[1]+imext)):
00509 shutil.rmtree(self.img[1]+imext)
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 def testCAS1972(self):
00530 """Clean test3:test CAS-1972 bug fixes ver2 (with smaller dataset)"""
00531 self.res= clean(vis=self.msfile,
00532 imagename=self.img,
00533 mode="mfs",
00534 interpolation="linear",
00535 niter=100, psfmode="clark",
00536 imagermode='',
00537
00538
00539 mask=['box[ [250pix, 250pix], [262pix, 262pix]]', 'box[ [250pix, 350pix], [262pix, 362pix]]'],
00540 imsize=[[512, 512], [512, 512]],
00541 cell="0.0001arcsec",
00542 phasecenter=['J2000 05h59m32.03313 23d53m53.9267', 'J2000 05h59m32.03313 23d53m53.9167'],
00543 weighting="natural",
00544 pbcor=False,
00545 minpb=0.1)
00546
00547 self.assertEqual(self.res,None)
00548
00549 for img in self.img:
00550 self.assertTrue(os.path.exists(img+".image"))
00551 stat0 = imstat(self.img[0]+'.model')
00552 stat1 = imstat(self.img[1]+'.model')
00553
00554 self.assertTrue(all(stat0['maxpos']==numpy.array([256,256,0,0])) and
00555 all(stat1['maxpos']==numpy.array([256,356,0,0])))
00556
00557 def testOutlier2(self):
00558 """Clean test3:test task parm input with outlier file (CAS-3221)"""
00559 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00560 shutil.copy(datapath+self.newoutlierfile,self.newoutlierfile)
00561 self.res= clean(vis=self.msfile,
00562 imagename=self.img[0],
00563 outlierfile=self.newoutlierfile,
00564 mode="mfs",
00565 interpolation="linear",
00566 niter=100, psfmode="clark",
00567 imagermode='',
00568 mask=[250, 250, 262, 262],
00569 imsize=[512, 512],
00570 cell="0.0001arcsec",
00571 phasecenter='J2000 05h59m32.03313 23d53m53.9267',
00572 weighting="natural",
00573 pbcor=False,
00574 minpb=0.1)
00575
00576
00577 self.assertEqual(self.res,None)
00578
00579 for img in self.img:
00580 self.assertTrue(os.path.exists(img+".image"))
00581 stat0 = imstat(self.img[0]+'.model')
00582 stat1 = imstat(self.img[1]+'.model')
00583
00584 self.assertTrue(all(stat0['maxpos']==numpy.array([256,256,0,0])) and
00585 all(stat1['maxpos']==numpy.array([256,356,0,0])))
00586
00587 def testOutlier3(self):
00588 """Clean test3:test task parm input with outlier file and with user-specified mask and model"""
00589 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00590 shutil.copy(datapath+self.newoutlierfile,self.newoutlierfile)
00591 shutil.copytree(datapath+self.inmodel, self.inmodel)
00592 self.res= clean(vis=self.msfile,
00593 imagename=self.img[0],
00594 outlierfile=self.newoutlierfile,
00595 mode="mfs",
00596 interpolation="linear",
00597 niter=100, psfmode="clark",
00598 imagermode='',
00599 mask='box [[250pix, 250pix], [262pix, 262pix]]',
00600 imsize=[512, 512],
00601 cell="0.0001arcsec",
00602 phasecenter='J2000 05h59m32.03313 23d53m53.9267',
00603 weighting="natural",
00604 pbcor=False,
00605 minpb=0.1)
00606
00607
00608 self.assertEqual(self.res,None)
00609
00610 for img in self.img:
00611 self.assertTrue(os.path.exists(img+".image"))
00612 stat0 = imstat(self.img[0]+'.model')
00613 stat1 = imstat(self.img[1]+'.model')
00614
00615 self.assertTrue(all(stat0['maxpos']==numpy.array([256,256,0,0])) and
00616 all(stat1['maxpos']==numpy.array([256,356,0,0])))
00617
00618 class clean_multiterm_multifield_test(unittest.TestCase):
00619
00620 msfile = 'tpts_2chan_split.ms'
00621
00622 res = None
00623 img = ['mtmf1','mtmf2']
00624 outlierfile = 'mtmf_outlier_withmodel.txt'
00625 inmodeldir = 'testmodels';
00626 modim2=['testmodels/inmodel0.model.tt0','testmodels/inmodel1.model.tt1']
00627
00628
00629 def checkdatacols(self,vis=''):
00630 tb.open(vis)
00631 colnames = tb.colnames()
00632 print 'data : ', colnames.count('DATA'), ' model : ', colnames.count('MODEL_DATA'), ' corrected : ', colnames.count('CORRECTED_DATA')
00633
00634 return [ colnames.count('DATA') , colnames.count('MODEL_DATA') , colnames.count('CORRECTED_DATA') ]
00635
00636
00637 def delmodelcorrcols(self,vis=''):
00638 tb.open(vis, nomodify=False);
00639 colnames = tb.colnames()
00640 remcols = []
00641 if colnames.count('CORRECTED_DATA'):
00642 remcols.append('CORRECTED_DATA')
00643
00644 if colnames.count('MODEL_DATA'):
00645 remcols.append('MODEL_DATA')
00646
00647 tb.removecols(remcols);
00648 tb.close();
00649
00650 def setUp(self):
00651 self.res = None
00652 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/multiterm_multifield_data/'
00653
00654 default(clean)
00655 if (os.path.exists(self.msfile)):
00656 os.system('rm -rf ' + self.msfile)
00657
00658 shutil.copytree(datapath+self.msfile, self.msfile)
00659 shutil.copy(datapath+self.outlierfile,self.outlierfile)
00660 shutil.copytree(datapath+self.inmodeldir, self.inmodeldir)
00661
00662 def tearDown(self):
00663 if (os.path.exists(self.msfile)):
00664 os.system('rm -rf ' + self.msfile)
00665 if (os.path.exists(self.inmodeldir)):
00666 os.system('rm -rf ' + self.inmodeldir)
00667 if (os.path.exists(self.outlierfile)):
00668 os.system('rm -rf ' + self.outlierfile)
00669
00670 for imext in ['.image.tt0','.image.tt1','.model.tt0','.model.tt1','.residual.tt0','.residual.tt1','.psf.tt0','.psf.tt1','.image.alpha','.flux','.mask']:
00671 if (os.path.exists(self.img[0]+imext)):
00672 shutil.rmtree(self.img[0]+imext)
00673 if (os.path.exists(self.img[1]+imext)):
00674 shutil.rmtree(self.img[1]+imext)
00675
00676 def testMultiTermOutlier(self):
00677 """Clean testMTOutlier1 (multi-term+multi-field : CAS-2664) : test task param input with nterms=2 and outlier file and with user-specified mask and model"""
00678
00679
00680 self.res= clean(vis=self.msfile,
00681 imagename=self.img[0],
00682 outlierfile=self.outlierfile,
00683 mode="mfs",
00684 nterms=2,
00685 niter=10,
00686 imagermode='',
00687 mask='circle[[50pix , 50pix] ,15pix ]',
00688 modelimage=['testmodels/inmodel0.model.tt0','testmodels/inmodel0.model.tt1'],
00689 imsize=[100, 100],
00690 interactive=False,
00691 npercycle=10,
00692 cell="8.0arcsec",
00693 weighting="briggs",
00694 pbcor=False,
00695 minpb=0.1)
00696
00697 self.assertEqual(self.res,None)
00698
00699
00700 for img in self.img:
00701 self.assertTrue(os.path.exists(img+".image.tt0"))
00702 self.assertTrue(os.path.exists(img+".image.tt1"))
00703 stat00 = imstat(self.img[0]+'.model.tt0')
00704 stat10 = imstat(self.img[1]+'.model.tt0')
00705 stat01 = imstat(self.img[0]+'.model.tt1')
00706 stat11 = imstat(self.img[1]+'.model.tt1')
00707
00708 self.assertTrue(all(stat00['maxpos']==numpy.array([50,50,0,0])) and
00709 all(stat10['maxpos']==numpy.array([15,15,0,0])))
00710 self.assertTrue(all(stat01['minpos']==numpy.array([50,50,0,0])) and
00711 all(stat11['minpos']==numpy.array([15,15,0,0])))
00712
00713
00714
00715 def testMultiTermLists(self):
00716 """Clean testMTOutlier2 (multi-term+multi-field) : test task param input with nterms=2 and inline lists for modelimages and masks"""
00717
00718
00719 self.res= clean(vis=self.msfile,
00720 imagename=self.img,
00721 mode="mfs",
00722 nterms=2,
00723 niter=10,
00724 mask=[ 'circle[[50pix , 50pix] ,15pix ]' , 'circle[[15pix , 15pix] ,8pix ]' ],
00725 modelimage=[ ['testmodels/inmodel0.model.tt0','testmodels/inmodel0.model.tt1'] , ['testmodels/inmodel1.model.tt0','testmodels/inmodel1.model.tt1' ] ],
00726 imsize=[ [100,100] , [50,50] ],
00727 phasecenter = [0 ,'J2000 19h58m34.032 +40d57m20.763' ],
00728 interactive=False,
00729 npercycle=10,
00730 cell="8.0arcsec",
00731 weighting="briggs",
00732 pbcor=False,
00733 minpb=0.1)
00734
00735 self.assertEqual(self.res,None)
00736
00737
00738 for img in self.img:
00739 self.assertTrue(os.path.exists(img+".image.tt0"))
00740 self.assertTrue(os.path.exists(img+".image.tt1"))
00741 stat00 = imstat(self.img[0]+'.model.tt0')
00742 stat10 = imstat(self.img[1]+'.model.tt0')
00743 stat01 = imstat(self.img[0]+'.model.tt1')
00744 stat11 = imstat(self.img[1]+'.model.tt1')
00745
00746 self.assertTrue(all(stat00['maxpos']==numpy.array([50,50,0,0])) and
00747 all(stat10['maxpos']==numpy.array([15,15,0,0])))
00748 self.assertTrue(all(stat01['minpos']==numpy.array([50,50,0,0])) and
00749 all(stat11['minpos']==numpy.array([15,15,0,0])))
00750
00751 def testMultiTermOutlierUvsub(self):
00752 """Clean testMTOutlier3 (multi-term+multi-field+uvsub) : test task param input with nterms=2, inline lists for outlier fields, clean with niter=0 to predict a model, and uvsub to subtract outliers.
00753 """
00754
00755 msname = self.msfile
00756
00757 usescratch = False
00758 testmschange = True
00759
00760
00761 rstate = True
00762
00763 print '(0) Start with only data column'
00764 self.delmodelcorrcols(msname);
00765 rstate = rstate & ( self.checkdatacols(msname) == [1,0,0] )
00766
00767
00768 os.system('rm -rf try_uvsub_1*');
00769 clean(vis = msname, \
00770 imagename = 'try_uvsub_1', \
00771 imsize=100,\
00772 mask = 'circle[[50pix , 50pix] ,8pix ]',\
00773 cell='8.0arcsec',\
00774 niter=10, \
00775 gain=0.5, \
00776 interactive=False, \
00777 npercycle=5,\
00778 psfmode='hogbom',\
00779 weighting='briggs',\
00780 nterms=2,\
00781 usescratch=usescratch);
00782
00783 print '(1) After first run'
00784 rstate = rstate & ( self.checkdatacols(msname) == [1,int(usescratch),0] )
00785 if rstate==False:
00786 print 'FAIL : usescratch=', usescratch, ' in clean'
00787 self.assertTrue( rstate )
00788
00789
00790 os.system('rm -rf try_uvsub_2*');
00791 clean(vis = msname, \
00792 imagename = [ 'try_uvsub_2_main', 'try_uvsub_2_outlier' ], \
00793 imsize=[ [100,100], [50,50] ], \
00794 phasecenter = [ 0 , 'J2000 19h58m34.032 +40d57m20.763' ], \
00795 mask = ['circle[[50pix , 50pix] ,8pix ]' , 'circle[[15pix , 15pix] ,8pix ]'],\
00796 cell=['8.0arcsec'],\
00797 niter=10, \
00798 gain=0.5, \
00799 interactive=False, \
00800 npercycle=5,\
00801 psfmode='hogbom',\
00802 weighting='briggs',\
00803 nterms=2,\
00804 usescratch=usescratch);
00805
00806 print '(2) After the multi-field run'
00807 rstate = rstate & ( self.checkdatacols(msname) == [1,int(usescratch),0] )
00808 if rstate==False:
00809 print 'FAIL : usescratch=', usescratch, ' in clean'
00810 self.assertTrue( rstate )
00811
00812 if testmschange == True:
00813 self.delmodelcorrcols(msname);
00814 print '(2.5) Deleting model col again'
00815 rstate = rstate & ( self.checkdatacols(msname) == [1,0,0] )
00816 if rstate==False:
00817 print 'FAIL : Error in deleting columns'
00818
00819
00820 clean(vis = msname, \
00821 imagename = 'try_uvsub_2_outlier', \
00822 imsize=[50,50],\
00823 phasecenter='J2000 19h58m34.032 +40d57m20.763' ,\
00824 mask = 'circle[[15pix , 15pix] ,8pix ]',\
00825 cell=['8.0arcsec'],\
00826 niter=0, \
00827 weighting='briggs',\
00828 nterms=2,\
00829 usescratch=usescratch);
00830
00831 print '(3) After the niter=0 prediction run with usescratch=', usescratch
00832 rstate = rstate & ( self.checkdatacols(msname) == [1,int(usescratch),0] )
00833 if rstate==False:
00834 print 'FAIL : usescratch=', usescratch, ' in clean'
00835 self.assertTrue( rstate )
00836
00837
00838 uvsub(vis = msname)
00839
00840 print '(4) After uvsub'
00841 rstate = rstate & ( self.checkdatacols(msname) == [1,int(usescratch),1] )
00842 if rstate==False:
00843 print 'FAIL : uvsub scratch-column construction. Should be ', [1,int(usescratch),1]
00844 self.assertTrue( rstate )
00845
00846
00847 os.system('rm -rf try_uvsub_3*');
00848 clean(vis = msname, \
00849 imagename = 'try_uvsub_3', \
00850 imsize=100,\
00851 mask = 'circle[[50pix , 50pix] ,8pix ]',\
00852 cell='8.0arcsec',\
00853 niter=10, \
00854 gain=0.5, \
00855 interactive=False, \
00856 npercycle=5,\
00857 psfmode='hogbom',\
00858 weighting='briggs',\
00859 nterms=2,\
00860 usescratch=usescratch);
00861
00862 print '(5) Finished second clean run'
00863 rstate = rstate & ( self.checkdatacols(msname) == [1,int(usescratch),1] )
00864 if rstate==False:
00865 print 'FAIL : usescratch in clean'
00866 self.assertTrue( rstate )
00867
00868 print '(6) Check that try_uvsub_3.image.tt0 looks like try_uvsub_2_main.image.tt0 and not have huge circles of try_uvsub_1.image.tt0 in the top right corner'
00869
00870 stat1 = imstat('try_uvsub_1.image.tt0')
00871 stat2 = imstat('try_uvsub_2_main.image.tt0')
00872 stat3 = imstat('try_uvsub_3.image.tt0')
00873
00874 print "Peak positions : ", stat1['maxpos'] , stat2['maxpos'] , stat3 ['maxpos']
00875
00876 rstate = rstate & all(stat1['maxpos']==numpy.array([50,50,0,0]))
00877 rstate = rstate & all(stat1['maxpos']==numpy.array([50,50,0,0]))
00878 rstate = rstate & all(stat1['maxpos']==numpy.array([50,50,0,0]))
00879
00880 statcorner1 = imstat('try_uvsub_1.image.tt0', box='70,70,99,99')
00881 statcorner2 = imstat('try_uvsub_2_main.image.tt0', box='70,70,99,99')
00882 statcorner3 = imstat('try_uvsub_3.image.tt0', box='70,70,99,99')
00883
00884 print "Corner RMS : ", statcorner1['rms'] , statcorner2['rms'] , statcorner3 ['rms']
00885
00886 rstate = rstate & (statcorner1['rms'] > 0.049 and statcorner1['rms'] < 0.05)
00887 rstate = rstate & (statcorner2['rms'] > 0.0021 and statcorner2['rms'] < 0.0022)
00888 rstate = rstate & (statcorner3['rms'] > 0.0021 and statcorner3['rms'] < 0.0022)
00889
00890 self.assertTrue( rstate )
00891
00892
00893
00894 class clean_multims_test(unittest.TestCase):
00895
00896 msfiles = ['point_spw1.ms', 'point_spw2.ms']
00897 res = None
00898 img = 'cleantest_multims'
00899 mask = 'multims-mfs.topo.mask'
00900
00901 def setUp(self):
00902 self.res = None
00903 default(clean)
00904 for msfile in self.msfiles:
00905 if (os.path.exists(msfile)):
00906 os.system('rm -rf ' + msfile)
00907 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00908
00909 shutil.copytree(datapath+msfile, msfile)
00910
00911 def tearDown(self):
00912 for msfile in self.msfiles:
00913 if (os.path.exists(msfile)):
00914 os.system('rm -rf ' + msfile)
00915
00916 for imext in ['.image','.model','.residual','.psf','.flux','.mask']:
00917 if (os.path.exists(self.img+imext)):
00918 shutil.rmtree(self.img+imext)
00919
00920 def test_multims1(self):
00921 '''Clean multims test1: Test two cases with wrong numbers of spw or field given)'''
00922 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00923 resfail1=clean(vis=self.msfiles,spw=['0~1','0~1','0'],field=['0~2','0~2'],imagename=self.img,imagermode='')
00924 resfail2=clean(vis=self.msfiles,spw=['0~1','0~1'],field=['0~2','0~2','0~2'],imagename=self.img,imagermode='')
00925 self.res=clean(vis=self.msfiles,spw=['0~1','0~1'],field=['0~2','0~2'],imagename=self.img,imagermode='')
00926 self.assertFalse(resfail1)
00927 self.assertFalse(resfail2)
00928 self.assertEqual(self.res,None)
00929
00930 def test_multims2(self):
00931 '''Clean multims test2: Test multiple MSes input in mfs mode'''
00932
00933
00934
00935
00936
00937 refimbandw=1.019991294e+09
00938 refimfreq=1.489984788e+09
00939 refimmax=1.19856226444
00940
00941 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00942 self.res=clean(vis=self.msfiles,mode='mfs', spw='', field='',imsize=[200],
00943 cell=['4.0arcsec', '4.0arcsec'], imagename=self.img,imagermode='')
00944 self.assertEqual(self.res,None)
00945 imhout=imhead(imagename=self.img+'.image',mode='list')
00946
00947 imbwrdiff = abs(refimbandw-float(imhout['cdelt4']))/refimbandw
00948 imfreqrdiff = abs(refimfreq-float(imhout['crval4']))/refimfreq
00949 maxrdiff = abs(refimmax-float(imhout['datamax']))/refimmax
00950
00951 self.assertTrue(imbwrdiff < 1.0e-9)
00952 self.assertTrue(imfreqrdiff < 1.0e-9)
00953 self.assertTrue(maxrdiff < 0.01)
00954
00955 def test_multims3(self):
00956 '''Clean multims test3: Test multiple MSes input in mfs mode with spw channel selection'''
00957
00958
00959
00960
00961
00962 refimbandw=9.59991881e+08
00963 refimfreq=1.459985082e+09
00964 refimmax=1.2438338995
00965
00966 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00967 self.res=clean(vis=self.msfiles,mode='mfs', spw=['0:0~19','0:0~16'], field='',imsize=[200],
00968 cell=['4.0arcsec', '4.0arcsec'], imagename=self.img, imagermode='')
00969 self.assertEqual(self.res,None)
00970 imhout=imhead(imagename=self.img+'.image',mode='list')
00971 imbwrdiff = abs(refimbandw-float(imhout['cdelt4']))/refimbandw
00972 imfreqrdiff = abs(refimfreq-float(imhout['crval4']))/refimfreq
00973 maxrdiff = abs(refimmax-float(imhout['datamax']))/refimmax
00974
00975
00976 self.assertTrue(imbwrdiff < 1.0e-9)
00977 self.assertTrue(imfreqrdiff < 1.0e-9)
00978 self.assertTrue(maxrdiff < 0.01)
00979
00980 def test_multims4(self):
00981 '''Clean multims test4: Test multiple MSes input in frequency mode, make a single fat bw image'''
00982
00983 refimbandw=1.0e+09
00984 refimmax=1.19717
00985 refimfreq=1.5e+09
00986
00987 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
00988 self.res=clean(vis=self.msfiles,mode='frequency', spw='', field='',imsize=[200],
00989 cell=['4.0arcsec', '4.0arcsec'], imagename=self.img, start='1.5GHz', width='1GHz', nchan=1, imagermode='')
00990 self.assertEqual(self.res,None)
00991 imhout=imhead(imagename=self.img+'.image',mode='list')
00992 imbwrdiff = abs(refimbandw-float(imhout['cdelt4']))/refimbandw
00993 imfreqrdiff = abs(refimfreq-float(imhout['crval4']))/refimfreq
00994 maxrdiff = abs(refimmax-float(imhout['datamax']))/refimmax
00995
00996
00997 self.assertTrue(imbwrdiff < 1.0e-9)
00998 self.assertTrue(imfreqrdiff < 1.0e-9)
00999 self.assertTrue(maxrdiff < 0.01)
01000
01001 def test_multims5(self):
01002 '''Clean multims test5: Test multiple MSes input with a mask in different spectral frame (mfs mode)'''
01003
01004 refimbandw=1.0e+09
01005 refimmax=1.19606661797
01006 refimfreq=1.49e+09
01007
01008 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/clean/'
01009 if not os.path.exists(self.mask):
01010 shutil.copytree(datapath+self.mask, self.mask)
01011 self.res=clean(vis=self.msfiles,mode='mfs', spw='', field='',imsize=[200],
01012 cell=['4.0arcsec', '4.0arcsec'], imagename=self.img, mask=self.mask,imagermode='')
01013 self.assertEqual(self.res,None)
01014 imhout=imhead(imagename=self.img+'.image',mode='list')
01015 imbwrdiff = abs(refimbandw-float(imhout['cdelt4']))/refimbandw
01016 imfreqrdiff = abs(refimfreq-float(imhout['crval4']))/refimfreq
01017 maxrdiff = abs(refimmax-float(imhout['datamax']))/refimmax
01018
01019 shutil.rmtree(self.mask)
01020
01021
01022 def suite():
01023
01024 return [clean_test1,clean_multifield_test,clean_multims_test,clean_multiterm_multifield_test]
01025