def test_metric_collection(tmpdir):
    m1 = DummyMetricSum()
    m2 = DummyMetricDiff()

    metric_collection = MetricCollection([m1, m2])

    # Test correct dict structure
    assert len(metric_collection) == 2
    assert metric_collection['DummyMetricSum'] == m1
    assert metric_collection['DummyMetricDiff'] == m2

    # Test correct initialization
    for name, metric in metric_collection.items():
        assert metric.x == 0, f'Metric {name} not initialized correctly'

    # Test every metric gets updated
    metric_collection.update(5)
    for name, metric in metric_collection.items():
        assert metric.x.abs() == 5, f'Metric {name} not updated correctly'

    # Test compute on each metric
    metric_collection.update(-5)
    metric_vals = metric_collection.compute()
    assert len(metric_vals) == 2
    for name, metric_val in metric_vals.items():
        assert metric_val == 0, f'Metric {name}.compute not called correctly'

    # Test that everything is reset
    for name, metric in metric_collection.items():
        assert metric.x == 0, f'Metric {name} not reset correctly'

    # Test pickable
    metric_pickled = pickle.dumps(metric_collection)
    metric_loaded = pickle.loads(metric_pickled)
    assert isinstance(metric_loaded, MetricCollection)
Exemple #2
0
def test_metric_collection_prefix_postfix_args(prefix, postfix):
    """ Test that the prefix arg alters the keywords in the output"""
    m1 = DummyMetricSum()
    m2 = DummyMetricDiff()
    names = ['DummyMetricSum', 'DummyMetricDiff']
    names = [prefix + n if prefix is not None else n for n in names]
    names = [n + postfix if postfix is not None else n for n in names]

    metric_collection = MetricCollection([m1, m2],
                                         prefix=prefix,
                                         postfix=postfix)

    # test forward
    out = metric_collection(5)
    for name in names:
        assert name in out, 'prefix or postfix argument not working as intended with forward method'

    # test compute
    out = metric_collection.compute()
    for name in names:
        assert name in out, 'prefix or postfix argument not working as intended with compute method'

    # test clone
    new_metric_collection = metric_collection.clone(prefix='new_prefix_')
    out = new_metric_collection(5)
    names = [n[len(prefix):] if prefix is not None else n
             for n in names]  # strip away old prefix
    for name in names:
        assert f"new_prefix_{name}" in out, 'prefix argument not working as intended with clone method'

    for k, _ in new_metric_collection.items():
        assert 'new_prefix_' in k

    for k in new_metric_collection.keys():
        assert 'new_prefix_' in k

    for k, _ in new_metric_collection.items(keep_base=True):
        assert 'new_prefix_' not in k

    for k in new_metric_collection.keys(keep_base=True):
        assert 'new_prefix_' not in k

    assert type(new_metric_collection.keys(keep_base=True)) == type(
        new_metric_collection.keys(keep_base=False))  # noqa E721
    assert type(new_metric_collection.items(keep_base=True)) == type(
        new_metric_collection.items(keep_base=False))  # noqa E721

    new_metric_collection = new_metric_collection.clone(postfix='_new_postfix')
    out = new_metric_collection(5)
    names = [n[:-len(postfix)] if postfix is not None else n
             for n in names]  # strip away old postfix
    for name in names:
        assert f"new_prefix_{name}_new_postfix" in out, 'postfix argument not working as intended with clone method'
def test_collection_add_metrics():
    m1 = DummyMetricSum()
    m2 = DummyMetricDiff()

    collection = MetricCollection([m1])
    collection.add_metrics({'m1_': DummyMetricSum()})
    collection.add_metrics(m2)

    collection.update(5)
    results = collection.compute()
    assert results['DummyMetricSum'] == results['m1_'] and results['m1_'] == 5
    assert results['DummyMetricDiff'] == -5
Exemple #4
0
def test_metric_collection_prefix_arg(tmpdir):
    """ Test that the prefix arg alters the keywords in the output"""
    m1 = DummyMetricSum()
    m2 = DummyMetricDiff()
    names = ['DummyMetricSum', 'DummyMetricDiff']

    metric_collection = MetricCollection([m1, m2], prefix='prefix_')

    # test forward
    out = metric_collection(5)
    for name in names:
        assert f"prefix_{name}" in out, 'prefix argument not working as intended with forward method'

    # test compute
    out = metric_collection.compute()
    for name in names:
        assert f"prefix_{name}" in out, 'prefix argument not working as intended with compute method'

    # test clone
    new_metric_collection = metric_collection.clone(prefix='new_prefix_')
    out = new_metric_collection(5)
    for name in names:
        assert f"new_prefix_{name}" in out, 'prefix argument not working as intended with clone method'