"""Contains SpecCase used by commissioning_cases.CommCase*. Defines all the common setup and testing. """ import os import numpy as N import pysynphot as S from pysynphot import etc #For thermal classes only from pysynphot.observationmode import ObservationMode #BUG: find a better way HERE = os.getcwd() DATADIR = os.path.join(os.path.dirname(HERE), 'data') #TODO: set a specified graph/comp/therm table set in a module setup class SpecCase(object): @classmethod def setUpClass(cls): """Always overridden by the child cases, but let's put some real values in here to test with""" self.obsmode=None self.spectrum=None self.bp=None self.sp=None self.obs=None self.setup2() @classmethod def setup2(cls): #Do the common setup here. cls.sigthresh = 0.01 cls.thresh = 0.01 cls.tda=dict(obsmode=cls.obsmode, spectrum=cls.spectrum, thresh=cls.thresh, sigthresh=cls.sigthresh) cls.tra=dict() if cls.obsmode != "None": cls.bp=S.ObsBandpass(cls.obsmode) cls.bp.writefits(cls.fname%'thru', clobber=True, trimzero=False) cls.tra['thru']=cls.bp.name else: cls.bp = None if cls.spectrum != "None": #All the data lives in a parallel directory, so go sit there #in case we need a file os.chdir(DATADIR) cls.sp=etc.parse_spec(cls.spectrum) os.chdir(HERE) cls.sp.writefits(cls.fname%'spec', clobber=True, trimzero=False) cls.tra['sp']=cls.sp.name else: cls.sp = None if "None" not in (cls.obsmode, cls.spectrum): cls.obs = S.Observation(cls.sp, cls.bp) cls.obs.convert('counts') x = dict(PSCNTRAT = (cls.obs.countrate(),'countrate'), PSEFFLAM = (cls.obs.efflam(),'efflam')) cls.obs.writefits(cls.fname%'obs', hkeys=x, clobber=True, trimzero=False) cls.tra['obs']=cls.obs.name else: cls.obs = None #Helper methods for arrays def count_outliers(self,Nsigma=3): mean=self.adiscrep.mean() std=self.adiscrep.std() outliers=N.where(abs(self.adiscrep) > mean + Nsigma*std) return len(outliers[0]) def arraysigtest(self,ref,test): #Raise an error if the arrays are not the same size if test.shape != ref.shape: raise ValueError("Array size mismatch") tt=test[2:-2] rr=ref[2:-2] #Identify the significant elements tidx=N.where(tt>(self.sigthresh*tt.max()))[0] ridx=N.where(rr>(self.sigthresh*rr.max()))[0] #Set a flag if they're not the same set if not (N.alltrue(tidx == ridx)): self.tra['SigElemDiscrep']=True tidx=ridx #Now compare only the significant elements. #We no longer need to exclude points with zero value, because #those points were already excluded as insignificant. self.arraytest(tt[ridx],rr[ridx]) def arraydiff(self,test,ref): idx=N.nonzero(ref) ans=(test[idx]-ref[idx])/ref[idx] return ans def arraytest(self,ref,test): self.adiscrep=self.arraydiff(test,ref) count=N.where(abs(self.adiscrep)>self.thresh)[0].size try: self.tra['Discrepfrac']=float(count)/self.adiscrep.size self.tra['Discrepmin']=self.adiscrep.min() self.tra['Discrepmax']=self.adiscrep.max() self.tra['Discrepmean']=self.adiscrep.mean() self.tra['Discrepstd']=self.adiscrep.std() self.tra['Outliers']=self.count_outliers(5) self.failUnless(N.alltrue(abs(self.adiscrep)