Exemple #1
0
 def __call__(self,Ntrain):
   d = loadSimulation(self.source)
   H = eval(d[0])
   
   # Check if a kalman filter exists, if not create one
   kalFile = self.noExtSource+"_kal.py"
   if isfile(kalFile):
     with open(kalFile,'r') as f:
       kal = eval(f.read())
   else:
     kal=kalman(d[-2].T)
     with open(kalFile,'w') as f:
       f.write(repr(kal))
   
   # Load data generated from same system
   self.sims = list()
   for f in self.fles:
     if f[-7:]==".pkl.gz" and f[-13:]!='_cache.pkl.gz':
       try:
         s = loadSimulation(osjoin(self.sourceDir,f))
         if eval(s[0]).spec==H.spec:
           self.sims.append(s)
       except EOFError:
         print "Failed to open", f, "skipping."
   
   # Grab the trials from the loaded data
   x,y = zip(*[(s[2],s[-2]) for s in self.sims])
   trials = Trials(x,y,Ntrain)
   trials.initialise(kal,self.pca,self.fd)
   return(DataSet(H,kal,trials,self.source,self.psfidx))
  # baseline in kTran and the scaling factors kx and kv
  D = y.shape[1]
  kal.C = array(kTran)
  kal.C[:D,:D]*= kx
  kal.C[D:,D:]*= kv
  K = kal.filter(flipud(y).T,zeros(2*D),sigma0*eye(2*D))
  ix = K[0][:,-1]
  iV = 2 * K[1][:,:,-1]
  ny = kal.smooth(y.T,ix,iV)[0].T
  f = fft.fftfreq(y.shape[0])
  ps = abs(fft.fft(ny.T).T)
  f,ps = welch(ny.T,1.0/D)
  return(kal,f,ps.T,ny)

# Global constants
glbData.kal =  kalman(y.T)
glbData.kTran = array(glbData.kal.C)
kx0 = 1.0
kv0 = 1.0
score = nan

glbData.kal,f,ps,ny = kalSpectrum(y,glbData.kTran,kx0,kv0,glbData.kal)
score = sum((ny[:,:y.shape[1]]-y)**2)

fig, (ax0,ax1,ax2) = subplots(1,3)
subplots_adjust(left=0.1, bottom=0.25)
glbData.l = ax0.plot(f,ps, lw=2, color='red')
glbData.m = ax1.plot(ny, lw=2, color='blue')
glbData.n, = ax2.plot(*PCA(zscore(ny)).T[:2])
ax0.set_yscale('log')
scoreT = text(0, 0, str(score))