Пример #1
0
def test_bayesian_TN_trace(pm):
    fd = woehler.determine_fractures(data, 1e7).fatigue_data
    bayes = woehler.Bayesian(fd)
    bayes._common_analysis()
    bayes._nsamples = 1000
    bayes._TN_trace()

    pm.HalfNormal.assert_called_with('stdev', sigma=1.3)

    assert pm.Normal.call_count == 2

    expected_mu = 5.294264482012933
    expected_sigma = 0.2621494419382026

    assert pm.Normal.call_args_list[0][0] == ('mu', )
    np.testing.assert_almost_equal(pm.Normal.call_args_list[0][1]['mu'],
                                   expected_mu,
                                   decimal=9)
    np.testing.assert_almost_equal(pm.Normal.call_args_list[0][1]['sigma'],
                                   expected_sigma,
                                   decimal=9)

    assert pm.Normal.call_args_list[1][0] == ('y', )
    observed = pm.Normal.call_args_list[1][1][
        'observed']  # Consider switch to kwargs property when py3.7 is dropped
    np.testing.assert_almost_equal(observed.mean(), expected_mu, decimal=9)
    np.testing.assert_almost_equal(observed.std(), expected_sigma, decimal=9)

    pm.sample.assert_called_with(1000,
                                 target_accept=0.99,
                                 random_seed=None,
                                 chains=3,
                                 tune=1000)
Пример #2
0
def test_bayesian_mock(_slope_trace, _TN_trace, _SD_TS_trace):
    expected = pd.Series({
        'SD_50': 100.,
        '1/TS': 1.12,
        'k_1': 7.0,
        'ND_50': 1e6,
        '1/TN': 5.3
    }).sort_index()

    expected_slope_trace = {
        'x': np.array([0.0, -8.0, -6.0]),
        'Intercept': np.array([0.0, 19., 21.])
    }

    expected_SD_TS_trace = {
        'SD_50': np.array([0.0, 150., 50]),
        'TS_50': np.array([0.0, 1.22, 1.02])
    }

    _slope_trace.__call__(
    ).get_values.side_effect = lambda key: expected_slope_trace[key]
    _TN_trace.__call__().get_values.return_value = np.array([0.0, 5.4, 5.2])
    _SD_TS_trace.__call__(
    ).get_values.side_effect = lambda key: expected_SD_TS_trace[key]

    fd = woehler.determine_fractures(data, 1e7).fatigue_data
    wc = woehler.Bayesian(fd).analyze(nsamples=10).sort_index()

    pd.testing.assert_series_equal(wc, expected)
    np.testing.assert_allclose(wc.to_numpy(), expected.to_numpy())
Пример #3
0
def test_bayesian_full():
    expected = pd.Series({
        'SD_50': 340.,
        '1/TS': 1.12,
        'k_1': 7.0,
        'ND_50': 400000.,
        '1/TN': 5.3
    }).sort_index()

    fd = woehler.determine_fractures(data, 1e7).fatigue_data
    wc = woehler.Bayesian(fd).analyze(random_seed=4223,
                                      progressbar=False).sort_index()
    pd.testing.assert_index_equal(wc.index, expected.index)
    np.testing.assert_allclose(wc.to_numpy(), expected.to_numpy(), rtol=1e-1)
Пример #4
0
def test_bayesian_slope_trace(pm):
    fd = woehler.determine_fractures(data, 1e7).fatigue_data
    bayes = woehler.Bayesian(fd)
    bayes._nsamples = 1000
    bayes._slope_trace()

    formula, data_dict = pm.glm.GLM.from_formula.call_args[0]
    assert formula == 'y ~ x'
    pd.testing.assert_series_equal(data_dict['x'], np.log10(fd.fractures.load))
    np.testing.assert_array_equal(data_dict['y'],
                                  np.log10(fd.fractures.cycles.to_numpy()))
    family = pm.glm.GLM.from_formula.call_args[1][
        'family']  # Consider switch to kwargs property when py3.7 is dropped
    assert family is pm.glm.families.StudentT()

    pm.sample.assert_called_with(1000,
                                 target_accept=0.99,
                                 random_seed=None,
                                 chains=2,
                                 tune=1000)
Пример #5
0
def test_bayesian_SD_TS_trace_mock(pm, tt):
    def check_likelihood(l, var):
        assert var == tt.as_tensor_variable.return_value
        assert isinstance(l.likelihood,
                          woehler.analyzers.likelihood.Likelihood)
        np.testing.assert_array_equal(l.likelihood._fd, fd)
        return 'foovar'

    fd = woehler.determine_fractures(data, 1e7).fatigue_data
    inf_load_mean = fd.infinite_zone.load.mean()
    inf_load_std = fd.infinite_zone.load.std()

    with mock.patch.object(woehler.Bayesian._LogLike,
                           '__call__',
                           autospec=True) as loglike_call:
        loglike_call.side_effect = check_likelihood

        bayes = woehler.Bayesian(fd)
        bayes._nsamples = 1000
        bayes._SD_TS_trace()

    pm.Normal.assert_called_once_with('SD_50',
                                      mu=inf_load_mean,
                                      sigma=inf_load_std * 5)
    pm.Lognormal.assert_called_once_with('TS_50',
                                         mu=np.log10(1.1),
                                         sigma=np.log10(0.5))

    tt.as_tensor_variable.assert_called_once_with(
        [pm.Normal.return_value, pm.Lognormal.return_value])

    pm.Potential.assert_called_once_with('likelihood', 'foovar')

    pm.sample.assert_called_with(1000,
                                 cores=1,
                                 chains=3,
                                 random_seed=None,
                                 discard_tuned_samples=True,
                                 tune=1000)