SmokinCaterpillar / pypet

The new python parameter exploration toolbox. pypet manages exploration of the parameter space and data storage into HDF5 files for you.
BSD 3-Clause "New" or "Revised" License
89 stars 22 forks source link

Adding parameters with a dictionary #39

Closed anandtrex closed 4 years ago

anandtrex commented 8 years ago

Is it possible to add parameters using a single dictionary?

For example, in the tutorial, instead of doing this:

traj.f_add_parameter('neuron.V_init', 0.0,
                     comment='The initial condition for the '
                                'membrane potential')
traj.f_add_parameter('neuron.I', 0.0,
                     comment='The externally applied current.')
traj.f_add_parameter('neuron.tau_V', 10.0,
                     comment='The membrane time constant in milliseconds')
traj.f_add_parameter('neuron.tau_ref', 5.0,
                    comment='The refractory period in milliseconds '
                            'where the membrane potnetial '
                            'is clamped.')

traj.f_add_parameter('simulation.duration', 1000.0,
                     comment='The duration of the experiment in '
                            'milliseconds.')
traj.f_add_parameter('simulation.dt', 0.1,
                     comment='The step size of an Euler integration step.')

It would be nice if one could do this:

parameters = dict(neuron=dict(V_init=0.0, I=0.0, tau_V=10.0, tau_ref=5.0), 
                  simulation=dict(duration=1000.0, dt=0.1))
traj.f_add_parameter(parameters)

Or with comments, each value being a tuple like V_init=(0.0, 'The initial condition for the membrane potential') and so on.

I currently have a large number of model parameters which are already parsed into a dictionary. I was unable to find definitive information on whether pypet already supports this. I think it would be a really useful feature.

SmokinCaterpillar commented 8 years ago

No not directly (yet), but you may use the flatten_dictionary function in pypet.utils.helpful_functions:

    from pypet.utils.helpful_functions import flatten_dictionary

    flatten_dict = flatten_dictionary(nested_dict, '.')
    for name, val in flatten_dict.items():
               traj.f_add_parameter(name, val)
anandtrex commented 8 years ago

Cool. That seems to work. Thanks.

anandtrex commented 8 years ago

I added some further functionality to that function to be able to handle lists of dictionaries within the dictionary. For example, if one of the values happened to be dict(a=[dict(b=1), dict(b=2)] this would be flattened to 'a.0.b' (=1) and 'a.1.b' (=2).

If this seems to be some functionality you think might be worth having in the main pypet codebase, I could send a pull request.

SmokinCaterpillar commented 8 years ago

Sorry for my delayed answer. How, do you know or distinguish if a list in your nested dictionary is not user data, i.e. should translate into data in a leaf node, but a list of potential nodes?

SmokinCaterpillar commented 4 years ago

inactive