00001 import shutil
00002 import unittest
00003 import os
00004 import numpy
00005 from recipes.pixelmask2cleanmask import *
00006 from tasks import *
00007 from taskinit import *
00008 from __main__ import default
00009 """
00010 Unit tests for task makemask
00011
00012 """
00013
00014 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/makemask/'
00015
00016
00017
00018 debug=False
00019
00020
00021 class makemaskTestBase(unittest.TestCase):
00022 """ base class for makemask unit test"""
00023 def compareimpix(self,refimage,inimage):
00024 """
00025 do pixel by pixel comparison of the cube image
00026 returns true if identical in terms of pixel values
00027 and shape (does not check coordinates)
00028 """
00029 ia.open(refimage)
00030 refdata=ia.getchunk()
00031 refshp=list(ia.shape())
00032 ia.close()
00033 ia.open(inimage)
00034 indata=ia.getchunk()
00035 inshp=list(ia.shape())
00036 ia.close()
00037 if inshp==refshp:
00038 for i in range(inshp[3]):
00039 for j in range(inshp[2]):
00040 for k in range(inshp[1]):
00041 for l in range(inshp[0]):
00042 if not indata[l][k][j][i]==refdata[l][k][j][i]:
00043 return False
00044 return True
00045
00046
00047 class test_copy(makemaskTestBase):
00048 """test copy mode"""
00049
00050
00051 inimage='ngc5921.cube1.mask'
00052 inimage2='ngc5921.cube2.mask'
00053
00054 outimage1='ngc5921.cube1.copy.mask'
00055 outimage2='ngc5921.cube1.copyinmage.mask'
00056 outimage3='ngc5921.cube2.copyinmage.mask'
00057
00058 def setUp(self):
00059
00060
00061
00062 for img in [self.inimage,self.inimage2]:
00063 if not os.path.isdir(img):
00064 shutil.copytree(datapath+img,img)
00065
00066 def tearDown(self):
00067 if not debug:
00068 for img in [self.inimage,self.inimage2,self.outimage1,self.outimage2,self.outimage3]:
00069 if os.path.isdir(img):
00070 shutil.rmtree(img)
00071 else:
00072 print "debugging mode: clean-up did not performed"
00073
00074 def test1_copyimagemask(self):
00075 """ (copy mode) testcopy1: copying an image mask (1/0 mask) to a new image mask"""
00076 try:
00077 makemask(mode='copy',inpimage=self.inimage,inpmask=self.inimage,output=self.outimage1)
00078 except Exception, e:
00079 print "\nError running makemask"
00080 raise e
00081
00082 self.assertTrue(os.path.exists(self.outimage1))
00083 self.assertTrue(self.compareimpix(self.inimage,self.outimage1))
00084
00085 def test2_copyimagemask(self):
00086 """ (copy mode) testcopy2: copying an image mask (1/0 mask) to a new in-image mask"""
00087 try:
00088 makemask(mode='copy',inpimage=self.inimage,inpmask=self.inimage,output=self.outimage2+":masknew")
00089 except Exception, e:
00090 print "\nError running makemask"
00091 raise e
00092
00093 self.assertTrue(os.path.exists(self.outimage2))
00094 if os.path.exists(self.outimage2):
00095 ia.open(self.outimage2)
00096 masknames=ia.maskhandler('get')
00097 if masknames.count('masknew')==1:
00098 pixelmask2cleanmask(self.outimage2,'masknew','_tmp_im',True)
00099 self.assertTrue(self.compareimpix(self.inimage,'_tmp_im'))
00100 shutil.rmtree('_tmp_im')
00101 ia.done()
00102
00103 def test3_copyimagemask(self):
00104 """ (copy mode) testcopy3: copying an image mask (1/0 amsk) to a new image with different coordinates(regrid)"""
00105 try:
00106 makemask(mode='copy',inpimage=self.inimage2,inpmask=self.inimage, output=self.outimage3)
00107 except Exception, e:
00108 print "\nError running makemask"
00109 raise e
00110
00111 self.assertTrue(os.path.exists(self.outimage3))
00112
00113 class test_merge(makemaskTestBase):
00114 """test merging of multiple masks in copy mode"""
00115
00116
00117 inimage='ngc5921.cube1.mask'
00118 inimage2='ngc5921.cube1.bmask'
00119 inimage3='ngc5921.cube2.mask'
00120 infile1='ellipse_rg.txt'
00121
00122 outimage1='ngc5921.cube1.merge.mask'
00123 outimage2='ngc5921.cube1.copyinmage.mask'
00124
00125 refimage1=datapath+'reference/ngc5921.mergetest1.ref.mask'
00126 refimage2=datapath+'reference/ngc5921.mergetest2.ref.mask'
00127 refimage3=datapath+'reference/ngc5921.mergetest3.ref.mask'
00128
00129 def setUp(self):
00130
00131
00132
00133 for img in [self.inimage,self.inimage2,self.inimage3]:
00134 if not os.path.isdir(img):
00135 shutil.copytree(datapath+img,img)
00136
00137 def tearDown(self):
00138 if not debug:
00139 for img in [self.inimage,self.inimage2,self.inimage3,self.outimage1,self.outimage2, self.infile1]:
00140 if os.path.isdir(img):
00141 shutil.rmtree(img)
00142 else:
00143 print "debugging mode: clean-up did not performed"
00144
00145 def test1_mergemasks(self):
00146 """ (copy mode) mergetest1: merging image mask (1/0 mask) and T/F mask and put into a new but existing image mask"""
00147 try:
00148 shutil.copytree(self.inimage,self.outimage1)
00149 makemask(mode='copy',inpimage=self.inimage,inpmask=[self.inimage,self.inimage2+':maskoo'], output=self.outimage1, overwrite=True)
00150 except Exception, e:
00151 print "\nError running makemask"
00152 raise e
00153
00154 self.assertTrue(os.path.exists(self.outimage1))
00155 self.assertTrue(self.compareimpix(self.refimage1,self.outimage1))
00156
00157
00158 def test2_mergemasks(self):
00159 """ (copy mode) mergetest2 :merging two image mask (1/0 mask) with different chan width to a new T/F mask"""
00160 try:
00161
00162 makemask(mode='copy',inpimage=self.inimage,inpmask=[self.inimage, self.inimage3, self.inimage2+':maskoo'], output=self.outimage1)
00163 except Exception, e:
00164 print "\nError running makemask"
00165 raise e
00166
00167 self.assertTrue(os.path.exists(self.outimage1))
00168 self.assertTrue(self.compareimpix(self.refimage2,self.outimage1))
00169
00170
00171 def test3_mergemasks(self):
00172 """ (copy mode) mergetest3: merging multiple masks (image mask, boolean mask, regions"""
00173 try:
00174
00175 if not os.path.exists(self.infile1):
00176 shutil.copy(datapath+self.infile1, self.infile1)
00177 makemask(mode='copy',inpimage=self.inimage,\
00178 inpmask=[self.inimage3, self.inimage2+':maskoo','ellipse_rg.txt','box[[130pix,135pix],[160pix,165pix]]'],\
00179 output=self.outimage1)
00180 except Exception, e:
00181 print "\nError running makemask"
00182 raise e
00183
00184 self.assertTrue(os.path.exists(self.outimage1))
00185 self.assertTrue(self.compareimpix(self.refimage3,self.outimage1))
00186
00187
00188
00189 class test_expand(makemaskTestBase):
00190 """test expand mode"""
00191
00192
00193 inimage='ngc5921.cont.mask'
00194 inimage2='ngc5921.cube1.mask'
00195 inimage3='ngc5921.cube2.mask'
00196 inimage4='ngc5921.cube1.bmask'
00197
00198 outimage='ngc5921.cube1.expand.mask'
00199 outimage2='ngc5921.cube1.copyinmage.mask'
00200 outimage3='ngc5921.cube2.expand.mask'
00201
00202 refimage1=datapath+'reference/ngc5921.expandtest1.ref.mask'
00203 refimage2=datapath+'reference/ngc5921.expandtest2.ref.mask'
00204 refimage3=datapath+'reference/ngc5921.expandtest5.ref.mask'
00205 refimage4=datapath+'reference/ngc5921.expandtest6.ref.mask'
00206
00207 def setUp(self):
00208
00209
00210
00211 for img in [self.inimage,self.inimage2,self.inimage3,self.inimage4]:
00212 if not os.path.isdir(img):
00213 shutil.copytree(datapath+img,img)
00214
00215 def tearDown(self):
00216 if not debug:
00217 for img in [self.inimage,self.inimage2,self.inimage3,self.inimage4,
00218 self.outimage,self.outimage2,self.outimage3]:
00219 if os.path.isdir(img):
00220 shutil.rmtree(img)
00221 else:
00222 print "debugging mode: clean-up did not performed"
00223
00224 def test1_expandmask(self):
00225 """ (expand mode) test1: an image mask from continuum clean to a cube mask"""
00226 try:
00227 shutil.copytree(self.inimage2,self.outimage)
00228 makemask(mode='expand',inpimage=self.inimage,inpmask=self.inimage, output=self.outimage, overwrite=True)
00229 except Exception, e:
00230 print "\nError running makemask"
00231 raise e
00232
00233 self.assertTrue(os.path.exists(self.outimage))
00234 self.assertTrue(self.compareimpix(self.refimage1,self.outimage))
00235
00236 def test2_expandmask(self):
00237 """ (expand mode) test2: an image mask from continuum clean to a cube mask with outfreqs by channel indices"""
00238 try:
00239 shutil.copytree(self.inimage2,self.outimage)
00240 makemask(mode='expand',inpimage=self.inimage,inpmask=self.inimage, output=self.outimage, outfreqs=[4,5,6,7],overwrite=True)
00241
00242 except Exception, e:
00243 print "\nError running makemask"
00244 raise e
00245
00246 self.assertTrue(os.path.exists(self.outimage))
00247 self.assertTrue(self.compareimpix(self.refimage2,self.outimage))
00248
00249 def test3_expandmask(self):
00250 """ (expand mode) test3: an image mask from continuum clean to a cube mask with outfreqs by a frequency range"""
00251
00252 try:
00253 shutil.copytree(self.inimage2,self.outimage)
00254 makemask(mode='expand',inpimage=self.inimage,inpmask=self.inimage, output=self.outimage,
00255 outfreqs='1413.007MHz~1413.08MHz',overwrite=True)
00256
00257 except Exception, e:
00258 print "\nError running makemask"
00259 raise e
00260
00261 self.assertTrue(os.path.exists(self.outimage))
00262 self.assertTrue(self.compareimpix(self.refimage2,self.outimage))
00263
00264
00265 def test4_expandmask(self):
00266 """ (expand mode) test4: an image mask from continuum clean to a cube mask with outfreqs by a velocity range"""
00267 try:
00268 shutil.copytree(self.inimage2,self.outimage)
00269 makemask(mode='expand',inpimage=self.inimage,inpmask=self.inimage, output=self.outimage,
00270 outfreqs='1561.62km/s~1546.16km/s',overwrite=True)
00271
00272 except Exception, e:
00273 print "\nError running makemask"
00274 raise e
00275
00276 self.assertTrue(os.path.exists(self.outimage))
00277 self.assertTrue(self.compareimpix(self.refimage2,self.outimage))
00278
00279 def test5_expandmask(self):
00280 """ (expand mode) test5: an image mask from a cube mask to another cube that sepecified by a template"""
00281
00282
00283 try:
00284 makemask(mode='expand',inpimage=self.inimage3, inpmask=self.inimage2, inpfreqs='1413.029MHz~1413.229MHz',
00285 output=self.outimage3, outfreqs='1413.117MHz~1413.263MHz')
00286
00287 except Exception, e:
00288 print "\nError running makemask"
00289 raise e
00290
00291 self.assertTrue(os.path.exists(self.outimage3))
00292 self.assertTrue(self.compareimpix(self.refimage3,self.outimage3))
00293
00294 def test6_expandmask(self):
00295 """ (expand mode) test6: T/F mask from a cube mask to another cube that sepecified by a template"""
00296
00297
00298 try:
00299 makemask(mode='expand',inpimage=self.inimage3,inpmask=self.inimage4+':maskoo', inpfreqs='1561km/s~1556km/s',
00300 output=self.outimage3, outfreqs='1559.04km/s~1517.82km/s')
00301
00302 except Exception, e:
00303 print "\nError running makemask"
00304 raise e
00305
00306 self.assertTrue(os.path.exists(self.outimage3))
00307 self.assertTrue(self.compareimpix(self.refimage4,self.outimage3))
00308
00309 def test7_expandmask(self):
00310 """ (expand mode) test7: T/F mask from a cube mask to another cube that sepecified by a template"""
00311
00312
00313 try:
00314 makemask(mode='expand',inpimage=self.inimage2,inpmask=self.inimage4+':maskoo', inpfreqs='1561km/s~1556km/s',
00315 output=self.inimage3, outfreqs='1559.04km/s~1517.82km/s',overwrite=True)
00316
00317 except Exception, e:
00318 print "\nError running makemask"
00319 raise e
00320
00321
00322
00323
00324 class test_inmask(makemaskTestBase):
00325 """internal mask manupilations"""
00326
00327
00328 inimage='ngc5921.cube1.bmask2'
00329
00330 def setUp(self):
00331 for img in [self.inimage]:
00332 if not os.path.isdir(img):
00333 shutil.copytree(datapath+img,img)
00334
00335 def tearDown(self):
00336 if not debug:
00337 for img in [self.inimage]:
00338 if os.path.isdir(img):
00339 shutil.rmtree(img)
00340 else:
00341 print "debugging mode: clean-up did not performed"
00342
00343 def test_deletemask(self):
00344 """ (delete mode) delete an internal mask from the image"""
00345 try:
00346 makemask(mode='delete',inpmask=self.inimage+':'+'mask2')
00347 except Exception, e:
00348 print "\nError running makemask"
00349 raise e
00350
00351 ia.open(self.inimage)
00352 mlist=ia.maskhandler('get')
00353 ia.close()
00354 self.assertTrue(mlist.count('mask2')==0)
00355
00356 def test_setdefault(self):
00357 """ (setdefaultmask mode) set an internal mask as a default mask"""
00358 try:
00359 makemask(mode='setdefaultmask',inpmask=self.inimage+':'+'mask2')
00360 except Exception, e:
00361 print "\nError running makemask"
00362 raise e
00363
00364 ia.open(self.inimage)
00365 defaultmask=ia.maskhandler('default')[0]
00366 ia.close()
00367 self.assertTrue(defaultmask=='mask2')
00368
00369 def suite():
00370
00371 return [test_merge,test_expand,test_copy,test_inmask]