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
53 stars 52 forks source link

Optimization example error #679

Closed raunakvijayakar closed 11 months ago

raunakvijayakar commented 11 months ago

Hello, I'm running into an error when I try to run the example code, "05. Optimize simulated evoked response parameters." I haven't modified the example, and the documentation version matches my hnn-core version. I've had no errors in running the other code examples.

Everything runs fine until I reach the optimization block:

from hnn_core.optimization import optimize_evoked
with MPIBackend(n_procs=n_procs):
    net_opt = optimize_evoked(net, tstop=tstop, n_trials=1,
                              target_dpl=exp_dpl, initial_dpl=initial_dpl,
                              scale_factor=scale_factor,
                              smooth_window_len=smooth_window_len)

The first iteration of optimization step one runs without issue, but the following error appears before the second iteration can begin:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File [~/anaconda3/envs/hnn_core_env/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py:141](https://file+.vscode-resource.vscode-cdn.net/Users/M298129/Desktop/Mayo/23%3A24/Q2/R2/hnn_core/~/anaconda3/envs/hnn_core_env/lib/python3.11/site-packages/scipy/optimize/_differentiable_functions.py:141), in ScalarFunction.__init__..fun_wrapped(x)
    140 try:
--> 141     fx = np.asarray(fx).item()
    142 except (TypeError, ValueError) as e:

ValueError: can only convert an array of size 1 to a Python scalar

As you can see, the line refers to _differentiable_functions.py from scipy.

System Information hnn-core v0.3 Macbook Pro M2 (ventura 13.5.2) python 3.11.4

I have also replicated the error on a Macbook Pro M1 and an iMac with intel procs.

Thank you.

ntolley commented 11 months ago

Thanks for pointing this out! I suspect this is due to one of our dependencies putting out an update since our last release of hnn-core Will do some digging and get back to you.

rythorpe commented 11 months ago

Which version are you using @raunakvijayakar? I suspect this is actually a bug fixed in #670.

raunakvijayakar commented 11 months ago

Hi @rythorpe, I was using hnn v0.3, now I've updated to v0.4.dev0 and optimization is working. Thank you so much!

rythorpe commented 11 months ago

Great, let us know if anything else comes up! Note that our development branch also has a new Optimizer class to try out if you're ever in need of a more flexible API for optimizing different types of simulation metrics. The old version works just fine as well, but check out this tutorial if you're interested. :)