tsam 2.1.0__py3-none-any.whl → 2.3.2__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.
@@ -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
- solver,
14
- representationMethod=None,
15
- representationDict=None,
16
- distributionPeriodWise=True,
17
- ):
18
- """
19
- Agglomerative clustering of adjacent time steps within a set of typical periods in order to further reduce the
20
- temporal resolution within typical periods and to further reduce complexity of input data.
21
-
22
- :param normalizedTypicalPeriods: MultiIndex DataFrame containing the typical periods as first index, the time steps
23
- within the periods as second index and the attributes as columns.
24
- :type normalizedTypicalPeriods: pandas DataFrame
25
-
26
- :param noSegments: Number of segments in which the typical periods shoul be subdivided - equivalent to the number of
27
- inner-period clusters.
28
- :type noSegments: integer
29
-
30
- :param timeStepsPerPeriod: Number of time steps per period
31
- :type timeStepsPerPeriod: integer
32
-
33
- :returns: - **segmentedNormalizedTypicalPeriods** (pandas DataFrame) -- MultiIndex DataFrame similar to
34
- normalizedTypicalPeriods but with segments instead of time steps. Moreover, two additional index
35
- levels define the length of each segment and the time step index at which each segment starts.
36
- - **predictedSegmentedNormalizedTypicalPeriods** (pandas DataFrame) -- MultiIndex DataFrame with the same
37
- shape of normalizedTypicalPeriods, but with overwritten values derived from segmentation used for
38
- prediction of the original periods and accuracy indicators.
39
- """
40
- # Initialize lists for predicted and segmented DataFrame
41
- segmentedNormalizedTypicalPeriodsList = []
42
- predictedSegmentedNormalizedTypicalPeriodsList = []
43
- # do for each typical period
44
- for i in normalizedTypicalPeriods.index.get_level_values(0).unique():
45
- # make numpy array with rows containing the segmenatation candidates (time steps)
46
- # and columns as dimensions of the
47
- segmentationCandidates = np.asarray(normalizedTypicalPeriods.loc[i, :])
48
- # produce adjacency matrix: Each time step is only connected to its preceding and succeeding one
49
- adjacencyMatrix = np.eye(timeStepsPerPeriod, k=1) + np.eye(
50
- timeStepsPerPeriod, k=-1
51
- )
52
- # execute clustering of adjacent time steps
53
- if noSegments == 1:
54
- clusterOrder = np.asarray([0] * len(segmentationCandidates))
55
- else:
56
- clustering = AgglomerativeClustering(
57
- n_clusters=noSegments, linkage="ward", connectivity=adjacencyMatrix
58
- )
59
- clusterOrder = clustering.fit_predict(segmentationCandidates)
60
- # determine the indices where the segments change and the number of time steps in each segment
61
- segNo, indices, segmentNoOccur = np.unique(
62
- clusterOrder, return_index=True, return_counts=True
63
- )
64
- clusterOrderUnique = [clusterOrder[index] for index in sorted(indices)]
65
- # determine the segments' values
66
- clusterCenters, clusterCenterIndices = representations(
67
- segmentationCandidates,
68
- clusterOrder,
69
- default="meanRepresentation",
70
- representationMethod=representationMethod,
71
- representationDict=representationDict,
72
- distributionPeriodWise=distributionPeriodWise,
73
- timeStepsPerPeriod=1,
74
- )
75
- # clusterCenters = meanRepresentation(segmentationCandidates, clusterOrder)
76
- # predict each time step of the period by representing it with the corresponding segment's values
77
- predictedSegmentedNormalizedTypicalPeriods = (
78
- pd.DataFrame(clusterCenters, columns=normalizedTypicalPeriods.columns)
79
- .reindex(clusterOrder)
80
- .reset_index(drop=True)
81
- )
82
- # represent the period by the segments in the right order only instead of each time step
83
- segmentedNormalizedTypicalPeriods = (
84
- pd.DataFrame(clusterCenters, columns=normalizedTypicalPeriods.columns)
85
- .reindex(clusterOrderUnique)
86
- .set_index(np.sort(indices))
87
- )
88
- # keep additional information on the lengths of the segments in the right order
89
- segmentDuration = (
90
- pd.DataFrame(segmentNoOccur, columns=["Segment Duration"])
91
- .reindex(clusterOrderUnique)
92
- .set_index(np.sort(indices))
93
- )
94
- # create DataFrame with reduced number of segments together with three indices per period:
95
- # 1. The segment number
96
- # 2. The segment duration
97
- # 3. The index of the original time step, at which the segment starts
98
- result = segmentedNormalizedTypicalPeriods.set_index(
99
- [
100
- pd.Index(segNo, name="Segment Step"),
101
- segmentDuration["Segment Duration"],
102
- pd.Index(np.sort(indices), name="Original Start Step"),
103
- ]
104
- )
105
- # append predicted and segmented DataFrame to list to create a big DataFrame for all periods
106
- predictedSegmentedNormalizedTypicalPeriodsList.append(
107
- predictedSegmentedNormalizedTypicalPeriods
108
- )
109
- segmentedNormalizedTypicalPeriodsList.append(result)
110
- # create a big DataFrame for all periods for predicted segmented time steps and segments and return
111
- predictedSegmentedNormalizedTypicalPeriods = pd.concat(
112
- predictedSegmentedNormalizedTypicalPeriodsList,
113
- keys=normalizedTypicalPeriods.index.get_level_values(0).unique(),
114
- ).rename_axis(["", "TimeStep"])
115
- segmentedNormalizedTypicalPeriods = pd.concat(
116
- segmentedNormalizedTypicalPeriodsList,
117
- keys=normalizedTypicalPeriods.index.get_level_values(0).unique(),
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.