Skip to content

tsam.timeseriesaggregation

tsam.timeseriesaggregation

TimeSeriesAggregation

Clusters time series data to typical periods.

Source code in src/tsam/timeseriesaggregation.py
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
class TimeSeriesAggregation:
    """
    Clusters time series data to typical periods.
    """

    CLUSTER_METHODS = [
        "averaging",
        "k_means",
        "k_medoids",
        "k_maxoids",
        "hierarchical",
        "adjacent_periods",
    ]

    REPRESENTATION_METHODS = [
        "meanRepresentation",
        "medoidRepresentation",
        "maxoidRepresentation",
        "minmaxmeanRepresentation",
        "durationRepresentation",
        "distributionRepresentation",
        "distributionAndMinMaxRepresentation",
    ]

    EXTREME_PERIOD_METHODS = [
        "None",
        "append",
        "new_cluster_center",
        "replace_cluster_center",
    ]

    def __init__(
        self,
        timeSeries,
        resolution=None,
        noTypicalPeriods=10,
        noSegments=10,
        hoursPerPeriod=24,
        clusterMethod="hierarchical",
        evalSumPeriods=False,
        sortValues=False,
        sameMean=False,
        rescaleClusterPeriods=True,
        rescaleExcludeColumns=None,
        weightDict=None,
        segmentation=False,
        extremePeriodMethod="None",
        representationMethod=None,
        representationDict=None,
        distributionPeriodWise=True,
        segmentRepresentationMethod=None,
        predefClusterOrder=None,
        predefClusterCenterIndices=None,
        predefExtremeClusterIdx=None,
        predefSegmentOrder=None,
        predefSegmentDurations=None,
        predefSegmentCenters=None,
        solver="highs",
        numericalTolerance=1e-13,
        roundOutput=None,
        addPeakMin=None,
        addPeakMax=None,
        addMeanMin=None,
        addMeanMax=None,
    ):
        """
        Initialize the periodly clusters.

        :param timeSeries: DataFrame with the datetime as index and the relevant
            time series parameters as columns. required
        :type timeSeries: pandas.DataFrame() or dict

        :param resolution: Resolution of the time series in hours [h]. If timeSeries is a
            pandas.DataFrame() the resolution is derived from the datetime
            index. optional, default: delta_T in timeSeries
        :type resolution: float

        :param hoursPerPeriod: Value which defines the length of a cluster period. optional, default: 24
        :type hoursPerPeriod: integer

        :param noTypicalPeriods: Number of typical Periods - equivalent to the number of clusters. optional, default: 10
        :type noTypicalPeriods: integer

        :param noSegments: Number of segments in which the typical periods shoul be subdivided - equivalent to the
            number of inner-period clusters. optional, default: 10
        :type noSegments: integer

        :param clusterMethod: Chosen clustering method. optional, default: 'hierarchical'
            |br| Options are:

            * 'averaging'
            * 'k_means'
            * 'k_medoids'
            * 'k_maxoids'
            * 'hierarchical'
            * 'adjacent_periods'
        :type clusterMethod: string

        :param evalSumPeriods: Boolean if in the clustering process also the averaged periodly values
            shall be integrated additional to the periodly profiles as parameters. optional, default: False
        :type evalSumPeriods: boolean

        :param sameMean: Boolean which is used in the normalization procedure. If true, all time series get normalized
            such that they have the same mean value. optional, default: False
        :type sameMean: boolean

        :param sortValues: Boolean if the clustering should be done by the periodly duration
            curves (true) or the original shape of the data. optional (default: False)
        :type sortValues: boolean

        :param rescaleClusterPeriods: Decides if the cluster Periods shall get rescaled such that their
            weighted mean value fits the mean value of the original time series. optional (default: True)
        :type rescaleClusterPeriods: boolean

        :param weightDict: Dictionary which weights the profiles. It is done by scaling
            the time series while the normalization process. Normally all time
            series have a scale from 0 to 1. By scaling them, the values get
            different distances to each other and with this, they are
            differently evaluated while the clustering process. optional (default: None )
        :type weightDict: dict

        :param segmentation: Boolean if time steps in periods should be aggregated to segments. optional (default: False)
        :type segmentation: boolean

        :param extremePeriodMethod: Method how to integrate extreme Periods (peak demand, lowest temperature etc.)
            into to the typical period profiles. optional, default: 'None'
            |br| Options are:

            * None: No integration at all.
            * 'append': append typical Periods to cluster centers
            * 'new_cluster_center': add the extreme period as additional cluster center. It is checked then for all
              Periods if they fit better to the this new center or their original cluster center.
            * 'replace_cluster_center': replaces the cluster center of the
              cluster where the extreme period belongs to with the periodly profile of the extreme period. (Worst
              case system design)
        :type extremePeriodMethod: string

        :param representationMethod: Chosen representation. If specified, the clusters are represented in the chosen
            way. Otherwise, each clusterMethod has its own commonly used default representation method.
            |br| Options are:

            * 'meanRepresentation' (default of 'averaging' and 'k_means')
            * 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
            * 'minmaxmeanRepresentation'
            * 'durationRepresentation'/ 'distributionRepresentation'
            * 'distribtionAndMinMaxRepresentation'
        :type representationMethod: string

        :param representationDict: Dictionary which states for each attribute whether the profiles in each cluster
            should be represented by the minimum value or maximum value of each time step. This enables estimations
            to the safe side. This dictionary is needed when 'minmaxmeanRepresentation' is chosen. If not specified, the
            dictionary is set to containing 'mean' values only.
        :type representationDict: dict

        :param distributionPeriodWise: If durationRepresentation is chosen, you can choose whether the distribution of
            each cluster should be separately preserved or that of the original time series only (default: True)
        :type distributionPeriodWise:

        :param segmentRepresentationMethod: Chosen representation for the segments. If specified, the segments are
            represented in the chosen way. Otherwise, it is inherited from the representationMethod.
            |br| Options are:

            * 'meanRepresentation' (default of 'averaging' and 'k_means')
            * 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
            * 'minmaxmeanRepresentation'
            * 'durationRepresentation'/ 'distributionRepresentation'
            * 'distribtionAndMinMaxRepresentation'
        :type segmentRepresentationMethod: string

        :param predefClusterOrder: Instead of aggregating a time series, a predefined grouping is taken
            which is given by this list. optional (default: None)
        :type predefClusterOrder: list or array

        :param predefClusterCenterIndices: If predefClusterOrder is give, this list can define the representative
            cluster candidates. Otherwise the medoid is taken. optional (default: None)
        :type predefClusterCenterIndices: list or array

        :param solver: Solver that is used for k_medoids clustering. optional (default: 'cbc' )
        :type solver: string

        :param numericalTolerance: Tolerance for numerical issues. Silences the warning for exceeding upper or lower bounds
            of the time series. optional (default: 1e-13 )
        :type numericalTolerance: float

        :param roundOutput: Decimals to what the output time series get round. optional (default: None )
        :type roundOutput: integer

        :param addPeakMin: List of column names which's minimal value shall be added to the
            typical periods. E.g.: ['Temperature']. optional, default: []
        :type addPeakMin: list

        :param addPeakMax: List of column names which's maximal value shall be added to the
            typical periods. E.g. ['EDemand', 'HDemand']. optional, default: []
        :type addPeakMax: list

        :param addMeanMin: List of column names where the period with the cumulative minimal value
            shall be added to the typical periods. E.g. ['Photovoltaic']. optional, default: []
        :type addMeanMin: list

        :param addMeanMax: List of column names where the period with the cumulative maximal value
            shall be added to the typical periods. optional, default: []
        :type addMeanMax: list
        """
        warnings.warn(
            "TimeSeriesAggregation will be removed in tsam v4.0. "
            "Use tsam.aggregate() instead. See the migration guide in the documentation.",
            LegacyAPIWarning,
            stacklevel=2,
        )
        if addMeanMin is None:
            addMeanMin = []
        if addMeanMax is None:
            addMeanMax = []
        if addPeakMax is None:
            addPeakMax = []
        if addPeakMin is None:
            addPeakMin = []
        if weightDict is None:
            weightDict = {}
        self.timeSeries = timeSeries

        self.resolution = resolution

        self.hoursPerPeriod = hoursPerPeriod

        self.noTypicalPeriods = noTypicalPeriods

        self.noSegments = noSegments

        self.clusterMethod = clusterMethod

        self.extremePeriodMethod = extremePeriodMethod

        self.evalSumPeriods = evalSumPeriods

        self.sortValues = sortValues

        self.sameMean = sameMean

        self.rescaleClusterPeriods = rescaleClusterPeriods

        self.rescaleExcludeColumns = rescaleExcludeColumns or []

        self.weightDict = weightDict

        self.representationMethod = representationMethod

        self.representationDict = representationDict

        self.distributionPeriodWise = distributionPeriodWise

        self.segmentRepresentationMethod = segmentRepresentationMethod

        self.predefClusterOrder = predefClusterOrder

        self.predefClusterCenterIndices = predefClusterCenterIndices

        self.predefExtremeClusterIdx = predefExtremeClusterIdx

        self.predefSegmentOrder = predefSegmentOrder

        self.predefSegmentDurations = predefSegmentDurations

        self.predefSegmentCenters = predefSegmentCenters

        self.solver = solver

        self.numericalTolerance = numericalTolerance

        self.segmentation = segmentation

        self.roundOutput = roundOutput

        self.addPeakMin = addPeakMin

        self.addPeakMax = addPeakMax

        self.addMeanMin = addMeanMin

        self.addMeanMax = addMeanMax

        self._check_init_args()

        # internal attributes
        self._normalizedMean = None

        return

    def _check_init_args(self):
        # check timeSeries and set it as pandas DataFrame
        if not isinstance(self.timeSeries, pd.DataFrame):
            if isinstance(self.timeSeries, dict) or isinstance(
                self.timeSeries, np.ndarray
            ):
                self.timeSeries = pd.DataFrame(self.timeSeries)
            else:
                raise ValueError(
                    "timeSeries has to be of type pandas.DataFrame() "
                    + "or of type np.array() "
                    "in initialization of object of class " + type(self).__name__
                )

        # check if extreme periods exist in the dataframe
        for peak in self.addPeakMin:
            if peak not in self.timeSeries.columns:
                raise ValueError(
                    peak
                    + ' listed in "addPeakMin"'
                    + " does not occur as timeSeries column"
                )
        for peak in self.addPeakMax:
            if peak not in self.timeSeries.columns:
                raise ValueError(
                    peak
                    + ' listed in "addPeakMax"'
                    + " does not occur as timeSeries column"
                )
        for peak in self.addMeanMin:
            if peak not in self.timeSeries.columns:
                raise ValueError(
                    peak
                    + ' listed in "addMeanMin"'
                    + " does not occur as timeSeries column"
                )
        for peak in self.addMeanMax:
            if peak not in self.timeSeries.columns:
                raise ValueError(
                    peak
                    + ' listed in "addMeanMax"'
                    + " does not occur as timeSeries column"
                )

        # derive resolution from date time index if not provided
        if self.resolution is None:
            try:
                timedelta = self.timeSeries.index[1] - self.timeSeries.index[0]
                self.resolution = float(timedelta.total_seconds()) / 3600
            except AttributeError as exc:
                raise ValueError(
                    "'resolution' argument has to be nonnegative float or int"
                    + " or the given timeseries needs a datetime index"
                ) from exc
            except TypeError:
                try:
                    self.timeSeries.index = pd.to_datetime(self.timeSeries.index)
                    timedelta = self.timeSeries.index[1] - self.timeSeries.index[0]
                    self.resolution = float(timedelta.total_seconds()) / 3600
                except Exception as exc:
                    raise ValueError(
                        "'resolution' argument has to be nonnegative float or int"
                        + " or the given timeseries needs a datetime index"
                    ) from exc

        if not (isinstance(self.resolution, int) or isinstance(self.resolution, float)):
            raise ValueError("resolution has to be nonnegative float or int")

        # check hoursPerPeriod
        if self.hoursPerPeriod is None or self.hoursPerPeriod <= 0:
            raise ValueError("hoursPerPeriod has to be nonnegative float or int")

        # check typical Periods
        if (
            self.noTypicalPeriods is None
            or self.noTypicalPeriods <= 0
            or not isinstance(self.noTypicalPeriods, int)
        ):
            raise ValueError("noTypicalPeriods has to be nonnegative integer")
        self.timeStepsPerPeriod = int(self.hoursPerPeriod / self.resolution)
        if not self.timeStepsPerPeriod == self.hoursPerPeriod / self.resolution:
            raise ValueError(
                "The combination of hoursPerPeriod and the "
                + "resulution does not result in an integer "
                + "number of time steps per period"
            )
        if self.segmentation:
            if self.noSegments > self.timeStepsPerPeriod:
                warnings.warn(
                    "The number of segments must be less than or equal to the number of time steps per period. "
                    "Segment number is decreased to number of time steps per period."
                )
                self.noSegments = self.timeStepsPerPeriod

        # check clusterMethod
        if self.clusterMethod not in self.CLUSTER_METHODS:
            raise ValueError(
                "clusterMethod needs to be one of "
                + "the following: "
                + f"{self.CLUSTER_METHODS}"
            )

        # check representationMethod
        if (
            self.representationMethod is not None
            and self.representationMethod not in self.REPRESENTATION_METHODS
        ):
            raise ValueError(
                "If specified, representationMethod needs to be one of "
                + "the following: "
                + f"{self.REPRESENTATION_METHODS}"
            )

        # check representationMethod
        if self.segmentRepresentationMethod is None:
            self.segmentRepresentationMethod = self.representationMethod
        else:
            if self.segmentRepresentationMethod not in self.REPRESENTATION_METHODS:
                raise ValueError(
                    "If specified, segmentRepresentationMethod needs to be one of "
                    + "the following: "
                    + f"{self.REPRESENTATION_METHODS}"
                )

        # if representationDict None, represent by maximum time steps in each cluster
        if self.representationDict is None:
            self.representationDict = dict.fromkeys(
                list(self.timeSeries.columns), "mean"
            )
        # sort representationDict alphabetically to make sure that the min, max or mean function is applied to the right
        # column
        self.representationDict = (
            pd.Series(self.representationDict).sort_index(axis=0).to_dict()
        )

        # check extremePeriods
        if self.extremePeriodMethod not in self.EXTREME_PERIOD_METHODS:
            raise ValueError(
                "extremePeriodMethod needs to be one of "
                + "the following: "
                + f"{self.EXTREME_PERIOD_METHODS}"
            )

        # check evalSumPeriods
        if not isinstance(self.evalSumPeriods, bool):
            raise ValueError("evalSumPeriods has to be boolean")
        # check sortValues
        if not isinstance(self.sortValues, bool):
            raise ValueError("sortValues has to be boolean")
        # check sameMean
        if not isinstance(self.sameMean, bool):
            raise ValueError("sameMean has to be boolean")
        # check rescaleClusterPeriods
        if not isinstance(self.rescaleClusterPeriods, bool):
            raise ValueError("rescaleClusterPeriods has to be boolean")

        # check predefClusterOrder
        if self.predefClusterOrder is not None:
            if not isinstance(self.predefClusterOrder, (list, np.ndarray)):
                raise ValueError("predefClusterOrder has to be an array or list")
            if self.predefClusterCenterIndices is not None:
                # check predefClusterCenterIndices
                if not isinstance(self.predefClusterCenterIndices, (list, np.ndarray)):
                    raise ValueError(
                        "predefClusterCenterIndices has to be an array or list"
                    )
        elif self.predefClusterCenterIndices is not None:
            raise ValueError(
                'If "predefClusterCenterIndices" is defined, "predefClusterOrder" needs to be defined as well'
            )

        # check predefSegmentOrder
        if self.predefSegmentOrder is not None:
            if not isinstance(self.predefSegmentOrder, (list, tuple)):
                raise ValueError("predefSegmentOrder has to be a list or tuple")
            if self.predefSegmentDurations is None:
                raise ValueError(
                    'If "predefSegmentOrder" is defined, "predefSegmentDurations" '
                    "needs to be defined as well"
                )
            if not isinstance(self.predefSegmentDurations, (list, tuple)):
                raise ValueError("predefSegmentDurations has to be a list or tuple")
        elif self.predefSegmentDurations is not None:
            raise ValueError(
                'If "predefSegmentDurations" is defined, "predefSegmentOrder" '
                "needs to be defined as well"
            )

        if self.predefSegmentCenters is not None:
            if self.predefSegmentOrder is None:
                raise ValueError(
                    'If "predefSegmentCenters" is defined, "predefSegmentOrder" '
                    "needs to be defined as well"
                )
            if not isinstance(self.predefSegmentCenters, (list, tuple)):
                raise ValueError("predefSegmentCenters has to be a list or tuple")

        return

    def _normalizeTimeSeries(self, sameMean=False):
        """
        Normalizes each time series independently.

        :param sameMean: Decides if the time series should have all the same mean value.
            Relevant for weighting time series. optional (default: False)
        :type sameMean: boolean

        :returns: normalized time series
        """
        min_max_scaler = preprocessing.MinMaxScaler()
        normalizedTimeSeries = pd.DataFrame(
            min_max_scaler.fit_transform(self.timeSeries),
            columns=self.timeSeries.columns,
            index=self.timeSeries.index,
        )

        self._normalizedMean = normalizedTimeSeries.mean()
        if sameMean:
            normalizedTimeSeries /= self._normalizedMean

        return normalizedTimeSeries

    def _unnormalizeTimeSeries(self, normalizedTimeSeries, sameMean=False):
        """
        Equivalent to '_normalizeTimeSeries'. Just does the back
        transformation.

        :param normalizedTimeSeries: Time series which should get back transformated. required
        :type normalizedTimeSeries: pandas.DataFrame()

        :param sameMean: Has to have the same value as in _normalizeTimeSeries. optional (default: False)
        :type sameMean: boolean

        :returns: unnormalized time series
        """
        from sklearn import preprocessing

        min_max_scaler = preprocessing.MinMaxScaler()
        min_max_scaler.fit(self.timeSeries)

        if sameMean:
            normalizedTimeSeries *= self._normalizedMean

        unnormalizedTimeSeries = pd.DataFrame(
            min_max_scaler.inverse_transform(normalizedTimeSeries),
            columns=normalizedTimeSeries.columns,
            index=normalizedTimeSeries.index,
        )

        return unnormalizedTimeSeries

    def _preProcessTimeSeries(self):
        """
        Normalize the time series, weight them based on the weight dict and
        puts them into the correct matrix format.
        """
        # first sort the time series in order to avoid bug mention in #18
        self.timeSeries.sort_index(axis=1, inplace=True)

        # convert the dataframe to floats
        self.timeSeries = self.timeSeries.astype(float)

        # normalize the time series and group them to periodly profiles
        self.normalizedTimeSeries = self._normalizeTimeSeries(sameMean=self.sameMean)

        for column in self.weightDict:
            if self.weightDict[column] < MIN_WEIGHT:
                print(
                    'weight of "'
                    + str(column)
                    + '" set to the minmal tolerable weighting'
                )
                self.weightDict[column] = MIN_WEIGHT
            self.normalizedTimeSeries[column] = (
                self.normalizedTimeSeries[column] * self.weightDict[column]
            )

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", LegacyAPIWarning)
            self.normalizedPeriodlyProfiles, self.timeIndex = unstackToPeriods(
                self.normalizedTimeSeries, self.timeStepsPerPeriod
            )

        # check if no NaN is in the resulting profiles
        if self.normalizedPeriodlyProfiles.isnull().values.any():
            raise ValueError(
                "Pre processed data includes NaN. Please check the timeSeries input data."
            )

    def _postProcessTimeSeries(self, normalizedTimeSeries, applyWeighting=True):
        """
        Neutralizes the weighting the time series back and unnormalizes them.
        """
        if applyWeighting:
            for column in self.weightDict:
                normalizedTimeSeries[column] = (
                    normalizedTimeSeries[column] / self.weightDict[column]
                )

        unnormalizedTimeSeries = self._unnormalizeTimeSeries(
            normalizedTimeSeries, sameMean=self.sameMean
        )

        if self.roundOutput is not None:
            unnormalizedTimeSeries = unnormalizedTimeSeries.round(
                decimals=self.roundOutput
            )

        return unnormalizedTimeSeries

    def _addExtremePeriods(
        self,
        groupedSeries,
        clusterCenters,
        clusterOrder,
        extremePeriodMethod="new_cluster_center",
        addPeakMin=None,
        addPeakMax=None,
        addMeanMin=None,
        addMeanMax=None,
    ):
        """
        Adds different extreme periods based on the to the clustered data,
        decribed by the clusterCenters and clusterOrder.

        :param groupedSeries: periodly grouped groupedSeries on which basis it should be decided,
            which period is an extreme period. required
        :type groupedSeries: pandas.DataFrame()

        :param clusterCenters: Output from clustering with sklearn. required
        :type clusterCenters: dict

        :param clusterOrder: Output from clsutering with sklearn. required
        :type clusterOrder: dict

        :param extremePeriodMethod: Chosen extremePeriodMethod. The method. optional(default: 'new_cluster_center' )
        :type extremePeriodMethod: string

        :returns: - **newClusterCenters** -- The new cluster centers extended with the extreme periods.
                  - **newClusterOrder** -- The new cluster order including the extreme periods.
                  - **extremeClusterIdx** -- A list of indices where in the newClusterCenters are the extreme
                    periods located.
        """

        # init required dicts and lists
        self.extremePeriods = {}
        extremePeriodNo = []

        ccList = [center.tolist() for center in clusterCenters]

        # check which extreme periods exist in the profile and add them to
        # self.extremePeriods dict
        for column in self.timeSeries.columns:
            if column in addPeakMax:
                stepNo = groupedSeries[column].max(axis=1).idxmax()
                # add only if stepNo is not already in extremePeriods
                # if it is not already a cluster center
                if (
                    stepNo not in extremePeriodNo
                    and groupedSeries.loc[stepNo, :].values.tolist() not in ccList
                ):
                    max_col = self._append_col_with(column, " max.")
                    self.extremePeriods[max_col] = {
                        "stepNo": stepNo,
                        "profile": groupedSeries.loc[stepNo, :].values,
                        "column": column,
                    }
                    extremePeriodNo.append(stepNo)

            if column in addPeakMin:
                stepNo = groupedSeries[column].min(axis=1).idxmin()
                # add only if stepNo is not already in extremePeriods
                # if it is not already a cluster center
                if (
                    stepNo not in extremePeriodNo
                    and groupedSeries.loc[stepNo, :].values.tolist() not in ccList
                ):
                    min_col = self._append_col_with(column, " min.")
                    self.extremePeriods[min_col] = {
                        "stepNo": stepNo,
                        "profile": groupedSeries.loc[stepNo, :].values,
                        "column": column,
                    }
                    extremePeriodNo.append(stepNo)

            if column in addMeanMax:
                stepNo = groupedSeries[column].mean(axis=1).idxmax()
                # add only if stepNo is not already in extremePeriods
                # if it is not already a cluster center
                if (
                    stepNo not in extremePeriodNo
                    and groupedSeries.loc[stepNo, :].values.tolist() not in ccList
                ):
                    mean_max_col = self._append_col_with(column, " daily max.")
                    self.extremePeriods[mean_max_col] = {
                        "stepNo": stepNo,
                        "profile": groupedSeries.loc[stepNo, :].values,
                        "column": column,
                    }
                    extremePeriodNo.append(stepNo)

            if column in addMeanMin:
                stepNo = groupedSeries[column].mean(axis=1).idxmin()
                # add only if stepNo is not already in extremePeriods and
                # if it is not already a cluster center
                if (
                    stepNo not in extremePeriodNo
                    and groupedSeries.loc[stepNo, :].values.tolist() not in ccList
                ):
                    mean_min_col = self._append_col_with(column, " daily min.")
                    self.extremePeriods[mean_min_col] = {
                        "stepNo": stepNo,
                        "profile": groupedSeries.loc[stepNo, :].values,
                        "column": column,
                    }
                    extremePeriodNo.append(stepNo)

        for periodType in self.extremePeriods:
            # get current related clusters of extreme periods
            self.extremePeriods[periodType]["clusterNo"] = clusterOrder[
                self.extremePeriods[periodType]["stepNo"]
            ]

            # init new cluster structure
        newClusterCenters = []
        newClusterOrder = clusterOrder
        extremeClusterIdx = []

        # integrate extreme periods to clusters
        if extremePeriodMethod == "append":
            # attach extreme periods to cluster centers
            for i, cluster_center in enumerate(clusterCenters):
                newClusterCenters.append(cluster_center)
            for i, periodType in enumerate(self.extremePeriods):
                extremeClusterIdx.append(len(newClusterCenters))
                newClusterCenters.append(self.extremePeriods[periodType]["profile"])
                newClusterOrder[self.extremePeriods[periodType]["stepNo"]] = i + len(
                    clusterCenters
                )

        elif extremePeriodMethod == "new_cluster_center":
            for i, cluster_center in enumerate(clusterCenters):
                newClusterCenters.append(cluster_center)
            # attach extrem periods to cluster centers and consider for all periods
            # if the fit better to the cluster or the extrem period
            for i, periodType in enumerate(self.extremePeriods):
                extremeClusterIdx.append(len(newClusterCenters))
                newClusterCenters.append(self.extremePeriods[periodType]["profile"])
                self.extremePeriods[periodType]["newClusterNo"] = i + len(
                    clusterCenters
                )

            for i, cPeriod in enumerate(newClusterOrder):
                # caclulate euclidean distance to cluster center
                cluster_dist = sum(
                    (groupedSeries.iloc[i].values - clusterCenters[cPeriod]) ** 2
                )
                for ii, extremPeriodType in enumerate(self.extremePeriods):
                    # exclude other extreme periods from adding to the new
                    # cluster center
                    isOtherExtreme = False
                    for otherExPeriod in self.extremePeriods:
                        if (
                            i == self.extremePeriods[otherExPeriod]["stepNo"]
                            and otherExPeriod != extremPeriodType
                        ):
                            isOtherExtreme = True
                    # calculate distance to extreme periods
                    extperiod_dist = sum(
                        (
                            groupedSeries.iloc[i].values
                            - self.extremePeriods[extremPeriodType]["profile"]
                        )
                        ** 2
                    )
                    # choose new cluster relation
                    if extperiod_dist < cluster_dist and not isOtherExtreme:
                        newClusterOrder[i] = self.extremePeriods[extremPeriodType][
                            "newClusterNo"
                        ]

        elif extremePeriodMethod == "replace_cluster_center":
            # Worst Case Clusterperiods
            newClusterCenters = clusterCenters
            for periodType in self.extremePeriods:
                index = groupedSeries.columns.get_loc(
                    self.extremePeriods[periodType]["column"]
                )
                newClusterCenters[self.extremePeriods[periodType]["clusterNo"]][
                    index
                ] = self.extremePeriods[periodType]["profile"][index]
                if (
                    self.extremePeriods[periodType]["clusterNo"]
                    not in extremeClusterIdx
                ):
                    extremeClusterIdx.append(
                        self.extremePeriods[periodType]["clusterNo"]
                    )

        return newClusterCenters, newClusterOrder, extremeClusterIdx

    def _append_col_with(self, column, append_with=" max."):
        """Appends a string to the column name. For MultiIndexes, which turn out to be
        tuples when this method is called, only last level is changed"""
        if isinstance(column, str):
            return column + append_with
        elif isinstance(column, tuple):
            col = list(column)
            col[-1] = col[-1] + append_with
            return tuple(col)

    def _rescaleClusterPeriods(self, clusterOrder, clusterPeriods, extremeClusterIdx):
        """
        Rescale the values of the clustered Periods such that mean of each time
        series in the typical Periods fits the mean value of the original time
        series, without changing the values of the extremePeriods.
        """
        # Initialize dict to store rescaling deviations per column
        self._rescaleDeviations = {}

        weightingVec = pd.Series(self._clusterPeriodNoOccur).values
        columns = list(self.timeSeries.columns)
        n_clusters = len(self.clusterPeriods)
        n_cols = len(columns)
        n_timesteps = self.timeStepsPerPeriod

        # Convert to 3D numpy array for fast operations: (n_clusters, n_cols, n_timesteps)
        arr = np.array(self.clusterPeriods).reshape(n_clusters, n_cols, n_timesteps)

        # Indices for non-extreme clusters
        idx_wo_peak = np.delete(np.arange(n_clusters), extremeClusterIdx)
        extremeClusterIdx_arr = np.array(extremeClusterIdx, dtype=int)

        for ci, column in enumerate(columns):
            # Skip columns excluded from rescaling
            if column in self.rescaleExcludeColumns:
                continue

            col_data = arr[:, ci, :]  # (n_clusters, n_timesteps)
            sum_raw = self.normalizedPeriodlyProfiles[column].sum().sum()

            # Sum of extreme periods (weighted)
            if len(extremeClusterIdx_arr) > 0:
                sum_peak = np.sum(
                    weightingVec[extremeClusterIdx_arr]
                    * col_data[extremeClusterIdx_arr, :].sum(axis=1)
                )
            else:
                sum_peak = 0.0

            sum_clu_wo_peak = np.sum(
                weightingVec[idx_wo_peak] * col_data[idx_wo_peak, :].sum(axis=1)
            )

            # define the upper scale dependent on the weighting of the series
            scale_ub = 1.0
            if self.sameMean:
                scale_ub = (
                    scale_ub
                    * self.timeSeries[column].max()
                    / self.timeSeries[column].mean()
                )
            if column in self.weightDict:
                scale_ub = scale_ub * self.weightDict[column]

            # difference between predicted and original sum
            diff = abs(sum_raw - (sum_clu_wo_peak + sum_peak))

            # use while loop to rescale cluster periods
            a = 0
            while diff > sum_raw * TOLERANCE and a < MAX_ITERATOR:
                # rescale values (only non-extreme clusters)
                arr[idx_wo_peak, ci, :] *= (sum_raw - sum_peak) / sum_clu_wo_peak

                # reset values higher than the upper scale or less than zero
                arr[:, ci, :] = np.clip(arr[:, ci, :], 0, scale_ub)

                # Handle NaN (replace with 0)
                np.nan_to_num(arr[:, ci, :], copy=False, nan=0.0)

                # calc new sum and new diff to orig data
                col_data = arr[:, ci, :]
                sum_clu_wo_peak = np.sum(
                    weightingVec[idx_wo_peak] * col_data[idx_wo_peak, :].sum(axis=1)
                )
                diff = abs(sum_raw - (sum_clu_wo_peak + sum_peak))
                a += 1

            # Calculate and store final deviation
            deviation_pct = (diff / sum_raw) * 100 if sum_raw != 0 else 0.0
            converged = a < MAX_ITERATOR
            self._rescaleDeviations[column] = {
                "deviation_pct": deviation_pct,
                "converged": converged,
                "iterations": a,
            }

            if not converged and deviation_pct > 0.01:
                warnings.warn(
                    'Max iteration number reached for "'
                    + str(column)
                    + '" while rescaling the cluster periods.'
                    + " The integral of the aggregated time series deviates by: "
                    + str(round(deviation_pct, 2))
                    + "%"
                )

        # Reshape back to 2D: (n_clusters, n_cols * n_timesteps)
        return arr.reshape(n_clusters, -1)

    def _clusterSortedPeriods(
        self, candidates, n_init=20, n_clusters=None, delClusterParams=None
    ):
        """
        Runs the clustering algorithms for the sorted profiles within the period
        instead of the original profiles. (Duration curve clustering)
        """
        # Strip extra evaluation columns for representation
        repr_candidates = (
            candidates[:, :delClusterParams] if delClusterParams else candidates
        )

        # Vectorized sort: reshape to 3D (periods x columns x timesteps), sort, reshape back
        values = self.normalizedPeriodlyProfiles.values.copy()
        n_periods, n_total = values.shape
        n_cols = len(self.timeSeries.columns)
        n_timesteps = n_total // n_cols

        # Sort each period's timesteps descending for all columns at once
        # Use stable sort for deterministic tie-breaking across environments
        values_3d = values.reshape(n_periods, n_cols, n_timesteps)
        sortedClusterValues = (-np.sort(-values_3d, axis=2, kind="stable")).reshape(
            n_periods, -1
        )

        if n_clusters is None:
            n_clusters = self.noTypicalPeriods

        (
            _altClusterCenters,
            self.clusterCenterIndices,
            clusterOrders_C,
        ) = aggregatePeriods(
            sortedClusterValues,
            n_clusters=n_clusters,
            n_iter=30,
            solver=self.solver,
            clusterMethod=self.clusterMethod,
            representationMethod=self.representationMethod,
            representationDict=self.representationDict,
            distributionPeriodWise=self.distributionPeriodWise,
            timeStepsPerPeriod=self.timeStepsPerPeriod,
        )

        clusterCenters_C = []

        # take the clusters and determine the most representative sorted
        # period as cluster center
        for clusterNum in np.unique(clusterOrders_C):
            indice = np.where(clusterOrders_C == clusterNum)[0]
            if len(indice) > 1:
                # mean value for each time step for each time series over
                # all Periods in the cluster
                currentMean_C = sortedClusterValues[indice].mean(axis=0)
                # index of the period with the lowest distance to the cluster
                # center
                mindistIdx_C = np.argmin(
                    np.square(sortedClusterValues[indice] - currentMean_C).sum(axis=1)
                )
                # append original time series of this period (without extra eval columns)
                medoid_C = repr_candidates[indice][mindistIdx_C]

                # append to cluster center
                clusterCenters_C.append(medoid_C)

            else:
                # if only on period is part of the cluster, add this index
                clusterCenters_C.append(repr_candidates[indice][0])

        return clusterCenters_C, clusterOrders_C

    def createTypicalPeriods(self):
        """
        Clusters the Periods.

        :returns: **self.typicalPeriods** --  All typical Periods in scaled form.
        """
        self._preProcessTimeSeries()

        # Compute effective number of clusters for the clustering algorithm
        effective_n_clusters = self.noTypicalPeriods

        # check for additional cluster parameters
        if self.evalSumPeriods:
            evaluationValues = (
                self.normalizedPeriodlyProfiles.stack(future_stack=True, level=0)
                .sum(axis=1)
                .unstack(level=1)
            )
            # how many values have to get deleted later
            delClusterParams = -len(evaluationValues.columns)
            candidates = np.concatenate(
                (self.normalizedPeriodlyProfiles.values, evaluationValues.values),
                axis=1,
            )
        else:
            delClusterParams = None
            candidates = self.normalizedPeriodlyProfiles.values

        # skip aggregation procedure for the case of a predefined cluster sequence and get only the correct representation
        if self.predefClusterOrder is not None:
            self._clusterOrder = self.predefClusterOrder
            # check if representatives are defined
            if self.predefClusterCenterIndices is not None:
                self.clusterCenterIndices = self.predefClusterCenterIndices
                repr_candidates = (
                    candidates[:, :delClusterParams] if delClusterParams else candidates
                )
                self.clusterCenters = repr_candidates[self.predefClusterCenterIndices]
            else:
                # otherwise take the medoids (strip extra eval columns)
                repr_candidates = (
                    candidates[:, :delClusterParams] if delClusterParams else candidates
                )
                self.clusterCenters, self.clusterCenterIndices = representations(
                    repr_candidates,
                    self._clusterOrder,
                    default="medoidRepresentation",
                    representationMethod=self.representationMethod,
                    representationDict=self.representationDict,
                    timeStepsPerPeriod=self.timeStepsPerPeriod,
                )
        else:
            cluster_duration = time.time()
            if not self.sortValues:
                # cluster the data
                (
                    self.clusterCenters,
                    self.clusterCenterIndices,
                    self._clusterOrder,
                ) = aggregatePeriods(
                    candidates,
                    n_clusters=effective_n_clusters,
                    n_iter=100,
                    solver=self.solver,
                    clusterMethod=self.clusterMethod,
                    representationMethod=self.representationMethod,
                    representationDict=self.representationDict,
                    distributionPeriodWise=self.distributionPeriodWise,
                    timeStepsPerPeriod=self.timeStepsPerPeriod,
                    n_extra_columns=-delClusterParams if delClusterParams else 0,
                )
            else:
                self.clusterCenters, self._clusterOrder = self._clusterSortedPeriods(
                    candidates,
                    n_clusters=effective_n_clusters,
                    delClusterParams=delClusterParams,
                )
            self.clusteringDuration = time.time() - cluster_duration

        # All paths now produce cluster centers without extra evaluation columns,
        # so no stripping is needed.
        self.clusterPeriods = list(self.clusterCenters)

        if not self.extremePeriodMethod == "None":
            (
                self.clusterPeriods,
                self._clusterOrder,
                self.extremeClusterIdx,
            ) = self._addExtremePeriods(
                self.normalizedPeriodlyProfiles,
                self.clusterPeriods,
                self._clusterOrder,
                extremePeriodMethod=self.extremePeriodMethod,
                addPeakMin=self.addPeakMin,
                addPeakMax=self.addPeakMax,
                addMeanMin=self.addMeanMin,
                addMeanMax=self.addMeanMax,
            )
        else:
            # Use predefined extreme cluster indices if provided (for transfer/apply)
            if self.predefExtremeClusterIdx is not None:
                self.extremeClusterIdx = list(self.predefExtremeClusterIdx)
            else:
                self.extremeClusterIdx = []

        # get number of appearance of the the typical periods
        nums, counts = np.unique(self._clusterOrder, return_counts=True)
        self._clusterPeriodNoOccur = {num: counts[ii] for ii, num in enumerate(nums)}

        if self.rescaleClusterPeriods:
            self.clusterPeriods = self._rescaleClusterPeriods(
                self._clusterOrder, self.clusterPeriods, self.extremeClusterIdx
            )

        # if additional time steps have been added, reduce the number of occurrence of the typical period
        # which is related to these time steps
        if not len(self.timeSeries) % self.timeStepsPerPeriod == 0:
            self._clusterPeriodNoOccur[self._clusterOrder[-1]] -= (
                1
                - float(len(self.timeSeries) % self.timeStepsPerPeriod)
                / self.timeStepsPerPeriod
            )

        # put the clustered data in pandas format and scale back
        self.normalizedTypicalPeriods = (
            pd.concat(
                [
                    pd.Series(s, index=self.normalizedPeriodlyProfiles.columns)
                    for s in self.clusterPeriods
                ],
                axis=1,
            )
            .unstack("TimeStep")
            .T
        )

        if self.segmentation:
            from tsam.utils.segmentation import segmentation

            (
                self.segmentedNormalizedTypicalPeriods,
                self.predictedSegmentedNormalizedTypicalPeriods,
                self.segmentCenterIndices,
            ) = segmentation(
                self.normalizedTypicalPeriods,
                self.noSegments,
                self.timeStepsPerPeriod,
                representationMethod=self.segmentRepresentationMethod,
                representationDict=self.representationDict,
                distributionPeriodWise=self.distributionPeriodWise,
                predefSegmentOrder=self.predefSegmentOrder,
                predefSegmentDurations=self.predefSegmentDurations,
                predefSegmentCenters=self.predefSegmentCenters,
            )
            self.normalizedTypicalPeriods = (
                self.segmentedNormalizedTypicalPeriods.reset_index(level=3, drop=True)
            )

        self.typicalPeriods = self._postProcessTimeSeries(self.normalizedTypicalPeriods)

        # check if original time series boundaries are not exceeded
        exceeds_max = self.typicalPeriods.max(axis=0) > self.timeSeries.max(axis=0)
        if exceeds_max.any():
            diff = self.typicalPeriods.max(axis=0) - self.timeSeries.max(axis=0)
            exceeding_diff = diff[exceeds_max]
            if exceeding_diff.max() > self.numericalTolerance:
                warnings.warn(
                    "At least one maximal value of the "
                    + "aggregated time series exceeds the maximal value "
                    + "the input time series for: "
                    + f"{exceeding_diff.to_dict()}"
                    + ". To silence the warning set the 'numericalTolerance' to a higher value."
                )
        below_min = self.typicalPeriods.min(axis=0) < self.timeSeries.min(axis=0)
        if below_min.any():
            diff = self.timeSeries.min(axis=0) - self.typicalPeriods.min(axis=0)
            exceeding_diff = diff[below_min]
            if exceeding_diff.max() > self.numericalTolerance:
                warnings.warn(
                    "Something went wrong... At least one minimal value of the "
                    + "aggregated time series exceeds the minimal value "
                    + "the input time series for: "
                    + f"{exceeding_diff.to_dict()}"
                    + ". To silence the warning set the 'numericalTolerance' to a higher value."
                )
        return self.typicalPeriods

    def prepareEnersysInput(self):
        """
        Creates all dictionaries and lists which are required for the energy system
        optimization input.
        """
        warnings.warn(
            '"prepareEnersysInput" is deprecated, since the created attributes can be directly accessed as properties',
            DeprecationWarning,
        )
        return

    @property
    def stepIdx(self):
        """
        Index inside a single cluster
        """
        if self.segmentation:
            return [ix for ix in range(0, self.noSegments)]
        else:
            return [ix for ix in range(0, self.timeStepsPerPeriod)]

    @property
    def clusterPeriodIdx(self):
        """
        Index of the clustered periods
        """
        if not hasattr(self, "clusterOrder"):
            self.createTypicalPeriods()
        return np.sort(np.unique(self._clusterOrder))

    @property
    def clusterOrder(self):
        """
        The sequence/order of the typical period to represent
        the original time series
        """
        if not hasattr(self, "_clusterOrder"):
            self.createTypicalPeriods()
        return self._clusterOrder

    @property
    def clusterPeriodNoOccur(self):
        """
        How often does a typical period occur in the original time series
        """
        if not hasattr(self, "clusterOrder"):
            self.createTypicalPeriods()
        return self._clusterPeriodNoOccur

    @property
    def clusterPeriodDict(self):
        """
        Time series data for each period index as dictionary
        """
        if not hasattr(self, "_clusterOrder"):
            self.createTypicalPeriods()
        if not hasattr(self, "_clusterPeriodDict"):
            self._clusterPeriodDict = {}
            for column in self.typicalPeriods:
                self._clusterPeriodDict[column] = self.typicalPeriods[column].to_dict()
        return self._clusterPeriodDict

    @property
    def segmentDurationDict(self):
        """
        Segment duration in time steps for each period index as dictionary
        """
        if not hasattr(self, "_clusterOrder"):
            self.createTypicalPeriods()
        if not hasattr(self, "_segmentDurationDict"):
            if self.segmentation:
                self._segmentDurationDict = (
                    self.segmentedNormalizedTypicalPeriods.drop(
                        self.segmentedNormalizedTypicalPeriods.columns, axis=1
                    )
                    .reset_index(level=3, drop=True)
                    .reset_index(2)
                    .to_dict()
                )
            else:
                self._segmentDurationDict = self.typicalPeriods.drop(
                    self.typicalPeriods.columns, axis=1
                )
                self._segmentDurationDict["Segment Duration"] = 1
                self._segmentDurationDict = self._segmentDurationDict.to_dict()
                warnings.warn(
                    "Segmentation is turned off. All segments are consistent the time steps."
                )
        return self._segmentDurationDict

    def predictOriginalData(self):
        """
        Predicts the overall time series if every period would be placed in the
        related cluster center

        :returns: **predictedData** (pandas.DataFrame) -- DataFrame which has the same shape as the original one.
        """
        if not hasattr(self, "_clusterOrder"):
            self.createTypicalPeriods()

        # Select typical periods source based on segmentation
        if self.segmentation:
            typical = self.predictedSegmentedNormalizedTypicalPeriods
        else:
            typical = self.normalizedTypicalPeriods

        from tsam.config import _expand_periods

        clustered_data_df = _expand_periods(typical, tuple(self._clusterOrder))

        # back in form
        self.normalizedPredictedData = pd.DataFrame(
            clustered_data_df.values[: len(self.timeSeries)],
            index=self.timeSeries.index,
            columns=self.timeSeries.columns,
        )
        # For the non-segmentation path, normalizedTypicalPeriods was already
        # unweighted and sameMean-reversed in-place by createTypicalPeriods →
        # _postProcessTimeSeries. We must undo the sameMean in-place change
        # so _unnormalizeTimeSeries can re-apply it during inverse transform.
        #
        # For the segmentation path, predictedSegmentedNormalizedTypicalPeriods
        # was NOT modified in-place, so it still carries weights and sameMean.
        # We pass applyWeighting=True so _postProcessTimeSeries removes them.
        if self.segmentation:
            self.predictedData = self._postProcessTimeSeries(
                self.normalizedPredictedData, applyWeighting=True
            )
        else:
            if self.sameMean:
                self.normalizedPredictedData /= self._normalizedMean
            self.predictedData = self._postProcessTimeSeries(
                self.normalizedPredictedData, applyWeighting=False
            )

        return self.predictedData

    def indexMatching(self):
        """
        Relates the index of the original time series with the indices
        represented by the clusters

        :returns: **timeStepMatching** (pandas.DataFrame) -- DataFrame which has the same shape as the original one.
        """
        if not hasattr(self, "_clusterOrder"):
            self.createTypicalPeriods()

        # create aggregated period and time step index lists
        periodIndex = []
        stepIndex = []
        for label in self._clusterOrder:
            for step in range(self.timeStepsPerPeriod):
                periodIndex.append(label)
                stepIndex.append(step)

        # create a dataframe
        timeStepMatching = pd.DataFrame(
            [periodIndex, stepIndex],
            index=["PeriodNum", "TimeStep"],
            columns=self.timeIndex,
        ).T

        # if segmentation is chosen, append another column stating which
        if self.segmentation:
            segmentIndex = []
            for label in self._clusterOrder:
                segmentIndex.extend(
                    np.repeat(
                        self.segmentedNormalizedTypicalPeriods.loc[
                            label, :
                        ].index.get_level_values(0),
                        self.segmentedNormalizedTypicalPeriods.loc[
                            label, :
                        ].index.get_level_values(1),
                    ).values
                )
            timeStepMatching = pd.DataFrame(
                [periodIndex, stepIndex, segmentIndex],
                index=["PeriodNum", "TimeStep", "SegmentIndex"],
                columns=self.timeIndex,
            ).T

        return timeStepMatching

    def accuracyIndicators(self):
        """
        Compares the predicted data with the original time series.

        :returns: **pd.DataFrame(indicatorRaw)** (pandas.DataFrame) -- Dataframe containing indicators evaluating the
                    accuracy of the
                    aggregation
        """
        if not hasattr(self, "predictedData"):
            self.predictOriginalData()

        indicatorRaw = {
            "RMSE": {},
            "RMSE_duration": {},
            "MAE": {},
        }  # 'Silhouette score':{},

        for column in self.normalizedTimeSeries.columns:
            if self.weightDict:
                origTS = self.normalizedTimeSeries[column] / self.weightDict.get(
                    column, 1
                )
            else:
                origTS = self.normalizedTimeSeries[column]

            predTS = self.normalizedPredictedData[column]
            indicatorRaw["RMSE"][column] = np.sqrt(mean_squared_error(origTS, predTS))
            indicatorRaw["RMSE_duration"][column] = np.sqrt(
                mean_squared_error(
                    origTS.sort_values(ascending=False).reset_index(drop=True),
                    predTS.sort_values(ascending=False).reset_index(drop=True),
                )
            )
            indicatorRaw["MAE"][column] = mean_absolute_error(origTS, predTS)

        return pd.DataFrame(indicatorRaw)

    def totalAccuracyIndicators(self):
        """
        Derives the accuracy indicators over all time series
        """
        return np.sqrt(
            self.accuracyIndicators().pow(2).sum()
            / len(self.normalizedTimeSeries.columns)
        )

stepIdx property

stepIdx

Index inside a single cluster

clusterPeriodIdx property

clusterPeriodIdx

Index of the clustered periods

clusterOrder property

clusterOrder

The sequence/order of the typical period to represent the original time series

clusterPeriodNoOccur property

clusterPeriodNoOccur

How often does a typical period occur in the original time series

clusterPeriodDict property

clusterPeriodDict

Time series data for each period index as dictionary

segmentDurationDict property

segmentDurationDict

Segment duration in time steps for each period index as dictionary

__init__

__init__(
    timeSeries,
    resolution=None,
    noTypicalPeriods=10,
    noSegments=10,
    hoursPerPeriod=24,
    clusterMethod="hierarchical",
    evalSumPeriods=False,
    sortValues=False,
    sameMean=False,
    rescaleClusterPeriods=True,
    rescaleExcludeColumns=None,
    weightDict=None,
    segmentation=False,
    extremePeriodMethod="None",
    representationMethod=None,
    representationDict=None,
    distributionPeriodWise=True,
    segmentRepresentationMethod=None,
    predefClusterOrder=None,
    predefClusterCenterIndices=None,
    predefExtremeClusterIdx=None,
    predefSegmentOrder=None,
    predefSegmentDurations=None,
    predefSegmentCenters=None,
    solver="highs",
    numericalTolerance=1e-13,
    roundOutput=None,
    addPeakMin=None,
    addPeakMax=None,
    addMeanMin=None,
    addMeanMax=None,
)

Initialize the periodly clusters.

Parameters:

Name Type Description Default
timeSeries DataFrame() | dict

DataFrame with the datetime as index and the relevant time series parameters as columns. required

required
resolution float

Resolution of the time series in hours [h]. If timeSeries is a pandas.DataFrame() the resolution is derived from the datetime index. optional, default: delta_T in timeSeries

None
hoursPerPeriod integer

Value which defines the length of a cluster period. optional, default: 24

24
noTypicalPeriods integer

Number of typical Periods - equivalent to the number of clusters. optional, default: 10

10
noSegments integer

Number of segments in which the typical periods shoul be subdivided - equivalent to the number of inner-period clusters. optional, default: 10

10
clusterMethod string

Chosen clustering method. optional, default: 'hierarchical' |br| Options are:

  • 'averaging'
  • 'k_means'
  • 'k_medoids'
  • 'k_maxoids'
  • 'hierarchical'
  • 'adjacent_periods'
'hierarchical'
evalSumPeriods boolean

Boolean if in the clustering process also the averaged periodly values shall be integrated additional to the periodly profiles as parameters. optional, default: False

False
sameMean boolean

Boolean which is used in the normalization procedure. If true, all time series get normalized such that they have the same mean value. optional, default: False

False
sortValues boolean

Boolean if the clustering should be done by the periodly duration curves (true) or the original shape of the data. optional (default: False)

False
rescaleClusterPeriods boolean

Decides if the cluster Periods shall get rescaled such that their weighted mean value fits the mean value of the original time series. optional (default: True)

True
weightDict dict

Dictionary which weights the profiles. It is done by scaling the time series while the normalization process. Normally all time series have a scale from 0 to 1. By scaling them, the values get different distances to each other and with this, they are differently evaluated while the clustering process. optional (default: None )

None
segmentation boolean

Boolean if time steps in periods should be aggregated to segments. optional (default: False)

False
extremePeriodMethod string

Method how to integrate extreme Periods (peak demand, lowest temperature etc.) into to the typical period profiles. optional, default: 'None' |br| Options are:

  • None: No integration at all.
  • 'append': append typical Periods to cluster centers
  • 'new_cluster_center': add the extreme period as additional cluster center. It is checked then for all Periods if they fit better to the this new center or their original cluster center.
  • 'replace_cluster_center': replaces the cluster center of the cluster where the extreme period belongs to with the periodly profile of the extreme period. (Worst case system design)
'None'
representationMethod string

Chosen representation. If specified, the clusters are represented in the chosen way. Otherwise, each clusterMethod has its own commonly used default representation method. |br| Options are:

  • 'meanRepresentation' (default of 'averaging' and 'k_means')
  • 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
  • 'minmaxmeanRepresentation'
  • 'durationRepresentation'/ 'distributionRepresentation'
  • 'distribtionAndMinMaxRepresentation'
None
representationDict dict

Dictionary which states for each attribute whether the profiles in each cluster should be represented by the minimum value or maximum value of each time step. This enables estimations to the safe side. This dictionary is needed when 'minmaxmeanRepresentation' is chosen. If not specified, the dictionary is set to containing 'mean' values only.

None
distributionPeriodWise

If durationRepresentation is chosen, you can choose whether the distribution of each cluster should be separately preserved or that of the original time series only (default: True)

True
segmentRepresentationMethod string

Chosen representation for the segments. If specified, the segments are represented in the chosen way. Otherwise, it is inherited from the representationMethod. |br| Options are:

  • 'meanRepresentation' (default of 'averaging' and 'k_means')
  • 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
  • 'minmaxmeanRepresentation'
  • 'durationRepresentation'/ 'distributionRepresentation'
  • 'distribtionAndMinMaxRepresentation'
None
predefClusterOrder list | array

Instead of aggregating a time series, a predefined grouping is taken which is given by this list. optional (default: None)

None
predefClusterCenterIndices list | array

If predefClusterOrder is give, this list can define the representative cluster candidates. Otherwise the medoid is taken. optional (default: None)

None
solver string

Solver that is used for k_medoids clustering. optional (default: 'cbc' )

'highs'
numericalTolerance float

Tolerance for numerical issues. Silences the warning for exceeding upper or lower bounds of the time series. optional (default: 1e-13 )

1e-13
roundOutput integer

Decimals to what the output time series get round. optional (default: None )

None
addPeakMin list

List of column names which's minimal value shall be added to the typical periods. E.g.: ['Temperature']. optional, default: []

None
addPeakMax list

List of column names which's maximal value shall be added to the typical periods. E.g. ['EDemand', 'HDemand']. optional, default: []

None
addMeanMin list

List of column names where the period with the cumulative minimal value shall be added to the typical periods. E.g. ['Photovoltaic']. optional, default: []

None
addMeanMax list

List of column names where the period with the cumulative maximal value shall be added to the typical periods. optional, default: []

None
Source code in src/tsam/timeseriesaggregation.py
def __init__(
    self,
    timeSeries,
    resolution=None,
    noTypicalPeriods=10,
    noSegments=10,
    hoursPerPeriod=24,
    clusterMethod="hierarchical",
    evalSumPeriods=False,
    sortValues=False,
    sameMean=False,
    rescaleClusterPeriods=True,
    rescaleExcludeColumns=None,
    weightDict=None,
    segmentation=False,
    extremePeriodMethod="None",
    representationMethod=None,
    representationDict=None,
    distributionPeriodWise=True,
    segmentRepresentationMethod=None,
    predefClusterOrder=None,
    predefClusterCenterIndices=None,
    predefExtremeClusterIdx=None,
    predefSegmentOrder=None,
    predefSegmentDurations=None,
    predefSegmentCenters=None,
    solver="highs",
    numericalTolerance=1e-13,
    roundOutput=None,
    addPeakMin=None,
    addPeakMax=None,
    addMeanMin=None,
    addMeanMax=None,
):
    """
    Initialize the periodly clusters.

    :param timeSeries: DataFrame with the datetime as index and the relevant
        time series parameters as columns. required
    :type timeSeries: pandas.DataFrame() or dict

    :param resolution: Resolution of the time series in hours [h]. If timeSeries is a
        pandas.DataFrame() the resolution is derived from the datetime
        index. optional, default: delta_T in timeSeries
    :type resolution: float

    :param hoursPerPeriod: Value which defines the length of a cluster period. optional, default: 24
    :type hoursPerPeriod: integer

    :param noTypicalPeriods: Number of typical Periods - equivalent to the number of clusters. optional, default: 10
    :type noTypicalPeriods: integer

    :param noSegments: Number of segments in which the typical periods shoul be subdivided - equivalent to the
        number of inner-period clusters. optional, default: 10
    :type noSegments: integer

    :param clusterMethod: Chosen clustering method. optional, default: 'hierarchical'
        |br| Options are:

        * 'averaging'
        * 'k_means'
        * 'k_medoids'
        * 'k_maxoids'
        * 'hierarchical'
        * 'adjacent_periods'
    :type clusterMethod: string

    :param evalSumPeriods: Boolean if in the clustering process also the averaged periodly values
        shall be integrated additional to the periodly profiles as parameters. optional, default: False
    :type evalSumPeriods: boolean

    :param sameMean: Boolean which is used in the normalization procedure. If true, all time series get normalized
        such that they have the same mean value. optional, default: False
    :type sameMean: boolean

    :param sortValues: Boolean if the clustering should be done by the periodly duration
        curves (true) or the original shape of the data. optional (default: False)
    :type sortValues: boolean

    :param rescaleClusterPeriods: Decides if the cluster Periods shall get rescaled such that their
        weighted mean value fits the mean value of the original time series. optional (default: True)
    :type rescaleClusterPeriods: boolean

    :param weightDict: Dictionary which weights the profiles. It is done by scaling
        the time series while the normalization process. Normally all time
        series have a scale from 0 to 1. By scaling them, the values get
        different distances to each other and with this, they are
        differently evaluated while the clustering process. optional (default: None )
    :type weightDict: dict

    :param segmentation: Boolean if time steps in periods should be aggregated to segments. optional (default: False)
    :type segmentation: boolean

    :param extremePeriodMethod: Method how to integrate extreme Periods (peak demand, lowest temperature etc.)
        into to the typical period profiles. optional, default: 'None'
        |br| Options are:

        * None: No integration at all.
        * 'append': append typical Periods to cluster centers
        * 'new_cluster_center': add the extreme period as additional cluster center. It is checked then for all
          Periods if they fit better to the this new center or their original cluster center.
        * 'replace_cluster_center': replaces the cluster center of the
          cluster where the extreme period belongs to with the periodly profile of the extreme period. (Worst
          case system design)
    :type extremePeriodMethod: string

    :param representationMethod: Chosen representation. If specified, the clusters are represented in the chosen
        way. Otherwise, each clusterMethod has its own commonly used default representation method.
        |br| Options are:

        * 'meanRepresentation' (default of 'averaging' and 'k_means')
        * 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
        * 'minmaxmeanRepresentation'
        * 'durationRepresentation'/ 'distributionRepresentation'
        * 'distribtionAndMinMaxRepresentation'
    :type representationMethod: string

    :param representationDict: Dictionary which states for each attribute whether the profiles in each cluster
        should be represented by the minimum value or maximum value of each time step. This enables estimations
        to the safe side. This dictionary is needed when 'minmaxmeanRepresentation' is chosen. If not specified, the
        dictionary is set to containing 'mean' values only.
    :type representationDict: dict

    :param distributionPeriodWise: If durationRepresentation is chosen, you can choose whether the distribution of
        each cluster should be separately preserved or that of the original time series only (default: True)
    :type distributionPeriodWise:

    :param segmentRepresentationMethod: Chosen representation for the segments. If specified, the segments are
        represented in the chosen way. Otherwise, it is inherited from the representationMethod.
        |br| Options are:

        * 'meanRepresentation' (default of 'averaging' and 'k_means')
        * 'medoidRepresentation' (default of 'k_medoids', 'hierarchical' and 'adjacent_periods')
        * 'minmaxmeanRepresentation'
        * 'durationRepresentation'/ 'distributionRepresentation'
        * 'distribtionAndMinMaxRepresentation'
    :type segmentRepresentationMethod: string

    :param predefClusterOrder: Instead of aggregating a time series, a predefined grouping is taken
        which is given by this list. optional (default: None)
    :type predefClusterOrder: list or array

    :param predefClusterCenterIndices: If predefClusterOrder is give, this list can define the representative
        cluster candidates. Otherwise the medoid is taken. optional (default: None)
    :type predefClusterCenterIndices: list or array

    :param solver: Solver that is used for k_medoids clustering. optional (default: 'cbc' )
    :type solver: string

    :param numericalTolerance: Tolerance for numerical issues. Silences the warning for exceeding upper or lower bounds
        of the time series. optional (default: 1e-13 )
    :type numericalTolerance: float

    :param roundOutput: Decimals to what the output time series get round. optional (default: None )
    :type roundOutput: integer

    :param addPeakMin: List of column names which's minimal value shall be added to the
        typical periods. E.g.: ['Temperature']. optional, default: []
    :type addPeakMin: list

    :param addPeakMax: List of column names which's maximal value shall be added to the
        typical periods. E.g. ['EDemand', 'HDemand']. optional, default: []
    :type addPeakMax: list

    :param addMeanMin: List of column names where the period with the cumulative minimal value
        shall be added to the typical periods. E.g. ['Photovoltaic']. optional, default: []
    :type addMeanMin: list

    :param addMeanMax: List of column names where the period with the cumulative maximal value
        shall be added to the typical periods. optional, default: []
    :type addMeanMax: list
    """
    warnings.warn(
        "TimeSeriesAggregation will be removed in tsam v4.0. "
        "Use tsam.aggregate() instead. See the migration guide in the documentation.",
        LegacyAPIWarning,
        stacklevel=2,
    )
    if addMeanMin is None:
        addMeanMin = []
    if addMeanMax is None:
        addMeanMax = []
    if addPeakMax is None:
        addPeakMax = []
    if addPeakMin is None:
        addPeakMin = []
    if weightDict is None:
        weightDict = {}
    self.timeSeries = timeSeries

    self.resolution = resolution

    self.hoursPerPeriod = hoursPerPeriod

    self.noTypicalPeriods = noTypicalPeriods

    self.noSegments = noSegments

    self.clusterMethod = clusterMethod

    self.extremePeriodMethod = extremePeriodMethod

    self.evalSumPeriods = evalSumPeriods

    self.sortValues = sortValues

    self.sameMean = sameMean

    self.rescaleClusterPeriods = rescaleClusterPeriods

    self.rescaleExcludeColumns = rescaleExcludeColumns or []

    self.weightDict = weightDict

    self.representationMethod = representationMethod

    self.representationDict = representationDict

    self.distributionPeriodWise = distributionPeriodWise

    self.segmentRepresentationMethod = segmentRepresentationMethod

    self.predefClusterOrder = predefClusterOrder

    self.predefClusterCenterIndices = predefClusterCenterIndices

    self.predefExtremeClusterIdx = predefExtremeClusterIdx

    self.predefSegmentOrder = predefSegmentOrder

    self.predefSegmentDurations = predefSegmentDurations

    self.predefSegmentCenters = predefSegmentCenters

    self.solver = solver

    self.numericalTolerance = numericalTolerance

    self.segmentation = segmentation

    self.roundOutput = roundOutput

    self.addPeakMin = addPeakMin

    self.addPeakMax = addPeakMax

    self.addMeanMin = addMeanMin

    self.addMeanMax = addMeanMax

    self._check_init_args()

    # internal attributes
    self._normalizedMean = None

    return

createTypicalPeriods

createTypicalPeriods()

Clusters the Periods.

:returns: self.typicalPeriods -- All typical Periods in scaled form.

Source code in src/tsam/timeseriesaggregation.py
def createTypicalPeriods(self):
    """
    Clusters the Periods.

    :returns: **self.typicalPeriods** --  All typical Periods in scaled form.
    """
    self._preProcessTimeSeries()

    # Compute effective number of clusters for the clustering algorithm
    effective_n_clusters = self.noTypicalPeriods

    # check for additional cluster parameters
    if self.evalSumPeriods:
        evaluationValues = (
            self.normalizedPeriodlyProfiles.stack(future_stack=True, level=0)
            .sum(axis=1)
            .unstack(level=1)
        )
        # how many values have to get deleted later
        delClusterParams = -len(evaluationValues.columns)
        candidates = np.concatenate(
            (self.normalizedPeriodlyProfiles.values, evaluationValues.values),
            axis=1,
        )
    else:
        delClusterParams = None
        candidates = self.normalizedPeriodlyProfiles.values

    # skip aggregation procedure for the case of a predefined cluster sequence and get only the correct representation
    if self.predefClusterOrder is not None:
        self._clusterOrder = self.predefClusterOrder
        # check if representatives are defined
        if self.predefClusterCenterIndices is not None:
            self.clusterCenterIndices = self.predefClusterCenterIndices
            repr_candidates = (
                candidates[:, :delClusterParams] if delClusterParams else candidates
            )
            self.clusterCenters = repr_candidates[self.predefClusterCenterIndices]
        else:
            # otherwise take the medoids (strip extra eval columns)
            repr_candidates = (
                candidates[:, :delClusterParams] if delClusterParams else candidates
            )
            self.clusterCenters, self.clusterCenterIndices = representations(
                repr_candidates,
                self._clusterOrder,
                default="medoidRepresentation",
                representationMethod=self.representationMethod,
                representationDict=self.representationDict,
                timeStepsPerPeriod=self.timeStepsPerPeriod,
            )
    else:
        cluster_duration = time.time()
        if not self.sortValues:
            # cluster the data
            (
                self.clusterCenters,
                self.clusterCenterIndices,
                self._clusterOrder,
            ) = aggregatePeriods(
                candidates,
                n_clusters=effective_n_clusters,
                n_iter=100,
                solver=self.solver,
                clusterMethod=self.clusterMethod,
                representationMethod=self.representationMethod,
                representationDict=self.representationDict,
                distributionPeriodWise=self.distributionPeriodWise,
                timeStepsPerPeriod=self.timeStepsPerPeriod,
                n_extra_columns=-delClusterParams if delClusterParams else 0,
            )
        else:
            self.clusterCenters, self._clusterOrder = self._clusterSortedPeriods(
                candidates,
                n_clusters=effective_n_clusters,
                delClusterParams=delClusterParams,
            )
        self.clusteringDuration = time.time() - cluster_duration

    # All paths now produce cluster centers without extra evaluation columns,
    # so no stripping is needed.
    self.clusterPeriods = list(self.clusterCenters)

    if not self.extremePeriodMethod == "None":
        (
            self.clusterPeriods,
            self._clusterOrder,
            self.extremeClusterIdx,
        ) = self._addExtremePeriods(
            self.normalizedPeriodlyProfiles,
            self.clusterPeriods,
            self._clusterOrder,
            extremePeriodMethod=self.extremePeriodMethod,
            addPeakMin=self.addPeakMin,
            addPeakMax=self.addPeakMax,
            addMeanMin=self.addMeanMin,
            addMeanMax=self.addMeanMax,
        )
    else:
        # Use predefined extreme cluster indices if provided (for transfer/apply)
        if self.predefExtremeClusterIdx is not None:
            self.extremeClusterIdx = list(self.predefExtremeClusterIdx)
        else:
            self.extremeClusterIdx = []

    # get number of appearance of the the typical periods
    nums, counts = np.unique(self._clusterOrder, return_counts=True)
    self._clusterPeriodNoOccur = {num: counts[ii] for ii, num in enumerate(nums)}

    if self.rescaleClusterPeriods:
        self.clusterPeriods = self._rescaleClusterPeriods(
            self._clusterOrder, self.clusterPeriods, self.extremeClusterIdx
        )

    # if additional time steps have been added, reduce the number of occurrence of the typical period
    # which is related to these time steps
    if not len(self.timeSeries) % self.timeStepsPerPeriod == 0:
        self._clusterPeriodNoOccur[self._clusterOrder[-1]] -= (
            1
            - float(len(self.timeSeries) % self.timeStepsPerPeriod)
            / self.timeStepsPerPeriod
        )

    # put the clustered data in pandas format and scale back
    self.normalizedTypicalPeriods = (
        pd.concat(
            [
                pd.Series(s, index=self.normalizedPeriodlyProfiles.columns)
                for s in self.clusterPeriods
            ],
            axis=1,
        )
        .unstack("TimeStep")
        .T
    )

    if self.segmentation:
        from tsam.utils.segmentation import segmentation

        (
            self.segmentedNormalizedTypicalPeriods,
            self.predictedSegmentedNormalizedTypicalPeriods,
            self.segmentCenterIndices,
        ) = segmentation(
            self.normalizedTypicalPeriods,
            self.noSegments,
            self.timeStepsPerPeriod,
            representationMethod=self.segmentRepresentationMethod,
            representationDict=self.representationDict,
            distributionPeriodWise=self.distributionPeriodWise,
            predefSegmentOrder=self.predefSegmentOrder,
            predefSegmentDurations=self.predefSegmentDurations,
            predefSegmentCenters=self.predefSegmentCenters,
        )
        self.normalizedTypicalPeriods = (
            self.segmentedNormalizedTypicalPeriods.reset_index(level=3, drop=True)
        )

    self.typicalPeriods = self._postProcessTimeSeries(self.normalizedTypicalPeriods)

    # check if original time series boundaries are not exceeded
    exceeds_max = self.typicalPeriods.max(axis=0) > self.timeSeries.max(axis=0)
    if exceeds_max.any():
        diff = self.typicalPeriods.max(axis=0) - self.timeSeries.max(axis=0)
        exceeding_diff = diff[exceeds_max]
        if exceeding_diff.max() > self.numericalTolerance:
            warnings.warn(
                "At least one maximal value of the "
                + "aggregated time series exceeds the maximal value "
                + "the input time series for: "
                + f"{exceeding_diff.to_dict()}"
                + ". To silence the warning set the 'numericalTolerance' to a higher value."
            )
    below_min = self.typicalPeriods.min(axis=0) < self.timeSeries.min(axis=0)
    if below_min.any():
        diff = self.timeSeries.min(axis=0) - self.typicalPeriods.min(axis=0)
        exceeding_diff = diff[below_min]
        if exceeding_diff.max() > self.numericalTolerance:
            warnings.warn(
                "Something went wrong... At least one minimal value of the "
                + "aggregated time series exceeds the minimal value "
                + "the input time series for: "
                + f"{exceeding_diff.to_dict()}"
                + ". To silence the warning set the 'numericalTolerance' to a higher value."
            )
    return self.typicalPeriods

prepareEnersysInput

prepareEnersysInput()

Creates all dictionaries and lists which are required for the energy system optimization input.

Source code in src/tsam/timeseriesaggregation.py
def prepareEnersysInput(self):
    """
    Creates all dictionaries and lists which are required for the energy system
    optimization input.
    """
    warnings.warn(
        '"prepareEnersysInput" is deprecated, since the created attributes can be directly accessed as properties',
        DeprecationWarning,
    )
    return

predictOriginalData

predictOriginalData()

Predicts the overall time series if every period would be placed in the related cluster center

:returns: predictedData (pandas.DataFrame) -- DataFrame which has the same shape as the original one.

Source code in src/tsam/timeseriesaggregation.py
def predictOriginalData(self):
    """
    Predicts the overall time series if every period would be placed in the
    related cluster center

    :returns: **predictedData** (pandas.DataFrame) -- DataFrame which has the same shape as the original one.
    """
    if not hasattr(self, "_clusterOrder"):
        self.createTypicalPeriods()

    # Select typical periods source based on segmentation
    if self.segmentation:
        typical = self.predictedSegmentedNormalizedTypicalPeriods
    else:
        typical = self.normalizedTypicalPeriods

    from tsam.config import _expand_periods

    clustered_data_df = _expand_periods(typical, tuple(self._clusterOrder))

    # back in form
    self.normalizedPredictedData = pd.DataFrame(
        clustered_data_df.values[: len(self.timeSeries)],
        index=self.timeSeries.index,
        columns=self.timeSeries.columns,
    )
    # For the non-segmentation path, normalizedTypicalPeriods was already
    # unweighted and sameMean-reversed in-place by createTypicalPeriods →
    # _postProcessTimeSeries. We must undo the sameMean in-place change
    # so _unnormalizeTimeSeries can re-apply it during inverse transform.
    #
    # For the segmentation path, predictedSegmentedNormalizedTypicalPeriods
    # was NOT modified in-place, so it still carries weights and sameMean.
    # We pass applyWeighting=True so _postProcessTimeSeries removes them.
    if self.segmentation:
        self.predictedData = self._postProcessTimeSeries(
            self.normalizedPredictedData, applyWeighting=True
        )
    else:
        if self.sameMean:
            self.normalizedPredictedData /= self._normalizedMean
        self.predictedData = self._postProcessTimeSeries(
            self.normalizedPredictedData, applyWeighting=False
        )

    return self.predictedData

indexMatching

indexMatching()

Relates the index of the original time series with the indices represented by the clusters

:returns: timeStepMatching (pandas.DataFrame) -- DataFrame which has the same shape as the original one.

Source code in src/tsam/timeseriesaggregation.py
def indexMatching(self):
    """
    Relates the index of the original time series with the indices
    represented by the clusters

    :returns: **timeStepMatching** (pandas.DataFrame) -- DataFrame which has the same shape as the original one.
    """
    if not hasattr(self, "_clusterOrder"):
        self.createTypicalPeriods()

    # create aggregated period and time step index lists
    periodIndex = []
    stepIndex = []
    for label in self._clusterOrder:
        for step in range(self.timeStepsPerPeriod):
            periodIndex.append(label)
            stepIndex.append(step)

    # create a dataframe
    timeStepMatching = pd.DataFrame(
        [periodIndex, stepIndex],
        index=["PeriodNum", "TimeStep"],
        columns=self.timeIndex,
    ).T

    # if segmentation is chosen, append another column stating which
    if self.segmentation:
        segmentIndex = []
        for label in self._clusterOrder:
            segmentIndex.extend(
                np.repeat(
                    self.segmentedNormalizedTypicalPeriods.loc[
                        label, :
                    ].index.get_level_values(0),
                    self.segmentedNormalizedTypicalPeriods.loc[
                        label, :
                    ].index.get_level_values(1),
                ).values
            )
        timeStepMatching = pd.DataFrame(
            [periodIndex, stepIndex, segmentIndex],
            index=["PeriodNum", "TimeStep", "SegmentIndex"],
            columns=self.timeIndex,
        ).T

    return timeStepMatching

accuracyIndicators

accuracyIndicators()

Compares the predicted data with the original time series.

Returns:

Type Description

pd.DataFrame(indicatorRaw) (pandas.DataFrame) -- Dataframe containing indicators evaluating the accuracy of the aggregation

Source code in src/tsam/timeseriesaggregation.py
def accuracyIndicators(self):
    """
    Compares the predicted data with the original time series.

    :returns: **pd.DataFrame(indicatorRaw)** (pandas.DataFrame) -- Dataframe containing indicators evaluating the
                accuracy of the
                aggregation
    """
    if not hasattr(self, "predictedData"):
        self.predictOriginalData()

    indicatorRaw = {
        "RMSE": {},
        "RMSE_duration": {},
        "MAE": {},
    }  # 'Silhouette score':{},

    for column in self.normalizedTimeSeries.columns:
        if self.weightDict:
            origTS = self.normalizedTimeSeries[column] / self.weightDict.get(
                column, 1
            )
        else:
            origTS = self.normalizedTimeSeries[column]

        predTS = self.normalizedPredictedData[column]
        indicatorRaw["RMSE"][column] = np.sqrt(mean_squared_error(origTS, predTS))
        indicatorRaw["RMSE_duration"][column] = np.sqrt(
            mean_squared_error(
                origTS.sort_values(ascending=False).reset_index(drop=True),
                predTS.sort_values(ascending=False).reset_index(drop=True),
            )
        )
        indicatorRaw["MAE"][column] = mean_absolute_error(origTS, predTS)

    return pd.DataFrame(indicatorRaw)

totalAccuracyIndicators

totalAccuracyIndicators()

Derives the accuracy indicators over all time series

Source code in src/tsam/timeseriesaggregation.py
def totalAccuracyIndicators(self):
    """
    Derives the accuracy indicators over all time series
    """
    return np.sqrt(
        self.accuracyIndicators().pow(2).sum()
        / len(self.normalizedTimeSeries.columns)
    )

unstackToPeriods

unstackToPeriods(timeSeries, timeStepsPerPeriod)

Extend the timeseries to an integer multiple of the period length and groups the time series to the periods.

Parameters:

Name Type Description Default
timeSeries pandas DataFrame
required
timeStepsPerPeriod integer

The number of discrete timesteps which describe one period. required

required

Returns:

Type Description
  • unstackedTimeSeries (pandas DataFrame) -- is stacked such that each row represents a candidate period - timeIndex (pandas Series index) -- is the modification of the original timeseriesindex in case an integer multiple was created

.. deprecated:: Use :func:tsam.unstack_to_periods instead.

Source code in src/tsam/timeseriesaggregation.py
def unstackToPeriods(timeSeries, timeStepsPerPeriod):
    """
    Extend the timeseries to an integer multiple of the period length and
    groups the time series to the periods.

    :param timeSeries:
    :type timeSeries: pandas DataFrame

    :param timeStepsPerPeriod: The number of discrete timesteps which describe one period. required
    :type timeStepsPerPeriod: integer

    :returns: - **unstackedTimeSeries** (pandas DataFrame) -- is stacked such that each row represents a
                candidate period
              - **timeIndex** (pandas Series index) -- is the modification of the original
                timeseriesindex in case an integer multiple was created

    .. deprecated::
        Use :func:`tsam.unstack_to_periods` instead.
    """
    warnings.warn(
        "unstackToPeriods will be removed in tsam v4.0. Use tsam.unstack_to_periods() instead.",
        LegacyAPIWarning,
        stacklevel=2,
    )
    # init new grouped timeindex
    unstackedTimeSeries = timeSeries.copy()

    # initialize new indices
    periodIndex = []
    stepIndex = []

    # extend to inger multiple of period length
    if len(timeSeries) % timeStepsPerPeriod == 0:
        attached_timesteps = 0
    else:
        # calculate number of timesteps which get attached
        attached_timesteps = timeStepsPerPeriod - len(timeSeries) % timeStepsPerPeriod

        # take these from the head of the original time series
        rep_data = unstackedTimeSeries.head(attached_timesteps)

        # append them at the end of the time series
        unstackedTimeSeries = pd.concat([unstackedTimeSeries, rep_data])

    # create period and step index
    for ii in range(0, len(unstackedTimeSeries)):
        periodIndex.append(int(ii / timeStepsPerPeriod))
        stepIndex.append(ii - int(ii / timeStepsPerPeriod) * timeStepsPerPeriod)

    # save old index
    timeIndex = copy.deepcopy(unstackedTimeSeries.index)

    # create new double index and unstack the time series
    unstackedTimeSeries.index = pd.MultiIndex.from_arrays(
        [stepIndex, periodIndex], names=["TimeStep", "PeriodNum"]
    )
    unstackedTimeSeries = unstackedTimeSeries.unstack(level="TimeStep")

    return unstackedTimeSeries, timeIndex