casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdtpimaging.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 
00012 from sdtpimaging import sdtpimaging
00013 import asap as sd
00014 
00015 # Unit test of sdtpimaging task.
00016 
00017 ###
00018 # Base class for sdtpimaging unit test
00019 ###
00020 class sdtpimaging_unittest_base:
00021     """
00022     Base class for sdtpimaging unit test.
00023     """
00024     taskname='sdtpimaging'
00025     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdtpimaging/'
00026     allowance=0.01
00027     
00028     def _compare(self, refdata, outdata):
00029         self._checkfile(outdata)
00030         refstat = imstat( imagename=refdata )
00031         outstat = imstat( imagename=outdata )
00032         keys = ['max', 'min', 'mean', 'maxpos', 'minpos', 'rms' ]
00033         for key in keys:
00034             ref = refstat[key]
00035             out = outstat[key]
00036             print 'stat %s: ref %s, out %s'%(key,ref,out)
00037             for i in xrange( len(ref) ):
00038                 if ref[i] != 0.0:
00039                     diff = (out[i]-ref[i])/ref[i]
00040                     self.assertTrue( all( abs(diff.flatten()) < self.allowance ),
00041                                      msg='statistics %s differ: reference %s, output %s'%(key,ref,out) )
00042                 else:
00043                     self.assertTrue( out[i] == 0.0,
00044                                      msg='statistics %s differ: reference %s, output %s'%(key,ref,out) )
00045 
00046     def _checkfile( self, name ):
00047         isthere=os.path.exists(name)
00048         self.assertEqual(isthere,True,
00049                          msg='output file %s was not created because of the task failure'%(name))
00050 
00051 
00052 ###
00053 # Test on bad parameter settings, data selection, ...
00054 ###
00055 class sdtpimaging_test0(unittest.TestCase,sdtpimaging_unittest_base):
00056     """
00057     Test on bad parameter settings
00058     """
00059     # Input and output names
00060     infile='tpimaging.ms'
00061     prefix=sdtpimaging_unittest_base.taskname+'Test0'
00062     outfile=prefix+'.ms'
00063     outimage=prefix+'.im'
00064 
00065     def setUp(self):
00066         self.res=None
00067         if (not os.path.exists(self.infile)):
00068             shutil.copytree(self.datapath+self.infile, self.infile)
00069 
00070         default(sdtpimaging)
00071 
00072     def tearDown(self):
00073         if (os.path.exists(self.infile)):
00074             shutil.rmtree(self.infile)
00075         os.system( 'rm -rf '+self.prefix+'*' )
00076 
00077     def test000(self):
00078         """Test 000: Default parameters"""
00079         # argument verification error
00080         self.res=sdtpimaging()
00081         self.assertFalse(self.res)
00082         
00083     def test001(self):
00084         """Test 001: Bad antenna id"""
00085         try:
00086             self.res=sdtpimaging(infile=self.infile,spw=2,antenna='99')
00087             self.assertTrue(False,
00088                             msg='The task must throw exception')
00089         except Exception, e:
00090             pos=str(e).find('No matching antenna ID or name in the data, please check antenna parameter')
00091             self.assertNotEqual(pos,-1,
00092                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00093 
00094     def test002(self):
00095         """Test 002: Bad stokes string"""
00096         try:
00097             self.res=sdtpimaging(infile=self.infile,spw=2,stokes='J')
00098             self.assertTrue(False,
00099                             msg='The task must throw exception')
00100         except Exception, e:
00101             pos=str(e).find('stokes=J specified but the data contains only [\'XX\']')
00102             self.assertNotEqual(pos,-1,
00103                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00104         
00105     def test003(self):
00106         """Test 003: Try to create image without output image name"""
00107         try:
00108             self.res=sdtpimaging(infile=self.infile,spw=2,createimage=True,outfile='')
00109             self.assertTrue(False,
00110                             msg='The task must throw exception')
00111         except Exception, e:
00112             pos=str(e).find('Please specify out image name')
00113             self.assertNotEqual(pos,-1,
00114                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00115 
00116     def test004(self):
00117         """Test 004: Negative imsize"""
00118         try:
00119             self.res=sdtpimaging(infile=self.infile,spw=2,createimage=True,outfile=self.outimage,imsize=[-1])
00120             self.assertTrue(False,
00121                             msg='The task must throw exception')
00122         except Exception, e:
00123             # failed to create image so that outimage not exist
00124             pos=str(e).find('%s does not exist'%(self.outimage))
00125             self.assertNotEqual(pos,-1,
00126                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00127 
00128 # this test is meaningless w.r.t. its original purpose
00129 #    def test005(self):
00130 #        """Test 005: Negative cell size"""
00131 #        self.res=sdtpimaging(infile=self.infile,spw=2,createimage=True,outfile=self.outimage,cell=[-1])
00132 #        self.assertFalse(self.res)
00133 
00134     def test006(self):
00135         """Test 006: Bad phase center string"""
00136         try:
00137             self.res=sdtpimaging(infile=self.infile,spw=2,createimage=True,outfile=self.outimage,phasecenter='XXX')
00138             self.assertTrue(False,
00139                             msg='The task must throw exception')
00140         except Exception, e:
00141             pos=str(e).find('Could not interprete phasecenter parameter')
00142             self.assertNotEqual(pos,-1,
00143                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00144 
00145     def test007(self):
00146         """Test 007: Bad pointing column name"""
00147         # argument verification error
00148         self.res=sdtpimaging(infile=self.infile,createimage=True,outfile=self.outimage,pointingcolumn='XXX')
00149         self.assertFalse(self.res)
00150 
00151     def test008(self):
00152         """Test 008: Unexisting grid function"""
00153         # argument verification error
00154         self.res=sdtpimaging(infile=self.infile,createimage=True,outfile=self.outimage,gridfunction='XXX')
00155         self.assertFalse(self.res)
00156  
00157     def test009(self):
00158         """Test 009: Invalid calmode"""
00159         # argument verification error
00160         self.res=sdtpimaging(infile=self.infile,calmode='ps')
00161         self.assertFalse(self.res)
00162  
00163 
00164 ###
00165 # Test to image data without spatial baseline subtraction
00166 ###
00167 class sdtpimaging_test1(unittest.TestCase,sdtpimaging_unittest_base):
00168     """
00169     Test to image data without spatial baseline subtraction
00170     """
00171     # Input and output names
00172     infile='tpimaging.ms'
00173     prefix=sdtpimaging_unittest_base.taskname+'Test1'
00174     outfile=prefix+'.ms'
00175     outimage=prefix+'.im'
00176     refimage='nobaseline.im'
00177 
00178     def setUp(self):
00179         self.res=None
00180         if (not os.path.exists(self.infile)):
00181             shutil.copytree(self.datapath+self.infile, self.infile)
00182         if (not os.path.exists(self.refimage)):
00183             shutil.copytree(self.datapath+self.refimage, self.refimage)
00184 
00185         default(sdtpimaging)
00186 
00187     def tearDown(self):
00188         if (os.path.exists(self.infile)):
00189             shutil.rmtree(self.infile)
00190         if (os.path.exists(self.refimage)):
00191             shutil.rmtree(self.refimage)
00192         os.system( 'rm -rf '+self.prefix+'*' )
00193         os.system( 'rm -rf '+self.infile+'*' )
00194 
00195     def test100(self):
00196         """Test 100: test to image data without spatial baseline subtraction"""
00197         self.res=sdtpimaging(infile=self.infile,calmode='none',stokes='XX',spw=2,createimage=True,outfile=self.outimage,imsize=[64],cell=['15arcsec'],phasecenter='J2000 05h35m07s -5d21m00s',pointingcolumn='direction',gridfunction='SF')
00198         self.assertEqual(self.res,None)
00199         self._compare(self.refimage,self.outimage)
00200                 
00201         
00202 ###
00203 # Test to image data with spatial baseline subtraction
00204 ###
00205 class sdtpimaging_test2(unittest.TestCase,sdtpimaging_unittest_base):
00206     """
00207     Test to image data with spatial baseline subtraction
00208     """
00209     # Input and output names
00210     infile='tpimaging.ms'
00211     prefix=sdtpimaging_unittest_base.taskname+'Test2'
00212     outfile=prefix+'.ms'
00213     outimage=prefix+'.im'
00214     refimage='dobaseline.im'
00215 
00216     def setUp(self):
00217         self.res=None
00218         if (not os.path.exists(self.infile)):
00219             shutil.copytree(self.datapath+self.infile, self.infile)
00220         if (not os.path.exists(self.refimage)):
00221             shutil.copytree(self.datapath+self.refimage, self.refimage)
00222 
00223         default(sdtpimaging)
00224 
00225     def tearDown(self):
00226         if (os.path.exists(self.infile)):
00227             shutil.rmtree(self.infile)
00228         if (os.path.exists(self.refimage)):
00229             shutil.rmtree(self.refimage)
00230         os.system( 'rm -rf '+self.prefix+'*' )
00231         os.system( 'rm -rf '+self.infile+'*' )
00232 
00233     def test200(self):
00234         """Test 200: test to image data with spatial baseline subtraction"""
00235         self.res=sdtpimaging(infile=self.infile,calmode='baseline',masklist=[10,10],blpoly=1,stokes='XX',spw=2,createimage=True,outfile=self.outimage,imsize=[64],cell=['15arcsec'],phasecenter='J2000 05h35m07s -5d21m00s',pointingcolumn='direction',gridfunction='SF')
00236         self.assertEqual(self.res,None)
00237         self._compare(self.refimage,self.outimage)
00238         
00239 
00240 def suite():
00241     return [sdtpimaging_test0,sdtpimaging_test1,sdtpimaging_test2]