jonescompneurolab / hnn-core

Simulation and optimization of neural circuits for MEG/EEG source estimates
https://jonescompneurolab.github.io/hnn-core/
BSD 3-Clause "New" or "Revised" License
50 stars 50 forks source link

[MRG] Added `BatchSimulate` class for batch simulations #782

Closed samadpls closed 1 day ago

samadpls commented 1 month ago

Closes #140

jasmainak commented 1 month ago

Looks like a good start! Can you prepare an example so I can see the class in action?

samadpls commented 1 month ago

Looks like a good start! Can you prepare an example so I can see the class in action?

Sure,

import numpy as np

def set_params(param_grid, net=None):
    """
    Set parameters in the network drives.

    Parameters
    ----------
    param_grid : dict
        Dictionary of parameter values.
    net : instance of Network, optional
        If None, a new network is created using the specified model type.
    """
    if net is None:
        net = jones_2009_model()

    # _validate_type(net, Network)

    weights_ampa = {'L2_basket': param_grid['weight_basket'],
                    'L2_pyramidal': param_grid['weight_pyr'],
                    'L5_basket': param_grid['weight_basket'],
                    'L5_pyramidal': param_grid['weight_pyr']}
    synaptic_delays = {'L2_basket': 0.1, 'L2_pyramidal': 0.1,
                       'L5_basket': 1., 'L5_pyramidal': 1.}

    print(param_grid['mu'])

    mu = param_grid['mu']
    sigma = param_grid['sigma']
    net.add_evoked_drive('evprox',
                         mu=mu,
                         sigma=sigma,
                         numspikes=1,
                         location='proximal',
                         weights_ampa=weights_ampa,
                         synaptic_delays=synaptic_delays)

param_grid = {
    'weight_basket': np.linspace(0.1, 0.5, 2),
    'weight_pyr': np.linspace(0.5, 1.0, 2),
    'mu': np.linspace(1, 6, 2),
    'sigma': np.linspace(1, 3, 2)
}

batch_simulation = BatchSimulate(set_params=set_params,tstop=100.)
simulation_results = batch_simulation.run(param_grid, n_jobs=2)
jasmainak commented 1 month ago

I meant that you should add it to the examples gallery :)

jasmainak commented 1 month ago

As it stands ... the BatchSimulate does not really enable user to do more things by writing less code ... you need to brainstorm a bit how to make it more useful. Write the API first before starting to code.

batch = BatchSimulate(func)
dpls = batch.simulate()
batch.plot_dpl() # plot dipoles in a grid
batch.plot_psd()  # overlay multiple psds or plot in a grid

I'm just dreaming ... but think of ways people may want to use such an API and how to simplify boilerplate code for them

ntolley commented 1 month ago

@jasmainak totally agree, I think the main utiluty of this function will be organizing code for saving/loading the potentially very large output files, along with some simulation metadata

To make this use less code, I think the best way will be to decide on a limited set of predefined functions. Perhaps even a class that helps users quickly specify the parameters they care about:

net = jones_2009_model()
func = make_evoked_simulator(net, weight_params='ampa', time_params='mean', custom_remove=['evprox1_L2Pyr_ampa'])
batch = BatchSimulate(func)
dpls = batch.simulate()

However, I want to avoid this make_simulator() scheme being too broad as it could quickly blow up in complexity and no be useful. The safest route right now will be to hard code the parameters for one of these functions, and have the example import the function.

Perhaps this will be a good way to replace this function too! https://github.com/jonescompneurolab/hnn-core/blob/aa773ec4ca89d92e700883d6756fa3814933d31f/hnn_core/network_models.py#L312

ntolley commented 3 weeks ago

after discussion with @jasmainak, useful features will include

codecov-commenter commented 6 days ago

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 88.09524% with 5 lines in your changes missing coverage. Please review.

Project coverage is 92.53%. Comparing base (4b7fdd7) to head (5651933).

:exclamation: Current head 5651933 differs from pull request most recent head 1277c94

Please upload reports for the commit 1277c94 to get more accurate results.

Files Patch % Lines
hnn_core/batch_simulate.py 87.80% 5 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #782 +/- ## ========================================== - Coverage 92.57% 92.53% -0.04% ========================================== Files 27 28 +1 Lines 5357 5399 +42 ========================================== + Hits 4959 4996 +37 - Misses 398 403 +5 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

ntolley commented 4 days ago

@samadpls do you think this is related to the modifications we made to remove the GUI tab?

samadpls commented 4 days ago

@samadpls do you think this is related to the modifications we made to remove the GUI tab?

I'm not sure if it's related to the removal of the GUI tab since the other PR (#754) was handling that. In this PR, I haven't made any changes related to the GUI tab. I believe it might be because of the example file. I'm currently debugging it

samadpls commented 3 days ago

@ntolley, It seems like the linkcheck passes now. I removed the import statement

from .batch_simulate import BatchSimulate

from the __init__.py file and the reference from api.rst.

ntolley commented 1 day ago

Really strange! There's definitely a step I'm forgetting in terms of adding a new file/class for importing.

In any case really great start @samadpls, going to go ahead and merge and we can continue to improve on this in the next PR