/
timeseriesaggregation.py
1342 lines (1146 loc) · 53.4 KB
/
timeseriesaggregation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# -*- coding: utf-8 -*-
import copy
import time
import warnings
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.metrics.pairwise import euclidean_distances
from sklearn import preprocessing
from tsam.periodAggregation import aggregatePeriods
from tsam.representations import representations
pd.set_option("mode.chained_assignment", None)
# max iterator while resacling cluster profiles
MAX_ITERATOR = 20
# tolerance while rescaling cluster periods to meet the annual sum of the original profile
TOLERANCE = 1e-6
# minimal weight that overwrites a weighting of zero in order to carry the profile through the aggregation process
MIN_WEIGHT = 1e-6
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
"""
# 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
class TimeSeriesAggregation(object):
"""
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,
weightDict=None,
segmentation=False,
extremePeriodMethod="None",
representationMethod=None,
representationDict=None,
distributionPeriodWise=True,
segmentRepresentationMethod=None,
predefClusterOrder=None,
predefClusterCenterIndices=None,
solver="highs",
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 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 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
"""
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.weightDict = weightDict
self.representationMethod = representationMethod
self.representationDict = representationDict
self.distributionPeriodWise = distributionPeriodWise
self.segmentRepresentationMethod = segmentRepresentationMethod
self.predefClusterOrder = predefClusterOrder
self.predefClusterCenterIndices = predefClusterCenterIndices
self.solver = solver
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):
self.timeSeries = pd.DataFrame(self.timeSeries)
elif 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:
raise ValueError(
"'resolution' argument has to be nonnegative float or int"
+ " or the given timeseries needs a datetime index"
)
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:
raise ValueError(
"'resolution' argument has to be nonnegative float or int"
+ " or the given timeseries needs a datetime index"
)
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: "
+ "{}".format(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: "
+ "{}".format(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: "
+ "{}".format(self.REPRESENTATION_METHODS)
)
# if representationDict None, represent by maximum time steps in each cluster
if self.representationDict is None:
self.representationDict = {i: "mean" for i in list(self.timeSeries.columns)}
# 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: "
+ "{}".format(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'
)
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]
)
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 (
not self.extremePeriods[periodType]["clusterNo"]
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.
"""
weightingVec = pd.Series(self._clusterPeriodNoOccur).values
typicalPeriods = pd.DataFrame(
clusterPeriods, columns=self.normalizedPeriodlyProfiles.columns
)
idx_wo_peak = np.delete(typicalPeriods.index, extremeClusterIdx)
for column in self.timeSeries.columns:
diff = 1
sum_raw = self.normalizedPeriodlyProfiles[column].sum().sum()
sum_peak = sum(
weightingVec[extremeClusterIdx]
* typicalPeriods[column].loc[extremeClusterIdx, :].sum(axis=1)
)
sum_clu_wo_peak = sum(
weightingVec[idx_wo_peak]
* typicalPeriods[column].loc[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
typicalPeriods.loc[idx_wo_peak, column] = (
typicalPeriods[column].loc[idx_wo_peak, :].values
* (sum_raw - sum_peak)
/ sum_clu_wo_peak
)
# reset values higher than the upper sacle or less than zero
typicalPeriods[column][typicalPeriods[column] > scale_ub] = scale_ub
typicalPeriods[column][typicalPeriods[column] < 0.0] = 0.0
typicalPeriods[column] = typicalPeriods[column].fillna(0.0)
# calc new sum and new diff to orig data
sum_clu_wo_peak = sum(
weightingVec[idx_wo_peak]
* typicalPeriods[column].loc[idx_wo_peak, :].sum(axis=1)
)
diff = abs(sum_raw - (sum_clu_wo_peak + sum_peak))
a += 1
if a == MAX_ITERATOR:
deviation = str(round((diff / sum_raw) * 100, 2))
warnings.warn(
'Max iteration number reached for "'
+ str(column)
+ '" while rescaling the cluster periods.'
+ " The integral of the aggregated time series deviates by: "
+ deviation
+ "%"
)
return typicalPeriods.values
def _clusterSortedPeriods(self, candidates, n_init=20):
"""
Runs the clustering algorithms for the sorted profiles within the period
instead of the original profiles. (Duration curve clustering)
"""
# initialize
normalizedSortedPeriodlyProfiles = copy.deepcopy(
self.normalizedPeriodlyProfiles
)
for column in self.timeSeries.columns:
# sort each period individually
df = normalizedSortedPeriodlyProfiles[column]
values = df.values
values.sort(axis=1)
values = values[:, ::-1]
normalizedSortedPeriodlyProfiles[column] = pd.DataFrame(
values, df.index, df.columns
)
sortedClusterValues = normalizedSortedPeriodlyProfiles.values
(
altClusterCenters,
self.clusterCenterIndices,
clusterOrders_C,
) = aggregatePeriods(
sortedClusterValues,
n_clusters=self.noTypicalPeriods,
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
medoid_C = 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(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()
# check for additional cluster parameters
if self.evalSumPeriods:
evaluationValues = (
self.normalizedPeriodlyProfiles.stack(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 not self.predefClusterOrder is None:
self._clusterOrder = self.predefClusterOrder
# check if representatives are defined
if not self.predefClusterCenterIndices is None:
self.clusterCenterIndices = self.predefClusterCenterIndices
self.clusterCenters = candidates[self.predefClusterCenterIndices]
else:
# otherwise take the medoids
self.clusterCenters, self.clusterCenterIndices = representations(
candidates,
self._clusterOrder,
default="medoidRepresentation",
representationMethod=self.representationMethod,
representationDict=self.representationDict,
timeStepsPerPeriod=self.timeStepsPerPeriod,
)