|
| 1 | +import re |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from sdmetrics.timeseries import StatisticMSAS |
| 7 | + |
| 8 | + |
| 9 | +class TestStatisticMSAS: |
| 10 | + def test_compute_identical_sequences(self): |
| 11 | + """Test it returns 1 when real and synthetic data are identical.""" |
| 12 | + # Setup |
| 13 | + real_keys = pd.Series(['id1', 'id1', 'id1', 'id2', 'id2', 'id2']) |
| 14 | + real_values = pd.Series([1, 2, 3, 4, 5, 6]) |
| 15 | + synthetic_keys = pd.Series(['id3', 'id3', 'id3', 'id4', 'id4', 'id4']) |
| 16 | + synthetic_values = pd.Series([1, 2, 3, 4, 5, 6]) |
| 17 | + |
| 18 | + # Run and Assert |
| 19 | + for statistic in ['mean', 'median', 'std', 'min', 'max']: |
| 20 | + score = StatisticMSAS.compute( |
| 21 | + real_data=(real_keys, real_values), |
| 22 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 23 | + statistic=statistic, |
| 24 | + ) |
| 25 | + assert score == 1 |
| 26 | + |
| 27 | + def test_compute_different_sequences(self): |
| 28 | + """Test it for distinct distributions.""" |
| 29 | + # Setup |
| 30 | + real_keys = pd.Series(['id1', 'id1', 'id1', 'id2', 'id2', 'id2']) |
| 31 | + real_values = pd.Series([1, 2, 3, 4, 5, 6]) |
| 32 | + synthetic_keys = pd.Series(['id3', 'id3', 'id3', 'id4', 'id4', 'id4']) |
| 33 | + synthetic_values = pd.Series([10, 20, 30, 40, 50, 60]) |
| 34 | + |
| 35 | + # Run and Assert |
| 36 | + for statistic in ['mean', 'median', 'std', 'min', 'max']: |
| 37 | + score = StatisticMSAS.compute( |
| 38 | + real_data=(real_keys, real_values), |
| 39 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 40 | + statistic=statistic, |
| 41 | + ) |
| 42 | + assert score == 0 |
| 43 | + |
| 44 | + def test_compute_with_single_sequence(self): |
| 45 | + """Test it with a single sequence.""" |
| 46 | + # Setup |
| 47 | + real_keys = pd.Series(['id1', 'id1', 'id1']) |
| 48 | + real_values = pd.Series([1, 2, 3]) |
| 49 | + synthetic_keys = pd.Series(['id2', 'id2', 'id2']) |
| 50 | + synthetic_values = pd.Series([1, 2, 3]) |
| 51 | + |
| 52 | + # Run |
| 53 | + score = StatisticMSAS.compute( |
| 54 | + real_data=(real_keys, real_values), |
| 55 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 56 | + statistic='mean', |
| 57 | + ) |
| 58 | + |
| 59 | + # Assert |
| 60 | + assert score == 1 |
| 61 | + |
| 62 | + def test_compute_with_different_sequence_lengths(self): |
| 63 | + """Test it with different sequence lengths.""" |
| 64 | + # Setup |
| 65 | + real_keys = pd.Series(['id1', 'id1', 'id1', 'id2', 'id2']) |
| 66 | + real_values = pd.Series([1, 2, 3, 4, 5]) |
| 67 | + synthetic_keys = pd.Series(['id2', 'id2', 'id3', 'id4', 'id5']) |
| 68 | + synthetic_values = pd.Series([1, 2, 3, 4, 5]) |
| 69 | + |
| 70 | + # Run |
| 71 | + score = StatisticMSAS.compute( |
| 72 | + real_data=(real_keys, real_values), |
| 73 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 74 | + statistic='mean', |
| 75 | + ) |
| 76 | + |
| 77 | + # Assert |
| 78 | + assert score == 0.75 |
| 79 | + |
| 80 | + def test_compute_with_invalid_statistic(self): |
| 81 | + """Test it raises ValueError for invalid statistic.""" |
| 82 | + # Setup |
| 83 | + real_keys = pd.Series(['id1', 'id1', 'id1']) |
| 84 | + real_values = pd.Series([1, 2, 3]) |
| 85 | + synthetic_keys = pd.Series(['id2', 'id2', 'id2']) |
| 86 | + synthetic_values = pd.Series([1, 2, 3]) |
| 87 | + |
| 88 | + # Run and Assert |
| 89 | + err_msg = re.escape( |
| 90 | + 'Invalid statistic: invalid. Choose from [mean, median, std, min, max].' |
| 91 | + ) |
| 92 | + with pytest.raises(ValueError, match=err_msg): |
| 93 | + StatisticMSAS.compute( |
| 94 | + real_data=(real_keys, real_values), |
| 95 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 96 | + statistic='invalid', |
| 97 | + ) |
| 98 | + |
| 99 | + def test_compute_invalid_real_data(self): |
| 100 | + """Test that it raises ValueError when real_data is invalid.""" |
| 101 | + # Setup |
| 102 | + real_data = [[1, 2, 3], [4, 5, 6]] # Not a tuple of pandas Series |
| 103 | + synthetic_keys = pd.Series(['id1', 'id1', 'id2', 'id2']) |
| 104 | + synthetic_values = pd.Series([1, 2, 3, 4]) |
| 105 | + |
| 106 | + # Run and Assert |
| 107 | + with pytest.raises(ValueError, match='The data must be a tuple of two pandas series.'): |
| 108 | + StatisticMSAS.compute( |
| 109 | + real_data=real_data, |
| 110 | + synthetic_data=(synthetic_keys, synthetic_values), |
| 111 | + ) |
| 112 | + |
| 113 | + def test_compute_invalid_synthetic_data(self): |
| 114 | + """Test that it raises ValueError when synthetic_data is invalid.""" |
| 115 | + # Setup |
| 116 | + real_keys = pd.Series(['id1', 'id1', 'id2', 'id2']) |
| 117 | + real_values = pd.Series([1, 2, 3, 4]) |
| 118 | + synthetic_data = [[1, 2, 3], [4, 5, 6]] # Not a tuple of pandas Series |
| 119 | + |
| 120 | + # Run and Assert |
| 121 | + with pytest.raises(ValueError, match='The data must be a tuple of two pandas series.'): |
| 122 | + StatisticMSAS.compute( |
| 123 | + real_data=(real_keys, real_values), |
| 124 | + synthetic_data=synthetic_data, |
| 125 | + ) |
0 commit comments