Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change shapes of arguments for get_features/get_errors #9

Merged
merged 7 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 175 additions & 118 deletions brian2modelfitting/modelfitting/metric.py

Large diffs are not rendered by default.

36 changes: 23 additions & 13 deletions brian2modelfitting/modelfitting/modelfitting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from numpy import ones, array, arange, concatenate, mean, nanmin
from numpy import ones, array, arange, concatenate, mean, nanmin, reshape
from brian2 import (NeuronGroup, defaultclock, get_device, Network,
StateMonitor, SpikeMonitor, ms, device, second,
get_local_namespace, Quantity)
Expand All @@ -22,18 +22,21 @@ def get_param_dic(params, param_names, n_traces, n_samples):
return d


def get_spikes(monitor):
def get_spikes(monitor, n_samples, n_traces):
"""
Get spikes from spike monitor change format from dict to a list,
remove units.
"""
spike_trains = monitor.spike_trains()

assert len(spike_trains) == n_samples*n_traces
spikes = []
for i in arange(len(spike_trains)):
spike_list = spike_trains[i] / ms
spikes.append(spike_list)

i = -1
for sample in range(n_samples):
sample_spikes = []
for trace in range(n_traces):
i += 1
sample_spikes.append(array(spike_trains[i], copy=False))
spikes.append(sample_spikes)
return spikes


Expand Down Expand Up @@ -129,7 +132,7 @@ def __init__(self, dt, model, input, output, input_var, output_var,
self.refractory = refractory

self.input = input
self.output = output
self.output = array(output)
self.output_var = output_var
self.model = model

Expand Down Expand Up @@ -387,7 +390,8 @@ def generate(self, params=None, output_var=None, param_init=None, level=0):
name='neurons_')

if output_var == 'spikes':
fits = get_spikes(self.simulator.network['monitor_'])
fits = get_spikes(self.simulator.network['monitor_'],
1, self.n_traces)[0] # a single "sample"
else:
fits = getattr(self.simulator.network['monitor_'], output_var)

Expand Down Expand Up @@ -440,8 +444,13 @@ def calc_errors(self, metric):
Returns errors after simulation with StateMonitor.
To be used inside optim_iter.
"""
traces = getattr(self.simulator.network['monitor'], self.output_var)
errors = metric.calc(traces, self.output, self.n_traces, self.dt)
traces = getattr(self.simulator.network['monitor'],
self.output_var+'_')
# Reshape traces for easier calculation of error
traces = reshape(traces, (traces.shape[0]//self.n_traces,
self.n_traces,
-1))
errors = metric.calc(traces, self.output, self.dt)
return errors

def fit(self, optimizer, metric=None, n_rounds=1, callback='text',
Expand Down Expand Up @@ -500,8 +509,9 @@ def calc_errors(self, metric):
Returns errors after simulation with SpikeMonitor.
To be used inside optim_iter.
"""
spikes = get_spikes(self.simulator.network['monitor'])
errors = metric.calc(spikes, self.output, self.n_traces, self.dt)
spikes = get_spikes(self.simulator.network['monitor'],
self.n_samples, self.n_traces)
errors = metric.calc(spikes, self.output, self.dt)
return errors

def fit(self, optimizer, metric=None, n_rounds=1, callback='text',
Expand Down
80 changes: 34 additions & 46 deletions brian2modelfitting/tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,99 +37,87 @@ def test_init():
def test_calc_mse():
mse = MSEMetric()
out = np.random.rand(2, 20)
inp = np.random.rand(10, 20)
inp = np.random.rand(5, 2, 20)

errors = mse.calc(inp, out, 2, 0.01*ms)
errors = mse.calc(inp, out, 0.01*ms)
assert_equal(np.shape(errors), (5,))
assert_equal(mse.calc(out, out, 2, 0.1*ms), [0.])
assert(np.all(mse.calc(inp, out, 2, 0.1*ms) > 0))
assert_equal(mse.calc(np.tile(out, (5, 1, 1)), out, 0.1*ms),
np.zeros(5))
assert(np.all(mse.calc(inp, out, 0.1*ms) > 0))

def test_calc_mse_t_start():
mse = MSEMetric(t_start=1*ms)
out = np.random.rand(2, 200)
inp = np.random.rand(10, 200)
out = np.random.rand(2, 20)
inp = np.random.rand(5, 2, 20)

errors = mse.calc(inp, out, 2, 0.1*ms)
errors = mse.calc(inp, out, 0.1*ms)
assert_equal(np.shape(errors), (5,))
assert_equal(mse.calc(out, out, 2, 0.1*ms), [0.])
assert(np.all(mse.calc(inp, out, 2, 0.1*ms) > 0))

assert(np.all(errors > 0))
# Everything before 1ms should be ignored, so having the same values for
# the rest should give an error of 0
inp[:, :, 10:] = out[None, :, 10:]
assert_equal(mse.calc(inp, out, 0.1*ms), np.zeros(5))

def test_calc_gf():
assert_raises(TypeError, GammaFactor)
assert_raises(DimensionMismatchError, GammaFactor, delta=10)
assert_raises(DimensionMismatchError, GammaFactor, time=10)

inp_gf = np.round(np.sort(np.random.rand(10, 5) * 10), 2)
inp_gf = np.round(np.sort(np.random.rand(5, 2, 5) * 10), 2)
out_gf = np.round(np.sort(np.random.rand(2, 5) * 10), 2)

gf = GammaFactor(delta=10*ms, time=10*ms)
errors = gf.calc(inp_gf, out_gf, 2, 0.1*ms)
errors = gf.calc(inp_gf, out_gf, 0.1*ms)
assert_equal(np.shape(errors), (5,))
assert(np.all(errors > 0))
errors = gf.calc(out_gf, out_gf, 2, 0.1*ms)
assert_almost_equal(errors, [2.])
assert(all(errors > 0))
errors = gf.calc([out_gf]*5, out_gf, 0.1*ms)
assert_almost_equal(errors, np.ones(5)*2)

def test_get_features_mse():
mse = MSEMetric()
out_mse = np.random.rand(2, 20)
inp_mse = np.random.rand(6, 20)

features = mse.get_features(inp_mse, out_mse, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 3))
assert(np.all(np.array(features) > 0))

features = mse.get_features(out_mse, out_mse, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 1))
assert_equal(features, [[0.], [0.]])


def test_get_features_mse_t_start():
mse = MSEMetric(t_start=1*ms)
out_mse = np.random.rand(2, 200)
inp_mse = np.random.rand(6, 200)
inp_mse = np.random.rand(5, 2, 20)

features = mse.get_features(inp_mse, out_mse, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 3))
features = mse.get_features(inp_mse, out_mse, 0.1*ms)
assert_equal(np.shape(features), (5, 2))
assert(np.all(np.array(features) > 0))

features = mse.get_features(out_mse, out_mse, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 1))
assert_equal(features, [[0.], [0.]])
features = mse.get_features(np.tile(out_mse, (5, 1, 1)), out_mse, 0.1*ms)
assert_equal(np.shape(features), (5, 2))
assert_equal(features, np.zeros((5, 2)))


def test_get_errors_mse():
mse = MSEMetric()
errors = mse.get_errors(np.random.rand(10, 5))
print(errors)
errors = mse.get_errors(np.random.rand(5, 10))
assert_equal(np.shape(errors), (5,))
assert(np.all(np.array(errors) > 0))

errors = mse.get_errors(np.zeros((10, 2)))
errors = mse.get_errors(np.zeros((2, 10)))
assert_equal(np.shape(errors), (2,))
assert_equal(errors, [0., 0.])


def test_get_features_gamma():
inp_gf = np.round(np.sort(np.random.rand(6, 5) * 10), 2)
inp_gf = np.round(np.sort(np.random.rand(3, 2, 5) * 10), 2)
out_gf = np.round(np.sort(np.random.rand(2, 5) * 10), 2)

gf = GammaFactor(delta=10*ms, time=10*ms)
features = gf.get_features(inp_gf, out_gf, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 3))
features = gf.get_features(inp_gf, out_gf, 0.1*ms)
assert_equal(np.shape(features), (3, 2))
assert(np.all(np.array(features) > 0))

features = gf.get_features(out_gf, out_gf, 2, 0.1*ms)
assert_equal(np.shape(features), (2, 1))
assert_almost_equal(features, [[2.], [2.]])
features = gf.get_features([out_gf]*3, out_gf, 0.1*ms)
assert_equal(np.shape(features), (3, 2))
assert_almost_equal(features, np.ones((3, 2))*2)


def test_get_errors_gamma():
gf = GammaFactor(delta=10*ms, time=10*ms)
errors = gf.get_errors(np.random.rand(10, 5))
errors = gf.get_errors(np.random.rand(5, 10))
assert_equal(np.shape(errors), (5,))
assert(np.all(np.array(errors) > 0))

errors = gf.get_errors(np.zeros((10, 2)))
errors = gf.get_errors(np.zeros((2, 10)))
assert_equal(np.shape(errors), (2,))
assert_almost_equal(errors, [0., 0.])
8 changes: 4 additions & 4 deletions brian2modelfitting/tests/test_modelfitting_spikefitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ def fin():
group.v = -70 * mV
spike_mon = SpikeMonitor(group)
run(60*ms)
spikes = getattr(spike_mon, 't') / ms
spikes = getattr(spike_mon, 't_')

return spike_mon, spikes


def test_get_spikes(setup_spikes):
spike_mon, spikes = setup_spikes
gs = get_spikes(spike_mon)
gs = get_spikes(spike_mon, 1, 1)
assert isinstance(gs, list)
assert isinstance(gs[0], np.ndarray)
assert_equal(gs, [np.array(spikes)])
assert isinstance(gs[0][0], np.ndarray)
assert_equal(gs, [[np.array(spikes)]])


def test_spikefitter_init(setup):
Expand Down
2 changes: 1 addition & 1 deletion brian2modelfitting/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_callback_none():
def test_ProgressBar():
pb = ProgressBar(toolbar_width=10)
assert_equal(pb.toolbar_width, 10)
assert isinstance(pb.t, tqdm._tqdm.tqdm)
assert isinstance(pb.t, tqdm.tqdm)
pb([1, 2, 3], [1.2, 2.3, 0.1], {'a':3}, 0.1, 2)


Expand Down
5 changes: 4 additions & 1 deletion docs_sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
sys.path.insert(0, os.path.abspath('..'))

needs_sphinx = '1.7'
needs_sphinx = '2.0'


brian2modelfitting_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
Expand Down Expand Up @@ -57,6 +57,9 @@
# The master toctree document.
master_doc = 'index'

# autodoc configuration
autodoc_default_options = {'inherited-members': True}

# -- Options for HTML output -------------------------------------------------
# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
Expand Down
Loading