def evaluate(self,y,s=None):
     if s is  None:
         return np.asscalar(self.Loss_(self.Feedforward_(self.times,self.n,self.theta),y))
     else:
         if np.isscalar(s):
             self.S__.set_value(s)
             return np.asscalar(self.Loss_(self.Feedforward_(self.times,self.n,self.theta),y))
         else:
             res=[]
             for si in s:
                 self.S__.set_value(si)
                 res.append((si,np.asscalar(self.Loss_(self.Feedforward_(self.times,self.n,self.theta),y))))
             return pd.DataFrame(res,columns=['s','loss'])
    def fit(self,winidx,windowIndex=None,filterAfterDrop=True,linesearchTheta=False,YslackLineSearch=False):
        if windowIndex is None:
            y=self.sim.getAverageHAF(self.sim.winIdx[winidx])
        else:
            y=self.sim.getAverageHAF(windowIndex)
        self.times= self.Times
        if filterAfterDrop:
            self.lastGenerationIndex = self.sim.filterTimeSamplesWithHighNegDer(y)
        else:
            self.lastGenerationIndex=(np.ones(self.numReplicates)*self.times.shape[0]).astype(int)-1
        self.y=y.values
        self.reset()
        if YslackLineSearch: self.setInitYslackViaSettingInitObservation()
        self.setInitSviaLineSearch()
        
        start_time=time.time()
        if self.verbose>2:
            print 'y:\n{},times:\n{}\nn:{},\ttheta:{}\tlastGenIDX:{}\tRepIDX:{}'.format(self.y,self.times,self.n,self.Theta__.get_value(),self.lastGenerationIndex,self.replicateIndex)
        self.obj=float(self.Loss_(self.y,self.times,self.n,self.lastGenerationIndex,self.replicateIndex))
        if self.verbose>1:      print 'Before\nIter,\tobj,\ts,\ttheta,\tYslack,\tnu\n','{}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{}'.format(0,self.obj ,float(self.S__.get_value()),float(self.Theta__.get_value()) ,float(self.Yslack__.get_value()),sig(self.c__.get_value()))
        for i in range(self.maxIter):
            self.saveState()
            self.obj=self.Objective_(self.y, self.lr_s, self.lr_nu, self.lr_Yslack, self.lr_theta, self.times, (self.final_momentum , self.initial_momentum)[i<5],self.n, self.lastGenerationIndex,self.replicateIndex)
            if self.verbose>1:  print '{}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{}'.format(i+1,float(self.obj) ,float(self.S__.get_value()),float(self.Theta__.get_value()) ,float(self.Yslack__.get_value()),sig(self.c__.get_value()))
            if self.obj>self.obj__prev:    
                self.undoStep()
                break
        s, nu0, slack,theta= np.asscalar(self.S__.get_value()), sig(self.c__.get_value()),np.asscalar(self.Yslack__.get_value()),np.asscalar(self.Theta__.get_value())
        obj=self.Loss_(self.y,self.times,self.n,self.lastGenerationIndex,self.replicateIndex)
        
        
        obj0=self.getZeroObj()
        negLogLikelihoodRatio=np.log(obj0)-np.log(obj)
        if s<0:
            negLogLikelihoodRatio=0
            s=0
#         if negLogLikelihoodRatio<0: negLogLikelihoodRatio=0
        self.sol=pd.Series({'s':s,'LR':negLogLikelihoodRatio,'Time':time.time()-start_time,'pos':self.sim.winMidPos[winidx],'nu0':nu0,'slack':slack,'obj':float(obj), 'obj0':float(obj0), 'lastTimes': self.lastGenerationIndex, 'y':self.y, 'theta':theta, 'n':self.n, 'winidx':winidx, 'SLR': np.exp(negLogLikelihoodRatio)*s, 'watterson':Estimate.watterson(self.sim.H0.iloc[:,self.sim.winIdx[winidx]]),'method':'HAF'})
        return self.sol
 def fit(self,y=None,pos=1,verbose=0):
     timesIDX=y.abs().sum(1)!=0
     self.y= y[timesIDX]
     self.times= self.Times[np.where(timesIDX.values)[0]]
     obj0=self.getZeroObj()
     if self.initSviaLineSearch:
         self.setInitYslackViaLineSearch()
         self.setInitSviaLineSearch()
     self.reset()
     start_time=time.time()
     for i in range(self.maxIter):
         obj=self.Objective_(self.y,self.lr_s, self.lr_nu, self.lr_theta, self.times, (self.final_momentum , self.initial_momentum)[i<5],self.n, self.theta)
         if verbose>1:
             print obj ,self.S__.get_value(), self.Yslack__.get_value(),sig(self.c__.get_value())
     if verbose:
         print obj ,self.S__.get_value(), self.Yslack__.get_value(),sig(self.c__.get_value())
         
     negLogLikelihoodRatio=np.log(obj0)-np.log(self.Loss_(self.y,self.Feedforward_(self.times,self.n,self.theta)))
     s=np.asscalar(self.S__.get_value())
     if s<1e-6:
         negLogLikelihoodRatio=0
         s=0
     self.sol=pd.Series({'s':s,'LR':negLogLikelihoodRatio,'Time':time.time()-start_time,'pos':pos,'nu0':sig(self.c__.get_value()),'slack':np.asscalar(self.Yslack__.get_value()),'obj':float(obj), 'obj0':float(obj0), 'times': self.times, 'y':self.y, 'theta':self.theta, 'n':self.n, 'smoothTimes': np.tile(np.arange(self.times[0][0] , self.times[-1][0]+1),(self.sim.numReplicates,1)).T})
     return self.sol
 def computeLoss(self,y,s=None,theta=None):
     if s is not  None:
         self.S__.set_value(s)
     if theta is not  None:
         self.Theta__.set_value(theta)
     return np.asscalar(self.Loss_(y,self.times,self.n,self.lastGenerationIndex,self.replicateIndex))