def test_insufficient_no_points():
    """Test if estimation aborts for too few data points."""
    expected_mi, source1, source2, target = _get_gauss_data(n=4)

    settings = {
        'kraskov_k': 4,
        'theiler_t': 0,
        'history': 1,
        'history_target': 1,
        'lag_mi': 1,
        'source_target_delay': 1
    }

    # Test first settings combination with k==N
    est = OpenCLKraskovMI(settings)
    with pytest.raises(RuntimeError):
        est.estimate(source1, target)
    est = OpenCLKraskovCMI(settings)
    with pytest.raises(RuntimeError):
        est.estimate(source1, target, target)

    # Test a second combination with a Theiler-correction != 0
    settings['theiler_t'] = 1
    settings['kraskov_k'] = 2
    est = OpenCLKraskovMI(settings)
    with pytest.raises(RuntimeError):
        est.estimate(source1, target)
    est = OpenCLKraskovCMI(settings)
    with pytest.raises(RuntimeError):
        est.estimate(source1, target, target)
def test_cmi_uncorrelated_gaussians():
    """Test CMI estimator on uncorrelated Gaussian data."""
    n_obs = 10000
    var1 = np.random.randn(n_obs, 1)
    var2 = np.random.randn(n_obs, 1)
    var3 = np.random.randn(n_obs, 1)

    # Run OpenCL estimator.
    settings = {'debug': True, 'return_counts': True}
    ocl_est = OpenCLKraskovCMI(settings=settings)
    (mi_ocl, dist, n_range_var1, n_range_var2,
     n_range_var3) = ocl_est.estimate(var1, var2, var3)
    mi_ocl = mi_ocl[0]

    # Run JIDT estimator.
    jidt_est = JidtKraskovCMI(settings={})
    mi_jidt = jidt_est.estimate(var1, var2, var3)

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to 0 nats for uncorrelated '
          'Gaussians.'.format(mi_jidt, mi_ocl))
    assert np.isclose(
        mi_jidt, 0,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'JIDT estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl, 0,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl, mi_jidt,
        atol=0.0001), ('MI estimation for uncorrelated Gaussians using the '
                       'OpenCL estimator failed (error larger 0.05).')
def test_cmi_correlated_gaussians():
    """Test estimators on correlated Gaussian data with conditional."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    # Run OpenCL estimator.
    settings = {'debug': True, 'return_counts': True}
    ocl_est = OpenCLKraskovCMI(settings=settings)
    (mi_ocl, dist, n_range_var1, n_range_var2,
     n_range_cond) = ocl_est.estimate(source, target, source_uncorr)

    mi_ocl = mi_ocl[0]
    # Run JIDT estimator.
    jidt_est = JidtKraskovCMI(settings={})
    mi_jidt = jidt_est.estimate(source, target, source_uncorr)

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to {2:.4f} nats for correlated '
          'Gaussians.'.format(mi_jidt, mi_ocl, expected_mi))
    assert np.isclose(
        mi_jidt, expected_mi,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'JIDT estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl, expected_mi,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl, mi_jidt,
        atol=0.0001), ('MI estimation for uncorrelated Gaussians using the '
                       'OpenCL estimator failed (error larger 0.05).')
def test_cmi_no_cond_correlated_gaussians():
    """Test estimators on correlated Gaussian data without conditional."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    # Run OpenCL estimator.
    settings = {'debug': True}
    ocl_est = OpenCLKraskovCMI(settings=settings)
    mi_ocl, dist, n_range_var1, n_range_var2 = ocl_est.estimate(source, target)

    mi_ocl = mi_ocl[0]
    # Run JIDT estimator.
    jidt_est = JidtKraskovCMI(settings={})
    mi_jidt = jidt_est.estimate(source, target)

    cov_effective = np.cov(np.squeeze(source), np.squeeze(target))[1, 0]
    expected_mi = math.log(1 / (1 - math.pow(cov_effective, 2)))
    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to {2:.4f} nats for correlated '
          'Gaussians.'.format(mi_jidt, mi_ocl, expected_mi))
    assert np.isclose(mi_jidt, expected_mi, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'JIDT estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, expected_mi, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, mi_jidt, atol=0.0001), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
def test_multi_gpu():
    """Test use of multiple GPUs."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()
    settings = {'debug': True, 'return_counts': True}

    # Get no. available devices on current platform.
    device_list, _, _ = OpenCLKraskovCMI()._get_device(gpuid=0)
    print(device_list)
    n_devices = len(device_list)

    # Try initialising estimator with unavailable GPU ID
    with pytest.raises(RuntimeError):
        settings['gpuid'] = n_devices + 1
        OpenCLKraskovCMI(settings=settings)

    # Run OpenCL estimator on available device with highest available ID.
    settings['gpuid'] = n_devices - 1
    ocl_est = OpenCLKraskovCMI(settings=settings)

    (mi_ocl, dist, n_range_var1, n_range_var2,
     n_range_cond) = ocl_est.estimate(source, target, source_uncorr)

    mi_ocl = mi_ocl[0]
    print('Expected MI: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to 0 nats for uncorrelated '
          'Gaussians.'.format(expected_mi, mi_ocl))
    assert np.isclose(
        mi_ocl, expected_mi,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
def test_multiple_runs_two_dim():
    """Test kNN with two chunks of 2D data in the same call."""
    settings = {
        'theiler_t': 0,
        'knn_k': 1,
        'gpu_id': 0,
        'debug': True,
        'max_mem': 5 * 1024 * 1024}
    EST_MI = OpenCLKraskovMI(settings)
    EST_CMI = OpenCLKraskovCMI(settings)

    n_chunks = 50000
    pointset1 = np.array(
        [[-1, 0.5, 1.1, 2, 10, 11, 10.5, -100, -50, 666, 9999, 9999],
         [-1, 0.5, 1.1, 2, 98, -9, -200, 45.3, -53, 0.1, 9999, 9999]]).T.copy()
    pointset1 = np.tile(pointset1, (n_chunks, 1))
    pointset2 = np.ones(pointset1.shape) * 9999
    pointset3 = np.ones(pointset1.shape) * 9999

    # Call MI estimator
    mi, dist1, npoints_x, npoints_y = EST_MI.estimate(
        pointset1, pointset2, n_chunks=n_chunks)
    assert np.isclose(dist1[0], 1.5), 'Distances 0 not correct.'
    assert np.isclose(dist1[1], 0.6), 'Distances 1 not correct.'
    assert np.isclose(dist1[2], 0.6), 'Distances 2 not correct.'
    assert np.isclose(dist1[3], 0.9), 'Distances 3 not correct.'

    # Call CMI estimator with pointset2 as conditional (otherwise the MI
    # estimator is called internally and the CMI estimator is never tested).
    cmi, dist2, npoints_x, npoints_y, npoints_c = EST_CMI.estimate(
        pointset1, pointset2, pointset3, n_chunks=n_chunks)
    assert np.isclose(dist2[0], 1.5), 'Distances 0 not correct.'
    assert np.isclose(dist2[1], 0.6), 'Distances 1 not correct.'
    assert np.isclose(dist2[2], 0.6), 'Distances 2 not correct.'
    assert np.isclose(dist2[3], 0.9), 'Distances 3 not correct.'
def test_cmi_uncorrelated_gaussians():
    """Test CMI estimator on uncorrelated Gaussian data."""
    n_obs = 10000
    var1 = np.random.randn(n_obs, 1)
    var2 = np.random.randn(n_obs, 1)
    var3 = np.random.randn(n_obs, 1)

    # Run OpenCL estimator.
    settings = {'debug': True}
    ocl_est = OpenCLKraskovCMI(settings=settings)
    (mi_ocl, dist, n_range_var1,
     n_range_var2, n_range_var3) = ocl_est.estimate(var1, var2, var3)
    mi_ocl = mi_ocl[0]

    # Run JIDT estimator.
    jidt_est = JidtKraskovCMI(settings={})
    mi_jidt = jidt_est.estimate(var1, var2, var3)

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to 0 nats for uncorrelated '
          'Gaussians.'.format(mi_jidt, mi_ocl))
    assert np.isclose(mi_jidt, 0, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'JIDT estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, 0, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, mi_jidt, atol=0.0001), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
def test_cmi_correlated_gaussians():
    """Test estimators on correlated Gaussian data with conditional."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    # Run OpenCL estimator.
    settings = {'debug': True}
    ocl_est = OpenCLKraskovCMI(settings=settings)
    (mi_ocl, dist, n_range_var1,
     n_range_var2, n_range_cond) = ocl_est.estimate(source, target,
                                                    source_uncorr)

    mi_ocl = mi_ocl[0]
    # Run JIDT estimator.
    jidt_est = JidtKraskovCMI(settings={})
    mi_jidt = jidt_est.estimate(source, target, source_uncorr)

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: {1:.4f} nats; '
          'expected to be close to {2:.4f} nats for correlated '
          'Gaussians.'.format(mi_jidt, mi_ocl, expected_mi))
    assert np.isclose(mi_jidt, expected_mi, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'JIDT estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, expected_mi, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl, mi_jidt, atol=0.0001), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
def test_amd_data_padding():
    """Test padding necessary for AMD devices."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    settings = {'debug': True, 'return_counts': True}
    est_mi = OpenCLKraskovMI(settings=settings)
    est_cmi = OpenCLKraskovCMI(settings=settings)

    # Run OpenCL estimator for various data sizes.
    for n in [11, 13, 25, 64, 100, 128, 999, 10000, 3781, 50000]:
        for n_chunks in [1, 3, 10, 50, 99]:
            data_run_source = np.tile(source[:n], (n_chunks, 1))
            data_run_target = np.tile(target[:n], (n_chunks, 1))
            mi, dist, n_range_var1, n_range_var2 = est_mi.estimate(
                data_run_source, data_run_target, n_chunks=n_chunks)
            cmi, dist, n_range_var1, n_range_var2 = est_cmi.estimate(
                data_run_source, data_run_target, n_chunks=n_chunks)
    # Run OpenCL esitmator for various no. points and check result for
    # correctness. Note that for smaller sample sizes the error becomes too
    # large.
    n_chunks = 1
    for n in [832, 999, 10000, 3781, 50000]:
        data_run_source = np.tile(source[:n], (n_chunks, 1))
        data_run_target = np.tile(target[:n], (n_chunks, 1))
        mi, dist, n_range_var1, n_range_var2 = est_mi.estimate(
            data_run_source, data_run_target, n_chunks=n_chunks)
        cmi, dist, n_range_var1, n_range_var2 = est_cmi.estimate(
            data_run_source, data_run_target, n_chunks=n_chunks)
        print('{0} points, {1} chunks: OpenCL MI result: {2:.4f} nats; '
              'expected to be close to {3:.4f} nats for correlated '
              'Gaussians.'.format(n, n_chunks, mi[0], expected_mi))
        assert np.isclose(mi[0], expected_mi, atol=0.05), (
            'MI estimation for uncorrelated Gaussians using the OpenCL '
            'estimator failed (error larger 0.05).')
        print('OpenCL CMI result: {0:.4f} nats; expected to be close to '
              '{1:.4f} nats for correlated Gaussians.'.format(
                  cmi[0], expected_mi))
        assert np.isclose(cmi[0], expected_mi, atol=0.05), (
            'CMI estimation for uncorrelated Gaussians using the OpenCL '
            'estimator failed (error larger 0.05).')

    # Test debugging switched off
    settings = {'debug': False, 'return_counts': False}
    est_mi = OpenCLKraskovMI(settings=settings)
    est_cmi = OpenCLKraskovCMI(settings=settings)
    mi = est_mi.estimate(source, target)
    cmi = est_cmi.estimate(source, target)

    settings['local_values'] = True
    est_mi = OpenCLKraskovMI(settings=settings)
    est_cmi = OpenCLKraskovCMI(settings=settings)
    mi = est_mi.estimate(source, target)
    cmi = est_cmi.estimate(source, target)
def test_multiple_runs_two_dim():
    """Test kNN with two chunks of 2D data in the same call."""
    settings = {
        'theiler_t': 0,
        'knn_k': 1,
        'gpu_id': 0,
        'debug': True,
        'max_mem': 5 * 1024 * 1024
    }
    EST_MI = OpenCLKraskovMI(settings)
    EST_CMI = OpenCLKraskovCMI(settings)

    n_chunks = 50000
    pointset1 = np.array([[1, 1.1, -1, -1.2, 1, 1.1, -1, -1.2],
                          [1, 1, -1, -1, 1, 1, -1, -1]]).T.copy()
    pointset1 = np.tile(pointset1, (n_chunks // 2, 1))
    pointset2 = np.ones(pointset1.shape) * 9999
    pointset3 = np.ones(pointset1.shape) * 9999

    # Points:       X    Y                   y
    #               1    1                   |  o o
    #             1.1    1                   |
    #              -1   -1               ----+----x
    #            -1.2   -1                   |
    #                                  o  o  |

    # Call MI estimator
    mi, dist1, npoints_x, npoints_y = EST_MI.estimate(pointset1,
                                                      pointset2,
                                                      n_chunks=n_chunks)

    print(dist1[0:8])
    assert np.isclose(dist1[0], 0.1), 'Distance 0 not correct.'
    assert np.isclose(dist1[1], 0.1), 'Distance 1 not correct.'
    assert np.isclose(dist1[2], 0.2), 'Distance 2 not correct.'
    assert np.isclose(dist1[3], 0.2), 'Distance 3 not correct.'
    assert np.isclose(dist1[4], 0.1), 'Distance 4 not correct.'
    assert np.isclose(dist1[5], 0.1), 'Distance 5 not correct.'
    assert np.isclose(dist1[6], 0.2), 'Distance 6 not correct.'
    assert np.isclose(dist1[7], 0.2), 'Distance 7 not correct.'

    # Call CMI estimator with pointset2 as conditional (otherwise the MI
    # estimator is called internally and the CMI estimator is never tested).
    cmi, dist2, npoints_x, npoints_y, npoints_c = EST_CMI.estimate(
        pointset1, pointset2, pointset3, n_chunks=n_chunks)
    assert np.isclose(dist2[0], 0.1), 'Distance 0 not correct.'
    assert np.isclose(dist2[1], 0.1), 'Distance 1 not correct.'
    assert np.isclose(dist2[2], 0.2), 'Distance 2 not correct.'
    assert np.isclose(dist2[3], 0.2), 'Distance 3 not correct.'
    assert np.isclose(dist2[4], 0.1), 'Distance 4 not correct.'
    assert np.isclose(dist2[5], 0.1), 'Distance 5 not correct.'
    assert np.isclose(dist2[6], 0.2), 'Distance 6 not correct.'
    assert np.isclose(dist2[7], 0.2), 'Distance 7 not correct.'
def test_user_input():

    est_mi = OpenCLKraskovMI()
    est_cmi = OpenCLKraskovCMI()
    N = 1000

    # Unequal variable dimensions.
    with pytest.raises(AssertionError):
        est_mi.estimate(var1=np.random.randn(N, 1),
                        var2=np.random.randn(N + 1, 1))
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N + 1, 1),
                         conditional=np.random.randn(N, 1))
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N, 1),
                         conditional=np.random.randn(N + 1, 1))

    # No. chunks doesn't fit the signal length.
    with pytest.raises(AssertionError):
        est_mi.estimate(var1=np.random.randn(N, 1),
                        var2=np.random.randn(N, 1),
                        n_chunks=7)
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N, 1),
                         conditional=np.random.randn(N, 1),
                         n_chunks=7)
예제 #12
0
def test_amd_data_padding():
    """Test padding necessary for AMD devices."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    settings = {'debug': True}
    est_mi = OpenCLKraskovMI(settings=settings)
    est_cmi = OpenCLKraskovCMI(settings=settings)

    # Run OpenCL estimator for various data sizes.
    for n in [11, 13, 25, 64, 100, 128, 999, 10000, 3781, 50000]:
        for n_chunks in [1, 3, 10, 50, 99]:
            data_run_source = np.tile(source[:n], (n_chunks, 1))
            data_run_target = np.tile(target[:n], (n_chunks, 1))
            mi, dist, n_range_var1, n_range_var2 = est_mi.estimate(
                data_run_source, data_run_target, n_chunks=n_chunks)
            cmi, dist, n_range_var1, n_range_var2 = est_cmi.estimate(
                data_run_source, data_run_target, n_chunks=n_chunks)
    # Run OpenCL esitmator for various no. points and check result for
    # correctness. Note that for smaller sample sizes the error becomes too
    # large.
    n_chunks = 1
    for n in [832, 999, 10000, 3781, 50000]:
        data_run_source = np.tile(source[:n], (n_chunks, 1))
        data_run_target = np.tile(target[:n], (n_chunks, 1))
        mi, dist, n_range_var1, n_range_var2 = est_mi.estimate(
            data_run_source, data_run_target, n_chunks=n_chunks)
        cmi, dist, n_range_var1, n_range_var2 = est_cmi.estimate(
            data_run_source, data_run_target, n_chunks=n_chunks)
        print('{0} points, {1} chunks: OpenCL MI result: {2:.4f} nats; '
              'expected to be close to {3:.4f} nats for correlated '
              'Gaussians.'.format(n, n_chunks, mi[0], expected_mi))
        assert np.isclose(mi[0], expected_mi, atol=0.05), (
            'MI estimation for uncorrelated Gaussians using the OpenCL '
            'estimator failed (error larger 0.05).')
        print('OpenCL CMI result: {0:.4f} nats; expected to be close to '
              '{1:.4f} nats for correlated Gaussians.'.format(
                    cmi[0], expected_mi))
        assert np.isclose(cmi[0], expected_mi, atol=0.05), (
            'CMI estimation for uncorrelated Gaussians using the OpenCL '
            'estimator failed (error larger 0.05).')

    # Test debugging switched off
    settings['debug'] = False
    mi = est_mi.estimate(source, target)
    cmi = est_cmi.estimate(source, target)

    settings['local_values'] = True
    mi = est_mi.estimate(source, target)
    cmi = est_cmi.estimate(source, target)
예제 #13
0
def test_local_values():
    """Test estimation of local MI and CMI using OpenCL estimators."""
    # Get data
    n_chunks = 2
    expec_mi, source, source_uncorr, target = _get_gauss_data(n=20000)
    chunklength = int(source.shape[0] / n_chunks)

    # Estimate local values
    settings = {'local_values': True}
    est_cmi = OpenCLKraskovCMI(settings=settings)
    cmi = est_cmi.estimate(source, target, source_uncorr, n_chunks=n_chunks)

    est_mi = OpenCLKraskovMI(settings=settings)
    mi = est_mi.estimate(source, target, n_chunks=n_chunks)

    mi_ch1 = np.mean(mi[0:chunklength])
    mi_ch2 = np.mean(mi[chunklength:])
    cmi_ch1 = np.mean(cmi[0:chunklength])
    cmi_ch2 = np.mean(cmi[chunklength:])

    # Estimate non-local values for comparison
    settings = {'local_values': False}
    est_cmi = OpenCLKraskovCMI(settings=settings)
    mi = est_cmi.estimate(source, target, source_uncorr, n_chunks=n_chunks)

    est_mi = OpenCLKraskovMI(settings=settings)
    cmi = est_mi.estimate(source, target, n_chunks=n_chunks)

    # Report results
    print('OpenCL MI result: {0:.4f} nats (chunk 1); {1:.4f} nats (chunk 2) '
          'expected to be close to {2:.4f} nats for uncorrelated '
          'Gaussians.'.format(mi_ch1, mi_ch2, expec_mi))
    print('OpenCL CMI result: {0:.4f} nats (chunk 1); {1:.4f} nats (chunk 2) '
          'expected to be close to {2:.4f} nats for uncorrelated '
          'Gaussians.'.format(cmi_ch1, cmi_ch2, expec_mi))

    assert np.isclose(mi_ch1, expec_mi, atol=0.05)
    assert np.isclose(mi_ch2, expec_mi, atol=0.05)
    assert np.isclose(cmi_ch1, expec_mi, atol=0.05)
    assert np.isclose(cmi_ch2, expec_mi, atol=0.05)
    assert np.isclose(mi_ch1, mi_ch2, atol=0.05)
    assert np.isclose(mi_ch1, mi[0], atol=0.05)
    assert np.isclose(mi_ch2, mi[1], atol=0.05)
    assert np.isclose(cmi_ch1, cmi_ch2, atol=0.05)
    assert np.isclose(cmi_ch1, cmi[0], atol=0.05)
    assert np.isclose(cmi_ch2, cmi[1], atol=0.05)
예제 #14
0
def test_user_input():

    est_mi = OpenCLKraskovMI()
    est_cmi = OpenCLKraskovCMI()
    N = 1000

    # Unequal variable dimensions.
    with pytest.raises(AssertionError):
        est_mi.estimate(var1=np.random.randn(N, 1),
                        var2=np.random.randn(N + 1, 1))
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N + 1, 1),
                         conditional=np.random.randn(N, 1))
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N, 1),
                         conditional=np.random.randn(N + 1, 1))

    # No. chunks doesn't fit the signal length.
    with pytest.raises(AssertionError):
        est_mi.estimate(var1=np.random.randn(N, 1),
                        var2=np.random.randn(N, 1),
                        n_chunks=7)
    with pytest.raises(AssertionError):
        est_cmi.estimate(var1=np.random.randn(N, 1),
                         var2=np.random.randn(N, 1),
                         conditional=np.random.randn(N, 1),
                         n_chunks=7)
def test_debug_setting():
    """Test setting of debugging options."""
    settings = {'debug': False, 'return_counts': True}
    # Estimators should raise an error if returning of neighborhood counts is
    # requested without the debugging option being set.
    with pytest.raises(RuntimeError):
        OpenCLKraskovMI(settings=settings)
    with pytest.raises(RuntimeError):
        OpenCLKraskovCMI(settings=settings)

    settings['debug'] = True
    est = OpenCLKraskovMI(settings=settings)
    res = est.estimate(np.arange(10), np.arange(10))
    assert len(res) == 4, (
        'Requesting debugging output from MI estimator did not return the '
        'correct no. values.')
    est = OpenCLKraskovCMI(settings=settings)
    res = est.estimate(np.arange(10), np.arange(10), np.arange(10))
    assert len(res) == 5, (
        'Requesting debugging output from CMI estimator did not return the '
        'correct no. values.')
def test_multiple_runs_two_dim():
    """Test kNN with two chunks of 2D data in the same call."""
    settings = {
        'theiler_t': 0,
        'knn_k': 1,
        'gpu_id': 0,
        'debug': True,
        'return_counts': True,
        'max_mem': 5 * 1024 * 1024
    }
    EST_MI = OpenCLKraskovMI(settings)
    EST_CMI = OpenCLKraskovCMI(settings)

    n_chunks = 50000
    pointset1 = np.array(
        [[-1, 0.5, 1.1, 2, 10, 11, 10.5, -100, -50, 666, 9999, 9999],
         [-1, 0.5, 1.1, 2, 98, -9, -200, 45.3, -53, 0.1, 9999,
          9999]]).T.copy()
    pointset1 = np.tile(pointset1, (n_chunks, 1))
    pointset2 = np.ones(pointset1.shape) * 9999
    pointset3 = np.ones(pointset1.shape) * 9999

    # Call MI estimator
    mi, dist1, npoints_x, npoints_y = EST_MI.estimate(pointset1,
                                                      pointset2,
                                                      n_chunks=n_chunks)
    assert np.isclose(dist1[0], 1.5), 'Distances 0 not correct.'
    assert np.isclose(dist1[1], 0.6), 'Distances 1 not correct.'
    assert np.isclose(dist1[2], 0.6), 'Distances 2 not correct.'
    assert np.isclose(dist1[3], 0.9), 'Distances 3 not correct.'

    # Call CMI estimator with pointset2 as conditional (otherwise the MI
    # estimator is called internally and the CMI estimator is never tested).
    cmi, dist2, npoints_x, npoints_y, npoints_c = EST_CMI.estimate(
        pointset1, pointset2, pointset3, n_chunks=n_chunks)
    assert np.isclose(dist2[0], 1.5), 'Distances 0 not correct.'
    assert np.isclose(dist2[1], 0.6), 'Distances 1 not correct.'
    assert np.isclose(dist2[2], 0.6), 'Distances 2 not correct.'
    assert np.isclose(dist2[3], 0.9), 'Distances 3 not correct.'
예제 #17
0
def test_local_values():
    """Test estimation of local MI and CMI using OpenCL estimators."""
    # Get data
    n_chunks = 2
    expec_mi, source, source_uncorr, target = _get_gauss_data(n=20000,
                                                              seed=SEED)
    chunklength = int(source.shape[0] / n_chunks)

    # Estimate local values
    settings = {'local_values': True}
    est_cmi = OpenCLKraskovCMI(settings=settings)
    cmi = est_cmi.estimate(source, target, source_uncorr, n_chunks=n_chunks)

    est_mi = OpenCLKraskovMI(settings=settings)
    mi = est_mi.estimate(source, target, n_chunks=n_chunks)

    mi_ch1 = np.mean(mi[0:chunklength])
    mi_ch2 = np.mean(mi[chunklength:])
    cmi_ch1 = np.mean(cmi[0:chunklength])
    cmi_ch2 = np.mean(cmi[chunklength:])

    # Estimate non-local values for comparison
    settings = {'local_values': False}
    est_cmi = OpenCLKraskovCMI(settings=settings)
    mi = est_cmi.estimate(source, target, source_uncorr, n_chunks=n_chunks)

    est_mi = OpenCLKraskovMI(settings=settings)
    cmi = est_mi.estimate(source, target, n_chunks=n_chunks)

    # Report results
    print('OpenCL MI result: {0:.4f} nats (chunk 1); {1:.4f} nats (chunk 2) '
          'expected to be close to {2:.4f} nats for uncorrelated '
          'Gaussians.'.format(mi_ch1, mi_ch2, expec_mi))
    print('OpenCL CMI result: {0:.4f} nats (chunk 1); {1:.4f} nats (chunk 2) '
          'expected to be close to {2:.4f} nats for uncorrelated '
          'Gaussians.'.format(cmi_ch1, cmi_ch2, expec_mi))

    assert np.isclose(mi_ch1, expec_mi, atol=0.05)
    assert np.isclose(mi_ch2, expec_mi, atol=0.05)
    assert np.isclose(cmi_ch1, expec_mi, atol=0.05)
    assert np.isclose(cmi_ch2, expec_mi, atol=0.05)
    assert np.isclose(mi_ch1, mi_ch2, atol=0.05)
    assert np.isclose(mi_ch1, mi[0], atol=0.05)
    assert np.isclose(mi_ch2, mi[1], atol=0.05)
    assert np.isclose(cmi_ch1, cmi_ch2, atol=0.05)
    assert np.isclose(cmi_ch1, cmi[0], atol=0.05)
    assert np.isclose(cmi_ch2, cmi[1], atol=0.05)
# Skip test module if pyopencl is not installed
pytest.importorskip('pyopencl')

settings = {
    'theiler_t': 0,
    'kraskov_k': 1,
    'noise_level': 0,
    'gpu_id': 0,
    'debug': True,
    'return_counts': True,
    'verbose': True
}

EST_MI = OpenCLKraskovMI(settings)
EST_CMI = OpenCLKraskovCMI(settings)

CHUNK_LENGTH = 500000  # add noise to beginning of chunks to achieve this


def test_three_large_chunks():
    """Test kNN with three large chunks, put test points at chunk end."""
    # Data for three individual chunks
    n_chunks = 3
    chunk1 = np.expand_dims(np.hstack(
        (np.ones(CHUNK_LENGTH - 4) * 9999, [5, 6, -5, -7])),
                            axis=1)
    chunk2 = np.expand_dims(np.hstack(
        (np.ones(CHUNK_LENGTH - 4) * 9999, [50, -50, 60, -70])),
                            axis=1)
    chunk3 = np.expand_dims(np.hstack(
예제 #19
0
print('Estimated CMI: {0:.5f}, expected CMI: {1:.5f}'.format(cmi, expected_mi))
est = JidtGaussianMI(settings)
mi = est.estimate(source_cor, target)
print('Estimated MI: {0:.5f}, expected MI: {1:.5f}'.format(mi, expected_mi))
settings['history_target'] = 1
est = JidtGaussianTE(settings)
te = est.estimate(source_cor[1:n], target[0:n - 1])
print('Estimated TE: {0:.5f}, expected TE: {1:.5f}'.format(te, expected_mi))
settings['history'] = 1
est = JidtGaussianAIS(settings)
ais = est.estimate(target)
print('Estimated AIS: {0:.5f}, expected AIS: ~0'.format(ais))

# OpenCL Kraskov estimators
settings = {}
est = OpenCLKraskovCMI(settings)
cmi = est.estimate(source_cor, target, source_uncor)
print('Estimated CMI: {0:.5f}, expected CMI: {1:.5f}'.format(
    cmi[0], expected_mi))
est = OpenCLKraskovMI(settings)
mi = est.estimate(source_cor, target)
print('Estimated MI: {0:.5f}, expected MI: {1:.5f}'.format(mi[0], expected_mi))

# Generate binary test data
alph_x = 2
alph_y = 2
alph_z = 2
x = np.random.randint(0, alph_x, n)
y = np.random.randint(0, alph_y, n)
z = np.logical_xor(x, y).astype(int)