casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_imtrans.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 imtrans
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 #   <li> <linkto class="task_imtrans.py:description">imtrans</linkto> 
00042 # </ul>
00043 # </prerequisite>
00044 #
00045 # <etymology>
00046 # Test for the imtrans task
00047 # </etymology>
00048 #
00049 # <synopsis>
00050 # Test the imtrans task and the ia.reorder() 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_imtrans[test1,test2,...]
00059 #
00060 # </example>
00061 #
00062 # <motivation>
00063 # To provide a test standard for the imtrans 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 good_image = "reorder_in.fits"
00077 cas_2364im = "CAS-2364.im"
00078 
00079 def run_transpose(imagename, outfile, order):
00080     myia = iatool()
00081     myia.open(imagename)
00082     print "*** order " + str(order)
00083     res = myia.transpose(outfile=outfile, order=order)
00084     myia.close()
00085     return res
00086 
00087 def run_imtrans(imagename, outfile, order):
00088     return imtrans(imagename=imagename, outfile=outfile, order=order)
00089 
00090 
00091 class imtrans_test(unittest.TestCase):
00092     
00093     def setUp(self):
00094         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/imtrans/'
00095         shutil.copy(datapath + good_image, good_image)
00096     
00097     def tearDown(self):
00098         os.remove(good_image)
00099 
00100     def test_exceptions(self):
00101         """imtrans: Test various exception cases"""
00102         
00103         def testit(imagename, outfile, order):
00104             for i in [0,1]:
00105                 if (i==0):
00106                     self.assertRaises(Exception, run_transpose, imagename, outfile, order)
00107                 else:
00108                     self.assertFalse(run_imtrans(imagename, outfile, order))
00109 
00110         # blank imagename
00111         testit("", "blah", "012")
00112         
00113         # not enough specified axes
00114         testit(good_image, "blah", "01")
00115         testit(good_image, "blah", 10)
00116         testit(good_image, "blah", ["r", "d"])
00117         
00118         # too many specified axes
00119         testit(good_image, "blah", "0123")
00120         testit(good_image, "blah", 1230)
00121         testit(good_image, "blah", ["r", "d", "f", "s"])
00122 
00123         # Bogus axes specification
00124         testit(good_image, "blah", "123")
00125         testit(good_image, "blah", ["r", "d", "s"])
00126         testit(good_image, "blah", ["r", "d", "r"])
00127         testit(good_image, "blah", 103)
00128         
00129     def test_straight_copy(self):
00130         """No actual transposing"""
00131         imagename = good_image
00132         myia = iatool()
00133         myia.open(imagename)
00134         expecteddata = myia.getchunk()
00135         expectednames = myia.coordsys().names()
00136         myia.close()
00137         count = 0
00138         for order in ["012", 12, ['r', 'd', 'f'], ["righ", "declin", "freq"]]:
00139             for code in [run_transpose, run_imtrans]:
00140                 newim = code(imagename, "straight_copy_" + str(count), order)
00141                 gotdata = newim.getchunk()
00142                 gotnames = newim.coordsys().names()
00143                 newim.close()
00144                 self.assertTrue((expecteddata == gotdata).all())
00145                 self.assertTrue(expectednames == gotnames)
00146                 count += 1
00147 
00148     def test_transpose(self):
00149         """Test transposing"""
00150         imagename = good_image
00151         myia = iatool()
00152         myia.open(imagename)
00153         expecteddata = myia.getchunk()
00154         expectednames = myia.coordsys().names()
00155         myia.done()
00156         count = 0
00157         for order in ["120", 120, ['d', 'f', 'r'], ["declin", "freq", "righ"]]:
00158             for code in [run_transpose, run_imtrans]:
00159                 for outname in ["transpose_" + str(count), ""]:
00160                     newim = code(imagename, outname, order)
00161                     gotdata = newim.getchunk()
00162                     inshape = expecteddata.shape
00163                     for i in range(inshape[0]):
00164                         for j in range(inshape[1]):
00165                             for k in range(inshape[2]):
00166                                 self.assertTrue(expecteddata[i][j][k] == gotdata[j][k][i])
00167                     gotnames = newim.coordsys().names()
00168                     newim.close()
00169                     self.assertTrue(expectednames[0] == gotnames[2])
00170                     self.assertTrue(expectednames[1] == gotnames[0])
00171                     self.assertTrue(expectednames[2] == gotnames[1])
00172                     count += 1
00173 
00174     def test_cas_2364(self):
00175         "test CAS-2364 fix"
00176         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/imtrans/'
00177         shutil.copytree(datapath + cas_2364im, cas_2364im)
00178         order="0132"
00179         out1 = "blah.im"
00180         myia = iatool()
00181         myia.open(cas_2364im)
00182         myia.transpose(out1, order)
00183         myia.close()
00184         # to verify fix, just open the image. bug was that exception was thrown when opening output from reorder
00185         myia.open(out1)
00186         self.assertTrue(myia)
00187         myia.close()
00188         out1 = "blah2.im"
00189         imtrans(outfile=out1, order=order)
00190         myia.open(out1)
00191         self.assertTrue(myia)
00192         myia.close()
00193         shutil.rmtree(cas_2364im)
00194 
00195 
00196 def suite():
00197     return [imtrans_test]