casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdstat.py
Go to the documentation of this file.
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 #
00009 import listing
00010 import numpy
00011 from numpy import array
00012 
00013 import asap as sd
00014 from sdstat import sdstat
00015 
00016 class sdstat_unittest_base:
00017     """
00018     Base class for sdstat unit test
00019     """
00020     # Data path of input/output
00021     datapath = os.environ.get('CASAPATH').split()[0] + \
00022                '/data/regression/unittest/sdstat/'
00023     taskname = "sdstat"
00024     outroot = taskname+'_test'
00025     outsuff = ".out"
00026     infile = 'OrionS_rawACSmod_calTPave.asap'
00027 
00028     ### helper functions for tests ###
00029     def _checkfile( self, name ):
00030         isthere=os.path.exists(name)
00031         self.assertTrue(isthere,
00032                          msg='output file %s was not created because of the task failure'%(name))
00033 
00034     def _convert_masklist( self, chanlist, unit, filename, spw=0, restfreq = '' ):
00035         """
00036         Convert a masklist from channel unit to a specified unit
00037             chanlist : masklist in channel unit
00038             unit     : an output unit of masklist
00039             filename : a file name to get spectral coordinate from
00040             spw      : spectral window ID to get spectral coordinate
00041         """
00042         self._checkfile(filename)
00043         vallist = []
00044         scan = sd.scantable(filename, average=False)
00045         oldunit = scan.get_unit()
00046         self.assertTrue(spw in scan.getifnos(), "IF=%d does not exists in %s." % (spw, filename))
00047         scan.set_unit(unit)
00048         scan.set_selection(ifs=[spw])
00049         if restfreq != '':
00050             molids = scan._getmolidcol_list()
00051             scan.set_restfreqs(restfreq)
00052         chanval = scan._getabcissa(0)
00053         scan.set_unit(oldunit)
00054         if restfreq != '':
00055             scan._setmolidcol_list(molids)
00056             del molids
00057         del scan, oldunit
00058         for schan, echan in chanlist:
00059             vallist.append([chanval[schan],chanval[echan]])
00060         return vallist
00061 
00062     def _isInAllowedRange( self, testval, refval, allowdiff=1.e-5 ):
00063         """
00064         Check if a test value is within permissive relative difference from refval.
00065         Returns a boolean.
00066         testval & refval : two numerical values to compare
00067         allowdiff        : allowed relative difference to consider the two
00068                            values to be equal. (default 0.01)
00069         """
00070         denom = refval
00071         if refval == 0:
00072             if testval == 0:
00073                 return True
00074             else:
00075                 denom = testval
00076         rdiff = (testval-refval)/denom
00077         del denom,testval,refval
00078         return (abs(rdiff) <= allowdiff)
00079 
00080     def _to_list( self, input ):
00081         """
00082         Convert input to a list
00083         If input is None, this method simply returns None.
00084         """
00085         listtypes = (list, tuple, numpy.ndarray)
00086         if input == None:
00087             return None
00088         elif type(input) in listtypes:
00089             return list(input)
00090         else:
00091             return [input]
00092 
00093     def _get_elements( self, fulllist, elems=None ):
00094         """
00095         Returns a list of selected elements from an input list
00096             fulllist  : an input list
00097             elems     : a list of element IDs to return
00098         """
00099         self.assertTrue(type(fulllist) in (list,tuple,numpy.ndarray), \
00100                         "Input array should be either a list or tuple")
00101         if elems is not None:
00102             elems = self._to_list(elems)
00103         else:
00104             # No selection
00105             return fulllist
00106         # Empty elems=[]
00107         self.assertTrue(len(elems) > 0, "No index specified.")
00108         # Check range of elems
00109         if (max(elems) > len(fulllist) - 1) or (min(elems) < 0):
00110             raise Exception("Indices out of range: %s\nShould be in 0 - %d" % \
00111                             (str(elems), len(fulllist) - 1))
00112         sellist = []
00113         for id in elems:
00114             sellist.append(fulllist[id])
00115         return sellist
00116 
00117     def _compareStats( self, currstat, refstat, icomp=None, allowdiff=1.e-5, compstats=None ):
00118         # compare statistic values
00119         if compstats:
00120             compstats = self._to_list(compstats)
00121         else:
00122             compstats = refstat.keys()
00123 
00124         # Task sdstat returns a dictionary of statistic values
00125         self.assertTrue(isinstance(refstat,dict),
00126                          msg="The referece statistics are not a dictionary")
00127         self.assertTrue(isinstance(currstat,dict),
00128                          msg="The returned statistics are not a dictionary")
00129         for stat in compstats:
00130             self.assertTrue(refstat.has_key(stat),
00131                             msg = "'%s' is not defined in reference data" % stat)
00132             self.assertTrue(currstat.has_key(stat),
00133                             msg = "'%s' is not defined in the current run" % stat)
00134             refval = refstat[stat]
00135             currval = currstat[stat]
00136             # Quantum values
00137             if isinstance(refval,dict):
00138                 if refval.has_key('unit') and currval.has_key('unit'):
00139                     self.assertEqual(refval['unit'],currval['unit'],\
00140                                      "The units differ in '%s' %s (current run), %s (reference)" % \
00141                                      (stat, currval['unit'], refval['unit']))
00142                     #print "Comparing unit of '%s': %s (current run), %s (reference)" % \
00143                     #      (stat,currval['unit'],refval['unit'])
00144                     refval = refval['value']
00145                     currval = currval['value']
00146                 else:
00147                     raise Exception("Invalid quantum values. %s (current run) %s (reference)" %\
00148                                     (str(currval),str(refval)))
00149             refval = self._to_list(refval)
00150             currval = self._to_list(currval)
00151             if icomp is not None:
00152                 refval = self._get_elements(refval,icomp)
00153             #print "Comparing '%s': %s (current run), %s (reference)" % \
00154             #      (stat,str(currval),str(refval))
00155             self.assertTrue(len(currval)==len(refval),"Number of elemnets differs.")
00156             for i in range(len(currval)):
00157                 self.assertTrue(self._isInAllowedRange(currval[i], refval[i], allowdiff),
00158                                 msg="'%s' of spectrum %d is different." % (stat, i))
00159         
00160 
00161     def _compareFiles( self, testfile, reffile ):
00162         # test if baseline parameters are equal to the reference values
00163         # currently comparing every lines in the files
00164         self.assertTrue(os.path.exists(testfile),
00165                         msg=("Output file '%s' doesn't exist" % testfile))
00166         self.assertTrue(os.path.exists(reffile),
00167                         msg=("Reference file '%s' doesn't exist: " % reffile))
00168         self.assertTrue(listing.compare(testfile,reffile),
00169                         'New and reference files are different. %s != %s. '
00170                         %(testfile,reffile))
00171         
00172 
00173 
00174 class sdstat_basicTest( sdstat_unittest_base, unittest.TestCase ):
00175     """
00176     Basic unit tests for task sdstat. No interactive testing.
00177 
00178     The list of tests:
00179     test00    --- default parameters (raises an errror)
00180     test01    --- default + valid input filename
00181     test02-03 --- available fluxunits
00182     test04-06 --- available specunits
00183     test07-09 --- masklist in various specunit
00184     test10    --- invertmask=True
00185     test11    --- format
00186 
00187     Note: input data is generated from a single dish regression data,
00188     'OrionS_rawACSmod', as follows:
00189       default(sdcal)
00190       sdcal(infile='OrionS_rawACSmod',scanlist=[20,21,22,23],
00191                 calmode='ps',tau=0.09,outfile='temp.asap')
00192       default(sdcal)
00193       sdcal(infile='temp.asap',timeaverage=True,tweight='tintsys',
00194                 polaverage=True,pweight='tsys',outfile=self.infile)
00195     """
00196     ### TODO:
00197     ### - need checking for flag application
00198     ### - comparison with simple spectral
00199 
00200     #compVstats = ['max','min','mean','sum','rms','median','stddev']
00201     # Line channels
00202     linechan0 = [[3999,4144]]
00203     linechan2 = [[2951,3088]]
00204     #### Reference data (from ASAP r2084 + CASA r14498, Mar. 31, 2011)
00205     # Reference statistic values (masklist=[], invertmask=False)
00206     ref_allK = {'rms': [4.1249432563781738, 4.0882534980773926, 4.4454779624938965, 4.1502971649169922],
00207                 'min': [-6.5844717025756836, 0.6394113302230835, -92.782661437988281, 1.9847347736358643],
00208                 'max': [12.278024673461914, 5.8824653625488281, 60.68634033203125, 5.2238655090332031],
00209                 'median': [4.1043367385864258, 4.0837831497192383, 4.1507611274719238, 4.1468963623046875],
00210                 'stddev': [0.2636776864528656, 0.1379869282245636, 1.5120075941085815, 0.1202411949634552],
00211                 'sum': [33722.40234375, 33471.87109375, 34246.4453125, 33984.94140625],
00212                 'mean': [4.1165041923522949, 4.0859217643737793, 4.1804742813110352, 4.148552417755127]}
00213 
00214     ref_allJy = {'rms': [3.2270569801330566, 3.1983535289764404, 3.4778206348419189, 3.24688720703125],
00215                  'min': [-5.1512141227722168, 0.50022917985916138, -72.586441040039062, 1.5527129173278809],
00216                  'max': [9.605438232421875, 4.602015495300293, 47.476600646972656, 4.0867743492126465],
00217                  'median': [3.2109360694885254, 3.1948564052581787, 3.2472553253173828, 3.2442317008972168],
00218                  'stddev': [0.20628249645233154, 0.10795092582702637, 1.1828852891921997, 0.094067998230457306],
00219                  'sum': [26381.958984375, 26186.046875, 26791.974609375, 26587.322265625],
00220                  'mean': [3.2204539775848389, 3.1965389251708984, 3.2705047130584717, 3.2455227375030518]}
00221 
00222     minmaxchan_all = {'max_abscissa': {'value': array([   21.,  8177.,  8186.,  8159.]), 'unit': 'channel'},
00223                       'min_abscissa': {'value': array([   18.,     0.,  8187.,     0.]), 'unit': 'channel'}}
00224 
00225     minmaxfreq_all = {'max_abscissa': {'value': array([ 45.46447881,  45.32569678,  44.09989566,  44.19101657]),
00226                                        'unit': 'GHz'},
00227                       'min_abscissa': {'value': array([ 45.4644605 ,  45.27578247,  44.09990176,  44.14121213]),
00228                                        'unit': 'GHz'}}
00229     minmaxvelo_all = {'max_abscissa': {'value': array([  169.89144245,  1084.50062449,  9162.84415004,  8562.33397937]),
00230                                        'unit': 'km/s'},
00231                       'min_abscissa': {'value': array([  170.01212783,  1413.44873913,  9162.80392158,  8890.55798174]),
00232                                        'unit': 'km/s'}}
00233 
00234     # Reference line statistic values (masklist=linechan2, invertmask=False)
00235     ref_line2 = {'rms': 5.0687642097473145, 'min': 3.9442729949951172,
00236                  'max': 6.1298322677612305, 'median': 4.9097409248352051,
00237                  'stddev': 0.57094955444335938, 'sum': 695.0701904296875,
00238                  'mean': 5.0367403030395508}
00239     minmaxchan_line2 = {'max_abscissa': {'value': 3048.0, 'unit': 'channel'}, 
00240                         'min_abscissa': {'value': 2951.0, 'unit': 'channel'}}
00241 
00242     # Reference baseline statistic values (masklist=linechan0, invertmask=True)
00243     ref_bl0 = {'rms': 4.1130456924438477, 'min': -6.5844717025756836,
00244                'max': 12.278024673461914, 'median': 4.1013059616088867,
00245                'stddev': 0.24747852981090546,'sum': 33033.6015625,
00246                'mean': 4.105593204498291}
00247     minmaxchan_bl0 = {'max_abscissa': {'value': 21.0, 'unit': 'channel'},
00248                       'min_abscissa': {'value': 18.0, 'unit': 'channel'}}
00249 
00250     ### Actual test scripts ###
00251     def setUp( self ):
00252         if os.path.exists(self.infile):
00253             shutil.rmtree(self.infile)
00254         shutil.copytree(self.datapath+self.infile, self.infile)
00255 
00256         default(sdstat)
00257 
00258     def tearDown( self ):
00259         if (os.path.exists(self.infile)):
00260             shutil.rmtree(self.infile)
00261 
00262     def test00( self ):
00263         """Test 0: Default parameters"""
00264         result = sdstat()
00265         self.assertFalse(result)
00266 
00267     def test01( self ):
00268         """Test 1: Default parameters + valid input filename """
00269         tid="01"
00270         outfile = self.outroot+tid+self.outsuff
00271 
00272         currstat = sdstat(infile=self.infile,outfile=outfile)
00273         # print "Statistics out of the current run:\n",currstat
00274 
00275         # Task sdstat returns a dictionary of statistic values
00276         self.assertTrue(isinstance(currstat,dict),
00277                          msg="The returned statistics are not a dictionary")
00278         self._compareStats(currstat,self.ref_allK)
00279         self._compareStats(currstat,self.minmaxchan_all)
00280 
00281 
00282     def test02( self ):
00283         """Test 2: fluxunit='K' """
00284         tid="02"
00285         fluxunit = 'K'
00286         # automatic conversion for GBT data
00287         telescopeparm = ""
00288         outfile = self.outroot+tid+self.outsuff
00289 
00290         currstat = sdstat(infile=self.infile,fluxunit=fluxunit,telescopeparm=telescopeparm,outfile=outfile)
00291         # print "Statistics out of the current run:\n",currstat
00292 
00293         # Task sdstat returns a dictionary of statistic values
00294         self.assertTrue(isinstance(currstat,dict),
00295                          msg="The returned statistics are not a dictionary")
00296         self._compareStats(currstat,self.ref_allK)
00297         self._compareStats(currstat,self.minmaxchan_all)
00298 
00299 
00300     def test03( self ):
00301         """Test 3: fluxunit='Jy' """
00302         tid="03"
00303         fluxunit = 'Jy'
00304         # automatic conversion for GBT data
00305         telescopeparm = ""
00306         outfile = self.outroot+tid+self.outsuff
00307 
00308         currstat = sdstat(infile=self.infile,fluxunit=fluxunit,telescopeparm=telescopeparm,outfile=outfile)
00309         # print "Statistics out of the current run:\n",currstat
00310 
00311         # Task sdstat returns a dictionary of statistic values
00312         self.assertTrue(isinstance(currstat,dict),
00313                          msg="The returned statistics are not a dictionary")
00314         self._compareStats(currstat,self.ref_allJy)
00315         self._compareStats(currstat,self.minmaxchan_all)
00316 
00317 
00318     def test04( self ):
00319         """Test 4: specunit='channel' """
00320         tid="04"
00321         specunit = 'channel'
00322         outfile = self.outroot+tid+self.outsuff
00323 
00324         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile)
00325         # print "Statistics out of the current run:\n",currstat
00326 
00327         # Task sdstat returns a dictionary of statistic values
00328         self.assertTrue(isinstance(currstat,dict),
00329                          msg="The returned statistics are not a dictionary")
00330         self._compareStats(currstat,self.ref_allK)
00331         self._compareStats(currstat,self.minmaxchan_all)
00332 
00333 
00334     def test05( self ):
00335         """Test 5: specunit='GHz' """
00336         tid="05"
00337         specunit = 'GHz'
00338         outfile = self.outroot+tid+self.outsuff
00339 
00340         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile)
00341         # print "Statistics out of the current run:\n",currstat
00342 
00343         # Task sdstat returns a dictionary of statistic values
00344         self.assertTrue(isinstance(currstat,dict),
00345                          msg="The returned statistics are not a dictionary")
00346         self._compareStats(currstat,self.ref_allK)
00347         self._compareStats(currstat,self.minmaxfreq_all)
00348 
00349 
00350     def test06( self ):
00351         """Test 6: specunit='km/s' """
00352         tid="06"
00353         specunit = 'km/s'
00354         outfile = self.outroot+tid+self.outsuff
00355 
00356         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile)
00357         # print "Statistics out of the current run:\n",currstat
00358 
00359         # Task sdstat returns a dictionary of statistic values
00360         self.assertTrue(isinstance(currstat,dict),
00361                          msg="The returned statistics are not a dictionary")
00362         self._compareStats(currstat,self.ref_allK)
00363         self._compareStats(currstat,self.minmaxvelo_all)
00364 
00365 
00366     def test07( self ):
00367         """Test 7: maskllist (line) in specunit='channel' """
00368         tid="07"
00369         outfile = self.outroot+tid+self.outsuff
00370         iflist = [2]
00371         specunit = 'channel'
00372 
00373         masklist = self.linechan2
00374 
00375         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00376                           iflist=iflist,masklist=masklist)
00377         # print "Statistics out of the current run:\n",currstat
00378 
00379         # Task sdstat returns a dictionary of statistic values
00380         self.assertTrue(isinstance(currstat,dict),
00381                          msg="The returned statistics are not a dictionary")
00382         self._compareStats(currstat,self.ref_line2)
00383         self._compareStats(currstat,self.minmaxchan_line2)
00384 
00385 
00386     def test08( self ):
00387         """Test 8: maskllist (line) in specunit='GHz' """
00388         tid="08"
00389         outfile = self.outroot+tid+self.outsuff
00390         iflist = [2]
00391         specunit = 'GHz'
00392 
00393         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0])
00394         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00395                           iflist=iflist,masklist=masklist)
00396         # print "Statistics out of the current run:\n",currstat
00397 
00398         # Task sdstat returns a dictionary of statistic values
00399         self.assertTrue(isinstance(currstat,dict),
00400                          msg="The returned statistics are not a dictionary")
00401         self._compareStats(currstat,self.ref_line2)
00402 
00403 
00404     def test09( self ):
00405         """Test 9: maskllist (line) in specunit='km/s' """
00406         tid="09"
00407         outfile = self.outroot+tid+self.outsuff
00408         iflist = [2]
00409         specunit = 'km/s'
00410 
00411         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0])
00412         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00413                           iflist=iflist,masklist=masklist)
00414         # print "Statistics out of the current run:\n",currstat
00415 
00416         # Task sdstat returns a dictionary of statistic values
00417         self.assertTrue(isinstance(currstat,dict),
00418                          msg="The returned statistics are not a dictionary")
00419         self._compareStats(currstat,self.ref_line2)
00420 
00421 
00422     def test10( self ):
00423         """Test 10: invert = True"""
00424         tid="10"
00425         outfile = self.outroot+tid+self.outsuff
00426         iflist = [0]
00427         specunit = 'channel'
00428 
00429         masklist = self.linechan0
00430         invert = True
00431 
00432         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00433                           iflist=iflist,masklist=masklist,invertmask=invert)
00434         # print "Statistics out of the current run:\n",currstat
00435 
00436         # Task sdstat returns a dictionary of statistic values
00437         self.assertTrue(isinstance(currstat,dict),
00438                          msg="The returned statistics are not a dictionary")
00439         self._compareStats(currstat,self.ref_bl0)
00440         self._compareStats(currstat,self.minmaxchan_bl0)
00441 
00442     def test11( self ):
00443         """Test1: specify format """
00444         tid="11"
00445         reference = self.datapath+"refstat"+tid
00446         
00447         specunit = 'channel'
00448         outfile = self.outroot+tid+self.outsuff
00449         format = '3.5f'
00450 
00451         currstat = sdstat(infile=self.infile,specunit=specunit,format=format,outfile=outfile)
00452         # print "Statistics out of the current run:\n",currstat
00453 
00454         # Task sdstat returns a dictionary of statistic values
00455         self.assertTrue(isinstance(currstat,dict),
00456                          msg="The returned statistics are not a dictionary")
00457         self._compareFiles(outfile, reference)
00458         #self._compareStats(currstat,self.ref_allK)
00459         #self._compareStats(currstat,self.minmaxchan_all)
00460 
00461 class sdstat_restfreqTest( sdstat_unittest_base, unittest.TestCase ):
00462     """
00463     Unit tests for task sdstat. Test variations of restfreq parameter.
00464 
00465     The list of tests:
00466     testRF01 - testRF02 --- a value (float, quantity w/ unit)
00467     testRF11 - testRF13 --- single element list (int, quantity w/o unit, dictionary)
00468     testRF21 - testRF23 --- single element list (float/int, quantity, dictionary)
00469     """
00470     iflist = [0,2]
00471     frf = [45.490e9, 44.075e9]
00472     irf = [45490000000, 44075000000]
00473     qurf = ['45490.MHz','44.075GHz']
00474     qrf = [str(frf[0]), str(irf[1])]
00475     drf = [{'name': "IF0 Rest", 'value': frf[0]}, \
00476            {'name': "IF2 Rest", 'value': qurf[1]}]
00477     badq = ['45490.km','44.075bad']
00478     
00479     #compVstats = ['max','min','mean','sum','rms','median','stddev']
00480 
00481     # Reference line statistic values (masklist=linechan2, invertmask=False)
00482     ref_allK02 = {'rms': [4.1249432563781738, 4.4454779624938965],
00483                 'min': [-6.5844717025756836, -92.782661437988281],
00484                 'max': [12.278024673461914, 60.68634033203125],
00485                 'median': [4.1043367385864258, 4.1507611274719238],
00486                 'stddev': [0.2636776864528656, 1.5120075941085815],
00487                 'sum': [33722.40234375, 34246.4453125],
00488                 'mean': [4.1165041923522949, 4.1804742813110352]}
00489 
00490     minmaxvrf0_all0 = {'max_abscissa': {'value': 168.19211024624954, 'unit': 'km/s'}, 
00491                         'min_abscissa': {'value': 168.3127963097034, 'unit': 'km/s'}}
00492     minmaxvrf2_all0 = {'max_abscissa': {'value': -9451.0554503663261, 'unit': 'km/s'}, 
00493                         'min_abscissa': {'value': -9450.9308897531319, 'unit': 'km/s'}}
00494     minmaxvrf2_all2 = {'max_abscissa': {'value': -169.33704197623328, 'unit': 'km/s'}, 
00495                         'min_abscissa': {'value': -169.37856218060918, 'unit': 'km/s'}}
00496     #minmaxchan_all = {'max_abscissa': {'value': array([   21.,  8186.]), 'unit': 'channel'},
00497     #                  'min_abscissa': {'value': array([   18.,  8187.]), 'unit': 'channel'}}    
00498     ### Actual test scripts ###
00499     def setUp( self ):
00500         if os.path.exists(self.infile):
00501             shutil.rmtree(self.infile)
00502         shutil.copytree(self.datapath+self.infile, self.infile)
00503 
00504         default(sdstat)
00505 
00506     def tearDown( self ):
00507         if (os.path.exists(self.infile)):
00508             shutil.rmtree(self.infile)
00509 
00510     # Tests
00511     def testRF01( self ):
00512         """Test RF01: restfreq (a float value)"""
00513         tid = "RF01"
00514         infile = self.infile
00515         outfile = self.outroot+tid+self.outsuff
00516         iflist = self.iflist
00517         specunit = 'km/s'
00518         restfreq = self.frf[1]
00519         
00520         print "Setting restfreq = %s" % (str(restfreq))
00521         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00522                           specunit=specunit,restfreq=restfreq)
00523 
00524         # Task sdstat returns a dictionary of statistic values
00525         self.assertTrue(isinstance(currstat,dict),
00526                          msg="The returned statistics are not a dictionary")
00527         self._compareStats(currstat,self.ref_allK02)
00528         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00529         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00530         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00531 
00532     def testRF02( self ):
00533         """Test RF02: restfreq (a quantity w/ unit)"""
00534         tid = "RF02"
00535         infile = self.infile
00536         outfile = self.outroot+tid+self.outsuff
00537         iflist = self.iflist
00538         specunit = 'km/s'
00539         restfreq = self.qurf[1]
00540 
00541         print "Setting restfreq = %s" % (str(restfreq))
00542         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00543                           specunit=specunit,restfreq=restfreq)
00544 
00545         # Task sdstat returns a dictionary of statistic values
00546         self.assertTrue(isinstance(currstat,dict),
00547                          msg="The returned statistics are not a dictionary")
00548         self._compareStats(currstat,self.ref_allK02)
00549         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00550         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00551         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00552 
00553     def testRF11( self ):
00554         """Test RF11: restfreq (single element list of int)"""
00555         tid = "RF11"
00556         infile = self.infile
00557         outfile = self.outroot+tid+self.outsuff
00558         iflist = self.iflist
00559         specunit = 'km/s'
00560         restfreq = [ self.irf[1] ]
00561 
00562         print "Setting restfreq = %s" % (str(restfreq))
00563         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00564                           specunit=specunit,restfreq=restfreq)
00565 
00566         # Task sdstat returns a dictionary of statistic values
00567         self.assertTrue(isinstance(currstat,dict),
00568                          msg="The returned statistics are not a dictionary")
00569         self._compareStats(currstat,self.ref_allK02)
00570         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00571         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00572         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00573 
00574     def testRF12( self ):
00575         """Test RF12: restfreq (single element list of quantity w/o unit)"""
00576         tid = "RF12"
00577         infile = self.infile
00578         outfile = self.outroot+tid+self.outsuff
00579         iflist = self.iflist
00580         specunit = 'km/s'
00581         restfreq = [ self.qrf[1] ]
00582 
00583         print "Setting restfreq = %s" % (str(restfreq))
00584         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00585                           specunit=specunit,restfreq=restfreq)
00586 
00587         # Task sdstat returns a dictionary of statistic values
00588         self.assertTrue(isinstance(currstat,dict),
00589                          msg="The returned statistics are not a dictionary")
00590         self._compareStats(currstat,self.ref_allK02)
00591         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00592         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00593         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00594 
00595     def testRF13( self ):
00596         """Test RF13: restfreq (single element list of dictionary)"""
00597         tid = "RF13"
00598         infile = self.infile
00599         outfile = self.outroot+tid+self.outsuff
00600         iflist = self.iflist
00601         specunit = 'km/s'
00602         restfreq = [ self.drf[1] ]
00603 
00604         print "Setting restfreq = %s" % (str(restfreq))
00605         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00606                           specunit=specunit,restfreq=restfreq)
00607 
00608         # Task sdstat returns a dictionary of statistic values
00609         self.assertTrue(isinstance(currstat,dict),
00610                          msg="The returned statistics are not a dictionary")
00611         self._compareStats(currstat,self.ref_allK02)
00612         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00613         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00614         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00615 
00616     def testRF21( self ):
00617         """Test RF21: restfreq (a list of float & int)"""
00618         tid = "RF21"
00619         infile = self.infile
00620         outfile = self.outroot+tid+self.outsuff
00621         iflist = self.iflist
00622         specunit = 'km/s'
00623         restfreq = [ self.frf[0], self.irf[1] ]
00624 
00625         print "Setting restfreq = %s" % (str(restfreq))
00626         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00627                           specunit=specunit,restfreq=restfreq)
00628 
00629         # Task sdstat returns a dictionary of statistic values
00630         self.assertTrue(isinstance(currstat,dict),
00631                          msg="The returned statistics are not a dictionary")
00632         self._compareStats(currstat,self.ref_allK02)
00633         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00634         self._compareStats(self.minmaxvrf0_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00635         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00636 
00637     def testRF22( self ):
00638         """Test RF22: restfreq (a list of quantity w/ and w/o unit)"""
00639         tid = "RF22"
00640         infile = self.infile
00641         outfile = self.outroot+tid+self.outsuff
00642         iflist = self.iflist
00643         specunit = 'km/s'
00644         restfreq = [ self.qurf[0], self.qrf[1] ]
00645 
00646         print "Setting restfreq = %s" % (str(restfreq))
00647         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00648                           specunit=specunit,restfreq=restfreq)
00649 
00650         # Task sdstat returns a dictionary of statistic values
00651         self.assertTrue(isinstance(currstat,dict),
00652                          msg="The returned statistics are not a dictionary")
00653         self._compareStats(currstat,self.ref_allK02)
00654         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00655         self._compareStats(self.minmaxvrf0_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00656         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00657 
00658     def testRF23( self ):
00659         """Test RF23: restfreq (a list of dictionary)"""
00660         tid = "RF23"
00661         infile = self.infile
00662         outfile = self.outroot+tid+self.outsuff
00663         iflist = self.iflist
00664         specunit = 'km/s'
00665         restfreq = [ self.drf[1], self.drf[0] ]
00666 
00667         print "Setting restfreq = %s" % (str(restfreq))
00668         currstat = sdstat(infile=self.infile,outfile=outfile,iflist=iflist,\
00669                           specunit=specunit,restfreq=restfreq)
00670 
00671         # Task sdstat returns a dictionary of statistic values
00672         self.assertTrue(isinstance(currstat,dict),
00673                          msg="The returned statistics are not a dictionary")
00674         self._compareStats(currstat,self.ref_allK02)
00675         # Comparing min/max pos (Need to invert order of ref/test vals for icomp)
00676         self._compareStats(self.minmaxvrf2_all0,currstat,icomp=0,compstats=self.minmaxvrf2_all0.keys())
00677         self._compareStats(self.minmaxvrf2_all2,currstat,icomp=1,compstats=self.minmaxvrf2_all2.keys())
00678 
00679 
00680 
00681         
00682 class sdstat_storageTest( sdstat_unittest_base, unittest.TestCase ):
00683     """
00684     Unit tests for task sdstat. Test scantable sotrage and insitu
00685     parameters
00686 
00687     The list of tests:
00688     testMT  --- storage = 'memory', insitu = True
00689     testMF  --- storage = 'memory', insitu = False
00690     testDT  --- storage = 'disk', insitu = True
00691     testDF  --- storage = 'disk', insitu = False
00692 
00693     Note on handlings of disk storage:
00694        Task script restores unit and frame information.
00695     """
00696     linechan2 = [[2951,3088]]
00697     # Reference line statistic values (masklist=linechan2, invertmask=False)
00698     ref_line2 = {'rms': 5.0687642097473145, 'min': 3.9442729949951172,
00699                  'max': 6.1298322677612305, 'median': 4.9097409248352051,
00700                  'stddev': 0.57094955444335938, 'sum': 695.0701904296875,
00701                  'mean': 5.0367403030395508}
00702     minmaxchan_line2 = {'max_abscissa': {'value': 3048.0, 'unit': 'channel'}, 
00703                         'min_abscissa': {'value': 2951.0, 'unit': 'channel'}}
00704     minmaxvrf_line2 = {'max_abscissa': {'value': 43.993768228253309, 'unit': 'km/s'}, 
00705                         'min_abscissa': {'value': 48.021228055013935, 'unit': 'km/s'}}
00706     
00707     def setUp( self ):
00708         if os.path.exists(self.infile):
00709             shutil.rmtree(self.infile)
00710         shutil.copytree(self.datapath+self.infile, self.infile)
00711         # back up the original settings
00712         self.storage = sd.rcParams['scantable.storage']
00713         self.insitu = sd.rcParams['insitu']
00714 
00715         default(sdstat)
00716 
00717     def tearDown( self ):
00718         # restore settings
00719         sd.rcParams['scantable.storage'] = self.storage
00720         sd.rcParams['insitu'] = self.insitu
00721         if (os.path.exists(self.infile)):
00722             shutil.rmtree(self.infile)
00723 
00724     def testMT( self ):
00725         """Storage Test MT: storage='memory' and insitu=T"""
00726         tid="MT"
00727         outfile = self.outroot+tid+self.outsuff
00728         iflist = [2]
00729         #specunit = 'GHz'
00730         specunit = 'km/s'
00731         restfreq = [44.075e9]
00732 
00733         sd.rcParams['scantable.storage'] = 'memory'
00734         initstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.init',
00735                           iflist=iflist,masklist=self.linechan2)
00736         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0],
00737                                           restfreq=restfreq[0])
00738         sd.rcParams['scantable.storage'] = 'memory'
00739         sd.rcParams['insitu'] = True
00740         print "Running test with storage='%s' and insitu=%s" % \
00741               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00742 
00743         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00744                           iflist=iflist,masklist=masklist,restfreq=restfreq)
00745         # print "Statistics out of the current run:\n",currstat
00746         
00747         sd.rcParams['scantable.storage'] = 'memory'
00748         newinstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.newin',
00749                           iflist=iflist,masklist=self.linechan2)
00750 
00751         # Test input data
00752         compstats = self.ref_line2.keys()
00753         print "Comparing INPUT statistics before/after calculations"
00754         self._compareStats(newinstat,initstat,compstats=compstats)
00755         print "Quantums before run"
00756         self._compareStats(initstat,self.minmaxchan_line2)
00757         print "Quantums after run"
00758         self._compareStats(newinstat,self.minmaxchan_line2)
00759         # Test output data
00760         print "Testing OUTPUT statistics"
00761         self._compareStats(currstat,self.ref_line2)
00762         print "Testing OUTPUT Quantums"
00763         self._compareStats(currstat,self.minmaxvrf_line2)
00764 
00765     def testMF( self ):
00766         """Storage Test MF: storage='memory' and insitu=F"""
00767         tid="MF"
00768         outfile = self.outroot+tid+self.outsuff
00769         iflist = [2]
00770         #specunit = 'GHz'
00771         specunit = 'km/s'
00772         restfreq = [44.075e9]
00773 
00774         sd.rcParams['scantable.storage'] = 'memory'
00775         initstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.init',
00776                           iflist=iflist,masklist=self.linechan2)
00777         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0],
00778                                           restfreq=restfreq[0])
00779         
00780         sd.rcParams['scantable.storage'] = 'memory'
00781         sd.rcParams['insitu'] = False
00782         print "Running test with storage='%s' and insitu=%s" % \
00783               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00784 
00785         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00786                           iflist=iflist,masklist=masklist,restfreq=restfreq)
00787         # print "Statistics out of the current run:\n",currstat
00788         
00789         sd.rcParams['scantable.storage'] = 'memory'
00790         newinstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.newin',
00791                           iflist=iflist,masklist=self.linechan2)
00792 
00793         # Test input data
00794         compstats = self.ref_line2.keys()
00795         print "Comparing INPUT statistics before/after calculations"
00796         self._compareStats(newinstat,initstat,compstats=compstats)
00797         print "Quantums before run"
00798         self._compareStats(initstat,self.minmaxchan_line2)
00799         print "Quantums after run"
00800         self._compareStats(newinstat,self.minmaxchan_line2)
00801         # Test output data
00802         print "Testing OUTPUT statistics"
00803         self._compareStats(currstat,self.ref_line2)
00804         print "Testing OUTPUT Quantums"
00805         self._compareStats(currstat,self.minmaxvrf_line2)
00806 
00807     def testDT( self ):
00808         """Storage Test DT: storage='disk' and insitu=T"""
00809         tid="DT"
00810         outfile = self.outroot+tid+self.outsuff
00811         iflist = [2]
00812         #specunit = 'GHz'
00813         specunit = 'km/s'
00814         restfreq = [44.075e9]
00815 
00816         sd.rcParams['scantable.storage'] = 'memory'
00817         initstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.init',
00818                           iflist=iflist,masklist=self.linechan2)
00819         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0],
00820                                           restfreq=restfreq[0])
00821         
00822         sd.rcParams['scantable.storage'] = 'disk'
00823         sd.rcParams['insitu'] = True
00824         print "Running test with storage='%s' and insitu=%s" % \
00825               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00826 
00827         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00828                           iflist=iflist,masklist=masklist,restfreq=restfreq)
00829         # print "Statistics out of the current run:\n",currstat
00830         
00831         sd.rcParams['scantable.storage'] = 'memory'
00832         newinstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.newin',
00833                           iflist=iflist,masklist=self.linechan2)
00834 
00835         # Test input data
00836         compstats = self.ref_line2.keys()
00837         print "Comparing INPUT statistics before/after calculations"
00838         self._compareStats(newinstat,initstat,compstats=compstats)
00839         print "Quantums before run"
00840         self._compareStats(initstat,self.minmaxchan_line2)
00841         print "Quantums after run"
00842         self._compareStats(newinstat,self.minmaxchan_line2)
00843         # Test output data
00844         print "Testing OUTPUT statistics"
00845         self._compareStats(currstat,self.ref_line2)
00846         print "Testing OUTPUT Quantums"
00847         self._compareStats(currstat,self.minmaxvrf_line2)
00848 
00849     def testDF( self ):
00850         """Storage Test DF: storage='disk' and insitu=F"""
00851         tid="DF"
00852         outfile = self.outroot+tid+self.outsuff
00853         iflist = [2]
00854         #specunit = 'GHz'
00855         specunit = 'km/s'
00856         restfreq = [44.075e9]
00857 
00858         sd.rcParams['scantable.storage'] = 'memory'
00859         initstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.init',
00860                           iflist=iflist,masklist=self.linechan2)
00861         masklist = self._convert_masklist(self.linechan2,specunit,self.infile,spw=iflist[0],
00862                                           restfreq=restfreq[0])
00863         
00864         sd.rcParams['scantable.storage'] = 'disk'
00865         sd.rcParams['insitu'] = False
00866         print "Running test with storage='%s' and insitu=%s" % \
00867               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00868 
00869         currstat = sdstat(infile=self.infile,specunit=specunit,outfile=outfile,
00870                           iflist=iflist,masklist=masklist,restfreq=restfreq)
00871         # print "Statistics out of the current run:\n",currstat
00872         
00873         sd.rcParams['scantable.storage'] = 'memory'
00874         newinstat = sdstat(infile=self.infile,specunit='',outfile=outfile+'.newin',
00875                           iflist=iflist,masklist=self.linechan2)
00876 
00877         # Test input data
00878         compstats = self.ref_line2.keys()
00879         print "Comparing INPUT statistics before/after calculations"
00880         self._compareStats(newinstat,initstat,compstats=compstats)
00881         print "Quantums before run"
00882         self._compareStats(initstat,self.minmaxchan_line2)
00883         print "Quantums after run"
00884         self._compareStats(newinstat,self.minmaxchan_line2)
00885         # Test output data
00886         print "Testing OUTPUT statistics"
00887         self._compareStats(currstat,self.ref_line2)
00888         print "Testing OUTPUT Quantums"
00889         self._compareStats(currstat,self.minmaxvrf_line2)
00890 
00891 class sdstat_exceptions( sdstat_unittest_base, unittest.TestCase ):
00892     """
00893     Test the case when the task throws exception.
00894     """
00895     # Data path of input/output
00896     datapath = os.environ.get('CASAPATH').split()[0] + \
00897                '/data/regression/unittest/sdstat/'
00898     taskname = "sdstat"
00899     outroot = taskname+'_test'
00900     outsuff = ".out"
00901     infile = 'OrionS_rawACSmod_calTPave.asap'
00902 
00903     def setUp( self ):
00904         if os.path.exists(self.infile):
00905             shutil.rmtree(self.infile)
00906         shutil.copytree(self.datapath+self.infile, self.infile)
00907 
00908         default(sdstat)
00909 
00910     def tearDown( self ):
00911         if (os.path.exists(self.infile)):
00912             shutil.rmtree(self.infile)
00913 
00914     def testNoData(self):
00915         try:
00916             res = sdstat(infile=self.infile,iflist=99)
00917             self.assertTrue(False,
00918                             msg='The task must throw exception')
00919         except Exception, e:
00920             pos=str(e).find('Selection contains no data. Not applying it.')
00921             self.assertNotEqual(pos,-1,
00922                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00923 
00924 def suite():
00925     return [sdstat_basicTest, sdstat_restfreqTest, sdstat_storageTest,
00926             sdstat_exceptions]