00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
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
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]