- Timestamp:
- 12/03/12 11:57:08 (3 years ago)
- Location:
- branches
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UQ/math/legacy/envelope/Looper_BoLiSurr_Cy.py
r596 r601 116 116 return rv 117 117 118 # from memoize import memoized 0_nopickle_round as memoized118 # from memoize import memoized_nopickle_round as memoized 119 119 120 120 # generate primary constraints function -
branches/UQ/math/legacy/envelope/envelope.py
r592 r601 193 193 # return model_sausage(x) 194 194 195 from memoize import memoized 0_nopickle_archived as memoized195 from memoize import memoized_nopickle_archived as memoized 196 196 # vectorize 'sausage_bounds' to find min/max for every evaluation point 197 197 @memoized() -
branches/UQ/math/legacy/envelope/memoize.py
r596 r601 72 72 73 73 74 def shallow_round_factory(tol): 75 from numpy import round as around #XXX: try or numpy makes this slow 76 def shallow_round(*args, **kwds): 77 argstype = type(args) 78 _args = list(args) 79 _kwds = kwds.copy() 80 for i,j in enumerate(args): 81 try: 82 jtype = type(j) 83 _args[i] = jtype(around(j, tol)) 84 except: pass 85 for i,j in kwds.items(): 86 try: 87 jtype = type(j) 88 _kwds[i] = jtype(around(j, tol)) 89 except: pass 90 return argstype(_args), _kwds 91 return shallow_round 92 93 def shallow_round(tol=0): #NOTE: rounds floats, lists, arrays one level deep 94 def dec(f): 95 def func(*args, **kwds): 96 _shallow_round = shallow_round_factory(tol) 97 _args,_kwds = _shallow_round(*args, **kwds) 98 return f(*_args, **_kwds) 99 return func 100 return dec 101 102 74 103 def get_archive(archive): 75 104 import dill as pickle … … 121 150 #FIXME: memoize*_round fails when decorating a class method 122 151 123 #FIXME: resolve signature difference... @memoized versus @memoized() 124 125 def memoized0_nopickle_round(tol=0, deep=False): 152 def memoized_nopickle_round(tol=0, deep=False): 126 153 """Decorator that memoizes a function's return value each time it is called. 127 154 If called later with the same arguments, the memoized value is returned, and … … 135 162 if deep: rounded = deep_round 136 163 else: rounded = simple_round 164 #else: rounded = shallow_round 137 165 138 166 @rounded(tol) … … 155 183 156 184 157 def memoized 0_round(tol=0, deep=False):185 def memoized_round(tol=0, deep=False): 158 186 """Decorator that memoizes a function's return value each time it is called. 159 187 If called later with the same arguments, the memoized value is returned, and … … 166 194 if deep: rounded = deep_round 167 195 else: rounded = simple_round 196 #else: rounded = shallow_round 168 197 169 198 @rounded(tol) … … 194 223 # - load an archive / update an existing archive 195 224 # - save some or all of memo to archive 196 def memoized 0_nopickle_archived():225 def memoized_nopickle_archived(): 197 226 """Decorator that memoizes a function's return value each time it is called. 198 227 If called later with the same arguments, the memoized value is returned, and … … 223 252 224 253 225 def memoized 0_archived():254 def memoized_archived(): 226 255 """Decorator that memoizes a function's return value each time it is called. 227 256 If called later with the same arguments, the memoized value is returned, and … … 254 283 255 284 256 def memoized0(f): 257 """Decorator that memoizes a function's return value each time it is called. 258 If called later with the same arguments, the memoized value is returned, and 259 not re-evaluated. This may lead to memory issues, as memo is never cleared. 260 """ 261 memo = {} 262 263 def func(*args, **kwds): 264 try: 265 #import cPickle as pickle 266 import dill as pickle 267 argstr = pickle.dumps((args, kwds)) 268 if not memo.has_key(argstr): 269 memo[argstr] = f(*args, **kwds) 270 return memo[argstr] 271 except: #TypeError 272 return f(*args, **kwds) 273 func.memo = memo 274 return func 275 276 277 class memoized(object): 285 def memoized(): 286 """Decorator that memoizes a function's return value each time it is called. 287 If called later with the same arguments, the memoized value is returned, and 288 not re-evaluated. This may lead to memory issues, as memo is never cleared. 289 """ 290 memo = {} 291 292 def dec(f): 293 def func(*args, **kwds): 294 try: 295 #import cPickle as pickle 296 import dill as pickle 297 argstr = pickle.dumps((args, kwds)) 298 if not memo.has_key(argstr): 299 memo[argstr] = f(*args, **kwds) 300 return memo[argstr] 301 except: #TypeError 302 return f(*args, **kwds) 303 func.memo = memo 304 return func 305 return dec 306 307 308 class memoize(object): 278 309 """Decorator that memoizes a function's return value each time it is called. 279 310 If called later with the same arguments, the memoized value is returned, and -
branches/UQ/math/legacy/envelope/otm_hvi.py
r596 r601 31 31 return 1.5 32 32 33 from memoize import memoized 0_nopickle_round as memoized33 from memoize import memoized_nopickle_round as memoized 34 34 35 35 @memoized(tol=3, deep=True) -
branches/UQ/math/legacy/envelope/otm_hvi_new.py
r596 r601 31 31 return 1.5 32 32 33 from memoize import memoized 0_nopickle_round as memoized33 from memoize import memoized_nopickle_round as memoized 34 34 35 35 @memoized(tol=3, deep=True) -
branches/UQ/math/legacy/envelope/sausage.py
r596 r601 101 101 return runs.tolist() 102 102 103 from memoize import memoized 0_nopickle_round as memoized103 from memoize import memoized_nopickle_round as memoized 104 104 # vectorize 'sausage_bounds' to find min/max for every evaluation point 105 105 @memoized(tol=3, deep=True) -
branches/decorate/memoize.py
r597 r601 150 150 #FIXME: memoize*_round fails when decorating a class method 151 151 152 #FIXME: resolve signature difference... @memoized versus @memoized() 153 154 def memoized0_nopickle_round(tol=0, deep=False): 152 def memoized_nopickle_round(tol=0, deep=False): 155 153 """Decorator that memoizes a function's return value each time it is called. 156 154 If called later with the same arguments, the memoized value is returned, and … … 185 183 186 184 187 def memoized 0_round(tol=0, deep=False):185 def memoized_round(tol=0, deep=False): 188 186 """Decorator that memoizes a function's return value each time it is called. 189 187 If called later with the same arguments, the memoized value is returned, and … … 225 223 # - load an archive / update an existing archive 226 224 # - save some or all of memo to archive 227 def memoized 0_nopickle_archived():225 def memoized_nopickle_archived(): 228 226 """Decorator that memoizes a function's return value each time it is called. 229 227 If called later with the same arguments, the memoized value is returned, and … … 254 252 255 253 256 def memoized 0_archived():254 def memoized_archived(): 257 255 """Decorator that memoizes a function's return value each time it is called. 258 256 If called later with the same arguments, the memoized value is returned, and … … 285 283 286 284 287 def memoized0(f): 288 """Decorator that memoizes a function's return value each time it is called. 289 If called later with the same arguments, the memoized value is returned, and 290 not re-evaluated. This may lead to memory issues, as memo is never cleared. 291 """ 292 memo = {} 293 294 def func(*args, **kwds): 295 try: 296 #import cPickle as pickle 297 import dill as pickle 298 argstr = pickle.dumps((args, kwds)) 299 if not memo.has_key(argstr): 300 memo[argstr] = f(*args, **kwds) 301 return memo[argstr] 302 except: #TypeError 303 return f(*args, **kwds) 304 func.memo = memo 305 return func 306 307 308 class memoized(object): 285 def memoized(): 286 """Decorator that memoizes a function's return value each time it is called. 287 If called later with the same arguments, the memoized value is returned, and 288 not re-evaluated. This may lead to memory issues, as memo is never cleared. 289 """ 290 memo = {} 291 292 def dec(f): 293 def func(*args, **kwds): 294 try: 295 #import cPickle as pickle 296 import dill as pickle 297 argstr = pickle.dumps((args, kwds)) 298 if not memo.has_key(argstr): 299 memo[argstr] = f(*args, **kwds) 300 return memo[argstr] 301 except: #TypeError 302 return f(*args, **kwds) 303 func.memo = memo 304 return func 305 return dec 306 307 308 class memoize(object): 309 309 """Decorator that memoizes a function's return value each time it is called. 310 310 If called later with the same arguments, the memoized value is returned, and -
branches/decorate/surrogate.py
r597 r601 20 20 21 21 22 from memoize import memoized 0_round, memoized0_nopickle_round23 from memoize import memoized 0_archived, memoized0_nopickle_archived24 #@memoized 0_round(0, deep=True) # slower, but more robust25 #@memoized 0_nopickle_round(0, deep=True)26 #@memoized 0_archived() # slower, but more robust27 @memoized 0_nopickle_archived()22 from memoize import memoized_round, memoized_nopickle_round 23 from memoize import memoized_archived, memoized_nopickle_archived 24 #@memoized_round(0, deep=True) # slower, but more robust 25 #@memoized_nopickle_round(0, deep=True) 26 #@memoized_archived() # slower, but more robust 27 @memoized_nopickle_archived() 28 28 def marc_surr(x): 29 29 """calculate perforation area using a tanh-based model surrogate -
branches/decorate/test_cached_memoize.py
r590 r601 1 from memoize import memoized 0_nopickle_archived as memoized2 #from memoize import memoized 0_archived as memoized1 from memoize import memoized_nopickle_archived as memoized 2 #from memoize import memoized_archived as memoized 3 3 from timer import timed 4 4 5 5 # here caching saves time in a recursive function... 6 6 @memoized() 7 @timed 7 @timed() 8 8 def fibonacci(n): 9 9 "Return the nth fibonacci number." -
branches/decorate/test_memoize.py
r595 r601 21 21 """ 22 22 23 #from memoize import memoized 0_nopickle_round as memoized24 #from memoize import memoized 0_round as memoized25 #from memoize import memoized0as memoized26 from memoize import memoized 23 #from memoize import memoized_nopickle_round as memoized 24 #from memoize import memoized_round as memoized 25 from memoize import memoized as memoized 26 #from memoize import memoize 27 27 from timer import timed 28 28 … … 31 31 """A simple class with a memoized method""" 32 32 33 @memoized 33 @memoized() 34 34 def eggs(self, *args, **kwds): 35 35 print 'new:', args, kwds … … 50 50 51 51 # here caching saves time in a recursive function... 52 @memoized 53 @timed 52 @memoized() 53 @timed() 54 54 def fibonacci(n): 55 55 "Return the nth fibonacci number." … … 64 64 print '=' * 30 65 65 66 from memoize import memoized 0_round66 from memoize import memoized_round 67 67 from numpy import sum, asarray 68 @memoized 0_round(3)68 @memoized_round(3) 69 69 def add(*args): 70 70 print 'new:', args … … 88 88 return sum(x**2 - y**2) 89 89 90 cost1 = memoized 0_round(1)(cost)91 cost0 = memoized 0_round(0)(cost)92 costD = memoized 0_round(0, deep=True)(cost)90 cost1 = memoized_round(1)(cost) 91 cost0 = memoized_round(0)(cost) 92 costD = memoized_round(0, deep=True)(cost) 93 93 94 94 print "rounding to one decimals..." -
branches/decorate/test_timer.py
r532 r601 11 11 from timer import timed 12 12 13 @timed 13 @timed(verbose=False) 14 14 def square_me(x): 15 15 return [i**2 for i in x] … … 17 17 # time the function call using the @timed function 18 18 result = square_me(big_array) 19 print "Timed: %s" % square_me.time() -
branches/decorate/timer.py
r579 r601 1 1 2 def timed(f): 3 """decorator for timing a function""" 4 def func(*args, **kwds): 5 from time import time 6 t = time() 7 res = f(*args, **kwds) 8 print "Timed: %s" % (time() - t) #XXX: write to a monitor instead ? 9 return res 10 return func 2 def timed(verbose=True): 3 def dec(f): 4 """decorator for timing a function""" 5 from time import time as now 6 __time = [True, None] 7 if not verbose: __time[0] = False 8 def time(stamp=False): 9 if not stamp: return __time[-1] 10 import datetime 11 return datetime.timedelta(seconds=__time[-1]) 12 def __verbose(on=True): 13 __time[0] = bool(on) 14 def func(*args, **kwds): 15 t = now() 16 res = f(*args, **kwds) 17 __time[-1] = now() - t 18 if __time[0]: 19 print "Timed: %s" % __time[-1] 20 return res 21 func.time = time 22 func.verbose = __verbose 23 return func 24 return dec 11 25 12 26
Note: See TracChangeset
for help on using the changeset viewer.