casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdreduce.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 #from numpy import array
00011 
00012 import asap as sd
00013 from sdreduce import sdreduce
00014 #from sdstat import sdstat
00015 
00016 class sdreduce_test(unittest.TestCase):
00017     """
00018     Basic unit tests for task sdreduce. No interactive testing.
00019 
00020     The list of tests:
00021     test00    --- default parameters (raises an errror)
00022     test01    --- Default parameters + valid input filename
00023     test02    --- operate all 3 steps (mostly with default parameters)
00024     test03    --- explicitly specify all parameters
00025     test04-07 --- do one of calibration, average, baseline, or smooth
00026     test08-10 --- skip one of of calibration and average, baseline, or smooth
00027     
00028     Note: input data (infile0) is generated from a single dish regression data,
00029     'OrionS_rawACSmod', as follows:
00030       default(sdsave)
00031       sdsave(infile='OrionS_rawACSmod',outfile=self.infile0,outform='ASAP')
00032     -> Just converted to scantable to eliminate errors by data conversion.
00033     """
00034     # Data path of input/output
00035     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdreduce/'
00036     # Input and output names
00037     # uncalibrated data
00038     infile0 = 'OrionS_rawACSmod.asap'
00039     # uncalibrated data
00040     infile1 = 'OrionS_rawACSmod_cal2123.asap'
00041     infiles = [infile0, infile1]
00042     outroot = 'sdreduce_test'
00043 
00044     def setUp(self):
00045         for file in self.infiles:
00046             if os.path.exists(file):
00047                 shutil.rmtree(file)
00048             shutil.copytree(self.datapath+file, file)
00049         default(sdreduce)
00050 
00051     def tearDown(self):
00052         for file in self.infiles:
00053             if (os.path.exists(file)):
00054                 shutil.rmtree(file)
00055 
00056     def _row0_stats(self,file):
00057         scan = sd.scantable(file,average=False)
00058         stats=["max","min","sum","rms","stddev","max_abc","min_abc"]
00059         edge = 500
00060         chanrange = [edge, scan.nchan()-edge-1]
00061         mask = scan.create_mask(chanrange)
00062         statdict = {}
00063         for stat in stats:
00064             statdict[stat] = scan.stats(stat,mask=mask,row=0)[0]
00065         del scan
00066         print "\nCurrent run: "+str(statdict)
00067         return statdict
00068 
00069     def _teststats0(self,teststat,refstat,places=4):
00070         for stat, refval in refstat.iteritems():
00071             self.assertTrue(teststat.has_key(stat),
00072                             msg = "'%s' is not defined in the current run" % stat)
00073             allowdiff = 0.01
00074             #print "Comparing '%s': %f (current run), %f (reference)" % \
00075             #      (stat,testdict[stat],refval)
00076             reldiff = (teststat[stat]-refval)/refval
00077             self.assertTrue(reldiff < allowdiff,\
00078                             msg="'%s' differs: %f (ref) != %f" % \
00079                             (stat, refval, teststat[stat]))
00080 
00081     def test00(self):
00082         """Test 0: Default parameters (raises an errror)"""
00083         #print blfunc
00084         result = sdreduce()
00085         self.assertFalse(result)
00086 
00087     def test01(self):
00088         """Test 1: Default parameters + valid input filename (do nothing)"""
00089         self.tid="01"
00090         infile = self.infile0
00091         outfile = self.outroot+self.tid+'.asap'
00092 
00093         result = sdreduce(infile=infile,outfile=outfile)
00094         self.assertEqual(result,None,
00095                          msg="The task returned '"+str(result)+"' instead of None")
00096         self.assertTrue(os.path.exists(outfile),
00097                          msg="Output file '"+str(outfile)+"' doesn't exists")
00098         refstat = {'rms': 0.55389463901519775, 'min': 0.26541909575462341,
00099                    'max_abc': 773.0, 'max': 0.91243284940719604,
00100                    'sum': 3802.1259765625, 'stddev': 0.16529126465320587,
00101                    'min_abc': 7356.0}
00102         teststat = self._row0_stats(outfile)
00103         self._teststats0(teststat,refstat)
00104 
00105     def test02(self):
00106         """
00107         Test 2: operate all steps (mostly with default parameters)
00108         testing if default parameter values are changed
00109         """
00110         # Don't average GBT ps data at the same time of calibration.
00111         self.tid="02"
00112         infile = self.infile0
00113         outfile = self.outroot+self.tid+'.asap'
00114         calmode = 'ps'
00115         kernel = 'hanning'
00116         blfunc='poly'
00117 
00118 
00119         result = sdreduce(infile=infile,outfile=outfile,calmode=calmode,
00120                        kernel=kernel,blfunc=blfunc)
00121         self.assertEqual(result,None,
00122                          msg="The task returned '"+str(result)+"' instead of None")
00123         self.assertTrue(os.path.exists(outfile),
00124                          msg="Output file '"+str(outfile)+"' doesn't exists")
00125 
00126         refstat = {'rms': 0.21985267102718353, 'min': -0.70194435119628906,
00127                    'max_abc': 4093.0, 'max': 0.96840262413024902,
00128                    'sum': 5.4850387573242188, 'stddev': 0.21986636519432068,
00129                    'min_abc': 7623.0}
00130         teststat = self._row0_stats(outfile)
00131         self._teststats0(teststat,refstat)
00132         
00133 
00134     def test03(self):
00135         """
00136         Test 3:  explicitly specify all parameters
00137         testing if parameter names are changed
00138         """
00139         self.tid="03"
00140         infile = self.infile0
00141         outfile = self.outroot+self.tid+'.asap'
00142 
00143         result = sdreduce(infile=infile,
00144                        antenna=0,
00145                        fluxunit='K',
00146                        telescopeparm='',
00147                        specunit='GHz',
00148                        frame='',
00149                        doppler='',
00150                        calmode='ps',
00151                        scanlist=[20,21,22,23],
00152                        field='OrionS*',
00153                        iflist=[0],
00154                        pollist=[],
00155                        channelrange=[],
00156                        average=False,
00157                        scanaverage=False,
00158                        timeaverage=False,
00159                        tweight='none',
00160                        averageall=False,
00161                        polaverage=False,
00162                        pweight='none',
00163                        tau=0.0,
00164                        kernel='hanning',
00165                        kwidth=5,
00166                        blfunc='poly',
00167                        order=5,
00168                        npiece=2,
00169                        clipthresh=3.0,
00170                        clipniter=1,
00171                        masklist=[],
00172                        maskmode='auto',
00173                        thresh=5.0,
00174                        avg_limit=4,
00175                        edge=[0],
00176                        verifycal=False,
00177                        verifysm=False,
00178                        verifybl=False,
00179                        verbosebl=True,
00180                        outfile=outfile,
00181                        outform='ASAP',
00182                        overwrite=False,
00183                        plotlevel=0)
00184         self.assertEqual(result,None,
00185                          msg="The task returned '"+str(result)+"' instead of None")
00186         self.assertTrue(os.path.exists(outfile),
00187                          msg="Output file '"+str(outfile)+"' doesn't exists")
00188 
00189     def test04(self):
00190         """ Test 4:  operate only calibration step """
00191         self.tid="04"
00192         infile = self.infile0
00193         outfile = self.outroot+self.tid+'.asap'
00194         calmode = 'ps'
00195 
00196         result = sdreduce(infile=infile,outfile=outfile,calmode=calmode)
00197         self.assertEqual(result,None,
00198                          msg="The task returned '"+str(result)+"' instead of None")
00199         self.assertTrue(os.path.exists(outfile),
00200                          msg="Output file '"+str(outfile)+"' doesn't exists")
00201         refstat = {'rms': 2.1299083232879639, 'min': 1.2246102094650269,
00202                    'max_abc': 4093.0, 'max': 3.1902554035186768,
00203                    'sum': 15209.119140625, 'stddev': 0.25390961766242981,
00204                    'min_abc': 7434.0}
00205         teststat = self._row0_stats(outfile)
00206         self._teststats0(teststat,refstat)
00207 
00208     def test05(self):
00209         """ Test 5:  operate only averaging step """
00210         self.tid="05"
00211         infile = self.infile1
00212         outfile = self.outroot+self.tid+'.asap'
00213         average = True
00214         # need to run one of average
00215         scanaverage = True
00216 
00217         result = sdreduce(infile=infile,outfile=outfile,
00218                        average=average,scanaverage=scanaverage)
00219         self.assertEqual(result,None,
00220                          msg="The task returned '"+str(result)+"' instead of None")
00221         self.assertTrue(os.path.exists(outfile),
00222                          msg="Output file '"+str(outfile)+"' doesn't exists")
00223 
00224         refstat = {'rms': 4.1353230476379395, 'min': 3.2386586666107178,
00225                    'max_abc': 4093.0, 'max': 5.6874399185180664,
00226                    'sum': 29690.876953125, 'stddev': 0.24056948721408844,
00227                    'min_abc': 2452.0}
00228         teststat = self._row0_stats(outfile)
00229         self._teststats0(teststat,refstat)
00230 
00231     def test06(self):
00232         """ Test 6:  operate only smoothing step """
00233         self.tid="06"
00234         infile = self.infile1
00235         outfile = self.outroot+self.tid+'.asap'
00236         kernel = 'hanning'
00237 
00238         result = sdreduce(infile=infile,outfile=outfile,kernel=kernel)
00239         self.assertEqual(result,None,
00240                          msg="The task returned '"+str(result)+"' instead of None")
00241         self.assertTrue(os.path.exists(outfile),
00242                          msg="Output file '"+str(outfile)+"' doesn't exists")
00243 
00244         refstat = {'rms': 3.5979659557342529, 'min': 2.3542881011962891,
00245                    'max_abc': 4093.0, 'max': 5.2421674728393555,
00246                    'sum': 25737.166015625, 'stddev': 0.37295544147491455,
00247                    'min_abc': 6472.0}
00248         teststat = self._row0_stats(outfile)
00249         self._teststats0(teststat,refstat)
00250 
00251     def test07(self):
00252         """ Test 7:  operate only baseline step """
00253         self.tid="07"
00254         infile = self.infile1
00255         outfile = self.outroot+self.tid+'.asap'
00256         blfunc = 'poly'
00257 
00258         result = sdreduce(infile=infile,outfile=outfile,blfunc=blfunc)
00259         self.assertEqual(result,None,
00260                          msg="The task returned '"+str(result)+"' instead of None")
00261         self.assertTrue(os.path.exists(outfile),
00262                          msg="Output file '"+str(outfile)+"' doesn't exists")
00263 
00264         refstat =  {'rms': 0.42929685115814209, 'min': -1.4878685474395752,
00265                     'max_abc': 4093.0, 'max': 1.8000495433807373,
00266                     'sum': 6.9646663665771484, 'stddev': 0.42932614684104919,
00267                     'min_abc': 7434.0}
00268         teststat = self._row0_stats(outfile)
00269         self._teststats0(teststat,refstat)
00270 
00271     def test08(self):
00272         """ Test 8:  skip calibration and averaging """
00273         self.tid="08"
00274         infile = self.infile1
00275         outfile = self.outroot+self.tid+'.asap'
00276         calmode = 'none'
00277         average = False
00278         kernel = 'hanning'
00279         blfunc = 'poly'
00280 
00281         result = sdreduce(infile=infile,outfile=outfile,calmode=calmode,
00282                        average=average,kernel=kernel,blfunc=blfunc)
00283         self.assertEqual(result,None,
00284                          msg="The task returned '"+str(result)+"' instead of None")
00285         self.assertTrue(os.path.exists(outfile),
00286                          msg="Output file '"+str(outfile)+"' doesn't exists")
00287         refstat = {'rms': 0.37204021215438843, 'min': -1.1878492832183838,
00288                    'max_abc': 4093.0, 'max': 1.6387548446655273,
00289                    'sum': 9.2789239883422852, 'stddev': 0.3720642626285553,
00290                    'min_abc': 7623.0}
00291         teststat = self._row0_stats(outfile)
00292         self._teststats0(teststat,refstat)
00293 
00294     def test09(self):
00295         """ Test 9:  skip smoothing"""
00296         self.tid="09"
00297         infile = self.infile0
00298         outfile = self.outroot+self.tid+'.asap'
00299         calmode = 'ps'
00300         kernel = 'none'
00301         blfunc = 'poly'
00302 
00303         result = sdreduce(infile=infile,outfile=outfile,calmode=calmode,
00304                        kernel=kernel,blfunc=blfunc)
00305         self.assertEqual(result,None,
00306                          msg="The task returned '"+str(result)+"' instead of None")
00307         self.assertTrue(os.path.exists(outfile),
00308                          msg="Output file '"+str(outfile)+"' doesn't exists")
00309 
00310         refstat = {'rms': 0.25368797779083252, 'min': -0.87923824787139893,
00311                    'max_abc': 4093.0, 'max': 1.0637180805206299,
00312                    'sum': 4.11566162109375, 'stddev': 0.25370475649833679,
00313                    'min_abc': 7434.0}
00314         teststat = self._row0_stats(outfile)
00315         self._teststats0(teststat,refstat)
00316 
00317     def test10(self):
00318         """ Test 10:  skip baseline"""
00319         self.tid="10"
00320         infile = self.infile0
00321         outfile = self.outroot+self.tid+'.asap'
00322         calmode = 'ps'
00323         kernel = 'hanning'
00324         blfunc = 'none'
00325 
00326         result = sdreduce(infile=infile,outfile=outfile,calmode=calmode,
00327                        kernel=kernel,blfunc=blfunc)
00328         self.assertEqual(result,None,
00329                          msg="The task returned '"+str(result)+"' instead of None")
00330         self.assertTrue(os.path.exists(outfile),
00331                          msg="Output file '"+str(outfile)+"' doesn't exists")
00332 
00333         refstat = {'rms': 2.126171350479126, 'min': 1.3912382125854492,
00334                    'max_abc': 4093.0, 'max': 3.0977959632873535,
00335                    'sum': 15209.0869140625, 'stddev': 0.2203933447599411,
00336                    'min_abc': 6472.0}
00337         teststat = self._row0_stats(outfile)
00338         self._teststats0(teststat,refstat)
00339 
00340 def suite():
00341     return [sdreduce_test]