from main.mcmc.output.Output import Output
from numpy.ma.core import mean, array
import time

class StatisticsOutput(Output):
    def __init__(self, print_from=1000, lag=1000):
        Output.__init__(self)
        
        self.print_from = print_from
        self.lag = lag
        
        # for time estimates
        self.times = []
        self.times.append(time.time())
    
    def update(self, mcmc_chain, step_output):
        i = mcmc_chain.iteration
        if i >= self.print_from and i % self.lag == 0:
            self.times.append(time.time())
            
            print "mean(mcmc_chain.accepteds[0:" + str(i) + "]:", \
                  mean(mcmc_chain.accepteds[0:i])
            
            print int(self.get_percent_done(i, mcmc_chain.mcmc_params.num_iterations)), \
            "\tpercent done in ", int(round(sum(array(self.times) - self.times[0]))), \
            "seconds. ETA:", self.get_estimated_time_remaining(i, mcmc_chain.mcmc_params.num_iterations)
         
    def prepare(self):
        pass

    def get_percent_done(self, iteration, num_iterations):
        return float(iteration) / num_iterations * 100

    def get_estimated_time_remaining(self, iteration, num_iterations):
        percent_done = self.get_percent_done(iteration, num_iterations)
        eta = sum(array(self.times) - self.times[0]) / percent_done * (100 - percent_done)
        return int(round(eta))

@@ -897,16 +897,107 @@
 econds. 
+Since last update:%22, int(round(self.times%5B-1%5D-self.times%5B-2%5D)), %22seconds.%22, %5C%0A            %22
 ETA:%22, s
@@ -1062,32 +1062,43 @@
 .num_iterations)
+, %22seconds%22
 %0A         %0A    d

update for timer statistics