|
| 1 | +import itertools |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
| 7 | +import tsam |
| 8 | +from tsam import ClusterConfig |
| 9 | + |
| 10 | +# All clustering methods (excluding "averaging" which does not cluster into n_clusters) |
| 11 | +_METHODS = ["kmeans", "kmedoids", "kmaxoids", "hierarchical", "contiguous"] |
| 12 | + |
| 13 | +# All representation methods |
| 14 | +_REPRESENTATIONS = ["mean", "medoid", "maxoid", "distribution", "distribution_minmax"] |
| 15 | + |
| 16 | +# Use duration curves when clustering by value distribution |
| 17 | +_DISTRIBUTION_REPS = {"distribution", "distribution_minmax"} |
| 18 | + |
| 19 | +_PARAMS = [ |
| 20 | + pytest.param( |
| 21 | + method, |
| 22 | + rep, |
| 23 | + rep in _DISTRIBUTION_REPS, |
| 24 | + id=f"{method}_{rep}", |
| 25 | + ) |
| 26 | + for method, rep in itertools.product(_METHODS, _REPRESENTATIONS) |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="module") |
| 31 | +def input_data() -> pd.DataFrame: |
| 32 | + costs = pd.DataFrame( |
| 33 | + [ |
| 34 | + np.array([0.05, 0.0, 0.1, 0.051]), |
| 35 | + np.array([0.0, 0.0, 0.0, 0.0]), |
| 36 | + ], |
| 37 | + index=["ElectrolyzerLocation", "IndustryLocation"], |
| 38 | + ).T |
| 39 | + revenues = pd.DataFrame( |
| 40 | + [ |
| 41 | + np.array([0.0, 0.01, 0.0, 0.0]), |
| 42 | + np.array([0.0, 0.0, 0.0, 0.0]), |
| 43 | + ], |
| 44 | + index=["ElectrolyzerLocationRevenue", "IndustryLocationRevenue"], |
| 45 | + ).T |
| 46 | + |
| 47 | + timeSeriesData = pd.concat([costs, revenues], axis=1) |
| 48 | + timeSeriesData.index = pd.date_range( |
| 49 | + "2050-01-01 00:30:00", |
| 50 | + periods=4, |
| 51 | + freq="1h", |
| 52 | + tz="Europe/Berlin", |
| 53 | + ) |
| 54 | + return timeSeriesData |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize("method,representation,use_duration_curves", _PARAMS) |
| 58 | +def test_same_cluster_as_input_data( |
| 59 | + input_data: pd.DataFrame, |
| 60 | + method: str, |
| 61 | + representation: str, |
| 62 | + use_duration_curves: bool, |
| 63 | +) -> None: |
| 64 | + """When n_clusters equals the number of input periods, reconstruction must |
| 65 | + be identical to the original time series for every method/representation.""" |
| 66 | + results = tsam.aggregate( |
| 67 | + input_data, |
| 68 | + n_clusters=4, |
| 69 | + period_duration=1, |
| 70 | + cluster=ClusterConfig( |
| 71 | + method=method, |
| 72 | + representation=representation, |
| 73 | + use_duration_curves=use_duration_curves, |
| 74 | + ), |
| 75 | + ) |
| 76 | + pd.testing.assert_frame_equal(results.reconstructed, input_data) |
0 commit comments