Skip to content

Commit

Permalink
consitency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Sep 8, 2024
1 parent 7a7dce4 commit 7613d39
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion neurodsp/sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
from .cycles import sim_cycle
from .transients import sim_synaptic_kernel, sim_action_potential
from .combined import sim_combined, sim_peak_oscillation, sim_modulated_signal, sim_combined_peak
from .multi import sim_multiple, sim_across_values, sim_from_sampler
from .multi import sim_multiple, sim_across_values, sim_multi_across_values, sim_from_sampler
60 changes: 36 additions & 24 deletions neurodsp/sim/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ def params(self):
return {label : {**self.base, **params} for label, params in self._params.items()}


def make_params(self, parameters=None, **kwargs):
def make_params(self, params=None, **kwargs):
"""Make a simulation parameter definition from given parameters.
Parameters
----------
parameters : dict or list of dict
params : dict or list of dict
Parameter definition(s) to create simulation parameter definition.
**kwargs
Additional keyword arguments to create the simulation definition.
Expand All @@ -216,23 +216,23 @@ def make_params(self, parameters=None, **kwargs):
Parameter definition.
"""

return {**self.base, **self._make_params(parameters, **kwargs)}
return {**self.base, **self._make_params(params, **kwargs)}


def register(self, label, parameters=None, **kwargs):
def register(self, label, params=None, **kwargs):
"""Register a new simulation parameter definition.
Parameters
----------
label : str
Label to set simulation parameters under in `params`.
parameters : dict or list of dict
params : dict or list of dict
Parameter definition(s) to create simulation parameter definition.
**kwargs
Additional keyword arguments to create the simulation definition.
"""

self._params[label] = self._make_params(parameters, **kwargs)
self._params[label] = self._make_params(params, **kwargs)


def register_group(self, group, clear=False):
Expand All @@ -250,8 +250,8 @@ def register_group(self, group, clear=False):
if clear:
self.clear()

for label, parameters in group.items():
self.register(label, parameters)
for label, params in group.items():
self.register(label, params)


def update_base(self, n_seconds=False, fs=False):
Expand Down Expand Up @@ -349,27 +349,39 @@ def copy(self):
return deepcopy(self)


def _make_params(self, parameters=None, **kwargs):
"""Sub-function for `make_params`."""
def _make_params(self, params=None, **kwargs):
"""Sub-function to make parameter definition.
parameters = {} if not parameters else deepcopy(parameters)
Parameters
----------
params : dict or list of dict
Parameter definition(s) to create simulation parameter definition.
**kwargs
Additional keyword arguments to create the simulation definition.
Returns
-------
params : dict
Parameter definition.
"""

params = {} if not params else deepcopy(params)

if isinstance(parameters, list):
comps = [parameters.pop(0)]
kwargs = {**kwargs, **parameters[0]} if parameters else kwargs
params = self._make_combined_params(comps, **kwargs)
if isinstance(params, list):
comps = [params.pop(0)]
kwargs = {**kwargs, **params[0]} if params else kwargs
out_params = self._make_combined_params(comps, **kwargs)
else:
params = {**parameters, **kwargs}
out_params = {**params, **kwargs}

# If any base parameters were passed in, clear them
for bparam in self.base:
params.pop(bparam, None)
out_params = drop_base_params(out_params)

return params
return out_params


def _make_combined_params(self, components, component_variances=None):
"""Make parameters for combined simulations, specifying multiple components.
"""Sub-function to make parameters for combined simulations, specifying multiple components.
Parameters
----------
Expand All @@ -384,17 +396,17 @@ def _make_combined_params(self, components, component_variances=None):
Parameter definition.
"""

parameters = {}
out_params = {}

comps = {}
for comp in components:
comps.update(**deepcopy(comp))
parameters['components'] = comps
out_params['components'] = comps

if component_variances:
parameters['component_variances'] = component_variances
out_params['component_variances'] = component_variances

Check warning on line 407 in neurodsp/sim/params.py

View check run for this annotation

Codecov / codecov/patch

neurodsp/sim/params.py#L407

Added line #L407 was not covered by tests

return parameters
return out_params


class SimIters(SimParams):
Expand Down
12 changes: 6 additions & 6 deletions neurodsp/sim/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def base(self):

## PARAM UPDATERS

def param_updater(parameter):
def param_updater(param):
"""Create a lambda updater function to update a specified parameter.
Parameters
----------
parameter : str
param : str
Name of the parameter to update.
Returns
Expand All @@ -51,15 +51,15 @@ def param_updater(parameter):
Updater function which can update specified parameter in simulation parameters.
"""

return lambda params, value : params.update({parameter : value})
return lambda params, value : params.update({param : value})


def component_updater(parameter, component):
def component_updater(param, component):
"""Create a lambda updater function to update a parameter within a simulation component.
Parameters
----------
parameter : str
param : str
Name of the parameter to update.
component : str
Name of the component to update the parameter within.
Expand All @@ -70,7 +70,7 @@ def component_updater(parameter, component):
Updater function which can update specified parameter in simulation parameters.
"""

return lambda params, value : params['components'][component].update({parameter : value})
return lambda params, value : params['components'][component].update({param : value})


def create_updater(update, component=None):
Expand Down

0 comments on commit 7613d39

Please sign in to comment.