Example #1
0
def test_get_errors_mse():
    mse = MSEMetric()
    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((2, 10)))
    assert_equal(np.shape(errors), (2, ))
    assert_equal(errors, [0., 0.])
Example #2
0
def test_calc_mse_t_start():
    mse = MSEMetric(t_start=1 * ms)
    out = np.random.rand(2, 20)
    inp = np.random.rand(5, 2, 20)

    errors = mse.calc(inp, out, 0.1 * ms)
    assert_equal(np.shape(errors), (5, ))
    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))
Example #3
0
def test_get_features_mse():
    mse = MSEMetric()
    out_mse = np.random.rand(2, 20)
    inp_mse = np.random.rand(5, 2, 20)

    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(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_spikefitter_fit_errors(setup):
    dt, sf = setup
    with pytest.raises(TypeError):
        results, errors = sf.fit(n_rounds=2,
                                 optimizer=n_opt,
                                 metric=MSEMetric(),
                                 gL=[20*nS, 40*nS],
                                 C=[0.5*nF, 1.5*nF])
    with pytest.raises(TypeError):
        results, errors = sf.fit(n_rounds=2,
                                 optimizer=None,
                                 metric=MSEMetric(),
                                 gL=[20*nS, 40*nS],
                                 C=[0.5*nF, 1.5*nF])
Example #5
0
def test_calc_mse_t_weights():
    with pytest.raises(ValueError):
        # t_start and t_weights
        MSEMetric(t_start=1 * ms, t_weights=np.ones(20))
    with pytest.raises(ValueError):
        # all values zero
        MSEMetric(t_weights=np.zeros(20))
    with pytest.raises(ValueError):
        # negative values
        weights = np.ones(20)
        weights[17] = -1
        MSEMetric(t_weights=weights)

    weights = np.ones(20)
    weights[:10] = 0
    mse = MSEMetric(t_weights=weights)
    out = np.random.rand(2, 20)
    inp = np.random.rand(5, 2, 20)

    errors = mse.calc(inp, out, 0.1 * ms)
    assert_equal(np.shape(errors), (5, ))
    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_fitter_fit_tstart(setup_constant):
    dt, tf = setup_constant

    # Ignore the first 50 steps at 10mV
    params, result = tf.fit(n_rounds=10, optimizer=n_opt,
                            metric=MSEMetric(t_start=50*dt),
                            c=[0 * mV, 30 * mV])
    # Fit should be close to 20mV
    assert np.abs(params['c'] - 20*mV) < 1*mV
def test_fitter_fit_tsteps(setup_constant):
    dt, tf = setup_constant

    with pytest.raises(ValueError):
        # Incorrect weight size
        tf.fit(n_rounds=10,
               optimizer=n_opt,
               metric=MSEMetric(t_weights=np.ones(101)),
               c=[0 * mV, 30 * mV])

    # Ignore the first 50 steps at 10mV
    weights = np.ones(100)
    weights[:50] = 0
    params, result = tf.fit(n_rounds=10,
                            optimizer=n_opt,
                            metric=MSEMetric(t_weights=weights),
                            c=[0 * mV, 30 * mV])
    # Fit should be close to 20mV
    assert np.abs(params['c'] - 20 * mV) < 1 * mV
def test_fitter_refine_reuse_tstart(setup_constant):
    dt, tf = setup_constant

    # Ignore the first 50 steps at 10mV but do not actually fit (0 rounds)
    params, result = tf.fit(n_rounds=0, optimizer=n_opt,
                            metric=MSEMetric(t_start=50*dt),
                            c=[0 * mV, 30 * mV])
    # t_start should be reused
    params, result = tf.refine({'c': 5 * mV}, c=[0 * mV, 30 * mV])

    # Fit should be close to 20mV
    assert np.abs(params['c'] - 20 * mV) < 1 * mV
def test_fitter_refine_tsteps_normalization(setup_constant):
    dt, tf = setup_constant

    model_traces = tf.generate(params={'c': 5 * mV})
    mse_error = MSEMetric(t_start=50 * dt).calc(model_traces[None, :, :],
                                                tf.output, dt)
    all_errors = []

    def callback(parameters, errors, best_parameters, best_error, index):
        all_errors.append(float(errors[0]))
        return True  # stop simulation

    # Ignore the first 50 steps at 10mV
    tf.refine({'c': 5 * mV},
              c=[0 * mV, 30 * mV],
              t_start=50 * dt,
              callback=callback)

    weights = np.ones(100)
    weights[:50] = 0
    tf.refine({'c': 5 * mV},
              c=[0 * mV, 30 * mV],
              t_weights=weights,
              callback=callback)

    tf.refine({'c': 5 * mV},
              c=[0 * mV, 30 * mV],
              t_weights=weights * 2,
              callback=callback)

    tf.fit(n_rounds=0,
           optimizer=n_opt,
           metric=MSEMetric(t_weights=weights * 3),
           c=[0 * mV, 30 * mV])
    tf.refine({'c': 5 * mV}, c=[0 * mV, 30 * mV], callback=callback)

    assert_almost_equal(float(mse_error[0]), all_errors[0])
    assert_almost_equal(all_errors[0], all_errors[1])
    assert_almost_equal(all_errors[1], all_errors[2])
    assert_almost_equal(all_errors[2], all_errors[3])
Example #10
0
def test_calc_mse():
    mse = MSEMetric()
    out = np.random.rand(2, 20)
    inp = np.random.rand(5, 2, 20)

    errors = mse.calc(inp, out, 0.01 * ms)
    assert_equal(np.shape(errors), (5, ))
    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))

    inp = np.vstack([np.ones((1, 3, 10)), np.zeros((1, 3, 10))])
    out = np.ones((3, 10))
    errors = mse.calc(inp, out, 0.01 * ms)
    assert_equal(errors, [0, 1])
    mse = MSEMetric(normalization=1 / 2)
    errors = mse.calc(inp, out, 0.01 * ms)
    # The normalization factor scales the traces, so the squared error scales
    # with the square of the normalization factor
    assert_equal(errors, [0, 4])
Example #11
0
def test_calc_mse_t_weights_normalization():
    # check that normalization works correctly
    dt = 0.1 * ms
    metric1 = MSEMetric(t_start=50 * dt)
    weights = np.ones(100)
    weights[:50] = 0
    metric2 = MSEMetric(t_weights=weights)
    weights2 = weights * 2  # should not make any difference
    metric3 = MSEMetric(t_weights=weights2)
    data_traces = np.random.rand(3, 100)
    model_traces = np.random.rand(2, 3, 100)
    error_1 = metric1.calc(model_traces=model_traces,
                           data_traces=data_traces,
                           dt=dt)
    error_2 = metric2.calc(model_traces=model_traces,
                           data_traces=data_traces,
                           dt=dt)
    error_3 = metric3.calc(model_traces=model_traces,
                           data_traces=data_traces,
                           dt=dt)
    assert_almost_equal(error_1, error_2)
    assert_almost_equal(error_1, error_3)
Example #12
0
def test_init():
    MSEMetric()
    GammaFactor(10 * ms, time=10 * ms)
    I = g*(v-E) : amp
    g : siemens (constant)
    '''

constant_model = Equations('''
    v = c + x: volt
    c : volt (constant)''')

all_constant_model = Equations('''
    v = 10*mV + x: volt
    c : volt (constant)
    penalty_fixed = 10*mV**2: volt**2
    penalty_wrong_unit = 10*mV : volt''')

n_opt = NevergradOptimizer()
metric = MSEMetric()


@pytest.fixture
def setup(request):
    dt = 0.01 * ms
    tf = TraceFitter(dt=dt,
                     model=model,
                     input_var='v',
                     output_var='I',
                     input=input_traces,
                     output=output_traces,
                     n_samples=30)

    def fin():
        reinit_devices()