- Timestamp:
- 11/21/12 19:22:46 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/test_wrapper.py
r594 r598 113 113 y = squared(x) 114 114 assert y == inf #XXX: better, [i**2 if i in range(1,10) else inf for i in x] ? 115 116 117 def 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 132 def 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 147 def 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 115 158 116 159 … … 151 194 test_mixedin() 152 195 test_discrete() 196 test_clip_bounded() 197 test_target_bounded() 198 test_bounce_bounded() 153 199 154 200 -
branches/decorate/wrapper.py
r594 r598 113 113 The function's input will be mapped to the given discrete set 114 114 115 >>> @d ecorate([1.0, 2.0])115 >>> @discrete([1.0, 2.0]) 116 116 ... def identity(x): 117 117 ... return x … … 154 154 155 155 156 #FIXME: the following should preserve the input type 157 def __clip_bound(x, amin, amax): 158 return max(min(x, amax), amin) 159 clip_bound = vectorize(__clip_bound) 160 161 def __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 167 bounce_bound = vectorize(__bounce_bound) 168 169 def __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 175 target_bound = vectorize(__target_bound) 176 177 """ 178 def _clip_bound(x, amin, amax): 179 return [__clip_bound(xi,li,ui) for xi,li,ui in zip(x,amin,amax)] 180 181 def _bounce_bound(x, amin, amax): 182 return [__bounce_bound(xi,li,ui) for xi,li,ui in zip(x,amin,amax)] 183 184 def _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 188 def 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 195 def 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 202 def 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 156 210 # EOF
Note: See TracChangeset
for help on using the changeset viewer.