00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 import sha
00009 import time
00010 import numpy
00011 import re
00012 import string
00013
00014 from msmoments import msmoments
00015
00016
00017
00018
00019
00020
00021
00022
00023 class msmoments_unittest_base:
00024 """
00025 Base class for msmoments unit test
00026 """
00027 taskname='msmoments'
00028 datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/msmoments/'
00029 place=5
00030
00031 def _compare(self,tab,ref,unit):
00032 cols=tab.getcolkeywords('FLOAT_DATA')
00033 if cols.has_key('QuantumUnits'):
00034 cunit=cols['QuantumUnits']
00035 else:
00036 cunit=cols['UNIT']
00037 self.assertEqual(cunit,unit,
00038 msg='Unit %s is wrong (should be %s)'%(cunit,unit))
00039 sp=tab.getcell('FLOAT_DATA',0)
00040 self.assertEqual(len(sp[0]),1,
00041 msg='moment must be 1 per RR sectrum')
00042 if sp[0][0] > 10.0:
00043 while(sp[0][0]>10.0):
00044 sp[0][0] /= 10.0
00045 ref[0] /= 10.0
00046 elif sp[0][0] < 1.0:
00047 while(sp[0][0]<1.0):
00048 sp[0][0] *= 10.0
00049 ref[0] *= 10.0
00050
00051 self.assertAlmostEqual(sp[0][0],ref[0],self.place,
00052 msg='RR value differ.')
00053 if sp[1][0] > 10.0:
00054 while(sp[1][0]>10.0):
00055 sp[1][0] /= 10.0
00056 ref[1] /= 10.0
00057 elif sp[1][0] < 1.0:
00058 while(sp[1][0]<1.0):
00059 sp[1][0] *= 10.0
00060 ref[1] *= 10.0
00061
00062 self.assertEqual(len(sp[1]),1,
00063 msg='moment must be 1 per LL sectrum')
00064 self.assertAlmostEqual(sp[1][0],ref[1],self.place,
00065 msg='LL value differ.')
00066 tab.close()
00067
00068
00069
00070
00071
00072 class msmoments_test0(unittest.TestCase,msmoments_unittest_base):
00073 """
00074 Test on bad parameter setting
00075
00076 """
00077
00078 rawfile='msmoments0.ms'
00079 prefix=msmoments_unittest_base.taskname+'Test0'
00080 outfile=prefix+'.ms'
00081
00082 def setUp(self):
00083 self.res=None
00084 if (not os.path.exists(self.rawfile)):
00085 shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00086
00087 default(msmoments)
00088
00089 def tearDown(self):
00090 if (os.path.exists(self.rawfile)):
00091 shutil.rmtree(self.rawfile)
00092 os.system( 'rm -rf '+self.prefix+'*' )
00093
00094 def test000(self):
00095 """Test 000: Default parameters"""
00096 res=msmoments()
00097 self.assertFalse(res)
00098
00099 def test001(self):
00100 """Test 001: Undefined moment"""
00101 moments=[-2]
00102 try:
00103 msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00104 self.assertTrue(False,
00105 msg='The task must throw exception')
00106 except StandardError, e:
00107 pos=str(e).find('Illegal moment requested')
00108 self.assertNotEqual(pos,-1,
00109 msg='Unexpected exception was thrown: %s'%(str(e)))
00110 except Exception, e:
00111 self.assertTrue(False,
00112 msg='Unexpected exception was thrown: %s'%(str(e)))
00113
00114 def test002(self):
00115 """Test 002: Illegal antenna id"""
00116 antenna=-2
00117
00118
00119
00120 try:
00121 msmoments(infile=self.rawfile,antenna=antenna,outfile=self.outfile)
00122 self.assertTrue(False,
00123 msg='The task must throw exception')
00124 except StandardError, e:
00125 pos=str(e).find('Antenna Expression: Parse error')
00126 self.assertNotEqual(pos,-1,
00127 msg='Unexpected exception was thrown: %s'%(str(e)))
00128 except Exception, e:
00129 self.assertTrue(False,
00130 msg='Unexpected exception was thrown: %s'%(str(e)))
00131
00132 def test003(self):
00133 """Test 003: Illegal field name"""
00134 field='SOMEWHARE'
00135
00136
00137
00138 try:
00139 msmoments(infile=self.rawfile,field=field,outfile=self.outfile)
00140 self.assertTrue(False,
00141 msg='The task must throw exception')
00142 except StandardError, e:
00143 pos=str(e).find('Field Expression: No match found')
00144 self.assertNotEqual(pos,-1,
00145 msg='Unexpected exception was thrown: %s'%(str(e)))
00146 except Exception, e:
00147 self.assertTrue(False,
00148 msg='Unexpected exception was thrown: %s'%(str(e)))
00149
00150 def test004(self):
00151 """Test 004: Illegal spw id"""
00152 id=99
00153
00154
00155
00156 try:
00157 msmoments(infile=self.rawfile,spw=id,outfile=self.outfile)
00158 self.assertTrue(False,
00159 msg='The task must throw exception')
00160 except StandardError, e:
00161 pos=str(e).find('No Spw ID(s) matched')
00162 self.assertNotEqual(pos,-1,
00163 msg='Unexpected exception was thrown: %s'%(str(e)))
00164 except Exception, e:
00165 self.assertTrue(False,
00166 msg='Unexpected exception was thrown: %s'%(str(e)))
00167
00168 def test005(self):
00169 """Test 005: Existing output file"""
00170 shutil.copytree(self.datapath+self.rawfile, self.outfile)
00171 try:
00172 msmoments(infile=self.rawfile,outfile=self.outfile)
00173 self.assertTrue(False,
00174 msg='The task must throw exception')
00175 except Exception, e:
00176 self.assertEqual(str(e),'%s exists.'%(self.outfile),
00177 msg='Unexpected exception was thrown.')
00178
00179
00180
00181
00182 class msmoments_test1(unittest.TestCase,msmoments_unittest_base):
00183 """
00184 Test to calculate moments.
00185
00186 Original data is OrionS_rawACSmod.
00187 The data is calibrated and averaged to get good example
00188 for moment calculation.
00189
00190 """
00191
00192 rawfile='msmoments0.ms'
00193 prefix=msmoments_unittest_base.taskname+'Test1'
00194 outfile=prefix+'.ms'
00195
00196 def setUp(self):
00197 self.res=None
00198 if (not os.path.exists(self.rawfile)):
00199 shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00200
00201 default(msmoments)
00202
00203 def tearDown(self):
00204 if (os.path.exists(self.rawfile)):
00205 shutil.rmtree(self.rawfile)
00206 os.system( 'rm -rf '+self.prefix+'*' )
00207
00208 def test100(self):
00209 """Test 100: Mean"""
00210 moments=[-1]
00211 postfix='.average'
00212 refval=[2.8974850177764893,2.6505289077758789]
00213 if (os.path.exists(self.outfile)):
00214 shutil.rmtree(self.outfile)
00215 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00216
00217 self.assertEqual(type(res),type(tb),
00218 msg='Any error occurred during task execution')
00219 self.assertEqual(res.name().split('/')[-1],self.outfile,
00220 msg='Returned table is wrong.')
00221 self._compare(res,refval,'K')
00222
00223 def test101(self):
00224 """Test 101: Integrated intensity"""
00225 moments=[0]
00226 postfix='.integrated'
00227 refval=[954.87066650390625,873.48583984375]
00228 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00229
00230 self.assertEqual(type(res),type(tb),
00231 msg='Any error occurred during task execution')
00232 self.assertEqual(res.name().split('/')[-1],self.outfile,
00233 msg='Returned table is wrong.')
00234 self._compare(res,refval,'K.km/s')
00235
00236 def test102(self):
00237 """Test 102: Weighted coordinate (velocity field)"""
00238 moments=[1]
00239 postfix='.weighted_coord'
00240 refval=[6.8671870231628418,7.6601519584655762]
00241 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00242
00243 self.assertEqual(type(res),type(tb),
00244 msg='Any error occurred during task execution')
00245 self.assertEqual(res.name().split('/')[-1],self.outfile,
00246 msg='Returned table is wrong.')
00247 self._compare(res,refval,'km/s')
00248
00249 def test103(self):
00250 """Test 103: Weighted dispersion of coordinate (velocity dispersion)"""
00251 moments=[2]
00252 postfix='.weighted_dispersion_coord'
00253 refval=[95.011787414550781,95.257194519042969]
00254 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00255
00256 self.assertEqual(type(res),type(tb),
00257 msg='Any error occurred during task execution')
00258 self.assertEqual(res.name().split('/')[-1],self.outfile,
00259 msg='Returned table is wrong.')
00260 self._compare(res,refval,'km/s')
00261
00262 def test104(self):
00263 """Test 104: Median"""
00264 moments=[3]
00265 postfix='.median'
00266 refval=[2.8934342861175537,2.6451926231384277]
00267 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00268
00269 self.assertEqual(type(res),type(tb),
00270 msg='Any error occurred during task execution')
00271 self.assertEqual(res.name().split('/')[-1],self.outfile,
00272 msg='Returned table is wrong.')
00273 self._compare(res,refval,'K')
00274
00275 def test105(self):
00276 """Test 105: Median coordinate (fail at the moment)"""
00277 moments=[4]
00278 postfix='.median_coordinate'
00279 refval=[]
00280 try:
00281 res=msmoments(infile=self.rawfile,spw=id,moments=moments,outfile=self.outfile)
00282 res.close()
00283 self.assertTrue(False,
00284 msg='The task must throw exception')
00285 except StandardError, e:
00286 pos=str(e).find('moment not allowed to calculate')
00287 self.assertNotEqual(pos,-1,
00288 msg='Unexpected exception was thrown: %s'%(str(e)))
00289 except Exception, e:
00290 self.assertTrue(False,
00291 msg='Unexpected exception was thrown: %s'%(str(e)))
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 def test106(self):
00302 """Test 106: Standard deviation about the mean"""
00303 moments=[5]
00304 postfix='.standard_deviation'
00305 refval=[0.13466399908065796,0.13539622724056244]
00306 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00307
00308 self.assertEqual(type(res),type(tb),
00309 msg='Any error occurred during task execution')
00310 self.assertEqual(res.name().split('/')[-1],self.outfile,
00311 msg='Returned table is wrong.')
00312 self._compare(res,refval,'K')
00313
00314 def test107(self):
00315 """Test 107: Rms"""
00316 moments=[6]
00317 postfix='.rms'
00318 refval=[2.9006123542785645, 2.6539843082427979]
00319 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00320
00321 self.assertEqual(type(res),type(tb),
00322 msg='Any error occurred during task execution')
00323 self.assertEqual(res.name().split('/')[-1],self.outfile,
00324 msg='Returned table is wrong.')
00325 self._compare(res,refval,'K')
00326
00327 def test108(self):
00328 """Test 108: Absolute mean deviation"""
00329 moments=[7]
00330 postfix='.abs_mean_dev'
00331 refval=[0.09538549929857254, 0.10068970918655396]
00332 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00333
00334 self.assertEqual(type(res),type(tb),
00335 msg='Any error occurred during task execution')
00336 self.assertEqual(res.name().split('/')[-1],self.outfile,
00337 msg='Returned table is wrong.')
00338 self._compare(res,refval,'K')
00339
00340 def test109(self):
00341 """Test 109: Maximum value"""
00342 moments=[8]
00343 postfix='.maximum'
00344 refval=[3.9766585826873779, 3.7296187877655029]
00345 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00346
00347 self.assertEqual(type(res),type(tb),
00348 msg='Any error occurred during task execution')
00349 self.assertEqual(res.name().split('/')[-1],self.outfile,
00350 msg='Returned table is wrong.')
00351 self._compare(res,refval,'K')
00352
00353 def test110(self):
00354 """Test 110: Coordinate of maximum value"""
00355 moments=[9]
00356 postfix='.maximum_Coord'
00357 refval=[45489389568.0, 45489410048.0]
00358 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00359
00360 self.assertEqual(type(res),type(tb),
00361 msg='Any error occurred during task execution')
00362 self.assertEqual(res.name().split('/')[-1],self.outfile,
00363 msg='Returned table is wrong.')
00364 self._compare(res,refval,'km/s')
00365
00366 def test111(self):
00367 """Test 111: Minimum value"""
00368 moments=[10]
00369 postfix='.minimum'
00370 refval=[1.7176277637481689, 1.4913345575332642]
00371 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00372
00373 self.assertEqual(type(res),type(tb),
00374 msg='Any error occurred during task execution')
00375 self.assertEqual(res.name().split('/')[-1],self.outfile,
00376 msg='Returned table is wrong.')
00377 self._compare(res,refval,'K')
00378
00379 def test112(self):
00380 """Test 112: Coordinate of minimum value"""
00381 moments=[11]
00382 postfix='.minimum_coord'
00383 refval=[45514190848.0, 45464350720.0]
00384 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00385
00386 self.assertEqual(type(res),type(tb),
00387 msg='Any error occurred during task execution')
00388 self.assertEqual(res.name().split('/')[-1],self.outfile,
00389 msg='Returned table is wrong.')
00390 self._compare(res,refval,'km/s')
00391
00392 def test113(self):
00393 """Test 113: Multiple moments (mean + velocity field)"""
00394 moments=[-1,1]
00395 postfix=['.average','.weighted_coord']
00396 refval0=[2.8974850177764893,2.6505289077758789]
00397 refval1=[6.8671870231628418,7.6601519584655762]
00398 res=msmoments(infile=self.rawfile,moments=moments,outfile=self.outfile)
00399
00400 self.assertEqual(type(res),type(tb),
00401 msg='Any error occurred during task execution')
00402
00403 for imom in range(len(moments)):
00404 name='./'+self.outfile+postfix[imom]
00405 self.assertEqual(os.path.exists(name),True,
00406 msg='%s must exist'%(name))
00407
00408 self.assertEqual(res.name().split('/')[-1],self.outfile+postfix[0],
00409 msg='Returned table is wrong.')
00410 self._compare(res,refval0,'K')
00411
00412 tb.open(self.outfile+postfix[1])
00413 self._compare(tb,refval1,'km/s')
00414
00415 def suite():
00416 return [msmoments_test0,msmoments_test1]