Skip to content

Instantly share code, notes, and snippets.

@elsonidoq
Created February 3, 2016 18:51
Show Gist options
  • Save elsonidoq/fa5654a460c1fee1f69e to your computer and use it in GitHub Desktop.
Save elsonidoq/fa5654a460c1fee1f69e to your computer and use it in GitHub Desktop.
from time import time
import scipy
from scipy import stats
from math import sqrt
from multiprocessing import current_process
class Progress(object):
def __init__(self, tot=None, prefix=None,
template='sp: %(speed).02fi/s, pr: %(progress).02f%%, rt: %(remaining)s, it: %(iteration_time)s, %(cnt)s of %(tot)s, pt: %(process_time)s',
threaded_cnt= None, print_time=0.25, print_pid=False):
self.print_pid= print_pid
self.tot= tot
self.prefix= prefix
self.print_time= print_time
self.is_threaded= threaded_cnt is not None
if threaded_cnt is not None: self.cnt= threaded_cnt
else: self.cnt= 0
self.t0= None
self.last_t= None
self.template= template
self.avg_inc_time= 0
self.var_inc_time= 0
self.remaining_time=0
self.times= []
self.started_at= time()
def _format_time(self, t):
units= 'smhd'
coefs= [60,60,24]
t= float(t)
for i in xrange(1,4):
if t < 60: break
t/=coefs[i-1]
return '%.02f%s' % (t, units[i-1])
def inc(self, **extras):
if self.is_threaded:
self.cnt.value+=1
cnt= self.cnt.value
else:
self.cnt+=1
cnt= self.cnt
t= time()
if self.last_t is not None:
if self.tot is not None: progress= cnt/float(self.tot)*100
iteration_time= t-self.last_t
delta= iteration_time-self.avg_inc_time
self.avg_inc_time+= delta/cnt
self.var_inc_time= float((cnt-2))/(cnt-1)*self.var_inc_time + \
(iteration_time- self.avg_inc_time)*delta/(cnt-1)
self.last_t= t
if self.t0 is None: self.t0= t
if t- self.t0 > self.print_time:
self.t0= time()
tot= self.tot
iteration_time= self._format_time(iteration_time)
process_time= self._format_time(time()-self.started_at)
if self.tot is not None:
remaining= self._format_time((tot-cnt)*self.avg_inc_time)
h= sqrt(self.var_inc_time) * stats.t._ppf(0.9, self.cnt-1)
h= self._format_time(h*(tot-cnt))
speed= 1.0/self.avg_inc_time
d= dict(extras)
d.update(locals())
if self.prefix is not None:
str= '%s%s' % (self.prefix, self.template % d)
else:
str= self.template % d
if self.print_pid:
str= '%s: %s' % (current_process().pid, str)
print str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment