casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdmath.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 import sha
00009 import time
00010 import numpy
00011 import re
00012 import string
00013 
00014 from sdmath import sdmath
00015 import asap as sd
00016 from asap.scantable import is_scantable
00017 
00018 #
00019 # Unit test of sdmath task.
00020 # 
00021 
00022 ###
00023 # Base class for sdmath unit test
00024 ###
00025 class sdmath_unittest_base:
00026     """
00027     Base class for sdmath unit test
00028     """
00029     taskname='sdmath'
00030     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdmath/'
00031     
00032     def _checkfile( self, name ):
00033         isthere=os.path.exists(name)
00034         self.assertEqual(isthere,True,
00035                          msg='output file %s was not created because of the task failure'%(name))
00036 
00037     def _compare( self, name, ref, factor, op, scale=1.0 ):
00038         self._checkfile( name )
00039         # get original nchan and nrow
00040         tb.open(ref)
00041         nrow0=tb.nrows()
00042         rspchans=[]
00043         for irow in xrange(nrow0):
00044             rspchans.append(len(tb.getcell('SPECTRA',irow)))
00045         tb.close()
00046         # check shape
00047         tb.open(name)
00048         nrow=tb.nrows()
00049         self.assertEqual(nrow,2,
00050                          msg='number of rows mismatch')
00051         sp=[]
00052         for irow in xrange(nrow):
00053             sp.append(tb.getcell('SPECTRA',irow))
00054             self.assertEqual(len(sp[irow]),rspchans[irow],
00055                              msg='SPECTRA: number of channel mismatch in row%s'%(irow)) 
00056         tb.close()
00057         # check data
00058         valuetype=type(sp[0][0])
00059         print ''
00060         for irow in xrange(nrow):
00061             if type(factor) is not list:
00062                 f = factor
00063             elif type(factor[0]) is not list:
00064                 f = factor
00065             elif len(factor[0]) == 1:
00066                 f = factor[irow][0]
00067             else:
00068                 f = factor[irow]
00069             arrs = self._getref( numpy.ones(rspchans[irow],dtype=valuetype)*scale, op, f )
00070             print 'irow=%s'%(irow)
00071             print '   arrs=%s'%(arrs)
00072             print '   sp=%s'%(sp[irow])
00073             ret=numpy.allclose(arrs,sp[irow])
00074             self.assertEqual(ret,True,
00075                              msg='SPECTRA: data differ in row%s'%(irow))
00076 
00077     def _getref( self, ref, op, factor ):
00078         if type(factor) is not list:
00079             # scalar factor
00080             f = factor
00081         elif type(factor[0]) is not list:
00082             # 1D array factor
00083             f = numpy.array(factor)
00084         if op == '+':
00085             return ref+f
00086         elif op == '-':
00087             return ref-f
00088         elif op == '*':
00089             return ref*f
00090         else: # op == '/'
00091             return ref/f
00092 
00093     def _makedata(self,name,factor):
00094         f=open(name,'w')
00095         if type(factor) is not list:
00096             print >> f, '%s'%factor
00097         elif type(factor[0]) is not list:
00098             for irow in xrange(len(factor)):
00099                 print >> f, '%s'%factor[irow]
00100         else:
00101             for irow in xrange(len(factor)):
00102                 s=''
00103                 for icol in xrange(len(factor[irow])):
00104                     s+='%s '%factor[irow][icol]
00105                 print >> f, s
00106         f.close()
00107         #os.system('cat %s'%name)
00108                 
00109     def _checkresult(self,r,l0,l1,op,scale=1.0):
00110         self._checkfile(r)
00111         tb.open(r)
00112         spr=tb.getcol('SPECTRA').transpose()
00113         nrow=tb.nrows()
00114         tb.close()
00115         tb.open(l0)
00116         spl0=tb.getcol('SPECTRA').transpose()
00117         tb.close()
00118         self.assertEqual(spr.shape[0],spl0.shape[0],
00119                          msg='number of rows mismatch')
00120         self.assertEqual(spr.shape[1],spl0.shape[1],
00121                          msg='number of channels mismatch')
00122         tb.open(l1)
00123         spl1=tb.getcol('SPECTRA').transpose()
00124         tb.close()
00125         self.assertEqual(spr.shape[0],spl1.shape[0],
00126                          msg='number of rows mismatch')
00127         self.assertEqual(spr.shape[1],spl1.shape[1],
00128                          msg='number of channels mismatch')
00129 
00130         spl0*=scale
00131         spl1*=scale
00132         
00133         if op=='+':
00134             spl=spl0+spl1
00135         elif op=='-':
00136             spl=spl0-spl1
00137         elif op=='*':
00138             spl=spl0*spl1
00139         else: # op=='/'
00140             spl=spl0/spl1
00141         print ''
00142         for irow in xrange(nrow):
00143             ret=numpy.allclose(spr[irow],spl[irow])
00144             print 'irow=%s'%(irow)
00145             print '   spr=%s'%(spr[irow])
00146             print '   spl=%s'%(spl[irow])
00147             self.assertEqual(ret,True,
00148                              msg='SPECTRA: data differ in row%s'%(irow))
00149 
00150 ###
00151 # Test on bad parameter settings
00152 ###
00153 class sdmath_test0(unittest.TestCase,sdmath_unittest_base):
00154     """
00155     Test on bad parameter setting
00156 
00157     Test data, sdmath0.asap, is artificial data with the following
00158     status:
00159 
00160        - nrow = 2
00161        - nchan = 4 for spectral data
00162        - all spectral values are 1.0
00163        
00164     """
00165     # Input and output names
00166     rawfile='sdmath0.asap'
00167     prefix=sdmath_unittest_base.taskname+'Test0'
00168     outfile=prefix+'.asap'
00169 
00170     def setUp(self):
00171         self.res=None
00172         if (not os.path.exists(self.rawfile)):
00173             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00174 
00175         default(sdmath)
00176 
00177     def tearDown(self):
00178         if (os.path.exists(self.rawfile)):
00179             shutil.rmtree(self.rawfile)
00180         os.system( 'rm -rf '+self.prefix+'*' )
00181 
00182     def test000(self):
00183         """Test 000: Default parameters"""
00184         try:
00185             res=sdmath()
00186             self.assertTrue(False,
00187                             msg='The task must throw exception')
00188         except Exception, e:
00189             pos=str(e).find('expr is undefined')
00190             self.assertNotEqual(pos,-1,
00191                                 msg='Unexpected exception was thrown: %s'%(str(e)))        
00192 
00193     def test001(self):
00194         """Test 001: Empty varlist"""
00195         ex='V0+V1'
00196         v={}
00197         try:
00198             res=sdmath(expr=ex,varlist=v,outfile=self.outfile)
00199             self.assertTrue(False,
00200                             msg='The task must throw exception')
00201         except Exception, e:
00202             pos=str(e).find('name \'V0\' is not defined')
00203             self.assertNotEqual(pos,-1,
00204                                 msg='Unexpected exception was thrown: %s'%(str(e)))        
00205 
00206     def test002(self):
00207         """Test 002: Lack of some variables in varlist"""
00208         ex='V0+V1'
00209         v={'V0': self.rawfile}
00210         try:
00211             res=sdmath(expr=ex,varlist=v,outfile=self.outfile)
00212             self.assertTrue(False,
00213                             msg='The task must throw exception')
00214         except Exception, e:
00215             pos=str(e).find('name \'V1\' is not defined')
00216             self.assertNotEqual(pos,-1,
00217                                 msg='Unexpected exception was thrown: %s'%(str(e)))       
00218         
00219     def test003(self):
00220         """Test 003: Specify existing output file"""
00221         os.system('cp -r %s %s'%(self.rawfile,self.outfile))
00222         ex='V0+V1'
00223         factor=1.0
00224         v={'V0': self.rawfile,
00225            'V1': factor}
00226         try:
00227             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=False)
00228             self.assertTrue(False,
00229                             msg='The task must throw exception')
00230         except Exception, e:
00231             pos=str(e).find('Output file \'%s\' exists.'%(self.outfile))
00232             self.assertNotEqual(pos,-1,
00233                                 msg='Unexpected exception was thrown: %s'%(str(e)))       
00234         
00235     def test004(self):
00236         """Test 004: Bad operation (non-scantable is left side)"""
00237         ex='V0+V1'
00238         factor=1.0
00239         v={'V0': factor,
00240            'V1': self.rawfile}
00241         try:
00242             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00243             self.assertTrue(False,
00244                             msg='The task must throw exception')
00245         except Exception, e:
00246             pos=str(e).find('unsupported operand type(s) for +:')
00247             self.assertNotEqual(pos,-1,
00248                                 msg='Unexpected exception was thrown: %s'%(str(e)))       
00249 
00250 
00251     def test005(self):
00252         """Test 005: Bad operation (non-scantable operation)"""
00253         ex='V0+V1'
00254         factor=1.0
00255         v={'V0': factor,
00256            'V1': factor}
00257         try:
00258             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00259             self.assertTrue(False,
00260                             msg='The task must throw exception')
00261         except Exception, e:
00262             pos=str(e).find('\'float\' object has no attribute ')
00263             self.assertNotEqual(pos,-1,
00264                                 msg='Unexpected exception was thrown: %s'%(str(e)))       
00265 
00266     def test006(self):
00267         """Test 006: non-conform array operation"""
00268         ex='V0+V1'
00269         factor=[1.0, 2.0]
00270         v={'V0': self.rawfile,
00271            'V1': factor}
00272         try:
00273             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00274             self.assertTrue(False,
00275                             msg='The task must throw exception')
00276         except Exception, e:
00277             pos=str(e).find('Vector size must be 1 or be same as number of channel.')
00278             self.assertNotEqual(pos,-1,
00279                                 msg='Unexpected exception was thrown: %s'%(str(e)))       
00280 
00281     def test007(self):
00282         """Test 007: non-conform scantable operation"""
00283         ex='V0+V1'
00284         infile2=self.prefix+'.in.asap'
00285         shutil.copytree(self.datapath+'sdmath2.asap', infile2)
00286         v={'V0': self.rawfile,
00287            'V1': infile2}
00288         try:
00289             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00290             self.assertTrue(False,
00291                             msg='The task must throw exception')
00292         except Exception, e:
00293             pos=str(e).find('arrays do not conform')
00294             self.assertNotEqual(pos,-1,
00295                                 msg='Unexpected exception was thrown: %s'%(str(e)))                   
00296 
00297     def test008(self):
00298         """Test 008: non-conform ASCII text data"""
00299         ex='V0+V1'
00300         datafile=self.prefix+'.dat'
00301         factor=[1.0, 2.0, 3.0]
00302         self._makedata(datafile,factor)
00303         v={'V0': self.rawfile,
00304            'V1': datafile}
00305         try:
00306             res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00307             self.assertTrue(False,
00308                             msg='The task must throw exception')
00309         except Exception, e:
00310             pos=str(e).find('len(value) must be 1 or conform to scan.nrow()')
00311             self.assertNotEqual(pos,-1,
00312                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00313         
00314 
00315 ###
00316 # Test on scalar/array operation
00317 ###
00318 class sdmath_test1(unittest.TestCase,sdmath_unittest_base):
00319     """
00320     Test on scalar/array operation
00321 
00322     Test data, sdmath0.asap, is artificial data with the following
00323     status:
00324 
00325        - nrow = 2
00326        - nchan = 4 for spectral data
00327        - all spectral values are 1.0
00328        
00329     """
00330     # Input and output names
00331     rawfile='sdmath0.asap'
00332     prefix=sdmath_unittest_base.taskname+'Test2'
00333     outfile=prefix+'.asap'
00334 
00335     def setUp(self):
00336         self.res=None
00337         if (not os.path.exists(self.rawfile)):
00338             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00339 
00340         default(sdmath)
00341 
00342     def tearDown(self):
00343         if (os.path.exists(self.rawfile)):
00344             shutil.rmtree(self.rawfile)
00345         os.system( 'rm -rf '+self.prefix+'*' )
00346 
00347     def test100(self):
00348         """Test 100: Addition of scalar"""
00349         op='+'
00350         ex='V0'+op+'V1'
00351         factor=1.0
00352         v={'V0': self.rawfile,
00353            'V1': factor}
00354         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00355         self.assertEqual(res,None,
00356                          msg='Any error occurred during task execution')
00357         self._compare(self.outfile,self.rawfile,factor,op)
00358         
00359     def test101(self):
00360         """Test 101: Subtraction of scalar"""
00361         op='-'
00362         ex='V0'+op+'V1'
00363         factor=1.0
00364         v={'V0': self.rawfile,
00365            'V1': factor}
00366         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00367         self.assertEqual(res,None,
00368                          msg='Any error occurred during task execution')
00369         self._compare(self.outfile,self.rawfile,factor,op)
00370 
00371     def test102(self):
00372         """Test 102: Multiplication of scalar"""
00373         op='*'
00374         ex='V0'+op+'V1'
00375         factor=2.0
00376         v={'V0': self.rawfile,
00377            'V1': factor}
00378         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00379         self.assertEqual(res,None,
00380                          msg='Any error occurred during task execution')
00381         self._compare(self.outfile,self.rawfile,factor,op)
00382 
00383     def test103(self):
00384         """Test 103: Division of scalar"""
00385         op='/'
00386         ex='V0'+op+'V1'
00387         factor=2.0
00388         v={'V0': self.rawfile,
00389            'V1': factor}
00390         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00391         self.assertEqual(res,None,
00392                          msg='Any error occurred during task execution')
00393         self._compare(self.outfile,self.rawfile,factor,op)
00394 
00395     def test104(self):
00396         """Test 104: Addition of 1D array of [nchan]"""
00397         op='+'
00398         ex='V0'+op+'V1'
00399         factor=[0.1,0.2,0.3,0.4]
00400         v={'V0': self.rawfile,
00401            'V1': factor}
00402         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00403         self.assertEqual(res,None,
00404                          msg='Any error occurred during task execution')
00405         self._compare(self.outfile,self.rawfile,factor,op)
00406         
00407     def test105(self):
00408         """Test 105: Subtraction of 1D array of [nchan]"""
00409         op='-'
00410         ex='V0'+op+'V1'
00411         factor=[0.1,0.2,0.3,0.4]
00412         v={'V0': self.rawfile,
00413            'V1': factor}
00414         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00415         self.assertEqual(res,None,
00416                          msg='Any error occurred during task execution')
00417         self._compare(self.outfile,self.rawfile,factor,op)
00418 
00419     def test106(self):
00420         """Test 106: Multiplication of 1D array of [nchan]"""
00421         op='*'
00422         ex='V0'+op+'V1'
00423         factor=[0.1,0.2,0.3,0.4]
00424         v={'V0': self.rawfile,
00425            'V1': factor}
00426         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00427         self.assertEqual(res,None,
00428                          msg='Any error occurred during task execution')
00429         self._compare(self.outfile,self.rawfile,factor,op)
00430 
00431     def test107(self):
00432         """Test 107: Division of 1D array of [nchan]"""
00433         op='/'
00434         ex='V0'+op+'V1'
00435         factor=[0.1,0.2,0.3,0.4]
00436         v={'V0': self.rawfile,
00437            'V1': factor}
00438         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00439         self.assertEqual(res,None,
00440                          msg='Any error occurred during task execution')
00441         self._compare(self.outfile,self.rawfile,factor,op)
00442 
00443     def test108(self):
00444         """Test 108: Addition of 2D array of [nrow,1]"""
00445         op='+'
00446         ex='V0'+op+'V1'
00447         factor=[[0.1],[0.2]]
00448         v={'V0': self.rawfile,
00449            'V1': factor}
00450         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00451         self.assertEqual(res,None,
00452                          msg='Any error occurred during task execution')
00453         self._compare(self.outfile,self.rawfile,factor,op)
00454         
00455     def test109(self):
00456         """Test 109: Subtraction of 2D array of [nrow,1]"""
00457         op='-'
00458         ex='V0'+op+'V1'
00459         factor=[[0.1],[0.2]]
00460         v={'V0': self.rawfile,
00461            'V1': factor}
00462         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00463         self.assertEqual(res,None,
00464                          msg='Any error occurred during task execution')
00465         self._compare(self.outfile,self.rawfile,factor,op)
00466 
00467     def test110(self):
00468         """Test 110: Multiplication of 2D array of [nrow,1]"""
00469         op='*'
00470         ex='V0'+op+'V1'
00471         factor=[[0.1],[0.2]]
00472         v={'V0': self.rawfile,
00473            'V1': factor}
00474         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00475         self.assertEqual(res,None,
00476                          msg='Any error occurred during task execution')
00477         self._compare(self.outfile,self.rawfile,factor,op)
00478 
00479     def test111(self):
00480         """Test 111: Division of 2D array of [nrow,1]"""
00481         op='/'
00482         ex='V0'+op+'V1'
00483         factor=[[0.1],[0.2]]
00484         v={'V0': self.rawfile,
00485            'V1': factor}
00486         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00487         self.assertEqual(res,None,
00488                          msg='Any error occurred during task execution')
00489         self._compare(self.outfile,self.rawfile,factor,op)
00490 
00491     def test112(self):
00492         """Test 112: Addition of 2D array of [nrow,nchan]"""
00493         op='+'
00494         ex='V0'+op+'V1'
00495         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
00496         v={'V0': self.rawfile,
00497            'V1': factor}
00498         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00499         self.assertEqual(res,None,
00500                          msg='Any error occurred during task execution')
00501         self._compare(self.outfile,self.rawfile,factor,op)
00502         
00503     def test113(self):
00504         """Test 113: Subtraction of 2D array of [nrow,nchan]"""
00505         op='-'
00506         ex='V0'+op+'V1'
00507         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
00508         v={'V0': self.rawfile,
00509            'V1': factor}
00510         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00511         self.assertEqual(res,None,
00512                          msg='Any error occurred during task execution')
00513         self._compare(self.outfile,self.rawfile,factor,op)
00514 
00515     def test114(self):
00516         """Test 114: Multiplication of 2D array of [nrow,nchan]"""
00517         op='*'
00518         ex='V0'+op+'V1'
00519         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]] 
00520         v={'V0': self.rawfile,
00521            'V1': factor}
00522         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00523         self.assertEqual(res,None,
00524                          msg='Any error occurred during task execution')
00525         self._compare(self.outfile,self.rawfile,factor,op)
00526 
00527     def test115(self):
00528         """Test 115: Division of 2D array of [nrow,nchan]"""
00529         op='/'
00530         ex='V0'+op+'V1'
00531         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
00532         v={'V0': self.rawfile,
00533            'V1': factor}
00534         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00535         self.assertEqual(res,None,
00536                          msg='Any error occurred during task execution')
00537         self._compare(self.outfile,self.rawfile,factor,op)
00538 
00539 ###
00540 # Test on ASCII text data operation
00541 ###
00542 class sdmath_test2(unittest.TestCase,sdmath_unittest_base):
00543     """
00544     Test on ASCII text data operation
00545 
00546     Test data, sdmath0.asap, is artificial data with the following
00547     status:
00548 
00549        - nrow = 2
00550        - nchan = 4 for spectral data
00551        - all spectral values are 1.0
00552        
00553     """
00554     # Input and output names
00555     rawfile='sdmath0.asap'
00556     prefix=sdmath_unittest_base.taskname+'Test2'
00557     outfile=prefix+'.asap'
00558     factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
00559     datafile=prefix+'.dat'
00560 
00561     def setUp(self):
00562         self.res=None
00563         if (not os.path.exists(self.rawfile)):
00564             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00565         self._makedata(self.datafile,self.factor)
00566         
00567         default(sdmath)
00568 
00569     def tearDown(self):
00570         if (os.path.exists(self.rawfile)):
00571             shutil.rmtree(self.rawfile)
00572         os.system( 'rm -rf '+self.prefix+'*' )
00573 
00574     def test200(self):
00575         """Test 200: Addition of ASCII text data"""
00576         op='+'
00577         ex='V0'+op+'V1'
00578         v={'V0': self.rawfile,
00579            'V1': self.datafile}
00580         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00581         self.assertEqual(res,None,
00582                          msg='Any error occurred during task execution')
00583         self._compare(self.outfile,self.rawfile,self.factor,op)
00584         
00585     def test201(self):
00586         """Test 201: Subtraction of ASCII text data"""
00587         op='-'
00588         ex='V0'+op+'V1'
00589         v={'V0': self.rawfile,
00590            'V1': self.datafile}
00591         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00592         self.assertEqual(res,None,
00593                          msg='Any error occurred during task execution')
00594         self._compare(self.outfile,self.rawfile,self.factor,op)
00595 
00596     def test202(self):
00597         """Test 202: Multiplication of ASCII text data"""
00598         op='*'
00599         ex='V0'+op+'V1'
00600         v={'V0': self.rawfile,
00601            'V1': self.datafile}
00602         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00603         self.assertEqual(res,None,
00604                          msg='Any error occurred during task execution')
00605         self._compare(self.outfile,self.rawfile,self.factor,op)
00606 
00607     def test203(self):
00608         """Test 203: Division of ASCII text data"""
00609         op='/'
00610         ex='V0'+op+'V1'
00611         v={'V0': self.rawfile,
00612            'V1': self.datafile}
00613         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00614         self.assertEqual(res,None,
00615                          msg='Any error occurred during task execution')
00616         self._compare(self.outfile,self.rawfile,self.factor,op)
00617 
00618 ###
00619 # Test on scantable operation
00620 ###
00621 class sdmath_test3(unittest.TestCase,sdmath_unittest_base):
00622     """
00623     Test on scantable operation
00624 
00625     Test data, sdmath0.asap, is artificial data with the following
00626     status:
00627 
00628        - nrow = 2
00629        - nchan = 4 for spectral data
00630        - all spectral values are 1.0
00631 
00632     Another test data, sdmath1.asap, is artificial data with the
00633     following status:
00634 
00635        - shape is identical with sdmath0.asap
00636        - all spectral values are 0.1
00637        
00638     """
00639     # Input and output names
00640     rawfile0='sdmath0.asap'
00641     rawfile1='sdmath1.asap'
00642     prefix=sdmath_unittest_base.taskname+'Test3'
00643     outfile=prefix+'.asap'
00644 
00645     def setUp(self):
00646         self.res=None
00647         if (not os.path.exists(self.rawfile0)):
00648             shutil.copytree(self.datapath+self.rawfile0, self.rawfile0)
00649         if (not os.path.exists(self.rawfile1)):
00650             shutil.copytree(self.datapath+self.rawfile1, self.rawfile1)
00651         
00652         default(sdmath)
00653 
00654     def tearDown(self):
00655         if (os.path.exists(self.rawfile0)):
00656             shutil.rmtree(self.rawfile0)
00657         if (os.path.exists(self.rawfile1)):
00658             shutil.rmtree(self.rawfile1)
00659         os.system( 'rm -rf '+self.prefix+'*' )
00660 
00661     def test300(self):
00662         """Test 300: Addition of scantable"""
00663         op='+'
00664         ex='V0'+op+'V1'
00665         v={'V0': self.rawfile0,
00666            'V1': self.rawfile1}
00667         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00668         self.assertEqual(res,None,
00669                          msg='Any error occurred during task execution')
00670         self._checkresult(self.outfile,self.rawfile0,self.rawfile1,op)
00671 
00672     def test301(self):
00673         """Test 301: Subtraction of scantable"""
00674         op='-'
00675         ex='V0'+op+'V1'
00676         v={'V0': self.rawfile0,
00677            'V1': self.rawfile1}
00678         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00679         self.assertEqual(res,None,
00680                          msg='Any error occurred during task execution')
00681         self._checkresult(self.outfile,self.rawfile0,self.rawfile1,op)
00682 
00683     def test302(self):
00684         """Test 302: Multiplication of scantable"""
00685         op='*'
00686         ex='V0'+op+'V1'
00687         v={'V0': self.rawfile0,
00688            'V1': self.rawfile1}
00689         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00690         self.assertEqual(res,None,
00691                          msg='Any error occurred during task execution')
00692         self._checkresult(self.outfile,self.rawfile0,self.rawfile1,op)
00693 
00694     def test303(self):
00695         """Test 303: Division of scantable"""
00696         op='/'
00697         ex='V0'+op+'V1'
00698         v={'V0': self.rawfile0,
00699            'V1': self.rawfile1}
00700         res=sdmath(expr=ex,varlist=v,outfile=self.outfile,overwrite=True)
00701         self.assertEqual(res,None,
00702                          msg='Any error occurred during task execution')
00703         self._checkresult(self.outfile,self.rawfile0,self.rawfile1,op)
00704 
00705 
00706 ###
00707 # Test on scantable storage and insitu settings
00708 ###
00709 class sdmath_storageTest( unittest.TestCase, sdmath_unittest_base ):
00710     """
00711     Test on scantable sotrage and insitu.
00712 
00713     Test data, sdmath0.asap, is artificial data with the following
00714     status:
00715 
00716        - nrow = 2
00717        - nchan = 4 for spectral data
00718        - all spectral values are 1.0
00719 
00720     Another test data, sdmath1.asap, is artificial data with the
00721     following status:
00722 
00723        - shape is identical with sdmath0.asap
00724        - all spectral values are 0.1
00725 
00726     The list of tests:
00727     testMT  --- storage = 'memory', insitu = True
00728     testMF  --- storage = 'memory', insitu = False
00729     testDT  --- storage = 'disk', insitu = True
00730     testDF  --- storage = 'disk', insitu = False
00731 
00732     Note on handlings of disk storage:
00733        Task script restores unit and frame information.
00734 
00735     Tested items:
00736        1. Test of output result by self._compare.
00737        2. Units and coordinates of output scantable.
00738        3. units and coordinates of input scantables before/after run.
00739     """
00740     # Input and output names
00741     rawfile='sdmath0.asap'
00742     rawfile1='sdmath1.asap'
00743     inlist = [rawfile, rawfile1]
00744     outprefix=sdmath_unittest_base.taskname+'.Tstorage'
00745     outsuffix='.asap'
00746 
00747     # specunit is ignored in current task script (CAS-4094)
00748     #out_uc = {'spunit': 'GHz', 'flunit': 'Jy',\
00749     #          'frame': 'LSRD', 'doppler': 'OPTICAL'}
00750     out_uc = {'spunit': 'channel', 'flunit': 'Jy',\
00751               'frame': 'LSRD', 'doppler': 'OPTICAL'}
00752 
00753     def setUp( self ):
00754         for infile in [self.rawfile, self.rawfile1]:
00755             if os.path.exists(infile):
00756                 shutil.rmtree(infile)
00757             shutil.copytree(self.datapath+infile, infile)
00758         
00759         default(sdmath)
00760 
00761     def tearDown( self ):
00762         for infile in [self.rawfile, self.rawfile1]:
00763             if os.path.exists(infile):
00764                 shutil.rmtree(infile)
00765         os.system( 'rm -rf %s*'%(self.outprefix) )
00766 
00767     # helper functions of tests
00768     def _get_unit_coord( self, scanname ):
00769         # Returns a dictionary which stores units and coordinates of a
00770         # scantable, scanname. Returned dictionary stores spectral
00771         # unit, flux unit, frequency frame, and doppler of scanname.
00772         self.assertTrue(os.path.exists(scanname),\
00773                         "'%s' does not exists." % scanname)
00774         self.assertTrue(is_scantable(scanname),\
00775                         "Input table is not a scantable: %s" % scanname)
00776         scan = sd.scantable(scanname, average=False,parallactify=False)
00777         retdict = {}
00778         retdict['spunit'] = scan.get_unit()
00779         retdict['flunit'] = scan.get_fluxunit()
00780         coord = scan._getcoordinfo()
00781         retdict['frame'] = coord[1]
00782         retdict['doppler'] = coord[2]
00783         return retdict
00784 
00785     def _get_uclist( self, stlist ):
00786         # Returns a list of dictionaries of units and coordinates of
00787         # a list of scantables in stlist. This method internally calls
00788         # _get_unit_coord(scanname).
00789         retlist = []
00790         for scanname in stlist:
00791             retlist.append(self._get_unit_coord(scanname))
00792         #print retlist
00793         return retlist
00794 
00795     def _comp_unit_coord( self, stlist, before):
00796         ### stlist: a list of scantable names
00797         if isinstance(stlist,str):
00798             stlist = [ stlist ]
00799         ### before: a return value of _get_uclist() before run
00800         if isinstance(before, dict):
00801             before = [ before ]
00802         if len(stlist) != len(before):
00803             raise Exception("Number of scantables in list is different from reference data.")
00804         self.assertTrue(isinstance(before[0],dict),\
00805                         "Reference data should be (a list of) dictionary")
00806 
00807         after = self._get_uclist(stlist)
00808         for i in range(len(stlist)):
00809             print "Comparing units and coordinates of '%s'" %\
00810                   stlist[i]
00811             self._compareDictVal(after[i],before[i])
00812 
00813     def _compareDictVal( self, testdict, refdict, reltol=1.0e-5, complist=None ):
00814         self.assertTrue(isinstance(testdict,dict) and \
00815                         isinstance(refdict, dict),\
00816                         "Need to specify two dictionaries to compare")
00817         if complist:
00818             keylist = complist
00819         else:
00820             keylist = refdict.keys()
00821         
00822         for key in keylist:
00823             self.assertTrue(testdict.has_key(key),\
00824                             msg="%s is not defined in the current results."\
00825                             % key)
00826             self.assertTrue(refdict.has_key(key),\
00827                             msg="%s is not defined in the reference data."\
00828                             % key)
00829             testval = self._to_list(testdict[key])
00830             refval = self._to_list(refdict[key])
00831             self.assertTrue(len(testval)==len(refval),"Number of elemnets differs.")
00832             for i in range(len(testval)):
00833                 if isinstance(refval[i],str):
00834                     self.assertTrue(testval[i]==refval[i],\
00835                                     msg="%s[%d] differs: %s (expected: %s) " % \
00836                                     (key, i, str(testval[i]), str(refval[i])))
00837                 else:
00838                     self.assertTrue(self._isInAllowedRange(testval[i],refval[i],reltol),\
00839                                     msg="%s[%d] differs: %s (expected: %s) " % \
00840                                     (key, i, str(testval[i]), str(refval[i])))
00841             del testval, refval
00842             
00843 
00844     def _isInAllowedRange( self, testval, refval, reltol=1.0e-5 ):
00845         """
00846         Check if a test value is within permissive relative difference from refval.
00847         Returns a boolean.
00848         testval & refval : two numerical values to compare
00849         reltol           : allowed relative difference to consider the two
00850                            values to be equal. (default 0.01)
00851         """
00852         denom = refval
00853         if refval == 0:
00854             if testval == 0:
00855                 return True
00856             else:
00857                 denom = testval
00858         rdiff = (testval-refval)/denom
00859         del denom,testval,refval
00860         return (abs(rdiff) <= reltol)
00861 
00862     def _to_list( self, input ):
00863         """
00864         Convert input to a list
00865         If input is None, this method simply returns None.
00866         """
00867         import numpy
00868         listtypes = (list, tuple, numpy.ndarray)
00869         if input == None:
00870             return None
00871         elif type(input) in listtypes:
00872             return list(input)
00873         else:
00874             return [input]
00875 
00876     # Actual tests
00877     def testMT( self ):
00878         """Storage Test MT: Division of scalar on storage='memory' and insitu=T"""
00879         # Operation with a numerical value calls stmath._unaryop()
00880         tid = "MT"
00881         op = '/'
00882         ex = 'V0'+op+'V1'
00883         factor = 2.0
00884         v = {'V0': self.rawfile,
00885              'V1': factor}
00886         outfile = self.outprefix+tid+self.outsuffix
00887 
00888         # Backup units and coords of input scantable before run.
00889         initval = self._get_unit_coord(self.rawfile)
00890 
00891         sd.rcParams['scantable.storage'] = 'memory'
00892         sd.rcParams['insitu'] = True
00893         print "Running test with storage='%s' and insitu=%s" % \
00894               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00895         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
00896                      frame=self.out_uc['frame'],\
00897                      doppler=self.out_uc['doppler'],\
00898                      fluxunit=self.out_uc['flunit'],\
00899                      specunit=self.out_uc['spunit'])
00900 
00901         self.assertEqual(res,None,
00902                          msg='Any error occurred during task execution')
00903         self._compare(outfile,self.rawfile,factor,op,0.78232766)
00904         # Test  units and coords of output scantable
00905         self._comp_unit_coord(outfile,self.out_uc)
00906         # Compare units and coords of input scantable before/after run
00907         self._comp_unit_coord(self.rawfile,initval)
00908 
00909 
00910     def testMT2( self ):
00911         """Storage Test MT2: Division of scalar on storage='memory' and insitu=T without converting spectral values"""
00912         # Operation with a numerical value calls stmath._unaryop()
00913         tid = "MT"
00914         op = '/'
00915         ex = 'V0'+op+'V1'
00916         factor = 2.0
00917         v = {'V0': self.rawfile,
00918              'V1': factor}
00919         outfile = self.outprefix+tid+self.outsuffix
00920 
00921         # Backup units and coords of input scantable before run.
00922         initval = self._get_unit_coord(self.rawfile)
00923 
00924         sd.rcParams['scantable.storage'] = 'memory'
00925         sd.rcParams['insitu'] = True
00926         print "Running test with storage='%s' and insitu=%s" % \
00927               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00928         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
00929                      frame=self.out_uc['frame'],\
00930                      doppler=self.out_uc['doppler'],\
00931                      fluxunit=self.out_uc['flunit'],\
00932                      telescopeparm='FIX',\
00933                      specunit=self.out_uc['spunit'])
00934 
00935         self.assertEqual(res,None,
00936                          msg='Any error occurred during task execution')
00937         self._compare(outfile,self.rawfile,factor,op)
00938         # Test  units and coords of output scantable
00939         self._comp_unit_coord(outfile,self.out_uc)
00940         # Compare units and coords of input scantable before/after run
00941         self._comp_unit_coord(self.rawfile,initval)
00942 
00943 
00944     def testMF( self ):
00945         """Storage Test MF: Multiplication of 1D array of [nchan] on storage='memory' and insitu=F"""
00946         # Operation with a numerical value calls stmath._arrayop()
00947         tid = "MF"
00948         op='*'
00949         ex='V0'+op+'V1'
00950         factor=[0.1,0.2,0.3,0.4]
00951         v={'V0': self.rawfile,
00952            'V1': factor}
00953         outfile = self.outprefix+tid+self.outsuffix
00954         
00955         # Backup units and coords of input scantable before run.
00956         initval = self._get_unit_coord(self.rawfile)
00957 
00958         sd.rcParams['scantable.storage'] = 'memory'
00959         sd.rcParams['insitu'] = False
00960         print "Running test with storage='%s' and insitu=%s" % \
00961               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00962         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
00963                      frame=self.out_uc['frame'],\
00964                      doppler=self.out_uc['doppler'],\
00965                      fluxunit=self.out_uc['flunit'],\
00966                      specunit=self.out_uc['spunit'])
00967 
00968         self.assertEqual(res,None,
00969                          msg='Any error occurred during task execution')
00970         self._compare(outfile,self.rawfile,factor,op,0.78232766)
00971         # Test  units and coords of output scantable
00972         self._comp_unit_coord(outfile,self.out_uc)
00973         # Compare units and coords of input scantable before/after run
00974         self._comp_unit_coord(self.rawfile,initval)
00975 
00976 
00977     def testMF2( self ):
00978         """Storage Test MF2: Multiplication of 1D array of [nchan] on storage='memory' and insitu=F without converting spectral values"""
00979         # Operation with a numerical value calls stmath._arrayop()
00980         tid = "MF"
00981         op='*'
00982         ex='V0'+op+'V1'
00983         factor=[0.1,0.2,0.3,0.4]
00984         v={'V0': self.rawfile,
00985            'V1': factor}
00986         outfile = self.outprefix+tid+self.outsuffix
00987         
00988         # Backup units and coords of input scantable before run.
00989         initval = self._get_unit_coord(self.rawfile)
00990 
00991         sd.rcParams['scantable.storage'] = 'memory'
00992         sd.rcParams['insitu'] = False
00993         print "Running test with storage='%s' and insitu=%s" % \
00994               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
00995         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
00996                      frame=self.out_uc['frame'],\
00997                      doppler=self.out_uc['doppler'],\
00998                      fluxunit=self.out_uc['flunit'],\
00999                      telescopeparm='FIX',\
01000                      specunit=self.out_uc['spunit'])
01001 
01002         self.assertEqual(res,None,
01003                          msg='Any error occurred during task execution')
01004         self._compare(outfile,self.rawfile,factor,op)
01005         # Test  units and coords of output scantable
01006         self._comp_unit_coord(outfile,self.out_uc)
01007         # Compare units and coords of input scantable before/after run
01008         self._comp_unit_coord(self.rawfile,initval)
01009 
01010 
01011     def testDT( self ):
01012         """Storage Test DT: Subtraction of 2D array of [nrow,nchan] on storage='disk' and insitu=T"""
01013         # Operation of 2-D array calls asapmath._array2dOp()
01014         tid = "DT"
01015         op='-'
01016         ex='V0'+op+'V1'
01017         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
01018         v={'V0': self.rawfile,
01019            'V1': factor}
01020         outfile = self.outprefix+tid+self.outsuffix
01021 
01022         # Backup units and coords of input scantable before run.
01023         initval = self._get_unit_coord(self.rawfile)
01024 
01025         sd.rcParams['scantable.storage'] = 'disk'
01026         sd.rcParams['insitu'] = True
01027         print "Running test with storage='%s' and insitu=%s" % \
01028               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01029         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
01030                      frame=self.out_uc['frame'],\
01031                      doppler=self.out_uc['doppler'],\
01032                      fluxunit=self.out_uc['flunit'],\
01033                      specunit=self.out_uc['spunit'])
01034 
01035         self.assertEqual(res,None,
01036                          msg='Any error occurred during task execution')
01037         self._compare(outfile,self.rawfile,factor,op,0.78232766)
01038         # Test  units and coords of output scantable
01039         self._comp_unit_coord(outfile,self.out_uc)
01040         # Compare units and coords of input scantable before/after run
01041         self._comp_unit_coord(self.rawfile,initval)
01042 
01043 
01044     def testDT2( self ):
01045         """Storage Test DT2: Subtraction of 2D array of [nrow,nchan] on storage='disk' and insitu=T without converting spectral values"""
01046         # Operation of 2-D array calls asapmath._array2dOp()
01047         tid = "DT"
01048         op='-'
01049         ex='V0'+op+'V1'
01050         factor=[[0.1,0.3,0.5,0.7],[0.2,0.4,0.6,0.8]]
01051         v={'V0': self.rawfile,
01052            'V1': factor}
01053         outfile = self.outprefix+tid+self.outsuffix
01054 
01055         # Backup units and coords of input scantable before run.
01056         initval = self._get_unit_coord(self.rawfile)
01057 
01058         sd.rcParams['scantable.storage'] = 'disk'
01059         sd.rcParams['insitu'] = True
01060         print "Running test with storage='%s' and insitu=%s" % \
01061               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01062         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
01063                      frame=self.out_uc['frame'],\
01064                      doppler=self.out_uc['doppler'],\
01065                      fluxunit=self.out_uc['flunit'],\
01066                      telescopeparm='FIX',\
01067                      specunit=self.out_uc['spunit'])
01068 
01069         self.assertEqual(res,None,
01070                          msg='Any error occurred during task execution')
01071         self._compare(outfile,self.rawfile,factor,op)
01072         # Test  units and coords of output scantable
01073         self._comp_unit_coord(outfile,self.out_uc)
01074         # Compare units and coords of input scantable before/after run
01075         self._comp_unit_coord(self.rawfile,initval)
01076 
01077 
01078     def testDF( self ):
01079         """Storage Test DF: Addition of two scantables on storage='disk' and insitu=F"""
01080         # Operation of two scantables calls stmath._binaryop()
01081         tid = "DF"
01082         op='+'
01083         ex='V0'+op+'V1'
01084         v={'V0': self.rawfile,
01085            'V1': self.rawfile1}
01086         outfile = self.outprefix+tid+self.outsuffix
01087 
01088         # Backup units and coords of input scantable before run.
01089         initval = self._get_uclist([self.rawfile, self.rawfile1])
01090 
01091         sd.rcParams['scantable.storage'] = 'disk'
01092         sd.rcParams['insitu'] = False
01093         print "Running test with storage='%s' and insitu=%s" % \
01094               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01095         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
01096                      frame=self.out_uc['frame'],\
01097                      doppler=self.out_uc['doppler'],\
01098                      fluxunit=self.out_uc['flunit'],\
01099                      specunit=self.out_uc['spunit'])
01100 
01101         self.assertEqual(res,None,
01102                          msg='Any error occurred during task execution')
01103         self._checkresult(outfile,self.rawfile,self.rawfile1,op,0.78232766)
01104         # Test  units and coords of output scantable
01105         self._comp_unit_coord(outfile,self.out_uc)
01106         # Compare units and coords of input scantable before/after run
01107         self._comp_unit_coord([self.rawfile, self.rawfile1],initval)
01108         
01109     def testDF2( self ):
01110         """Storage Test DF2: Addition of two scantables on storage='disk' and insitu=F without converting spectral values"""
01111         # Operation of two scantables calls stmath._binaryop()
01112         tid = "DF"
01113         op='+'
01114         ex='V0'+op+'V1'
01115         v={'V0': self.rawfile,
01116            'V1': self.rawfile1}
01117         outfile = self.outprefix+tid+self.outsuffix
01118 
01119         # Backup units and coords of input scantable before run.
01120         initval = self._get_uclist([self.rawfile, self.rawfile1])
01121 
01122         sd.rcParams['scantable.storage'] = 'disk'
01123         sd.rcParams['insitu'] = False
01124         print "Running test with storage='%s' and insitu=%s" % \
01125               (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01126         res = sdmath(expr=ex,varlist=v,outfile=outfile,\
01127                      frame=self.out_uc['frame'],\
01128                      doppler=self.out_uc['doppler'],\
01129                      fluxunit=self.out_uc['flunit'],\
01130                      telescopeparm='FIX',\
01131                      specunit=self.out_uc['spunit'])
01132 
01133         self.assertEqual(res,None,
01134                          msg='Any error occurred during task execution')
01135         self._checkresult(outfile,self.rawfile,self.rawfile1,op)
01136         # Test  units and coords of output scantable
01137         self._comp_unit_coord(outfile,self.out_uc)
01138         # Compare units and coords of input scantable before/after run
01139         self._comp_unit_coord([self.rawfile, self.rawfile1],initval)
01140         
01141 
01142 def suite():
01143     return [sdmath_test0,sdmath_test1,
01144             sdmath_test2,sdmath_test3,
01145             sdmath_storageTest]