def test_cmi_uncorrelated_gaussians_three_dims(): """Test CMI estimator on uncorrelated 3D Gaussian data.""" n_obs = 10000 dim = 3 np.random.seed(SEED) var1 = np.random.randn(n_obs, dim) var2 = np.random.randn(n_obs, dim) var3 = np.random.randn(n_obs, dim) # Run OpenCL estimator. settings = {'debug': True, 'return_counts': True} ocl_est = OpenCLKraskovCMI(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 = JidtKraskovCMI(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).') # Run with conditional (mi_ocl, dist, n_range_var1, n_range_var2, n_range_var3) = ocl_est.estimate(var1, var2, var3) mi_ocl = mi_ocl[0] 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_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_cmi_uncorrelated_gaussians_three_dims(): """Test CMI 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) var3 = np.random.randn(n_obs, dim) # Run OpenCL estimator. settings = {'debug': True} ocl_est = OpenCLKraskovCMI(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 = JidtKraskovCMI(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).') # Run with conditional (mi_ocl, dist, n_range_var1, n_range_var2, n_range_var3) = ocl_est.estimate(var1, var2, var3) mi_ocl = mi_ocl[0] 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_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)
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_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_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_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_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_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)
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)
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.'