00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 import math
00009 import numpy
00010
00011
00012
00013
00014
00015 '''
00016 Unit tests for task imstat.
00017 '''
00018 class imstat_test(unittest.TestCase):
00019
00020
00021 moment = 'moment_map.im'
00022 s150 = '150arcsec_pix.im'
00023 s15 = '15arcsec_pix.im'
00024 s0_015 = '0.015arcsec_pix.im'
00025 s0_0015 = '0.0015arcsec_pix.im'
00026 s0_00015 = '0.00015arcsec_pix.im'
00027 linear_coords = 'linearCoords.fits'
00028 fourdim = '4dim.im'
00029 res = None
00030
00031 def setUp(self):
00032 self.res = None
00033 self._myia = iatool()
00034 default(clean)
00035 self.datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/imstat/'
00036
00037 def tearDown(self):
00038 self._myia.done()
00039 for dir in [
00040 self.moment, self.s150, self.s15, self.s0_015, self.s0_0015,
00041 self.s0_00015, self.linear_coords, self.fourdim
00042 ]:
00043 if os.path.isfile(dir):
00044 os.remove(dir)
00045 elif (os.path.exists(dir)):
00046 shutil.rmtree(dir)
00047
00048 def test001(self):
00049 """Test 1: verify moment maps can have flux densities computed in statistics"""
00050 shutil.copytree(self.datapath+self.moment, self.moment)
00051 stats = imstat(imagename=self.moment)
00052 mean = stats['mean']
00053 npts = stats['npts']
00054
00055 _myia = iatool()
00056 _myia.open(self.moment)
00057 summary = _myia.summary()
00058 _myia.close()
00059 rainc = qa.abs(qa.quantity(summary['incr'][0],'rad'))
00060 rainc = qa.convert(rainc,'arcsec')
00061 decinc = qa.abs(qa.quantity(summary['incr'][1],'rad'))
00062 decinc = qa.convert(decinc,'arcsec')
00063 beam = summary['restoringbeam']
00064 major = beam['major']
00065 minor = beam['minor']
00066 pixperbeam = qa.div(qa.mul(major,minor),(qa.mul(rainc,decinc)))['value']*(math.pi/(4*math.log(2)))
00067 got = stats['flux'][0]
00068 expected = (mean*npts/pixperbeam)[0]
00069 self.assertTrue(abs(got - expected) < 1e-11)
00070
00071 def test002(self):
00072 """ Test 2: test position format for 150 arcsec pixel image is correct """
00073 shutil.copytree(self.datapath+self.s150, self.s150)
00074 _myia = iatool()
00075 _myia.open(self.s150)
00076 stats = _myia.statistics()
00077 _myia.close()
00078 self.assertTrue(stats['blcf'] == '15:43:21.873, -00.17.47.274, I, 1.41332e+09Hz')
00079 self.assertTrue(stats['maxposf'] == '15:22:40.165, +05.11.29.923, I, 1.41332e+09Hz')
00080 self.assertTrue(stats['minposf'] == '15:43:25.618, +04.22.40.617, I, 1.41332e+09Hz')
00081 self.assertTrue(stats['trcf'] == '15:00:27.115, +10.20.37.699, I, 1.41332e+09Hz')
00082
00083 def test003(self):
00084 """ Test 3: test position format for 15 arcsec pixel image is correct """
00085 shutil.copytree(self.datapath+self.s15, self.s15)
00086 _myia = iatool()
00087 _myia.open(self.s15)
00088 print "*** before "
00089 stats = _myia.statistics()
00090 print "*** after"
00091 _myia.close()
00092 self.assertTrue(stats['blcf'] == '15:24:08.404, +04.31.59.181, I, 1.41332e+09Hz')
00093 self.assertTrue(stats['maxposf'] == '15:22:04.016, +05.04.44.999, I, 1.41332e+09Hz')
00094 self.assertTrue(stats['minposf'] == '15:24:08.491, +04.59.59.208, I, 1.41332e+09Hz')
00095 self.assertTrue(stats['trcf'] == '15:19:52.390, +05.35.44.246, I, 1.41332e+09Hz')
00096
00097 def test004(self):
00098 """ Test 4: test position format for 0.015 arcsec pixel image is correct """
00099 shutil.copytree(self.datapath+self.s0_015, self.s0_015)
00100 _myia = iatool()
00101 _myia.open(self.s0_015)
00102 stats = _myia.statistics()
00103 _myia.close()
00104 self.assertTrue(stats['blcf'] == '15:22:00.1285, +05.03.58.0800, I, 1.41332e+09Hz')
00105 self.assertTrue(stats['maxposf'] == '15:22:00.0040, +05.04.00.0450, I, 1.41332e+09Hz')
00106 self.assertTrue(stats['minposf'] == '15:22:00.1285, +05.03.59.7600, I, 1.41332e+09Hz')
00107 self.assertTrue(stats['trcf'] == '15:21:59.8725, +05.04.01.9050, I, 1.41332e+09Hz')
00108
00109 def test005(self):
00110 """ Test 5: test position format for 0.0015 arcsec pixel image is correct """
00111 print "*** test 5"
00112 _myia = iatool()
00113 shutil.copytree(self.datapath+self.s0_0015, self.s0_0015)
00114 _myia.open(self.s0_0015)
00115 stats = _myia.statistics()
00116 _myia.close()
00117 print "*** blcf xxx " + str(stats['blcf'])
00118 self.assertTrue(stats['blcf'] == '15:22:00.01285, +05.03.59.80800, I, 1.41332e+09Hz')
00119 self.assertTrue(stats['maxposf'] == '15:22:00.00040, +05.04.00.00450, I, 1.41332e+09Hz')
00120 self.assertTrue(stats['minposf'] == '15:22:00.01285, +05.03.59.97600, I, 1.41332e+09Hz')
00121 self.assertTrue(stats['trcf'] == '15:21:59.98725, +05.04.00.19050, I, 1.41332e+09Hz')
00122
00123 def test006(self):
00124 """ Test 6: test position format for 0.00015 arcsec pixel image is correct """
00125 shutil.copytree(self.datapath+self.s0_00015, self.s0_00015)
00126 _myia = iatool()
00127 _myia.open(self.s0_00015)
00128 stats = _myia.statistics()
00129 _myia.close()
00130 self.assertTrue(stats['blcf'] == '15:22:00.001285, +05.03.59.980800, I, 1.41332e+09Hz')
00131 self.assertTrue(stats['maxposf'] == '15:22:00.000040, +05.04.00.000450, I, 1.41332e+09Hz')
00132 self.assertTrue(stats['minposf'] == '15:22:00.001285, +05.03.59.997600, I, 1.41332e+09Hz')
00133 self.assertTrue(stats['trcf'] == '15:21:59.998725, +05.04.00.019050, I, 1.41332e+09Hz')
00134
00135 def test007(self):
00136 """ Test 7: test that box parameter can have spaces, CAS-2050 """
00137 shutil.copytree(self.datapath+self.s0_00015, self.s0_00015)
00138 box = '0, 0, 1 , 1'
00139 stats = imstat(imagename=self.s0_00015, box=box)
00140 self.assertTrue(stats['npts'] == 4)
00141
00142 def test008(self):
00143 """ Test 8: verify fix for CAS-2195"""
00144 def test_statistics(image):
00145 _myia = iatool()
00146 _myia.open(myim)
00147 stats = _myia.statistics()
00148 _myia.done()
00149 return stats
00150
00151 def test_imstat(image):
00152 return imstat(image)
00153
00154 myim = self.linear_coords
00155 shutil.copy(self.datapath + myim, myim)
00156 expected_max = [3, 10]
00157 expected_min = [4, 0]
00158 for code in [test_statistics, test_imstat]:
00159 stats = code(myim)
00160 self.assertTrue((stats['maxpos'] == expected_max).all())
00161 self.assertTrue((stats['minpos'] == expected_min).all())
00162
00163 def test009(self):
00164 """ Test 9: choose axes works"""
00165 def test_statistics(image, axes):
00166 _myia = iatool()
00167 _myia.open(myim)
00168 stats = _myia.statistics(axes=axes)
00169 _myia.done()
00170 return stats
00171
00172 def test_imstat(image, axes):
00173 return imstat(image, axes=axes)
00174
00175 myim = self.fourdim
00176 shutil.copytree(self.datapath + myim, myim)
00177 axes = [-1, [0, 1, 2], [0, 1], 3]
00178 expected_mean = [
00179 [59.5], [ 57.5, 58.5, 59.5, 60.5, 61.5],
00180 [
00181 [50., 51., 52., 53., 54.],
00182 [55., 56., 57., 58., 59.],
00183 [60., 61., 62., 63., 64.],
00184 [65., 66., 67., 68., 69.]
00185 ],
00186 [
00187 [
00188 [2., 7., 12., 17.],
00189 [22., 27., 32., 37.],
00190 [42., 47., 52., 57.]
00191 ],
00192 [
00193 [62., 67., 72., 77.],
00194 [ 82., 87., 92., 97.],
00195 [ 102., 107., 112., 117.]
00196 ]
00197 ]
00198 ]
00199 expected_sumsq = [
00200 [568820], [ 108100., 110884., 113716., 116596., 119524.],
00201 [
00202 [ 22000., 22606., 23224., 23854., 24496.],
00203 [ 25150., 25816., 26494., 27184., 27886.],
00204 [ 28600., 29326., 30064., 30814., 31576.],
00205 [ 32350., 33136., 33934., 34744., 35566.]
00206 ],
00207 [
00208 [
00209 [ 3.00000000e+01, 2.55000000e+02, 7.30000000e+02, 1.45500000e+03],
00210 [ 2.43000000e+03, 3.65500000e+03, 5.13000000e+03, 6.85500000e+03],
00211 [ 8.83000000e+03, 1.10550000e+04, 1.35300000e+04, 1.62550000e+04]
00212 ],
00213 [
00214 [ 1.92300000e+04, 2.24550000e+04, 2.59300000e+04, 2.96550000e+04],
00215 [ 3.36300000e+04, 3.78550000e+04, 4.23300000e+04, 4.70550000e+04],
00216 [ 5.20300000e+04, 5.72550000e+04, 6.27300000e+04, 6.84550000e+04]
00217 ]
00218 ]
00219 ]
00220 for i in range(len(axes)):
00221 for code in [test_statistics, test_imstat]:
00222 stats = code(myim, axes[i])
00223 self.assertTrue((stats['mean'] == expected_mean[i]).all())
00224 self.assertTrue((stats['sumsq'] == expected_sumsq[i]).all())
00225
00226 """
00227 def test_robust(self):
00228 *"" Confirm robust parameter*""
00229 def test_statistics(image, robust):
00230 _myia = iatool()
00231 _myia.open(myim)
00232 stats = _myia.statistics(robust=robust)
00233 _myia.done()
00234 return stats
00235 def test_imstat(image, robust):
00236 return imstat(image, robust=robust)
00237 myim = self.fourdim
00238 shutil.copytree(self.datapath + myim, myim)
00239 for robust in [True, False]:
00240 for code in [test_statistics, test_imstat]:
00241 stats = code(myim, robust)
00242 self.assertTrue(stats.has_key('median') == robust)
00243 self.assertTrue(stats.has_key('medabsdevmed') == robust)
00244 self.assertTrue(stats.has_key('quartile') == robust)
00245
00246 """
00247
00248 def test_stretch(self):
00249 """ ia.statistics(): Test stretch parameter"""
00250 yy = iatool()
00251 mymask = "maskim"
00252 yy.fromshape(mymask, [200, 200, 1, 1])
00253 yy.addnoise()
00254 yy.done()
00255 shape = [200,200,1,20]
00256 imagename = "tmp.im"
00257 yy.fromshape(imagename, shape)
00258 yy.addnoise()
00259 self.assertRaises(
00260 Exception,
00261 yy.statistics,
00262 mask=mymask + ">0", stretch=False
00263 )
00264 zz = yy.statistics(
00265 mask=mymask + ">0", stretch=True
00266 )
00267 self.assertTrue(zz and type(zz) == type({}))
00268 yy.done()
00269
00270 zz = imstat(
00271 imagename=imagename, mask=mymask + ">0", stretch=False
00272 )
00273 self.assertTrue(type(zz) == type({}) and (zz == {}))
00274
00275 zz = imstat(
00276 imagename=imagename, mask=mymask + ">0", stretch=True
00277 )
00278 self.assertTrue(type(zz) == type({}) and (not zz == {}))
00279 yy.done()
00280
00281 def test010(self):
00282 """test logfile """
00283 def test_statistics(image, axes, logfile, append):
00284 _myia = iatool()
00285 _myia.open(image)
00286 stats = _myia.statistics(
00287 axes=axes, logfile=logfile, append=append
00288 )
00289 _myia.done()
00290 return stats
00291
00292 def test_imstat(image, axes, logfile, append):
00293 return imstat(image, axes=axes, logfile=logfile, append=append)
00294
00295 logfile = "imstat.log"
00296 i = 1
00297 myim = self.fourdim
00298 shutil.copytree(self.datapath + myim, myim)
00299 for code in [test_statistics, test_imstat]:
00300 append = False
00301 if i == 2:
00302 append = True
00303 stats = code(myim, [0], logfile, append)
00304 size = os.path.getsize(logfile)
00305 print "size " + str(size)
00306
00307 self.assertTrue(size > 1.2e4*i and size < 1.3e4*i )
00308 i = i+1
00309
00310
00311 def test011(self):
00312 """ test multiple region support"""
00313 shape = [10, 10, 10]
00314 myia = self._myia
00315 myia.fromshape("test011.im", shape)
00316 box = "0, 0, 2, 2, 4, 4, 6, 6"
00317 chans = "0~4, 6, >8"
00318 reg = rg.frombcs(
00319 myia.coordsys().torecord(), shape,
00320 box=box, chans=chans
00321 )
00322 bb = myia.statistics(region=reg)
00323 self.assertTrue(bb["npts"][0] == 126)
00324 bb = imstat(imagename=myia.name(), chans=chans, box=box)
00325 self.assertTrue(bb["npts"][0] == 126)
00326
00327 def test012(self):
00328 """ Test multi beam support"""
00329 myia = self._myia
00330 shape = [15, 20, 4, 10]
00331 myia.fromshape("", shape)
00332 xx = numpy.array(range(shape[0]*shape[1]))
00333 xx.resize(shape[0], shape[1])
00334 myia.putchunk(xx, replicate=True)
00335 myia.setbrightnessunit("Jy/pixel")
00336 def check(myia, axes, exp):
00337 for i in range(shape[2]):
00338 for j in range(shape[3]):
00339 got = myia.statistics(
00340 axes=axes,
00341 region=rg.box(
00342 blc = [0, 0, i, j],
00343 trc=[shape[0]-1, shape[1]-1, i, j]
00344 )
00345 )
00346 self.assertTrue(len(got.keys()) == len(exp.keys()))
00347 for k in got.keys():
00348 if (type(got[k]) == type(got["rms"])):
00349 if (k != "blc" and k != "trc"):
00350 self.assertTrue((got[k] == exp[k][0][0]).all())
00351
00352 axes = [0, 1]
00353 exp = myia.statistics(axes=axes)
00354 self.assertFalse(exp.has_key("flux"))
00355 check(myia, axes, exp)
00356 myia.setbrightnessunit("Jy/beam")
00357 self.assertTrue(myia.brightnessunit() == "Jy/beam")
00358 major = qa.quantity("3arcmin")
00359 minor = qa.quantity("2.5arcmin")
00360 pa= qa.quantity("20deg")
00361 myia.setrestoringbeam(
00362 major=major, minor=minor, pa=pa
00363 )
00364 self.assertTrue(myia.brightnessunit() == "Jy/beam")
00365 exp = myia.statistics(axes=axes)
00366 self.assertTrue(exp.has_key("flux"))
00367 check(myia, axes, exp)
00368 myia.setrestoringbeam(remove=True)
00369 self.assertTrue(
00370 myia.setrestoringbeam(
00371 major=major, minor=minor, pa=pa,
00372 channel=0, polarization=0
00373 )
00374 )
00375 exp = myia.statistics(axes=axes)
00376 self.assertTrue(exp.has_key("flux"))
00377 check(myia, axes, exp)
00378 nmajor = qa.add(major, qa.quantity("0.1arcmin"))
00379 self.assertTrue(
00380 myia.setrestoringbeam(
00381 major=nmajor, minor=minor, pa=pa,
00382 channel=1, polarization=1
00383 )
00384 )
00385 exp = myia.statistics(axes=axes)
00386 self.assertTrue(exp.has_key("flux"))
00387 self.assertTrue(
00388 abs(1 - qa.getvalue(nmajor)*exp["flux"][1][1]/(qa.getvalue(major)*exp["flux"][0][0]))
00389 < 1e-7
00390 )
00391
00392 def test_CAS4545(self):
00393 """verify CAS-4545 fix: full support for >2Gpixel images"""
00394 myia = self._myia
00395 myia.fromshape("", [1290, 1290, 1290])
00396 stats = myia.statistics()
00397 self.assertTrue(stats['npts'][0] == myia.shape().prod())
00398
00399 def suite():
00400 return [imstat_test]
00401
00402