Changeset 646 for branches


Ignore:
Timestamp:
01/18/13 06:20:57 (3 years ago)
Author:
mmckerns
Message:

moved discrete from decorate/wrapper to mystic.constraints;
added impose_reweighted_mean and impose_reweighted_variance

Location:
branches/decorate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/test_wrapper.py

    r629 r646  
    126126 
    127127 
    128 def test_discrete(): 
    129  
    130   @discrete([1.0, 3.5, 5.5, 7.0]) 
    131   def discrete_squared(x): 
    132     return x**2 
    133  
    134   assert discrete_squared(5.6) == 5.5**2 
    135   assert all(discrete_squared(asarray([1, 3])) == asarray([1.0, 3.5])**2) 
    136   discrete_squared.samples([1.0, 7.0]) 
    137   assert discrete_squared(5.6) == 7.0**2 
    138   discrete_squared.index([0, -1]) 
    139   assert all(discrete_squared(asarray([0, 3, 6])) == asarray([1.0, 3.0, 7.0])**2) 
    140  
    141  
    142128if __name__ == '__main__': 
    143129  test_monitored() 
     
    146132  test_bounded() 
    147133  test_mixedin() 
    148   test_discrete() 
    149134  test_clip_bounded() 
    150135  test_target_bounded() 
  • branches/decorate/wrapper.py

    r643 r646  
    4141 
    4242 
    43 from numpy import asarray, any, inf, vectorize, choose, zeros, ones, ndarray 
     43from numpy import asarray, any, inf, vectorize 
    4444def bounded(min=None, max=None): 
    4545    """impose an infinite barrier box constraint on a function 
     
    118118 
    119119 
    120 #from random import sample, choice 
    121 def discrete(samples, index=None): 
    122     """impose a discrete set of input values for the selected function 
    123  
    124 The function's input will be mapped to the given discrete set 
    125  
    126 >>> @discrete([1.0, 2.0]) 
    127 ... def identity(x): 
    128 ...     return x 
    129  
    130 >>> identity([0.123, 1.789, 4.000]) 
    131 [1.0, 2.0, 2.0] 
    132  
    133 >>> @discrete([1,3,5,7], index=(0,3)) 
    134 ... def squared(x): 
    135 ....    return [i**2 for i in x] 
    136  
    137 >>> squared([0,2,4,6,8,10]) 
    138 [1, 4, 16, 25, 64, 100]""" 
    139     samples = [asarray(samples)] 
    140     samples[0].sort() 
    141     if isinstance(index, int): index = (index,) 
    142     index = [index] 
    143  
    144     #XXX: refactor to use points_factory(samples) 
    145     def _points(alist): 
    146         alist = asarray(alist) 
    147         alist.sort() 
    148         samples[0] = alist 
    149  
    150     def _index(alist=None): 
    151         index[0] = alist 
    152  
    153     #XXX: refactor to use argnear_factory(samples) 
    154     def _argnear(xi): 
    155         arghi = sum(xi > samples[0]) 
    156         arglo = max(0, arghi - 1) # minimum = x[0] 
    157         if arghi == len(samples[0]):  
    158             arghi = arglo         # maximum = x[-1] 
    159         return arglo, arghi 
    160  
    161     def _near(xi, lo, hi): 
    162         if hi - xi < xi - lo:  
    163             return hi 
    164         return lo 
    165  
    166     argnear = vectorize(_argnear) 
    167     near = vectorize(_near) 
    168  
    169     def dec(f): 
    170         def func(x, *args, **kwds): 
    171             if isinstance(x, ndarray): xtype = asarray 
    172             else: xtype = type(x) 
    173             arglo, arghi = argnear(x) 
    174             xp = near(x, samples[0][arglo], samples[0][arghi]) 
    175             # create a choice array from given indices 
    176             #FIXME: better ways to do the following 
    177             if index[0] is None:  
    178                 mask = ones(xp.size, dtype=bool) 
    179             else: 
    180                 mask = zeros(xp.size, dtype=bool) 
    181                 try: mask[sorted(index[0], key=abs)] = True 
    182                 except IndexError: pass 
    183             xp = xtype(choose(mask, (x,xp))) 
    184             return f(xp, *args, **kwds) 
    185         func.samples = _points 
    186         func.index = _index 
    187         return func 
    188     return dec 
    189  
    190  
    191120#FIXME: the following should preserve the input type 
    192121def __clip_bound(x, amin, amax): 
Note: See TracChangeset for help on using the changeset viewer.