Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Test failing when building KDE with kombine (sometimes) #28

Open
duncanmmacleod opened this issue Apr 26, 2018 · 0 comments
Open

Test failing when building KDE with kombine (sometimes) #28

duncanmmacleod opened this issue Apr 26, 2018 · 0 comments
Assignees

Comments

@duncanmmacleod
Copy link
Member

Some of the time one of the unit tests fails, related to constructing a KDE with kombine:

$ python setup.py test
...
test/test_results.py::test_construct_kde[True] FAILED                                      [ 77%]
...

The test function is:

gwin/test/test_results.py

Lines 95 to 110 in 0d50dca

@pytest.mark.parametrize('kombine', [
None, # test ImportError
False,
pytest.param(True, marks=skip_no_kombine),
])
def test_construct_kde(kombine):
samples = numpy.random.rand(5, 5)
if kombine is None:
with mock.patch.dict('sys.modules', {'kombine': None}):
with pytest.raises(ImportError):
scatter_histograms.construct_kde(samples, use_kombine=True)
return
kde = scatter_histograms.construct_kde(samples, use_kombine=kombine)
assert isinstance(kde, kde_types)

The target code (being tested) is:

def construct_kde(samples_array, use_kombine=False):
"""Constructs a KDE from the given samples.
"""
if use_kombine:
try:
import kombine
except ImportError:
raise ImportError("kombine is not installed.")
# construct the kde
if use_kombine:
kde = kombine.clustered_kde.KDE(samples_array)
else:
kde = scipy.stats.gaussian_kde(samples_array.T)
return kde

explicitly with use_kombine=True. The pytest output for that failure is as follows:

____________________________________ test_construct_kde[True] ____________________________________

kombine = True

    @pytest.mark.parametrize('kombine', [
        None,  # test ImportError
        False,
        pytest.param(True, marks=skip_no_kombine),
    ])
    def test_construct_kde(kombine):
        samples = numpy.random.rand(5, 5)

        if kombine is None:
            with mock.patch.dict('sys.modules', {'kombine': None}):
                with pytest.raises(ImportError):
                    scatter_histograms.construct_kde(samples, use_kombine=True)
            return

>       kde = scatter_histograms.construct_kde(samples, use_kombine=kombine)

test/test_results.py:109:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gwin/results/scatter_histograms.py:156: in construct_kde
    kde = kombine.clustered_kde.KDE(samples_array)
../../opt/gwin-2.7/lib/python2.7/site-packages/kombine/clustered_kde.py:245: in __init__
    self._set_bandwidth()
../../opt/gwin-2.7/lib/python2.7/site-packages/kombine/clustered_kde.py:272: in _set_bandwidth
    self._cho_factor = la.cho_factor(self._kernel_cov)
../../opt/gwin-2.7/lib/python2.7/site-packages/scipy/linalg/decomp_cholesky.py:142: in cho_factor
    check_finite=check_finite)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

a = array([[ 0.06329217, -0.00541257,  0.02746434, -0.0176992 , -0.00789024],
    ...47],
       [-0.00789024, -0.05523657, -0.02178179,  0.00170347,  0.11439074]])
lower = False, overwrite_a = False, clean = False, check_finite = True

    def _cholesky(a, lower=False, overwrite_a=False, clean=True,
                  check_finite=True):
        """Common code for cholesky() and cho_factor()."""

        a1 = asarray_chkfinite(a) if check_finite else asarray(a)
        a1 = atleast_2d(a1)

        # Dimension check
        if a1.ndim != 2:
            raise ValueError('Input array needs to be 2 dimensional but received '
                             'a {}d-array.'.format(a1.ndim))
        # Squareness check
        if a1.shape[0] != a1.shape[1]:
            raise ValueError('Input array is expected to be square but has '
                             'the shape: {}.'.format(a1.shape))

        # Quick return for square empty array
        if a1.size == 0:
            return a1.copy(), lower

        overwrite_a = overwrite_a or _datacopied(a1, a)
        potrf, = get_lapack_funcs(('potrf',), (a1,))
        c, info = potrf(a1, lower=lower, overwrite_a=overwrite_a, clean=clean)
        if info > 0:
            raise LinAlgError("%d-th leading minor of the array is not positive "
>                             "definite" % info)
E           LinAlgError: 5-th leading minor of the array is not positive definite

../../opt/gwin-2.7/lib/python2.7/site-packages/scipy/linalg/decomp_cholesky.py:40: LinAlgError

This is not reproducable 100% of the time, which suggests some issue with random numbers, but it could also be that using a simple 5x5 array of samples created by numpy isn't good enough to actually perform this test.

This test was only designed to make sure that gwin.results.scatter_histogram.construct_kde is doing something sensible, and not to test kombine.clustered_kde.KDE, so as long as we can create a KDE, I'm happy.

All that said, I'm not sure the best way to fix the issue, but it probably requires some combination of @cdcapano and @bfarr working out what is a good enough array of samples to successfully construct a KDE in order to test this code properly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants