- Timestamp:
- 10/15/12 10:27:21 (4 years ago)
- Location:
- branches/decorate
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/timer.py
r554 r579 11 11 12 12 13 class Monitor(object):13 class Timer(object): 14 14 """ 15 Instances of objects that can be passed as monitors. 16 Typically, a Monitor logs a list of parameters and the 17 corresponding costs, retrievable by accessing the Monitor's 18 member variables. 15 Instances of timer objects that can be passed as timers. 16 Typically, a timer is attached to a monitor and is called 17 when the monitor is called. 19 18 20 19 example usage... 21 >>> sow = Monitor() 22 >>> sow([1,2],3) 23 >>> sow([4,5],6) 24 >>> sow.x 25 [[1, 2], [4, 5]] 26 >>> sow.y 27 [3, 6] 28 20 >>> timer = Timer() 21 >>> timer.stamp = False 22 >>> timer() 23 >>> timer._t 24 51.91421914100647 25 >>> timer() 26 >>> timer._t 27 74.009551048278809 28 >>> timer.t 29 [51.91421914100647, 74.009551048278809] 30 >>> timer.stamp = True 31 >>> print timer._t 32 0:01:14.009551 33 >>> print timer.t0 34 2012-10-15 13:00:53.758812 29 35 """ 30 def __init__(self, **kwds):#, all=True): 31 self._x = [] 32 self._y = [] 33 self._id = [] 34 self._info = [] 35 self._t = [] 36 def __init__(self, **kwds): 37 self.__on = kwds.get('on', False) 38 self.__off = 0 39 self.__t = [] 36 40 self.__t0 = [self.__now()] 37 self._timed = kwds.get('timed', False) 38 #self._all = all 39 40 def __len__(self): 41 return len(self.x) 42 43 def info(self, message): 44 self._info.append("%s" % "".join(["",str(message)])) 45 return 46 47 def timer(self, on='?'): #XXX: str-driven args... a hallmark of bad design 48 if on == '?': 49 return self._timed 50 if on in [False, None, 'off']: #turn timing off 51 self._timed = False 52 elif isinstance(on, bool) or on == 'on': #turn timing on 53 self._timed = True 54 elif on == 'toggle': #toggle timing 55 self._timed = not self._timed 56 elif on in ['reset', 'restart']: #'restart' the StartTime 57 self.__t0.append(self.__now()) 58 return 59 41 self.__stamp = kwds.get('stamp', True) 42 self.__lazy = kwds.get('lazy', True) 43 def __call__(self): 44 if self.__on or self.__lazy: 45 self.__on = True 46 self.__t.extend(self.__deadtime()) 47 self.__t.append(self.__now()) 48 self.__off = 0 49 else: 50 self.__off += 1 51 def __get_lazy(self): 52 return self.__lazy 53 def __set_lazy(self, ind): 54 """if True, 'call' will turn timer on""" 55 self.__lazy = bool(ind) 56 def __get_stamp(self): 57 return self.__stamp 58 def __set_stamp(self, ind): 59 """if True, _t & t0 return datetime object""" 60 self.__stamp = bool(ind) 61 def __get_on(self): 62 return self.__on 63 def __set_on(self, ind): 64 """if False, record 'nan' instead of a time""" 65 self.__on = bool(ind) 66 def __toggle(self): 67 """toggle the timer on/off""" 68 self.__on = not self.__on 69 def __reset(self): 70 """reset t0 to now""" 71 self.__t0.append(self.__now()) 72 #XXX: should reset to original kwds settings? (on, lazy, ...) 73 def __get_t0(self): 74 t = self.__t0[-1] 75 if not self.__stamp: return t 76 import datetime 77 return datetime.datetime.fromtimestamp(t) 78 def __set_t0(self, t): #XXX: allow setting t0 ? 79 self.__t0.append(t) 80 def __first(self): 81 """get all t0 as a list""" 82 return self.__t0 60 83 def __now(self): 61 84 from time import time as now 62 85 return now() 63 64 86 def __deadtime(self): 65 87 from numpy import NaN 66 return [NaN] * (len(self._y) - len(self._t)) 88 return [NaN] * self.__off 89 def __get_t(self): 90 from numpy import array 91 return list(array(self.__t + self.__deadtime()) - self.__t0[-1]) 92 def __last(self): 93 """just get the last t, instead of all t""" 94 if not self.__t: return None # require a 'call' 95 t = self.__t[-1] - self.__t0[-1] 96 if t < 0.0: return None # require a 'call' after a 'reset' 97 if not self.__stamp: return t 98 import datetime 99 return datetime.timedelta(seconds=t) 100 on = property(__get_on, __set_on) 101 toggle = property(__toggle) 102 reset = property(__reset) 103 _t0 = property(__first) # [t0, ...] 104 t0 = property(__get_t0,__set_t0) # t0 105 _t = property(__last) # t 106 t = property(__get_t) # [t, ...] 107 stamp = property(__get_stamp, __set_stamp) 108 lazy = property(__get_lazy, __set_lazy) 109 110 111 from mystic.monitors import Monitor 112 class TimedMonitor(Monitor): 113 """A timed version of the basic Monitor. 114 115 Every time the monitor is called, the time is also logged. 116 Timer is configurable through 'monitor.timer'. 117 """ 118 def __init__(self, **kwds):#, all=True): 119 timed = kwds.pop('timed', False) 120 #called = kwds.pop('lazy', True) 121 #stamp = kwds.pop('stamp', True) 122 super(TimedMonitor,self).__init__(**kwds) 123 self.timer = Timer(on=timed)#,lazy=called,stamp=stamp) 124 return 67 125 68 126 def __call__(self, x, y, id=None, **kwds):#, best=0): 69 if self._timed: 70 self._t.extend(self.__deadtime()) 71 self._t.append(self.__now()) 72 from mystic.tools import listify 73 self._x.append(listify(x)) #XXX: better to save as-is? 74 self._y.append(listify(y)) #XXX: better to save as-is? 75 self._id.append(id) 76 #if not self._all and list_or_tuple_or_ndarray(x): 77 # self._x[-1] = self._x[-1][best] 78 #if not self._all and list_or_tuple_or_ndarray(y): 79 # self._y[-1] = self._y[-1][best] 80 81 def get_x(self): 82 return self._x 83 84 def get_y(self): 85 return self._y 86 87 def get_id(self): 88 return self._id 89 90 def get_info(self): 91 return self._info 92 93 def get_t(self): 94 from numpy import array 95 return list(array(self._t + self.__deadtime()) - self._t0) 96 97 def _get_t0(self): 98 return self.__t0[-1] 99 100 def _set_t0(self, t): #XXX: allow setting t0 ? 101 self.__t0.append(t) 127 self.timer() 128 super(TimedMonitor,self).__call__(x, y, id, **kwds) 102 129 return 103 130 104 x = property(get_x, doc = "Params")105 y = property(get_y, doc = "Costs")106 id = property(get_id, doc = "Id")107 t = property(get_t, doc = "Time")108 _t0 = property(_get_t0, _set_t0, doc = "StartTime")109 131 pass 110 132 133 134 if __name__ == '__main__': 135 monitor = TimedMonitor() 136 monitor.timer.lazy = False 137 monitor([1],1) 138 monitor.timer.on = True 139 monitor([2],2) 140 monitor([3],3) 141 monitor.timer.toggle 142 print "x: %s" % monitor.x 143 print "y: %s" % monitor.y 144 print "t: %s" % monitor.timer.t 145 print "t0: %s" % monitor.timer.t0 146 monitor.timer.stamp = False 147 print "Ran: %s seconds" % monitor.timer._t 148 monitor.timer.stamp = True 149 monitor.timer.reset 150 monitor([4],4) 151 print "x: %s" % monitor.x 152 print "y: %s" % monitor.y 153 print "t: %s" % monitor.timer.t 154 print "t0: %s" % monitor.timer.t0 155 monitor.timer.stamp = False 156 print "Ran: %s seconds" % monitor.timer._t 157 monitor.timer.stamp = True 158 monitor.timer.toggle 159 monitor([5],5) 160 print "x: %s" % monitor.x 161 print "y: %s" % monitor.y 162 print "t: %s" % monitor.timer.t 163 print "t0: %s" % monitor.timer.t0 164 monitor.timer.stamp = False 165 print "Ran: %s seconds" % monitor.timer._t 166 monitor.timer.stamp = True 167 print "Completed: %s" % (monitor.timer._t + monitor.timer.t0) 168 169 # EOF
Note: See TracChangeset
for help on using the changeset viewer.