Commits
Ville Suoranta authored 4209b6b8e11 Merge
20 20 | # https://casa.nrao.edu/casadocs-devel/stable/global-task-list/task_imrebin/about |
21 21 | # |
22 22 | # |
23 23 | ########################################################################## |
24 24 | |
25 25 | CASA6 = False |
26 26 | try: |
27 27 | import casatools |
28 28 | from casatasks import imrebin, casalog |
29 29 | CASA6 = True |
30 + | _tb = casatools.table() |
30 31 | except ImportError: |
31 32 | from __main__ import default |
32 33 | from tasks import * |
33 34 | from taskinit import * |
34 35 | from casa_stack_manip import stack_frame_find |
35 36 | casa_stack_rethrow = stack_frame_find().get('__rethrow_casa_exceptions', False) |
36 37 | ia = iatool() |
37 38 | import sys |
38 39 | import os |
39 40 | import unittest |
97 98 | |
98 99 | def makeCompImage(): |
99 100 | |
100 101 | imagename = "gencomp.im" |
101 102 | putArr = np.array([[complex(j,2) for i in range(10)] for j in range(10)]) |
102 103 | ia.fromarray(outfile=imagename, pixels=putArr) |
103 104 | |
104 105 | ia.done() |
105 106 | |
106 107 | return imagename |
108 + | |
109 + | # function from merged test case |
110 + | |
111 + | def alleqnum(x,num,tolerance=0): |
112 + | if len(x.shape)==1: |
113 + | for i in range(x.shape[0]): |
114 + | if not (abs(x[i]-num) < tolerance): |
115 + | print("x[",i,"]=", x[i]) |
116 + | return False |
117 + | if len(x.shape)==2: |
118 + | for i in range(x.shape[0]): |
119 + | for j in range(x.shape[1]): |
120 + | if not (abs(x[i][j]-num) < tolerance): |
121 + | print("x[",i,"][",j,"]=", x[i][j]) |
122 + | return False |
123 + | if len(x.shape)==3: |
124 + | for i in range(x.shape[0]): |
125 + | for j in range(x.shape[1]): |
126 + | for k in range(x.shape[2]): |
127 + | if not (abs(x[i][j][k]-num) < tolerance): |
128 + | print("x[",i,"][",j,"][",k,"]=", x[i][j][k]) |
129 + | return False |
130 + | if len(x.shape)==4: |
131 + | for i in range(x.shape[0]): |
132 + | for j in range(x.shape[1]): |
133 + | for k in range(x.shape[2]): |
134 + | for l in range(x.shape[3]): |
135 + | if not (abs(x[i][j][k][l]-num) < tolerance): |
136 + | print("x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k]) |
137 + | return False |
138 + | if len(x.shape)>4: |
139 + | stop('unhandled array shape in alleq') |
140 + | return True |
107 141 | |
108 142 | useImage = 'gen.im' |
109 143 | useFloat = 'genfloat.im' |
110 144 | useComp = 'gencomp.im' |
111 145 | useDeg = 'gendeg.im' |
112 146 | |
113 147 | rebinned = 'rebinned.im' |
114 148 | rebinned2 = 'rebinned2.im' |
115 149 | |
116 150 | logpath = casalog.logfile() |
117 151 | testlog = 'testlog.log' |
152 + | |
153 + | teardownList = ['dkfajfas.im', 'dx.im', 'erzvd.im', 'gd.im', 'gk.im', 'kbesd.im', |
154 + | 'kjfasd.im', 'kyzb5.im', 'maskim', 'outxdkd.im', 'st.im', 'vcsfea.im', |
155 + | 'xxyy.im', 'aa.im', 'ab.im', 'zz.im', 'zz_out.im'] |
118 156 | |
119 157 | class imrebin_test(unittest.TestCase): |
120 158 | |
121 159 | |
122 160 | def setUpClass(cls): |
123 161 | |
124 162 | makeImage() |
125 163 | makeFloatImage() |
126 164 | makeCompImage() |
127 165 | makeDegImage() |
128 166 | |
129 167 | def setUp(self): |
130 168 | if not CASA6: |
169 + | self._myia = iatool() |
131 170 | default(imrebin) |
171 + | else: |
172 + | self._myia = casatools.image() |
132 173 | |
133 174 | def tearDown(self): |
175 + | self._myia.done() |
134 176 | casalog.setlogfile(logpath) |
135 177 | |
136 178 | if os.path.exists(testlog): |
137 179 | os.remove(testlog) |
138 180 | |
139 181 | if os.path.exists(rebinned): |
140 182 | shutil.rmtree(rebinned) |
141 183 | |
142 184 | if os.path.exists(rebinned2): |
143 185 | shutil.rmtree(rebinned2) |
144 186 | |
145 187 | |
146 188 | def tearDownClass(cls): |
147 189 | |
148 190 | shutil.rmtree(useImage) |
149 191 | shutil.rmtree(useFloat) |
150 192 | shutil.rmtree(useComp) |
151 193 | shutil.rmtree(useDeg) |
194 + | for item in teardownList: |
195 + | if os.path.exists(item): |
196 + | shutil.rmtree(item) |
152 197 | |
153 198 | |
154 199 | def test_newsize(self): |
155 200 | ''' |
156 201 | test_newsize |
157 202 | -------------- |
158 203 | |
159 204 | Check that the new image is downscaled in size by the appropriate factor |
160 205 | ''' |
161 206 | |
459 504 | if CASA6 or casa_stack_rethrow: |
460 505 | with self.assertRaises(RuntimeError): |
461 506 | imrebin(imagename=useImage, outfile=rebinned, factor=[2,2]) |
462 507 | else: |
463 508 | casalog.setlogfile(testlog) |
464 509 | imrebin(imagename=useImage, outfile=rebinned, factor=[2,2]) |
465 510 | self.assertTrue('SEVERE' in open(testlog).read()) |
466 511 | |
467 512 | imrebin(imagename=useImage, outfile=rebinned, factor=[2,2], overwrite=True) |
468 513 | self.assertTrue(os.path.exists(rebinned)) |
469 - | |
514 + | |
515 + | # ------ merged test cases ------ |
516 + | |
517 + | def test_stretch(self): |
518 + | """ ia.rebin(): Test stretch parameter""" |
519 + | yy = self._myia |
520 + | mymask = "maskim" |
521 + | yy.fromshape(mymask, [200, 200, 1, 1]) |
522 + | yy.addnoise() |
523 + | yy.done() |
524 + | shape = [200, 200, 1, 10] |
525 + | imagename = "aa.im" |
526 + | yy.fromshape(imagename, shape) |
527 + | yy.addnoise() |
528 + | yy.done() |
529 + | outfile = "ab.im" |
530 + | self.assertRaises( |
531 + | Exception, imrebin, imagename=imagename, outfile=outfile, factor=[2, 2, 1, 1], |
532 + | mask=mymask + ">0", stretch=False, overwrite=True |
533 + | ) |
534 + | imrebin( |
535 + | imagename=imagename, outfile=outfile, factor=[2, 2, 1, 1], |
536 + | mask=mymask + ">0", stretch=True, overwrite=True |
537 + | ) |
538 + | yy.open(outfile) |
539 + | self.assertTrue((yy.shape() == [100, 100, 1, 10]).all()) |
540 + | yy.done() |
541 + | |
542 + | def test_general(self): |
543 + | """ ia.rebin(): General tests""" |
544 + | # tests moved from imagetest_regression.py and modified |
545 + | |
546 + | myia = self._myia |
547 + | shp2 = [20, 40] |
548 + | d2 = myia.makearray(1.0, [shp2[0], shp2[1]]) |
549 + | # |
550 + | imagename = "st.im" |
551 + | myim2 = myia.newimagefromarray(outfile=imagename, pixels=d2) |
552 + | myim2.done() |
553 + | outfile = "gk.im" |
554 + | self.assertRaises( |
555 + | Exception, imrebin, imagename=imagename, outfile=outfile, |
556 + | factor=[-100, 2], overwrite=True |
557 + | ) |
558 + | imrebin( |
559 + | imagename=imagename, outfile=outfile, overwrite=True, |
560 + | factor=[2, 2] |
561 + | ) |
562 + | myia.open(outfile) |
563 + | p = myia.getchunk() |
564 + | self.assertTrue(alleqnum(p, 1.0, tolerance=0.0001)) |
565 + | myia.done() |
566 + | |
567 + | def test_multibeam(self): |
568 + | """Test multiple beams""" |
569 + | myia = self._myia |
570 + | imagename = "gd.im" |
571 + | myia.fromshape(imagename, [10, 10, 10]) |
572 + | myia.setrestoringbeam( |
573 + | major="4arcsec", minor="2arcsec", pa="0deg", |
574 + | channel=0, polarization=0 |
575 + | ) |
576 + | outfile = "dx.im" |
577 + | imrebin( |
578 + | imagename=imagename, outfile=outfile, |
579 + | factor=[2, 2, 1] |
580 + | ) |
581 + | |
582 + | self.assertRaises( |
583 + | Exception, imrebin, imagename=imagename, outfile=outfile, |
584 + | factor=[2, 2, 2] |
585 + | ) |
586 + | |
587 + | def test_crop(self): |
588 + | """Test crop parameter""" |
589 + | myia = self._myia |
590 + | imagename = "xxyy.im" |
591 + | myia.fromshape(imagename, [20, 20, 20]) |
592 + | factor = [3, 3, 3] |
593 + | myia.done() |
594 + | outfile = "outxdkd.im" |
595 + | imrebin(imagename=imagename, outfile=outfile, factor=factor, crop=True) |
596 + | myia.open(outfile) |
597 + | self.assertTrue((myia.shape() == [6, 6, 6]).all()) |
598 + | myia.done() |
599 + | imrebin(imagename=imagename, outfile=outfile, factor=factor, crop=False, overwrite=True) |
600 + | myia.open(outfile) |
601 + | self.assertTrue((myia.shape() == [7, 7, 7]).all()) |
602 + | myia.done() |
603 + | |
604 + | def test_dropdeg(self): |
605 + | """Test dropdeg parameter""" |
606 + | myia = self._myia |
607 + | imagename = "kjfasd.im" |
608 + | myia.fromshape(imagename, [20, 20, 1]) |
609 + | factor = [5, 5] |
610 + | myia.done() |
611 + | outfile = "dkfajfas.im" |
612 + | imrebin(imagename=imagename, outfile=outfile, factor=factor, dropdeg=True) |
613 + | myia.open(outfile) |
614 + | self.assertTrue((myia.shape() == [4, 4]).all()) |
615 + | myia.done() |
616 + | |
617 + | def test_box(self): |
618 + | """Test use of box""" |
619 + | myia = self._myia |
620 + | imagename = "erzvd.im" |
621 + | myia.fromshape(imagename, [30, 30, 1]) |
622 + | factor = [5, 5] |
623 + | myia.done() |
624 + | outfile = "vcsfea.im" |
625 + | imrebin(imagename=imagename, outfile=outfile, factor=factor, box="5,5,25,25", crop=True) |
626 + | myia.open(outfile) |
627 + | self.assertTrue((myia.shape() == [4, 4, 1]).all()) |
628 + | myia.done() |
629 + | |
630 + | def test_dropdeg2(self): |
631 + | """ axes that become degenerate when regridded are dropped if dropdeg=True: CAS-5836""" |
632 + | myia = self._myia |
633 + | imagename = "kbesd.im" |
634 + | myia.fromshape(imagename, [20, 20, 20]) |
635 + | factor = [1, 1, 20] |
636 + | myia.done() |
637 + | outfile = "kyzb5.im" |
638 + | imrebin( |
639 + | imagename=imagename, outfile=outfile, |
640 + | factor=factor, dropdeg=True |
641 + | ) |
642 + | myia.open(outfile) |
643 + | self.assertTrue((myia.shape() == [20, 20]).all()) |
644 + | myia.done() |
645 + | |
646 + | def test_history(self): |
647 + | """Test history writing""" |
648 + | myia = self._myia |
649 + | imagename = "zz.im" |
650 + | factor = [1, 1, 20] |
651 + | myia.fromshape(imagename, [20, 20, 20]) |
652 + | myia.done() |
653 + | outfile = "zz_out.im" |
654 + | imrebin(imagename=imagename, outfile=outfile, factor=factor) |
655 + | myia.open(outfile) |
656 + | msgs = myia.history() |
657 + | myia.done() |
658 + | teststr = "version" |
659 + | self.assertTrue(teststr in msgs[-2], "'" + teststr + "' not found") |
660 + | teststr = "imrebin" |
661 + | self.assertTrue(teststr in msgs[-1], "'" + teststr + "' not found") |
470 662 | |
471 663 | |
472 664 | |
473 665 | def suite(): |
474 666 | return[imrebin_test] |
475 667 | |
476 668 | if __name__ == '__main__': |
477 669 | unittest.main() |