casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_impbcor.py
Go to the documentation of this file.
00001 ##########################################################################
00002 # imfit_test.py
00003 #
00004 # Copyright (C) 2008, 2009
00005 # Associated Universities, Inc. Washington DC, USA.
00006 #
00007 # This script is free software; you can redistribute it and/or modify it
00008 # under the terms of the GNU Library General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or (at your
00010 # option) any later version.
00011 #
00012 # This library is distributed in the hope that it will be useful, but WITHOUT
00013 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 # License for more details.
00016 #
00017 # You should have received a copy of the GNU Library General Public License
00018 # along with this library; if not, write to the Free Software Foundation,
00019 # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00020 #
00021 # Correspondence concerning AIPS++ should be adressed as follows:
00022 #        Internet email: aips2-request@nrao.edu.
00023 #        Postal address: AIPS++ Project Office
00024 #                        National Radio Astronomy Observatory
00025 #                        520 Edgemont Road
00026 #                        Charlottesville, VA 22903-2475 USA
00027 #
00028 # <author>
00029 # Dave Mehringer
00030 # </author>
00031 #
00032 # <summary>
00033 # Test suite for the CASA task impbcor
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 #   <li> <linkto class="task_imcollapse.py:description">impbcor</linkto> 
00042 # </ul>
00043 # </prerequisite>
00044 #
00045 # <etymology>
00046 # Test for the impbcor task
00047 # </etymology>
00048 #
00049 # <synopsis>
00050 # Test the impbcor task and the ia.pbcor() method upon which it is built.
00051 # </synopsis> 
00052 #
00053 # <example>
00054 #
00055 # This test runs as part of the CASA python unit test suite and can be run from
00056 # the command line via eg
00057 # 
00058 # `echo $CASAPATH/bin/casapy | sed -e 's$ $/$'` --nologger --log2term -c `echo $CASAPATH | awk '{print $1}'`/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_impbcor[test1,test2,...]
00059 #
00060 # </example>
00061 #
00062 # <motivation>
00063 # To provide a test standard for the impbcor task to ensure
00064 # coding changes do not break the associated bits 
00065 # </motivation>
00066 #
00067 
00068 ###########################################################################
00069 import shutil
00070 import casac
00071 from tasks import *
00072 from taskinit import *
00073 from __main__ import *
00074 import unittest
00075 
00076 im1 = "pbtest1_im.fits"
00077 pb1 = "pbtest1_pb.fits"
00078 co1_1 = "pbtest1_co1.fits"
00079 co1_2 = "pbtest1_co2.im"
00080 
00081 im2 = "pb2_im.fits"
00082 pb2 = "pb2_pb.fits"  
00083 co2 = "pb2_co.im"  
00084 
00085 data = [im1, pb1, co1_1, co1_2, im2, pb2, co2]
00086 
00087 datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/imageanalysis/ImageAnalysis/'
00088 
00089 
00090 def run_pbcor(
00091     imagename, pbimage, outfile, overwrite, region, box, chans,
00092     stokes, mask, mode, cutoff
00093 ):
00094     myia = iatool()
00095     myia.open(imagename)
00096     res = myia.pbcor(
00097         pbimage=pbimage, outfile=outfile, overwrite=overwrite,
00098         region=region, box=box, chans=chans, stokes=stokes,
00099         mask=mask, mode=mode, cutoff=cutoff
00100     )
00101     myia.close()
00102     myia.done()
00103     return res
00104 
00105 def run_impbcor(
00106     imagename, pbimage, outfile, overwrite, region, box, chans,
00107     stokes, mask, mode, cutoff, wantreturn
00108 ):
00109     return impbcor(
00110         imagename=imagename, pbimage=pbimage, outfile=outfile,
00111         overwrite=overwrite, region=region, box=box, chans=chans,
00112         stokes=stokes, mask=mask, mode=mode, cutoff=cutoff,
00113         wantreturn=wantreturn
00114     )
00115 
00116 class impbcor_test(unittest.TestCase):
00117     
00118     def setUp(self):
00119         
00120         for f in data:
00121             if os.path.isdir(datapath + f):
00122                 shutil.copytree(datapath + f, f)
00123             else:
00124                 shutil.copy(datapath + f, f)
00125     
00126     def tearDown(self):
00127         for f in data:
00128             if os.path.isdir(f):
00129                 shutil.rmtree(f)
00130             else:
00131                 os.remove(f)
00132 
00133     def checkImage(self, gotImage, expectedName):
00134         expected = iatool()                                
00135         expected.open(expectedName)
00136         got = iatool()
00137         if type(gotImage) == str:
00138             got.open(gotImage)
00139         else:
00140             got = gotImage
00141         self.assertTrue((got.shape() == expected.shape()).all())
00142         diffData = got.getchunk() - expected.getchunk()
00143         self.assertTrue(abs(diffData).max() == 0)
00144         gotCsys = got.coordsys()
00145         expectedCsys = expected.coordsys()
00146         diffPixels = gotCsys.referencepixel()['numeric'] - expectedCsys.referencepixel()['numeric']
00147         self.assertTrue(abs(diffPixels).max() == 0)
00148         denom = expectedCsys.referencevalue()['numeric']
00149         for i in range(len(denom)):
00150             if (denom[i] == 0):
00151                 denom[i] = 1
00152         fracDiffRef = (
00153             gotCsys.referencevalue()['numeric'] - expectedCsys.referencevalue()['numeric']
00154         )/denom;
00155         print "*** " + str(abs(fracDiffRef).max())
00156         self.assertTrue(abs(fracDiffRef).max() <= 1.5e-6)
00157         gotnpts = got.statistics()['npts']
00158         expnpts = expected.statistics()['npts']
00159         self.assertTrue(gotnpts == expnpts)
00160 
00161         got.close()
00162         got.done()
00163         expected.close()
00164         expected.done()
00165 
00166     def test_exceptions(self):
00167         """impbcor: Test various exception cases"""
00168         
00169         def testit(
00170             imagename, pbimage, outfile, overwrite, region,
00171             box, chans, stokes, mask, mode, cutoff, wantreturn
00172         ):
00173             for i in [0,1]:
00174                 if (i==0):
00175                     self.assertRaises(
00176                         Exception, run_pbcor, imagename=imagename,
00177                         pbimage=pbimage, outfile=outfile,
00178                         overwrite=overwrite, region=region, box=box,
00179                         chans=chans, stokes=stokes, mask=mask,
00180                         mode=mode, cutoff=cutoff
00181                     )
00182                 else:
00183                     self.assertFalse(
00184                         run_impbcor(
00185                             imagename=imagename, pbimage=pbimage,
00186                             outfile=outfile, overwrite=overwrite,
00187                             region=region, box=box, chans=chans,
00188                             stokes=stokes, mask=mask, mode=mode,
00189                             cutoff=cutoff, wantreturn=wantreturn
00190                         )
00191                     )
00192         # no image name given
00193         testit(
00194             imagename="", pbimage=pb1, outfile="",
00195             overwrite=False, region="", box="",
00196             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00197             wantreturn=True
00198         )
00199         # bad image name given
00200         testit(
00201             imagename="totally_bogus", pbimage=pb1, outfile="",
00202             overwrite=False, region="", box="",
00203             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00204             wantreturn=True
00205         )
00206         # no pbimage name given
00207         testit(
00208             imagename=im1, pbimage="", outfile="",
00209             overwrite=False, region="", box="",
00210             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00211             wantreturn=True
00212         )
00213         # bad pbimage name given
00214         testit(
00215             imagename=im1, pbimage="totally_bogus2", outfile="",
00216             overwrite=False, region="", box="",
00217             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00218             wantreturn=True
00219         )
00220         # unwritable outfile
00221         testit(
00222             imagename=im1, pbimage=pb1, outfile="/bogusplace/bogusimage",
00223             overwrite=False, region="", box="",
00224             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00225             wantreturn=True
00226         )
00227         # bogus region
00228         testit(
00229             imagename=im1, pbimage=pb1, outfile="",
00230             overwrite=False, region="bogus_region", box="",
00231             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00232             wantreturn=True
00233         )
00234         # bad mode
00235         testit(
00236             imagename=im1, pbimage=pb1, outfile="",
00237             overwrite=False, region="", box="",
00238             chans="", stokes="", mask="", mode="zz", cutoff=-1.0,
00239             wantreturn=True
00240         )
00241         # incompatible image and pb
00242         testit(
00243             imagename=im1, pbimage=pb2, outfile="",
00244             overwrite=False, region="", box="",
00245             chans="", stokes="", mask="", mode="d", cutoff=-1.0,
00246             wantreturn=True
00247         )
00248 
00249     def _testit(
00250         self, expected, imagename, pbimage, overwrite, region, box,
00251         chans, stokes, mask, mode, cutoff
00252     ):
00253         myia = iatool()
00254         myia.open(pbimage)
00255         pbpix = myia.getchunk()
00256         myia.done()
00257         del myia
00258         for j in [0, 1]:
00259             for i in [0, 1]:
00260                 outfile = str(i) + ".im"
00261                 if j == 1:
00262                     pbimage = pbpix
00263                 if i == 0:
00264                     mytool = run_pbcor(
00265                         imagename=imagename, pbimage=pbimage,
00266                         outfile=outfile, overwrite=overwrite,
00267                         region=region, box=box, chans=chans,
00268                         stokes=stokes, mask=mask, mode=mode,
00269                         cutoff=cutoff
00270                     )
00271                             
00272                     self.assertTrue(type(mytool) == type(ia))
00273                     self.checkImage(mytool, expected)
00274                     self.checkImage(outfile, expected)
00275                     shutil.rmtree(outfile)
00276                 else:
00277                     for wantreturn in [True, False]:
00278                         outfile = outfile + str(wantreturn)
00279                         mytool = run_impbcor(
00280                             imagename=imagename, pbimage=pbimage,
00281                             outfile=outfile, overwrite=overwrite,
00282                             region=region, box=box, chans=chans,
00283                             stokes=stokes, mask=mask, mode=mode,
00284                             cutoff=cutoff, wantreturn=wantreturn
00285                         )
00286                         if (wantreturn):
00287                             print "*** " + str(type(mytool)) + " " + str(type(ia))
00288                             self.assertTrue(type(mytool) == type(ia))
00289                             self.checkImage(mytool, expected)
00290                         else:
00291                             self.assertTrue(mytool == None)
00292                             self.checkImage(outfile, expected)
00293                         shutil.rmtree(outfile)
00294 
00295     def test_1(self):
00296         """impbcor: Test full image divide"""
00297         self._testit(
00298             expected=co1_1, imagename=im1, pbimage=pb1,
00299             overwrite=False, region="", box="",
00300             chans="", stokes="", mask="", mode="d",
00301             cutoff=-1.0
00302         )
00303 
00304     def test_2(self):
00305         """impbcor: Test full image divide with cutoff"""
00306         self._testit(
00307             expected=co1_2, imagename=im1, pbimage=pb1,
00308             overwrite=False, region="", box="",
00309             chans="", stokes="", mask="", mode="d",
00310             cutoff=0.001
00311         )
00312     
00313     def test_3(self):
00314         """impbcor: Test full image divide with cutoff. Primary beam is 2 D, image is 4 D"""
00315         self._testit(
00316             expected=co2, imagename=im2, pbimage=pb2,
00317             overwrite=False, region="", box="",
00318             chans="", stokes="", mask="", mode="d",
00319             cutoff=0.001
00320         )
00321 
00322     def test_stretch(self):
00323         """ ia.pbcor(): Test stretch parameter"""
00324         yy = iatool()
00325         mymask = "maskim"
00326         yy.fromshape("", [113, 76, 1, 1])
00327         yy.addnoise()
00328         xx = yy.transpose(mymask, "0132")
00329         yy.done()
00330         xx.done()
00331         for i in [0,1]:
00332             if i == 0:
00333                 yy.open(im2)
00334                 self.assertRaises(
00335                     Exception,
00336                     yy.pbcor, pbimage=pb2,
00337                     mask=mymask + ">0", stretch=False
00338                 )
00339                 zz = yy.pbcor(
00340                     pbimage=pb2, mask=mymask + ">0", stretch=True
00341                 )
00342                 self.assertTrue(type(yy) == type(zz))
00343                 yy.done()
00344             else:
00345                 zz = impbcor(
00346                     imagename=im2, pbimage=pb2,
00347                     mask=mymask + ">0", stretch=False,
00348                     wantreturn=True
00349                 )
00350                 self.assertTrue(zz == None)
00351                 zz = impbcor(
00352                     imagename=im2, pbimage=pb2, mask=mymask + ">0", stretch=True,
00353                     wantreturn=True
00354                 )
00355                 self.assertTrue(type(zz) == type(ia))
00356             zz.done()
00357         
00358 
00359 def suite():
00360     return [impbcor_test]