Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ace8e7587d
Fetching contributors…

Cannot retrieve contributors at this time

file 69 lines (56 sloc) 2.537 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# Copyright (c) 2010 Matej Laitl <matej@laitl.cz>
# Distributed under the terms of the GNU General Public License v2 or any
# later version of the license, at your option.

"""Various support methods for tests"""

import functools
import numpy as np
import sys
import unittest as ut
try:
    from unittest.case import _ExpectedFailure as ExpectedFailure
except ImportError:
    ExpectedFailure = None


def stochastic(func):
    """Decorator to mark test as stochastic - these tests are not fatal when they fail."""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception:
            if ExpectedFailure is not None: # added in Py 2.7
                raise ExpectedFailure(sys.exc_info())
    wrapper.__doc__ += ' (stochastic, failures ignored)'
    return wrapper


class PbTestCase(ut.TestCase):
    """Test case that adds some numeric assert functions"""

    def assertApproxEqual(self, X, Y):
        """Return true if X = Y to within machine precision

Function for checking that different matrices from different
computations are in some sense "equal" in the verification tests.
"""
        X = np.asarray(X)
        Y = np.asarray(Y)
        fuzz = 1.0e-8

        self.assertEqual(X.ndim, Y.ndim)
        self.assertEqual(X.shape, Y.shape)

        if np.all(abs(X - Y) < fuzz):
            return
        else:
            self.fail("NumPy arrays {0} and {1} are not fuzzy equal (+- {2})".format(X, Y, fuzz))

    def assertArraysEqualNotSame(self, a, b):
        """Assert that numpy arrays a and b are equal, but are not the same instances"""
        self.assertTrue(id(a) != id(b))
        self.assertTrue(a.ndim == b.ndim)
        self.assertTrue(np.all(a == b))

    def assertRVsEqualNotSame(self, a, b):
        """Assert that :class:`~pybayes.pdfs.RV` objects a and b are equal, but
are not the same instances neither shallow copies of themselves.

RVs are special case during deepcopy - the RVComps should be referenced,
not copied."""
        self.assertTrue(id(a) != id(b))
        self.assertTrue(id(a.components) != id(b.components))
        self.assertEqual(a.name, b.name) # no need to test for id inequality - strings are immutable
        self.assertEqual(a.dimension, b.dimension) # ditto
        for (a_comp, b_comp) in zip(a.components, b.components):
            # equality for rv comps is defined as object instance identity
            self.assertEqual(a_comp, b_comp)
Something went wrong with that request. Please try again.