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 import shutil
00070 import casac
00071 from tasks import *
00072 from taskinit import *
00073 from __main__ import *
00074 import unittest
00075
00076 image = "imregion.fits"
00077 image_nospec = "imregion_nospec.fits"
00078 image_dironly = "imregion_dironly.fits"
00079
00080 box1 = 1.24795026
00081 box2 = 0.782552901
00082 box3 = 1.24794616
00083 box4 = 0.782555814
00084 box5 = 1.24794206
00085 box6 = 0.782558727
00086 box7 = 1.24793797
00087 box8 = 0.782561641
00088 box9 = 1.2479338718038551
00089 box10 = 0.78256455381109313
00090 box11 = 1.2479297756405987
00091 box12 = 0.78256746696533663
00092 chan0 = 4.73510000e+09
00093 chan4 = 6.33510000e+09
00094 chan15 = 1.07351000e+10
00095 chan19 = 1.23351000e+10
00096
00097 def run_frombcs(imagename, box, chans, stokes, stokes_control, region=""):
00098 myia = iatool()
00099 myia.open(imagename)
00100 mycsys = myia.coordsys()
00101 myrg = rgtool()
00102 res = rg.frombcs(
00103 mycsys.torecord(), myia.shape(), box, chans,
00104 stokes, stokes_control, region
00105 )
00106 myia.close()
00107 del myia
00108 del myrg
00109 return res
00110
00111 def recToList(rec):
00112 mylist = []
00113 mykeys = rec.keys()
00114 mykeys.sort()
00115 for k in mykeys:
00116 mylist.append(rec[k]['value'])
00117 return mylist
00118
00119 class rg_frombcs_test(unittest.TestCase):
00120
00121 def setUp(self):
00122 datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/rg.frombcs/'
00123 for im in [image, image_nospec, image_dironly]:
00124 shutil.copy(datapath + im, im)
00125
00126 def tearDown(self):
00127 for im in [image, image_nospec, image_dironly]:
00128 os.remove(im)
00129
00130 def compLists(self, got, exp):
00131 epsilon = 1e-8
00132 print "got " + str(got)
00133 print "exp " + str(exp)
00134 for i in range(len(got)):
00135 fracDiff = abs((got[i]-exp[i])/exp[i]);
00136 self.assertTrue(fracDiff < epsilon)
00137
00138 def test_full_image(self):
00139 """Test default gives region of entire image"""
00140
00141 stokes = ""
00142 chans = ""
00143 stokes_control = "a"
00144 box = ""
00145 myreg = run_frombcs(
00146 image, box, chans, stokes, stokes_control
00147 )
00148 gotblc = recToList(myreg["blc"])
00149 expblc = [1.24795230, 0.782549990, chan0, 1.0]
00150 self.compLists(gotblc, expblc);
00151 gottrc = recToList(myreg["trc"])
00152 exptrc = [1.24791339, 0.782577665, chan19, 4.0]
00153 self.compLists(gottrc, exptrc)
00154
00155 def test_single_stokes(self):
00156 """Test setting a single stokes"""
00157
00158 stokes = "Q"
00159 chans = ""
00160 stokes_control = "a"
00161 box = ""
00162 myreg = run_frombcs(
00163 image, box, chans, stokes, stokes_control
00164 )
00165 gotblc = recToList(myreg["blc"])
00166 expblc = [1.24795230, 0.782549990, chan0, 2.0]
00167 self.compLists(gotblc, expblc);
00168 gottrc = recToList(myreg["trc"])
00169 exptrc = [1.24791339, 0.782577665, chan19, 2.0]
00170
00171 def test_continguous_stokes(self):
00172 """Test setting a contiguous stokes"""
00173
00174 box = ""
00175 chans = ""
00176 stokes = "QU"
00177 stokes_control = "a"
00178 myreg = run_frombcs(
00179 image, box, chans, stokes, stokes_control
00180 )
00181 gotblc = recToList(myreg["blc"])
00182 expblc = [1.24795230, 0.782549990, chan0, 2.0]
00183 self.compLists(gotblc, expblc);
00184 gottrc = recToList(myreg["trc"])
00185 exptrc = [1.24791339, 0.782577665, chan19, 3.0]
00186
00187 def test_single_channel(self):
00188 """Test setting a single channel"""
00189 box = ""
00190 chans = "4"
00191 stokes = ""
00192 stokes_control = "a"
00193 myreg = run_frombcs(
00194 image, box, chans, stokes, stokes_control
00195 )
00196 gotblc = recToList(myreg["blc"])
00197 expblc = [1.24795230, 0.782549990, chan4, 1.0]
00198 self.compLists(gotblc, expblc);
00199 gottrc = recToList(myreg["trc"])
00200 exptrc = [1.24791339, 0.782577665, chan4, 4.0]
00201
00202 def test_contiguous_channels(self):
00203 """Test setting multiple continuous channels"""
00204
00205 box = ""
00206 chans = "0~4"
00207 stokes = ""
00208 stokes_control = "a"
00209 myreg = run_frombcs(
00210 image, box, chans, stokes, stokes_control
00211 )
00212 gotblc = recToList(myreg["blc"])
00213 expblc = [1.24795230, 0.782549990, chan0, 1.0]
00214 self.compLists(gotblc, expblc);
00215 gottrc = recToList(myreg["trc"])
00216 exptrc = [1.24791339, 0.782577665, chan4, 4.0]
00217
00218 def test_single_box(self):
00219 """Test setting single box"""
00220
00221 box = "1,2,3,4"
00222 chans = ""
00223 stokes = ""
00224 stokes_control = "a"
00225 myreg = run_frombcs(
00226 image, box, chans, stokes, stokes_control
00227 )
00228 gotblc = recToList(myreg["blc"])
00229 expblc = [box1, box2, chan0, 1.0]
00230 self.compLists(gotblc, expblc);
00231 gottrc = recToList(myreg["trc"])
00232 exptrc = [box3, box4, chan19, 4.0]
00233
00234 def test_region_record(self):
00235 """Test setting region record"""
00236
00237 box = ""
00238 chans = ""
00239 stokes = ""
00240 stokes_control = "a"
00241 blahia = iatool()
00242 blahia.open(image)
00243 mycsys = blahia.coordsys()
00244 blahia.done()
00245 mybox = rg.wbox(
00246 ["1pix", "2pix", "0pix", "0pix"],
00247 ["3pix", "4pix", "19pix", "3pix"],
00248 csys=mycsys.torecord()
00249 )
00250 myreg = run_frombcs(
00251 image, box, chans, stokes, stokes_control, mybox
00252 )
00253 gotblc = recToList(myreg["blc"])
00254 for i in range(len(gotblc)):
00255 gotblc[i] = gotblc[i] - 1
00256 gotblc = mycsys.toworld(gotblc)['numeric']
00257 expblc = [box1, box2, chan0, 1.0]
00258 self.compLists(gotblc, expblc);
00259 gottrc = recToList(myreg["trc"])
00260 for i in range(len(gottrc)):
00261 gottrc[i] = gottrc[i] - 1
00262 gottrc = mycsys.toworld(gottrc)['numeric']
00263 exptrc = [box3, box4, chan19, 4.0]
00264 self.compLists(gottrc, exptrc);
00265
00266
00267 def test_first_stokes(self):
00268 """Test setting first stokes"""
00269
00270 box = ""
00271 chans = ""
00272 stokes = ""
00273 stokes_control = "f"
00274 myreg = run_frombcs(
00275 image, box, chans, stokes, stokes_control
00276 )
00277 gotblc = recToList(myreg["blc"])
00278 expblc = [1.24795230, 0.782549990, chan0, 1.0]
00279 self.compLists(gotblc, expblc);
00280 gottrc = recToList(myreg["trc"])
00281 exptrc = [1.24791339, 0.782577665, chan19, 1.0]
00282
00283
00284 def test_multiple_boxes(self):
00285 """Test setting multiple boxes"""
00286
00287 stokes = ""
00288 chans = ""
00289 stokesControl = "a"
00290 box = "1,2,3,4,5,6,7,8,9,10,11,12"
00291 stokes_control = "a"
00292 myreg = run_frombcs(
00293 image, box, chans, stokes, stokes_control
00294 )
00295
00296 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00297 expblc = [1.24793387, 0.782564554, chan0, 1.0]
00298 self.compLists(gotblc, expblc);
00299 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00300 exptrc = [1.24792978, 0.782567467, chan19, 4.0]
00301 self.compLists(gottrc, exptrc)
00302
00303 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["blc"])
00304 expblc = [box1, box2, chan0, 1.0]
00305 self.compLists(gotblc, expblc);
00306 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["trc"])
00307 exptrc = [box3, box4, chan19, 4.0]
00308 self.compLists(gottrc, exptrc);
00309
00310 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["blc"])
00311 expblc = [box5, box6, chan0, 1.0]
00312 self.compLists(gotblc, expblc);
00313 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["trc"])
00314 exptrc = [box7, box8, chan19, 4.0]
00315 self.compLists(gottrc, exptrc);
00316
00317 def test_set_multiple_stokes_ranges(self):
00318 """Test setting multiple stokes ranges"""
00319
00320 stokes = "IUV"
00321 chans = ""
00322 stokesControl = "a"
00323 box = ""
00324 stokes_control = "a"
00325 myreg = run_frombcs(
00326 image, box, chans, stokes, stokes_control
00327 )
00328
00329 gotblc = recToList(myreg["regions"]["*1"]["blc"])
00330 expblc = [1.24795230, 0.782549990, chan0, 1.0]
00331 self.compLists(gotblc, expblc);
00332 gottrc = recToList(myreg["regions"]["*1"]["trc"])
00333 exptrc = [1.24791339, 0.782577665, chan19, 1.0]
00334 self.compLists(gottrc, exptrc)
00335
00336 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00337 expblc = [1.24795230, 0.782549990, chan0, 3.0]
00338 self.compLists(gotblc, expblc);
00339 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00340 exptrc = [1.24791339, 0.782577665, chan19, 4.0]
00341 self.compLists(gottrc, exptrc)
00342
00343 def test_multiple_channel_ranges(self):
00344 """Test multiple channel ranges"""
00345
00346 stokes = ""
00347 chans = "<5,>=15"
00348 stokesControl = "a"
00349 box = ""
00350 stokes_control = "a"
00351 myreg = run_frombcs(
00352 image, box, chans, stokes, stokes_control
00353 )
00354
00355 gotblc = recToList(myreg["regions"]["*1"]["blc"])
00356 expblc = [1.24795230, 0.782549990, chan0, 1.0]
00357 self.compLists(gotblc, expblc);
00358 gottrc = recToList(myreg["regions"]["*1"]["trc"])
00359 exptrc = [1.24791339, 0.782577665, chan4, 4.0]
00360 self.compLists(gottrc, exptrc)
00361
00362 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00363 expblc = [1.24795230, 0.782549990, chan15, 1.0]
00364 self.compLists(gotblc, expblc);
00365 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00366 exptrc = [1.24791339, 0.782577665, chan19, 4.0]
00367 self.compLists(gottrc, exptrc)
00368
00369 def test_multiple_boxes_channel_ranges_stokes_ranges(self):
00370 """Test multiple channel ranges, multiple stokes ranges, and multiple boxes"""
00371
00372 stokes = "IQV"
00373 chans = "<5,>=15"
00374 stokesControl = "a"
00375 box = "1,2,3,4,5,6,7,8"
00376 stokes_control = "a"
00377 myreg = run_frombcs(
00378 image, box, chans, stokes, stokes_control
00379 )
00380
00381
00382 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00383 expblc = [box5, box6, chan15, 4.0]
00384 self.compLists(gotblc, expblc)
00385 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00386 exptrc = [box7, box8, chan19, 4.0]
00387 self.compLists(gottrc, exptrc)
00388
00389
00390 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["blc"])
00391 expblc = [box5, box6, chan0, 4.0]
00392 self.compLists(gotblc, expblc)
00393 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["trc"])
00394 exptrc = [box7, box8, chan4, 4.0]
00395 self.compLists(gottrc, exptrc)
00396
00397
00398 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["blc"])
00399 expblc = [box5, box6, chan15, 1.0]
00400 self.compLists(gotblc, expblc)
00401 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["trc"])
00402 exptrc = [box7, box8, chan19, 2.0]
00403 self.compLists(gottrc, exptrc)
00404
00405
00406 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["blc"])
00407 expblc = [box5, box6, chan0, 1.0]
00408 self.compLists(gotblc, expblc)
00409 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["trc"])
00410 exptrc = [box7, box8, chan4, 2.0]
00411 self.compLists(gottrc, exptrc)
00412
00413
00414 gotblc = recToList(
00415 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00416 ["regions"]["*2"]["blc"]
00417 )
00418 expblc = [box1, box2, chan15, 4.0]
00419 self.compLists(gotblc, expblc)
00420 gottrc = recToList(
00421 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00422 ["regions"]["*2"]["trc"]
00423 )
00424 exptrc = [box3, box4, chan19, 4.0]
00425 self.compLists(gottrc, exptrc)
00426
00427
00428 gotblc = recToList(
00429 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00430 ["regions"]["*1"]["regions"]["*2"]["blc"]
00431 )
00432 expblc = [box1, box2, chan0, 4.0]
00433 self.compLists(gotblc, expblc)
00434 gottrc = recToList(
00435 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00436 ["regions"]["*1"]["regions"]["*2"]["trc"]
00437 )
00438 exptrc = [box3, box4, chan4, 4.0]
00439 self.compLists(gottrc, exptrc)
00440
00441
00442 gotblc = recToList(
00443 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00444 ["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["blc"]
00445 )
00446 expblc = [box1, box2, chan15, 1.0]
00447 self.compLists(gotblc, expblc)
00448 gottrc = recToList(
00449 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00450 ["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["trc"]
00451 )
00452 exptrc = [box3, box4, chan19, 2.0]
00453 self.compLists(gottrc, exptrc)
00454
00455
00456 gotblc = recToList(
00457 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00458 ["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["blc"]
00459 )
00460 expblc = [box1, box2, chan0, 1.0]
00461 self.compLists(gotblc, expblc)
00462 gottrc = recToList(
00463 myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]
00464 ["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["trc"]
00465 )
00466 exptrc = [box3, box4, chan4, 2.0]
00467 self.compLists(gottrc, exptrc)
00468
00469 def test_multiple_boxes_multiple_stokes_no_spectral_axis(self):
00470 """Test multiple stokes ranges, and multiple boxes on image with no spectral axis"""
00471
00472 stokes = "IQV"
00473 chans = ""
00474 stokesControl = "a"
00475 box = "1,2,3,4,5,6,7,8"
00476 stokes_control = "a"
00477 myreg = run_frombcs(
00478 image_nospec, box, chans, stokes, stokes_control
00479 )
00480
00481
00482 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00483 expblc = [box5, box6, 4.0]
00484 self.compLists(gotblc, expblc)
00485 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00486 exptrc = [box7, box8, 4.0]
00487 self.compLists(gottrc, exptrc)
00488
00489
00490 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["blc"])
00491 expblc = [box5, box6, 1.0]
00492 self.compLists(gotblc, expblc)
00493 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["trc"])
00494 exptrc = [box7, box8, 2.0]
00495 self.compLists(gottrc, exptrc)
00496
00497
00498 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["blc"])
00499 expblc = [box1, box2, 4.0]
00500 self.compLists(gotblc, expblc)
00501 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*2"]["trc"])
00502 exptrc = [box3, box4, 4.0]
00503 self.compLists(gottrc, exptrc)
00504
00505
00506 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["blc"])
00507 expblc = [box1, box2, 1.0]
00508 self.compLists(gotblc, expblc)
00509 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["regions"]["*1"]["trc"])
00510 exptrc = [box3, box4, 2.0]
00511 self.compLists(gottrc, exptrc)
00512
00513 def test_multiple_boxes_image_with_direction_coordinate_only(self):
00514 """Test multiple boxes on image with direction coordinate only"""
00515
00516 stokes = ""
00517 chans = ""
00518 stokesControl = "a"
00519 box = "1,2,3,4,5,6,7,8"
00520 stokes_control = "a"
00521 myreg = run_frombcs(
00522 image_dironly, box, chans, stokes, stokes_control
00523 )
00524
00525
00526 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00527 expblc = [box5, box6]
00528 self.compLists(gotblc, expblc)
00529 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00530 exptrc = [box7, box8]
00531 self.compLists(gottrc, exptrc)
00532
00533
00534 gotblc = recToList(myreg["regions"]["*1"]["blc"])
00535 expblc = [box1, box2]
00536 self.compLists(gotblc, expblc)
00537 gottrc = recToList(myreg["regions"]["*1"]["trc"])
00538 exptrc = [box3, box4]
00539 self.compLists(gottrc, exptrc)
00540
00541 def test_region_text_string(self):
00542 """Test setting a region text string"""
00543
00544 region = "box[[1pix,2pix],[3pix,4pix]]\nbox[[5pix,6pix],[7pix,8pix]]\nbox[[9pix,10pix],[11pix,12pix]]"
00545 myreg = run_frombcs(
00546 image, "", "", "", "a", region
00547 )
00548
00549 gotblc = recToList(myreg["regions"]["*2"]["blc"])
00550 expblc = [box9, box10]
00551 self.compLists(gotblc, expblc);
00552 gottrc = recToList(myreg["regions"]["*2"]["trc"])
00553 exptrc = [box11, box12]
00554 self.compLists(gottrc, exptrc)
00555
00556 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["blc"])
00557 expblc = [box1, box2]
00558 self.compLists(gotblc, expblc);
00559 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*1"]["trc"])
00560 exptrc = [box3, box4]
00561 self.compLists(gottrc, exptrc);
00562
00563 gotblc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["blc"])
00564 expblc = [box5, box6]
00565 self.compLists(gotblc, expblc);
00566 gottrc = recToList(myreg["regions"]["*1"]["regions"]["*2"]["trc"])
00567 exptrc = [box7, box8]
00568 self.compLists(gottrc, exptrc);
00569
00570
00571 def suite():
00572 return [rg_frombcs_test]