Changeset 591 for branches


Ignore:
Timestamp:
11/19/12 17:05:38 (3 years ago)
Author:
mmckerns
Message:

added decorator in wrapper to provide discrete input
added test for memoized*_archived

Location:
branches/decorate
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/surrogate.py

    r535 r591  
    6464 
    6565from memoize import memoized0_round, memoized0_nopickle_round 
     66from memoize import memoized0_archived, memoized0_nopickle_archived 
    6667#@memoized0_round(0)         # slower, but more robust 
    67 @memoized0_nopickle_round(0) 
     68#@memoized0_nopickle_round(0) 
     69#@memoized0_archived()       # slower, but more robust 
     70@memoized0_nopickle_archived() 
    6871def marc_surr(x): 
    6972  """calculate perforation area using a tanh-based model surrogate 
  • branches/decorate/test_timed_monitor.py

    r579 r591  
    174174  print " upper bounds: %s" % upper_bounds 
    175175# print " ..." 
     176  try: model.load('surrogate.pkl') 
     177  except: pass 
    176178  diameter = UQ(RVstart,RVend,lower_bounds,upper_bounds) 
     179  try: model.dump('surrogate.pkl') 
     180  except: pass 
    177181 
    178182# EOF 
  • branches/decorate/test_wrapper.py

    r580 r591  
    128128 
    129129 
     130def test_discrete(): 
     131 
     132  @discrete([1.0, 3.5, 5.5, 7.0]) 
     133  def discrete_squared(x): 
     134    return x**2 
     135 
     136  assert discrete_squared(5.6) == 5.5**2 
     137  assert discrete_squared([1, 3]) == asarray([1.0, 3.5])**2 
     138  discrete_squared.samples([1.0, 7.0]) 
     139  assert discrete_squared(5.6) == 7.0**2 
     140 
     141 
    130142 
    131143if __name__ is '__main__': 
     
    138150  test_bounded() 
    139151  test_mixedin() 
     152  test_discrete() 
    140153 
    141154 
  • branches/decorate/wrapper.py

    r580 r591  
    5050 
    5151 
    52 from numpy import asarray, any, inf 
     52from numpy import asarray, any, inf, vectorize 
    5353def bounded(min=None, max=None): 
    5454    """impose an infinite barrier box constraint on a function 
     
    107107 
    108108 
     109#from random import sample, choice 
     110def discrete(samples): 
     111    """impose a discrete set of input values for the selected function 
     112 
     113The function's input will be mapped to the given discrete set 
     114 
     115>>> @decorate([1.0, 2.0]) 
     116... def identity(x): 
     117...   return x 
     118 
     119>>> identity([0.123, 1.789, 4.000]) 
     120[1.0, 2.0, 2.0]""" #FIXME: should return same type as input 
     121    samples = [asarray(samples)] 
     122    samples[0].sort() 
     123 
     124    #XXX: refactor to use points_factory(samples) 
     125    def _points(alist): 
     126        alist = asarray(alist) 
     127        alist.sort() 
     128        samples[0] = alist 
     129 
     130    #XXX: refactor to use argnear_factory(samples) 
     131    def _argnear(xi): 
     132        arghi = sum(xi > samples[0]) 
     133        arglo = max(0, arghi - 1) # minimum = x[0] 
     134        if arghi == len(samples[0]):  
     135            arghi = arglo         # maximum = x[-1] 
     136        return arglo, arghi 
     137 
     138    def _near(xi, lo, hi): 
     139        if hi - xi < xi - lo:  
     140            return hi 
     141        return lo 
     142 
     143    argnear = vectorize(_argnear) 
     144    near = vectorize(_near) 
     145 
     146    def dec(f): 
     147        def func(x, *args, **kwds): 
     148            arglo, arghi = argnear(x) 
     149            xp = near(x, samples[0][arglo], samples[0][arghi]) 
     150            return f(xp, *args, **kwds) 
     151        func.samples = _points 
     152        return func 
     153    return dec 
     154 
     155 
    109156# EOF 
Note: See TracChangeset for help on using the changeset viewer.