casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_ia_histograms.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 tool method ia.histograms()
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 # </ul>
00042 # </prerequisite>
00043 #
00044 # <etymology>
00045 # Test for the ia.histograms() tool method
00046 # </etymology>
00047 #
00048 # <synopsis>
00049 # Test the ia.histograms() tool method
00050 # </synopsis> 
00051 #
00052 # <example>
00053 #
00054 # This test runs as part of the CASA python unit test suite and can be run from
00055 # the command line via eg
00056 # 
00057 # `echo $CASAPATH/bin/casapy | sed -e 's$ $/$'` --nologger --log2term -c `echo $CASAPATH | awk '{print $1}'`/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_ia_histograms[test1,test2,...]
00058 #
00059 # </example>
00060 #
00061 # <motivation>
00062 # To provide a test standard for the ia.histograms() tool method to ensure
00063 # coding changes do not break the associated bits 
00064 # </motivation>
00065 #
00066 
00067 ###########################################################################
00068 import shutil
00069 import casac
00070 from tasks import *
00071 from taskinit import *
00072 from __main__ import *
00073 import unittest
00074 
00075 def alleqnum(x,num,tolerance=0):
00076     if len(x.shape)==1:
00077         for i in range(x.shape[0]):
00078             if not (abs(x[i]-num) < tolerance):
00079                 print "x[",i,"]=", x[i]
00080                 return false
00081     if len(x.shape)==2:
00082         for i in range(x.shape[0]):
00083             for j in range(x.shape[1]):
00084                 if not (abs(x[i][j]-num) < tolerance):
00085                     print "x[",i,"][",j,"]=", x[i][j]
00086                     return false
00087     if len(x.shape)==3:
00088         for i in range(x.shape[0]):
00089             for j in range(x.shape[1]):
00090                 for k in range(x.shape[2]):
00091                     if not (abs(x[i][j][k]-num) < tolerance):
00092                         print "x[",i,"][",j,"][",k,"]=", x[i][j][k]
00093                         return false
00094     if len(x.shape)==4:
00095         for i in range(x.shape[0]):
00096             for j in range(x.shape[1]):
00097                 for k in range(x.shape[2]):
00098                     for l in range(x.shape[3]):
00099                         if not (abs(x[i][j][k][l]-num) < tolerance):
00100                             print "x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k]
00101                             return false
00102     if len(x.shape)>4:
00103         stop('unhandled array shape in alleq')
00104     return true
00105 
00106 
00107 class ia_histograms_test(unittest.TestCase):
00108     
00109     def setUp(self):
00110         self._myia = iatool()
00111     
00112     def tearDown(self):
00113         self._myia.done()
00114     
00115     def test_general(self):
00116         """general tests"""
00117         # moved from imagetest_regression.py
00118         myia = self._myia
00119         imshape = [5,10]
00120         pixels = myia.makearray(0.0, imshape)
00121         pixels[0,0] = -100
00122         pixels[imshape[0]-1,imshape[1]-1] = 100
00123         imname = 'ia.fromarray.image'
00124         myim = myia.newimagefromarray(outfile=imname, pixels=pixels)
00125         self.assertTrue(myim)
00126         try:
00127             ok = myim.histograms(axes=[9,19])
00128         except Exception, e:
00129             print 'Caught expected Exception' + str(e)
00130             ok = false
00131         self.assertFalse(ok, 'Histograms unexpectedly did not fail (1)')
00132         
00133         nbins = 25
00134         idx = nbins/2+1
00135         out = myim.histograms(list=F, nbins=nbins)
00136         self.assertTrue(out, 'Histograms failed (1)')
00137         hists=out
00138         self.assertTrue(
00139             hists.has_key('values') and hists.has_key('counts'),
00140             'Histograms record does not have the correct fields'
00141         )
00142         self.assertTrue(
00143             len(hists['values'])==nbins and len(hists['counts'])==nbins
00144             , 'Histograms value arrays have the wrong shape (1)'
00145         )
00146         ok = hists['counts'][0]==1 and hists['counts'][nbins-1]==1
00147         ok = ok and (hists['counts'][idx-1]==(imshape[0]*imshape[1]-2))
00148         self.assertTrue(ok, 'histogram counts wrong')
00149         
00150         blc = [0,0]; trc = [4,4]
00151         r1 = rg.box(blc=blc, trc=trc)
00152         hists = myim.histograms(nbins=nbins, list=F, region=r1)
00153         self.assertTrue(hists, 'Histograms failed (2)')
00154         ok = (hists['counts'][0]==1) and (hists['counts'][nbins-1]==((trc[0]-blc[0]+1)*(trc[1]-blc[1]+1)-1))
00155         self.assertTrue(ok, 'Histograms values are wrong (2)')
00156 
00157         for j in range(imshape[1]):
00158             pixels[0,j] = -100*(j+1)
00159             pixels[imshape[0]-1,j] = 100*(j+1)
00160         ok = myim.putchunk(pixels)
00161         self.assertTrue(ok, 'putchunk failed (1)')
00162         hists = myim.histograms(nbins=nbins, list=F, axes=[0])
00163         self.assertTrue(hists, 'Histograms failed (3)')
00164         ok = list(hists['values'].shape)==[nbins,imshape[1]]
00165         ok = ok and list(hists['counts'].shape)==[nbins,imshape[1]]
00166         self.assertTrue(ok, 'Histograms value arrays have the wrong shape (2)')
00167         for j in range(imshape[1]):
00168             ok = hists['counts'][0,j]==1 and hists['counts'][nbins-1,j]==1
00169             ok = ok and alleqnum(hists['counts'][idx-1],(imshape[0]-2),tolerance=0.0001)
00170         self.assertTrue(ok, 'Histograms values are wrong (3)')
00171         
00172         hists = myim.histograms(list=F, includepix=[-5,5], nbins=25)
00173         self.assertTrue(hists, 'Histograms failed (4)')
00174         ok = hists['counts'][idx-1]==(imshape[0]*imshape[1]-(imshape[1]+imshape[1]))
00175         ok = ok and alleqnum(hists['counts'][0:(idx-2)],0,tolerance=0.0001)
00176         ok = ok and alleqnum(hists['counts'][idx:nbins],0,tolerance=0.0001)
00177         self.assertTrue(ok, 'Histograms values are wrong (4)')
00178         
00179         hists = myim.histograms(list=F, disk=T, force=T)
00180         self.assertTrue(hists, 'histograms failed (4)')
00181         hists = myim.histograms(list=F, disk=F, force=T)
00182         self.assertTrue(hists, 'histograms failed (5)')
00183         hists = myim.histograms(list=F, gauss=T, cumu=T, log=T)
00184         self.assertTrue(hists, 'histograms failed (6)')
00185 
00186         ok = myim.done()
00187         self.assertTrue(ok, 'Done failed (1)')
00188     
00189     def test_stretch(self):
00190         """ ia.histogram(): Test stretch parameter"""
00191         yy = iatool()
00192         mymask = "maskim"
00193         yy.fromshape(mymask, [200, 200, 1, 1])
00194         yy.addnoise()
00195         yy.done()
00196         shape = [200,200,1,20]
00197         yy.fromshape("", shape)
00198         yy.addnoise()
00199         self.assertRaises(
00200             Exception,
00201             yy.histograms, mask=mymask + ">0", stretch=False
00202         )
00203         zz = yy.histograms(
00204             mask=mymask + ">0", stretch=True
00205         )
00206         self.assertTrue(zz and type(zz) == type({}))
00207         yy.done()
00208     
00209 def suite():
00210     return [ia_histograms_test]