from casatasks.private.casa_transition import is_CASA6
from casatasks import plotprofilemap
from casatasks import exportfits
from casatools import image
from casatools import regionmanager
from casatools import ctsys
datapath = ctsys.resolve('regression/unittest/imregrid/')
from testhelper import copytree_ignore_subversion
# default isn't used in CASA6
from __main__ import default
from tasks import protprofilemap
from tasks import exportfits
from taskinit import iatool as image
from taskinit import rgtool as regionmanager
# Data path of input/output
datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/imregrid/'
from testutils import copytree_ignore_subversion
from tests.testutils import copytree_ignore_subversion
class plotprofilemap_test(unittest.TestCase):
This is a test suite for plotprofilemap task.
test_image_not_exist: input image does not exist (causes error)
test_not_overwrite: output image already exists (causes error)
test_pol_not_out_of_range: pol index is out of range (causes error)
test_plotmasked_invalid: unsupported plotmasked value (causes error)
test_numpanel_5x5: standard test (5x5 panels)
test_numpanel_10x10: standard test (10x10 panels)
test_plotmasked_empty: plotmasked is empty
test_plotmasked_zero: plotmasked is zero
test_plotmasked_text: plotmasked is text
test_plotmasked_plot: plotmasked is plot
test_plotmasked_none: plotmasked is none
test_export_image: test export the plot to PNG file
test_fits_image: input image is FITS cube
test_title: put title to the plot
imagename_ref = 'expected.im'
prefix = 'plotprofilemap_test'
imagename = prefix + '.im'
fitsimage = prefix + '.fits'
figfile = prefix + '.png'
standard_task_param = {'imagename': imagename,
# turn off interactive mode
self.is_interactive = matplotlib.is_interactive()
copytree_ignore_subversion(datapath, self.imagename_ref, self.imagename)
# make parameters default
# copy standard task parameter set
self.task_param = self.standard_task_param.copy()
self.assertTrue(os.path.exists(self.imagename))
self.assertFalse(matplotlib.is_interactive())
for f in [self.fitsimage, self.figfile]:
if os.path.exists(self.imagename):
shutil.rmtree(self.imagename)
def make_mask(self, imagename):
make mask so that there is at least one blank panel when
profile map with 5x5 panels is created.
imageshape = myia.shape()
nx = imageshape[0] // 5 - 1
ny = imageshape[1] // 5 - 1
print('masked region: blc=[0,0,0], trc=[{0},{1},{2}]'.format(
region = myrg.box(blc=[0, 0, 0], trc=[nx, ny, ns])
myia.set(pixelmask=False, region=region)
msk = myia.getchunk(getmask=True)
self.assertTrue(numpy.all(msk[:nx, :ny, :] == False))
def run_task(self, **kwargs):
self.task_param.update(kwargs)
res = plotprofilemap(**self.task_param)
def _verify_axes_for_text(self, a, text=None):
self.assertFalse(a.axison)
self.assertEqual(len(lines), 0)
self.assertEqual(len(texts), 1)
self.assertEqual(texts[0].get_text(), text)
def _verify_plotmasked(self, a, plotmasked):
if plotmasked == 'empty':
self.assertEqual(len(lines), 0)
self.assertEqual(len(texts), 0)
elif plotmasked == 'zero':
self.assertEqual(len(lines), 1)
self.assertEqual(len(texts), 0)
xdata, ydata = lines[0].get_data()
self.assertTrue(numpy.all(ydata == 0.0))
elif plotmasked == 'none':
self.assertEqual(len(lines), 0)
self.assertEqual(len(texts), 0)
elif plotmasked == 'text':
self.assertEqual(len(lines), 0)
self.assertEqual(len(texts), 1)
text = texts[0].get_text()
self.assertEqual(text, 'NO DATA')
elif plotmasked == 'plot':
# plot data with different color
self.assertEqual(len(lines), 1)
self.assertEqual(len(texts), 0)
xdata, ydata = lines[0].get_data()
self.assertFalse(numpy.all(ydata == 0.0))
self.fail('Invalid plotmasked value {0}'.format(plotmasked))
def verify(self, numpanels, plotmasked=None, title=None):
# get list of axes object
# expected number of axes are
# nx * ny (number of plot panels)
# + (nx + ny) (number of position labels)
# + 2 (number of position titles)
expected_num_panels = nx * ny + nx + ny + 2
# if title is specified, the task adds additional axes for title
self.assertEqual(len(alist), expected_num_panels)
# 0~nx-1: axes for horizontal axis label (right to left)
# nx~nx+ny-1: axes for vertical axis label (bottom to top)
# nx+ny: axes for horizontal axis title
# nx+ny+1: axes for vertical axis title
# nx+ny+2: axes for plot title (if title is specified)
# subsequent: axes for plots (bottom right -> top right
# -> bottom of next column -> top of next column
# -> ... -> bottom left -> top left)
# NOTE: (ny * (nx - 1) + 1)-th panel is empty
# axes for horizontal axis label
# - axison should be False
self._verify_axes_for_text(alist[index])
# axes for vertical axis label
# - axison should be False
self._verify_axes_for_text(alist[index])
# axes for horizontal axis title
# - axison should be False
# - text should be 'Right Ascension (J2000)'
text = 'Right Ascension (J2000)'
self._verify_axes_for_text(alist[index], text=text)
# axes for vertical axis title
# - axison should be False
# - text should be 'Declination (J2000)'
text = 'Declination (J2000)'
self._verify_axes_for_text(alist[index], text=text)
# axes for vertical axis title
# - axison should be False
self._verify_axes_for_text(alist[index], text=title)
# - axison should be True
# - one line (if not empty)
# - no text (if not empty)
empty_panel = ny * (nx - 1)
#print 'verify axes {0}'.format(index)
if plotmasked is None or i != empty_panel:
self.assertTrue(a.axison)
self.assertEqual(len(lines), 1)
self.assertEqual(len(texts), 0)
print('verifying plotmasked parameter: axes {0} plotmasked {1}'.format(index, plotmasked))
# verify plotmasked parameter
self._verify_plotmasked(a, plotmasked)
def verify_figfile(self, figfile):
self.assertTrue(os.path.exists(figfile))
# figfile must be PNG format
self.assertEqual(imghdr.what(figfile), 'png')
def skip_if_darwin(self):
self.skipTest('Skip test_export_image on OS X since it may cause segfault')
def test_image_not_exist(self):
"""test_image_not_exist: input image does not exist (causes error)"""
with self.assertRaises(AssertionError) as cm:
res = self.run_task(imagename=imagename)
def test_not_overwrite(self):
"""test_not_overwrite: output image already exists (causes error)"""
os.system('echo "" > {0}'.format(self.figfile))
with self.assertRaises(RuntimeError) as cm:
res = self.run_task(figfile=figfile, overwrite=False)
the_exception = cm.exception
#print 'Exception reported: "{0}"'.format(str(the_exception))
self.assertTrue(re.match('^overwrite is False and output file exists:', str(the_exception)) is not None)
def test_pol_not_out_of_range(self):
"""test_pol_not_out_of_range: pol index is out of range (causes error)"""
# pol index is out of range
with self.assertRaises(RuntimeError) as cm:
res = self.run_task(pol=1)
the_exception = cm.exception
#print 'Exception reported: "{0}"'.format(str(the_exception))
self.assertTrue(re.match('^pol {0} is out of range'.format(pol), str(the_exception)) is not None)
def test_plotmasked_invalid(self):
"""test_plotmasked_invalid: unsupported plotmasked value (causes error)"""
# invalid plotmasked value
with self.assertRaises(AssertionError) as cm:
res = self.run_task(plotmasked=plotmasked)
def test_numpanel_5x5(self):
"""test_numpanel_5x5: standard test (5x5 panels)"""
res = self.run_task(numpanels=numpanels)
def test_numpanel_10x10(self):
"""test_numpanel_10x10: standard test (10x10 panels)"""
res = self.run_task(numpanels=numpanels)
def test_plotmasked_empty(self):
"""test_plotmasked_empty: plotmasked is empty"""
self.make_mask(self.imagename)
res = self.run_task(numpanels=numpanels, plotmasked=plotmasked)
self.verify(numpanels, plotmasked)
def test_plotmasked_zero(self):
"""test_plotmasked_zero: plotmasked is zero"""
self.make_mask(self.imagename)
res = self.run_task(numpanels=numpanels, plotmasked=plotmasked)
self.verify(numpanels, plotmasked)
def test_plotmasked_text(self):
"""test_plotmasked_text: plotmasked is text"""
self.make_mask(self.imagename)
res = self.run_task(numpanels=numpanels, plotmasked=plotmasked)
self.verify(numpanels, plotmasked)
def test_plotmasked_plot(self):
"""test_plotmasked_plot: plotmasked is plot"""
self.make_mask(self.imagename)
res = self.run_task(numpanels=numpanels, plotmasked=plotmasked)
self.verify(numpanels, plotmasked)
def test_plotmasked_none(self):
"""test_plotmasked_plot: plotmasked is none"""
self.make_mask(self.imagename)
res = self.run_task(numpanels=numpanels, plotmasked=plotmasked)
self.verify(numpanels, plotmasked)
def test_export_image(self):
"""test_export_image: test export the plot to PNG file"""
res = self.run_task(numpanels=numpanels, figfile=figfile)
self.verify_figfile(figfile)
def test_fits_image(self):
"""test_fits_image: input image is FITS cube"""
# convert input image to FITS
self.assertFalse(os.path.exists(self.fitsimage))
exportfits(imagename=self.imagename, fitsimage=self.fitsimage)
self.assertTrue(os.path.exists(self.fitsimage))
imagename = self.fitsimage
res = self.run_task(imagename=imagename, numpanels=numpanels)
"""test_title: put title to the plot"""
title = 'This is test image'
res = self.run_task(numpanels=numpanels, title=title)
self.verify(numpanels, title=title)
return [plotprofilemap_test]
if __name__ == '__main__':