tsam 2.2.2__py3-none-any.whl → 2.3.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tsam/hyperparametertuning.py +245 -245
- tsam/periodAggregation.py +141 -141
- tsam/representations.py +167 -167
- tsam/timeseriesaggregation.py +1358 -1309
- tsam/utils/durationRepresentation.py +204 -128
- tsam/utils/k_maxoids.py +145 -145
- tsam/utils/k_medoids_contiguity.py +140 -133
- tsam/utils/k_medoids_exact.py +239 -234
- tsam/utils/segmentation.py +118 -119
- {tsam-2.2.2.dist-info → tsam-2.3.4.dist-info}/LICENSE.txt +20 -20
- {tsam-2.2.2.dist-info → tsam-2.3.4.dist-info}/METADATA +175 -167
- tsam-2.3.4.dist-info/RECORD +16 -0
- {tsam-2.2.2.dist-info → tsam-2.3.4.dist-info}/WHEEL +1 -1
- tsam-2.2.2.dist-info/RECORD +0 -16
- {tsam-2.2.2.dist-info → tsam-2.3.4.dist-info}/top_level.txt +0 -0
tsam/utils/segmentation.py
CHANGED
|
@@ -1,119 +1,118 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
import pandas as pd
|
|
5
|
-
from sklearn.cluster import AgglomerativeClustering
|
|
6
|
-
from tsam.representations import representations
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def segmentation(
|
|
10
|
-
normalizedTypicalPeriods,
|
|
11
|
-
noSegments,
|
|
12
|
-
timeStepsPerPeriod,
|
|
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
|
-
return segmentedNormalizedTypicalPeriods, predictedSegmentedNormalizedTypicalPeriods
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from sklearn.cluster import AgglomerativeClustering
|
|
6
|
+
from tsam.representations import representations
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def segmentation(
|
|
10
|
+
normalizedTypicalPeriods,
|
|
11
|
+
noSegments,
|
|
12
|
+
timeStepsPerPeriod,
|
|
13
|
+
representationMethod=None,
|
|
14
|
+
representationDict=None,
|
|
15
|
+
distributionPeriodWise=True,
|
|
16
|
+
):
|
|
17
|
+
"""
|
|
18
|
+
Agglomerative clustering of adjacent time steps within a set of typical periods in order to further reduce the
|
|
19
|
+
temporal resolution within typical periods and to further reduce complexity of input data.
|
|
20
|
+
|
|
21
|
+
:param normalizedTypicalPeriods: MultiIndex DataFrame containing the typical periods as first index, the time steps
|
|
22
|
+
within the periods as second index and the attributes as columns.
|
|
23
|
+
:type normalizedTypicalPeriods: pandas DataFrame
|
|
24
|
+
|
|
25
|
+
:param noSegments: Number of segments in which the typical periods should be subdivided - equivalent to the number of
|
|
26
|
+
inner-period clusters.
|
|
27
|
+
:type noSegments: integer
|
|
28
|
+
|
|
29
|
+
:param timeStepsPerPeriod: Number of time steps per period
|
|
30
|
+
:type timeStepsPerPeriod: integer
|
|
31
|
+
|
|
32
|
+
:returns: - **segmentedNormalizedTypicalPeriods** (pandas DataFrame) -- MultiIndex DataFrame similar to
|
|
33
|
+
normalizedTypicalPeriods but with segments instead of time steps. Moreover, two additional index
|
|
34
|
+
levels define the length of each segment and the time step index at which each segment starts.
|
|
35
|
+
- **predictedSegmentedNormalizedTypicalPeriods** (pandas DataFrame) -- MultiIndex DataFrame with the same
|
|
36
|
+
shape of normalizedTypicalPeriods, but with overwritten values derived from segmentation used for
|
|
37
|
+
prediction of the original periods and accuracy indicators.
|
|
38
|
+
"""
|
|
39
|
+
# Initialize lists for predicted and segmented DataFrame
|
|
40
|
+
segmentedNormalizedTypicalPeriodsList = []
|
|
41
|
+
predictedSegmentedNormalizedTypicalPeriodsList = []
|
|
42
|
+
# do for each typical period
|
|
43
|
+
for i in normalizedTypicalPeriods.index.get_level_values(0).unique():
|
|
44
|
+
# make numpy array with rows containing the segmenatation candidates (time steps)
|
|
45
|
+
# and columns as dimensions of the
|
|
46
|
+
segmentationCandidates = np.asarray(normalizedTypicalPeriods.loc[i, :])
|
|
47
|
+
# produce adjacency matrix: Each time step is only connected to its preceding and succeeding one
|
|
48
|
+
adjacencyMatrix = np.eye(timeStepsPerPeriod, k=1) + np.eye(
|
|
49
|
+
timeStepsPerPeriod, k=-1
|
|
50
|
+
)
|
|
51
|
+
# execute clustering of adjacent time steps
|
|
52
|
+
if noSegments == 1:
|
|
53
|
+
clusterOrder = np.asarray([0] * len(segmentationCandidates))
|
|
54
|
+
else:
|
|
55
|
+
clustering = AgglomerativeClustering(
|
|
56
|
+
n_clusters=noSegments, linkage="ward", connectivity=adjacencyMatrix
|
|
57
|
+
)
|
|
58
|
+
clusterOrder = clustering.fit_predict(segmentationCandidates)
|
|
59
|
+
# determine the indices where the segments change and the number of time steps in each segment
|
|
60
|
+
segNo, indices, segmentNoOccur = np.unique(
|
|
61
|
+
clusterOrder, return_index=True, return_counts=True
|
|
62
|
+
)
|
|
63
|
+
clusterOrderUnique = [clusterOrder[index] for index in sorted(indices)]
|
|
64
|
+
# determine the segments' values
|
|
65
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
66
|
+
segmentationCandidates,
|
|
67
|
+
clusterOrder,
|
|
68
|
+
default="meanRepresentation",
|
|
69
|
+
representationMethod=representationMethod,
|
|
70
|
+
representationDict=representationDict,
|
|
71
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
72
|
+
timeStepsPerPeriod=1,
|
|
73
|
+
)
|
|
74
|
+
# clusterCenters = meanRepresentation(segmentationCandidates, clusterOrder)
|
|
75
|
+
# predict each time step of the period by representing it with the corresponding segment's values
|
|
76
|
+
predictedSegmentedNormalizedTypicalPeriods = (
|
|
77
|
+
pd.DataFrame(clusterCenters, columns=normalizedTypicalPeriods.columns)
|
|
78
|
+
.reindex(clusterOrder)
|
|
79
|
+
.reset_index(drop=True)
|
|
80
|
+
)
|
|
81
|
+
# represent the period by the segments in the right order only instead of each time step
|
|
82
|
+
segmentedNormalizedTypicalPeriods = (
|
|
83
|
+
pd.DataFrame(clusterCenters, columns=normalizedTypicalPeriods.columns)
|
|
84
|
+
.reindex(clusterOrderUnique)
|
|
85
|
+
.set_index(np.sort(indices))
|
|
86
|
+
)
|
|
87
|
+
# keep additional information on the lengths of the segments in the right order
|
|
88
|
+
segmentDuration = (
|
|
89
|
+
pd.DataFrame(segmentNoOccur, columns=["Segment Duration"])
|
|
90
|
+
.reindex(clusterOrderUnique)
|
|
91
|
+
.set_index(np.sort(indices))
|
|
92
|
+
)
|
|
93
|
+
# create DataFrame with reduced number of segments together with three indices per period:
|
|
94
|
+
# 1. The segment number
|
|
95
|
+
# 2. The segment duration
|
|
96
|
+
# 3. The index of the original time step, at which the segment starts
|
|
97
|
+
result = segmentedNormalizedTypicalPeriods.set_index(
|
|
98
|
+
[
|
|
99
|
+
pd.Index(segNo, name="Segment Step"),
|
|
100
|
+
segmentDuration["Segment Duration"],
|
|
101
|
+
pd.Index(np.sort(indices), name="Original Start Step"),
|
|
102
|
+
]
|
|
103
|
+
)
|
|
104
|
+
# append predicted and segmented DataFrame to list to create a big DataFrame for all periods
|
|
105
|
+
predictedSegmentedNormalizedTypicalPeriodsList.append(
|
|
106
|
+
predictedSegmentedNormalizedTypicalPeriods
|
|
107
|
+
)
|
|
108
|
+
segmentedNormalizedTypicalPeriodsList.append(result)
|
|
109
|
+
# create a big DataFrame for all periods for predicted segmented time steps and segments and return
|
|
110
|
+
predictedSegmentedNormalizedTypicalPeriods = pd.concat(
|
|
111
|
+
predictedSegmentedNormalizedTypicalPeriodsList,
|
|
112
|
+
keys=normalizedTypicalPeriods.index.get_level_values(0).unique(),
|
|
113
|
+
).rename_axis(["", "TimeStep"])
|
|
114
|
+
segmentedNormalizedTypicalPeriods = pd.concat(
|
|
115
|
+
segmentedNormalizedTypicalPeriodsList,
|
|
116
|
+
keys=normalizedTypicalPeriods.index.get_level_values(0).unique(),
|
|
117
|
+
)
|
|
118
|
+
return segmentedNormalizedTypicalPeriods, predictedSegmentedNormalizedTypicalPeriods
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2017 Leander Kotzur (FZJ IEK-3), Maximilian Hoffmann (FZJ IEK-3), Peter Markewitz (FZJ IEK-3), Martin Robinius (FZJ IEK-3), Detlef Stolten (FZJ IEK-3)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Leander Kotzur (FZJ IEK-3), Maximilian Hoffmann (FZJ IEK-3), Peter Markewitz (FZJ IEK-3), Martin Robinius (FZJ IEK-3), Detlef Stolten (FZJ IEK-3)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|