Ejemplo n.º 1
0
    assert_array_equal(metric.batch_data, np.array([1.0, 3.0]))

    assert_array_equal(metric.epoch_domain, np.array([99]))
    assert_array_equal(metric.epoch_data, np.array([1.0 / 2.0 + 3.0 / 2.0]))

    dict_ = metric.to_dict()
    for name in ("batch_data", "epoch_data", "epoch_domain"):
        assert_array_equal(
            dict_[name],
            getattr(metric, name),
            err_msg=name + " does not map to the correct value in the metric-dict",
        )


@pytest.mark.parametrize(
    "bad_input", [cst.everything_except(dict), st.just(dict(a_bad_key=1))]
)
@settings(suppress_health_check=[HealthCheck.too_slow])
@given(data=st.data())
def test_from_dict_input_validation(bad_input: st.SearchStrategy, data: st.DataObject):
    bad_input = data.draw(bad_input, label="bad_input")
    with pytest.raises((ValueError, TypeError)):
        LiveMetric.from_dict(bad_input)


static_logger_dict = cst.live_metrics(min_num_metrics=1).example()  # type: dict


@pytest.mark.parametrize(
    "bad_input",
    [
Ejemplo n.º 2
0
import hypothesis.extra.numpy as hnp
import hypothesis.strategies as st
import numpy as np
import pytest
from hypothesis import HealthCheck, given, settings
from numpy.testing import assert_allclose
from pytest import raises

from mygrad._utils import is_invalid_gradient, reduce_broadcast
from tests.custom_strategies import broadcastable_shapes, everything_except


@pytest.mark.parametrize(
    ("grad", "is_invalid"),
    [
        (everything_except((np.ndarray, Real)), True),
        (None, True),
        (np.ndarray([1], dtype="O"), True),
        (
            hnp.arrays(
                shape=hnp.array_shapes(),
                dtype=hnp.floating_dtypes(),
                elements=st.floats(width=16),
            ),
            False,
        ),
        ((st.integers(min_value=int(-1e6), max_value=int(1e6)) | st.floats()),
         False),
    ],
)
@settings(deadline=None, suppress_health_check=(HealthCheck.too_slow, ))
Ejemplo n.º 3
0
from numpy.testing import assert_array_equal
from tests.base_state_machines import LivePlotStateMachine
from tests.utils import compare_all_metrics

from noggin import load_metrics, save_metrics
from noggin.plotter import LivePlot


@settings(deadline=None)
@pytest.mark.parametrize(
    "bad_input",
    [
        dict(metrics=[]),
        dict(metrics=st.lists(st.text(), min_size=2).filter(
            lambda x: len(set(x)) != len(x))),
        dict(metrics=cst.everything_except((str, Sequence))
             | st.lists(cst.everything_except(str))),
        dict(nrows=cst.everything_except((Integral, type(None)))
             | st.integers(max_value=0)),
        dict(ncols=cst.everything_except((Integral, type(None)))
             | st.integers(max_value=0)),
        dict(max_fraction_spent_plotting=cst.everything_except((float, int))
             | st.floats().filter(lambda x: not 0 <= x <= 1)),
        dict(last_n_batches=cst.everything_except((int, type(None)))
             | st.integers(max_value=0)),
    ],
)
@given(data=st.data())
def test_input_validation(bad_input: dict, data: st.DataObject):
    defaults = dict(metrics=["a"])
    defaults.update({
Ejemplo n.º 4
0
    assert y_copy.constant is constant
    if y.grad is None:
        assert y_copy.grad is None
    else:
        assert_array_equal(y.grad, y_copy.grad)
    assert_array_equal(y.data, y_copy.data)


@settings(max_examples=10)
@given(
    tensor=st.builds(
        mg.Tensor,
        x=hnp.arrays(shape=hnp.array_shapes(), dtype=np.float32),
        constant=st.booleans(),
    ),
    constant=everything_except(bool),
)
def test_set_constant_validation(tensor, constant):
    with pytest.raises(TypeError):
        tensor.constant = constant


def test_to_scalar():
    nd_tensor = Tensor([1, 2])
    with raises(TypeError):
        float(nd_tensor)

    with raises(TypeError):
        int(nd_tensor)

    with raises(ValueError):
Ejemplo n.º 5
0
    logger = LiveLogger()
    logger.set_test_batch({metric_name: 1}, batch_size=1)
    save_metrics("test.pkl", test_metrics=logger.test_metrics)
    train, test = load_metrics("test.pkl")

    assert list(test) == list(logger.test_metrics)
    for k, actual in test[metric_name].items():
        expected = logger.test_metrics[metric_name][k]
        if isinstance(actual, np.ndarray):
            assert_array_equal(actual, expected)
        else:
            assert expected == actual


@settings(deadline=None)
@given(bad_logger=cst.everything_except(LiveLogger))
def test_plot_logger_validation(bad_logger):
    with pytest.raises(TypeError):
        plot_logger(bad_logger)


@settings(deadline=None)
@given(
    logger=cst.loggers(min_num_metrics=1),
    plot_batches=st.booleans(),
    kwargs=cst.plot_kwargs(),
    data=st.data(),
)
def test_plot_logger(logger: LiveLogger, plot_batches: bool, kwargs: dict,
                     data: st.DataObject):
    try:
Ejemplo n.º 6
0
                   abs_tol=0.01)


@settings(deadline=None, max_examples=20)
@given(plotter=cst.plotters(),
       liveplot=st.booleans(),
       plot_batches=st.booleans())
def test_fuzz_plot_method(plotter: LivePlot, liveplot: bool,
                          plot_batches: bool):
    with close_plots():
        plotter._liveplot = liveplot
        plotter.plot(plot_batches=plot_batches)


@settings(deadline=None)
@given(plotter=cst.plotters(), bad_input=cst.everything_except(bool))
def test_validate_plot_input(plotter: LivePlot, bad_input):
    with pytest.raises(TypeError):
        plotter.plot(plot_batches=bad_input)


@settings(deadline=None, max_examples=10)
@given(plotter=cst.plotters(), plot_batches=st.booleans())
def test_plot_batches_flag_via_plot(plotter: LivePlot, plot_batches: bool):
    with close_plots():
        plotter.last_n_batches = None
        plotter.plot(plot_batches=plot_batches)
        for name, metric in plotter._train_metrics.items():
            if metric.batch_domain.size:
                assert bool(metric.batch_line.get_xdata().size) is plot_batches
            if metric.epoch_domain.size: