Source
##############################################################################
# #
# Test Name: #
# imagepoltest_regression.py #
# Rationale for Inclusion: #
# This is a Python translation of the Glish assay test #
# imagepolservertest.g It tests many imagepol tool methods. #
# The first test is a general test of everything at a basic level. #
# Succeeding tests work on individual areas of the imagepol tool. #
# Some of the tests included forced errors. As long as imagepoltest #
# finally returns True it has succeeded (regardless of what error #
# messages you might see). #
# Features Tested: #
# open, done, summary, stokesi, stokesq, stokesu, stokesv, stokes, #
# linpolint, linpolposang, totpolint, fraclinpol, fractotpol, pol, #
# sigmalinpolint, sigmalinpolposang, sigmatotpolint, stokes #
# sigmastokesi, sigmastokesq, sigmastokesu, sigmastokesv, sigmastokes #
# sigmafraclinpol, sigmafractotpol, imagepoltestimage, rotationmeasure #
# fourierrotationmeasure, complexlinpol, complexfraclinpol, makecomplex, #
# depolratio, sigmadepolratio #
# Success/failure criteria: #
# Internally tests each method for pass/fail. Throws an uncaught #
# exception ending test when an unexpected failure occurs. #
# All tests pass if script runs to completion. #
# #
##############################################################################
# #
# Converted by RRusk 2007-11-16 from imagepolservertest.g #
# #
##############################################################################
import os
import shutil
import time
import math
import random
from regression_utility import note
po = casac.imagepol()
def imagepoltest(which=None):
#
dowait = True
#
def info(message):
note(message, origin="imagepoltest")
def stop(message):
note(message, priority="SEVERE", origin="imagepoltest")
raise RuntimeError, message
def fail(message=""):
stop(message)
def cleanup(dir):
if (os.path.isdir(dir)):
info("Cleaning up directory "+dir)
def errFunc(raiser, problemPath, excInfo):
note(raiser.__name__+'failed on'+problemPath,"SEVERE")
raise RuntimeError, "Cleanup of " + dir + " fails!"
shutil.rmtree(dir,0,errFunc)
return True
def addnoise(data, sigma):
if len(data.shape)==3:
for i in range(data.shape[0]):
for j in range(data.shape[1]):
for k in range(data.shape[2]):
data[i][j][k] += random.normalvariate(0.0, sigma)
else:
stop('unhandled array shape '+str(data.shape)+' in addnoise')
return True
# 3D only
def make_data(imshape, stokes):
if imshape[2]>4: fail()
if imshape[2]!=len(stokes): fail()
data = ia.makearray(0, [imshape[0], imshape[1], imshape[2]])
for k in range(imshape[2]):
for i in range(imshape[0]):
for j in range(imshape[1]):
data[i][j][k] = stokes[k]
return data
def alleqnum(x,num,tolerance=0):
if len(x.shape)==3:
for i in range(x.shape[0]):
for j in range(x.shape[1]):
for k in range(x.shape[2]):
if not (abs(x[i][j][k]-num) < tolerance):
print "x[",i,"][",j,"][",k,"]=", x[i][j][k]
return False
else:
stop('unhandled array shape in alleqnum')