Commits

Sandra M Castro authored 65cbdf0d95f
Removed casa5 suite() function and fixed the headers of the documentation.
No tags

casatools/tests/tools/image/test_ia_collapse.py

Deleted
1 -#############################################
2 -# test_ia_collapse.py
3 -#
4 -# Copyright (C) 2008, 2009
5 -# Associated Universities, Inc. Washington DC, USA.
6 -#
7 -# This script is free software; you can redistribute it and/or modify it
8 -# under the terms of the GNU Library General Public License as published by
9 -# the Free Software Foundation; either version 2 of the License, or (at your
10 -# option) any later version.
11 -#
12 -# This library is distributed in the hope that it will be useful, but WITHOUT
13 -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 -# License for more details.
16 -#
17 -# You should have received a copy of the GNU Library General Public License
18 -# along with this library; if not, write to the Free Software Foundation,
19 -# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20 -#
21 -# Correspondence concerning AIPS++ should be adressed as follows:
22 -# Internet email: aips2-request@nrao.edu.
23 -# Postal address: AIPS++ Project Office
24 -# National Radio Astronomy Observatory
25 -# 520 Edgemont Road
26 -# Charlottesville, VA 22903-2475 USA
27 -#
28 -# <author>
29 -# Dave Mehringer
30 -# </author>
31 -#
32 -# <summary>
33 -# Test suite for the CASA tool method ia.collapse()
34 -# </summary>
35 -#
36 -# <reviewed reviwer="" date="" tests="" demos="">
37 -# </reviewed
38 -#
39 -# <prerequisite>
40 -# <ul>
41 -# <li> <linkto class="task_imcollapse.py:description">imcollapse</linkto>
42 -# </ul>
43 -# </prerequisite>
44 -#
45 -# <etymology>
46 -# Test for the imcollapse task
47 -# </etymology>
48 -#
49 -# <synopsis>
50 -# Test the imcollapse task and the ia.collapse() method upon which it is built.
51 -# </synopsis>
52 -#
53 -# <motivation>
54 -# To provide a test standard for the imcollapse task to ensure
55 -# coding changes do not break the associated bits
56 -# </motivation>
57 -#
58 -
59 -###########################################################################
60 -import shutil
61 -import unittest
62 -import numpy
63 -import os
64 -from math import sqrt
65 -
66 -from casatools import image as iatool
67 -from casatools import regionmanager
68 -from casatools import table
69 -from casatools import quanta
70 -from casatools import ctsys
71 -ctsys_resolve = ctsys.resolve
72 -
73 -
74 -good_image = "collapse_in.fits"
75 -masked_image = "im_w_mask.im"
76 -datapath = ctsys_resolve('unittest/ia_collapse/')
77 -
78 -def run_collapse(
79 - imagename, function, axes, outfile, region, box, chans,
80 - stokes, mask, overwrite, stretch=False
81 -):
82 - myia = iatool()
83 - myia.open(imagename)
84 - res = myia.collapse(
85 - function=function, axes=axes, outfile=outfile,
86 - region=region, box=box, chans=chans, stokes=stokes,
87 - mask=mask, overwrite=overwrite, stretch=stretch
88 - )
89 - myia.done()
90 - return res
91 -
92 -class ia_collapse_test(unittest.TestCase):
93 -
94 - def setUp(self):
95 - self.rg = regionmanager( )
96 - self.qa = quanta( )
97 -# shutil.copy(ctsys.resolve(datapath + good_image), good_image)
98 - shutil.copy(datapath + good_image, good_image)
99 - self.tabular_spectral_image = datapath + "longZax"
100 -
101 - def tearDown(self):
102 - self.qa.done( )
103 - self.rg.done( )
104 - os.remove(good_image)
105 - data = ["collapse_avg_0_1.fits","collapse_avg_0.fits",
106 - "collapse_avg_2.fits","collapse_in.im",
107 - "collapse_sum_1.fits","flux_test.im","median.im",
108 - "test_1_0.im","test_1_right.im","test_1_r.im",
109 - "test_2_2.im","test_2_f.im","test_2_freq.im",
110 - "test_3.im","test_8_0mean","test_8_0median","test_8_1mean",
111 - "test_8_1median","test_8_2mean","test_8_2median","ymask"
112 - ]
113 - for f in data:
114 - if os.path.exists(f):
115 - if os.path.isfile(f) or os.path.islink(f):
116 - os.unlink(f)
117 - else:
118 - shutil.rmtree(f)
119 -
120 -
121 - tb = table( )
122 - self.assertTrue(len(tb.showcache()) == 0)
123 - tb.done()
124 -
125 - def checkImage(self, gotImage, expectedName):
126 - expected = iatool()
127 - expected.open(expectedName)
128 - got = iatool()
129 - if type(gotImage) == str:
130 - got.open(gotImage)
131 - else:
132 - got = gotImage
133 - self.assertTrue(all(got.shape() == expected.shape()))
134 - diffData = got.getchunk() - expected.getchunk()
135 - self.assertTrue(abs(diffData).max() == 0)
136 - gotCsys = got.coordsys()
137 - expectedCsys = expected.coordsys()
138 - diffPixels = gotCsys.referencepixel()['numeric'] - expectedCsys.referencepixel()['numeric']
139 - self.assertTrue(abs(diffPixels).max() == 0)
140 - fracDiffRef = (
141 - gotCsys.referencevalue()['numeric'] - expectedCsys.referencevalue()['numeric']
142 - )/expectedCsys.referencevalue()['numeric'];
143 - self.assertTrue(abs(fracDiffRef).max() <= 1.5e-6)
144 - beam = got.restoringbeam()
145 - self.assertTrue(len(beam) == 3)
146 - self.assertTrue(abs(beam["major"]["value"] - 1) < 1.5e-6)
147 - self.assertTrue(abs(beam["minor"]["value"] - 1) < 1.5e-6)
148 - self.assertTrue(abs(beam["positionangle"]["value"] - 40) < 1.5e-6)
149 - got.close()
150 - got.done()
151 - expected.close()
152 - expected.done()
153 -
154 - def test_exceptions(self):
155 - """ia.collapse: Test various exception cases"""
156 -
157 - bogus = "mybogus.im"
158 - def testit(
159 - imagename, function, axes, outfile, region,
160 - box, chans, stokes, mask, overwrite, wantreturn
161 - ):
162 - self.assertRaises(
163 - Exception, run_collapse, imagename,
164 - function, axes, outfile, region, box,
165 - chans, stokes, mask, overwrite
166 - )
167 -
168 - # bad image name given
169 - testit(bogus, "mean", 0, "", "", "", "", "", "", False, True)
170 - # no function given
171 - testit(good_image, "", 0, "", "", "", "", "", "", False, True)
172 - # bogus function given
173 - testit(good_image, "bogus function", 0, "", "", "", "", "", "", False, True)
174 - # bogus region given
175 - testit(good_image, "mean", 0, "", "bogus_region", "", "", "", "", False, True)
176 - #bogus box
177 - testit(good_image, "mean", 0, "", "", "abc", "", "", "", False, True)
178 - # another bogus box
179 - testit(good_image, "mean", 0, "", "", "0,0,1000,1000", "", "", "", False, True)
180 - # no axes
181 - testit(good_image, "mean", "", "", "", "", "", "", "", False, True)
182 - # bogus axes
183 - testit(good_image, "mean", 10, "", "", "", "", "", "", False, True)
184 -
185 - def test_1(self):
186 - """ia.collapse(): average full image collapse along axis 0"""
187 - expected = "collapse_avg_0.fits"
188 - shutil.copy(datapath + expected, expected)
189 - for axis in (0 ,"r", "right"):
190 - outname = "test_1_" + str(axis) + ".im"
191 - mytool = run_collapse(
192 - good_image, "mean", axis, outname, "", "",
193 - "", "", "", False
194 - )
195 - self.assertTrue(isinstance(mytool,iatool))
196 - self.checkImage(mytool, expected)
197 - self.checkImage(outname, expected)
198 -
199 - def test_2(self):
200 - """ia.collapse(): average full image collapse along axis 2"""
201 - expected = "collapse_avg_2.fits"
202 - shutil.copy(datapath + expected, expected)
203 - for axis in (2, "f", "freq"):
204 - outname = "test_2_" + str(axis) + ".im"
205 - mytool = run_collapse(
206 - good_image, "mean", axis, outname, "", "",
207 - "", "", "", False
208 - )
209 - self.assertTrue(isinstance(mytool,iatool))
210 - self.checkImage(mytool, expected)
211 - self.checkImage(outname, expected)
212 -
213 - def test_3(self):
214 - """ia.collapse(): average full image collapse along axis 2 and check output overwritability"""
215 - expected = "collapse_sum_1.fits"
216 - shutil.copy(datapath + expected, expected)
217 - box = "1,1,2,2"
218 - chans = "1~2"
219 - stokes = "qu"
220 - outname = "test_3.im"
221 - mytool = run_collapse(
222 - good_image, "sum", 1, outname, "", box,
223 - chans, stokes, "", False
224 - )
225 - # this should throw an exception because we are trying to overwrite a file
226 - # that is open in the table cache
227 - self.assertRaises(
228 - Exception, run_collapse, good_image, "sum", 1, outname, "", box,
229 - chans, stokes, "", True
230 - )
231 - mytool.done()
232 - # now the image is closed, so check that can overwrite previous output. Then check output image
233 - mytool = run_collapse(
234 - good_image, "sum", 1, outname, "", box,
235 - chans, stokes, "", True
236 - )
237 - self.assertTrue(isinstance(mytool,iatool))
238 - self.checkImage(mytool, expected)
239 - self.checkImage(outname, expected)
240 - mytool.done()
241 -
242 - def test_4(self):
243 - """ia.collapse(): not specifying an output image is ok"""
244 - expected = "collapse_avg_2.fits"
245 - shutil.copy(datapath + expected, expected)
246 - mytool = run_collapse(
247 - good_image, "mean", 2, "", "", "",
248 - "", "", "", False
249 - )
250 - self.assertTrue(isinstance(mytool,iatool))
251 - self.checkImage(mytool, expected)
252 -
253 - def test_6(self):
254 - """ia.collapse(): memory only images can be collapsed"""
255 - mytool = run_collapse(
256 - good_image, "mean", 2, "", "", "",
257 - "", "", "", False
258 - )
259 - mytool2 = mytool.collapse("mean", 3)
260 - expected = [3, 3, 1, 1]
261 - self.assertTrue(all(mytool2.shape() == expected))
262 -
263 - def test_7(self):
264 - """ia.collapse(): verify collapsing along multiple axes works"""
265 - expected = "collapse_avg_0_1.fits"
266 - shutil.copy(datapath + expected, expected)
267 - for axes in ([0, 1], ["r", "d"], ["right", "dec"]):
268 - mytool = run_collapse(
269 - good_image, "mean", axes, "", "", "",
270 - "", "", "", False
271 - )
272 - self.assertTrue(isinstance(mytool,iatool))
273 - self.checkImage(mytool, expected)
274 -
275 - def test_8(self):
276 - """ia.collapse(): test both OTF and permanent masking works"""
277 - xx = iatool()
278 - good_image_im = "collapse_in.im"
279 - xx.fromfits(good_image_im, good_image)
280 - xx.calcmask(good_image_im + "<78")
281 - xx.close()
282 - xx.done()
283 - mytool = False
284 - axes = 3
285 - for j in [0, 1, 2]:
286 - mask = good_image_im + ">7"
287 - if j == 0:
288 - xx.open(good_image_im)
289 - xx.maskhandler("set", "")
290 - xx.close()
291 - xx.done()
292 - if j == 1:
293 - mask = ""
294 - xx.open(good_image_im)
295 - xx.maskhandler("set", "mask0")
296 - xx.close()
297 - xx.done()
298 - for func in ["mean", "median"]:
299 - for outfile in ["", "test_8_" + str(j) + func]:
300 - mytool = run_collapse(
301 - good_image_im, func, axes, outfile, "", "",
302 - "", "", mask, False
303 - )
304 - self.assertTrue(isinstance(mytool,iatool))
305 - npts = mytool.statistics()["npts"]
306 - mytool.close()
307 - mytool.done()
308 - if (j == 0):
309 - self.assertTrue(npts == 25)
310 - elif (j == 1):
311 - self.assertTrue(npts == 26)
312 - else:
313 - self.assertTrue(npts == 24)
314 -
315 - def test_median(self):
316 - """Test median when collapsing along multiple axes"""
317 - myia = iatool()
318 - imagename = "median.im"
319 - myia.fromshape(imagename, [3, 3, 3])
320 - bb = myia.getchunk()
321 - count = 0
322 - for i in range(3):
323 - for j in range(3):
324 - for k in range(3):
325 - bb[i, j, k] = count
326 - count += 1
327 - myia.putchunk(bb)
328 - collapsed = myia.collapse(axes=[0, 1], function="median")
329 - bb = collapsed.getchunk()
330 - self.assertTrue(bb[0, 0, 0] == 12)
331 - self.assertTrue(bb[0, 0, 1] == 13)
332 - self.assertTrue(bb[0, 0, 2] == 14)
333 - collapsed.done()
334 -
335 - collapsed = myia.collapse(
336 - axes=[0, 1], function="median",
337 - mask=imagename + "<14 || " + imagename + ">16"
338 - )
339 - bb = collapsed.getchunk()
340 - self.assertTrue(bb[0, 0, 0] == 10.5)
341 - self.assertTrue(bb[0, 0, 1] == 11.5)
342 - self.assertTrue(bb[0, 0, 2] == 14)
343 - collapsed.done()
344 -
345 - myia.fromshape("", [20, 20, 5])
346 - reg = self.rg.fromtext(
347 - "circle [[10pix, 10pix], 5pix]", csys=myia.coordsys().torecord(),
348 - shape=myia.shape()
349 - )
350 - collapsed = myia.collapse(axes=[0, 1], function="median", region=reg)
351 - myia.done()
352 - collapsed.done()
353 -
354 - def test_CAS_3418(self):
355 - """ia.collapse(): Test separate code for median due to performance issues"""
356 - for i in range(0,4):
357 - xx = iatool()
358 - xx.open(good_image)
359 - exp = xx.statistics(robust=True, axes=i)["median"]
360 - xx.done()
361 - mytool = run_collapse(
362 - good_image, "median", i, "", "", "",
363 - "", "", "", False
364 - )
365 - zz = mytool.subimage("", dropdeg=True)
366 - got = zz.getchunk()
367 - self.assertTrue((got == exp).all())
368 - mytool.done()
369 - zz.done()
370 -
371 - def test_region(self):
372 - """ ia.collapse(): Test region"""
373 - myia = iatool()
374 - myia.fromshape("", [10, 10, 10])
375 - bb = myia.getchunk()
376 - for i in range(10):
377 - bb[i,5,:] = i
378 - bb[i,0:5,:] = i+1
379 - bb[i,6:10,:] = i+2
380 - myia.putchunk(bb)
381 - res = myia.collapse("mean", 1, box="0,4,9,6")
382 - expec = myia.makearray(0, [10, 1, 10])
383 - for i in range(10):
384 - expec[i, 0, :] = i+1
385 - got = res.getchunk()
386 - self.assertTrue((expec == got).all())
387 -
388 - def test_stretch(self):
389 - """ ia.collapse(): Test stretch parameter"""
390 - yy = iatool()
391 - yy.open(good_image)
392 - mycs = yy.coordsys().torecord()
393 - yy.done()
394 - maskim = "ymask"
395 - yy.fromshape(maskim,[3,3,1,1])
396 - bb = yy.getchunk()
397 - bb = bb + 1
398 - bb[1,1] = -1
399 - yy.putchunk(bb)
400 - yy.setcoordsys(mycs)
401 - yy.done()
402 - yy = run_collapse(
403 - good_image, "mean", 0, "", "", "", "",
404 - "", maskim + ">0", False, stretch=True
405 - )
406 - self.assertTrue(isinstance(yy,iatool))
407 - yy.done()
408 -
409 - def test_CAS3737(self):
410 - """ ia.collapse(): test tabular spectral axis has correct collapsed reference value """
411 - image = self.tabular_spectral_image
412 - for chans in ["2445~2555", "range=[2445pix,2555pix]"]:
413 - mytool = run_collapse(
414 - image, "mean", 2, "", "", "",
415 - chans, "", "", False
416 - )
417 - expected = 98318505973583.641
418 - got = mytool.toworld([0,0,0])["numeric"][2]
419 - mytool.done()
420 - frac = got/expected - 1
421 - self.assertTrue(frac < 1e-6 and frac > -1e-6)
422 -
423 - def test_beams(self):
424 - """test per plane beams"""
425 - myia = iatool()
426 - myia.fromshape("", [10, 10, 10, 4])
427 - myia.setrestoringbeam(
428 - major="4arcsec", minor="3arcsec",
429 - pa="20deg", channel=1, polarization=1
430 - )
431 - for i in range (myia.shape()[2]):
432 - for j in range(myia.shape()[3]):
433 - major = self.qa.quantity(4 + i + j, "arcsec")
434 - minor = self.qa.quantity(2 + i + 0.5*j, "arcsec")
435 - pa = self.qa.quantity(10*i + j, "deg")
436 - myia.setrestoringbeam(
437 - major=major, minor=minor, pa=pa,
438 - channel=i, polarization=j
439 - )
440 - reg = self.rg.box(blc=[1,1,1,1], trc=[2,2,2,2])
441 - collapsed = myia.collapse(function="mean", axes=2, outfile="", region=reg)
442 - beam = collapsed.restoringbeam()
443 - self.assertTrue(len(beam) == 3)
444 - self.assertTrue(beam["major"] == self.qa.quantity(6, "arcsec"))
445 - self.assertTrue(beam["minor"] == self.qa.quantity(3.5, "arcsec"))
446 - self.assertTrue(beam["positionangle"] == self.qa.quantity(11, "deg"))
447 - myia.done()
448 - collapsed.done()
449 -
450 - def test_complex(self):
451 - """Test support for complex valued images"""
452 - myia = iatool()
453 -
454 - myia.fromshape("", [2, 2, 2], type='c')
455 - bb = myia.getchunk()
456 - counter = 0
457 - for i in [0, 1]:
458 - for j in [0, 1]:
459 - for k in [0, 1]:
460 - bb[i, j, k] = counter*(1-1j)
461 - counter += 1
462 - myia.putchunk(bb)
463 - col = myia.collapse("min", [2])
464 - got = col.subimage(dropdeg=True).getchunk()
465 - exp = numpy.min(bb, 2)
466 - self.assertTrue((got == exp).all())
467 -
468 - col = myia.collapse("mean", [2])
469 - got = col.subimage(dropdeg=True).getchunk()
470 - exp = numpy.average(bb, 2)
471 - self.assertTrue((got == exp).all())
472 -
473 - myia.done()
474 - col.done()
475 -
476 - def test_flux(self):
477 - """Test flux function"""
478 - myia = iatool()
479 - imagename = "flux_test.im"
480 - myia.fromshape(imagename, [10, 10, 10])
481 - bb = myia.getchunk()
482 - bb[:] = 1
483 - bb[0,0,0] = 0
484 - myia.putchunk(bb)
485 - self.assertRaises(Exception, myia.collapse, axes=[0,1], function="flux")
486 - myia.setrestoringbeam(major="3arcmin", minor="3arcmin", pa="0deg")
487 - myia.setbrightnessunit("Jy/beam")
488 - col = myia.collapse(axes=[0,1], function="flux", mask=imagename + "> 0")
489 - self.assertTrue((col.shape() == [1, 1, 10]).all())
490 - bb = col.getchunk()
491 - for i in range(10):
492 - if i == 0:
493 - self.assertTrue(abs(bb[0,0,i] - 9.707966) < 1e-5)
494 - else:
495 - self.assertTrue(abs(bb[0,0,i] - 9.806027) < 1e-5)
496 - col.done()
497 - myia.done()
498 -
499 - def test_sqrtsum(self):
500 - """Test sqrtsum function"""
501 - myia = iatool()
502 - myia.fromshape("",[2,2,2])
503 - bb = myia.getchunk()
504 - bb[:, :, 0] = 1
505 - bb[:, :, 1] = 2
506 - myia.putchunk(bb)
507 - zz = myia.collapse(axes=[0,1], function="sqrtsum")
508 - bb = zz.getchunk()
509 - self.assertTrue(bb[0, 0, 0] == 2)
510 - self.assertTrue(abs(bb[0, 0, 1] - 2*sqrt(2)) < 1e-6)
511 - bb = myia.getchunk()
512 - bb[:, :, 0] = -1
513 - myia.putchunk(bb)
514 - zz = myia.collapse(axes=[0,1], function="sqrtsum")
515 - bb = zz.getchunk()
516 - self.assertTrue(bb[0, 0, 0] == 0)
517 - self.assertTrue(abs(bb[0, 0, 1] - 2*sqrt(2)) < 1e-6)
518 -
519 - def test_sqrtsum_npix(self):
520 - """Test sqrtsum function"""
521 - myia = iatool()
522 - myia.fromshape("",[2,2,2])
523 - bb = myia.getchunk()
524 - bb[:, :, 0] = 1
525 - bb[:, :, 1] = 2
526 - myia.putchunk(bb)
527 - zz = myia.collapse(axes=[0,1], function="sqrtsum_npix")
528 - bb = zz.getchunk()
529 - self.assertTrue(bb[0, 0, 0] == 0.5)
530 - self.assertTrue(abs(bb[0, 0, 1] - 0.5*sqrt(2)) < 1e-6)
531 - bb = myia.getchunk()
532 - bb[:, :, 0] = -1
533 - myia.putchunk(bb)
534 - zz = myia.collapse(axes=[0,1], function="sqrtsum_npix")
535 - bb = zz.getchunk()
536 - self.assertTrue(bb[0, 0, 0] == 0)
537 - self.assertTrue(abs(bb[0, 0, 1] - 0.5*sqrt(2)) < 1e-6)
538 -
539 - def test_sqrtsum_npix_beam(self):
540 - """Test sqrtsum function"""
541 - myia = iatool()
542 - myia.fromshape("",[2,2,2])
543 - myia.setrestoringbeam(major="3arcmin", minor="3arcmin", pa="0deg")
544 - bb = myia.getchunk()
545 - bb[:, :, 0] = 1
546 - bb[:, :, 1] = 2
547 - myia.putchunk(bb)
548 - zz = myia.collapse(axes=[0,1], function="sqrtsum_npix_beam")
549 - bb = zz.getchunk()
550 - self.assertTrue(abs(bb[0, 0, 0] - 0.19612053) < 1e-6)
551 - self.assertTrue(abs(bb[0, 0, 1] - 0.27735632) < 1e-6)
552 - bb = myia.getchunk()
553 - bb[:, :, 0] = -1
554 - myia.putchunk(bb)
555 - zz = myia.collapse(axes=[0,1], function="sqrtsum_npix_beam")
556 - bb = zz.getchunk()
557 - self.assertTrue(bb[0, 0, 0] == 0)
558 - self.assertTrue(abs(bb[0, 0, 1] - 0.27735632) < 1e-6)
559 -
560 - def test_history(self):
561 - """Test history record is written"""
562 - myia = iatool()
563 - myia.fromshape("",[20,20,20])
564 - bb = myia.collapse(function="mean", axes=2)
565 - myia.done()
566 - msgs = bb.history()
567 - bb.done()
568 - self.assertTrue("ia.collapse" in msgs[-1])
569 -
570 -if __name__ == '__main__':
571 - unittest.main()

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut