brian-team / brian2modelfitting Goto Github PK
View Code? Open in Web Editor NEWModel fitting toolbox for the Brian 2 simulator
Home Page: https://brian2modelfitting.readthedocs.io
License: Other
Model fitting toolbox for the Brian 2 simulator
Home Page: https://brian2modelfitting.readthedocs.io
License: Other
When doing an optimization, the program reports the current best value for parameters. However, this is stated as a list of values in random order. To be more useful, it should be shown as a dictionary (ideally with units).
E.g., visualization of conditional distribution given all other parameters, visualization of conditional correlation matrix of a distribution...
It should be possible to create an example trace after fitting, and then to continue fitting (at least in runtime mode), but currently it fails like this (after adding a fits = fitter.generate_traces()
in between the two fitting calls in hh_nevergrad_multiruns.py):
Traceback (most recent call last):
File "hh_nevergrad_multiruns.py", line 75, in <module>
Cm=[0.1*ufarad*cm**-2 * area, 2*ufarad*cm**-2 * area],)
File "/home/marcel/programming/brian2modelfitting/brian2modelfitting/fitter.py", line 479, in fit
callback, restart, **params)
File "/home/marcel/programming/brian2modelfitting/brian2modelfitting/fitter.py", line 303, in fit
metric)
File "/home/marcel/programming/brian2modelfitting/brian2modelfitting/fitter.py", line 229, in optimization_iter
self.simulator.run(self.duration, d_param, self.parameter_names)
File "/home/marcel/programming/brian2modelfitting/brian2modelfitting/simulator.py", line 100, in run
self.network[name].set_states(params, units=False)
File "/mnt/data/anaconda2/envs/brian2modelfitting/lib/python3.6/site-packages/brian2/core/network.py", line 395, in __getitem__
raise KeyError('No object with name "%s" found' % item)
KeyError: 'No object with name "neurons" found'
Currently it is possible to include only the part t>t_start in the calculation of error. It would be nice to be able to weight differently different parts of the traces. Typically, with responses to pulses, we might want to weight the onset and offset of the pulse more strongly. A simple way would be to provide a weight trace, of the same size as the other traces.
I'm pretty new to python, so this could be an issue with what I have done. I pip installed brian2 and brian2modelfitting. Now I'm trying to run the Multi-run Hodgkin-Huxley example (https://brian2modelfitting.readthedocs.io/en/stable/examples/hh_multirun.html). I copied and pasted the code from the tutorial but when I run it I get the error:
ValueError Traceback (most recent call last)
[c:\Users\mmgee\Documents\DMV\test_brian2modelfitting.py](file:///C:/Users/mmgee/Documents/DMV/test_brian2modelfitting.py) in line 55
[48](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=47) ## Fitting
[49](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=48) fitter = TraceFitter(model=eqs, input_var='I', output_var='v',
[50](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=49) input=inp_traces*amp, output=out_traces*mV, dt=dt,
[51](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=50) n_samples=20,
[52](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=51) param_init={'v': -65*mV},
[53](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=52) method='exponential_euler')
---> [55](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=54) res, error = fitter.fit(n_rounds=2,
[56](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=55) optimizer=n_opt, metric=metric,
[57](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=56) callback='progressbar',
[58](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=57) gl = [1e-09 *siemens, 1e-07 *siemens],
[59](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=58) g_na = [2e-06*siemens, 2e-04*siemens],
[60](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=59) g_kd = [6e-07*siemens, 6e-05*siemens],
[61](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=60) Cm=[0.1*ufarad*cm**-2 * area, 2*ufarad*cm**-2 * area])
[63](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=62) ## Show results
[64](file:///c%3A/Users/mmgee/Documents/DMV/test_brian2modelfitting.py?line=63) all_output = fitter.results(format='dataframe')
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2modelfitting\fitter.py:866](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py:866), in TraceFitter.fit(self, optimizer, metric, n_rounds, callback, restart, start_iteration, penalty, level, **params)
[861](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=860) raise ValueError("The 't_weights' argument of the metric has "
[862](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=861) "to be a one-dimensional array of length "
[863](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=862) f"{self.output.shape[1]} but has shape "
[864](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=863) f"{metric.t_weights.shape}")
[865](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=864) self.bounds = dict(params)
--> [866](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=865) best_params, error = super().fit(optimizer, metric, n_rounds,
[867](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=866) callback, restart,
[868](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=867) start_iteration=start_iteration,
[869](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=868) penalty=penalty,
[870](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=869) level=level+1,
[871](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=870) **params)
[872](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=871) return best_params, error
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2modelfitting\fitter.py:590](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py:590), in Fitter.fit(self, optimizer, metric, n_rounds, callback, restart, online_error, start_iteration, penalty, level, **params)
[586](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=585) # Check whether we can reuse the current simulator or whether we have
[587](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=586) # to create a new one (only relevant for standalone, but does not hurt
[588](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=587) # for runtime)
[589](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=588) if self.simulator is None or self.simulator.current_net != 'fit':
--> [590](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=589) self.simulator = self.setup_simulator('fit', self.n_neurons,
[591](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=590) output_var=self.output_var,
[592](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=591) online_error=online_error,
[593](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=592) param_init=self.param_init,
[594](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=593) level=level+1)
[596](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=595) # Run Optimization Loop
[597](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=596) for index in range(n_rounds):
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2modelfitting\fitter.py:365](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py:365), in Fitter.setup_simulator(self, network_name, n_neurons, output_var, param_init, calc_gradient, optimize, online_error, level)
[362](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=361) if self.output_var != 'spikes':
[363](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=362) namespace['output_var'] = TimedArray(self.output.transpose(),
[364](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=363) dt=self.dt)
--> [365](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=364) neurons = self.setup_neuron_group(n_neurons, namespace,
[366](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=365) calc_gradient=calc_gradient,
[367](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=366) optimize=optimize,
[368](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=367) online_error=online_error)
[369](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=368) network = Network(neurons)
[370](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=369) if isinstance(output_var, str):
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2modelfitting\fitter.py:418](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py:418), in Fitter.setup_neuron_group(self, n_neurons, namespace, calc_gradient, optimize, online_error, name)
[416](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=415) kwds['method'] = self.method
[417](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=416) model = self.model + Equations('iteration : integer (constant, shared)')
--> [418](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=417) neurons = NeuronGroup(n_neurons, model,
[419](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=418) threshold=self.threshold, reset=self.reset,
[420](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=419) refractory=self.refractory, name=name,
[421](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=420) namespace=namespace, dt=self.dt, **kwds)
[422](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=421) if calc_gradient:
[423](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=422) sensitivity_eqs = get_sensitivity_equations(neurons,
[424](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=423) parameters=self.parameter_names,
[425](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=424) optimize=optimize,
[426](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2modelfitting/fitter.py?line=425) namespace=namespace)
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2\groups\neurongroup.py:538](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py:538), in NeuronGroup.__init__(self, N, model, method, method_options, threshold, reset, refractory, events, namespace, dtype, dt, clock, order, name, codeobj_class)
[520](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=519) def __init__(
[521](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=520) self,
[522](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=521) N,
(...)
[536](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=535) codeobj_class=None,
[537](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=536) ):
--> [538](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=537) Group.__init__(
[539](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=538) self,
[540](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=539) dt=dt,
[541](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=540) clock=clock,
[542](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=541) when="start",
[543](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=542) order=order,
[544](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=543) namespace=namespace,
[545](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=544) name=name,
[546](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=545) )
[547](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=546) if dtype is None:
[548](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/groups/neurongroup.py?line=547) dtype = {}
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2\units\fundamentalunits.py:2780](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py:2780), in check_units.<locals>.do_check_units.<locals>.new_f(*args, **kwds)
[2770](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2769) error_message = (
[2771](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2770) f"Function '{f.__name__}' "
[2772](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2771) "expected a quantitity with unit "
[2773](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2772) f"{unit} for argument '{k}' but got "
[2774](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2773) f"'{value}'"
[2775](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2774) )
[2776](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2775) raise DimensionMismatchError(
[2777](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2776) error_message, get_dimensions(newkeyset[k])
[2778](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2777) )
-> [2780](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2779) result = f(*args, **kwds)
[2781](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2780) if "result" in au:
[2782](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/units/fundamentalunits.py?line=2781) if isinstance(au["result"], Callable) and au["result"] != bool:
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brian2\core\base.py:161](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/mmgee/Documents/DMV/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py:161), in BrianObject.__init__(self, dt, clock, when, order, namespace, name)
[159](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=158) for key in namespace:
[160](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=159) if key.startswith("_"):
--> [161](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=160) raise ValueError(
[162](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=161) "Names starting with underscores are "
[163](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=162) "reserved for internal use an cannot be "
[164](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=163) "defined in the namespace argument."
[165](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=164) )
[166](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=165) #: The group-specific namespace
[167](file:///c%3A/Users/mmgee/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/LocalCache/local-packages/Python39/site-packages/brian2/core/base.py?line=166) self.namespace = namespace
ValueError: Names starting with underscores are reserved for internal use an cannot be defined in the namespace argument.
Enable the use of GPU for training neural density estimator.
The GPU is expected be effective only for large neural networks with operations that are inherently fast on the GPU, e.g., convolutions. No speed ups in training time will be achieved for the default architectures provided in sbi
.
After updating nevergrad to version 0.3, our test suite fails: https://travis-ci.org/brian-team/brian2modelfitting/jobs/638442404
It fails with an error in the "instrumentation" code:
def _check_shape(self, x: np.ndarray) -> None:
if self.shape != (1,) and x.shape != self.shape:
> raise ValueError(f"Shapes do not match: {self.shape} and {x.shape}")
E ValueError: Shapes do not match: () and (1,)
../../../virtualenv/python3.6.7/lib/python3.6/site-packages/nevergrad/instrumentation/transforms.py:127: ValueError
I did not look in detail, but I think it expects a scalar where we provide a length-1 array (or the other way around).
Instead of asking the user to provide metrics to extract features, it might be possible to automatically reduce the dimensionality of the output (e.g. voltage trace)?
[Refers to the current state of PR #2]
Again, very minor/trivial: in Brian, we usually allow to give equations as strings or as Equations
object. The Fitter
currently requires the model to be given as an Equations
object, we should simply wrap it if it is a simple string.
The first stages of the process detailed in #44 might potentially have to simulate a large number of samples, too big to fit into memory as part of a single simulation. As a first step, we might simply let the user deal with this (e.g. by calling something like inferencer.simulate(n_samples=10_000)
repeatedly). A fancier solution would be to ask the user for the maximum memory that the simulation should use, estimate the memory usage of the monitors and split up the simulation accordingly. Even more automatic (but maybe too automatic/too complicated to get right on all platforms?) would be to look at the actual memory available.
Currently, users can provide FeatureMetric
with a list of feature names known to eFEL. In addition to that functionality, it should be possible to hand over a function instead of a name to support arbitrary feature calculations not supported by eFEL.
This class should take parameters similarly to Fitter
, and should wrap the first stages of the sbi process:
It seems that best parameters are unitless. But the parameter ranges, on the other hand, are given with units. It would be more consistent to have units in results too.
[Refers to the current state of PR #2]
Of course a very minor issue, but if possible we should use a more specific exception type than Exception
(in most places probably ValueError
and TypeError
)
"optimizer has to be a child of class Optimizer"
instead?There's one optimization strategy with highly nonlinear systems which could be interesting to explore. It consists in adding a coupling term k*(V_recording-V_model), which decays over the time of fitting. This could be implemented quite simply by having k as a parameter, and the error criterion is k**2 + a*sum((V_recording-V_model)**2).
At the end we might want to have a second fitting run with k=0 (but starting from the final population?).
Shouldn't we close brian2modelfitting and merge with brian2tools? (see wiki)
I think it would be useful to interface with sequential optimization algorithms. For example, lmfit has support for parameter bounds. Our choice of using parallelizable algorithms is for performance reasons, but in practice I found that around 100 000 runs are necessary to fit a model to our data. So, it could very well be that using a more standard sequential algorithm requires fewer runs and ends up being more efficient.
I guess it should be possible to make an ask and tell interface for such algorithms. For example, the algorithm (eg lmfit) runs in a separate thread and calls a function f, which informs an ask method, blocks, and unblocks when tell is called.
I'm not sure it's a bug or a documentation issue. When I get results with fitter.results
, I obtain 10 times more values than specified with TraceFitter(n_samples=...)
.
[Refers to the current state of PR #2]
Currently, SpikeFitter
and TraceFitter
require the specification of a metric and an optimizer, but we could have default values (MSEMetric
for TraceFitter
, GammaFactor
for SpikeFitter
, NevergradOptimizer
for both?).
Both classes have a lot in common (everything related to the Brian simulation), and should inherit from a common superclass to share the code. Another possible approach would be to put all the shared code in a separate class that neither Fitter
nor Inferencer
inherits from, and delegate to an object of that class (e.g. everything related to the simulation could be in a BrianSimulator
class, and both Fitter
and Inferencer
would do something like self.brian_simulator = BrianSimulator(...)
in their __init__
and then call self.brian_simulator...
for all simulation-related tasks.) Actually, there is already a Simulator
class (with implementations for runtime and standalone) โ maybe some more code could simply move into this class.
During testing, I noticed a peculiar problem: the recommendation that we get from Nevergrad
is not necessarily the best parameter combination that we have seen so far (at least for the differential evolution algorithm that we use by default, but I think it is more or less the same issue for the other optimizers). The reason is that Nevergrad
always assumes that simulations are noisy, so it will not use the observed error as such. Instead, it will try to estimate lower and upper bounds on the error. This is in itself not unreasonable, even though our tests and examples all use deterministic models, so re-evaluating a parameter combination for a second time is a waste of time. But the main problem is that we can run into a numerical issue: Our errors tend to be rather small numerical values, because we use the mean squared error (and not its square root). If we are fitting currents, errors could be on the order of nA, i.e. 1e-9 and the squared error therefore on the order of 1e-18. Now, Nevergrad
will not base its recommendation based on the observed error, but on its estimated "lower bound". This lower bound is calculated by reducing the average observed error by something like 10% of the standard error of the observed values. This should be fine if there were indeed several observed values (in a deterministic system, the standard error would be 0 of course), but when running a relatively small number of parameters in a high-dimensional space, almost all parameter combinations have only been tried once. In this case, Nevergrad
uses a default variance value that is very inappropriate in our case:
# TODO May be safer to use a default variance which depends on y for scale invariance?
self.variance = 1.e6
Using this variance value that is many orders of magnitude higher than typical errors, leads to a dramatic loss in accuracy, e.g. a 10 fold difference between 1e-16 and 1e-15 will be considered the same error and which of the two gets recommended as the "best solution" is completely arbitrary.
I could think of various ways to fix this:
float128
when calculating our errors, hoping that the type propagates to Nevergrad
Nevergrad
's recommendation at all, but simply return the value with the lowest error (we keep track of all parameters and their errors ourselves already).The last option is probably the easiest and least prone to break with changes in Nevergrad
?
It would be nice to have some tools to analyze population parameters. Useful ones would be:
There are a number of exceptions using multiline strings like this:
brian2modelfitting/brian2modelfitting/modelfitting/metric.py
Lines 241 to 242 in a4d78d7
this has the unwanted side effect that all the spaces at the start of the second line are included in the string:
>>> raise ValueError('you can only use features that return \
one value')
[...]
ValueError: you can only use features that return one value
A better solution than using the backslash is to use Python's implied continuation:
raise ValueError('you can only use features that return '
'one value')
[Refers to the current state of PR #2]
I think we already had this option at the point: we need to be able to exclude a certain part of the simulation in the calculation of the error (ignoring a certain time in the beginning should be enough I guess). This could be an (optional) argument to the metric.
Currently, the sbi Inferencer
always works on traces, but it should also work on spikes (cf. SpikeFitter
). Internally, this should already mostly work if the name of the output variable is set to spikes
. Note that features will no longer receive an array, but instead a list of arrays, since for each parameter combination/input, there can be a different number of spikes.
The first stages of the sbi process (simulation and training of the network) are time-consuming. The results can be reused for several experimental data sets, so it would be wasteful to simulate more often than necessary. Check whether sbi provides mechanisms to store/load these results and wrap them as part of the class created for #44 .
[Refers to the current state of PR #2]
In this code:
brian2modelfitting/brian2modelfitting/modelfitting/modelfitting.py
Lines 268 to 280 in 4cb7035
the result returned by the library is the current best result, i.e. the parameter combination giving the best result over all iterations. This is what is returned in the end, and also what is handed over to the callback. This is inconsistent with the errors that are displayed and returned: they are the errors for the current iteration, the minimal error is therefore only the minimal error for the current iteration and not the overall minimum. The callback will display the (global) best parameters together with the best error of the current iteration, and therefore give misleading information.
Minor issue: the error minimum should probably use nanmin
to ignore NaN
values.
Hi, when installing using pip, it installs nevergrad 0.4.1post3 which is not compatible.
It raises,
from nevergrad import instrumentation as inst
ImportError: cannot import name 'instrumentation'
Going back to nevegrad 0.3.0 solved the problem for me.
Best,
For users not using simulation-based inference, forcing an installation of sbi
does not make a lot of sense. In particular since it pulls in pytorch, which can be huge (wheel package > 500 Mo).
I got this:
ERROR Brian 2 encountered an unexpected error. If you think this is bug in Brian 2, please report this issue either to the mailing list at <http://groups.google.com/group/brian-development/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /var/folders/m7/w45_jgp92nv46f4mjs9g8_d00000gn/T/brian_debug_zhp0bptb.log Additionally, you can also include a copy of the script that was run, available at: /var/folders/m7/w45_jgp92nv46f4mjs9g8_d00000gn/T/brian_script_wbk4k1u0.py Thanks! [brian2]
Traceback (most recent call last):
File "/Users/romainbrette/PycharmProjects/Paramecium-model-fitting/scripts/run_fit.py", line 202, in <module>
callback='text', **model.bounds)
File "/Users/romainbrette/PycharmProjects/brian2modelfitting/brian2modelfitting/fitter.py", line 495, in fit
callback, restart, **params)
File "/Users/romainbrette/PycharmProjects/brian2modelfitting/brian2modelfitting/fitter.py", line 302, in fit
**params)
File "/Users/romainbrette/PycharmProjects/brian2modelfitting/brian2modelfitting/optimizer.py", line 163, in initialize
[bounds[i][1]])
File "/anaconda3/envs/Paramecium-model-fitting/lib/python3.7/site-packages/nevergrad/instrumentation/variables.py", line 315, in bounded
return self.with_transform(Transf(a_min=a_min, a_max=a_max))
File "/anaconda3/envs/Paramecium-model-fitting/lib/python3.7/site-packages/nevergrad/instrumentation/transforms.py", line 183, in __init__
self._b = .5 * (self.a_max + self.a_min)
TypeError: can't multiply sequence by non-int of type 'float'
For more than one output variable, the features
argument should not be a list, but instead a dictionary mapping each output variable name to a list of features.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.