casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdfit.py
Go to the documentation of this file.
00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 #
00009 import listing
00010 from numpy import array
00011 
00012 import asap as sd
00013 from sdfit import sdfit
00014 
00015 class sdfit_test(unittest.TestCase):
00016     """
00017     Unit tests for task sdfit. No interactive testing.
00018 
00019     The list of tests:
00020     testGaussian00 --- test fitting a broad Gaussian profile (centre=4000, fwhm=1000, ampl=10)
00021     testGaussian01 --- test fitting a broad Gaussian profile (centre= 500, fwhm=1000, ampl=10) : on spectral edge
00022     testGaussian02 --- test fitting a narrow Gaussian profile (centre=4000, fwhm=100, ampl=10)
00023     testGaussian03 --- test fitting a combination of broad and narrow Gaussian profiles
00024                        (cen1=3000,fwhm1=1000,ampl1=10,cen2=6000,fwhm2=100,ampl2=10) : separated
00025     testGaussian04 --- test fitting a combination of broad and narrow Gaussian profiles
00026                        (cen1=4000,fwhm1=1000,ampl1=10,cen2=4700,fwhm2=100,ampl2=10) : overlapped
00027     testLorentzian00 --- test fitting a broad Lorentzian profile (centre=4000, fwhm=1000, ampl=10)
00028     testLorentzian01 --- test fitting a broad Lorentzian profile (centre= 500, fwhm=1000, ampl=10) : on spectral edge
00029     testLorentzian02 --- test fitting a narrow Lorentzian profile (centre=4000, fwhm=100, ampl=10)
00030     testLorentzian03 --- test fitting a combination of broad and narrow Lorentzian profiles
00031                        (cen1=3000,fwhm1=1000,ampl1=10,cen2=6000,fwhm2=100,ampl2=10) : separated
00032     testLorentzian04 --- test fitting a combination of broad and narrow Lorentzian profiles
00033                        (cen1=4000,fwhm1=1000,ampl1=10,cen2=4700,fwhm2=100,ampl2=10) : overlapped
00034     
00035     Note: (1) the rms noise is 1.0 for all data.
00036 
00037     created 21/04/2011 by Wataru Kawasaki
00038     """
00039     # Data path of input/output
00040     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdfit/'
00041     # Input and output names
00042     infile_gaussian   = 'Artificial_Gaussian.asap'
00043     infile_lorentzian = 'Artificial_Lorentzian.asap'
00044 
00045     def setUp(self):
00046         if os.path.exists(self.infile_gaussian):
00047             shutil.rmtree(self.infile_gaussian)
00048         shutil.copytree(self.datapath+self.infile_gaussian, self.infile_gaussian)
00049         if os.path.exists(self.infile_lorentzian):
00050             shutil.rmtree(self.infile_lorentzian)
00051         shutil.copytree(self.datapath+self.infile_lorentzian, self.infile_lorentzian)
00052 
00053         default(sdfit)
00054 
00055     def tearDown(self):
00056         if os.path.exists(self.infile_gaussian):
00057             shutil.rmtree(self.infile_gaussian)
00058         if os.path.exists(self.infile_lorentzian):
00059             shutil.rmtree(self.infile_lorentzian)
00060 
00061     def testGaussian00(self):
00062         """Test Gaussian00: single broad profile """
00063         infile = self.infile_gaussian
00064         scanlist = [0]
00065         fitfunc = "gauss"
00066         fitmode = "list"
00067         maskline = []
00068         nfit = 1
00069         
00070         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00071         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00072 
00073         ans = {'cent': [[4000.0]],
00074                'fwhm': [[1000.0]],
00075                'peak': [[10.0]]}
00076 
00077         """ the result (RHEL5 64bit)
00078         ref = {'cent': [[[3997.420166015625, 2.2180848121643066]]],
00079                'fwhm': [[[1006.1046142578125, 5.2231903076171875]]],
00080                'nfit': [1],
00081                'peak': [[[9.9329404830932617, 0.044658195227384567]]]}
00082         """
00083 
00084         self.checkResult(res, ans)
00085 
00086     def testGaussian01(self):
00087         """Test Gaussian01: single broad profile on spectral edge"""
00088         infile = self.infile_gaussian
00089         scanlist = [1]
00090         fitfunc = "gauss"
00091         fitmode = "list"
00092         maskline = [0,2000]
00093         nfit = 1
00094         
00095         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00096         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00097 
00098         ans = {'cent': [[500.0]],
00099                'fwhm': [[1000.0]],
00100                'peak': [[10.0]]}
00101 
00102         """ the result (RHEL5 64bit)
00103         ref = {'cent': [[[504.638427734375, 2.7173392772674561]]],
00104                'fwhm': [[[998.78643798828125, 7.1386871337890625]]],
00105                'nfit': [1],
00106                'peak': [[[10.030097961425781, 0.047238241881132126]]]}
00107         """
00108 
00109         self.checkResult(res, ans)
00110 
00111     def testGaussian02(self):
00112         """Test Gaussian02: single narrow profile """
00113         infile = self.infile_gaussian
00114         scanlist = [2]
00115         fitfunc = "gauss"
00116         fitmode = "list"
00117         maskline = []
00118         nfit = 1
00119         
00120         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00121         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00122 
00123         ans = {'cent': [[4000.0]],
00124                'fwhm': [[100.0]],
00125                'peak': [[10.0]]}
00126 
00127         """ the result (RHEL5 64bit)
00128         ref = {'cent': [[[3999.159912109375, 0.68400073051452637]]],
00129                'fwhm': [[[98.87506103515625, 1.6106985807418823]]],
00130                'nfit': [1],
00131                'peak': [[[9.9385099411010742, 0.14021013677120209]]]}
00132         """
00133 
00134         self.checkResult(res, ans)
00135 
00136     def testGaussian03(self):
00137         """Test Gaussian03: broad/narrow combination : separated """
00138         infile = self.infile_gaussian
00139         scanlist = [3]
00140         fitfunc = "gauss"
00141         fitmode = "list"
00142         maskline = [[2000,4000],[5500,6500]]
00143         nfit = [1,1]
00144         
00145         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00146         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00147 
00148         ans = {'cent': [[3000.0, 6000.0]],
00149                'fwhm': [[1000.0, 100.0]],
00150                'peak': [[10.0, 10.0]]}
00151 
00152         """ the result (RHEL5 64bit)
00153         ref = {'cent': [[[2996.004638671875, 2.2644386291503906],
00154                          [5999.11181640625, 0.70802927017211914]]],
00155                'fwhm': [[[1001.549560546875, 5.4809303283691406],
00156                          [99.259437561035156, 1.6672815084457397]]],
00157                'nfit': [2],
00158                'peak': [[[9.899937629699707, 0.04574853926897049],
00159                          [9.9107418060302734, 0.14416992664337158]]]}
00160         """
00161 
00162         self.checkResult(res, ans)
00163 
00164     def testGaussian04(self):
00165         """Test Gaussian04: broad/narrow combination : overlapped """
00166         infile = self.infile_gaussian
00167         scanlist = [4]
00168         fitfunc = "gauss"
00169         fitmode = "list"
00170         maskline = [[3000,4400],[4500,5000]]
00171         nfit = [1,1]
00172         
00173         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00174         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00175 
00176         ans = {'cent': [[4000.0, 4700.0]],
00177                'fwhm': [[1000.0, 100.0]],
00178                'peak': [[10.0, 10.0]]}
00179 
00180         """ the result (RHEL5 64bit)
00181         ref = {'cent': [[[4001.522216796875, 2.6332762241363525],
00182                          [4699.75732421875, 0.6802678108215332]]],
00183                'fwhm': [[[999.63507080078125, 6.4683256149291992],
00184                          [97.721427917480469, 1.7482517957687378]]],
00185                'nfit': [2],
00186                'peak': [[[9.9929990768432617, 0.04641139879822731],
00187                          [10.233022689819336, 0.15014420449733734]]]}
00188         """
00189 
00190         self.checkResult(res, ans)
00191 
00192     def testLorentzian00(self):
00193         """Test Lorentzian00: single broad profile """
00194         infile = self.infile_lorentzian
00195         scanlist = [0]
00196         fitfunc = "lorentz"
00197         fitmode = "list"
00198         maskline = []
00199         nfit = 1
00200         
00201         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00202         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00203 
00204         ans = {'cent': [[4000.0]],
00205                'fwhm': [[1000.0]],
00206                'peak': [[10.0]]}
00207 
00208         """ the result (RHEL5 64bit)
00209         ref = {'cent': [[[3997.696044921875, 2.5651662349700928]]],
00210                'fwhm': [[[1010.3181762695312, 7.2803301811218262]]],
00211                'nfit': [1],
00212                'peak': [[[9.9210958480834961, 0.05041566863656044]]]}
00213         """
00214 
00215         self.checkResult(res, ans)
00216 
00217     def testLorentzian01(self):
00218         """Test Lorentzian01: single broad profile on spectral edge"""
00219         infile = self.infile_lorentzian
00220         scanlist = [1]
00221         fitfunc = "lorentz"
00222         fitmode = "list"
00223         maskline = [0,2000]
00224         nfit = 1
00225         
00226         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00227         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00228 
00229         ans = {'cent': [[500.0]],
00230                'fwhm': [[1000.0]],
00231                'peak': [[10.0]]}
00232         
00233         """ the result (RHEL5 64bit)
00234         ref = {'cent': [[[500.99105834960938, 2.8661653995513916]]],
00235                'fwhm': [[[995.85455322265625, 9.5194911956787109]]],
00236                'nfit': [1],
00237                'peak': [[[10.041034698486328, 0.053434751927852631]]]}
00238         """
00239 
00240         self.checkResult(res, ans)
00241 
00242     def testLorentzian02(self):
00243         """Test Lorentzian02: single narrow profile """
00244         infile = self.infile_lorentzian
00245         scanlist = [2]
00246         fitfunc = "lorentz"
00247         fitmode = "list"
00248         maskline = []
00249         nfit = 1
00250         
00251         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00252         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00253 
00254         ans = {'cent': [[4000.0]],
00255                'fwhm': [[100.0]],
00256                'peak': [[10.0]]}
00257 
00258         """ the result (RHEL5 64bit)
00259         ref = {'cent': [[[3999.230224609375, 0.79903918504714966]]],
00260                'fwhm': [[[102.48796081542969, 2.2600326538085938]]],
00261                'nfit': [1],
00262                'peak': [[[9.9708395004272461, 0.1554737389087677]]]}
00263         """
00264 
00265         self.checkResult(res, ans)
00266 
00267     def testLorentzian03(self):
00268         """Test Lorentzian03: broad/narrow combination : separated """
00269         infile = self.infile_lorentzian
00270         scanlist = [3]
00271         fitfunc = "lorentz"
00272         fitmode = "list"
00273         maskline = [[2000,4000],[5500,6500]]
00274         nfit = [1,1]
00275         
00276         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00277         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00278 
00279         ans = {'cent': [[3000.0, 6000.0]],
00280                'fwhm': [[1000.0, 100.0]],
00281                'peak': [[10.0, 10.0]]}
00282 
00283         """ the result (RHEL5 64bit)
00284         ref = {'cent': [[[3001.23876953125, 2.5231325626373291],
00285                          [5999.01953125, 0.82874661684036255]]],
00286                'fwhm': [[[990.19671630859375, 8.1528301239013672],
00287                          [102.10212707519531, 2.3521277904510498]]],
00288                'nfit': [2],
00289                'peak': [[[9.9958734512329102, 0.051685664802789688],
00290                          [9.6133279800415039, 0.1561257392168045]]]}
00291         """
00292 
00293         self.checkResult(res, ans)
00294 
00295     def testLorentzian04(self):
00296         """Test Lorentzian04: broad/narrow combination : overlapped """
00297         infile = self.infile_lorentzian
00298         scanlist = [4]
00299         fitfunc = "lorentz"
00300         fitmode = "list"
00301         maskline = [[3000,4400],[4500,5000]]
00302         nfit = [1,1]
00303         
00304         res = sdfit(infile=infile,scanlist=scanlist,fitfunc=fitfunc,fitmode=fitmode,maskline=maskline,nfit=nfit)
00305         self.assertNotEqual(res, None, msg="The task returned None. Fit failed.")
00306 
00307         ans = {'cent': [[4000.0, 4700.0]],
00308                'fwhm': [[1000.0, 100.0]],
00309                'peak': [[10.0, 10.0]]}
00310         
00311         """ the result (RHEL5 64bit)
00312         ref = {'cent': [[[3995.85693359375, 3.0016641616821289],
00313                          [4699.53271484375, 0.82658475637435913]]],
00314                'fwhm': [[[972.22833251953125, 10.149419784545898],
00315                          [104.71010589599609, 2.7239837646484375]]],
00316                'nfit': [2],
00317                'peak': [[[10.013784408569336, 0.053735069930553436],
00318                          [9.9273672103881836, 0.15813499689102173]]]}
00319         """
00320 
00321         self.checkResult(res, ans)
00322 
00323     def checkResult(self, result, answer):
00324         for key in ['cent', 'fwhm', 'peak']:
00325             for i in range(len(result[key][0])):
00326                 val = result[key][0][i][0]
00327                 err = result[key][0][i][1]
00328                 ans = answer[key][0][i]
00329 
00330                 #check if result is consistent with answer in 3-sigma level
00331                 threshold = 3.0
00332                 
00333                 within_errorrange = (abs(ans - val) <= abs(err * threshold))
00334                 self.assertTrue(within_errorrange)
00335 
00336 class sdfit_test_exceptions(unittest.TestCase):
00337     """
00338     test the case when sdfit throws exception.
00339     """
00340     # Data path of input/output
00341     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdfit/'
00342     # Input and output names
00343     infile_gaussian   = 'Artificial_Gaussian.asap'
00344 
00345     def setUp(self):
00346         if os.path.exists(self.infile_gaussian):
00347             shutil.rmtree(self.infile_gaussian)
00348         shutil.copytree(self.datapath+self.infile_gaussian, self.infile_gaussian)
00349         default(sdfit)
00350 
00351     def tearDown(self):
00352         if os.path.exists(self.infile_gaussian):
00353             shutil.rmtree(self.infile_gaussian)
00354 
00355     def testNoData(self):
00356         try:
00357             res = sdfit(infile=self.infile_gaussian,iflist=99)
00358             self.assertTrue(False,
00359                             msg='The task must throw exception')
00360         except Exception, e:
00361             pos=str(e).find('Selection contains no data. Not applying it.')
00362             self.assertNotEqual(pos,-1,
00363                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00364 
00365 def suite():
00366     return [sdfit_test, sdfit_test_exceptions]