casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdflag.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 asap as sd
00010 from sdflag import sdflag
00011 
00012 class sdflag_test(unittest.TestCase):
00013     """
00014     Basic unit tests for task sdflag.
00015 
00016     The list of tests:
00017     test00   --- test channel flagging/unflagging
00018     test01   --- test row flagging/unflagging
00019     test02   --- test clipping flagging/unflagging
00020     test03   --- test no data after selection (raises an exception)
00021 
00022     ***NOTE*** These tests are for Scantable only. Tests for the other formats
00023                which ASAP supports, including MS and SDFITS, are to be made later.
00024 
00025     NOTE2: 'Artificial_Flat.asap' has 6 flat spectra with rms of 1.0.
00026 
00027     """
00028     # Data path of input/output
00029     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdflag/'
00030     # Input and output names
00031     infile = 'Artificial_Flat.asap'
00032 
00033     def setUp(self):
00034         if os.path.exists(self.infile):
00035             shutil.rmtree(self.infile)
00036         shutil.copytree(self.datapath+self.infile, self.infile)
00037 
00038         default(sdflag)
00039 
00040     def tearDown(self):
00041         if os.path.exists(self.infile):
00042             shutil.rmtree(self.infile)
00043 
00044     def test00(self):
00045         """Test 0: channel flagging"""
00046         infile = self.infile
00047         maskflag = [[1,3],[10,15]]
00048 
00049         #flag
00050         result = sdflag(infile=infile,maskflag=maskflag)
00051 
00052         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00053 
00054         scan = sd.scantable(filename=infile, average=False)
00055         ansmask = scan.create_mask(maskflag, invert=True)
00056         for i in range (scan.nrow()):
00057             mask = scan.get_mask(i)
00058             res = True
00059             for j in range(len(mask)):
00060                 if mask[j] != ansmask[j]:
00061                     res = False
00062                     break
00063             self.assertTrue(res)
00064         del scan
00065 
00066         #unflag
00067         result = sdflag(infile=infile,maskflag=maskflag,flagmode="unflag")
00068 
00069         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00070 
00071         scan = sd.scantable(filename=infile, average=False)
00072         for i in range (scan.nrow()):
00073             mask = scan.get_mask(i)
00074             res = True
00075             for j in range(len(mask)):
00076                 if mask[j] != True:
00077                     res = False
00078                     break
00079             self.assertTrue(res)
00080         del scan
00081 
00082     def test01(self):
00083         """Test 1: row flagging"""
00084         infile = self.infile
00085         flagrow = [2,4]
00086 
00087         #flag
00088         result = sdflag(infile=infile,flagrow=flagrow)
00089 
00090         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00091 
00092         scan = sd.scantable(filename=infile, average=False)
00093         for i in range (scan.nrow()):
00094             resrflag = scan._getflagrow(i)
00095             ansrflag = False
00096             for j in flagrow:
00097                 if i == j:
00098                     ansrflag = True
00099                     break
00100             res = (resrflag == ansrflag)
00101             self.assertTrue(res)
00102         del scan
00103 
00104         #unflag
00105         result = sdflag(infile=infile,flagrow=flagrow,flagmode="unflag")
00106 
00107         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00108 
00109         scan = sd.scantable(filename=infile, average=False)
00110         for i in range (scan.nrow()):
00111             resrflag = scan._getflagrow(i)
00112             res = (resrflag == False)
00113             self.assertTrue(res)
00114         del scan
00115 
00116     def test02(self):
00117         """Test 2: clipping"""
00118         infile = self.infile
00119         clip = True
00120         clipminmax = [-3.5, 3.5] #clip at 3.5-sigma level, i.e., flag channels at which abs(value) exceeds 3.5.
00121 
00122         #flag
00123         result = sdflag(infile=infile,clip=clip,clipminmax=clipminmax)
00124 
00125         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00126 
00127         ansmlist = [
00128             [[0,301],[303,8191]],
00129             [[0,3326],[3328,7753],[7755,8191]],
00130             [[0,6545],[6547,8191]],
00131             [[0,3996],[3998,7920],[7922,8191]],
00132             [[0,3274],[3276,8191]],
00133             [[0,1708],[1710,7710],[7712,7799],[7801,8191]]
00134             ]
00135 
00136         scan = sd.scantable(filename=infile, average=False)
00137         for i in range (scan.nrow()):
00138             mask = scan.get_mask(i)
00139             ansmask = scan.create_mask(ansmlist[i])
00140             res = True
00141             for j in range(len(mask)):
00142                 if mask[j] != ansmask[j]:
00143                     res = False
00144                     break
00145             self.assertTrue(res)
00146         del scan
00147 
00148         #unflag
00149         result = sdflag(infile=infile,clip=clip,clipminmax=clipminmax,flagmode="unflag")
00150 
00151         self.assertEqual(result, None, msg="The task returned '"+str(result)+"' instead of None")
00152 
00153         scan = sd.scantable(filename=infile, average=False)
00154         for i in range (scan.nrow()):
00155             mask = scan.get_mask(i)
00156             res = True
00157             for j in range(len(mask)):
00158                 if mask[j] != True:
00159                     res = False
00160                     break
00161             self.assertTrue(res)
00162         del scan
00163 
00164     def test03(self):
00165         #test for the default parameters (raises an exception)
00166         infile = self.infile
00167         iflist = [10] # non-existent IF value
00168         maskflag = [0,3]
00169         try:
00170             result = sdflag(infile=infile, iflist=iflist, maskflag=maskflag)
00171         except Exception, e:
00172             pos = str(e).find('Selection contains no data. Not applying it.')
00173             self.assertNotEqual(pos, -1, msg='Unexpected exception was thrown: %s'%(str(e)))
00174 
00175 
00176 def suite():
00177     return [sdflag_test]