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 import os
00066 import casac
00067 from tasks import *
00068 from taskinit import *
00069 import hashlib
00070 import shutil
00071 from __main__ import *
00072 import unittest
00073
00074
00075 noisy_image = "gaussian_model_with_noise.im"
00076 noisy_image_xx = "gaussian_model_with_noise_xx.im"
00077 expected_model = "gaussian_model_with_noise_model.fits"
00078 expected_residual = "gaussian_model_with_noise_resid.fits"
00079 convolved_model = "gaussian_convolved.fits"
00080 estimates_convolved = "estimates_convolved.txt"
00081 two_gaussians_image = "two_gaussian_model.fits"
00082 stokes_image = "imfit_stokes.fits"
00083 two_gaussians_estimates = "estimates_2gauss.txt"
00084 expected_new_estimates = "expected_new_estimates.txt"
00085 gauss_no_pol = "gauss_no_pol.fits"
00086 jyperbeamkms = "jyperbeamkmpersec.fits";
00087 masked_image = 'myout.im'
00088 multiplane_image = "gauss_multiplane.fits"
00089 multibeam_image = "gauss_multibeam.im"
00090 two_gauss_multiplane_estimates="estimates_2gauss_multiplane.txt"
00091 msgs = ''
00092
00093
00094 def near (first, second, epsilon):
00095 if first == 0 and second == 0:
00096 return True
00097 denom = first
00098 if first == 0:
00099 denom = second
00100 return (abs((first - second)/denom) <= abs(epsilon))
00101
00102 def near_abs(first, second, epsilon):
00103 return abs(first - second) <= epsilon
00104
00105
00106
00107
00108 def check_image(got, expected):
00109 myia = iatool()
00110 myia.open(got)
00111 gotpix = myia.getchunk()
00112 myia.open(expected)
00113 exppix = myia.getchunk()
00114 myia.done()
00115 return (gotpix - exppix == 0).all()
00116
00117
00118 def count_matches(filename, match_string):
00119 count = 0
00120 for line in open(filename):
00121 if (match_string in line):
00122 count += 1
00123 return count
00124
00125 class imfit_test(unittest.TestCase):
00126
00127 def setUp(self):
00128 datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/imfit/'
00129 for f in [
00130 noisy_image, noisy_image_xx, expected_model, expected_residual, convolved_model,
00131 estimates_convolved, two_gaussians_image, two_gaussians_estimates,
00132 expected_new_estimates, stokes_image, gauss_no_pol, jyperbeamkms,
00133 masked_image, multiplane_image, multibeam_image, two_gauss_multiplane_estimates
00134 ] :
00135 if not os.path.exists(f):
00136 if (os.path.isdir(datapath + f)):
00137 shutil.copytree(datapath + f, f)
00138 if (os.path.isfile(datapath + f)):
00139 shutil.copy(datapath + f, f)
00140
00141 def tearDown(self):
00142 for f in [
00143
00144
00145 noisy_image_xx, expected_model, expected_residual, convolved_model,
00146 estimates_convolved, two_gaussians_image, two_gaussians_estimates,
00147 expected_new_estimates, stokes_image, gauss_no_pol, jyperbeamkms,
00148 masked_image, multiplane_image, multibeam_image, two_gauss_multiplane_estimates
00149 ] :
00150 if (os.path.isdir(f)):
00151 os.system("rm -rf " + f)
00152
00153 if (os.path.isfile(f)):
00154 os.remove(f)
00155
00156 def test_fit_using_full_image(self):
00157 '''Imfit: Fit using full image'''
00158 test = "fit_using_full_image: "
00159 def run_fitcomponents():
00160 myia = iatool()
00161 myia.open(noisy_image)
00162 res = myia.fitcomponents()
00163 myia.done()
00164 return res
00165 def run_imfit():
00166 default('imfit')
00167 return imfit(imagename=noisy_image)
00168
00169 for i in [0 ,1]:
00170 if (i == 0):
00171 code = run_fitcomponents
00172 method = test + "ia.fitcomponents: "
00173 else:
00174 code = run_imfit
00175 method += test + "imfit: "
00176 self._check_results(code())
00177
00178 def _check_results(self, res):
00179 success = True
00180 global msgs
00181 clist = res['results']
00182 if (not res['converged'][0]):
00183 success = False
00184 msgs += method + "fit did not converge unexpectedly"
00185 epsilon = 1e-5
00186
00187 got = clist['component0']['flux']['value'][0]
00188 expected = 60291.7956
00189 if (not near(got, expected, epsilon)):
00190 success = False
00191 msgs += method + "I flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
00192
00193 got = clist['component0']['flux']['value'][1]
00194 expected = 0
00195 if (got != expected):
00196 success = False
00197 msgs += method + "Q flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
00198
00199 got = clist['component0']['shape']['direction']['m0']['value']
00200 expected = 0.00021339
00201 if (not near_abs(got, expected, epsilon)):
00202 success = False
00203 msgs += method + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
00204
00205 got = clist['component0']['shape']['direction']['m1']['value']
00206 expected = 1.935825e-5
00207 if (not near_abs(got, expected, epsilon)):
00208 success = False
00209 msgs += method + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
00210
00211 got = clist['component0']['shape']['majoraxis']['value']
00212 expected = 23.530022
00213 epsilon = 1e-6
00214 if (not near(got, expected, epsilon)):
00215 success = False
00216 msgs += method + "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00217
00218 got = clist['component0']['shape']['minoraxis']['value']
00219 expected = 18.862125
00220 if (not near(got, expected, epsilon)):
00221 success = False
00222 msgs += method + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00223
00224 got = clist['component0']['shape']['positionangle']['value']
00225 expected = 119.88185
00226 epsilon = 1e-5
00227 if (not near_abs(got, expected, epsilon)):
00228 success = False
00229 msgs += method + "Position angle test failure, got " + str(got) + " expected " + str(expected) + "\n"
00230
00231 self.assertTrue(success,msgs)
00232
00233
00234 def test_fit_using_box(self):
00235 '''Imfit: Fit using box'''
00236 method = "test_fit_using_box"
00237 success = True
00238 global msgs
00239 for i in range(4):
00240 test = 'fit_using_box, loop #' + str(i) + ': '
00241
00242
00243
00244
00245
00246
00247
00248 if (i == 0):
00249 box = "130,89,170,129"
00250 region = ""
00251 elif (i == 1):
00252 box = "130,89,170,129"
00253 region = rg.box([0,0,0,0],[2,2,0,0])
00254 elif (i == 2):
00255 box = ''
00256 region = rg.box([130,89,0,0],[170,129,0,0])
00257 elif (i == 3):
00258 box = ''
00259 region = 'mybox'
00260
00261 def run_fitcomponents():
00262 myia = iatool()
00263 myia.open(noisy_image)
00264 res = myia.fitcomponents(box=box, region=region)
00265 myia.close()
00266 return res
00267 def run_imfit():
00268 default('imfit')
00269 return imfit(imagename=noisy_image, box=box, region=region)
00270
00271 for code in [run_fitcomponents, run_imfit]:
00272 res = code()
00273 clist = res['results']
00274 if (not res['converged'][0]):
00275 success = False
00276 msgs += method + " fit did not converge unexpectedly. box=" + box + " region=" + str(region)
00277 epsilon = 1e-5
00278
00279 got = clist['component0']['flux']['value'][0]
00280 expected = 60319.8604
00281 if (not near(got, expected, epsilon)):
00282 success = False
00283 msgs += method + "I flux density test failure, got " + str(got) \
00284 + " expected " + str(expected) + "\n"
00285
00286 got = clist['component0']['flux']['value'][1]
00287 expected = 0
00288 if (got != expected):
00289 success = False
00290 msgs += method + "Q flux density test failure, got " + str(got) \
00291 + " expected " + str(expected) + "\n"
00292
00293 got = clist['component0']['shape']['direction']['m0']['value']
00294 expected = 0.00021337
00295 if (not near_abs(got, expected, epsilon)):
00296 success = False
00297 msgs += method + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
00298
00299 got = clist['component0']['shape']['direction']['m1']['value']
00300 expected = 1.935906e-05
00301 if (not near_abs(got, expected, epsilon)):
00302 success = False
00303 msgs += method + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
00304
00305 got = clist['component0']['shape']['majoraxis']['value']
00306 expected = 23.545212
00307 epsilon = 1e-6
00308 if (not near(got, expected, epsilon)):
00309 success = False
00310 msgs += method + "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00311 got = clist['component0']['shape']['majoraxiserror']['value']
00312 expected = 0.0188108125957
00313 epsilon = 1e-6
00314 if (not near(got, expected, epsilon)):
00315 success = False
00316 msgs += method + "Major axis error test failure, got " + str(got) + " expected " + str(expected) + "\n"
00317 if (
00318 not clist['component0']['shape']['majoraxis']['unit']
00319 == clist['component0']['shape']['majoraxiserror']['unit']
00320 ):
00321 success = False
00322 msgs += method + "Major axis and major axis error units are different\n"
00323
00324 got = clist['component0']['shape']['minoraxis']['value']
00325 expected = 18.86450
00326
00327 if (not near(got, expected, epsilon)):
00328 success = False
00329 msgs += method + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00330 got = clist['component0']['shape']['minoraxiserror']['value']
00331 expected = 0.0207481352015
00332
00333 if (not near(got, expected, epsilon)):
00334 success = False
00335 msgs += method + "Minor axis error test failure, got " + str(got) + " expected " + str(expected) + "\n"
00336 if (
00337 not clist['component0']['shape']['minoraxis']['unit']
00338 == clist['component0']['shape']['minoraxiserror']['unit']
00339 ):
00340 success = False
00341 msgs += method + "Minor axis and minor axis error units are different\n"
00342
00343 got = clist['component0']['shape']['positionangle']['value']
00344 expected = 119.81296
00345 epsilon = 1e-5
00346 if (not near_abs(got, expected, epsilon)):
00347 success = False
00348 msgs += method + "Position angle test failure, got " + str(got) \
00349 + " expected " + str(expected) + "\n"
00350 got = clist['component0']['shape']['positionangleerror']['value']
00351 expected = 0.1425508
00352 if (not near(got, expected, epsilon)):
00353 success = False
00354 msgs += method + "Position angle error test failure, got " + str(got) + " expected " + str(expected) + "\n"
00355 if (
00356 not clist['component0']['shape']['positionangle']['unit']
00357 == clist['component0']['shape']['positionangleerror']['unit']
00358 ):
00359 success = False
00360 msgs += method + "Position angle and position angle error units are different\n"
00361 self.assertTrue(success,msgs)
00362
00363 def test_nonconvergence(self):
00364 '''Imfit: Test non-convergence'''
00365 test = "test_nonconvergence: "
00366 success = True
00367 global msgs
00368
00369 box = '0,0,20,20'
00370 def run_fitcomponents():
00371 myia = iatool()
00372 myia.open(noisy_image)
00373 res = myia.fitcomponents(box=box)
00374 myia.done()
00375 return res
00376 def run_imfit():
00377 default('imfit')
00378 return imfit(imagename=noisy_image, box=box)
00379
00380 for code in [run_fitcomponents, run_imfit]:
00381 res = code()
00382 if (res['converged'][0]):
00383 success = False
00384 msgs += method + "fit unexpectedly converged\n"
00385
00386 self.assertTrue(success,msgs)
00387
00388 def test_fit_using_range(self):
00389 '''Imfit: Fit using range'''
00390 success = True
00391 global msgs
00392 for i in range(4):
00393 test = 'fit_using_range, loop #' + str(i) + ': '
00394
00395
00396
00397
00398
00399
00400
00401 if (i == 0):
00402 mask = noisy_image + ">40"
00403 includepix = []
00404 excludepix = []
00405 pixelmask = ""
00406 elif (i == 1):
00407 mask = ''
00408 includepix = [40,400]
00409 excludepix = []
00410 pixelmask = ""
00411 elif (i == 2):
00412 mask = ''
00413 includepix = []
00414 excludepix = [-10,40]
00415 pixelmask = ""
00416 elif (i == 3):
00417 mask = ''
00418 includepix = []
00419 excludepix = []
00420 pixelmask = "mymask"
00421
00422 def run_fitcomponents():
00423 myia = iatool()
00424 myia.open(masked_image)
00425 myia.maskhandler("set", pixelmask)
00426 res = myia.fitcomponents(mask=mask, includepix=includepix, excludepix=excludepix)
00427 myia.close()
00428 return res
00429 def run_imfit():
00430 default('imfit')
00431 return imfit(imagename=masked_image, mask=mask, includepix=includepix, excludepix=excludepix)
00432
00433 for code in [run_fitcomponents, run_imfit]:
00434 res = code()
00435 clist = res['results']
00436 if (not res['converged'][0]):
00437 success = False
00438 msgs += method + "fit did not converge unexpectedly"
00439 epsilon = 1e-5
00440
00441 got = clist['component0']['flux']['value'][0]
00442 expected = 60354.3232
00443 if (not near(got, expected, epsilon)):
00444 success = False
00445 msgs += test + "I flux density test failure, got " + str(got) \
00446 + " expected " + str(expected) + "\n"
00447
00448 got = clist['component0']['flux']['value'][1]
00449 expected = 0
00450 if (got != expected):
00451 success = False
00452 msgs += test + "Q flux density test failure, got " + str(got) \
00453 + " expected " + str(expected) + "\n"
00454
00455 got = clist['component0']['shape']['direction']['m0']['value']
00456 expected = 0.000213391
00457 if (not near(got, expected, epsilon)):
00458 success = False
00459 msgs += test + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
00460
00461 got = clist['component0']['shape']['direction']['m1']['value']
00462 expected = 1.93449e-05
00463 if (not near(got, expected, epsilon)):
00464 success = False
00465 msgs += test + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
00466
00467 got = clist['component0']['shape']['majoraxis']['value']
00468 expected = 23.541712
00469 epsilon = 1e-7
00470 if (not near(got, expected, epsilon)):
00471 success = False
00472 msgs += test + "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00473
00474 got = clist['component0']['shape']['minoraxis']['value']
00475 expected = 18.882029
00476 if (not near(got, expected, epsilon)):
00477 success = False
00478 msgs += test + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00479
00480 got = clist['component0']['shape']['positionangle']['value']
00481 expected = 119.769648
00482 if (not near(got, expected, epsilon)):
00483 success = False
00484 msgs += test + "Position angle test failure, got " + str(got) + \
00485 " expected " + str(expected) + "\n"
00486
00487 self.assertTrue(success,msgs)
00488
00489
00490
00491 def test_residual_and_model(self):
00492 '''Imfit: Test residual and model'''
00493 test = "residual_and_model_test: "
00494 success = True
00495 global msgs
00496 box="100,100,200,200"
00497 def run_fitcomponents(model, residual):
00498 myia = iatool()
00499 myia.open(noisy_image)
00500 res = myia.fitcomponents(
00501 box=box, residual=residual, model=model
00502 )
00503 myia.done()
00504 return res
00505 def run_imfit(model, residual):
00506 default('imfit')
00507 return imfit(
00508 imagename=noisy_image, box=box, residual=residual,
00509 model=model
00510 )
00511
00512 for code in [run_fitcomponents, run_imfit]:
00513 model = 'model_' + str(code) + '.im'
00514 residual = 'resid_' + str(code) + '.im'
00515
00516 res = code(model, residual)
00517 clist = res['results']
00518
00519 if (not res['converged'][0]):
00520 success = False
00521 msgs + test + "fit did not converge unexpectedly"
00522 if (not check_image(residual, expected_residual)):
00523 success = False
00524 msgs += test + "Did not get expected residual image\n"
00525 if (not check_image(model, expected_model)):
00526 success = False
00527 msgs += test + "Did not get expected model image\n"
00528
00529 self.assertTrue(success,msgs)
00530
00531
00532 def test_fit_using_estimates(self):
00533 '''Imfit: Test using estimates'''
00534 success = True
00535 test = 'fit_using_estimates: '
00536 global msgs
00537
00538 def run_fitcomponents():
00539 myia = iatool()
00540 myia.open(convolved_model)
00541 res = myia.fitcomponents(estimates=estimates_convolved)
00542 print "** image " + convolved_model
00543 print "*** estimates " + estimates_convolved
00544 myia.done()
00545 return res
00546 def run_imfit():
00547 default('imfit')
00548 return imfit(imagename=convolved_model, estimates=estimates_convolved)
00549
00550 for code in [run_fitcomponents, run_imfit]:
00551 res = code()
00552
00553 clist = res['results']
00554 if (not res['converged'][0]):
00555 success = False
00556 msgs += test + "fit did not converge unexpectedly"
00557 epsilon = 1e-5
00558
00559 got = clist['component0']['flux']['value'][0]
00560 expected = 60082.6
00561 if (not near(got, expected, epsilon)):
00562 success = False
00563 msgs += test + "I flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
00564
00565 got = clist['component0']['flux']['value'][1]
00566 expected = 0
00567 if (got != expected):
00568 success = False
00569 msgs += test + "Q flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
00570
00571 shape = clist['component0']['shape']
00572 got = shape['direction']['m0']['value']
00573 expected = 0.000213318
00574 if (not near(got, expected, epsilon)):
00575 success = False
00576 msgs += test + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
00577
00578 got = shape['direction']['m1']['value']
00579 expected = 1.939254e-5
00580 if (not near(got, expected, epsilon)):
00581 success = False
00582 msgs += test + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
00583
00584 got = shape['majoraxis']['value']
00585 expected = 28.21859344
00586 epsilon = 1e-7
00587 if (not near(got, expected, epsilon)):
00588 success = False
00589 msgs += test+ "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00590
00591 got = shape['minoraxis']['value']
00592 expected = 25.55011520
00593 if (not near(got, expected, epsilon)):
00594 success = False
00595 msgs += test + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00596
00597 got = shape['positionangle']['value']
00598 expected = 126.3211050
00599 if (not near(got, expected, epsilon)):
00600 success = False
00601 msgs += test + "Position angle test failure, got " + str(got) + " expected " + str(expected) + "\n"
00602
00603
00604
00605
00606 self.assertTrue(success,msgs)
00607
00608 def test_position_errors(self):
00609 '''Imfit: Test position errors'''
00610 success = True
00611 test = 'test_position_errors: '
00612 global msgs
00613
00614 def run_fitcomponents():
00615 myia = iatool()
00616 myia.open(convolved_model)
00617 res = myia.fitcomponents()
00618 myia.done()
00619 return res
00620 def run_imfit():
00621 default('imfit')
00622 return imfit(imagename=convolved_model)
00623
00624 for code in [run_fitcomponents, run_imfit]:
00625 res = code()
00626 clist = res['results']
00627 shape = clist['component0']['shape']
00628
00629 if (not res['converged'][0]):
00630 success = False
00631 msgs += test + "fit did not converge unexpectedly"
00632 epsilon = 1e-5
00633
00634 got = shape['direction']['error']['latitude']['value']
00635 expected = 1.0511699866407922e-07
00636 if (not near(got, expected, epsilon)):
00637 success = False
00638 msgs += test + "Dec error test failure, got " + str(got) + " expected " + str(expected) + "\n"
00639
00640 got = shape['direction']['error']['longitude']['value']
00641 expected = 8.8704046316191542e-08
00642 if (not near(got, expected, epsilon)):
00643 success = False
00644 msgs += test + "RA error test failure, got " + str(got) + " expected " + str(expected) + "\n"
00645
00646 self.assertTrue(success,msgs)
00647
00648
00649
00650
00651
00652 def test_logfile(self):
00653 '''Imfit: Test logfile'''
00654 success = True
00655 test = "test_logfile: "
00656 global msgs
00657
00658 for i in [0, 1]:
00659 logfile = os.getcwd() + "/imfit.log" + str(i)
00660 if (i == 0):
00661 def run_fitcomponents(append=None):
00662 myia = iatool()
00663 myia.open(two_gaussians_image)
00664 if (append == None):
00665 res = myia.fitcomponents(estimates=two_gaussians_estimates, logfile=logfile)
00666 else:
00667 res = myia.fitcomponents(estimates=two_gaussians_estimates, logfile=logfile, append=append)
00668 myia.done()
00669 return res
00670
00671 code = run_fitcomponents
00672 method = test + "ia.fitcomponents: "
00673 else:
00674 def run_imfit(append=None):
00675 default('imfit')
00676 if (append == None):
00677 return imfit(imagename=two_gaussians_image, estimates=two_gaussians_estimates, logfile=logfile)
00678 else:
00679 return imfit(
00680 imagename=two_gaussians_image, estimates=two_gaussians_estimates,
00681 logfile=logfile, append=append
00682 )
00683 code = run_imfit
00684 method = test + "imfit: "
00685 res = code()
00686 if (not os.path.exists(logfile)):
00687 success = False
00688 msgs += method + "logfile was not written\n"
00689 return {'success' : success, 'error_msgs' : msgs}
00690
00691 if ( count_matches(logfile, "****** Fit performed") != 1):
00692 success = False
00693 msgs += method + "unexpected logfile\n"
00694
00695 res = code()
00696 if (not os.path.exists(logfile)):
00697 success = False
00698 msgs += method + "logfile was not written\n"
00699 return {'success' : success, 'error_msgs' : msgs}
00700
00701 if ( count_matches(logfile, "****** Fit performed") != 2):
00702 success = False
00703 msgs += method + "logfile not appended\n"
00704
00705
00706 res = code(True)
00707 if (not os.path.exists(logfile)):
00708 success = False
00709 msgs += method + "logfile was not written\n"
00710 return {'success' : success, 'error_msgs' : msgs}
00711
00712 if ( count_matches(logfile, "****** Fit performed") != 3):
00713 success = False
00714 msgs += method + "logfile not appended\n"
00715
00716 res = code(False)
00717 if (not os.path.exists(logfile)):
00718 success = False
00719 msgs += method + "logfile was not written\n"
00720 return {'success' : success, 'error_msgs' : msgs}
00721
00722 if ( count_matches(logfile, "****** Fit performed") != 1):
00723 success = False
00724 msgs += method + "logfile not overwritten\n"
00725
00726 self.assertTrue(success,msgs)
00727
00728
00729 def test_newestimates(self):
00730 '''Imfit: Test new estimates'''
00731 success = True
00732 test = 'test_newestimates: '
00733 global msgs
00734
00735 for i in [0, 1]:
00736 newestimates = "newestimates" + str(i) + ".txt"
00737 if (i == 0):
00738 def run_fitcomponents():
00739 myia = iatool()
00740 myia.open(two_gaussians_image)
00741 res = myia.fitcomponents(estimates=two_gaussians_estimates, newestimates=newestimates)
00742 return res
00743 code = run_fitcomponents
00744 method = test + "ia.fitcomponents: "
00745 else:
00746 def run_imfit():
00747 default('imfit')
00748 return imfit(
00749 imagename=two_gaussians_image, estimates=two_gaussians_estimates, newestimates=newestimates
00750 )
00751 code = run_imfit
00752 method = test + "imfit: "
00753 res = code()
00754
00755 if (not os.path.exists(newestimates)):
00756 success = False
00757 msgs += method + "new estimates file was not written\n"
00758 return {'success' : success, 'error_msgs' : msgs}
00759
00760 expected_sha = hashlib.sha512(open(expected_new_estimates, 'r').read()).hexdigest()
00761
00762 got_sha = hashlib.sha512(open(newestimates, 'r').read()).hexdigest()
00763 if (got_sha != expected_sha):
00764 success = False
00765 msgs += method + "new estimates file differs from expected\n"
00766
00767 self.assertTrue(success,msgs)
00768
00769
00770 def test_polarization_image(self):
00771 '''Imfit: Test polarization image'''
00772 success = True
00773 test = 'test_polarization_image: '
00774 global msgs
00775 def run_fitcomponents(stokes):
00776 myia = iatool()
00777 myia.open(stokes_image)
00778 res = myia.fitcomponents(stokes=stokes)
00779 return res
00780 def run_imfit(stokes):
00781 default('imfit')
00782 return imfit(imagename=stokes_image, stokes=stokes)
00783
00784 stokes = ['I','Q','U','V']
00785 expectedFlux = [133.60641, 400.81921, 375.76801, -1157.92212]
00786 expectedRA = [1.2479113396, 1.2479113694, 1.2478908580, 1.2478908284]
00787 expectedDec = [0.782579122, 0.782593666, 0.782593687, 0.782579143]
00788 expectedMaj = [7.992524398, 11.988806751, 8.991589959, 12.987878913]
00789 expectedMin = [5.994405977, 5.994395540, 4.995338093, 7.992524265]
00790 expectedPA = [40.083248, 160.083213, 50.082442, 135.08243]
00791
00792 for i in [0 ,1]:
00793 for j in range(len(stokes)):
00794 if (i == 0):
00795 code = run_fitcomponents
00796 method = test + "ia.fitcomponents: "
00797 else:
00798 code = run_imfit
00799 method += test + "imfit: "
00800 res = code(stokes[j])
00801
00802 clist = res['results']
00803 if (not res['converged'][0]):
00804 success = False
00805 msgs += method + "fit did not converge unexpectedly for stokes " + stokes[j]
00806 got = clist['component0']['flux']['value'][j]
00807
00808
00809 if (not near(got, expectedFlux[j], 1e-5)):
00810 success = False
00811 msgs += method + " " + str(stokes) + " flux density test failure, got " + str(got) + " expected " + str(expectedFlux[j]) + "\n"
00812
00813
00814 got = clist['component0']['shape']['direction']['m0']['value']
00815 if (not near_abs(got, expectedRA[j], 1e-8)):
00816 success = False
00817 msgs += method + "stokes " + stokes[j] + " RA test failure, got " + str(got) + " expected " + str(expectedRA[j]) + "\n"
00818
00819
00820 got = clist['component0']['shape']['direction']['m1']['value']
00821 if (not near_abs(got, expectedDec[j], 1e-8)):
00822 success = False
00823 msgs += method + "stokes " + stokes[j] + " Dec test failure, got " + str(got) + " expected " + str(expectedDec[j]) + "\n"
00824
00825
00826 got = clist['component0']['shape']['majoraxis']['value']
00827 if (not near(got, expectedMaj[j], 1e-7)):
00828 success = False
00829 msgs += method + "stokes " + stokes[j] + " Major axis test failure, got " + str(got) + " expected " + str(expectedMaj[j]) + "\n"
00830
00831
00832 got = clist['component0']['shape']['minoraxis']['value']
00833 if (not near(got, expectedMin[j], 1e-7)):
00834 success = False
00835 msgs += method + "stokes " + stokes[j] + " Minor axis test failure, got " + str(got) + " expected " + str(expectedMin[j]) + "\n"
00836
00837
00838 got = clist['component0']['shape']['positionangle']['value']
00839 if (not near_abs(got, expectedPA[j], 1e-5)):
00840 success = False
00841 msgs += method + "stokes " + stokes[j] + " Position angle test failure, got " + str(got) + " expected " + str(expectedPA[j]) + "\n"
00842
00843 self.assertTrue(success,msgs)
00844
00845 def test_CAS_2318(self):
00846 "Verification of CAS-2318 fix"
00847 success = True
00848 test = 'test_CAS_2318: '
00849 global msgs
00850 def run_fitcomponents():
00851 myia = iatool()
00852 myia.open(gauss_no_pol)
00853 res = myia.fitcomponents()
00854 myia.done()
00855 return res
00856 def run_imfit():
00857 default('imfit')
00858 return imfit(imagename=gauss_no_pol)
00859
00860 for code in [run_fitcomponents, run_imfit]:
00861
00862
00863 clist = code()['results']
00864 got = clist['component0']['flux']['value'][0]
00865 expected = 394312.65593496
00866 self.assertTrue(near(got, expected, 1e-5))
00867
00868 def test_CAS_1233(self):
00869 method = "test_CAS_1233"
00870 test = "test_CAS_1233"
00871
00872 global msgs
00873 success = True
00874 def run_fitcomponents():
00875 myia = iatool()
00876 myia.open(jyperbeamkms)
00877 res = myia.fitcomponents()
00878 myia.done()
00879 return res
00880 def run_imfit():
00881 default('imfit')
00882 return imfit(imagename=jyperbeamkms)
00883
00884 for i in [0 ,1]:
00885 if (i == 0):
00886 code = run_fitcomponents
00887 method = test + "ia.fitcomponents: "
00888 else:
00889 code = run_imfit
00890 method += test + "imfit: "
00891 res = code()
00892 clist = res['results']
00893 if (not res['converged'][0]):
00894 success = False
00895 msgs += method + "fit did not converge unexpectedly"
00896 epsilon = 1e-5
00897
00898 self.assertTrue(clist['component0']['flux']['unit'] == 'Jy.km/s')
00899
00900 got = clist['component0']['flux']['value'][0]
00901 expected = 60318.6
00902 if (not near(got, expected, epsilon)):
00903 success = False
00904 msgs += method + "I flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
00905
00906
00907 got = clist['component0']['shape']['direction']['m0']['value']
00908 expected = 0.000213318
00909 if (not near_abs(got, expected, epsilon)):
00910 success = False
00911 msgs += method + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
00912
00913 got = clist['component0']['shape']['direction']['m1']['value']
00914 expected = 1.939254e-5
00915 if (not near_abs(got, expected, epsilon)):
00916 success = False
00917 msgs += method + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
00918
00919 got = clist['component0']['shape']['majoraxis']['value']
00920 expected = 26.50461508
00921 epsilon = 1e-6
00922 if (not near(got, expected, epsilon)):
00923 success = False
00924 msgs += method + "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00925
00926 got = clist['component0']['shape']['minoraxis']['value']
00927 expected = 23.99821851
00928 if (not near(got, expected, epsilon)):
00929 success = False
00930 msgs += method + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
00931
00932 got = clist['component0']['shape']['positionangle']['value']
00933 expected = 126.3211060
00934 epsilon = 1e-5
00935 if (not near_abs(got, expected, epsilon)):
00936 success = False
00937 msgs += method + "Position angle test failure, got " + str(got) + " expected " + str(expected) + "\n"
00938
00939 self.assertTrue(success,msgs)
00940
00941 def test_CAS_2633(self):
00942 method = "test_CAS_2633"
00943 test = "test_CAS_2633"
00944
00945 global msgs
00946 success = True
00947 def run_fitcomponents():
00948 myia = iatool()
00949 myia.open(jyperbeamkms)
00950 res = myia.fitcomponents()
00951 myia.done()
00952 return res
00953 def run_imfit():
00954 default('imfit')
00955 return imfit(imagename=jyperbeamkms)
00956
00957 for i in [0 ,1]:
00958 if (i == 0):
00959 code = run_fitcomponents
00960 method = test + "ia.fitcomponents: "
00961 else:
00962 code = run_imfit
00963 method += test + "imfit: "
00964 res = code()
00965 clist = res['results']
00966 if (not res['converged'][0]):
00967 success = False
00968 msgs += method + "fit did not converge unexpectedly"
00969 epsilon = 1e-7
00970
00971 got = clist['component0']['spectrum']['frequency']['m0']['value']
00972 expected = 1.415
00973 if (not near(got, expected, epsilon)):
00974 success = False
00975 msgs += method + "frequency test failure, got " + str(got) + " expected " + str(expected) + "\n"
00976
00977
00978 self.assertTrue(success,msgs)
00979
00980 def test_CAS_2595(self):
00981 """ Test CAS-2595 feature addition: write component list table"""
00982 method = "test_CAS_2595"
00983 test = "test_CAS_2595"
00984 mycl = cltool()
00985 complist = "mycomplist.tbl"
00986 def run_fitcomponents(imagename, estimates, overwrite):
00987 myia = iatool()
00988 myia.open(imagename)
00989 res = myia.fitcomponents(complist=complist, estimates=estimates, overwrite=overwrite)
00990 myia.done()
00991 return res
00992 def run_imfit(imagename, estimates, overwrite):
00993 default('imfit')
00994 return imfit(imagename=imagename, estimates=estimates, complist=complist, overwrite=overwrite)
00995
00996 for code in (run_fitcomponents, run_imfit):
00997 res = code(noisy_image, "", False)
00998 mycl.open(complist)
00999 self.assertTrue(
01000 mycl.length() == 1,
01001 str(code) + " didn't get 1 component as expected from table"
01002 )
01003 mycl.done()
01004
01005 res = code(two_gaussians_image, two_gaussians_estimates, False)
01006 mycl.open(complist)
01007 self.assertTrue(
01008 mycl.length() == 1,
01009 str(code) + " didn't get 1 component as expected from table"
01010 )
01011 mycl.done()
01012
01013 res = code(two_gaussians_image, two_gaussians_estimates, True)
01014 mycl.open(complist)
01015 self.assertTrue(
01016 mycl.length() == 2,
01017 str(code) + " didn't get 2 component as expected from table"
01018 )
01019
01020 mycl.done()
01021 shutil.rmtree(complist)
01022
01023 def test_CAS_2999(self):
01024 """Test multiplane fitting"""
01025
01026 method = "test_CAS_2999"
01027 test = "test_CAS_2999"
01028 imagename = multiplane_image
01029 complist = "mycomplist.tbl"
01030 estimates = two_gauss_multiplane_estimates
01031 chans = "0~3"
01032 resid = "residualImage_multi"
01033 model = "modelImage_multi"
01034 mask = "gauss_multiplane.fits<15";
01035 def run_fitcomponents():
01036 myia = iatool()
01037 myia.open(imagename)
01038 res = myia.fitcomponents(
01039 chans=chans, mask=mask, complist=complist,
01040 estimates=estimates, overwrite=True,
01041 model=model, residual=resid
01042 )
01043 myia.done()
01044 return res
01045 def run_imfit():
01046 default('imfit')
01047 return imfit(
01048 imagename=imagename, chans=chans,
01049 mask=mask, complist=complist, estimates=estimates,
01050 overwrite=True, model=model, residual=resid
01051 )
01052 mycl = cltool()
01053 for code in (run_fitcomponents, run_imfit):
01054 res = code()
01055 mycl.open(complist)
01056 self.assertTrue(
01057 mycl.length() == 8,
01058 str(code) + " didn't get 8 component as expected from table"
01059 )
01060 mycl.done()
01061 self.assertTrue(
01062 res['converged'].size == 4,
01063 "Size of converged array is not 4"
01064 )
01065 self.assertTrue(
01066 all(res['converged']),
01067 "One or more of the converged elements are False"
01068 )
01069
01070 def test_zero_level(self):
01071 """Test zero level fitting"""
01072
01073 method = "test_zero_level"
01074 test = "test_zero_level"
01075 mycl = cltool()
01076 myia = iatool()
01077
01078 def run_fitcomponents(imagename):
01079 myia = iatool()
01080 myia.open(imagename)
01081 res = myia.fitcomponents(
01082 box="130,89,170,129", dooff=True, offset=0.0
01083 )
01084 myia.done()
01085 return res
01086 def run_imfit(imagename):
01087 default('imfit')
01088 return imfit(
01089 imagename=imagename,
01090 box="130,89,170,129", dooff=True, offset=0.0
01091 )
01092
01093 j = 0
01094 for code in (run_fitcomponents, run_imfit):
01095 for i in range(3):
01096 if i == 0:
01097 off = -10
01098 if i == 1:
01099 off = 0
01100 if i == 2:
01101 off = 5
01102 myia.open(noisy_image)
01103 myshape = myia.shape()
01104 csys = myia.coordsys().torecord()
01105 myia.done()
01106 myia.fromshape(
01107 "tmp" + str(i) + "_" + str(j) + ".im",
01108 myshape, csys
01109 )
01110 myia.calc(noisy_image + "+" + str(off))
01111 imagename = "xx" + str(i) + "_" + str(j) + ".im"
01112 myia.subimage(imagename)
01113 myia.done()
01114
01115 res = code(imagename)
01116 mycl.fromrecord(res["results"])
01117 got = mycl.getfluxvalue(0)[0]
01118 expected = 60498.5586
01119 epsilon = 1e-5
01120 self.assertTrue(near(got, expected, epsilon))
01121 got = mycl.getfluxvalue(0)[1]
01122 self.assertTrue(got == 0)
01123 got = mycl.getrefdir(0)["m0"]["value"]
01124 expected = 0.000213372126
01125 epsilon = 1e-5
01126 self.assertTrue(near(got, expected, epsilon))
01127 got = mycl.getrefdir(0)["m1"]["value"]
01128 expected = 1.93581236e-05
01129 epsilon = 1e-5
01130 self.assertTrue(near(got, expected, epsilon))
01131 shape = mycl.getshape(0)
01132 got = shape["majoraxis"]["value"]
01133 expected = 23.5743464
01134 epsilon = 1e-5
01135 self.assertTrue(near(got, expected, epsilon))
01136 got = shape["minoraxis"]["value"]
01137 expected = 18.8905131
01138 epsilon = 1e-5
01139 self.assertTrue(near(got, expected, epsilon))
01140 got = shape["positionangle"]["value"]
01141 expected = 119.818744
01142 epsilon = 1e-5
01143 self.assertTrue(near(got, expected, epsilon))
01144 mycl.done()
01145
01146 got = res["zerooff"]
01147 expected = off - 0.102277
01148 self.assertTrue(near(got, expected, epsilon))
01149 mycl.done()
01150
01151 j = j + 1
01152
01153 def test_fix_zero_level(self):
01154 """Test fixing zero level offset"""
01155
01156 method = "test_fix_zero_level"
01157 test = method
01158 mycl = cltool()
01159 myia = iatool()
01160 offset = -0.102277
01161 imagename = noisy_image
01162
01163 def run_fitcomponents(imagename):
01164 myia = iatool()
01165 myia.open(imagename)
01166 res = myia.fitcomponents(
01167 box="130,89,170,129", dooff=True,
01168 offset=offset, fixoffset=True
01169 )
01170 myia.done()
01171 return res
01172 j = 0
01173 def run_imfit(imagename):
01174 default('imfit')
01175 return imfit(
01176 imagename=imagename,
01177 box="130,89,170,129", dooff=True,
01178 offset=offset, fixoffset=True
01179 )
01180 for code in (run_fitcomponents, run_imfit):
01181 res = code(imagename)
01182 mycl.fromrecord(res["results"])
01183 got = mycl.getfluxvalue(0)[0]
01184 expected = 60498.5586
01185 epsilon = 1e-5
01186 print "***got " + str(got)
01187 self.assertTrue(near(got, expected, epsilon))
01188 got = mycl.getfluxvalue(0)[1]
01189 self.assertTrue(got == 0)
01190 got = mycl.getrefdir(0)["m0"]["value"]
01191 expected = 0.000213372126
01192 epsilon = 1e-5
01193 self.assertTrue(near(got, expected, epsilon))
01194 got = mycl.getrefdir(0)["m1"]["value"]
01195 expected = 1.93581236e-05
01196 epsilon = 1e-5
01197 self.assertTrue(near(got, expected, epsilon))
01198 shape = mycl.getshape(0)
01199 got = shape["majoraxis"]["value"]
01200 expected = 23.5743464
01201 epsilon = 1e-5
01202 self.assertTrue(near(got, expected, epsilon))
01203 got = shape["minoraxis"]["value"]
01204 expected = 18.8905131
01205 epsilon = 1e-5
01206 self.assertTrue(near(got, expected, epsilon))
01207 got = shape["positionangle"]["value"]
01208 expected = 119.818744
01209 epsilon = 1e-5
01210 self.assertTrue(near(got, expected, epsilon))
01211 mycl.done()
01212
01213 got = res["zerooff"]
01214 expected = offset
01215 self.assertTrue(near(got, expected, epsilon))
01216
01217 got = res["zeroofferr"]
01218 expected = 0
01219 self.assertTrue(near(got, expected, epsilon))
01220 mycl.done()
01221
01222 j = j + 1
01223
01224 def test_stretch(self):
01225 """imfit : test mask stretch"""
01226 imagename = multiplane_image
01227 yy = iatool()
01228 yy.open(imagename)
01229 mycsys = yy.coordsys().torecord()
01230 yy.done()
01231 mymask = "maskim"
01232 yy.fromshape(mymask, [70, 70, 1])
01233 yy.setcoordsys(mycsys)
01234 yy.addnoise()
01235 yy.done()
01236 yy.open(imagename)
01237 zz = yy.fitcomponents(
01238 mask=mymask + ">-100",
01239 stretch=False
01240 )
01241 self.assertTrue(zz['results']['nelements'] == 0)
01242 zz = imfit(
01243 imagename, mask=mymask + ">-100",
01244 stretch=False
01245 )
01246 self.assertTrue(zz['results']['nelements'] == 0)
01247
01248 zz = yy.fitcomponents(
01249 mask=mymask + ">-100",
01250 stretch=True
01251 )
01252 self.assertTrue(zz['results']['nelements'] == 4)
01253
01254 yy.done()
01255 zz = imfit(
01256 imagename, mask=mymask + ">-100",
01257 stretch=True
01258 )
01259 self.assertTrue(zz['results']['nelements'] == 4)
01260
01261 def test_non_zero_channel(self):
01262 """imfit: test fitting for channel number other than zero (CAS-3676)"""
01263 imagename = multiplane_image
01264 chans = "1~3"
01265 def run_fitcomponents():
01266 myia = iatool()
01267 myia.open(imagename)
01268 res = myia.fitcomponents(
01269 chans=chans, mask="", complist="",
01270 estimates="", overwrite=True,
01271 model="", residual=""
01272 )
01273 myia.done()
01274 return res
01275 def run_imfit():
01276 default('imfit')
01277 return imfit(
01278 imagename=imagename, chans=chans,
01279 mask="", complist="", estimates="",
01280 overwrite=True, model="", residual=""
01281 )
01282 mycl = cltool()
01283 epsilon = 1e-5
01284 for code in (run_fitcomponents, run_imfit):
01285 res = code()
01286 mycl.fromrecord(res["results"])
01287 self.assertTrue(
01288 near(mycl.getfluxvalue(0)[0], 757.2717438, epsilon),
01289 str(code) + " didn't get right flux for comp 0"
01290 )
01291 self.assertTrue(
01292 near(mycl.getfluxvalue(1)[0], 1048.7750351, epsilon),
01293 str(code) + " didn't get right flux for comp 1"
01294 )
01295 self.assertTrue(
01296 near(mycl.getfluxvalue(2)[0], 2712.41789, epsilon),
01297 str(code) + " didn't get right flux for comp 2"
01298 )
01299 mycl.done()
01300 self.assertTrue(
01301 res['converged'].size == 3,
01302 "Size of converged array is not 3"
01303 )
01304 self.assertTrue(
01305 all(res['converged']),
01306 "One or more of the converged elements are False"
01307 )
01308
01309 def test_xx_fit(self):
01310 '''Imfit: Fit using full image'''
01311 success = True
01312 test = "fit_xx: "
01313 global msgs
01314 def run_fitcomponents():
01315 myia = iatool()
01316 myia.open(noisy_image_xx)
01317 res = myia.fitcomponents()
01318 myia.done()
01319 return res
01320 def run_imfit():
01321 default('imfit')
01322 return imfit(imagename=noisy_image_xx)
01323
01324 for i in [0 ,1]:
01325 if (i == 0):
01326 code = run_fitcomponents
01327 method = test + "ia.fitcomponents: "
01328 else:
01329 code = run_imfit
01330 method += test + "imfit: "
01331 res = code()
01332 clist = res['results']
01333 if (not res['converged'][0]):
01334 success = False
01335 msgs += method + "fit did not converge unexpectedly"
01336 epsilon = 1e-5
01337
01338 got = clist['component0']['flux']['value'][0]
01339 expected = 60291.7956
01340 if (not near(got, expected, epsilon)):
01341 success = False
01342 msgs += method + "I flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
01343
01344 got = clist['component0']['flux']['value'][1]
01345 expected = 0
01346 if (got != expected):
01347 success = False
01348 msgs += method + "Q flux density test failure, got " + str(got) + " expected " + str(expected) + "\n"
01349
01350 got = clist['component0']['shape']['direction']['m0']['value']
01351 expected = 0.00021339
01352 if (not near_abs(got, expected, epsilon)):
01353 success = False
01354 msgs += method + "RA test failure, got " + str(got) + " expected " + str(expected) + "\n"
01355
01356 got = clist['component0']['shape']['direction']['m1']['value']
01357 expected = 1.935825e-5
01358 if (not near_abs(got, expected, epsilon)):
01359 success = False
01360 msgs += method + "Dec test failure, got " + str(got) + " expected " + str(expected) + "\n"
01361
01362 got = clist['component0']['shape']['majoraxis']['value']
01363 expected = 23.530022
01364 epsilon = 1e-6
01365 if (not near(got, expected, epsilon)):
01366 success = False
01367 msgs += method + "Major axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
01368
01369 got = clist['component0']['shape']['minoraxis']['value']
01370 expected = 18.862125
01371 if (not near(got, expected, epsilon)):
01372 success = False
01373 msgs += method + "Minor axis test failure, got " + str(got) + " expected " + str(expected) + "\n"
01374
01375 got = clist['component0']['shape']['positionangle']['value']
01376 expected = 119.88185
01377 epsilon = 1e-5
01378 if (not near_abs(got, expected, epsilon)):
01379 success = False
01380 msgs += method + "Position angle test failure, got " + str(got) + " expected " + str(expected) + "\n"
01381
01382 self.assertTrue(success,msgs)
01383
01384 def test_multibeam(self):
01385 myia = iatool()
01386 myia.open(multibeam_image)
01387
01388 res = myia.fitcomponents()
01389 self.assertTrue(res["converged"].all())
01390
01391 def test_strange_units(self):
01392 '''Imfit: Test strange units'''
01393 myia = iatool()
01394 test = "test_strange_units: "
01395 myia.open(noisy_image)
01396 outname = "bad_units.im"
01397 subim = myia.subimage(outname)
01398 myia.done()
01399 unit = "erg"
01400 subim.setbrightnessunit(unit)
01401 self.assertTrue(subim.brightnessunit() == unit)
01402 subim.done()
01403 def run_fitcomponents():
01404 myia.open(outname)
01405 res = myia.fitcomponents()
01406 myia.done()
01407 return res
01408 def run_imfit():
01409 default('imfit')
01410 return imfit(imagename=outname)
01411
01412 for i in [0 ,1]:
01413 if (i == 0):
01414 code = run_fitcomponents
01415 method = test + "ia.fitcomponents: "
01416 else:
01417 code = run_imfit
01418 method += test + "imfit: "
01419 self._check_results(code())
01420
01421 def suite():
01422 return [imfit_test]