casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_imregrid.py
Go to the documentation of this file.
00001 import os
00002 import shutil
00003 import numpy
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 
00009 IMAGE = 'image.im'
00010 
00011 total = 0
00012 fail  = 0
00013 current_test =""
00014 stars = "*************"
00015 
00016 datapath = os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/imregrid/'
00017 
00018 def alleqnum(x,num,tolerance=0):
00019     if len(x.shape)==1:
00020         for i in range(x.shape[0]):
00021             if not (abs(x[i]-num) < tolerance):
00022                 print "x[",i,"]=", x[i]
00023                 return False
00024     if len(x.shape)==2:
00025         for i in range(x.shape[0]):
00026             for j in range(x.shape[1]):
00027                 if not (abs(x[i][j]-num) < tolerance):
00028                     print "x[",i,"][",j,"]=", x[i][j]
00029                     return False
00030     if len(x.shape)==3:
00031         for i in range(x.shape[0]):
00032             for j in range(x.shape[1]):
00033                 for k in range(x.shape[2]):
00034                     if not (abs(x[i][j][k]-num) < tolerance):
00035                         print "x[",i,"][",j,"][",k,"]=", x[i][j][k]
00036                         return False
00037     if len(x.shape)==4:
00038         for i in range(x.shape[0]):
00039             for j in range(x.shape[1]):
00040                 for k in range(x.shape[2]):
00041                     for l in range(x.shape[3]):
00042                         if not (abs(x[i][j][k][l]-num) < tolerance):
00043                             print "x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k]
00044                             return False
00045     if len(x.shape)>4:
00046         stop('unhandled array shape in alleq')
00047     return True
00048 
00049 def test_start(msg):
00050     global total, current_test
00051     total += 1
00052     print
00053     print stars + " Test " + msg + " start " + stars
00054     current_test = msg
00055     
00056 def test_end(condition, error_msg):
00057     global total, fail
00058     status = "OK"
00059     if not condition:
00060         print >> sys.stderr, error_msg
00061         fail += 1
00062         status = "FAIL"
00063     print stars + " Test " + current_test + " " + status + " " + stars
00064         
00065 out1 = 'regridded'
00066 out2 = 'bigger_image'
00067 out3 = 'shifted_image'
00068 out4 = 'back_to_image'
00069 out5 = 'template'
00070 
00071 class imregrid_test(unittest.TestCase):
00072 
00073     def setUp(self):
00074         self._myia = iatool()
00075     
00076     def tearDown(self):
00077         self._myia.done()
00078 
00079         os.system('rm -rf ' +IMAGE)
00080         os.system('rm -rf ' +out1)
00081         os.system('rm -rf ' +out2)
00082         os.system('rm -rf ' +out3)
00083         os.system('rm -rf ' +out4)
00084         os.system('rm -rf ' +out5)
00085 
00086         
00087     def test1(self):    
00088         myia = self._myia  
00089         myia.maketestimage(outfile = IMAGE)
00090         default('imregrid')
00091         
00092         outim=imregrid(
00093             imagename = IMAGE,
00094             template = IMAGE,
00095             output = out1
00096         )
00097         im1 = myia.newimage(IMAGE)
00098         im2 = myia.newimage(out1)
00099         
00100         im1.statistics()
00101         im2.statistics()
00102         
00103         rec1 = im1.torecord()
00104         print '*************'
00105         print rec1['shape']
00106         print '*************'
00107         shape = im1.shape()
00108         print shape
00109         checked = 0
00110         for x in range(shape[0]):
00111             for y in range(shape[1]):
00112                 p1 = im1.pixelvalue([x, y])
00113                 p2 = im2.pixelvalue([x, y])
00114                 if p1['mask'] != p2['mask']: raise Exception, p1['mask'] + ' != ' + p2['mask']
00115                 if p1['value']['value'] != p2['value']['value']: raise Exception, p1['value']['value'] + ' != ' + p2['value']['value']
00116                 if p1['value']['unit'] != p2['value']['unit']: raise Exception, p1['value']['unit'] + ' != ' + p2['value']['unit']
00117                 checked += 3
00118         
00119         im2.done()
00120         
00121         print str(checked) + ' values checked'
00122         
00123         # rescale by factors 3 x 2
00124         rec1 = im1.torecord()
00125         print '*************'
00126         print "shape before " + str(rec1['shape'])
00127         print '*************'
00128         rec1['shape'] = numpy.array([3*rec1['shape'][0], 2*rec1['shape'][1]], numpy.int32)
00129         print "shape after " + str(rec1['shape'])
00130 
00131         rec1['coordsys']['coordsys']['direction0']['cdelt'] = [
00132             rec1['coordsys']['coordsys']['direction0']['cdelt'][0]/3.0,
00133             rec1['coordsys']['coordsys']['direction0']['cdelt'][1]/2.0]
00134         rec1['coordsys']['coordsys']['direction0']['crpix'] = [
00135             rec1['coordsys']['coordsys']['direction0']['crpix'][0]*3.0,
00136             rec1['coordsys']['coordsys']['direction0']['crpix'][1]*2.0]
00137         print rec1
00138         
00139         myia.fromrecord(rec1, out2)
00140         
00141         # First we need to remove the output file.
00142         if (  os.path.exists(out1) ):
00143               shutil.rmtree( out1 )
00144         outim=imregrid(
00145             imagename=IMAGE, template=out2,
00146             output=out1, shape=rec1["shape"]
00147         )
00148         s1 = imstat(IMAGE)
00149         s2 = imstat(out1)
00150         ia.open(out1)
00151         print "out shape " + str(ia.shape())
00152         print "S1: ", s1
00153         print " "
00154         print " "
00155         print "S2: ", s2
00156         
00157         if s1['maxpos'][0]*3 != s2['maxpos'][0]:
00158             raise Exception, str(s1['maxpos'][0]*3) + ' != ' + str(s2['maxpos'][0])
00159         if s1['maxpos'][1]*2 != s2['maxpos'][1]:
00160             raise Exception, str(s1['maxpos'][1]*2) + ' != ' + str(s2['maxpos'][1])
00161         
00162         
00163         
00164         # shift by -13, 1 pixels
00165 
00166         rec1 = im1.torecord()
00167         rec1['coordsys']['coordsys']['direction0']['crpix'] = [
00168             rec1['coordsys']['coordsys']['direction0']['crpix'][0]-13,
00169             rec1['coordsys']['coordsys']['direction0']['crpix'][1]+1]
00170         
00171         myia.fromrecord(rec1, out3)
00172         myia.close()
00173         # First we need to remove the output file.
00174         if (  os.path.exists(out1 ) ):
00175               shutil.rmtree( out1)
00176         outim = imregrid(imagename = IMAGE,
00177                  template = out3,
00178                  output = out1)
00179         s1 = imstat(IMAGE)
00180         s2 = imstat(out1)
00181         if s1['maxpos'][0]-13 != s2['maxpos'][0]:
00182             raise Exception, str(s1['maxpos'][0]-13) + ' != ' + str(s2['maxpos'][0])
00183         if s1['maxpos'][1]+1 != s2['maxpos'][1]:
00184             raise Exception, str(s1['maxpos'][1]+1) + ' != ' + str(s2['maxpos'][1])
00185         
00186         
00187         # Shift back to original
00188         rec1['coordsys']['coordsys']['direction0']['crpix'] = [
00189             rec1['coordsys']['coordsys']['direction0']['crpix'][0]+13,
00190             rec1['coordsys']['coordsys']['direction0']['crpix'][1]-1]
00191         if (  os.path.exists(out3 ) ):
00192             shutil.rmtree( out3)
00193         myia.fromrecord(rec1, out3)
00194         myia.close()
00195         outim = imregrid(imagename = IMAGE,
00196                  template = out3,
00197                  output = out4)
00198         s1 = imstat(IMAGE)
00199         s2 = imstat(out4)
00200         print s1
00201         print s2
00202         for stat in ['rms', 'medabsdevmed', 'minpos',
00203                      'min', 'max', 'sum', 'minposf',
00204                      'median', 'flux', 'sumsq', 'maxposf',
00205                      'trcf', 'quartile', 'npts', 'maxpos',
00206                      'mean', 'sigma', 'trc', 'blc', 'blcf']:
00207             if type(s1[stat]) == type('a string'):
00208                 print "Checking string", stat, s1[stat]
00209                 if s1[stat] != s2[stat]:
00210                     raise Exception
00211             else:
00212                 for i in range(len(s1[stat])):
00213                     print "Checking", stat, "[", i, "]", s1[stat][i]
00214                     if s1[stat][i] != s2[stat][i]:
00215                         # Note:  == comparison of floating point values,
00216                         # it works right now on this computer but might need to get fixed...
00217                         raise Exception
00218         
00219         
00220         # Exercise various reference codes (no check on output)
00221         codes = cs.newcoordsys(direction=True).referencecode('dir', True)
00222         rec1 = im1.torecord()
00223         im1.done()
00224         for ref in codes:
00225             print "Regrid to", ref
00226             if ref not in ['JMEAN', 'JTRUE', 'APP',
00227                            'BMEAN', 'BTRUE', 'HADEC',
00228                            'AZEL', 'AZELSW', 'AZELNE',
00229                            'AZELGEO',
00230                            'AZELSWGEO', 'AZELNEGEO',
00231                            'JNAT',
00232                            'MECLIPTIC', 'TECLIPTIC',
00233                            'ITRF', 'TOPO']:
00234                 rec1['coordsys']['coordsys']['direction0']['conversionSystem'] = ref
00235                 
00236                 myia.fromrecord(rec1, out5)
00237                 myia.close()
00238                 if (  os.path.exists(out1 ) ):
00239                     shutil.rmtree( out1 )
00240                 outim=imregrid(imagename = IMAGE,
00241                          template = out5,
00242                          output = out1)
00243         self.assertTrue(len(tb.showcache()) == 0)
00244 
00245     def test_asvelocity(self):
00246         """ Test regrid by velocity """
00247         image = "byvel.im"
00248         expected = "expected.im"
00249         shutil.copytree(datapath + image, image)
00250         shutil.copytree(datapath + expected, expected)
00251         myia = self._myia
00252         myia.open(expected)
00253         csys = myia.coordsys().torecord()
00254         myia.done()
00255         myia.open(image)
00256         ff = myia.regrid("",csys=csys,asvelocity=True)
00257         myia.done()
00258         myia.open(expected)
00259         res = (ff.getchunk() == myia.getchunk()).all()
00260         self.assertTrue(res)
00261         res = (ff.getchunk(getmask=True) == myia.getchunk(getmask=True)).all()
00262         self.assertTrue(res)
00263         ff.done()
00264         outfile = "junk"
00265         outim = myia.regrid(outfile=outfile, csys=csys, asvelocity=True)
00266         outim.done()
00267         ff.open(outfile)
00268         res = (ff.getchunk() == myia.getchunk()).all()
00269         self.assertTrue(res)
00270         res = (ff.getchunk(getmask=True) == myia.getchunk(getmask=True)).all()
00271         ff.done()
00272         myia.done()
00273         self.assertTrue(res)  
00274         shutil.rmtree(outfile)
00275         shutil.rmtree(image)
00276         shutil.rmtree(expected)      
00277         self.assertTrue(len(tb.showcache()) == 0)        
00278 
00279     def test_stretch(self):
00280         """ ia.regrid(): Test stretch parameter"""
00281         yy = self._myia
00282         mymask = "maskim"
00283         yy.fromshape(mymask, [200, 200, 1, 1])
00284         yy.addnoise()
00285         yy.done()
00286         shape = [200,200,1,20]
00287         yy.fromshape("", shape)
00288         yy.addnoise()
00289         mycsys = yy.coordsys()
00290         mycsys.setreferencepixel([2.5], "spectral")
00291         for i in [0,1]:
00292             byvel = i == 0
00293             self.assertRaises(
00294                 Exception,
00295                 yy.regrid, outfile="", asvelocity=byvel,
00296                 csys=mycsys.torecord(),
00297                 mask=mymask + ">0", stretch=False
00298             )
00299             zz = yy.regrid(
00300                 outfile="", asvelocity=byvel,
00301                 csys=mycsys.torecord(),
00302                 mask=mymask + ">0", stretch=True
00303             )
00304             self.assertTrue(type(zz) == type(yy))
00305             zz.done()
00306             
00307         yy.done()
00308         self.assertTrue(len(tb.showcache()) == 0)
00309         
00310     def test_axes(self):
00311         imagename = "test_axes.im"
00312         templatename = "test_axes.tmp"
00313         output = "test_axes.out"
00314         myia = self._myia
00315         myia.fromshape(imagename, [10, 10, 10])
00316         exp = myia.coordsys().increment()["numeric"]
00317         myia.fromshape(templatename, [10, 10, 10])
00318         mycsys = myia.coordsys()
00319         mycsys.setincrement(mycsys.increment()["numeric"]/2)
00320         myia.setcoordsys(mycsys.torecord())
00321         exp[2] = mycsys.increment()["numeric"][2]
00322         zz = imregrid(imagename, template=templatename, output=output, axes=2)
00323         myia.open(output)
00324         got = myia.coordsys().increment()["numeric"]
00325         self.assertTrue((got == exp).all())
00326         myia.done()
00327         self.assertTrue(len(tb.showcache()) == 0)
00328         
00329     def test_general(self):
00330         """ imregrid general tests """
00331         # moved from iamgetest_regression
00332         
00333         # Make RA/DEC/Spectral image
00334         
00335         imname = 'ia.fromshape.image1'
00336         imshape = [32,32,32]
00337         myia = self._myia
00338         myim = myia.newimagefromshape(imname, imshape)
00339         self.assertTrue(myim)
00340         self.assertTrue(myim.set(1.0))
00341         # Forced failures
00342         self.assertRaises(Exception, myim.regrid, axes=[20])
00343         self.assertRaises(Exception, myim.regrid, shape=[10,20,30,40])       
00344         self.assertRaises(Exception, myim.regrid, csys='fish')
00345         self.assertRaises(Exception, myim.regrid, method='doggies')
00346 
00347         # Regrid it to itself (all axes        #
00348         iDone = 1
00349         #      for method in ["near","linear","cubic"]:
00350         for method in ["cubic"]:
00351             myim2 = myim.regrid(method=method)
00352             self.assertTrue(myim2)
00353             p = myim2.getchunk([3,3],[imshape[0]-3,imshape[1]-3,imshape[2]-3])
00354             self.assertTrue(alleqnum(p,1,tolerance=1e-3))
00355             self.assertTrue(myim2.done())
00356             iDone = iDone + 1
00357             
00358         #      for method in ["cubic","linear","near"]:
00359         for method in ["cubic"]:
00360             myim2 = myim.regrid(method=method, axes=[0,1])
00361             self.assertTrue(myim2)
00362             p = myim2.getchunk([3,3],[imshape[0]-3,imshape[1]-3,imshape[2]-3])
00363             self.assertTrue(alleqnum(p,1,tolerance=1e-3))
00364             self.assertTrue(myim2.done())
00365             iDone = iDone + 1
00366 
00367         #      for method in ["near","linear","cubic"]:
00368         for method in ["cubic"]:
00369             myim2 = myim.regrid(method=method, axes=[2])
00370             self.assertTrue(myim2)
00371             p = myim2.getchunk([3,3],[imshape[0]-3,imshape[1]-3,imshape[2]-3])
00372             self.assertTrue(alleqnum(p,1,tolerance=1e-3))
00373             self.assertTrue(myim2.done())
00374             iDone = iDone + 1
00375         #
00376         self.assertTrue(myim.done())
00377         self.assertTrue(len(tb.showcache()) == 0)        
00378 
00379     def test_multibeam(self):
00380         """imregrid, test multibeam image"""
00381         myia = self._myia
00382         myia.fromshape("", [10, 10, 10])
00383         csys = myia.coordsys()
00384         refpix = csys.increment()["numeric"][2]
00385         refpix = refpix * 0.9
00386         csys.setincrement(refpix, "spectral")
00387         
00388         myia.setrestoringbeam(major="4arcsec", minor="2arcsec", pa="0deg", channel=0, polarization=-1)
00389         regridded = myia.regrid(axes=[0, 1], csys=csys.torecord())
00390         regridded.done()
00391         self.assertRaises(Exception, myia.regrid, axes=[0,1,2], csys=csys.torecord())
00392         self.assertTrue(len(tb.showcache()) == 0)
00393         
00394     def test_CAS_4315(self):
00395         """ test ia.regrid does not leave image open after tool is closed"""
00396         myia = self._myia
00397         myia.fromshape("",[100,100,1,1])
00398         myib = myia.regrid(
00399             outfile='moulou1', csys=myia.coordsys().torecord(), axes=[0,1],
00400             overwrite=True, shape=[100, 100, 1, 1]
00401         )
00402         myia.done()
00403         myib.done()
00404         self.assertTrue(len(tb.showcache()) == 0)
00405         
00406     def test_CAS_4262(self):
00407         """ Test degenerate axes are not relabeled to template"""
00408         myia = self._myia
00409         myia.fromshape("", [10, 10, 1, 10])
00410         csys = myia.coordsys()
00411         refvals = csys.referencevalue()["numeric"]
00412         refvals[3] *= 10
00413         csys.setreferencevalue(refvals)
00414         regridded = myia.regrid("", myia.shape(), csys.torecord())
00415         self.assertTrue((regridded.getchunk(getmask=True) == False).all())
00416         self.assertTrue(
00417             (
00418              regridded.coordsys().referencevalue()["numeric"] == refvals
00419             ).all()
00420         )
00421         # test degenerate spectral exis is not regridded nor relabeled in output
00422         myia.fromshape("", [10, 10, 1, 1])
00423         csys = myia.coordsys()
00424         refvals = csys.referencevalue()["numeric"]
00425         refvals[3] *= 10
00426         csys.setreferencevalue(refvals)
00427         regridded = myia.regrid("", myia.shape(), csys.torecord())
00428         self.assertTrue(regridded.getchunk(getmask=True).all())
00429         self.assertTrue(
00430             (
00431              regridded.coordsys().referencevalue()["numeric"]
00432              == myia.coordsys().referencevalue()["numeric"]
00433             ).all()
00434         )
00435         
00436         
00437             
00438 def suite():
00439     return [imregrid_test]
00440     
00441