Changeset 601 for branches


Ignore:
Timestamp:
12/03/12 11:57:08 (3 years ago)
Author:
mmckerns
Message:

memoized0 functions renamed to memoized, memoized class to memoize;
improved simple timer decorator to include verbosity

Location:
branches
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • branches/UQ/math/legacy/envelope/Looper_BoLiSurr_Cy.py

    r596 r601  
    116116    return rv 
    117117 
    118 # from memoize import memoized0_nopickle_round as memoized 
     118# from memoize import memoized_nopickle_round as memoized 
    119119 
    120120  # generate primary constraints function 
  • branches/UQ/math/legacy/envelope/envelope.py

    r592 r601  
    193193#  return model_sausage(x) 
    194194 
    195 from memoize import memoized0_nopickle_archived as memoized 
     195from memoize import memoized_nopickle_archived as memoized 
    196196# vectorize 'sausage_bounds' to find min/max for every evaluation point 
    197197@memoized() 
  • branches/UQ/math/legacy/envelope/memoize.py

    r596 r601  
    7272 
    7373 
     74def 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 
     93def 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 
    74103def get_archive(archive): 
    75104    import dill as pickle 
     
    121150#FIXME: memoize*_round fails when decorating a class method 
    122151 
    123 #FIXME: resolve signature difference...  @memoized versus @memoized() 
    124  
    125 def memoized0_nopickle_round(tol=0, deep=False): 
     152def memoized_nopickle_round(tol=0, deep=False): 
    126153    """Decorator that memoizes a function's return value each time it is called. 
    127154    If called later with the same arguments, the memoized value is returned, and 
     
    135162    if deep: rounded = deep_round 
    136163    else: rounded = simple_round 
     164   #else: rounded = shallow_round 
    137165 
    138166    @rounded(tol) 
     
    155183 
    156184 
    157 def memoized0_round(tol=0, deep=False): 
     185def memoized_round(tol=0, deep=False): 
    158186    """Decorator that memoizes a function's return value each time it is called. 
    159187    If called later with the same arguments, the memoized value is returned, and 
     
    166194    if deep: rounded = deep_round 
    167195    else: rounded = simple_round 
     196   #else: rounded = shallow_round 
    168197 
    169198    @rounded(tol) 
     
    194223# - load an archive / update an existing archive 
    195224# - save some or all of memo to archive 
    196 def memoized0_nopickle_archived(): 
     225def memoized_nopickle_archived(): 
    197226    """Decorator that memoizes a function's return value each time it is called. 
    198227    If called later with the same arguments, the memoized value is returned, and 
     
    223252 
    224253 
    225 def memoized0_archived(): 
     254def memoized_archived(): 
    226255    """Decorator that memoizes a function's return value each time it is called. 
    227256    If called later with the same arguments, the memoized value is returned, and 
     
    254283 
    255284 
    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): 
     285def 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 
     308class memoize(object): 
    278309    """Decorator that memoizes a function's return value each time it is called. 
    279310    If called later with the same arguments, the memoized value is returned, and 
  • branches/UQ/math/legacy/envelope/otm_hvi.py

    r596 r601  
    3131        return 1.5 
    3232     
    33 from memoize import memoized0_nopickle_round as memoized 
     33from memoize import memoized_nopickle_round as memoized 
    3434 
    3535@memoized(tol=3, deep=True) 
  • branches/UQ/math/legacy/envelope/otm_hvi_new.py

    r596 r601  
    3131        return 1.5 
    3232     
    33 from memoize import memoized0_nopickle_round as memoized 
     33from memoize import memoized_nopickle_round as memoized 
    3434 
    3535@memoized(tol=3, deep=True) 
  • branches/UQ/math/legacy/envelope/sausage.py

    r596 r601  
    101101  return runs.tolist() 
    102102 
    103 from memoize import memoized0_nopickle_round as memoized 
     103from memoize import memoized_nopickle_round as memoized 
    104104# vectorize 'sausage_bounds' to find min/max for every evaluation point 
    105105@memoized(tol=3, deep=True) 
  • branches/decorate/memoize.py

    r597 r601  
    150150#FIXME: memoize*_round fails when decorating a class method 
    151151 
    152 #FIXME: resolve signature difference...  @memoized versus @memoized() 
    153  
    154 def memoized0_nopickle_round(tol=0, deep=False): 
     152def memoized_nopickle_round(tol=0, deep=False): 
    155153    """Decorator that memoizes a function's return value each time it is called. 
    156154    If called later with the same arguments, the memoized value is returned, and 
     
    185183 
    186184 
    187 def memoized0_round(tol=0, deep=False): 
     185def memoized_round(tol=0, deep=False): 
    188186    """Decorator that memoizes a function's return value each time it is called. 
    189187    If called later with the same arguments, the memoized value is returned, and 
     
    225223# - load an archive / update an existing archive 
    226224# - save some or all of memo to archive 
    227 def memoized0_nopickle_archived(): 
     225def memoized_nopickle_archived(): 
    228226    """Decorator that memoizes a function's return value each time it is called. 
    229227    If called later with the same arguments, the memoized value is returned, and 
     
    254252 
    255253 
    256 def memoized0_archived(): 
     254def memoized_archived(): 
    257255    """Decorator that memoizes a function's return value each time it is called. 
    258256    If called later with the same arguments, the memoized value is returned, and 
     
    285283 
    286284 
    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): 
     285def 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 
     308class memoize(object): 
    309309    """Decorator that memoizes a function's return value each time it is called. 
    310310    If called later with the same arguments, the memoized value is returned, and 
  • branches/decorate/surrogate.py

    r597 r601  
    2020 
    2121 
    22 from memoize import memoized0_round, memoized0_nopickle_round 
    23 from memoize import memoized0_archived, memoized0_nopickle_archived 
    24 #@memoized0_round(0, deep=True)          # slower, but more robust 
    25 #@memoized0_nopickle_round(0, deep=True) 
    26 #@memoized0_archived()                   # slower, but more robust 
    27 @memoized0_nopickle_archived() 
     22from memoize import memoized_round, memoized_nopickle_round 
     23from 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() 
    2828def marc_surr(x): 
    2929  """calculate perforation area using a tanh-based model surrogate 
  • branches/decorate/test_cached_memoize.py

    r590 r601  
    1 from memoize import memoized0_nopickle_archived as memoized 
    2 #from memoize import memoized0_archived as memoized 
     1from memoize import memoized_nopickle_archived as memoized 
     2#from memoize import memoized_archived as memoized 
    33from timer import timed 
    44 
    55# here caching saves time in a recursive function... 
    66@memoized() 
    7 @timed 
     7@timed() 
    88def fibonacci(n): 
    99    "Return the nth fibonacci number." 
  • branches/decorate/test_memoize.py

    r595 r601  
    2121""" 
    2222 
    23 #from memoize import memoized0_nopickle_round as memoized 
    24 #from memoize import memoized0_round as memoized 
    25 #from memoize import memoized0 as memoized 
    26 from memoize import memoized 
     23#from memoize import memoized_nopickle_round as memoized 
     24#from memoize import memoized_round as memoized 
     25from memoize import memoized as memoized 
     26#from memoize import memoize 
    2727from timer import timed 
    2828 
     
    3131    """A simple class with a memoized method""" 
    3232 
    33     @memoized 
     33    @memoized() 
    3434    def eggs(self, *args, **kwds): 
    3535        print 'new:', args, kwds 
     
    5050 
    5151# here caching saves time in a recursive function... 
    52 @memoized 
    53 @timed 
     52@memoized() 
     53@timed() 
    5454def fibonacci(n): 
    5555    "Return the nth fibonacci number." 
     
    6464print '=' * 30 
    6565 
    66 from memoize import memoized0_round 
     66from memoize import memoized_round 
    6767from numpy import sum, asarray 
    68 @memoized0_round(3) 
     68@memoized_round(3) 
    6969def add(*args): 
    7070    print 'new:', args 
     
    8888    return sum(x**2 - y**2) 
    8989 
    90 cost1 = memoized0_round(1)(cost) 
    91 cost0 = memoized0_round(0)(cost) 
    92 costD = memoized0_round(0, deep=True)(cost) 
     90cost1 = memoized_round(1)(cost) 
     91cost0 = memoized_round(0)(cost) 
     92costD = memoized_round(0, deep=True)(cost) 
    9393 
    9494print "rounding to one decimals..." 
  • branches/decorate/test_timer.py

    r532 r601  
    1111from timer import timed 
    1212 
    13 @timed 
     13@timed(verbose=False) 
    1414def square_me(x): 
    1515    return [i**2 for i in x] 
     
    1717# time the function call using the @timed function 
    1818result = square_me(big_array) 
     19print "Timed: %s" % square_me.time() 
  • branches/decorate/timer.py

    r579 r601  
    11 
    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 
     2def 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 
    1125 
    1226 
Note: See TracChangeset for help on using the changeset viewer.