test
– Domain-specific language for writing tests
Domain-specific language for writing numeric tests.
This module provides a few classes and functions to write numeric tests.
Let us import the relevant modules first:
>>> from collections import OrderedDict
>>> from valjean.eponine.dataset import Dataset
>>> import numpy as np
Now we create a toy data set:
>>> x = np.linspace(-5., 5., num=100)
>>> y = x**2
>>> error = np.zeros_like(y)
>>> bins = OrderedDict()
>>> bins['x'] = x
>>> parabola = Dataset(y, error, bins=bins)
We perturb the data by applying some small amount of noise:
>>> eps = 1e-8
>>> noise = np.random.uniform(-eps, eps, parabola.shape)
>>> y2 = y + noise
>>> parabola2 = Dataset(y2, error, bins=bins)
Now we can test if the new dataset is equal to the original one:
>>> from valjean.gavroche.test import TestEqual
>>> test_equality = TestEqual(parabola, parabola2, name="parabola",
... description="equality test")
>>> test_equality_res = test_equality.evaluate()
>>> print(bool(test_equality_res))
False
However, they are approximately equal:
>>> from valjean.gavroche.test import TestApproxEqual
>>> test_approx = TestApproxEqual(parabola, parabola2, name="parabola",
... description="approx equal test")
>>> test_approx_res = test_approx.evaluate()
>>> print(bool(test_approx_res))
True
- exception valjean.gavroche.test.CheckBinsException[source]
An error is raised when check bins fails.
- valjean.gavroche.test.same_arrays(arr1, arr2)[source]
Return True if arr1 and arr2 are equal.
- Parameters:
arr1 – the first array.
arr2 – the second array.
- valjean.gavroche.test.same_bins(bins1, bins2)[source]
Return True if all the coordinate arrays are compatible.
- Parameters:
bins1 – the first dictionary of coordinate arrays.
bins2 – the second dictionary of coordinate arrays.
- valjean.gavroche.test.same_bins_datasets(*datasets)[source]
Return True if all datasets have the same coordinates.
- Parameters:
datasets (
Dataset
) – any number of datasets.
- valjean.gavroche.test.check_bins(*datasets)[source]
Check if the datasets have compatible coordinates, raise if not.
- Raises:
ValueError – if the datasets do not have compatible coordinates.
- class valjean.gavroche.test.Test(*, name, description='', labels=None)[source]
Generic class for comparing any kind of results.
Base class for tests.
- __init__(*, name, description='', labels=None)[source]
Initialize the
Test
object with a name, a description of the test (may be long) and labels if needed.The test is actually performed in the
evaluate
method, which is abstract in the base class and must be implemented by sub-classes.- Parameters:
name (str) – name of the test, this string will typically end up in the test report as a section name.
description (str) – description of the test exepcted with context, this string will typically end up in the test report.
labels (dict) – labels to be used for test classification in reports (for example category, input file name, type of result, …)
- abstract evaluate()[source]
Evaluate the test on the given inputs.
Must return a subclass of
TestResult
.
- class valjean.gavroche.test.TestDataset(dsref, *datasets, name, description='', labels=None)[source]
Generic class for comparing datasets.
- __init__(dsref, *datasets, name, description='', labels=None)[source]
Initialisation of
TestEqual
:- Parameters:
name (str) – name of the test (in analysis)
description (str) – specific description of the test
labels (dict) – labels to be used for test classification in reports (for example category, input file name, type of result, …)
dsref (Dataset) – reference dataset
datasets (list(Dataset)) – list of datasets to be compared to reference dataset
- data()[source]
Generator yielding objects supporting the buffer protocol that (as a whole) represent a serialized version of self.
- abstract evaluate()[source]
Evaluate the test on the given datasets.
Must return a subclass of
TestResult
.
- class valjean.gavroche.test.TestResult(test)[source]
Base class for test results.
This result should be filled by
Test
daughter classes.- __init__(test)[source]
Initialisation of
TestResult
.- Parameters:
test (
Test
used) – the used test
- class valjean.gavroche.test.TestResultFailed(test, msg)[source]
Class for failed TestResults when an exception was raised during the evaluation.
- __init__(test, msg)[source]
Initialisation of
TestResult
.- Parameters:
test (
Test
used) – the used test
- class valjean.gavroche.test.TestResultEqual(test, equal)[source]
Result from
TestEqual
.- __init__(test, equal)[source]
Initialisation of the result from
TestEqual
:- Parameters:
test (
TestEqual
) – the used testequal (
list
(numpy.bool_
) if datasets arenumpy.generic
,list
(numpy.ndarray
) if datasets arenumpy.ndarray
withdtype == bool
.) – result from the test
- class valjean.gavroche.test.TestEqual(dsref, *datasets, name, description='', labels=None)[source]
Test if the datasets values are equal. Errors are ignored.
- class valjean.gavroche.test.TestResultApproxEqual(test, approx_equal)[source]
Result from
TestApproxEqual
.- __init__(test, approx_equal)[source]
Initialisation of the result from
TestApproxEqual
:- Parameters:
test (
TestApproxEqual
) – the used testapprox_equal (
numpy.generic
if datasets arenumpy.generic
,numpy.ndarray
if datasets arenumpy.ndarray
. In both casesdtype == bool
.) – result from the test
- class valjean.gavroche.test.TestApproxEqual(dsref, *datasets, name, description='', labels=None, rtol=1e-05, atol=1e-08)[source]
Test if the datasets values are equal within the given tolerances. Errors are ignored.
- __init__(dsref, *datasets, name, description='', labels=None, rtol=1e-05, atol=1e-08)[source]
Initialisation of
TestApproxEqual
:- Parameters:
name (str) – local name of the test
description (str) – specific description of the test
labels (dict) – labels to be used for test classification in reports (for example category, input file name, type of result, …)
dsref (Dataset) – reference dataset
datasets (list(Dataset)) – list of datasets to be compared to reference dataset
rtol (float) – relative tolerance, default =
atol (float) – absolute tolerance, default =
To get more details on rtol and atol parameters, see
numpy.isclose
.
- evaluate()[source]
Evaluation of
TestApproxEqual
.- Returns: