Changeset 597 for branches


Ignore:
Timestamp:
11/20/12 14:46:46 (3 years ago)
Author:
mmckerns
Message:

add 'shallow' rounding (one level deep for floats, lists, arrays)

Location:
branches/decorate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/memoize.py

    r595 r597  
    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 
     
    135164    if deep: rounded = deep_round 
    136165    else: rounded = simple_round 
     166   #else: rounded = shallow_round 
    137167 
    138168    @rounded(tol) 
     
    166196    if deep: rounded = deep_round 
    167197    else: rounded = simple_round 
     198   #else: rounded = shallow_round 
    168199 
    169200    @rounded(tol) 
  • branches/decorate/surrogate.py

    r595 r597  
    2222from memoize import memoized0_round, memoized0_nopickle_round 
    2323from memoize import memoized0_archived, memoized0_nopickle_archived 
    24 #@memoized0_round(0)         # slower, but more robust 
    25 #@memoized0_nopickle_round(0) 
    26 #@memoized0_archived()       # slower, but more robust 
     24#@memoized0_round(0, deep=True)          # slower, but more robust 
     25#@memoized0_nopickle_round(0, deep=True) 
     26#@memoized0_archived()                   # slower, but more robust 
    2727@memoized0_nopickle_archived() 
    2828def marc_surr(x): 
Note: See TracChangeset for help on using the changeset viewer.