Commits

Akeem Wells authored 2666d390b99 Merge
Merge branch 'master' into CAS-13770

casatasks/tests/tasks/test_task_imval.py

Modified
57 57 # SHORT DESCRIPTION: Display information.
58 58 #
59 59 # DESCRIPTION: Write information to the local logger with the given priority
60 60 #
61 61 ############################################################################
62 62
63 63 def note(message, priority="NORMAL", origin="imval_test"):
64 64 print(message)
65 65 casalog.postLocally(message, priority, origin)
66 66
67 +###########################################################################
68 +# Input test general format
69 +
70 +
71 +def tryInput(imagename, box='', chans='', stokes='', region='', errormsg=''):
72 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
73 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
74 + info('Performing input/output tests on imagename, errors WILL occur.')
75 +
76 + results = None
77 + try:
78 + results = imval(imagename=imagename, box=box, chans=chans, stokes=stokes, region=region)
79 + except:
80 + pass
81 + else:
82 + if (results != None \
83 + and ((isinstance(results, bool) and results == True) \
84 + or (isinstance(results, dict) and results != {}))):
85 + retValue['success'] = False
86 + retValue['error_msgs'] = retValue['error_msgs'] \
87 + + errormsg
88 + return retValue
89 +
90 +###########################################################################
91 +# Single point test general format
92 +
93 +
94 +def setUpImage(retValue):
95 + bbox = {}
96 + try:
97 + _ia.open(image_file)
98 + bbox = _ia.boundingbox()
99 + _ia.done()
100 + except:
101 + retValue['success'] = False
102 + retValue['error_msgs'] = retValue['error_msgs'] \
103 + + "\nError: Unable to find size of image " + image_file
104 +
105 + dir_blc = []
106 + dir_trc = []
107 + min_chan = max_chan = -1
108 + min_stokes = max_stokes = -1
109 + if (len(bbox) > 0 and 'blc' in bbox and 'trc' in bbox):
110 + blc = bbox['blc']
111 + trc = bbox['trc']
112 +
113 + dir_blc = [blc[0], blc[1]]
114 + dir_trc = [trc[0], trc[1]]
115 + min_chan = blc[3]
116 + max_chan = trc[3]
117 + min_Stokes = blc[2]
118 + max_stokes = trc[2]
119 +
120 + error_margin = 0.00001
121 +
122 + return dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin
123 +
124 +
125 +def trySinglePoint(retValue, imagename='', box='', chans='', stokes='', expected=0.0, errormsg='', errorval=0.00001):
126 + results = None
127 + try:
128 + results = imval(imagename=imagename, box=box, chans=str(chans), \
129 + stokes=str(stokes))
130 + except Exception:
131 + retValue['success'] = False
132 + retValue['error_msgs'] = retValue['error_msgs'] \
133 + + "\nError: Failed to get the value in the bottom right" \
134 + + " corner. " + box + "."
135 + else:
136 + if (results != None and 'blc' in results \
137 + and 'data' in results and 'unit' in results \
138 + and 'mask' in results):
139 + msg = 'Bottom right corner, ' + str(results['blc']) + ', value is: ' \
140 + + str(results['data']) + str(results['unit']) \
141 + + ' with mask ' + str(results['mask'])
142 + if (results == None or 'data' not in results or \
143 + (results['data'] + expected > errorval or not results['mask'])):
144 + retValue['success'] = False
145 + retValue['error_msgs'] = retValue['error_msgs'] \
146 + + errormsg
147 +
148 + return retValue
149 +
150 +
67 151
68 152 ###########################################################################
69 153 # NAME: input_test
70 154 #
71 155 # SHORT DESCRIPTION: Make sure invalid input is detected.
72 156 #
73 157 # DESCRIPTION: Test input that is the wrong type, to no values given.
74 158 # We expect lots of exceptions for this test!
75 159 # More precisely
76 160 # a) No imagename given
80 164 # e) incorrect data type to box, chans, & stokes parameters
81 165 # f) Bad file name to region parameter
82 166 # g) Incorrect data type, not a string, to region parameter
83 167 # h) File name that does not contain a region to the region param.
84 168 #
85 169 ############################################################################
86 170
87 171 class imval_test(unittest.TestCase):
88 172
89 173 def setUp(self):
90 - if (os.path.exists(image_file)):
91 - os.system('rm -rf ' +image_file+ ' ' +good_rgn_file)
92 -
93 - os.system('cp -RH '+ os.path.join(datapath,image_file)+' ' + image_file)
94 - os.system('cp -RH '+ os.path.join(datapath,good_rgn_file)+' ' + good_rgn_file)
174 +
175 + if os.path.exists(image_file):
176 + shutil.rmtree(image_file)
177 + if os.path.exists(good_rgn_file):
178 + os.remove(good_rgn_file)
179 +
180 + shutil.copytree(os.path.join(datapath, image_file), image_file)
181 + shutil.copyfile(os.path.join(datapath, good_rgn_file), good_rgn_file)
95 182
96 183 def tearDown(self):
97 - os.system('rm -rf ' +image_file+ ' ' +good_rgn_file)
98 -
99 - def test_input(self):
100 - '''Imval: Input/output tests'''
101 - retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
102 - note( "Starting imval INPUT/OUTPUT tests.", 'NORMAL2' )
103 -
184 +
185 + shutil.rmtree(image_file)
186 + os.remove(good_rgn_file)
187 +
188 + if os.path.exists('mypv.im'):
189 + shutil.rmtree('mypv.im')
190 + if os.path.exists('xxyy.im'):
191 + shutil.rmtree('xxyy.im')
192 + if os.path.exists('garbage.rgn'):
193 + os.remove('garbage.rgn')
194 +
195 + def test_inputNoInputImage(self):
196 + '''Test when no image is provided'''
197 + retValue = tryInput(imagename='',
198 + errormsg="\nError: Empty imagename parameter not detected.")
199 + self.assertTrue(retValue['success'], retValue['error_msgs'])
200 +
201 + def test_inputRealInputImage(self):
202 + '''Test when .rgn file is used as input instead of Image'''
203 + retValue = tryInput(imagename=good_rgn_file,
204 + errormsg="\nError: Invalid image file name not detected.")
205 + self.assertTrue(retValue['success'], retValue['error_msgs'])
206 +
207 + def test_inputFloatVal(self):
208 + '''Test when a float/invalid file is given as the input'''
209 + retValue = tryInput(imagename=2.3,
210 + errormsg="\nError: Invalid image file name, 2.3, not detected.")
211 + self.assertTrue(retValue['success'], retValue['error_msgs'])
212 +
213 + def test_inputn4826_bima(self):
214 + '''Test with real image file n4826_bima'''
215 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
216 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
217 +
104 218 ###########################################################
105 219 # Image name tests
106 - info( 'Performing input/output tests on imagename, errors WILL occur.' )
107 - results=None
108 - try:
109 - results = imval( imagename='' )
110 - except:
111 - pass
112 - else:
113 - if ( results!=None \
114 - and ( (isinstance(results,bool) and results==True )\
115 - or (isinstance(results,dict) and results!={} ) ) ):
116 - retValue['success']=False
117 - retValue['error_msgs']=retValue['error_msgs']\
118 - +"\nError: Empty imagename parameter not detected."
119 -
220 + info('Performing input/output tests on imagename, errors WILL occur.')
120 221 results = None
121 222 try:
122 - results = imval( imagename=good_rgn_file )
123 - except:
124 - pass
125 - else:
126 - if ( results!=None \
127 - and ( (isinstance(results,bool) and results==True )\
128 - or (isinstance(results,dict) and results!={} ) ) ):
129 - retValue['success']=False
130 - retValue['error_msgs']=retValue['error_msgs']\
131 - +"\nError: Invalid image file name not detected."
132 -
133 - results=None
134 - try:
135 - results = imval( imagename=2.3 )
136 - except:
137 - pass
138 - else:
139 - if ( results!=None \
140 - and ( (isinstance(results,bool) and results==True )\
141 - or (isinstance(results,dict) and results!={} ) ) ):
142 - retValue['success']=False
143 - retValue['error_msgs']=retValue['error_msgs']\
144 - +"\nError: Invalid image file name, 2.3, not detected."
145 -
146 - results=None
147 - try:
148 - results = imval( imagename='n4826_bima.im' )
223 + results = imval(imagename='n4826_bima.im')
149 224 except:
150 - retValue['success']=False
151 - retValue['error_msgs']=retValue['error_msgs']\
152 - +"\nError: imval failed with valid file name, n4826_bima.im."
153 - if ( results == None ):
154 - retValue['success']=False
155 - retValue['error_msgs']=retValue['error_msgs']\
156 - +"\nError: Valid imagename, n4826_bima.im, test failed."
157 - del results
158 -
225 + retValue['success'] = False
226 + retValue['error_msgs'] = retValue['error_msgs'] \
227 + + "\nError: imval failed with valid file name, n4826_bima.im."
228 + if (results == None):
229 + retValue['success'] = False
230 + retValue['error_msgs'] = retValue['error_msgs'] \
231 + + "\nError: Valid imagename, n4826_bima.im, test failed."
232 + del results
233 +
159 234 ###################################################################
160 235 # Testing out of range errors.
161 236 # BLC=0,0,0,0 and TRC= 255,255,0,29 for n4826_bima.im
162 - info( 'Performing input/output tests on "box", errors WILL occur.' )
163 - results=None
164 - try:
165 - results = imval( imagename=image_file, box='-3,0,-3,3' )
166 - except:
167 - pass
168 - else:
169 - if ( results!=None \
170 - and ( (isinstance(results,bool) and results==True )\
171 - or (isinstance(results,dict) and results!={} ) ) ):
172 - retValue['success']=False
173 - retValue['error_msgs']=retValue['error_msgs']\
174 - +'\nInvalid box parameter, x=-3, values not detected.'
175 -
176 - results=None
177 - try:
178 - results = imval( imagename=image_file, box='200,0,262,3' )
179 - except:
180 - pass
181 - else:
182 - if ( results!=None \
183 - and ( (isinstance(results,bool) and results==True )\
184 - or (isinstance(results,dict) and results!={} ) ) ):
185 - retValue['success']=False
186 - retValue['error_msgs']=retValue['error_msgs']\
187 - +'Invalid box parameter values,262, not detected.'
188 -
189 - results=None
190 - try:
191 - results = imval( imagename=image_file, box='0,-3,0,3' )
192 - except:
193 - pass
194 - else:
195 - if ( results!=None \
196 - and ( (isinstance(results,bool) and results==True )\
197 - or (isinstance(results,dict) and results!={} ) ) ):
198 - retValue['success']=False
199 - retValue['error_msgs']=retValue['error_msgs']\
200 - + 'Invalid box parameter value, y=-3, not detected.'
201 -
202 - results=None
203 - try:
204 - results = imval( imagename=image_file, box='0,270,0,3' )
205 - except:
206 - pass
207 - else:
208 - if ( results!=None \
209 - and ( (isinstance(results,bool) and results==True )\
210 - or (isinstance(results,dict) and results!={} ) ) ):
211 - retValue['success']=False
212 - retValue['error_msgs']=retValue['error_msgs']\
213 - + 'Invalid box parameter value, y=270, not detected.'
214 -
215 - results=None
216 - try:
217 - results = imval( imagename=image_file, box='0,110,0,10' )
218 - except:
219 - pass
220 - else:
221 - if ( results!=None \
222 - and ( (isinstance(results,bool) and results==True )\
223 - or (isinstance(results,dict) and results!={} ) ) ):
224 - retValue['success']=False
225 - retValue['error_msgs']=retValue['error_msgs']\
226 - + 'Invalid box parameter value, y[1]>y[0], not detected.'
227 -
228 - results=None
237 + info('Performing input/output tests on "box", errors WILL occur.')
238 + self.assertTrue(retValue['success'], retValue['error_msgs'])
239 +
240 + def test_inputBoxNegativeX(self):
241 + '''Test when negative x value is given in the box parameter'''
242 + retValue = tryInput(imagename=image_file,
243 + box='-3,0,-3,3',
244 + errormsg='\nInvalid box parameter, x=-3, values not detected.')
245 + self.assertTrue(retValue['success'], retValue['error_msgs'])
246 +
247 + def test_inputBoxLargeRangeX(self):
248 + '''Test with large values for x in the box'''
249 + retValue = tryInput(imagename=image_file,
250 + box='200,0,262,3',
251 + errormsg='Invalid box parameter values,262, not detected.')
252 +
253 + self.assertTrue(retValue['success'], retValue['error_msgs'])
254 +
255 + def test_inputBoxNegativeY(self):
256 + '''Test when negative y value is given in the box parameter'''
257 + retValue = tryInput(imagename=image_file,
258 + box='0,-3,0,3',
259 + errormsg='Invalid box parameter value, y=-3, not detected.')
260 +
261 + self.assertTrue(retValue['success'], retValue['error_msgs'])
262 +
263 + def test_inputLargeRangeY(self):
264 + '''Test with large values of y in the box'''
265 + retValue = tryInput(imagename=image_file,
266 + box='0,270,0,3',
267 + errormsg='Invalid box parameter value, y=270, not detected.')
268 +
269 + self.assertTrue(retValue['success'], retValue['error_msgs'])
270 +
271 + def test_inputBoxLargeY0(self):
272 + '''Test when the first y value in the box is larger'''
273 + retValue = tryInput(imagename=image_file,
274 + box='0,110,0,10',
275 + errormsg='Invalid box parameter value, y[1]>y[0], not detected.')
276 +
277 + self.assertTrue(retValue['success'], retValue['error_msgs'])
278 +
279 + def test_inputBox(self):
280 + '''Test with valid box inputs'''
281 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
282 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
283 +
284 + ###########################################################
285 + # Image name tests
286 + info('Performing input/output tests on imagename, errors WILL occur.')
287 + results = None
229 288 try:
230 - results = imval( imagename=image_file, box="1,2,3,4" )
289 + results = imval(imagename=image_file, box="1,2,3,4")
231 290 except:
232 - retValue['success']=False
233 - retValue['error_msgs']=retValue['error_msgs'] \
234 - +'Valid box parameter values caused an error.'
235 -
236 - if ( results == None ):
237 - retValue['success']=False
238 - retValue['error_msgs']=retValue['error_msgs']\
239 - +"\nError: Valid box test, box=[1,2,3,4], failed."
240 - del results
241 -
291 + retValue['success'] = False
292 + retValue['error_msgs'] = retValue['error_msgs'] \
293 + + 'Valid box parameter values caused an error.'
294 +
295 + if (results == None):
296 + retValue['success'] = False
297 + retValue['error_msgs'] = retValue['error_msgs'] \
298 + + "\nError: Valid box test, box=[1,2,3,4], failed."
299 + del results
300 +
242 301 ##############################################################
243 302 # CHANS parameter testing
244 - info( 'Performing input/output tests on "chans", errors WILL occur.' )
245 - result=None
246 - try:
247 - results = imval( imagename=image_file, chans='-3' )
248 - except:
249 - pass
250 - else:
251 - if ( results!=None \
252 - and ( (isinstance(results,bool) and results==True )\
253 - or (isinstance(results,dict) and results!={} ) ) ):
254 - retValue['success']=False
255 - retValue['error_msgs']=retValue['error_msgs'] \
256 - +'Invalid chans parameter value,-3, not detected.'
257 -
258 - resutls=None
259 - try:
260 - results = imval( imagename=image_file, chans='50' )
261 - except:
262 - pass
263 - else:
264 - if ( results!=None \
265 - and ( (isinstance(results,bool) and results==True )\
266 - or (isinstance(results,dict) and results!={} ) ) ):
267 - retValue['success']=False
268 - retValue['error_msgs']=retValue['error_msgs'] \
269 - +'Invalid chans parameter value,50, not detected.'
270 -
271 - results=None
303 + info('Performing input/output tests on "chans", errors WILL occur.')
304 + results = None
305 + self.assertTrue(retValue['success'], retValue['error_msgs'])
306 +
307 + def test_inputNegativeChans(self):
308 + '''Test when the given channels are negative'''
309 + retValue = tryInput(imagename=image_file,
310 + chans='-3',
311 + errormsg='Invalid chans parameter value,-3, not detected.')
312 +
313 + self.assertTrue(retValue['success'], retValue['error_msgs'])
314 +
315 + def test_inputChans50(self):
316 + '''Test out of range channel value'''
317 + retValue = tryInput(imagename=image_file,
318 + chans='50',
319 + errormsg='Invalid chans parameter value,50, not detected.')
320 +
321 + self.assertTrue(retValue['success'], retValue['error_msgs'])
322 +
323 + def test_inputChans10(self):
324 + '''Test valid channel input'''
325 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
326 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
327 +
328 + ###########################################################
329 + # Image name tests
330 + info('Performing input/output tests on imagename, errors WILL occur.')
331 + results = None
272 332 try:
273 - results = imval( imagename=image_file, chans="10" )
333 + results = imval(imagename=image_file, chans="10")
274 334 except:
275 - retValue['success']=False
276 - retValue['error_msgs']=retValue['error_msgs'] \
277 - +'Valid chans parameter value caused an error.'
278 -
279 - if ( results == None ):
280 - retValue['success']=False
281 - retValue['error_msgs']=retValue['error_msgs']\
282 - +"\nError: Valid channel test, chans='10', failed."
335 + retValue['success'] = False
336 + retValue['error_msgs'] = retValue['error_msgs'] \
337 + + 'Valid chans parameter value caused an error.'
338 +
339 + if (results == None):
340 + retValue['success'] = False
341 + retValue['error_msgs'] = retValue['error_msgs'] \
342 + + "\nError: Valid channel test, chans='10', failed."
283 343 del results
284 -
285 -
344 +
286 345 ###############################################################
287 346 # STOKES parameter testing
288 - info( 'Performing input/output tests on "stokes", errors WILL occur.' )
289 - results=None
290 - try:
291 - results = imval( imagename=image_file, stokes='Q' )
292 - except:
293 - pass
294 - else:
295 - if ( results!=None \
296 - and ( (isinstance(results,bool) and results==True )\
297 - or (isinstance(results,dict) and results!={} ) ) ):
298 - retValue['success']=False
299 - retValue['error_msgs']=retValue['error_msgs'] \
300 - +'Invalid stokes value, Q, not detected.'
301 -
302 - results=None
303 - try:
304 - results = imval( imagename=image_file, stokes=0 )
305 - except:
306 - pass
307 - else:
308 - if ( results!=None \
309 - and ( (isinstance(results,bool) and results==True )\
310 - or (isinstance(results,dict) and results!={} ) ) ):
311 - retValue['success']=False
312 - retValue['error_msgs']=retValue['error_msgs'] \
313 - +'Invalid stokes value, 0, not detected.'
314 - results=None
347 + info('Performing input/output tests on "stokes", errors WILL occur.')
348 + results = None
349 + self.assertTrue(retValue['success'], retValue['error_msgs'])
350 +
351 + def test_inputStokesQ(self):
352 + '''Test with stokes Q'''
353 + retValue = tryInput(imagename=image_file,
354 + stokes='Q',
355 + errormsg='Invalid stokes value, Q, not detected.')
356 +
357 + self.assertTrue(retValue['success'], retValue['error_msgs'])
358 +
359 + def test_inputStokes0(self):
360 + '''Test with 0 given as stokes value'''
361 + retValue = tryInput(imagename=image_file,
362 + stokes=0,
363 + errormsg='Invalid stokes value, 0, not detected.')
364 +
365 + self.assertTrue(retValue['success'], retValue['error_msgs'])
366 +
367 + def test_inputStokesI(self):
368 + '''Test with valid stokes input I'''
369 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
370 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
371 +
372 + ###########################################################
373 + # Image name tests
374 + info('Performing input/output tests on imagename, errors WILL occur.')
375 + results = None
315 376 try:
316 - results = imval( imagename=image_file, stokes='I' )
377 + results = imval(imagename=image_file, stokes='I')
317 378 except:
318 - retValue['success']=False
319 - retValue['error_msgs']=retValue['error_msgs'] \
320 - +'Valid stokes value, I, caused errors.'
321 -
322 - if ( results == None ):
323 - retValue['success']=False
324 - retValue['error_msgs']=retValue['error_msgs']\
325 - +"\nError: Valid stokes, 'I', test failed on file ."\
326 - +image_file+"\nRESULTS: "+str(results)
379 + retValue['success'] = False
380 + retValue['error_msgs'] = retValue['error_msgs'] \
381 + + 'Valid stokes value, I, caused errors.'
382 +
383 + if (results == None):
384 + retValue['success'] = False
385 + retValue['error_msgs'] = retValue['error_msgs'] \
386 + + "\nError: Valid stokes, 'I', test failed on file ." \
387 + + image_file + "\nRESULTS: " + str(results)
327 388 del results
328 -
389 +
329 390 ########################################
330 391 # REGION parameter tests
331 - info( 'Performing input/output tests on "region", errors WILL occur.' )
332 - results=None
333 - try:
334 - results = imval( imagename=image_file, region=[1,3] )
335 - except:
336 - pass
337 - else:
338 - if ( results!=None \
339 - and ( (isinstance(results,bool) and results==True )\
340 - or (isinstance(results,dict) and results!={} ) ) ):
341 - retValue['success']=False
342 - retValue['error_msgs']=retValue['error_msgs']\
343 - +"\nError: Bad region, '[1, 3]', was not reported."
344 -
345 -
392 + info('Performing input/output tests on "region", errors WILL occur.')
393 + results = None
394 + self.assertTrue(retValue['success'], retValue['error_msgs'])
395 +
396 + def test_inputRegion(self):
397 + '''Test when given region array'''
398 + retValue = tryInput(imagename=image_file,
399 + region=[1, 3],
400 + errormsg="\nError: Bad region, '[1, 3]', was not reported.")
401 +
402 + self.assertTrue(retValue['success'], retValue['error_msgs'])
403 +
404 + def test_inputRegionFile(self):
405 + '''Test when given a bad region file'''
346 406 # First make sure the region file does not exist.
347 - garbage_rgn_file = os.getcwd()+'/garbage.rgn'
348 - if ( os.path.exists( garbage_rgn_file )):
349 - os.remove( garbage_rgn_file )
350 -
351 - try:
352 - results = imval( imagename=image_file, \
353 - region=garbage_rgn_file )
354 - except:
355 - #We want this to fail
356 - no_op = 'noop'
357 - else:
358 - if ( results!=None \
359 - and ( (isinstance(results,bool) and results==True )\
360 - or (isinstance(results,dict) and results!={} ) ) ):
361 - retValue['success']=False
362 - retValue['error_msgs']=retValue['error_msgs']\
363 - + "\nError: Bad region file, 'garbage.rgn', was NOT "\
364 - + "reported as missing."
365 -
407 + garbage_rgn_file = 'garbage.rgn'
408 + if (os.path.exists(garbage_rgn_file)):
409 + os.remove(garbage_rgn_file)
410 +
411 + retValue = tryInput(imagename=image_file,
412 + region=garbage_rgn_file,
413 + errormsg="\nError: Bad region file, 'garbage.rgn', was NOT " \
414 + + "reported as missing.")
415 +
416 + self.assertTrue(retValue['success'], retValue['error_msgs'])
417 +
418 + def test_inputModifiedRegionFile(self):
419 + '''Test with modified bad region file'''
420 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
421 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
422 +
423 + ###########################################################
424 + # Image name tests
425 + info('Performing input/output tests on imagename, errors WILL occur.')
426 + results = None
366 427 try:
367 - rgn_file = os.getcwd()+'garbage.rgn'
368 - fp=open( rgn_file, 'w' )
428 + rgn_file = 'garbage.rgn'
429 + fp = open(rgn_file, 'w')
369 430 fp.writelines('This file does NOT contain a valid CASA region specification\n')
370 431 fp.close()
371 432 except:
372 - retValue['success']=False
373 - retValue['error_msgs']=retValue['error_msgs']\
374 - +"\nError: Unable to create bad region file.\n\t"
433 + retValue['success'] = False
434 + retValue['error_msgs'] = retValue['error_msgs'] \
435 + + "\nError: Unable to create bad region file.\n\t"
375 436 raise
376 -
377 -
378 -
379 437 try:
380 438 results = imval( imagename=image_file, region=rgn_file )
381 439 except:
382 440 no_op='noop'
383 441 else:
384 442 if ( results!=None \
385 443 and ( (isinstance(results,bool) and results==True )\
386 444 or (isinstance(results,dict) and results!={} ) ) ):
387 445 retValue['success']=False
388 446 retValue['error_msgs']=retValue['error_msgs']\
389 447 + "\nError: Bad region file, 'garbage.rgn',"\
390 448 + " was not reported as bad."
391 -
392 -
393 - results=None
449 + results = None
450 + self.assertTrue(retValue['success'], retValue['error_msgs'])
451 +
452 + def test_inputGoodRegionFile(self):
453 + '''Test region seleciton with good region file'''
454 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
455 + note("Starting imval INPUT/OUTPUT tests.", 'NORMAL2')
456 +
457 + ###########################################################
458 + # Image name tests
459 + info('Performing input/output tests on imagename, errors WILL occur.')
460 + results = None
394 461 try:
395 462 results=imval( imagename=image_file, region=good_rgn_file )
396 463 except:
397 464 retValue['success']=False
398 465 retValue['error_msgs']=retValue['error_msgs']\
399 466 +"\nError: Unable to get image values in region "\
400 467 +" specified by file, "+good_rgn_file
401 468 if ( results == None or results==False ):
402 469 retValue['success']=False
403 470 retValue['error_msgs']=retValue['error_msgs']\
404 471 +"\nError: Valid region file, "+good_rgn_file\
405 472 +" tset has failed."\
406 473 +"\nRESULTS: "+str(results)
407 - del results
408 -
409 - self.assertTrue(retValue['success'],retValue['error_msgs'])
474 + results = None
475 + self.assertTrue(retValue['success'], retValue['error_msgs'])
410 476
411 477 ###########################################################################
412 478 # NAME: single_point
413 479 #
414 480 # SHORT DESCRIPTION: Do tests to find the value at a single point
415 481 #
416 482 # DESCRIPTION:
417 483 # a) Value at bottom-left corner
418 484 # b) Value at bottom-right corner
419 485 # c) Value at top-left corner
420 486 # d) Value at top-right corner
421 487 # e) Value at 3 points within the image.
422 488 #
423 489 ############################################################################
424 -
425 - def test_single_point(self):
426 - '''Imval: Single point tests'''
427 - retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
428 - note( "Starting SINGLE POINT tests.", 'NORMAL2' )
429 -
430 - # Find the min/max points of the image.
431 - bbox={}
432 - try:
433 - _ia.open( image_file )
434 - bbox=_ia.boundingbox()
435 - _ia.done()
436 - except:
437 - retValue['success']=False
438 - retValue['error_msgs']=retValue['error_msgs']\
439 - +"\nError: Unable to find size of image "+image_file
440 -
441 - dir_blc=[]
442 - dir_trc=[]
443 - min_chan=max_chan=-1
444 - min_stokes=max_stokes=-1
445 - if ( len(bbox) > 0 and 'blc' in bbox and 'trc' in bbox ):
446 - blc=bbox['blc']
447 - trc=bbox['trc']
448 -
449 - dir_blc=[blc[0], blc[1]]
450 - dir_trc=[trc[0], trc[1]]
451 - min_chan=blc[3]
452 - max_chan=trc[3]
453 - min_Stokes=blc[2]
454 - max_stokes=trc[2]
455 -
456 - error_margin=0.00001
457 -
458 -
459 - #############################################################
460 - # Bottom-left
461 - tbox=str(dir_blc[0])+','+str(dir_blc[1])+','+str(dir_blc[0])+','\
462 - +str(dir_blc[1])
463 - msg="Bottom left corner value was Not Found"
464 - results=None
465 - try:
466 - results=imval( imagename=image_file, box=tbox, chans=str(min_chan), \
467 - stokes=str(min_stokes) )
468 - except Exception:
469 - retValue['success']=False
470 - retValue['error_msgs']=retValue['error_msgs']\
471 - +"\nError: Failed to get the value in the bottom left"\
472 - +" corner, "+tbox+"."
473 - else:
474 - if ( results!=None and 'blc' in results \
475 - and 'data' in results and 'unit' in results\
476 - and 'mask' in results ):
477 - msg='Bottom left corner valus is, '+str(results['blc'])\
478 - +', value is: '+str(results['data'])+str(results['unit'])\
479 - +' with mask '+str(results['mask'])
480 - if ( results==None or 'data' not in results \
481 - or 'data' not in results or \
482 - ( results['data']+1.035184e-09>error_margin or not results['mask']) ):
483 - retValue['success']=False
484 - retValue['error_msgs']=retValue['error_msgs']\
485 - +"\nError: Expected 1.035184e-09Jy/Beam with mask=True."\
486 - +"\n\t"+msg
487 -
488 - #############################################################
489 - # Bottom-right
490 - tbox=str(dir_trc[0])+','+str(dir_blc[1])+','+str(dir_trc[0])+','\
490 +
491 +
492 + def test_singlePointBottomLeft(self):
493 + '''Test box selection at bottom left'''
494 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
495 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
496 +
497 + tbox = str(dir_trc[0])+','+str(dir_blc[1])+','+str(dir_trc[0])+','\
491 498 +str(dir_blc[1])
492 - msg="Bottom right corner value was Not Found"
493 - results=None
494 - try:
495 - results=imval( imagename=image_file, box=tbox, chans=str(min_chan),\
496 - stokes=str(min_stokes) )
497 - except Exception:
498 - retValue['success']=False
499 - retValue['error_msgs']=retValue['error_msgs']\
500 - +"\nError: Failed to get the value in the bottom right"\
501 - +" corner. "+tbox+"."
502 - else:
503 - if ( results!=None and 'blc' in results \
504 - and 'data' in results and 'unit' in results\
505 - and 'mask' in results ):
506 - msg='Bottom right corner, '+str(results['blc'])+', value is: '\
507 - +str(results['data'])+str(results['unit'])\
508 - +' with mask '+str(results['mask'])
509 - if ( results==None or 'data' not in results or \
510 - ( results['data']+1.172165e-09 > 0.00001 or not results['mask'])):
511 - retValue['success']=False
512 - retValue['error_msgs']=retValue['error_msgs']\
513 - +'\nError: Expected value of -1.172165e-09 and mask=True'\
514 - +'\n\t'+msg
515 -
516 - ######################################################3
517 - # Top-left
518 - tbox=str(dir_blc[0])+','+str(dir_trc[1])+','+str(dir_blc[0])+','\
519 - +str(dir_trc[1])
520 - msg="Top left corner value was Not Found"
521 - results=None
522 -
523 - try:
524 - results=imval( imagename=image_file, box=tbox, chans=str(min_chan),
525 - stokes=str(min_stokes) )
526 - except Exception:
527 - retValue['success']=False
528 - retValue['error_msgs']=retValue['error_msgs']\
529 - +"\nError: Failed to get the value in the top left"\
530 - +" corner, "+tbox+"."
531 - else:
532 - if ( results!=None and 'blc' in results \
533 - and 'data' in results and 'unit' in results\
534 - and 'mask' in results ):
535 - msg='Top left corner, '+str(results['blc'])+', value is: '\
536 - +str(results['data'])+str(results['unit'])\
537 - +' with mask '+str(results['mask'])
538 - if ( results==None or 'data' not in results or \
539 - ( results['data']+4.2731923e-09>error_margin or not results['mask'])):
540 - retValue['success']=False
541 - retValue['error_msgs'] = retValue['error_msgs'] + "\nError: Expected value of -4.273192e-09, and mask=True" + "\n\t" + msg
542 -
543 - #############################################################
544 - # Top-right
545 - tbox=str(dir_trc[0])+','+str(dir_trc[1])+','+str(dir_trc[0])+','\
546 - +str(dir_trc[1])
547 - msg="Top right corner value was Not Found"
548 - results=None
499 +
500 + trySinglePoint(retValue,
501 + imagename=image_file,
502 + box=tbox,
503 + chans=str(min_chan),
504 + stokes=str(min_stokes),
505 + expected=1.035184e-09,
506 + errormsg="\nError: Expected 1.035184e-09Jy/Beam with mask=True."\
507 + +"\n\t"+"Bottom left corner value was Not Found")
508 +
509 + self.assertTrue(retValue['success'], retValue['error_msgs'])
510 +
511 + def test_singlePointBottomRight(self):
512 + '''Test box seleciton at bottom right'''
513 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
514 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
515 +
516 + tbox = str(dir_trc[0]) + ',' + str(dir_blc[1]) + ',' + str(dir_trc[0]) + ',' \
517 + + str(dir_blc[1])
518 +
519 + trySinglePoint(retValue,
520 + imagename=image_file,
521 + box=tbox,
522 + chans=str(min_chan),
523 + stokes=str(min_stokes),
524 + expected=1.172165e-09,
525 + errormsg='\nError: Expected value of -1.172165e-09 and mask=True'\
526 + +'\n\t'+"Bottom right corner value was Not Found")
527 +
528 + self.assertTrue(retValue['success'], retValue['error_msgs'])
529 +
530 + def test_singlePointTopLeft(self):
531 + '''Test box selection at top left'''
532 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
533 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
534 +
535 + tbox = str(dir_blc[0]) + ',' + str(dir_trc[1]) + ',' + str(dir_blc[0]) + ',' \
536 + + str(dir_trc[1])
537 +
538 + trySinglePoint(retValue,
539 + imagename=image_file,
540 + box=tbox,
541 + chans=str(min_chan),
542 + stokes=str(min_stokes),
543 + expected=4.2731923e-09,
544 + errormsg="\nError: Expected value of -4.273192e-09, and mask=True" + "\n\t" + "Top left corner value was Not Found")
545 +
546 + self.assertTrue(retValue['success'], retValue['error_msgs'])
547 +
548 + def test_singlePointTopRight(self):
549 + '''Test box selection at top right'''
550 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
551 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
552 +
553 + tbox = str(dir_trc[0]) + ',' + str(dir_trc[1]) + ',' + str(dir_trc[0]) + ',' \
554 + + str(dir_trc[1])
555 +
556 + trySinglePoint(retValue,
557 + imagename=image_file,
558 + box=tbox,
559 + chans=str(min_chan),
560 + stokes=str(min_stokes),
561 + expected=3.647830e-09,
562 + errormsg='\nError: Expected value -3.647830e-09Jy/Beam and mask=True'\
563 + +'\n\t'+"Top right corner value was Not Found")
564 + self.assertTrue(retValue['success'], retValue['error_msgs'])
565 +
566 +
567 + def test_singlePointLastChanStokes(self):
568 + '''Test box seleciton with max stokes and chans'''
569 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
570 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
571 +
572 + tbox = str(dir_trc[0]) + ',' + str(dir_trc[1]) + ',' + str(dir_trc[0]) + ',' + \
573 + str(dir_trc[1])
574 +
575 + trySinglePoint(retValue,
576 + imagename=image_file,
577 + box=tbox,
578 + chans=str(max_chan),
579 + stokes=str(max_stokes),
580 + expected=-3.55266e-10,
581 + errormsg='\nError: Expected value -3.647830e-09Jy/Beam and'\
582 + +' mask=True \n\t'+"Value NOT found when looking at last chanel and last stokes")
583 + self.assertTrue(retValue['success'], retValue['error_msgs'])
584 +
585 + def test_singlePointRandom(self):
586 + '''Test selection a single random point'''
587 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
588 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
589 +
590 + # A couple of not so random points
591 + tbox = str(int(dir_trc[0] * 2 / 3)) + ',' + str(int(dir_trc[1] * 2 / 3)) + ',' \
592 + + str(int(dir_trc[0] * 2 / 3)) + ',' + str(int(dir_trc[1] * 2 / 3))
593 + msg = "Value NOT found when looking at first random point," + tbox + "."
594 + results = None
595 +
549 596 try:
550 - results=imval( imagename=image_file, box=tbox, chans=str(min_chan),\
551 - stokes=str(min_stokes) )
597 + results = imval(imagename=image_file, box=tbox, \
598 + chans=str(int(max_chan * 2 / 3)), stokes=str(max_stokes))
552 599 except Exception:
553 - retValue['success']=False
554 - retValue['error_msgs']=retValue['error_msgs']\
555 - +"\nError: Failed to get the value in the top right"\
556 - +" corner. "+tbox+"."
600 + retValue['success'] = False
601 + retValue['error_msgs'] = retValue['error_msgs'] \
602 + + "\nError: " + msg
557 603 else:
558 - if ( results!=None and 'blc' in results \
559 - and 'data' in results and 'unit' in results\
560 - and 'mask' in results ):
561 - msg='Top right corner, '+str(results['blc'])+', value is: '\
562 - +str(results['data'])+str(results['unit'])\
563 - +' with mask '+str(results['mask'])
564 - if ( results==None or 'data' not in results or \
565 - (results['data']+3.647830e-09>error_margin or not results['mask'])):
566 - retValue['success']=False
567 - retValue['error_msgs']=retValue['error_msgs']\
568 - +'\nError: Expected value -3.647830e-09Jy/Beam and mask=True'\
569 - +'\n\t'+msg
570 -
571 - #########################################################3
572 - # Last channel and stokes
573 - tbox=str(dir_trc[0])+','+str(dir_trc[1])+','+str(dir_trc[0])+','+\
574 - str(dir_trc[1])
575 - msg="Value NOT found when looking at last chanel and last stokes"
576 - results=None
577 -
604 + if (results != None and 'blc' in results \
605 + and 'data' in results and 'unit' in results \
606 + and 'mask' in results):
607 + msg = 'Value found at' + str(results['blc']) + ' is: ' \
608 + + str(results['data']) + str(results['unit']) \
609 + + '. with mask ' + str(results['mask'])
610 + if (results == None or 'data' not in results or \
611 + (results['data'] - 0.062294 > error_margin)):
612 + retValue['success'] = False
613 + retValue['error_msgs'] = retValue['error_msgs'] \
614 + + '\nError: Expected value of 0.062294Jy/Beam and mask=True' \
615 + + '\n\t' + msg
616 +
617 + self.assertTrue(retValue['success'], retValue['error_msgs'])
618 +
619 + def test_singlePointSecondRandom(self):
620 + '''Test selecting another single random point'''
621 + retValue = {'success': True, 'msgs': "", 'error_msgs': ''}
622 + dir_blc, dir_trc, min_chan, max_chan, min_stokes, max_stokes, error_margin = setUpImage(retValue)
623 +
624 + # Second random point
625 + tbox = str(int(dir_trc[0] * 1 / 6)) + ',' + str(int(dir_trc[1] * 2 / 6)) + ',' \
626 + + str(int(dir_trc[0] * 1 / 6)) + ',' + str(int(dir_trc[1] * 2 / 6))
627 + msg = "Value NOT found when looking at second random point," + tbox + "."
628 + results = None
629 +
578 630 try:
579 - results=imval( imagename=image_file, box=tbox, chans=str(max_chan), \
580 - stokes=str(max_stokes) )
631 + results = imval(imagename=image_file, box=tbox, \
632 + chans=str(int(max_chan * 5 / 6)), stokes=str(max_stokes))
581 633 except Exception:
582 - retValue['success']=False
583 - retValue['error_msgs']=retValue['error_msgs']\
584 - +"\nError: Failed to get the value at the last channel "\
585 - +" and last stokes, "+tbox+"."
634 + retValue['success'] = False
635 + retValue['error_msgs'] = retValue['error_msgs'] \
636 + + "\nError: " + msg
586 637 else:
587 - if ( results!=None and 'blc' in results \
588 - and 'data' in results and 'unit' in results\
589 - and 'mask' in results ):
590 - msg='Value found at'+str(results['blc'])+' is: '\
591 - +str(results['data'])+str(results['unit'])\
592 - +'. with mask '+str(results['mask'])
593 - if ( results==None or 'data' not in results or \
594 - ( results['data']-3.55266e-10 > error_margin ) ):
595 - retValue['success']=False
596 - retValue['error_msgs']=retValue['error_msgs']\
597 - +'\nError: Expected value -3.647830e-09Jy/Beam and'\
598 - +' mask=True \n\t'+msg
599 -
600 - #######################################################
601 - # A couple of not so random points
602 - tbox=str(int(dir_trc[0]*2/3))+','+str(int(dir_trc[1]*2/3))+','\
603 - +str(int(dir_trc[0]*2/3))+','+str(int(dir_trc[1]*2/3))
604 - msg="Value NOT found when looking at first random point,"+tbox+"."
605 - results=None
606 -
607 - try:
608 - results=imval( imagename=image_file, box=tbox, \
609 - chans=str(int(max_chan*2/3)), stokes=str(max_stokes) )
610 - except Exception:
611 - retValue['success']=False
612 - retValue['error_msgs']=retValue['error_msgs']\
613 - +"\nError: "+msg
614 - else:
615 - if ( results!=None and 'blc' in results \
616 - and 'data' in results and 'unit' in results\
617 - and 'mask' in results ):
618 - msg='Value found at'+str(results['blc'])+' is: '\
619 - +str(results['data'])+str(results['unit'])\
620 - +'. with mask '+str(results['mask'])
621 - if ( results==None or 'data' not in results or \
622 - ( results['data']-0.062294 > error_margin ) ):
623 - retValue['success']=False
624 - retValue['error_msgs']=retValue['error_msgs']\
625 - +'\nError: Expected value of 0.062294Jy/Beam and mask=True'\
626 - +'\n\t'+msg
627 -
628 - # Second random point
629 - tbox=str(int(dir_trc[0]*1/6))+','+str(int(dir_trc[1]*2/6))+','\
630 - +str(int(dir_trc[0]*1/6))+','+str(int(dir_trc[1]*2/6))
631 - msg="Value NOT found when looking at second random point,"+tbox+"."
632 - results=None
633 -
634 - try:
635 - results=imval( imagename=image_file, box=tbox, \
636 - chans=str(int(max_chan*5/6)), stokes=str(max_stokes) )
637 - except Exception:
638 - retValue['success']=False
639 - retValue['error_msgs']=retValue['error_msgs']\
640 - +"\nError: "+msg
641 - else:
642 - if ( results!=None and 'blc' in results \
643 - and 'data' in results and 'unit' in results\
644 - and 'mask' in results ):
645 - msg='Value found at'+str(results['blc'])+' is: '\
646 - +str(results['data'])+str(results['unit'])\
647 - +'. with mask '+str(results['mask'])
648 -
649 - if ( results==None or 'data' not in results or \
650 - ( results['data']+0.070744 > error_margin ) ):
651 - retValue['success']=False
652 - retValue['error_msgs']=retValue['error_msgs']\
653 - +'Error: Expected value of -0.070744Jy/Beam and '\
654 - +'mask=True'+'\n\t'+msg
655 -
656 - self.assertTrue(retValue['success'],retValue['error_msgs'])
638 + if (results != None and 'blc' in results \
639 + and 'data' in results and 'unit' in results \
640 + and 'mask' in results):
641 + msg = 'Value found at' + str(results['blc']) + ' is: ' \
642 + + str(results['data']) + str(results['unit']) \
643 + + '. with mask ' + str(results['mask'])
644 +
645 + if (results == None or 'data' not in results or \
646 + (results['data'] + 0.070744 > error_margin)):
647 + retValue['success'] = False
648 + retValue['error_msgs'] = retValue['error_msgs'] \
649 + + 'Error: Expected value of -0.070744Jy/Beam and ' \
650 + + 'mask=True' + '\n\t' + msg
651 +
652 + self.assertTrue(retValue['success'], retValue['error_msgs'])
657 653
658 654 ###########################################################################
659 655 # NAME: arrays
660 656 #
661 657 # SHORT DESCRIPTION: Do tests to find the value at a single point
662 658 #
663 659 # DESCRIPTION:
664 660 # a) A slice of the directional plane
665 661 # b) Two slices of the directional plane
666 662 # c) A cube RA,Dec,and Spectral axes
856 852 imagename = "mypv.im"
857 853 myia.fromshape("", [20,20,20])
858 854 myia.addnoise()
859 855 pv = myia.pv(outfile=imagename, start=[3,3], end=[17,17])
860 856 myia.done()
861 857 expec = pv.getchunk()
862 858 pv.done()
863 859 res = imval(imagename)
864 860 got = res['data']
865 861 self.assertTrue((got == expec).all())
866 -
862 +
867 863 if __name__ == '__main__':
868 864 unittest.main()

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut