Changeset 554
- Timestamp:
- 09/05/12 17:55:08 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/timer.py
r532 r554 6 6 t = time() 7 7 res = f(*args, **kwds) 8 print "Timed: %s" % (time() - t) 8 print "Timed: %s" % (time() - t) #XXX: write to a monitor instead ? 9 9 return res 10 10 return func 11 11 12 12 13 class Monitor(object): 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. 19 20 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 29 """ 30 def __init__(self, **kwds):#, all=True): 31 self._x = [] 32 self._y = [] 33 self._id = [] 34 self._info = [] 35 self._t = [] 36 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 60 def __now(self): 61 from time import time as now 62 return now() 63 64 def __deadtime(self): 65 from numpy import NaN 66 return [NaN] * (len(self._y) - len(self._t)) 67 68 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) 102 return 103 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 pass 110
Note: See TracChangeset
for help on using the changeset viewer.