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)
Exemplo n.º 2
0
def test_mi_correlated_gaussians():
    """Test estimators on correlated Gaussian data."""
    expected_mi, source, source_uncorr, target = _get_gauss_data(seed=SEED)

    # Run OpenCL estimator.
    settings = {'debug': True, 'return_counts': True}
    ocl_est = OpenCLKraskovMI(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 = JidtKraskovMI(settings={})
    mi_jidt = jidt_est.estimate(source, target)

    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_mi_uncorrelated_gaussians_three_dims():
    """Test MI estimator on uncorrelated 3D Gaussian data."""
    n_obs = 10000
    dim = 3
    var1 = np.random.randn(n_obs, dim)
    var2 = np.random.randn(n_obs, dim)

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

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

    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_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_mi_uncorrelated_gaussians_three_dims():
    """Test MI estimator on uncorrelated 3D Gaussian data."""
    n_obs = 10000
    dim = 3
    var1 = np.random.randn(n_obs, dim)
    var2 = np.random.randn(n_obs, dim)

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

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

    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_mi_correlated_gaussians():
    """Test estimators on correlated Gaussian data."""
    expected_mi, source, source_uncorr, target = _get_gauss_data()

    # Run OpenCL estimator.
    settings = {'debug': True}
    ocl_est = OpenCLKraskovMI(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 = JidtKraskovMI(settings={})
    mi_jidt = jidt_est.estimate(source, target)

    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).')
Exemplo n.º 7
0
def knn_mi_est(pointset,
               kth,
               theiler,
               nchunkspergpu,
               pointsdim,
               signallengthpergpu,
               gpuid,
               chunklength,
               cpu=False,
               padding=True):
    settings = {
        'gpuid': gpuid,
        'kraskov_k': kth,
        'normalise': False,
        'theiler_t': theiler,
        'noise_level': 0,
        'debug': True,
        'padding': padding,
        'return_counts': True,
        'lag_mi': 0
    }

    est_mi = OpenCLKraskovMI(settings)
    mi_array, distances, count_var1, count_var2 = est_mi.estimate(
        pointset[0, :], pointset[1, :], nchunkspergpu)
    print(distances.shape)
    print(count_var1.shape)
    return distances
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.'
Exemplo n.º 10
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)
Exemplo n.º 11
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)
Exemplo n.º 12
0
def test_mi_correlated_gaussians_two_chunks():
    """Test estimators on two chunks of correlated Gaussian data."""
    expected_mi, source, source_uncorr, target = _get_gauss_data(n=20000,
                                                                 seed=SEED)
    n_points = source.shape[0]

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

    # Run JIDT estimator.
    jidt_est = JidtKraskovMI(settings={})
    mi_jidt = jidt_est.estimate(source[0:int(n_points / 2), :],
                                target[0:int(n_points / 2), :])

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: [{1:.4f}, {2:.4f}] '
          'nats; expected to be close to {3:.4f} nats for correlated '
          'Gaussians.'.format(mi_jidt, mi_ocl[0], mi_ocl[1], 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[0], expected_mi,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl[0], mi_jidt,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl[1], mi_jidt,
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(
        mi_ocl[0], mi_ocl[1],
        atol=0.05), ('MI estimation for uncorrelated Gaussians using the '
                     'OpenCL estimator failed (error larger 0.05).')
Exemplo n.º 13
0
 def __init__(self, data_loader: DataLoader, pca_size=50, debug=False):
     super().__init__(data_loader=data_loader,
                      pca_size=pca_size,
                      debug=debug)
     settings = {'kraskov_k': 4}
     try:
         self.estimator = OpenCLKraskovMI(settings=settings)
     except RuntimeError:
         warnings.warn("No OpenCL backed detected. Run "
                       "'conda install -c conda-forge pyopencl' "
                       "in a terminal.")
         self.estimator = JidtKraskovMI(settings=settings)
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_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.'
Exemplo n.º 16
0
def test_mi_correlated_gaussians_two_chunks():
    """Test estimators on two chunks of correlated Gaussian data."""
    expected_mi, source, source_uncorr, target = _get_gauss_data(n=20000)
    n_points = source.shape[0]

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

    # Run JIDT estimator.
    jidt_est = JidtKraskovMI(settings={})
    mi_jidt = jidt_est.estimate(source[0:int(n_points/2), :],
                                target[0:int(n_points/2), :])

    print('JIDT MI result: {0:.4f} nats; OpenCL MI result: [{1:.4f}, {2:.4f}] '
          'nats; expected to be close to {3:.4f} nats for correlated '
          'Gaussians.'.format(mi_jidt, mi_ocl[0], mi_ocl[1], 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[0], expected_mi, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl[0], mi_jidt, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl[1], mi_jidt, atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
    assert np.isclose(mi_ocl[0], mi_ocl[1], atol=0.05), (
                        'MI estimation for uncorrelated Gaussians using the '
                        'OpenCL estimator failed (error larger 0.05).')
Exemplo n.º 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)
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.')
from idtxl.estimators_opencl import OpenCLKraskovMI, OpenCLKraskovCMI

# 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)
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)
Exemplo n.º 21
0
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)

# PID estimators
settings = {
    'alph_s1': alph_x,
    'alph_s2': alph_y,
Exemplo n.º 22
0
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)