00001 import os
00002 import numpy
00003 import sys
00004 import shutil
00005 from __main__ import default
00006 from tasks import *
00007 from taskinit import *
00008
00009 import unittest
00010
00011 '''
00012 Unit tests for task uvcontsub3.
00013
00014 Features tested:
00015 1. uvcontsub3 will use DATA if CORRECTED_DATA is absent.
00016 2. It gets the right answer for a known line + 0th order continuum.
00017 3. It gets the right answer for a known line + 4th order continuum.
00018 4. It gets the right answer for a known line + 0th order continuum,
00019 even when fitorder = 4.
00020 '''
00021 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/'
00022 uvcdatadir = 'unittest/uvcontsub/'
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 class UVContsub3UnitTestBase(unittest.TestCase):
00054 """
00055 uvcontsub3 unittest base class (refactored)
00056 """
00057 def initialize(self,inpms):
00058 """
00059 initialize
00060 """
00061 self.inpms = inpms
00062 if not os.path.exists('unittest/uvcontsub3'):
00063 os.system('mkdir -p unittest/uvcontsub')
00064
00065 if not os.path.exists(self.inpms):
00066 try:
00067 shutil.copytree(datapath + inpms, inpms)
00068 except Exception, e:
00069 raise Exception, "Missing input MS: " + datapath + inpms
00070
00071 def cleanup(self):
00072 """
00073 clean up the test dir
00074 """
00075 if os.path.exists(self.inpms):
00076 shutil.rmtree(self.inpms)
00077
00078
00079
00080 def check_eq(self, val, expval, tol=None):
00081 """Checks that val matches expval within tol."""
00082 if type(val) == dict:
00083 for k in val:
00084 check_eq(val[k], expval[k], tol)
00085 else:
00086 try:
00087 if tol and hasattr(val, '__rsub__'):
00088 are_eq = abs(val - expval) < tol
00089 else:
00090 are_eq = val == expval
00091 if hasattr(are_eq, 'all'):
00092 are_eq = are_eq.all()
00093 if not are_eq:
00094 raise ValueError, '!='
00095 except ValueError:
00096 errmsg = "%r != %r" % (val, expval)
00097 if (len(errmsg) > 66):
00098 errmsg = "\n%r\n!=\n%r" % (val, expval)
00099 raise ValueError, errmsg
00100 except Exception, e:
00101 print "Error comparing", val, "to", expval
00102 raise e
00103
00104
00105 class zeroth(UVContsub3UnitTestBase):
00106 """Test zeroth order fit"""
00107
00108 def setUp(self):
00109 self.initialize(uvcdatadir+'known0.ms')
00110
00111 def tearDown(self):
00112 self.cleanup()
00113
00114 def test_zeroth(self):
00115
00116 record={}
00117 infitorder = 0
00118
00119 try:
00120 print "\nRunning uvcontsub3"
00121 uvran = uvcontsub3(self.inpms, fitspw='0:0~5;18~23',
00122 fitorder=infitorder, async=False)
00123 except Exception, e:
00124 print "Error running uvcontsub3"
00125 raise e
00126 specms = self.inpms + '.contsub'
00127 tb.open(specms)
00128 record['contsub'] = tb.getcell('DATA', 52)
00129 tb.close()
00130 shutil.rmtree(specms)
00131 self.assertTrue(uvran)
00132
00133 print "Continuum-subtracted data in line-free region"
00134 self.check_eq(record['contsub'][:,21],
00135 numpy.array([ 0.+0.j, 0.+0.j]), 0.0001)
00136 print "Continuum-subtracted data in line region"
00137 self.check_eq(record['contsub'][:,9],
00138 numpy.array([87.+26.j, 31.+20.j]), 0.0001)
00139
00140 class fourth(UVContsub3UnitTestBase):
00141 """Test forth order fit"""
00142
00143 def setUp(self):
00144 self.initialize(uvcdatadir + 'known4.ms')
00145
00146 def tearDown(self):
00147 self.cleanup()
00148
00149 def test_fourth(self):
00150 record = {}
00151
00152 infitorder = 4
00153
00154 try:
00155 print "\nRunning uvcontsub3"
00156 uvran = uvcontsub3(self.inpms, fitspw='0:0~5;18~23',
00157 fitorder=infitorder, async=False)
00158 except Exception, e:
00159 print "Error running uvcontsub3"
00160 raise e
00161 specms = self.inpms + '.contsub'
00162 tb.open(specms)
00163 record['contsub'] = tb.getcell('DATA', 52)
00164 tb.close()
00165 shutil.rmtree(specms)
00166 self.assertTrue(uvran)
00167
00168 print "Continuum-subtracted data"
00169 self.check_eq(record['contsub'],
00170 numpy.array([[0.00000+0.00000j, 0.00000+0.00000j,
00171 0.00000+0.00000j, 0.00000+0.00000j,
00172 0.00000+0.00000j, 0.00000+0.00000j,
00173 30.86956-0.10000j, 34.34782-0.10000j,
00174 37.82608-0.10000j, 41.30434-0.10000j,
00175 44.78260-0.10000j, 48.26086-0.10000j,
00176 48.26086-0.10000j, 44.78260-0.10000j,
00177 41.30434-0.10000j, 37.82608-0.10000j,
00178 34.34782-0.10000j, 30.86956-0.10000j,
00179 0.00000+0.00000j, 0.00000+0.00000j,
00180 0.00000+0.00000j, 0.00000+0.00000j,
00181 0.00000+0.00000j, 0.00000+0.00000j],
00182 [0.00000+0.00000j, 0.00000+0.00000j,
00183 0.00000+0.00000j, 0.00000+0.00000j,
00184 0.00000+0.00000j, 0.00000+0.00000j,
00185 -30.86956+0.10000j, -34.34782+0.10000j,
00186 -37.82608+0.10000j, -41.30434+0.10000j,
00187 -44.78260+0.10000j, -48.26086+0.10000j,
00188 -48.26086+0.10000j, -44.78260+0.10000j,
00189 -41.30434+0.10000j, -37.82608+0.10000j,
00190 -34.34782+0.10000j, -30.86956+0.10000j,
00191 0.00000+0.00000j, 0.00000+0.00000j,
00192 0.00000+0.00000j, 0.00000+0.00000j,
00193 0.00000+0.00000j, 0.00000+0.00000j]]),
00194 0.0001)
00195
00196 class combspw(UVContsub3UnitTestBase):
00197 """Test combine spw"""
00198 def setUp(self):
00199 self.initialize(uvcdatadir + 'combspw.ms')
00200
00201 def tearDown(self):
00202 self.cleanup()
00203
00204 def test_combspw(self):
00205 record = {}
00206 fitorders=[0,1]
00207 for infitorder in fitorders:
00208 record[infitorder]={}
00209 try:
00210 print "\nRunning uvcontsub3"
00211 uvran = uvcontsub3(self.inpms, fitspw='1~10:5~122,15~22:5~122',
00212 spw='6~14', combine='spw',
00213 fitorder=infitorder, async=False)
00214 except Exception, e:
00215 print "Error running uvcontsub3"
00216 raise e
00217
00218 specms = self.inpms + '.contsub'
00219 tb.open(specms)
00220
00221
00222 record[infitorder]['contsub'] = tb.getcell('DATA', 31)[0][73]
00223 tb.close()
00224 shutil.rmtree(specms)
00225 self.assertTrue(uvran)
00226
00227 print "combspw fitorder=0 line estimate"
00228 self.check_eq(record[0]['contsub'], -6.2324+17.9865j, 0.001)
00229
00230 print "combspw fitorder=1 line estimate"
00231 self.check_eq(record[1]['contsub'], -6.2533+17.6584j, 0.001)
00232
00233 class knowncombspw(UVContsub3UnitTestBase):
00234 """Test combine spw with a know line"""
00235
00236 corrsels = [1]
00237 def setUp(self):
00238 self.initialize(uvcdatadir + 'knowncombspw.ms')
00239
00240 def tearDown(self):
00241 self.cleanup()
00242
00243 def test_knowncombspw(self):
00244 record = {}
00245 infitorder = 1
00246
00247 try:
00248 print "\nRunning uvcontsub3"
00249 uvran = uvcontsub3(self.inpms, fitspw='0,1:0~15,3:23~31,4',
00250 spw='1~3', combine='spw',
00251 fitorder=infitorder, async=False)
00252 except Exception, e:
00253 print "Error running uvcontsub3"
00254 raise e
00255 specms = self.inpms + '.contsub'
00256 tb.open(specms)
00257
00258
00259
00260 record['blue'] = tb.getcell('DATA', 7)[0]
00261 record['line'] = tb.getcell('DATA', 37)[0]
00262 record['red'] = tb.getcell('DATA', 61)[0]
00263
00264 tb.close()
00265 shutil.rmtree(specms)
00266 self.assertTrue(uvran)
00267
00268 print "The blueward side"
00269 self.check_eq(record['blue'],
00270 numpy.array([ -3.03268433e-04 +1.00890160e-01j,
00271 -3.02314758e-04 +9.34381485e-02j,
00272 -3.00884247e-04 +8.59861374e-02j,
00273 -2.99692154e-04 +7.85341263e-02j,
00274 -2.98261642e-04 +7.10821152e-02j,
00275 -2.96354294e-04 +6.36301041e-02j,
00276 -2.92539597e-04 +5.61790466e-02j,
00277 -2.84433365e-04 +4.87308502e-02j,
00278 -2.66551971e-04 +4.12874222e-02j,
00279 -2.25067139e-04 +3.38554382e-02j,
00280 -1.28984451e-04 +2.64511108e-02j,
00281 8.96453857e-05 +1.91068649e-02j,
00282 5.75304031e-04 +1.18970871e-02j,
00283 1.62601471e-03 +4.96959686e-03j,
00284 3.83996964e-03 -1.37615204e-03j,
00285 8.37826729e-03 -6.55937195e-03j,
00286 1.74312592e-02 -9.48524475e-03j,
00287 3.49960327e-02 -8.15582275e-03j,
00288 6.81409836e-02 +9.63211060e-04j,
00289 1.28964663e-01 +2.39229202e-02j,
00290 2.37491846e-01 +7.07340240e-02j,
00291 4.25746441e-01 +1.57408714e-01j,
00292 7.43155241e-01 +3.08660507e-01j,
00293 1.26323771e+00 +5.61248779e-01j,
00294 2.09118795e+00 +9.67771530e-01j,
00295 3.37145138e+00 +1.60044956e+00j,
00296 5.29375315e+00 +2.55414772e+00j,
00297 8.09537888e+00 +3.94750881e+00j,
00298 1.20570116e+01 +5.92087221e+00j,
00299 1.74892941e+01 +8.62956047e+00j,
00300 2.47078934e+01 +1.22314072e+01j,
00301 3.39962006e+01 +1.68681087e+01j]), 0.001)
00302
00303 print "The known line"
00304 self.check_eq(record['line'],
00305 numpy.array([ 1.53886628 +0.8961916j , 2.52163553 +1.38012409j,
00306 4.02423143 +2.12396812j, 6.25472546 +3.23176193j,
00307 9.46806622 +4.83097982j, 13.95866108 +7.06882477j,
00308 20.04272079+10.10340309j, 28.02855110+14.08886528j,
00309 38.17472839+19.15450096j, 50.63872910+25.37904739j,
00310 65.42163086+32.76304626j, 82.31739807+41.20347595j,
00311 100.87736511+50.47600555j, 120.40031433+60.23003006j,
00312 139.95661926+70.00072479j, 158.44956970+79.23975372j,
00313 174.71115112+87.36308289j, 187.62126160+93.8106842j ,
00314 196.23449707+98.10986328j, 199.89437866+99.93234253j,
00315 198.31597900+99.13568878j, 191.62258911+95.78153992j,
00316 180.32984924+90.12772369j, 165.28004456+82.59536743j,
00317 147.53840637+73.71709442j, 128.26898193+64.07492828j,
00318 108.61006165+54.23802185j, 89.56748199+44.70927429j,
00319 71.93868256+35.88742447j, 56.27381897+28.04753876j,
00320 42.87281799+21.33958817j, 31.81188011+15.80166435j]),
00321 0.001)
00322
00323 print "The redward side"
00324 self.check_eq(record['red'],
00325 numpy.array([ 1.23712059e+02 +6.19947433e+01j,
00326 1.04113014e+02 +5.21877708e+01j,
00327 8.53355255e+01 +4.27915688e+01j,
00328 6.81218643e+01 +3.41772842e+01j,
00329 5.29632835e+01 +2.65905476e+01j,
00330 4.01046829e+01 +2.01537933e+01j,
00331 2.95765038e+01 +1.48822517e+01j,
00332 2.12436962e+01 +1.07083941e+01j,
00333 1.48608809e+01 +7.50953341e+00j,
00334 1.01248760e+01 +5.13407898e+00j,
00335 6.71838522e+00 +3.42338037e+00j,
00336 4.34179068e+00 +2.22763014e+00j,
00337 2.73274589e+00 +1.41565514e+00j,
00338 1.67514348e+00 +8.79401207e-01j,
00339 1.00004458e+00 +5.34399033e-01j,
00340 5.81416249e-01 +3.17632675e-01j,
00341 3.29175472e-01 +1.84060097e-01j,
00342 1.81462049e-01 +1.02749825e-01j,
00343 9.73758698e-02 +5.32541275e-02j,
00344 5.08389473e-02 +2.25324631e-02j,
00345 2.57951021e-02 +2.55870819e-03j,
00346 1.26885176e-02 -1.14479065e-02j,
00347 6.01756573e-03 -2.22358704e-02j,
00348 2.71499157e-03 -3.13396454e-02j,
00349 1.12497807e-03 -3.95870209e-02j,
00350 3.80396843e-04 -4.74128723e-02j,
00351 4.14252281e-05 -5.50346375e-02j,
00352 -1.08182430e-04 -6.25619888e-02j,
00353 -1.72197819e-04 -7.00464249e-02j,
00354 -1.98423862e-04 -7.75127411e-02j,
00355 -2.08497047e-04 -8.49704742e-02j,
00356 -2.11834908e-04 -9.24243927e-02j]), 0.001)
00357
00358 def suite():
00359 return [zeroth, fourth, combspw, knowncombspw]