casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_slsearch.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 task slsearch
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 #   <li> <linkto class="task_slsearch.py:description">slsearch</linkto> 
00042 # </ul>
00043 # </prerequisite>
00044 #
00045 # <etymology>
00046 # Test for the slsearch task
00047 # </etymology>
00048 #
00049 # <synopsis>
00050 # Test the slsearch task and the sl.search() method upon which it is built.
00051 # </synopsis> 
00052 #
00053 # <example>
00054 #
00055 # This test runs as part of the CASA python unit test suite and can be run from
00056 # the command line via eg
00057 # 
00058 # `echo $CASAPATH/bin/casapy | sed -e 's$ $/$'` --nologger --log2term -c `echo $CASAPATH | awk '{print $1}'`/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_slsearch[test1,test2,...]
00059 #
00060 # </example>
00061 #
00062 # <motivation>
00063 # To provide a test standard for the slsearch task to ensure
00064 # coding changes do not break the associated bits 
00065 # </motivation>
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 good_table = "biglist.tbl"
00077 
00078 def run_search(
00079     table, outfile, freqrange, species, reconly,
00080     chemnames, qns, intensity, smu2, loga, el,
00081     eu, rrlinclude, rrlonly, verbose, logfile,
00082     append
00083 ):
00084     mysl = sltool()
00085     if (not mysl.open(table)):
00086         raise Exception
00087     try: 
00088         restool = mysl.search(
00089             outfile=outfile, freqrange=freqrange,
00090             species=species, reconly=reconly,
00091             chemnames=chemnames, qns=qns,
00092             intensity=intensity, smu2=smu2, loga=loga,
00093             el=el, eu=eu, rrlinclude=rrlinclude,
00094             rrlonly=rrlonly, verbose=verbose, logfile=logfile,
00095             append=append
00096         )
00097         mysl.close()
00098         return restool
00099     except Exception, instance:
00100         mysl.close()
00101         raise instance
00102 
00103 def run_slsearch(
00104     table, outfile, freqrange, species, reconly,
00105     chemnames, qns, intensity, smu2, loga, el,
00106     eu, rrlinclude, rrlonly, verbose, logfile,
00107     append
00108 ):
00109     default(slsearch)
00110     return slsearch(
00111         tablename=table, outfile=outfile, freqrange=freqrange,
00112         species=species, reconly=reconly,
00113         chemnames=chemnames, qns=qns,
00114         intensity=intensity, smu2=smu2, loga=loga,
00115         el=el, eu=eu, rrlinclude=rrlinclude,
00116         rrlonly=rrlonly, verbose=verbose, logfile=logfile,
00117         append=append
00118     )
00119 
00120 class slsearch_test(unittest.TestCase):
00121     
00122     def _testit(
00123         self, table, outfile, freqrange, species, reconly,
00124         chemnames, qns, intensity, smu2, loga, el,
00125         eu, rrlinclude, rrlonly, verbose, logfile,
00126         append, nrows
00127     ):
00128         mysl = sltool()
00129         mytb = tbtool()
00130         for i in [0, 1]:
00131             if (i==0):
00132                 mysl = run_search(table, outfile,
00133                     freqrange, species, reconly, chemnames,
00134                     qns, intensity, smu2, loga, el, eu,
00135                     rrlinclude, rrlonly, verbose, logfile, 
00136                     append
00137                 )
00138             else:
00139                 mysl = run_slsearch(
00140                     table, outfile, freqrange, species,
00141                     reconly, chemnames, qns, intensity,
00142                     smu2, loga, el, eu, rrlinclude, rrlonly,
00143                     verbose, logfile, append
00144                 )
00145             self.assertEqual(nrows, mysl.nrows())
00146             mysl.close()
00147 
00148             if (outfile):
00149                 mytb.open(outfile)
00150                 self.assertEqual(nrows, mytb.nrows())
00151                 mytb.close()
00152                 shutil.rmtree(outfile)
00153     
00154     def setUp(self):
00155         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/slsearch/'
00156         shutil.copytree(datapath + good_table, good_table)
00157 
00158     def tearDown(self):
00159         shutil.rmtree(good_table)
00160 
00161     def test_exceptions(self):
00162         """slsearch: Test various exception cases"""
00163         def testit(
00164             table, outfile, freqrange, species, reconly,
00165             chemnames, qns, intensity, smu2, loga, el,
00166             eu, rrlinclude, rrlonly, verbose, logfile, 
00167             append
00168         ):
00169             for i in [0, 1]:
00170                 if (i==0):
00171                     self.assertRaises(
00172                         Exception, run_search, table, outfile,
00173                         freqrange, species, reconly, chemnames,
00174                         qns, intensity, smu2, loga, el, eu,
00175                         rrlinclude, rrlonly, verbose, logfile, 
00176                         append
00177                     )
00178                 else:
00179                     self.assertEqual(
00180                         run_slsearch(
00181                             table, outfile, freqrange, species,
00182                             reconly, chemnames, qns, intensity,
00183                             smu2, loga, el, eu, rrlinclude, rrlonly,
00184                             verbose, logfile, append
00185                         ), None
00186                     )
00187         # bogus input table name
00188         testit(
00189             table="fred.tbl", outfile="x", freqrange=[0, 100], species=[],
00190             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00191             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=True,
00192             verbose=True, logfile="", append=True
00193         )
00194         # bad output name
00195         testit(
00196             table=good_table, outfile="/x", freqrange=[0, 100], species=[],
00197             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00198             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=True,
00199             verbose=True, logfile="", append=True
00200         )
00201 
00202     def test_table(self):
00203         """ test various settings of the table parameter"""
00204 
00205         # no table name works because it defaults to the system spectral line table
00206         self._testit(
00207             table="", outfile="", freqrange=[0, 100], species=[],
00208             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00209             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00210             verbose=False, logfile="", append=True, nrows=59998
00211         )
00212         # test user specified table search
00213         self._testit(
00214             table=good_table, outfile="", freqrange=[0, 100], species=[],
00215             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00216             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00217             verbose=False, logfile="", append=True, nrows=67858
00218         )
00219 
00220     def test_outfile(self):
00221         """ test various settings of the outfile parameter"""
00222 
00223         outfile = "blah.tbl"
00224         self._testit(
00225             table=good_table, outfile=outfile, freqrange=[0, 100], species=[],
00226             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00227             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00228             verbose=False, logfile="", append=True, nrows=67858
00229         )
00230         
00231     def test_freqrange(self):
00232         """ test various settings of the freqrange parameter"""
00233 
00234         self._testit(
00235             table=good_table, outfile="", freqrange=[0, 100], species=[],
00236             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00237             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00238             verbose=False, logfile="", append=True, nrows=67858
00239         )
00240         self._testit(
00241             table=good_table, outfile="", freqrange=[0, 10], species=[],
00242             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00243             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00244             verbose=False, logfile="", append=True, nrows=15292
00245         )
00246 
00247     def test_species(self):
00248         """ test various settings of the species parameter"""
00249 
00250         self._testit(
00251             table=good_table, outfile="", freqrange=[0, 100], species=['S18O'],
00252             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00253             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00254             verbose=False, logfile="", append=True, nrows=9
00255         )
00256         self._testit(
00257             table=good_table, outfile="", freqrange=[0, 100], species=["S18O","HC5Nv11=1"],
00258             reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1],
00259             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00260             verbose=False, logfile="", append=True, nrows=81
00261         )
00262         
00263     def test_chemnames(self):
00264         """ test various settings of the chemnames parameter"""
00265 
00266         self._testit(
00267             table=good_table, outfile="", freqrange=[0, 100], species=[],
00268             reconly=True, chemnames=["Silicon Monocarbide"], qns=[],
00269             intensity=[-1], smu2=[-1],
00270             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00271             verbose=False, logfile="", append=True, nrows=6
00272         )
00273         self._testit(
00274             table=good_table, outfile="", freqrange=[0, 100], species=[],
00275             reconly=True, chemnames=["Silicon Monocarbide", "Potassium chloride"],
00276             qns=[], intensity=[-1], smu2=[-1],
00277             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00278             verbose=False, logfile="", append=True, nrows=32
00279         )
00280 
00281     def test_qns(self):
00282         """ test various settings of the qns parameter"""
00283 
00284         self._testit(
00285             table=good_table, outfile="", freqrange=[0, 100], species=[],
00286             reconly=True, chemnames=[], qns=["11-10"],
00287             intensity=[-1], smu2=[-1],
00288             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00289             verbose=False, logfile="", append=True, nrows=17
00290         )
00291 
00292     def test_intensity(self):
00293         """ test various settings of the intensity parameter"""
00294 
00295         self._testit(
00296             table=good_table, outfile="", freqrange=[0, 100], species=[],
00297             reconly=True, chemnames=[], qns=[],
00298             intensity=[-10,-8], smu2=[-1],
00299             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00300             verbose=False, logfile="", append=True, nrows=13447
00301         )
00302         self._testit(
00303             table=good_table, outfile="", freqrange=[0, 100], species=[],
00304             reconly=True, chemnames=[], qns=[],
00305             intensity=[-10,-8], smu2=[-1],
00306             loga=[-1], el=[-1], eu=[-1], rrlinclude=False, rrlonly=False,
00307             verbose=False, logfile="", append=True, nrows=7626
00308         )
00309 
00310     def test_smu2(self):
00311         """ test various settings of the smu2 parameter"""
00312 
00313         self._testit(
00314             table=good_table, outfile="", freqrange=[0, 100], species=[],
00315             reconly=True, chemnames=[], qns=[],
00316             intensity=[], smu2=[5, 10],
00317             loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00318             verbose=False, logfile="", append=True, nrows=12227
00319         )
00320         self._testit(
00321             table=good_table, outfile="", freqrange=[0, 100], species=[],
00322             reconly=True, chemnames=[], qns=[],
00323             intensity=[-1], smu2=[5, 10],
00324             loga=[-1], el=[-1], eu=[-1], rrlinclude=False, rrlonly=False,
00325             verbose=False, logfile="", append=True, nrows=6406
00326         )
00327 
00328     def test_loga(self):
00329         """ test various settings of the loga parameter"""
00330 
00331         self._testit(
00332             table=good_table, outfile="", freqrange=[0, 100], species=[],
00333             reconly=True, chemnames=[], qns=[],
00334             intensity=[], smu2=[],
00335             loga=[-6, -4], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False,
00336             verbose=False, logfile="", append=True, nrows=22781
00337         )
00338         self._testit(
00339             table=good_table, outfile="", freqrange=[0, 100], species=[],
00340             reconly=True, chemnames=[], qns=[],
00341             intensity=[], smu2=[],
00342             loga=[-6, -4], el=[-1], eu=[-1], rrlinclude=False, rrlonly=False,
00343             verbose=False, logfile="", append=True, nrows=16960
00344         )
00345 
00346     def test_eu(self):
00347         """ test various settings of the eu parameter"""
00348 
00349         self._testit(
00350             table=good_table, outfile="", freqrange=[0, 100], species=[],
00351             reconly=True, chemnames=[], qns=[],
00352             intensity=[], smu2=[],
00353             loga=[], el=[-1], eu=[150,200], rrlinclude=True, rrlonly=False,
00354             verbose=False, logfile="", append=True, nrows=9079
00355         )
00356         self._testit(
00357             table=good_table, outfile="", freqrange=[0, 100], species=[],
00358             reconly=True, chemnames=[], qns=[],
00359             intensity=[], smu2=[],
00360             loga=[], el=[-1], eu=[150,200], rrlinclude=False, rrlonly=False,
00361             verbose=False, logfile="", append=True, nrows=3258
00362         )
00363         self._testit(
00364             table=good_table, outfile="", freqrange=[1, 1.1], species=[],
00365             reconly=True, chemnames=[], qns=[],
00366             intensity=[], smu2=[],
00367             loga=[], el=[], eu=[1581.52, 1581.53], rrlinclude=False, rrlonly=False,
00368             verbose=False, logfile="", append=True, nrows=0
00369         )
00370         self._testit(
00371             table=good_table, outfile="", freqrange=[1, 1.1], species=[],
00372             reconly=True, chemnames=[], qns=[],
00373             intensity=[], smu2=[],
00374             loga=[], el=[], eu=[1581.57, 1581.58] , rrlinclude=False, rrlonly=False,
00375             verbose=False, logfile="", append=True, nrows=1
00376         )
00377 
00378 
00379     def test_el(self):
00380         """ test various settings of the el parameter"""
00381 
00382         self._testit(
00383             table=good_table, outfile="", freqrange=[0, 100], species=[],
00384             reconly=True, chemnames=[], qns=[],
00385             intensity=[], smu2=[],
00386             loga=[], el=[150,200], eu=[], rrlinclude=True, rrlonly=False,
00387             verbose=False, logfile="", append=True, nrows=9023
00388         )
00389         self._testit(
00390             table=good_table, outfile="", freqrange=[0, 100], species=[],
00391             reconly=True, chemnames=[], qns=[],
00392             intensity=[], smu2=[],
00393             loga=[], el=[150,200], eu=[], rrlinclude=False, rrlonly=False,
00394             verbose=False, logfile="", append=True, nrows=3202
00395         )
00396         self._testit(
00397             table=good_table, outfile="", freqrange=[1, 1.1], species=[],
00398             reconly=True, chemnames=[], qns=[],
00399             intensity=[], smu2=[],
00400             loga=[], el=[1581.52, 1581.53], eu=[], rrlinclude=False, rrlonly=False,
00401             verbose=False, logfile="", append=True, nrows=1
00402         )
00403         self._testit(
00404             table=good_table, outfile="", freqrange=[1, 1.1], species=[],
00405             reconly=True, chemnames=[], qns=[],
00406             intensity=[], smu2=[],
00407             loga=[], el=[1581.57, 1581.58] , eu=[], rrlinclude=False, rrlonly=False,
00408             verbose=False, logfile="", append=True, nrows=0
00409         )
00410 
00411     def test_rrlonly(self):
00412         """ test various settings of the rrlonly parameter"""
00413 
00414         self._testit(
00415             table=good_table, outfile="", freqrange=[0, 100], species=[],
00416             reconly=True, chemnames=[], qns=[],
00417             intensity=[], smu2=[],
00418             loga=[], el=[], eu=[], rrlinclude=True, rrlonly=False,
00419             verbose=False, logfile="", append=True, nrows=67858
00420         )
00421         self._testit(
00422             table=good_table, outfile="", freqrange=[0, 100], species=[],
00423             reconly=True, chemnames=[], qns=[],
00424             intensity=[], smu2=[],
00425             loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True,
00426             verbose=False, logfile="", append=True, nrows=5821
00427         )
00428 
00429     def test_logfile(self):
00430         """ test various settings of the logfile and append parameters"""
00431 
00432         logfile = "xx.log"
00433         # verbose = False so no logfile should be written
00434         self._testit(
00435             table=good_table, outfile="", freqrange=[0, 100], species=[],
00436             reconly=True, chemnames=[], qns=[],
00437             intensity=[], smu2=[],
00438             loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True,
00439             verbose=False, logfile=logfile, append=False, nrows=5821
00440         )
00441         self.assertFalse(os.path.exists(logfile))
00442         # verbose and overwrite
00443         self._testit(
00444             table=good_table, outfile="", freqrange=[0, 100], species=[],
00445             reconly=True, chemnames=[], qns=[],
00446             intensity=[], smu2=[],
00447             loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True,
00448             verbose=True, logfile=logfile, append=False, nrows=5821
00449         )
00450         self.assertTrue(os.path.exists(logfile))
00451 
00452         num_lines = sum(1 for line in open(logfile))
00453         # append (twice)
00454         self.assertEquals(num_lines, 5822)
00455         self._testit(
00456             table=good_table, outfile="", freqrange=[0, 100], species=[],
00457             reconly=True, chemnames=[], qns=[],
00458             intensity=[], smu2=[],
00459             loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True,
00460             verbose=True, logfile=logfile, append=True, nrows=5821
00461         )
00462         self.assertTrue(os.path.exists(logfile))
00463         num_lines = sum(1 for line in open(logfile))
00464         self.assertEquals(num_lines, 3*5822)
00465         os.remove(logfile)
00466 
00467 
00468 def suite():
00469     return [slsearch_test]