Exemplo n.º 1
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))
Exemplo n.º 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))
Exemplo n.º 3
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])
Exemplo n.º 4
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)