00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
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 import numpy
00076
00077 image = "imregion.fits"
00078 text1 = "goodfile1.txt"
00079 res1 = "res1.rgn"
00080 cas_3258t = "CAS-3258.txt"
00081 cas_3258r = "CAS-3258.rgn"
00082 cas_3259t = "CAS-3259.txt"
00083 cas_3259r = "CAS-3259.rgn"
00084 cas_3260t = "CAS-3260.txt"
00085 cas_3260r = "CAS-3260.rgn"
00086
00087 def deep_equality(a, b):
00088 if (type(a) != type(b)):
00089 print "types don't match, a is a " + str(type(a)) + " b is a " + str(type(b))
00090 return False
00091 if (type(a) == dict):
00092 if (a.keys() != b.keys()):
00093 print "keys don't match, a is " + str(a.keys()) + " b is " + str(b.keys())
00094 return False
00095 for k in a.keys():
00096 if (
00097 k == "telescope" or k == "observer"
00098 or k == "telescopeposition"
00099 ):
00100 continue
00101 elif (not deep_equality(a[k], b[k])):
00102 print "dictionary member inequality a[" + str(k) \
00103 + "] is " + str(a[k]) + " b[" + str(k) + "] is " + str(b[k])
00104 return False
00105 return True
00106 if (type(a) == float):
00107 if not (a == b or abs((a-b)/a) <= 1e-6):
00108 print "float mismatch, a is " + str(a) + ", b is " + str(b)
00109 return a == b or abs((a-b)/a) <= 1e-6
00110 if (type(a) == numpy.ndarray):
00111 if (a.shape != b.shape):
00112 print "shape mismatch a is " + str(a.shape) + " b is " + str(b.shape)
00113 return False
00114 x = a.tolist()
00115 y = b.tolist()
00116 for i in range(len(x)):
00117 if (not deep_equality(x[i], y[i])):
00118 print "array element mismatch, x is " + str(x[i]) + " y is " + str(y[i])
00119 return False
00120 return True
00121 return a == b
00122
00123 class rg_fromtextfile_test(unittest.TestCase):
00124
00125 _fixtures = [
00126 image, text1, res1, cas_3258t, cas_3258r, cas_3259t, cas_3259r,
00127 cas_3260t, cas_3260r
00128 ]
00129
00130 def setUp(self):
00131 datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/rg.fromtextfile/'
00132 for im in self._fixtures:
00133 shutil.copy(datapath + im, im)
00134 self.ia = iatool()
00135 self.rg = rgtool()
00136
00137 def tearDown(self):
00138 for im in self._fixtures:
00139 os.remove(im)
00140 self.ia.done()
00141 del self.ia
00142 self.rg.done()
00143 del self.rg
00144
00145 def _testit(self, text, rgn):
00146 csys = self.ia.coordsys().torecord()
00147 shape = self.ia.shape()
00148 got = self.rg.fromtextfile(text, shape, csys)
00149 expected = self.rg.fromfiletorecord(rgn)
00150 expected['comment'] = ""
00151 self.assertTrue(deep_equality(got, expected))
00152
00153 f = open(text, 'r')
00154 text = f.read()
00155 got = self.rg.fromtext(text, shape, csys)
00156 self.assertTrue(deep_equality(got, expected))
00157
00158 def test_exceptions(self):
00159 """test exception cases"""
00160
00161
00162 self.assertRaises(Exception, self.rg.fromtextfile, "blah", {}, [1,1])
00163
00164 self.assertRaises(Exception, self.rg.fromtextfile, text1, {}, [1,1])
00165
00166 def test_read(self):
00167 """Read test"""
00168 self.ia.open(image)
00169 self._testit(text1, res1)
00170
00171 def test_CAS_3258(self):
00172 """Verify fix to CAS-3258"""
00173 self.ia.fromshape("", [250,250])
00174 self._testit(cas_3258t, cas_3258r)
00175
00176 def test_CAS_3259(self):
00177 """Verify fix to CAS-3259"""
00178 self.ia.fromshape("", [250,250])
00179 self._testit(cas_3259t, cas_3259r)
00180
00181 def test_CAS_3260(self):
00182 """Verify fix to CAS-3260"""
00183 self.ia.fromshape("", [250,250])
00184 self._testit(cas_3260t, cas_3260r)
00185
00186 def test_CAS_4415(self):
00187 """Verify CAS-4415 (parser did not properly handle frquency decreasing with pixel number)"""
00188 shape = [50, 50, 10]
00189 self.ia.fromshape("", shape)
00190 csys = self.ia.coordsys()
00191 increment = csys.increment()["numeric"]
00192 increment[2] = -increment[2]
00193 csys.setincrement(increment)
00194 self.ia.setcoordsys(csys.torecord())
00195 zz = rg.fromtext(
00196 "circle[[20pix,20pix],6pix],range=[1pix,3pix]",
00197 shape, csys.torecord()
00198 )
00199 self.assertTrue(len(zz.keys()) > 0)
00200
00201 def test_CAS_4425(self):
00202 """ Verify CAS-4425 (pixel units now accounted for in range and no units throws exception)"""
00203 shape = [100, 100, 80]
00204 self.ia.fromshape("", shape)
00205 csys = self.ia.coordsys()
00206 zz = rg.fromtext("box[[30pix, 30pix], [39pix, 39pix]], range=[55pix,59pix]", shape, csys.torecord())
00207 self.assertTrue(self.ia.statistics(region=zz)["npts"] == 500)
00208 zz = rg.fromtext("box[[30pix, 30pix], [39pix, 39pix]], range=[59pix,55pix]", shape, csys.torecord())
00209 self.assertTrue(self.ia.statistics(region=zz)["npts"] == 500)
00210 self.assertRaises(
00211 Exception, rg.fromtext, "box[[30pix, 30pix], [39pix, 39pix]], range=[59,55]",
00212 shape, csys.torecord()
00213 )
00214
00215
00216
00217 def suite():
00218 return [rg_fromtextfile_test]