I do a lot of ODE simulations and I work with a few python parameter optimization tools (e.g. scipy.optimize.minimize, emcee) that require parameters to be passed in as a list. This makes them very cumbersome, as I have to refer to the parameters as params[0]
, params[1]
, etc. as opposed to more intuitive names that actually describe their role in the simulation. My solution to this so far has been something along the lines of this:
k1 = 1.0
k2 = 0.5
N = 0.01
params = [k1,k2,N]
def sim(params,timerange):
k1 = params[0]
k2 = params[1]
N = params[2]
# run the simulation
This is really kludgey and is unsatisfactory for a number of reasons. Whenever I need to add new parameters to the simulation I have to modify the params list and change how I'm manually unpacking the list inside the simulate function; wastes some time making new references each round of simulating, etc.
I'm wondering if there's a sane, non-kludgey solution for defining the parameters with names, passing them to a function as a list, and then referring to them by those same names inside the list.
def sim(params,timerange):
k1,k2,N = params
Is what you want I think ..,.. if you added an extra param you would just add it after N ... but it would not be an optional argument
or maybe better
def sim(*params,**kwargs):
timerange = kwargs.get('timerange',default_timerange)
K1,K2,N = params #assuming you know you have exactly 3
#then call it like so
sim(k1,k2,N,timerange=(-100,100))