casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_boxit.py
Go to the documentation of this file.
00001 ##########################################################################
00002 # imfit_test.py
00003 #
00004 # Copyright (C) 2008, 2009
00005 # Associated Universities, Inc. Washington DC, USA.
00006 #
00007 # This script is free software; you can redistribute it and/or modify it
00008 # under the terms of the GNU Library General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or (at your
00010 # option) any later version.
00011 #
00012 # This library is distributed in the hope that it will be useful, but WITHOUT
00013 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 # License for more details.
00016 #
00017 # You should have received a copy of the GNU Library General Public License
00018 # along with this library; if not, write to the Free Software Foundation,
00019 # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00020 #
00021 # Correspondence concerning AIPS++ should be adressed as follows:
00022 #        Internet email: aips2-request@nrao.edu.
00023 #        Postal address: AIPS++ Project Office
00024 #                        National Radio Astronomy Observatory
00025 #                        520 Edgemont Road
00026 #                        Charlottesville, VA 22903-2475 USA
00027 #
00028 # <author>
00029 # Dave Mehringer
00030 # </author>
00031 #
00032 # <summary>
00033 # Test suite for the CASA boxit Task
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 #   <li> <linkto class="boxit.py:description">imfit</linkto> 
00042 # </ul>
00043 # </prerequisite>
00044 #
00045 # <etymology>
00046 # boxit_test stands for boxit test
00047 # </etymology>
00048 #
00049 # <synopsis>
00050 # boxit_test.py is a Python script that tests the correctness
00051 # boxit task in CASA.
00052 # </synopsis> 
00053 #
00054 # <example>
00055 # # This test was designed to run in the automated CASA unit test system.
00056 # # This example shows who to run it manually outside casapy:
00057 # casapy -c os.environ["CASAPATH"].split()[0]+'/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_boxit
00058 #
00059 # inside casapy:
00060 # runUnitTest.main(['test_boxit'])
00061 #
00062 # </example>
00063 #
00064 # <motivation>
00065 # boxit needs a regression script. Since I didn't write boxit, I'm assuming
00066 # it works properly and am basing the tests on that assumption.
00067 # </motivation>
00068 #
00069 
00070 ###########################################################################
00071 import os
00072 import re
00073 import casac
00074 from tasks import *
00075 from taskinit import *
00076 import sha
00077 from __main__ import *
00078 import shutil
00079 import unittest
00080 
00081 imagename = 'R1046_boxit' 
00082 msgs = ''
00083 exp_basic_rgn = "boxit_expected_basic.rgn"
00084 exp_basic_mask = "boxit_expected_basic.mask"
00085 exp_minsize_rgn = "boxit_expected_minsize.rgn"
00086 exp_minsize_mask = "boxit_expected_minsize.mask"
00087 exp_diag_rgn = "boxit_expected_diag.rgn"
00088 exp_diag_mask = "boxit_expected_diag.mask"
00089 exp_boxstretch_rgn = "boxit_expected_boxstretch.rgn"
00090 exp_boxstretch_mask = "boxit_expected_boxstretch.mask"
00091 
00092 List=[imagename,exp_basic_rgn,exp_basic_mask,exp_minsize_rgn,exp_minsize_mask,
00093       exp_diag_rgn,exp_diag_mask,exp_boxstretch_rgn,exp_boxstretch_mask]
00094 
00095 def compare_region(expected_region_file, got_region_file):
00096     # sha1 test for now, not great since regions can be permuted, but works for now
00097     f = open(expected_region_file, 'r')
00098     expected_sha1 = sha.sha(f.read()).hexdigest()
00099     f.close()
00100     f = open(got_region_file, 'r')
00101     got_sha1 = sha.sha(f.read()).hexdigest()
00102     f.close()
00103     return expected_sha1 == got_sha1
00104 
00105 def compare_mask(expected_mask, got_mask, difference_image):
00106     if (not immath(imagename=[expected_mask, got_mask], mode='evalexpr', expr='IM0-IM1', outfile=difference_image)):
00107         return False
00108     if not ia.open(difference_image):
00109         return False
00110     pixels = ia.getchunk()
00111     ia.done()
00112     return not pixels.any() 
00113 
00114 
00115 class boxit_test(unittest.TestCase):
00116     
00117     def setUp(self):
00118         if(os.path.exists(List[0])):
00119             for file in List:
00120                 os.system('rm -rf ' +file)
00121                 
00122         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/boxit/'
00123         for file in List:
00124             os.system('cp -r ' +datapath+file +' ' +file)        
00125     
00126     def tearDown(self):
00127         for file in List:
00128             os.system('rm -rf ' +file)                              
00129 
00130     def test_basic(self):
00131         """Boxit: Basic test to ensure correct region file is being written"""
00132         success = True
00133         test = "Basic boxit test to ensure correct region file is being written"
00134         global msgs, imagename
00135         regionfile = 'boxit_basic.box'
00136         mask = 'boxit_basic.mask'
00137         boxit(imagename=imagename, threshold=.5, regionfile=regionfile, maskname=mask)
00138         if (not compare_region(exp_basic_rgn, regionfile)):
00139             success = False
00140             msgs += test + ": region file not correctly written" 
00141         if (not compare_mask(exp_basic_mask, mask, 'basic_mask_diff')):
00142             success = False
00143             msgs += test + ": mask  not correctly written"
00144         self.assertTrue(success,msgs)
00145         
00146     def test_minsize(self):
00147         """Boxit: Test of non-default minsize parameter"""
00148         success = True
00149         test = "Test of non-default minsize parameter"
00150         global msgs, imagename
00151         regionfile = "boxit_minsize.box"
00152         mask = "boxit_minsize.mask"
00153         boxit(imagename=imagename, threshold=.5, regionfile=regionfile, maskname=mask, minsize=10)
00154         if (not compare_region(exp_minsize_rgn, regionfile)):
00155             success = False
00156             msgs += test + ": did not write expected region file"
00157         if (not compare_mask(exp_minsize_mask, mask, 'minsize_mask_diff')):
00158             success = False
00159             msgs += test + ": mask  not correctly written"
00160         self.assertTrue(success,msgs)
00161         
00162     def test_diag(self):
00163         """Boxit: Test of non-default diag parameter"""
00164         success = True
00165         test = "Test of non-default diag parameter"
00166         global msgs, imagename
00167         regionfile = 'boxit_diag.box'
00168         mask = 'boxit_diag.mask'
00169         boxit(imagename=imagename, threshold=.5, regionfile=regionfile, maskname=mask, minsize=10, diag=True)
00170         if (not compare_region(exp_diag_rgn, regionfile)):
00171             success = False
00172             msgs += test + ": did not write expected region file"
00173         if (not compare_mask(exp_diag_mask, mask, 'diag_mask_diff')):
00174             success = False
00175             msgs += test + ": mask  not correctly written"
00176         self.assertTrue(success,msgs)
00177     
00178     def test_boxstretch(self):
00179         """Boxit: Test of non-default boxstretch parameter"""
00180         success = True
00181         test = "Test of non-default boxstretch parameter"
00182         global msgs, imagename
00183         regionfile = "boxit_boxtretch.box"
00184         mask = "boxit_boxtretch.mask"
00185         boxit(imagename=imagename, threshold=.5, regionfile=regionfile, maskname=mask, boxstretch=5)
00186         if (not compare_region(exp_boxstretch_rgn, regionfile)):
00187             success = False
00188             msgs += test + ": did not write expected region file"
00189         if (not compare_mask(exp_boxstretch_mask, mask, 'boxstretch_mask_diff')):
00190             success = False
00191             msgs += test + ": mask  not correctly written"
00192         self.assertTrue(success,msgs)
00193        
00194     def test_CAS_2059(self):
00195         '''Boxit: CAS-2059 confirm that imagename can contain dashes'''
00196         test = "CAS-2059: confirm imagename can contain dashes"
00197         success = True
00198         global msgs, imagename
00199         myimagename = "I+am-a*weird*name"
00200         shutil.copytree(imagename, myimagename)
00201         regionfile = 'boxit_basic_2.box'
00202         mask = 'boxit_basic_2.mask'
00203         boxit(imagename=myimagename, threshold=.5, regionfile=regionfile, maskname=mask)
00204         if (not compare_region(exp_basic_rgn, regionfile)):
00205             success = False
00206             msgs += test + ": region file not correctly written" 
00207         if (not compare_mask(exp_basic_mask, mask, 'basic_mask_diff_2')):
00208             success = False
00209             msgs += test + ": mask  not correctly written"
00210     
00211         self.assertTrue(success,msgs)
00212 
00213 
00214 def suite():
00215     return [boxit_test]