Changeset 750


Ignore:
Timestamp:
09/07/14 16:12:20 (21 months ago)
Author:
mmckerns
Message:

clean forward_model docs; removed ForwardModel?1; fix: observations in sum;
name in addModel now generated by default

Location:
mystic
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/NOTES

    r470 r750  
    7777x = (array([range(101)])-50.)[0] 
    7878F = CostFactory() 
    79 F.addModel(ForwardPolyFactory,'poly',3) 
     79F.addModel(ForwardPolyFactory,3,'poly') 
    8080myCost = F.getCostFunction(evalpts=x, observations=datapts) 
    8181 
  • mystic/examples/example12.py

    r722 r750  
    3838    from numpy import sum 
    3939    F = CostFactory() 
    40     F.addModel(ForwardPolyFactory,"noisy_polynomial",ndim) 
     40    F.addModel(ForwardPolyFactory,ndim,"noisy_polynomial") 
    4141    return F.getCostFunction(evalpts=evalpts,observations=datapts, \ 
    4242                             metric=lambda x: sum(x*x),sigma=1000.) 
  • mystic/examples/forward_model.py

    r713 r750  
    2121 
    2222    A = CostFactory() 
    23     A.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter=PickComponent(2)) 
    24     A.addModel(ForwardMogiFactory, 'mogi2', 4, outputFilter=PickComponent(2)) 
     23    A.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter=PickComponent(2)) 
     24    A.addModel(ForwardMogiFactory, 4, 'mogi2', outputFilter=PickComponent(2)) 
    2525    fe = A.getForwardEvaluator(stations) 
    2626    p = [random.random() for i in range(8)] 
  • mystic/examples/test_getCost.py

    r722 r750  
    7272  ##myCost = PolyCostFactory(target,x) 
    7373    F = CostFactory() 
    74     F.addModel(ForwardPolyFactory,'poly',len(target)) 
     74    F.addModel(ForwardPolyFactory,len(target),'poly') 
    7575    myCost = F.getCostFunction(evalpts=x, observations=datapts)     
    7676 
  • mystic/examples/test_lorentzian.py

    r721 r750  
    101101    from mystic.forward_model import CostFactory 
    102102    CF = CostFactory() 
    103     CF.addModel(F, 'lorentz', ND) 
     103    CF.addModel(F, ND, 'lorentz') 
    104104    myCF = CF.getCostFunction(binsc, histo/N) 
    105105    sol, steps = de_solve(myCF) 
  • mystic/examples/test_mogi3.py

    r713 r750  
    3636if __name__ == '__main__': 
    3737    F = CostFactory() 
    38     F.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter = PickComponent(2)) 
     38    F.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter = PickComponent(2)) 
    3939    myCostFunction = F.getCostFunction(evalpts = stations, observations = data_z) 
    4040    print F 
  • mystic/examples/test_mogi4.py

    r713 r750  
    4242 
    4343    F = CostFactory() 
    44     F.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter = PickComponent(2, -1)) 
    45     F.addModel(ForwardMogiFactory, 'mogi2', 4, outputFilter = PickComponent(2, -1)) 
     44    F.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter = PickComponent(2, -1)) 
     45    F.addModel(ForwardMogiFactory, 4, 'mogi2', outputFilter = PickComponent(2, -1)) 
    4646    myCostFunction = F.getCostFunction(evalpts = stations, observations = data_z) 
    4747    print F 
  • mystic/models/abstract_model.py

    r713 r750  
    104104        datapts = self.evaluate(target,pts) 
    105105        F = CF() 
    106         F.addModel(self.ForwardFactory,self.__name__,len(target)) 
     106        F.addModel(self.ForwardFactory,len(target),self.__name__) 
    107107        self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=self.__sigma__,metric=self.__metric__) 
    108108        return self.__cost__ 
     
    112112and evaluation points""" 
    113113        F = CF() 
    114         F.addModel(self.ForwardFactory,self.__name__,nparams) 
     114        F.addModel(self.ForwardFactory,nparams,self.__name__) 
    115115        self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=self.__sigma__,metric=self.__metric__) 
    116116        return self.__cost__ 
  • mystic/models/br8.py

    r713 r750  
    5151        datapts = self.evaluate(target,pts) 
    5252        F = CF() 
    53         F.addModel(self.ForwardFactory,self.__name__,len(target)) 
     53        F.addModel(self.ForwardFactory,len(target),self.__name__) 
    5454        self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=sqrt(datapts),metric=self.__metric__) 
    5555        return self.__cost__ 
     
    5858        """generates a cost function instance from datapoints & evaluation points""" 
    5959        F = CF() 
    60         F.addModel(self.ForwardFactory,self.__name__,nparams) 
     60        F.addModel(self.ForwardFactory,nparams,self.__name__) 
    6161        self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=sqrt(datapts),metric=self.__metric__) 
    6262        return self.__cost__ 
  • mystic/mystic/forward_model.py

    r721 r750  
    1717The basic usage pattern for a cost factory is to generate a cost function 
    1818from a set of data points and a corresponding set of evaluation points. 
    19 The cost factory requires a "forward model factory", which is just a generator 
    20 of forward model instances from a list of coefficients. The following example 
     19The cost factory requires a "model factory", which is just a generator 
     20of model function instances from a list of coefficients. The following example 
    2121uses numpy.poly1d, which provides a factory for generating polynomials. An 
    2222expanded version of the following can be found in `mystic.examples.example12`. 
    2323 
    24     >>> # get a forward model factory, and generate some evaluation points 
    25     >>> from numpy import array, sum, poly1d, random 
    26     >>> ForwardFactory = poly1d 
    27     >>> pts = 0.1*(array([range(101)])-50.)[0] 
     24    >>> # get a model factory 
     25    >>> import numpy as np 
     26    >>> FunctionFactory = np.poly1d 
    2827    >>>  
    29     >>> # we don't have real data, so generate some fake data from the model 
     28    >>> # generate some evaluation points 
     29    >>> xpts = 0.1 * np.arange(-50.,51.) 
     30    >>>  
     31    >>> # we don't have real data, so generate fake data from target and model 
    3032    >>> target = [2.,-5.,3.] 
    31     >>> datapts = [random.normal(0,1) + i for i in ForwardFactory(target)(pts)] 
     33    >>> ydata = FunctionFactory(target)(xpts) 
     34    >>> noise = np.random.normal(0,1,size=len(ydata)) 
     35    >>> ydata = ydata + noise 
    3236    >>>  
    3337    >>> # get a cost factory 
    3438    >>> from mystic.forward_model import CostFactory 
    35     >>> F = CostFactory() 
     39    >>> C = CostFactory() 
    3640    >>>  
    3741    >>> # generate a cost function for the model factory 
    38     >>> costmetric = lambda x: sum(x*x) 
    39     >>> F.addModel(ForwardFactory, name='example', inputs=len(target)) 
    40     >>> costfunction = F.getCostFunction(evalpts=pts, observations=datapts, 
    41     ...                                  sigma=1.0, metric=costmetric) 
     42    >>> metric = lambda x: np.sum(x*x) 
     43    >>> C.addModel(FunctionFactory, inputs=len(target)) 
     44    >>> cost = C.getCostFunction(evalpts=xpts, observations=ydata, 
     45    ...                                        sigma=1.0, metric=metric) 
    4246    >>> 
    4347    >>> # pass the cost function to the optimizer 
    4448    >>> initial_guess = [1.,-2.,1.] 
    45     >>> solution = fmin_powell(costfunction, initial_guess) 
     49    >>> solution = fmin_powell(cost, initial_guess) 
     50    >>> print solution 
     51    [ 2.00495233 -5.0126248   2.72873734] 
     52 
    4653 
    4754In general, a user will be required to write their own model factory. 
     
    5966from numpy import pi, sqrt, array, mgrid, random, real, conjugate, arange, sum 
    6067#from numpy.random import rand 
    61  
    62  
    63 class ForwardModel1(object): 
    64     """ 
    65 A simple utility to 'prettify' object representations of forward models. 
    66     """ 
    67     def __init__(self, func, inputs, outputs): 
    68         """ 
    69 Takes three initial inputs: 
    70     func      -- the forward model 
    71     inputs    -- the number of inputs 
    72     outputs   -- the number of outputs 
    73  
    74 Example: 
    75     >>> f = lambda x: sum(x*x) 
    76     >>> fwd = ForwardModel1(f,1,1) 
    77     >>> fwd 
    78     func: ['x0'] -> ['y0'] 
    79     >>> fwd(1) 
    80     1 
    81     >>> fwd(2) 
    82     4 
    83         """ 
    84         self._func = func 
    85         self._m = inputs 
    86         self._n = outputs 
    87         self._inNames = ['x%d'%i for i in range(self._m)] 
    88         self._outNames = ['y%d'%i for i in range(self._n)] 
    89  
    90     def __call__(self, *args, **kwds): 
    91         return self._func(*args, **kwds) 
    92  
    93     def __repr__(self): 
    94         return 'func: %s -> %s' % (self._inNames, self._outNames) 
    9568 
    9669class CostFactory(object): 
     
    11487        pass 
    11588 
    116     def addModel(self, model, name, inputs, outputFilter = Identity, inputChecker = NullChecker): 
     89    def addModel(self, model, inputs, name=None, outputFilter=Identity, inputChecker=NullChecker): 
    11790        """ 
    11891Adds a forward model factory to the cost factory. 
     
    12093Inputs: 
    12194    model   -- a callable function factory object 
     95    inputs  -- number of input arguments to model 
    12296    name    -- a string representing the model name 
    123     inputs  -- number of input arguments to model 
    124         """ 
    125         if name in self._names: 
    126              print "Model [%s] already in database." % name 
    127              raise AssertionError 
     97 
     98Example: 
     99    >>> import numpy as np 
     100    >>> C = CostFactory() 
     101    >>> C.addModel(np.poly, inputs=3) 
     102        """ 
     103        if name is None: 
     104            name = dill.source.getname(model) 
     105        if name is None: 
     106            for i in range(len(self._names)+1): 
     107                name = 'model'+str(i) 
     108                if name not in self._names: break 
     109        elif name in self._names: 
     110            print "Model [%s] already in database." % name 
     111            raise AssertionError 
    128112        self._names.append(name) 
    129113        self._forwardFactories.append(model) 
     
    145129        #NOTE: better to replace "old-style" addModel above? 
    146130        if name in self._names: 
    147              print "Model [%s] already in database." % name 
    148              raise AssertionError 
     131            print "Model [%s] already in database." % name 
     132            raise AssertionError 
    149133        self._names.append(name) 
    150134        self._forwardFactories.append(model) 
     
    162146Inputs: 
    163147    evalpts -- a list of evaluation points 
     148 
     149Example: 
     150    >>> import numpy as np 
     151    >>> C = CostFactory() 
     152    >>> C.addModel(np.poly, inputs=3) 
     153    >>> F = C.getForwardEvaluator([1,2,3,4,5]) 
     154    >>> F([1,0,0]) 
     155    [array([ 1,  4,  9, 16, 25])] 
     156    >>> F([0,1,0]) 
     157    [array([1, 2, 3, 4, 5])] 
    164158        """ 
    165159        #NOTE: does NOT go through inputChecker 
     
    187181 
    188182NOTE: Input parameters do NOT go through filters registered as inputCheckers. 
     183 
     184Example: 
     185    >>> import numpy as np 
     186    >>> C = CostFactory() 
     187    >>> C.addModel(np.poly, inputs=3) 
     188    >>> x = np.array([-2., -1., 0., 1., 2.]) 
     189    >>> y = np.array([-4., -2., 0., 2., 4.]) 
     190    >>> F = C.getVectorCostFunction(x, y) 
     191    >>> F([1,0,0]) 
     192    0.0 
     193    >>> F([2,0,0]) 
     194    10.0 
    189195        """ 
    190196        def _(params): 
    191197            forward = self.getForwardEvaluator(evalpts) 
    192             return sum(forward(params)) - observations 
     198            return sum(forward(params) - observations) 
    193199        return _ 
    194200 
     
    209215 
    210216NOTE: Input parameters WILL go through filters registered as inputCheckers. 
     217 
     218Example: 
     219    >>> import numpy as np 
     220    >>> C = CostFactory() 
     221    >>> C.addModel(np.poly, inputs=3) 
     222    >>> x = np.array([-2., -1., 0., 1., 2.]) 
     223    >>> y = np.array([-4., -2., 0., 2., 4.]) 
     224    >>> F = C.getCostFunction(x, y, metric=lambda x: np.sum(x)) 
     225    >>> F([1,0,0]) 
     226    0.0 
     227    >>> F([2,0,0]) 
     228    10.0 
     229    >>> F = C.getCostFunction(x, y) 
     230    >>> F([2,0,0]) 
     231    34.0 
    211232        """ 
    212233        #XXX: better interface for sigma? 
     
    250271 
    251272NOTE: Input parameters do NOT go through filters registered as inputCheckers. 
     273 
     274Example: 
     275    >>> import numpy as np 
     276    >>> C = CostFactory() 
     277    >>> C.addModel(np.poly, inputs=3) 
     278    >>> x = np.array([-2., -1., 0., 1., 2.]) 
     279    >>> y = np.array([-4., -2., 0., 2., 4.]) 
     280    >>> F = C.getCostFunctionSlow(x, y) 
     281    >>> F([1,0,0]) 
     282    0.0 
     283    >>> F([2,0,0]) 
     284    100.0 
    252285        """ 
    253286        #XXX: update interface to allow metric? 
Note: See TracChangeset for help on using the changeset viewer.