Package SloppyCell :: Module Optimization
[hide private]

Source Code for Module SloppyCell.Optimization

  1  import copy 
  2  import sys 
  3   
  4  import logging 
  5  logger = logging.getLogger('Optimization') 
  6   
  7  import scipy 
  8   
  9  import SloppyCell.KeyedList_mod as KeyedList_mod 
 10  import SloppyCell.Utility as Utility 
 11  KeyedList = KeyedList_mod.KeyedList 
 12   
13 -def fmin_powell_log_params(m, params, *args, **kwargs):
14 func = m.cost_log_params 15 16 pmin = scipy.optimize.fmin_powell(func, scipy.log(params), 17 *args, **kwargs) 18 if isinstance(params, KeyedList): 19 pout = params.copy() 20 pout.update(scipy.exp(pmin)) 21 return pout 22 else: 23 return scipy.exp(pmin)
24
25 -def fmin_log_params(m, params, *args, **kwargs):
26 def func(log_params): 27 try: 28 return m.cost_log_params(log_params) 29 except Utility.SloppyCellException: 30 logger.warn('Exception in cost evaluation. Cost set to inf.') 31 return scipy.inf
32 33 pmin = scipy.optimize.fmin(func, scipy.log(params), 34 *args, **kwargs) 35 36 if isinstance(params, KeyedList): 37 pout = params.copy() 38 pout.update(scipy.exp(pmin)) 39 return pout 40 else: 41 return scipy.exp(pmin) 42
43 -def fmin(m, params, *args, **kwargs):
44 def func(params): 45 try: 46 return m.cost(params) 47 except Utility.SloppyCellException: 48 logger.warn('Exception in cost evaluation. Cost set to inf.') 49 return scipy.inf
50 51 pmin = scipy.optimize.fmin(func, params, *args, **kwargs) 52 53 if isinstance(params, KeyedList): 54 pout = params.copy() 55 pout.update(pmin) 56 return pout 57 else: 58 return pmin 59 60
61 -def fmin_xform(m, params, xforms, invforms, *args, **kwargs):
62 """ 63 Nelder-Mead the cost over an arbitrary transform on the parameters. 64 65 m Model to minimize the cost for 66 params initial parameter estimate 67 xforms sequence of transforms (of length, len(params)) to apply to the 68 parameters before optimizing 69 invforms sequences of inverse transforms to get back to straight parameters 70 *args passed on to scipy.optimize.fmin 71 **kwargs passed on to scipy.optimize.fmin 72 For information on these, consult help(scipy.optimize.fmin) 73 """ 74 def func(xp, invforms): 75 p = [inv(xp_val) for (xp_val, inv) in zip(xp, invforms)] 76 try: 77 return m.cost(p) 78 except Utility.SloppyCellException: 79 logger.warn('Exception in cost evaluation. Cost set to inf.') 80 return scipy.inf
81 82 params = scipy.array([x(xp_val) for (xp_val, x) in zip(params, xforms)]) 83 pmin = scipy.optimize.fmin(func, params, args = (invforms,), 84 *args, **kwargs) 85 86 pmin = [inv(xp_val) for (xp_val, inv) in zip(pmin, invforms)] 87 if isinstance(params, KeyedList): 88 pout = params.copy() 89 pout.update(pmin) 90 return pout 91 else: 92 return pmin 93 94 import SloppyCell.lmopt as lmopt
95 -def fmin_lm_log_params(m, params, *args, **kwargs):
96 """ 97 Minimize the cost of a model using Levenberg-Marquadt in terms of log 98 parameters. 99 100 The *args and **kwargs represent additional parmeters that will be passed to 101 the optimization algorithm. For your convenience, the docstring of that 102 function is appended below: 103 104 """ 105 jac = lambda lp: scipy.asarray(m.jacobian_log_params_sens(lp)) 106 sln = lmopt.fmin_lm(f=m.res_log_params, x0=scipy.log(params), fprime=jac, 107 *args, **kwargs) 108 if isinstance(params, KeyedList): 109 pout = params.copy() 110 pout.update(scipy.exp(sln)) 111 return pout 112 else: 113 return scipy.exp(sln)
114 fmin_lm_log_params.__doc__ = fmin_lm_log_params.__doc__ + lmopt.fmin_lm.__doc__ 115
116 -def leastsq_log_params(m, params, *args, **kwargs):
117 # Dfun = m.jacobian_log_params_sens 118 func = m.res_log_params 119 120 pmin, msg = scipy.optimize.leastsq(func, scipy.log(params), *args, **kwargs) 121 122 if isinstance(params, KeyedList): 123 pout = params.copy() 124 pout.update(scipy.exp(pmin)) 125 return pout 126 else: 127 return scipy.exp(pmin)
128