|
| 1 | +import random |
| 2 | +import re |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | +from sklearn.model_selection import train_test_split |
| 7 | + |
| 8 | +from sdmetrics.demos import load_single_table_demo |
| 9 | +from sdmetrics.single_table.privacy import DCROverfittingProtection |
| 10 | + |
| 11 | + |
| 12 | +class TestDCROverfittingProtection: |
| 13 | + def test_end_to_end_with_demo(self): |
| 14 | + """Test end to end for DCROverfittingProtection metric against the demo dataset. |
| 15 | +
|
| 16 | + In this end to end test, test against demo dataset. Use subsampling to speed |
| 17 | + up the test. Make sure that if hold two datasets to be the same we get expected |
| 18 | + values even with subsampling. Note that if synthetic data is equally distant from |
| 19 | + the training data and the holdout data, it is labeled as closer to holdout data. |
| 20 | + """ |
| 21 | + # Setup |
| 22 | + real_data, synthetic_data, metadata = load_single_table_demo() |
| 23 | + train_df, holdout_df = train_test_split(real_data, test_size=0.2) |
| 24 | + |
| 25 | + # Run |
| 26 | + num_rows_subsample = 50 |
| 27 | + compute_breakdown_result = DCROverfittingProtection.compute_breakdown( |
| 28 | + train_df, synthetic_data, holdout_df, metadata |
| 29 | + ) |
| 30 | + compute_result = DCROverfittingProtection.compute( |
| 31 | + train_df, synthetic_data, holdout_df, metadata |
| 32 | + ) |
| 33 | + compute_holdout_same = DCROverfittingProtection.compute_breakdown( |
| 34 | + train_df, synthetic_data, synthetic_data, metadata, num_rows_subsample |
| 35 | + ) |
| 36 | + compute_train_same = DCROverfittingProtection.compute_breakdown( |
| 37 | + synthetic_data, synthetic_data, holdout_df, metadata, num_rows_subsample |
| 38 | + ) |
| 39 | + compute_all_same = DCROverfittingProtection.compute_breakdown( |
| 40 | + synthetic_data, |
| 41 | + synthetic_data, |
| 42 | + synthetic_data, |
| 43 | + metadata, |
| 44 | + num_rows_subsample, |
| 45 | + ) |
| 46 | + |
| 47 | + synth_percentages_key = 'synthetic_data_percentages' |
| 48 | + synth_train_key = 'closer_to_training' |
| 49 | + synth_holdout_key = 'closer_to_holdout' |
| 50 | + score_key = 'score' |
| 51 | + |
| 52 | + # Assert |
| 53 | + assert compute_result == compute_breakdown_result[score_key] |
| 54 | + assert compute_holdout_same[score_key] == 1.0 |
| 55 | + assert compute_holdout_same[synth_percentages_key][synth_train_key] == 0.0 |
| 56 | + assert compute_holdout_same[synth_percentages_key][synth_holdout_key] == 1.0 |
| 57 | + assert compute_train_same[score_key] == 0.0 |
| 58 | + assert compute_train_same[synth_percentages_key][synth_train_key] == 1.0 |
| 59 | + assert compute_train_same[synth_percentages_key][synth_holdout_key] == 0.0 |
| 60 | + assert compute_all_same[score_key] == 1.0 |
| 61 | + assert compute_all_same[synth_percentages_key][synth_train_key] == 0.0 |
| 62 | + assert compute_all_same[synth_percentages_key][synth_holdout_key] == 1.0 |
| 63 | + |
| 64 | + def test_compute_breakdown_drop_all_columns(self): |
| 65 | + """Testing invalid sdtypes and ensure only appropriate columns are measured.""" |
| 66 | + # Setup |
| 67 | + train_data = pd.DataFrame({'bad_col': [10.0, 15.0], 'num_col': [1.0, 2.0]}) |
| 68 | + synth_data = pd.DataFrame({'bad_col': [2.0, 1.0], 'num_col': [1.0, 2.0]}) |
| 69 | + holdout_data = pd.DataFrame({'bad_col': [2.0, 1.0], 'num_col': [3.0, 4.0]}) |
| 70 | + metadata = { |
| 71 | + 'columns': { |
| 72 | + 'bad_col': {'sdtype': 'unknown'}, |
| 73 | + 'num_col': {'sdtype': 'numerical'}, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Run |
| 78 | + result = DCROverfittingProtection.compute_breakdown( |
| 79 | + train_data, synth_data, holdout_data, metadata |
| 80 | + ) |
| 81 | + |
| 82 | + # Assert |
| 83 | + assert result['score'] == 0.0 |
| 84 | + assert result['synthetic_data_percentages']['closer_to_training'] == 1.0 |
| 85 | + assert result['synthetic_data_percentages']['closer_to_holdout'] == 0.0 |
| 86 | + |
| 87 | + def test_compute_breakdown_subsampling(self): |
| 88 | + """Test subsampling produces different values.""" |
| 89 | + # Setup |
| 90 | + train_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(50)]}) |
| 91 | + holdout_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(50)]}) |
| 92 | + synthetic_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(50)]}) |
| 93 | + metadata = {'columns': {'num_col': {'sdtype': 'numerical'}}} |
| 94 | + num_rows_subsample = 4 |
| 95 | + large_num_subsample = len(synthetic_data) * 2 |
| 96 | + |
| 97 | + # Run |
| 98 | + compute_subsample = DCROverfittingProtection.compute_breakdown( |
| 99 | + train_data, synthetic_data, holdout_data, metadata, num_rows_subsample |
| 100 | + ) |
| 101 | + |
| 102 | + large_subsample_msg = re.escape('Ignoring the num_rows_subsample and num_iterations args.') |
| 103 | + with pytest.warns(UserWarning, match=large_subsample_msg): |
| 104 | + compute_large_subsample = DCROverfittingProtection.compute_breakdown( |
| 105 | + train_data, synthetic_data, holdout_data, metadata, large_num_subsample |
| 106 | + ) |
| 107 | + |
| 108 | + compute_full_1 = DCROverfittingProtection.compute_breakdown( |
| 109 | + train_data, synthetic_data, holdout_data, metadata |
| 110 | + ) |
| 111 | + compute_full_2 = DCROverfittingProtection.compute_breakdown( |
| 112 | + train_data, synthetic_data, holdout_data, metadata |
| 113 | + ) |
| 114 | + |
| 115 | + # Assert that subsampling provides different values if smaller than data length. |
| 116 | + assert compute_subsample != compute_full_1 |
| 117 | + assert compute_full_1 == compute_full_2 |
| 118 | + assert compute_large_subsample == compute_full_1 |
| 119 | + |
| 120 | + def test_compute_breakdown_iterations(self): |
| 121 | + """Test that number iterations for subsampling works as expected.""" |
| 122 | + # Setup |
| 123 | + train_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(10)]}) |
| 124 | + holdout_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(10)]}) |
| 125 | + synthetic_data = pd.DataFrame({'num_col': [random.randint(1, 1000) for _ in range(10)]}) |
| 126 | + metadata = {'columns': {'num_col': {'sdtype': 'numerical'}}} |
| 127 | + num_rows_subsample = 3 |
| 128 | + num_iterations = 1000 |
| 129 | + |
| 130 | + # Run |
| 131 | + compute_num_iteration_1 = DCROverfittingProtection.compute_breakdown( |
| 132 | + train_data, synthetic_data, holdout_data, metadata, num_rows_subsample, 1 |
| 133 | + ) |
| 134 | + compute_num_iteration_1000 = DCROverfittingProtection.compute_breakdown( |
| 135 | + train_data, synthetic_data, holdout_data, metadata, num_rows_subsample, num_iterations |
| 136 | + ) |
| 137 | + compute_train_same = DCROverfittingProtection.compute_breakdown( |
| 138 | + synthetic_data, |
| 139 | + synthetic_data, |
| 140 | + holdout_data, |
| 141 | + metadata, |
| 142 | + num_rows_subsample, |
| 143 | + num_iterations, |
| 144 | + ) |
| 145 | + |
| 146 | + # Assert |
| 147 | + assert compute_num_iteration_1 != compute_num_iteration_1000 |
| 148 | + assert compute_train_same['score'] == 0.0 |
0 commit comments