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
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 import random
00100 import os
00101 import numpy
00102 import shutil
00103 import casac
00104 from tasks import *
00105 from taskinit import *
00106 import unittest
00107 import math
00108
00109 targetres_im = "imsmooth_targetres.fits"
00110 list=['g192_a2.image', 'g192_a2.image-2.rgn']
00111
00112 def _near(got, expected, tol):
00113 return qa.le(
00114 qa.div(
00115 qa.abs(qa.sub(got, expected)),
00116 expected
00117 ),
00118 tol
00119 )
00120
00121 def run_imsmooth(
00122 imagename, major, minor, pa, targetres,
00123 outfile, kernel="gauss", overwrite=False, beam={}
00124 ):
00125 return imsmooth(
00126 imagename=imagename, kernel=kernel,
00127 major=major, minor=minor, pa=pa,
00128 targetres=targetres, outfile=outfile,
00129 overwrite=overwrite, beam=beam
00130 )
00131 def make_gauss2d(shape, xfwhm, yfwhm):
00132 fac = 4*math.log(2)
00133 values = numpy.empty(shape, dtype=float)
00134 for i in range(shape[0]):
00135 x = shape[0]/2 - i
00136 for j in range(shape[1]):
00137 y = shape[1]/2 - j
00138 xfac = x*x*fac/(xfwhm*xfwhm)
00139 yfac = y*y*fac/(yfwhm*yfwhm)
00140 values[i, j] = math.exp(-(xfac + yfac));
00141 return values
00142
00143
00144 def run_convolve2d(
00145 imagename, major, minor, pa, targetres,
00146 outfile, kernel="gauss", beam={}, overwrite=False
00147 ):
00148 myia = iatool()
00149 myia.open(imagename)
00150 res = myia.convolve2d(
00151 type=kernel,
00152 major=major, minor=minor, pa=pa,
00153 targetres=targetres, outfile=outfile,
00154 beam=beam, overwrite=overwrite
00155 )
00156 myia.done()
00157 return res
00158
00159 class imsmooth_test(unittest.TestCase):
00160
00161 def setUp(self):
00162 if(os.path.exists(list[0])):
00163 for file in list:
00164 os.system('rm -rf ' +file)
00165
00166 datapath = os.environ.get('CASAPATH').split()[0]+'/data/regression/g192redux/reference/'
00167 for file in list:
00168 os.system('cp -r ' +datapath + file +' ' + file)
00169
00170 if(os.path.exists(targetres_im)):
00171 os.system('rm -rf ' +targetres_im)
00172 self.ia = iatool()
00173 self.datapath = os.environ.get('CASAPATH').split()[0]+'/data/regression/imsmooth/'
00174 os.system('cp -r ' + self.datapath + targetres_im +' ' + targetres_im)
00175
00176 def tearDown(self):
00177 for file in list:
00178 os.system('rm -rf ' +file)
00179 os.system('rm -rf input_test*')
00180 os.system('rm -rf rgn*')
00181 os.system('rm -rf smooth*')
00182 os.system('rm -rf ' +targetres_im)
00183 os.system('rm -rf tr!.im')
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 def _compare_beams(self, beam1, beam2):
00202 self.assertTrue(_near(beam1["major"], beam2["major"], 2e-5))
00203 self.assertTrue(_near(beam1["minor"], beam2["minor"], 2e-5))
00204 pa = []
00205 for b in [beam1, beam2]:
00206 if (b.has_key("positionangle")):
00207 pa.append(b["positionangle"])
00208 else:
00209 pa.append(b["pa"])
00210
00211 diff = abs(
00212 qa.sub(
00213 qa.quantity(pa[0]),
00214 qa.quantity(pa[1])
00215 )["value"]
00216 )
00217 self.assertTrue(diff < 1e-5)
00218
00219
00220 def test_input(self):
00221 '''Imsmooth: Testing INPUT/OUTPUT tests'''
00222 retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00223 casalog.post( "Starting imsmooth INPUT/OUTPUT tests.", 'NORMAL2' )
00224
00225
00226 for file in os.listdir( '.' ):
00227 if file.startswith( 'input_test' ):
00228 shutil.rmtree( file )
00229 if os.path.exists( 'garbage.rgn' ):
00230 os.remove('garbage.rgn')
00231
00232
00233
00234
00235
00236
00237
00238 casalog.post( "The IMAGENAME parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00239
00240 result = None
00241 try:
00242 results = imsmooth( 'g192', outfile='input_test1' )
00243 except:
00244 no_op='noop'
00245 else:
00246 if ( results != None and \
00247 ( not isinstance(results, bool) or results==True ) ):
00248 retValue['success']=False
00249 retValue['error_msgs']=retValue['error_msgs']\
00250 +"\nError: Badfile, 'g192', was not reported as missing."
00251
00252 results = None
00253 try:
00254 results = imsmooth( 'g192_a2.image', outfile='input_test1' )
00255
00256 except:
00257 retValue['success']=False
00258 retValue['error_msgs']=retValue['error_msgs']\
00259 +"\nError: Unable to smooth g192_a2.image"
00260
00261 if ( not os.path.exists( 'input_test1' ) or results==False ):
00262 retValue['success']=False
00263 retValue['error_msgs']=retValue['error_msgs']\
00264 +"\nError: continuum files for 'input_test1' were not created."
00265
00266
00267
00268
00269
00270
00271
00272 casalog.post( "The OUTFILE parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00273
00274 results = None
00275 try:
00276 results = imsmooth( 'g192_a2.image', outfile='input_test1' )
00277 except:
00278 pass
00279 else:
00280 if ( results != None and \
00281 ( not isinstance(results, bool) or results==True ) ):
00282 retValue['error_msgs']=retValue['error_msgs']\
00283 +"\nError: Badfile, 'input_test1', was not reported as already existing."
00284
00285 results = None
00286 try:
00287 results=imsmooth( 'g192_a2.image', outfile='input_test2' )
00288 except:
00289 retValue['success']=False
00290 retValue['error_msgs']=retValue['error_msgs']\
00291 +"\nError: Unable to create smoothed image 'input_test2'"
00292 if ( not os.path.exists( 'input_test2' ) and results==False ):
00293 retValue['success']=False
00294 retValue['error_msgs']=retValue['error_msgs']\
00295 +"\nError: output file, 'input_test2', was not created."
00296
00297
00298
00299
00300
00301
00302
00303 casalog.post( "The KERNEL parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00304
00305 results = None
00306 try:
00307 results = imsmooth( 'g192_a2.image', kernel='', outfile='input_test3' )
00308 except:
00309 pass
00310 else:
00311 if ( results != None and \
00312 ( not isinstance(results, bool) or results==True ) ):
00313 retValue['success']=False
00314 retValue['error_msgs']=retValue['error_msgs']\
00315 +"\nError: No exception thrown for bad kernel value, ''"
00316
00317 results = None
00318 try:
00319 results = imsmooth( 'g192_a2.image', kernel='junk', outfile='input_test4' )
00320 except:
00321 pass
00322 else:
00323 if ( results != None and \
00324 ( not isinstance(results, bool) or results==True ) ):
00325 retValue['success']=False
00326 retValue['error_msgs']=retValue['error_msgs']\
00327 +"\nError: No exception thrown for bad kernel value, 'junk'"
00328
00329 results = None
00330 try:
00331 results=imsmooth( 'g192_a2.image', kernel='gauss', outfile='input_test5' )
00332 except Exception, err:
00333 retValue['success']=False
00334 retValue['error_msgs']=retValue['error_msgs']\
00335 +"\nError: Gaussian smoothing failed."
00336 if ( not os.path.exists( 'input_test5' ) or results == False ):
00337 retValue['success']=False
00338 retValue['error_msgs']=retValue['error_msgs']\
00339 +"\nError: input_test5 output file was NOT created."\
00340 + "\n\tRESULTS: "+str(results)
00341
00342
00343 results = None
00344 try:
00345 results=imsmooth( 'g192_a2.image', kernel='boxcar', outfile='input_test6' )
00346 except Exception, err:
00347 retValue['success']=False
00348 retValue['error_msgs']=retValue['error_msgs']\
00349 +"\nError: Boxcar smoothing failed. " \
00350 +str(err)
00351
00352 if ( not os.path.exists( 'input_test6' ) or results==False ):
00353 retValue['success']=False
00354 retValue['error_msgs']=retValue['error_msgs']\
00355 +"\nError: output file 'input_test6' was NOT created."
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 casalog.post( "The MAJOR parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00380
00381 try:
00382 result = None
00383 results = imsmooth( 'g192_a2.image', major='bad', oufile='input_test8')
00384 except:
00385 no_op='noop'
00386 else:
00387 if ( results != None and results!=False ):
00388 retValue['success']=False
00389 retValue['error_msgs']=retValue['error_msgs']\
00390 +"\nError: Bad major value, 'bad', was not reported."
00391
00392 try:
00393 result = None
00394 results = imsmooth( 'g192_a2.image', major=-5, oufile='input_test9' )
00395 except:
00396 no_op='noop'
00397 else:
00398 if ( results != None and results!=False ):
00399 retValue['success']=False
00400 retValue['error_msgs']=retValue['error_msgs']\
00401 +"\nError: Bad major value, '-5', was not reported."
00402
00403 result = None
00404 try:
00405 results = imsmooth( 'g192_a2.image', major=2, minor=1, outfile='input_test11')
00406 except Exception, err:
00407 retValue['success']=False
00408 retValue['error_msgs']=retValue['error_msgs']\
00409 +"\nError: Unable to smooth with major=2 and minor=1 "
00410
00411 if ( not os.path.exists( 'input_test11' ) or results==False ):
00412 retValue['success']=False
00413 retValue['error_msgs']=retValue['error_msgs']\
00414 +"\nError: Smoothing failed with numerical major/minor values."\
00415 +"\nRESULTS: "+str(results)
00416
00417 result = None
00418 try:
00419 results = imsmooth( 'g192_a2.image', major='2pix', minor='1pix', outfile='input_test12')
00420 except Exception, err:
00421 retValue['success']=False
00422 retValue['error_msgs']=retValue['error_msgs']\
00423 +"\nError: Unable to smooth with major='2pix' and minor='1pix' "
00424
00425 if ( not os.path.exists( 'input_test12' ) or results==False ):
00426 retValue['success']=False
00427 retValue['error_msgs']=retValue['error_msgs']\
00428 +"\nError: Smoothing failed with pixel major/minor values."
00429
00430 result = None
00431 try:
00432 results = imsmooth( 'g192_a2.image', major='1.5arcsec', minor='1arcsec', outfile='input_test13')
00433 except Exception, err:
00434 retValue['success']=False
00435 retValue['error_msgs']=retValue['error_msgs']\
00436 +"\nError: Unable to smooth with major='1.5arcsec' and minor='1arcsec' "
00437
00438 if ( not os.path.exists( 'input_test13' ) or results==False ):
00439 retValue['success']=False
00440 retValue['error_msgs']=retValue['error_msgs']\
00441 +"\nError: Smoothing failed with arcsecond major/minor values."
00442 result = None
00443 try:
00444 results = imsmooth( 'g192_a2.image', major='0.5arcsec', minor='2arcsec', outfile='input_test14')
00445 except:
00446 pass
00447 else:
00448 if ( results != None and results!=False ):
00449 retValue['success']=False
00450 retValue['error_msgs']=retValue['error_msgs']\
00451 +"\nError: Bad major value less than minor value was not reported."
00452
00453
00454
00455
00456
00457
00458
00459 casalog.post( "The REGION parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00460
00461 results = imsmooth( 'g192_a2.image', region=7 )
00462 if ( results ):
00463 retValue['success']=False
00464 retValue['error_msgs']=retValue['error_msgs']\
00465 +"\nError: Bad region file, 7, was not reported as bad."
00466
00467
00468
00469 try:
00470 results = imsmooth( 'g192_a2.image', region='garbage.rgn' )
00471 except:
00472
00473 no_op = 'noop'
00474 else:
00475 if ( results ):
00476 retValue['success']=False
00477 retValue['error_msgs']=retValue['error_msgs']\
00478 +"\nError: Bad region file, 'garbage.rgn', was not reported as missing."
00479
00480 try:
00481 filename = os.getcwd()+'/garbage.rgn'
00482 fp=open( filename, 'w' )
00483 fp.writelines('This file does NOT contain a valid CASA region specification\n')
00484 fp.close()
00485
00486 try:
00487 results = imsmooth( 'g192_a2.image', region=filename )
00488 except:
00489 no_op='noop'
00490 else:
00491 if ( results ):
00492 retValue['success']=False
00493 retValue['error_msgs']=retValue['error_msgs']\
00494 + "\nError: Bad region file, 'garbage.rgn',"\
00495 + " was not reported as bad."
00496 except Exception, err:
00497 retValue['success']=False
00498 retValue['error_msgs']=retValue['error_msgs']\
00499 +"\nError: Unable to create bad region file.\n\t"
00500 raise Exception, err
00501
00502
00503 try:
00504 results=imsmooth( 'g192_a2.image', region='g192_a2.image-2.rgn', outfile='input_test15' )
00505 except:
00506 retValue['success']=False
00507 retValue['error_msgs']=retValue['error_msgs']\
00508 +"\nError: Unable to do smoothing with region file g192_a2.image-2.rgn"
00509 if ( not os.path.exists( 'input_test15' ) or not results ):
00510 retValue['success']=False
00511 retValue['error_msgs']=retValue['error_msgs']\
00512 +"\nError: output file 'input_test15', was not created."
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 casalog.post( "The BOX parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00527
00528 results = None
00529 try:
00530 results = imsmooth( 'g192_a2.image', box='-3,0,511,511' )
00531 except:
00532 pass
00533 else:
00534 if ( results != None and \
00535 ( not isinstance(results, bool) or results==True ) ):
00536 retValue['success']=False
00537 retValue['error_msgs']=retValue['error_msgs']\
00538 +"\nError: Bad box value, 'x=-3', was not reported as bad."
00539
00540 results = None
00541 try:
00542 results = imsmooth( 'g192_a2.image', box='0,-3,511,511' )
00543 except:
00544 pass
00545 else:
00546 if ( results != None and \
00547 ( not isinstance(results, bool) or results==True ) ):
00548 retValue['success']=False
00549 retValue['error_msgs']=retValue['error_msgs']\
00550 +"\nError: Bad box value, 'y=-3', was not reported as bad."
00551
00552 results = None
00553 try:
00554 results = imsmooth( 'g192_a2.image', box='-2,0,511,511' )
00555 except:
00556 pass
00557 else:
00558 if ( results != None and \
00559 ( not isinstance(results, bool) or results==True ) ):
00560 retValue['success']=False
00561 retValue['error_msgs']=retValue['error_msgs']\
00562 +"\nError: Bad box value, 'x=-2', was not reported."\
00563 +"\n\tRESULTS: "+str(results)
00564
00565 results = None
00566 try:
00567 results = imsmooth( 'g192_a2.image', box='0,-2,511,511' )
00568 except:
00569 pass
00570 else:
00571 if ( results != None and \
00572 ( not isinstance(results, bool) or results==True ) ):
00573 retValue['success']=False
00574 retValue['error_msgs']=retValue['error_msgs']\
00575 +"\nError: Bad box value, 'y=-2', was not reported as bad."
00576
00577 results = None
00578 try:
00579 results = imsmooth( 'g192_a2.image', box='0,0,512,511' )
00580 except:
00581 pass
00582 else:
00583 if ( results != None and \
00584 ( not isinstance(results, bool) or results==True ) ):
00585 retValue['success']=False
00586 retValue['error_msgs']=retValue['error_msgs']\
00587 +"\nError: Bad box value, 'x=512', was not reported as bad."
00588
00589 results = None
00590 try:
00591 results = imsmooth( 'g192_a2.image', box='0,0,511,512' )
00592 except:
00593 pass
00594 else:
00595 if ( results != None and \
00596 ( not isinstance(results, bool) or results==True ) ):
00597 retValue['success']=False
00598 retValue['error_msgs']=retValue['error_msgs']\
00599 +"\nError: Bad box value, 'y=512', was not reported as bad."
00600
00601
00602 results = None
00603 try:
00604 results = imsmooth( 'g192_a2.image', box='0, 0,525,511' )
00605 except:
00606 pass
00607 else:
00608 if ( results != None and \
00609 ( not isinstance(results, bool) or results==True ) ):
00610 retValue['success']=False
00611 retValue['error_msgs']=retValue['error_msgs']\
00612 +"\nError: Bad box value, 'x=525', was not reported as bad."
00613
00614 results = None
00615 try:
00616 results = imsmooth( 'g192_a2.image', box='0,0,511,525' )
00617 except:
00618 pass
00619 else:
00620 if ( results != None and \
00621 ( not isinstance(results, bool) or results==True ) ):
00622 retValue['success']=False
00623 retValue['error_msgs']=retValue['error_msgs']\
00624 +"\nError: Bad box value, 'y=525', was not reported as bad."
00625
00626 x1=random.randint(0,511)
00627 x2=random.randint(x1,511)
00628 y1=random.randint(0,511)
00629 y2=random.randint(y1,511)
00630 boxstr=str(x1)+','+str(y1)+','+str(x2)+','+str(y2)
00631
00632 results = None
00633 try:
00634 results = imsmooth( 'g192_a2.image', box=boxstr, outfile='input_test16' )
00635 except:
00636 retValue['success']=False
00637 retValue['error_msgs']=retValue['error_msgs']\
00638 +"\nError: Unable to smooth in box="+boxstr
00639 if ( not os.path.exists( 'input_test16' ) or results==False ):
00640 retValue['success']=False
00641 retValue['error_msgs']=retValue['error_msgs']\
00642 +"\nError: output file 'input_test_box16' was not "\
00643 +"created at "+boxstr
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 casalog.post( "The CHANS parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00654
00655 results = None
00656 try:
00657 results = imsmooth( 'g192_a2.image', chans='-5' )
00658 except:
00659 pass
00660 else:
00661 if ( results != None and \
00662 ( not isinstance(results, bool) or results==True ) ):
00663 retValue['success']=False
00664 retValue['error_msgs']=retValue['error_msgs']\
00665 +"\nError: Bad channel value, '-5', was not reported."
00666
00667 results = None
00668 try:
00669 results = imsmooth( 'g192_a2.image', chans='-2' )
00670 except:
00671 pass
00672 else:
00673 if ( results != None and \
00674 ( not isinstance(results, bool) or results==True ) ):
00675 retValue['success']=False
00676 retValue['error_msgs']=retValue['error_msgs']\
00677 +"\nError: Bad channel value, '-2', was not reported."
00678
00679 results = None
00680 try:
00681 results = imsmooth( 'g192_a2.image', chans='-18' )
00682 except:
00683 pass
00684 else:
00685 if ( results != None and \
00686 ( not isinstance(results, bool) or results==True ) ):
00687 retValue['success']=False
00688 retValue['error_msgs']=retValue['error_msgs']\
00689 +"\nError: Bad channel value of -18 was not reported."
00690
00691 results = None
00692 try:
00693 results = imsmooth( 'g192_a2.image', chans='45' )
00694 except:
00695 pass
00696 else:
00697 if ( results != None and \
00698 ( not isinstance(results, bool) or results==True ) ):
00699 retValue['success']=False
00700 retValue['error_msgs']=retValue['error_msgs']\
00701 +"\nError: Bad channel value of 45 was not reported."
00702
00703 results = None
00704 try:
00705 results = imsmooth( 'g192_a2.image', chans='40' )
00706 except:
00707 pass
00708 else:
00709 if ( results != None and \
00710 ( not isinstance(results, bool) or results==True ) ):
00711 retValue['success']=False
00712 retValue['error_msgs']=retValue['error_msgs']\
00713 +"\nError: Bad channel value of 40 was not reported."
00714
00715
00716 results = None
00717 try:
00718 results = imsmooth( 'g192_a2.image', chans='22~35', outfile='input_test17' )
00719 except:
00720 retValue['success']=False
00721 retValue['error_msgs']=retValue['error_msgs']\
00722 +"\nError: Unable to smooth on chans=22~35 only "
00723 if ( not os.path.exists( 'input_test17' ) or results==False ):
00724 retValue['success']=False
00725 retValue['error_msgs']=retValue['error_msgs']\
00726 +"\nError: output file, 'input_test17', was not created."
00727
00728 results = None
00729 try:
00730 results = imsmooth( 'g192_a2.image', chans='0', outfile='input_test17b' )
00731 except:
00732 retValue['success']=False
00733 retValue['error_msgs']=retValue['error_msgs']\
00734 +"\nError: Unable to create smooth chans=0 only"
00735 if ( not os.path.exists( 'input_test17b' ) or results==False ):
00736 retValue['success']=False
00737 retValue['error_msgs']=retValue['error_msgs']\
00738 +"\nError: output file 'input_test17b`' was not created."\
00739 +"\nRESULT: "+str(results)
00740
00741 results=None
00742 try:
00743 results = imsmooth( 'g192_a2.image', chans='39', outfile='input_test18' )
00744 except:
00745 retValue['success']=False
00746 retValue['error_msgs']=retValue['error_msgs']\
00747 +"\nError: Unable to smooth with chans=39 only"
00748 if ( not os.path.exists( 'input_test18' ) or results==False ):
00749 retValue['success']=False
00750 retValue['error_msgs']=retValue['error_msgs']\
00751 +"\nError: output file 'input_test18' was not created."
00752
00753
00754
00755
00756
00757
00758 casalog.post( "The STOKES parameter tests will cause errors to occur, do not be alarmed", 'WARN' )
00759
00760 results=None
00761 try:
00762 results = imsmooth( 'g192_a2.image', stokes='Q' )
00763 except:
00764 pass
00765 else:
00766 if ( results != None and \
00767 ( not isinstance(results, bool) or results==True ) ):
00768 retValue['success']=False
00769 retValue['error_msgs']=retValue['error_msgs']\
00770 +"\nError: Bad stokes value, 'Q', was not reported."
00771
00772 results=None
00773 try:
00774 results = imsmooth( 'g192_a2.image', stokes='yellow' )
00775 except:
00776 pass
00777 else:
00778 if ( results != None and \
00779 ( not isinstance(results, bool) or results==True ) ):
00780 retValue['success']=False
00781 retValue['error_msgs']=retValue['error_msgs']\
00782 +"\nError: Bad stokes value, 'yellow', was not reported."
00783
00784 results = None
00785 try:
00786 results = imsmooth( 'g192_a2.image', stokes='I', outfile='input_test19' )
00787 except:
00788 retValue['success']=False
00789 retValue['error_msgs']=retValue['error_msgs']\
00790 +"\nError: Smmoothing failed with stokes=Q"
00791 if ( not os.path.exists( 'input_test19' ) or results==False ):
00792 retValue['success']=False
00793 retValue['error_msgs']=retValue['error_msgs']\
00794 +"\nError: output file 'input_test19' was not created."
00795
00796 self.assertTrue(retValue['success'],retValue['error_msgs'])
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 def test_smooth(self):
00812 '''Imsmooth: Testing correctness'''
00813 retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00814 casalog.post( "Starting imsmooth CORRECTNESS tests.", 'NORMAL2' )
00815
00816
00817 for file in os.listdir( '.' ):
00818 if file.startswith( 'smooth_test' ):
00819 shutil.rmtree( file )
00820
00821 if os.path.exists( 'smooth.pointsrc.image' ):
00822 shutil.rmtree( 'smooth.pointsrc.image' )
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 try:
00836
00837 ia.open( 'g192_a2.image' )
00838 csys = ia.coordsys()
00839 bb = ia.boundingbox()
00840 shape = bb['bbShape']
00841 ia.done()
00842
00843
00844
00845
00846
00847 inputArray = numpy.zeros( (shape[0], shape[1], shape[2], shape[3]), 'float' )
00848 inputArray[212,220,0,20] = 100
00849
00850
00851 ia.fromarray( pixels=inputArray, csys=csys.torecord(), \
00852 outfile='smooth.pointsrc.image' )
00853 ia.done()
00854 except Exception, err:
00855 retValue['success']=False
00856 retValue['error_msgs']=retValue['error_msgs']\
00857 +"\nError: Unable to create point source image."\
00858 +"\n\t REULTS: "+str(err)
00859
00860
00861
00862
00863
00864
00865
00866 results = None
00867 try:
00868 results=imsmooth( 'smooth.pointsrc.image', kernel='gauss', \
00869 major=50, minor=25, outfile='smooth_test1' )
00870 except Exception, err:
00871 retValue['success']=False
00872 retValue['error_msgs']=retValue['error_msgs']\
00873 +"\nError: boxcar smooth failed on point source file."
00874
00875
00876 if ( not os.path.exists( 'smooth_test1' ) or results==None ):
00877 retValue['success']=False
00878 retValue['error_msgs']=retValue['error_msgs']\
00879 +"\nError: Gaussian smoothfailed on point source file."
00880 else:
00881
00882
00883
00884 allowedError = 0.009
00885
00886 ia.open( 'smooth_test1')
00887 stats = ia.statistics()
00888 sum = stats['sum'][0]
00889 if ( ( sum < 100 and sum < ( 100-allowedError ) )
00890 or ( sum > 100 and sum > ( 100+allowedError) ) ):
00891 retValue['success']=False
00892 retValue['error_msgs']=retValue['error_msgs']\
00893 +"\nError: Sum under Gaussian is "+str(stats['sum'][0])\
00894 +" expected 100."
00895
00896 maxpos=stats['maxpos'].tolist()
00897 if ( maxpos[0]!=212 or maxpos[1]!=220 or \
00898 maxpos[2]!=0 or maxpos[3]!=20 ):
00899 retValue['success']=False
00900 retValue['error_msgs']=retValue['error_msgs']\
00901 +"\nError: Max position found at "+str(maxpos)\
00902 +" expected it to be at 212,220,0,20."
00903
00904 ia.done()
00905
00906
00907
00908
00909 results = None
00910 try:
00911 results=imsmooth( 'smooth.pointsrc.image', kernel='boxcar', \
00912 major=20, minor=10, outfile='smooth_test2' )
00913 except Exception, err:
00914 retValue['success']=False
00915 retValue['error_msgs']=retValue['error_msgs']\
00916 +"\nError: boxcar smooth failed on point source file."
00917
00918 if ( not os.path.exists( 'smooth_test2' ) or results==None ):
00919 retValue['success']=False
00920 retValue['error_msgs']=retValue['error_msgs']\
00921 +"\nError: output file 'smooth_test2' was NOT created."
00922 else:
00923
00924
00925
00926 ia.open( 'smooth_test2')
00927 stats = ia.statistics()
00928 if ( ( sum < 100 and sum < ( 100-allowedError ) )
00929 or ( sum > 100 and sum > ( 100+allowedError) ) ):
00930 retValue['success']=False
00931 retValue['error_msgs']=retValue['error_msgs']\
00932 +"\nError: Sum under Gaussian is "+str(stats['sum'][0])\
00933 +" expected 100."
00934
00935 val1 = ia.pixelvalue( [ 204,200,0,20] )
00936 val2 = ia.pixelvalue( [ 222,236,0,20] )
00937 val3 = ia.pixelvalue( [ 204,239,0,20] )
00938 val4 = ia.pixelvalue( [ 222,201,0,20] )
00939 midVal = ia.pixelvalue( [212,220,0,20] )
00940 for value in [val1, val2, val3, val4, midVal ]:
00941 if ( value>(0.125-allowedError) and value<(0.125+allowedError)):
00942 retValue['success']=False
00943 retValue['error_msgs']=retValue['error_msgs']\
00944 +"\nError: Values in the smoothed box are not all 0.125"\
00945 +" found value of "+str(value)
00946 ia.done()
00947
00948 self.assertTrue(retValue['success'],retValue['error_msgs'])
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959 def test_region(self):
00960 '''Imsmooth: Region selection correction test'''
00961 retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00962 casalog.post( "Starting imsmooth REGION tests.", 'NORMAL2' )
00963 allowedError = 0.0005
00964
00965
00966 for file in os.listdir( '.' ):
00967 if file.startswith( 'rgn_test' ):
00968 shutil.rmtree( file )
00969
00970 if os.path.exists( 'rgn.pointsrc.image' ):
00971 shutil.rmtree( 'rgn.pointsrc.image' )
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 try:
00985
00986 ia.open( 'g192_a2.image' )
00987 csys = ia.coordsys()
00988 bb = ia.boundingbox()
00989 shape = bb['bbShape']
00990 ia.done()
00991
00992
00993
00994
00995
00996 inputArray = numpy.zeros( (shape[0], shape[1], shape[2], shape[3]), 'float' )
00997 inputArray[49,71,0,14] = 100
00998 inputArray[233,276,0,20] = 100
00999 inputArray[15,15,0,30] = 100
01000
01001
01002
01003 ia.fromarray( pixels=inputArray, csys=csys.torecord(), \
01004 outfile='rgn.pointsrc.image' )
01005 ia.done()
01006 except Exception, err:
01007 retValue['success']=False
01008 retValue['error_msgs']=retValue['error_msgs']\
01009 +"\nError: Unable to create point source image."\
01010 +"\n\t REULTS: "+str(err)
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020 results = None
01021 try:
01022 results=imsmooth( 'rgn.pointsrc.image', kernel='gauss', \
01023 major=50, minor=25, outfile='rgn_test1', \
01024 box='350,350,375,390')
01025 except Exception, err:
01026 retValue['success']=False
01027 retValue['error_msgs']=retValue['error_msgs']\
01028 +"\nError: Smoothng failed on region 250,250,275,290." + str(err)
01029
01030
01031 if ( not os.path.exists( 'rgn_test1' ) or results==None ):
01032 retValue['success']=False
01033 retValue['error_msgs']=retValue['error_msgs']\
01034 +"\nError: Smoothing failed on region 250,250,275,290. second block"
01035 else:
01036
01037
01038 ia.open( 'rgn_test1')
01039 stats = ia.statistics()
01040 if ( stats['sum'][0] < ( 0-allowedError) \
01041 or stats['sum'][0] > ( 0+allowedError) ):
01042 retValue['success']=False
01043 retValue['error_msgs']=retValue['error_msgs']\
01044 +"\nError: Sum on smoothed file rgn_test1 is "\
01045 +str(stats['sum'][0]) +" expected value is 0."
01046 ia.done()
01047
01048
01049 results = None
01050 try:
01051 results=imsmooth( 'rgn.pointsrc.image', kernel='gauss', \
01052 major=50, minor=25, outfile='rgn_test2', \
01053 chans='22')
01054 except Exception, err:
01055 retValue['success']=False
01056 retValue['error_msgs']=retValue['error_msgs']\
01057 +"\nError: Smoothng failed on channel 22."
01058
01059
01060 if ( not os.path.exists( 'rgn_test2' ) or results==None ):
01061 retValue['success']=False
01062 retValue['error_msgs']=retValue['error_msgs']\
01063 +"\nError: Smoothing failed on channel 22."
01064 else:
01065
01066
01067 ia.open( 'rgn_test2')
01068 stats = ia.statistics()
01069 if ( stats['sum'][0] < ( 0-allowedError) \
01070 or stats['sum'][0] > ( 0+allowedError) ):
01071 retValue['success']=False
01072 retValue['error_msgs']=retValue['error_msgs']\
01073 +"\nError: Sum on smoothed file rgn_test2 is "\
01074 +str(stats['sum'][0]) +" expected value is 0."
01075 ia.done()
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086 results = None
01087 try:
01088 results=imsmooth( 'rgn.pointsrc.image', kernel='gauss', \
01089 major=10, minor=5, outfile='rgn_test3', \
01090 chans='14', box='0,0,200,200')
01091 except Exception, err:
01092 retValue['success']=False
01093 retValue['error_msgs']=retValue['error_msgs']\
01094 +"\nError: Smoothng failed on channel 14, box 0,0,200,200."
01095
01096
01097 if ( not os.path.exists( 'rgn_test3' ) or results==None ):
01098 retValue['success']=False
01099 retValue['error_msgs']=retValue['error_msgs']\
01100 +"\nError: Smoothing failed on channel 14, box 0,0,200,200."
01101 else:
01102
01103
01104
01105 ia.open( 'rgn_test3')
01106 stats = ia.statistics()
01107 if ( stats['sum'][0] < ( 100-allowedError) \
01108 or stats['sum'][0] > ( 100+allowedError) ):
01109 retValue['success']=False
01110 retValue['error_msgs']=retValue['error_msgs']\
01111 +"\nError: Sum on smoothed file rgn_test3 is "\
01112 +str(stats['sum'][0]) +" expected value is 100."
01113 ia.done()
01114
01115
01116
01117
01118
01119 maxpos=stats['maxpos'].tolist()
01120 if ( maxpos[0]!=49 or maxpos[1]!=71 or \
01121 maxpos[2]!=0 or maxpos[3]!=0 ):
01122 retValue['success']=False
01123 retValue['error_msgs']=retValue['error_msgs']\
01124 +"\nError: Max position found at "+str(maxpos)\
01125 +" expected it to be at 49,71,0,0."
01126
01127
01128 results = None
01129
01130 output = 'rgn_test5'
01131 try:
01132 results=imsmooth( 'rgn.pointsrc.image', kernel='gauss', \
01133 major=2, minor=1, outfile = output, \
01134 region='g192_a2.image:testregion')
01135 except Exception, err:
01136 retValue['success']=False
01137 retValue['error_msgs']=retValue['error_msgs']\
01138 +"\nError: Smoothing failed with internal image region 'testregion'."
01139
01140 if ( not os.path.exists(output) or results==None ):
01141 retValue['success']=False
01142 retValue['error_msgs']=retValue['error_msgs']\
01143 +"\nError: Smoothing failed internal image region 'testregion'."
01144 else:
01145
01146
01147
01148 ia.open(output)
01149 stats = ia.statistics()
01150 ia.done()
01151
01152 sum = stats['sum'][0]
01153 fluxDensity = 99.948
01154 allowedError=0.001
01155 if (sum < (fluxDensity - allowedError) or sum > (fluxDensity + allowedError)):
01156 retValue['success']=False
01157 retValue['error_msgs']=retValue['error_msgs']\
01158 +"\nError: Sum on smoothed file " + output + " is "\
01159 +str(stats['sum'][0]) +" expected value is 100."
01160
01161
01162 maxpos=stats['maxpos'].tolist()
01163 if ( maxpos[0] != 5 or maxpos[1] != 5 or \
01164 maxpos[2] != 0 or maxpos[3] != 30 ):
01165 retValue['success']=False
01166 retValue['error_msgs']=retValue['error_msgs']\
01167 +"\nError: Max position found at "+str(maxpos)\
01168 +" expected it to be at 212,220,0,20."
01169
01170 self.assertTrue(retValue['success'],retValue['error_msgs'])
01171
01172
01173 def test_stretch(self):
01174 """ imsmooth(): Test stretch parameter"""
01175 yy = iatool()
01176 mymask = "maskim"
01177 yy.fromshape(mymask, [200, 200, 1, 1])
01178 yy.addnoise()
01179 yy.done()
01180 shape = [200,200,1,20]
01181 imagename = "tmp.im"
01182 yy.fromshape(imagename, shape)
01183 yy.addnoise()
01184 yy.done()
01185 zz = imsmooth(
01186 imagename=imagename,
01187 mask=mymask + ">0", stretch=False
01188 )
01189 self.assertFalse(zz)
01190
01191 zz = imsmooth(
01192 imagename=imagename,
01193 mask=mymask + ">0", stretch=True
01194 )
01195 self.assertTrue(zz and type(zz) == type(True))
01196
01197 def test_multibeam(self):
01198 """Test per plane beams"""
01199 myia = self.ia
01200 myia.open(self.datapath + "test_image2dconvolver_multibeam.im")
01201 major = "10arcmin"
01202 minor = "8arcmin"
01203 pa = "80deg"
01204 got = myia.convolve2d(axes=[0, 1], major=major, minor=minor, pa=pa)
01205 shape = myia.shape()
01206 for i in range(5):
01207 blc=[0, 0, i]
01208 trc=[shape[0]-1, shape[1]-1, i]
01209 reg = rg.box(blc=blc, trc=trc)
01210 xx = myia.subimage(region=reg)
01211 exp = xx.convolve2d(axes=[0, 1], major=major, minor=minor, pa=pa)
01212 expbeam = exp.restoringbeam()
01213 gotbeam = got.restoringbeam(channel=i)
01214 for j in ["major", "minor", "positionangle"]:
01215 self.assertTrue(_near(gotbeam[j], expbeam[j], 2e-7))
01216 self.assertTrue(abs(got.getchunk(blc=blc, trc=trc) - exp.getchunk()).max() < 3e-5)
01217 exp.done()
01218 xx.done()
01219 got.done()
01220
01221 def test_targetres(self):
01222 """Test targetres parameter"""
01223 myia = self.ia
01224 imagename = "tres1.im"
01225 myia.fromshape(imagename, [100, 100])
01226 csys = myia.coordsys()
01227 csys.setunits(["arcsec", "arcsec"])
01228 csys.setincrement([1, 1])
01229 myia.setcoordsys(csys.torecord())
01230 myia.setbrightnessunit("Jy/beam")
01231 myia.setrestoringbeam(major="6arcsec", minor="3arcsec", pa="0deg")
01232 shape = myia.shape()
01233 values = make_gauss2d(shape, 3.0, 6.0)
01234 expected = make_gauss2d(shape, 5.0, 10.0)
01235 myia.putchunk(values)
01236 myia.done()
01237 emaj = qa.quantity("10arcsec")
01238 emin = qa.quantity("5arcsec")
01239 epa = qa.quantity("0deg")
01240 for code in (run_convolve2d, run_imsmooth):
01241 for targetres in [False, True]:
01242 if not targetres:
01243 major = "8arcsec"
01244 minor = "4arcsec"
01245 pa = "0deg"
01246 outfile = "tres1" + str(code)
01247 else:
01248 major = "10arcsec"
01249 minor = "5arcsec"
01250 pa = "0deg"
01251 outfile = "tres2" + str(code)
01252 code(
01253 imagename=imagename, kernel="gaussian",
01254 major=major, minor=minor, pa=pa, targetres=targetres,
01255 outfile=outfile
01256 )
01257 myia.open(outfile)
01258 gotbeam = myia.restoringbeam()
01259 gotvals = myia.getchunk()
01260 myia.done()
01261 self._compare_beams(
01262 gotbeam, {"major": emaj, "minor": emin, "pa": epa}
01263 )
01264 maxdiff = (abs(gotvals-expected)).max()
01265 self.assertTrue(maxdiff < 1e-6)
01266
01267 csys.addcoordinate(spectral=True)
01268 myia.fromshape(
01269 outfile=imagename, shape=[100, 100, 2],
01270 csys=csys.torecord(), overwrite=True
01271 )
01272 myia.setbrightnessunit("Jy/beam")
01273 myia.setrestoringbeam(
01274 major="6arcsec", minor="3arcsec", pa="0deg", channel=0
01275 )
01276 myia.setrestoringbeam(
01277 major="4arcsec", minor="2arcsec", pa="0deg", channel=1
01278 )
01279 values = myia.getchunk()
01280 shape = myia.shape()
01281 expected = values.copy()
01282 for k in range(shape[2]):
01283 if k == 0:
01284 xfwhm = 3
01285 yfwhm = 6
01286 else:
01287 xfwhm = 2
01288 yfwhm = 4
01289 values[:,:,k] = make_gauss2d([shape[0], shape[1]], xfwhm, yfwhm)
01290 myia.putchunk(values)
01291 outia = iatool()
01292 for targetres in [False, True]:
01293 ebeam = []
01294 if targetres:
01295 major = "10arcsec"
01296 minor = "5arcsec"
01297 else:
01298 major = "8arcsec"
01299 minor = "4arcsec"
01300 pa = "0deg"
01301
01302 for k in range(shape[2]):
01303 reg = rg.box(blc=[0, 0, k], trc=[shape[0]-1, shape[1]-1, k])
01304 subim = myia.subimage(outfile="", region=reg, dropdeg=True)
01305 convim = subim.convolve2d(
01306 type="gaussian", major=major, minor=minor,
01307 pa=pa, targetres=targetres
01308 )
01309 subim.done()
01310 expected[:, :, k] = convim.getchunk()
01311 gotbeam = convim.restoringbeam()
01312
01313 if targetres:
01314 self._compare_beams(gotbeam, {"major": major, "minor": minor, "pa": pa})
01315
01316 ebeam.append(gotbeam)
01317 convim.done()
01318 for code in [run_convolve2d, run_imsmooth]:
01319 if targetres:
01320 outfile = "tres3" + str(code)
01321 else:
01322 outfile = "tres4" + str(code)
01323 code(
01324 imagename=imagename, kernel="gaussian",
01325 major=major, minor=minor, pa=pa, targetres=targetres,
01326 outfile=outfile
01327 )
01328 outia.open(outfile)
01329 for k in range(shape[2]):
01330 gotbeam = outia.restoringbeam(channel=k)
01331 self._compare_beams(gotbeam, ebeam[k])
01332 maxdiff = (abs(outia.getchunk()-expected)).max()
01333 self.assertTrue(maxdiff < 1e-6)
01334 myia.done()
01335 outia.done()
01336
01337 def test_overwrite(self):
01338 """ test overwrite parameter """
01339 myia = self.ia
01340 outfile = "test_overwrite.im"
01341 myia.fromshape(outfile, [200, 200])
01342 imagename = "input_overwrite"
01343 myia.fromshape(imagename, [200, 200])
01344 myia.done()
01345 self.assertTrue(
01346 run_imsmooth(
01347 imagename=imagename, kernel="gauss", major="5arcmin",
01348 minor="4arcmin", pa="0deg", targetres=False,
01349 overwrite=True, outfile=outfile
01350 )
01351 )
01352 self.assertFalse(
01353 run_imsmooth(
01354 imagename=imagename,
01355 kernel="gauss", major="5arcmin", minor="4arcmin",
01356 pa="0deg", targetres=False, overwrite=False, outfile=outfile
01357 )
01358 )
01359
01360 def test_beam(self):
01361 """Test the beam parameter"""
01362 myia = self.ia
01363 imagename = "tbeam1.im"
01364 myia.fromshape(imagename, [100, 100])
01365 csys = myia.coordsys()
01366 csys.setunits(["arcsec", "arcsec"])
01367 csys.setincrement([1, 1])
01368 myia.setcoordsys(csys.torecord())
01369 myia.setbrightnessunit("Jy/beam")
01370 myia.setrestoringbeam(major="6arcsec", minor="3arcsec", pa="0deg")
01371 shape = myia.shape()
01372 myia.putchunk(make_gauss2d(shape, 3.0, 6.0))
01373 expected = make_gauss2d(shape, 5.0, 10.0)
01374 for code in (run_convolve2d, run_imsmooth):
01375 for beam in [
01376 {"major": "8arcsec", "minor": "4arcsec", "pa": "0deg"},
01377 {
01378 "major": {"unit": "arcsec", "value": 8},
01379 "minor": {"unit": "arcsec", "value": 4},
01380 "pa": {"unit": "deg", "value": 0},
01381 }
01382 ]:
01383 outfile = str(code)
01384 x = code(
01385 imagename=imagename, major="", minor="", pa="",
01386 beam=beam, outfile=outfile, targetres=False,
01387 overwrite=True
01388 )
01389 if type(x) == type(myia):
01390 x.done()
01391 myia.open(outfile)
01392 maxdiff = (abs(myia.getchunk()-expected)).max()
01393 self.assertTrue(maxdiff < 1e-6)
01394 myia.done()
01395
01396 def suite():
01397 return [imsmooth_test]
01398