casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_rg_fromtextfile.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 method rg.fromtextfile
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 #   <li> <linkto class="task_rg_fromtextfile.py:description">rg.fromtextfile</linkto> 
00042 # </ul>
00043 # </prerequisite>
00044 #
00045 # <etymology>
00046 # Test for the rg.fromtextile method
00047 # </etymology>
00048 #
00049 # <synopsis>
00050 # Test the rg.fromtextfile method
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_rg_fromtextfile[test1,test2,...]
00059 #
00060 # </example>
00061 #
00062 # <motivation>
00063 # To provide a test standard for the rg.fromtextfile method 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 import numpy
00076 
00077 image = "imregion.fits"
00078 text1 = "goodfile1.txt"
00079 res1 = "res1.rgn"
00080 cas_3258t = "CAS-3258.txt"
00081 cas_3258r = "CAS-3258.rgn"
00082 cas_3259t = "CAS-3259.txt"
00083 cas_3259r = "CAS-3259.rgn"
00084 cas_3260t = "CAS-3260.txt"
00085 cas_3260r = "CAS-3260.rgn"
00086 
00087 def deep_equality(a, b):
00088     if (type(a) != type(b)):
00089         print "types don't match, a is a " + str(type(a)) + " b is a " + str(type(b))
00090         return False
00091     if (type(a) == dict):
00092         if (a.keys() != b.keys()):
00093             print "keys don't match, a is " + str(a.keys()) + " b is " + str(b.keys())
00094             return False
00095         for k in a.keys():
00096             if (
00097                 k == "telescope" or k == "observer"
00098                 or k == "telescopeposition"
00099             ):
00100                 continue
00101             elif (not deep_equality(a[k], b[k])):
00102                 print "dictionary member inequality a[" + str(k) \
00103                     + "] is " + str(a[k]) + " b[" + str(k) + "] is " + str(b[k])
00104                 return False
00105         return True
00106     if (type(a) == float):
00107         if not (a == b or abs((a-b)/a) <= 1e-6):
00108             print "float mismatch, a is " + str(a) + ", b is " + str(b)
00109         return a == b or abs((a-b)/a) <= 1e-6
00110     if (type(a) == numpy.ndarray):
00111         if (a.shape != b.shape):
00112             print "shape mismatch a is " + str(a.shape) + " b is " + str(b.shape)
00113             return False
00114         x = a.tolist()
00115         y = b.tolist()
00116         for i in range(len(x)):
00117             if (not deep_equality(x[i], y[i])):
00118                 print "array element mismatch, x is " + str(x[i]) + " y is " + str(y[i])
00119                 return False
00120         return True
00121     return a == b
00122 
00123 class rg_fromtextfile_test(unittest.TestCase):
00124     
00125     _fixtures = [
00126         image, text1, res1, cas_3258t, cas_3258r, cas_3259t, cas_3259r,
00127         cas_3260t, cas_3260r
00128     ]
00129     
00130     def setUp(self):
00131         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/rg.fromtextfile/'
00132         for im in self._fixtures:
00133             shutil.copy(datapath + im, im)
00134         self.ia = iatool()
00135         self.rg = rgtool()
00136     
00137     def tearDown(self):
00138         for im in self._fixtures:
00139             os.remove(im)
00140         self.ia.done()
00141         del self.ia
00142         self.rg.done()
00143         del self.rg
00144 
00145     def _testit(self, text, rgn):
00146         csys = self.ia.coordsys().torecord()
00147         shape = self.ia.shape()
00148         got = self.rg.fromtextfile(text, shape, csys)
00149         expected = self.rg.fromfiletorecord(rgn)
00150         expected['comment'] = ""
00151         self.assertTrue(deep_equality(got, expected))
00152         
00153         f = open(text, 'r')
00154         text = f.read()
00155         got = self.rg.fromtext(text, shape, csys)
00156         self.assertTrue(deep_equality(got, expected))
00157 
00158     def test_exceptions(self):
00159         """test exception cases"""
00160 
00161         # bad file
00162         self.assertRaises(Exception, self.rg.fromtextfile, "blah", {}, [1,1])
00163         # coordsys not set
00164         self.assertRaises(Exception, self.rg.fromtextfile, text1, {}, [1,1])
00165 
00166     def test_read(self):
00167         """Read test"""
00168         self.ia.open(image)
00169         self._testit(text1, res1)
00170         
00171     def test_CAS_3258(self):
00172         """Verify fix to CAS-3258"""
00173         self.ia.fromshape("", [250,250])
00174         self._testit(cas_3258t, cas_3258r)
00175         
00176     def test_CAS_3259(self):
00177         """Verify fix to CAS-3259"""
00178         self.ia.fromshape("", [250,250])
00179         self._testit(cas_3259t, cas_3259r)
00180         
00181     def test_CAS_3260(self):
00182         """Verify fix to CAS-3260"""
00183         self.ia.fromshape("", [250,250])
00184         self._testit(cas_3260t, cas_3260r)
00185         
00186     def test_CAS_4415(self):
00187         """Verify CAS-4415 (parser did not properly handle frquency decreasing with pixel number)"""
00188         shape = [50, 50, 10]
00189         self.ia.fromshape("", shape)
00190         csys = self.ia.coordsys()
00191         increment = csys.increment()["numeric"]
00192         increment[2] = -increment[2]
00193         csys.setincrement(increment)
00194         self.ia.setcoordsys(csys.torecord())
00195         zz = rg.fromtext(
00196             "circle[[20pix,20pix],6pix],range=[1pix,3pix]",
00197             shape, csys.torecord()
00198         )
00199         self.assertTrue(len(zz.keys()) > 0)
00200 
00201     def test_CAS_4425(self):
00202         """ Verify CAS-4425 (pixel units now accounted for in range and no units throws exception)"""
00203         shape = [100, 100, 80]
00204         self.ia.fromshape("", shape)
00205         csys = self.ia.coordsys()
00206         zz = rg.fromtext("box[[30pix, 30pix], [39pix, 39pix]], range=[55pix,59pix]", shape, csys.torecord())
00207         self.assertTrue(self.ia.statistics(region=zz)["npts"] == 500)
00208         zz = rg.fromtext("box[[30pix, 30pix], [39pix, 39pix]], range=[59pix,55pix]", shape, csys.torecord())
00209         self.assertTrue(self.ia.statistics(region=zz)["npts"] == 500)
00210         self.assertRaises(
00211             Exception, rg.fromtext, "box[[30pix, 30pix], [39pix, 39pix]], range=[59,55]",
00212             shape, csys.torecord()
00213         )
00214          
00215         
00216 
00217 def suite():
00218     return [rg_fromtextfile_test]