Changeset 598 for branches


Ignore:
Timestamp:
11/21/12 19:22:46 (3 years ago)
Author:
mmckerns
Message:

added clip_bounded, target_bounded, and bounce_bounded decorators to wrapper

Location:
branches/decorate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/test_wrapper.py

    r594 r598  
    113113  y = squared(x) 
    114114  assert y == inf #XXX: better, [i**2 if i in range(1,10) else inf for i in x] ? 
     115 
     116 
     117def test_clip_bounded(): 
     118 
     119  @clip_bounded(min=1.0,max=9.9999) 
     120  def squared(x): 
     121    return x**2 
     122 
     123  from numpy import array, arange 
     124  x = range(11) 
     125  y = [squared(i) for i in x] 
     126  assert y == [min(max(i,1.0),9.9999)**2 for i in x] 
     127 
     128  y = squared(x) 
     129  assert all(y == array([min(max(i,1.0),9.9999)**2 for i in x])) 
     130 
     131 
     132def test_target_bounded(): 
     133 
     134  @target_bounded(min=1.0,max=9.9999,target=5.0) 
     135  def squared(x): 
     136    return x**2 
     137 
     138  from numpy import array, arange 
     139  x = range(11) 
     140  y = [squared(i) for i in x] 
     141  assert y == [i**2 if 1.0 <= i <= 9.999 else 5.0**2 for i in x] 
     142 
     143  y = squared(x) 
     144  assert all(y == [i**2 if 1.0 <= i <= 9.9999 else 5.0**2 for i in x]) 
     145 
     146 
     147def test_bounce_bounded(): 
     148 
     149  from mystic.math.measures import impose_mean, mean 
     150 
     151  @bounce_bounded(min=1.0,max=9.9999) 
     152  def set_mean(x, m): 
     153    return impose_mean(m, x) 
     154 
     155  from numpy import array, arange 
     156  x = range(11) 
     157  assert round(mean(set_mean(x, 5.0)), 15) == 5.0 
    115158 
    116159 
     
    151194  test_mixedin() 
    152195  test_discrete() 
     196  test_clip_bounded() 
     197  test_target_bounded() 
     198  test_bounce_bounded() 
    153199 
    154200 
  • branches/decorate/wrapper.py

    r594 r598  
    113113The function's input will be mapped to the given discrete set 
    114114 
    115 >>> @decorate([1.0, 2.0]) 
     115>>> @discrete([1.0, 2.0]) 
    116116... def identity(x): 
    117117...   return x 
     
    154154 
    155155 
     156#FIXME: the following should preserve the input type 
     157def __clip_bound(x, amin, amax): 
     158  return max(min(x, amax), amin) 
     159clip_bound = vectorize(__clip_bound) 
     160 
     161def __bounce_bound(x, amin, amax): 
     162  if not x <= amax: 
     163    x = amax-(amax-amin)*((x-amax)/(x+amax)) 
     164  if not x >= amin: 
     165    x = amin+(amax-amin)*((amin-x)/(amin+x)) 
     166  return x 
     167bounce_bound = vectorize(__bounce_bound) 
     168 
     169def __target_bound(x, amin, amax, target): 
     170  if not x <= amax: 
     171    x = min(target, amax) 
     172  if not x >= amin: 
     173    x = max(target, amin) 
     174  return x 
     175target_bound = vectorize(__target_bound) 
     176 
     177""" 
     178def _clip_bound(x, amin, amax): 
     179  return [__clip_bound(xi,li,ui) for xi,li,ui in zip(x,amin,amax)] 
     180 
     181def _bounce_bound(x, amin, amax): 
     182  return [__bounce_bound(xi,li,ui) for xi,li,ui in zip(x,amin,amax)] 
     183 
     184def _target_bound(x, amin, amax, target): 
     185  return [__target_bound(xi,li,ui,target) for xi,li,ui in zip(x,amin,amax)] 
     186""" 
     187 
     188def clip_bounded(min, max): 
     189    def dec(f): 
     190        def func(x, *args, **kwds): 
     191            return f(clip_bound(x, min, max), *args, **kwds) 
     192        return func 
     193    return dec 
     194 
     195def bounce_bounded(min, max): 
     196    def dec(f): 
     197        def func(x, *args, **kwds): 
     198            return f(bounce_bound(x, min, max), *args, **kwds) 
     199        return func 
     200    return dec 
     201 
     202def target_bounded(min, max, target): 
     203    def dec(f): 
     204        def func(x, *args, **kwds): 
     205            return f(target_bound(x, min, max, target), *args, **kwds) 
     206        return func 
     207    return dec 
     208 
     209 
    156210# EOF 
Note: See TracChangeset for help on using the changeset viewer.