tsam 2.3.9__py3-none-any.whl → 3.0.0__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/representations.py CHANGED
@@ -1,167 +1,177 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- import numpy as np
4
- from sklearn.metrics.pairwise import euclidean_distances
5
- from tsam.utils.durationRepresentation import durationRepresentation
6
-
7
-
8
- def representations(
9
- candidates,
10
- clusterOrder,
11
- default,
12
- representationMethod=None,
13
- representationDict=None,
14
- distributionPeriodWise=True,
15
- timeStepsPerPeriod=None,
16
- ):
17
- clusterCenterIndices = None
18
- if representationMethod is None:
19
- representationMethod = default
20
- if representationMethod == "meanRepresentation":
21
- clusterCenters = meanRepresentation(candidates, clusterOrder)
22
- elif representationMethod == "medoidRepresentation":
23
- clusterCenters, clusterCenterIndices = medoidRepresentation(
24
- candidates, clusterOrder
25
- )
26
- elif representationMethod == "maxoidRepresentation":
27
- clusterCenters, clusterCenterIndices = maxoidRepresentation(
28
- candidates, clusterOrder
29
- )
30
- elif representationMethod == "minmaxmeanRepresentation":
31
- clusterCenters = minmaxmeanRepresentation(
32
- candidates, clusterOrder, representationDict, timeStepsPerPeriod
33
- )
34
- elif representationMethod == "durationRepresentation" or representationMethod == "distributionRepresentation":
35
- clusterCenters = durationRepresentation(
36
- candidates, clusterOrder, distributionPeriodWise, timeStepsPerPeriod, representMinMax=False,
37
- )
38
- elif representationMethod == "distributionAndMinMaxRepresentation":
39
- clusterCenters = durationRepresentation(
40
- candidates, clusterOrder, distributionPeriodWise, timeStepsPerPeriod, representMinMax=True,
41
- )
42
- else:
43
- raise ValueError("Chosen 'representationMethod' does not exist.")
44
-
45
- return clusterCenters, clusterCenterIndices
46
-
47
-
48
- def maxoidRepresentation(candidates, clusterOrder):
49
- """
50
- Represents the candidates of a given cluster group (clusterOrder)
51
- by its medoid, measured with the euclidean distance.
52
-
53
- :param candidates: Dissimilarity matrix where each row represents a candidate. required
54
- :type candidates: np.ndarray
55
-
56
- :param clusterOrder: Integer array where the index refers to the candidate and the
57
- Integer entry to the group. required
58
- :type clusterOrder: np.array
59
- """
60
- # set cluster member that is farthest away from the points of the other clusters as maxoid
61
- clusterCenters = []
62
- clusterCenterIndices = []
63
- for clusterNum in np.unique(clusterOrder):
64
- indice = np.where(clusterOrder == clusterNum)
65
- innerDistMatrix = euclidean_distances(candidates, candidates[indice])
66
- mindistIdx = np.argmax(innerDistMatrix.sum(axis=0))
67
- clusterCenters.append(candidates[indice][mindistIdx])
68
- clusterCenterIndices.append(indice[0][mindistIdx])
69
-
70
- return clusterCenters, clusterCenterIndices
71
-
72
-
73
- def medoidRepresentation(candidates, clusterOrder):
74
- """
75
- Represents the candidates of a given cluster group (clusterOrder)
76
- by its medoid, measured with the euclidean distance.
77
-
78
- :param candidates: Dissimilarity matrix where each row represents a candidate. required
79
- :type candidates: np.ndarray
80
-
81
- :param clusterOrder: Integer array where the index refers to the candidate and the
82
- Integer entry to the group. required
83
- :type clusterOrder: np.array
84
- """
85
- # set cluster center as medoid
86
- clusterCenters = []
87
- clusterCenterIndices = []
88
- for clusterNum in np.unique(clusterOrder):
89
- indice = np.where(clusterOrder == clusterNum)
90
- innerDistMatrix = euclidean_distances(candidates[indice])
91
- mindistIdx = np.argmin(innerDistMatrix.sum(axis=0))
92
- clusterCenters.append(candidates[indice][mindistIdx])
93
- clusterCenterIndices.append(indice[0][mindistIdx])
94
-
95
- return clusterCenters, clusterCenterIndices
96
-
97
-
98
- def meanRepresentation(candidates, clusterOrder):
99
- """
100
- Represents the candidates of a given cluster group (clusterOrder)
101
- by its mean.
102
-
103
- :param candidates: Dissimilarity matrix where each row represents a candidate. required
104
- :type candidates: np.ndarray
105
-
106
- :param clusterOrder: Integer array where the index refers to the candidate and the
107
- Integer entry to the group. required
108
- :type clusterOrder: np.array
109
- """
110
- # set cluster centers as means of the group candidates
111
- clusterCenters = []
112
- for clusterNum in np.unique(clusterOrder):
113
- indice = np.where(clusterOrder == clusterNum)
114
- currentMean = candidates[indice].mean(axis=0)
115
- clusterCenters.append(currentMean)
116
- return clusterCenters
117
-
118
-
119
- def minmaxmeanRepresentation(
120
- candidates, clusterOrder, representationDict, timeStepsPerPeriod
121
- ):
122
- """
123
- Represents the candidates of a given cluster group (clusterOrder)
124
- by either the minimum, the maximum or the mean values of each time step for
125
- all periods in that cluster depending on the command for each attribute.
126
-
127
- :param candidates: Dissimilarity matrix where each row represents a candidate. required
128
- :type candidates: np.ndarray
129
-
130
- :param clusterOrder: Integer array where the index refers to the candidate and the
131
- Integer entry to the group. required
132
- :type clusterOrder: np.array
133
-
134
- :param representationDict: A dictionary which defines for each attribute whether the typical
135
- period should be represented by the minimum or maximum values within each cluster.
136
- optional (default: None)
137
- :type representationDict: dictionary
138
-
139
- :param timeStepsPerPeriod: The number of discrete timesteps which describe one period. required
140
- :type timeStepsPerPeriod: integer
141
- """
142
- # set cluster center depending of the representationDict
143
- clusterCenters = []
144
- for clusterNum in np.unique(clusterOrder):
145
- indice = np.where(clusterOrder == clusterNum)
146
- currentClusterCenter = np.zeros(len(representationDict) * timeStepsPerPeriod)
147
- for attributeNum in range(len(representationDict)):
148
- startIdx = attributeNum * timeStepsPerPeriod
149
- endIdx = (attributeNum + 1) * timeStepsPerPeriod
150
- if list(representationDict.values())[attributeNum] == "min":
151
- currentClusterCenter[startIdx:endIdx] = candidates[
152
- indice, startIdx:endIdx
153
- ].min(axis=1)
154
- elif list(representationDict.values())[attributeNum] == "max":
155
- currentClusterCenter[startIdx:endIdx] = candidates[
156
- indice, startIdx:endIdx
157
- ].max(axis=1)
158
- elif list(representationDict.values())[attributeNum] == "mean":
159
- currentClusterCenter[startIdx:endIdx] = candidates[
160
- indice, startIdx:endIdx
161
- ].mean(axis=1)
162
- else:
163
- raise ValueError(
164
- 'At least one value in the representationDict is neither "min", "max" nor "mean".'
165
- )
166
- clusterCenters.append(currentClusterCenter)
167
- return clusterCenters
1
+ import numpy as np
2
+ from sklearn.metrics.pairwise import euclidean_distances
3
+
4
+ from tsam.utils.durationRepresentation import durationRepresentation
5
+
6
+
7
+ def representations(
8
+ candidates,
9
+ clusterOrder,
10
+ default,
11
+ representationMethod=None,
12
+ representationDict=None,
13
+ distributionPeriodWise=True,
14
+ timeStepsPerPeriod=None,
15
+ ):
16
+ clusterCenterIndices = None
17
+ if representationMethod is None:
18
+ representationMethod = default
19
+ if representationMethod == "meanRepresentation":
20
+ clusterCenters = meanRepresentation(candidates, clusterOrder)
21
+ elif representationMethod == "medoidRepresentation":
22
+ clusterCenters, clusterCenterIndices = medoidRepresentation(
23
+ candidates, clusterOrder
24
+ )
25
+ elif representationMethod == "maxoidRepresentation":
26
+ clusterCenters, clusterCenterIndices = maxoidRepresentation(
27
+ candidates, clusterOrder
28
+ )
29
+ elif representationMethod == "minmaxmeanRepresentation":
30
+ clusterCenters = minmaxmeanRepresentation(
31
+ candidates, clusterOrder, representationDict, timeStepsPerPeriod
32
+ )
33
+ elif (
34
+ representationMethod == "durationRepresentation"
35
+ or representationMethod == "distributionRepresentation"
36
+ ):
37
+ clusterCenters = durationRepresentation(
38
+ candidates,
39
+ clusterOrder,
40
+ distributionPeriodWise,
41
+ timeStepsPerPeriod,
42
+ representMinMax=False,
43
+ )
44
+ elif representationMethod == "distributionAndMinMaxRepresentation":
45
+ clusterCenters = durationRepresentation(
46
+ candidates,
47
+ clusterOrder,
48
+ distributionPeriodWise,
49
+ timeStepsPerPeriod,
50
+ representMinMax=True,
51
+ )
52
+ else:
53
+ raise ValueError("Chosen 'representationMethod' does not exist.")
54
+
55
+ return clusterCenters, clusterCenterIndices
56
+
57
+
58
+ def maxoidRepresentation(candidates, clusterOrder):
59
+ """
60
+ Represents the candidates of a given cluster group (clusterOrder)
61
+ by its medoid, measured with the euclidean distance.
62
+
63
+ :param candidates: Dissimilarity matrix where each row represents a candidate. required
64
+ :type candidates: np.ndarray
65
+
66
+ :param clusterOrder: Integer array where the index refers to the candidate and the
67
+ Integer entry to the group. required
68
+ :type clusterOrder: np.array
69
+ """
70
+ # set cluster member that is farthest away from the points of the other clusters as maxoid
71
+ clusterCenters = []
72
+ clusterCenterIndices = []
73
+ for clusterNum in np.unique(clusterOrder):
74
+ indice = np.where(clusterOrder == clusterNum)
75
+ innerDistMatrix = euclidean_distances(candidates, candidates[indice])
76
+ mindistIdx = np.argmax(innerDistMatrix.sum(axis=0))
77
+ clusterCenters.append(candidates[indice][mindistIdx])
78
+ clusterCenterIndices.append(indice[0][mindistIdx])
79
+
80
+ return clusterCenters, clusterCenterIndices
81
+
82
+
83
+ def medoidRepresentation(candidates, clusterOrder):
84
+ """
85
+ Represents the candidates of a given cluster group (clusterOrder)
86
+ by its medoid, measured with the euclidean distance.
87
+
88
+ :param candidates: Dissimilarity matrix where each row represents a candidate. required
89
+ :type candidates: np.ndarray
90
+
91
+ :param clusterOrder: Integer array where the index refers to the candidate and the
92
+ Integer entry to the group. required
93
+ :type clusterOrder: np.array
94
+ """
95
+ # set cluster center as medoid
96
+ clusterCenters = []
97
+ clusterCenterIndices = []
98
+ for clusterNum in np.unique(clusterOrder):
99
+ indice = np.where(clusterOrder == clusterNum)
100
+ innerDistMatrix = euclidean_distances(candidates[indice])
101
+ mindistIdx = np.argmin(innerDistMatrix.sum(axis=0))
102
+ clusterCenters.append(candidates[indice][mindistIdx])
103
+ clusterCenterIndices.append(indice[0][mindistIdx])
104
+
105
+ return clusterCenters, clusterCenterIndices
106
+
107
+
108
+ def meanRepresentation(candidates, clusterOrder):
109
+ """
110
+ Represents the candidates of a given cluster group (clusterOrder)
111
+ by its mean.
112
+
113
+ :param candidates: Dissimilarity matrix where each row represents a candidate. required
114
+ :type candidates: np.ndarray
115
+
116
+ :param clusterOrder: Integer array where the index refers to the candidate and the
117
+ Integer entry to the group. required
118
+ :type clusterOrder: np.array
119
+ """
120
+ # set cluster centers as means of the group candidates
121
+ clusterCenters = []
122
+ for clusterNum in np.unique(clusterOrder):
123
+ indice = np.where(clusterOrder == clusterNum)
124
+ currentMean = candidates[indice].mean(axis=0)
125
+ clusterCenters.append(currentMean)
126
+ return clusterCenters
127
+
128
+
129
+ def minmaxmeanRepresentation(
130
+ candidates, clusterOrder, representationDict, timeStepsPerPeriod
131
+ ):
132
+ """
133
+ Represents the candidates of a given cluster group (clusterOrder)
134
+ by either the minimum, the maximum or the mean values of each time step for
135
+ all periods in that cluster depending on the command for each attribute.
136
+
137
+ :param candidates: Dissimilarity matrix where each row represents a candidate. required
138
+ :type candidates: np.ndarray
139
+
140
+ :param clusterOrder: Integer array where the index refers to the candidate and the
141
+ Integer entry to the group. required
142
+ :type clusterOrder: np.array
143
+
144
+ :param representationDict: A dictionary which defines for each attribute whether the typical
145
+ period should be represented by the minimum or maximum values within each cluster.
146
+ optional (default: None)
147
+ :type representationDict: dictionary
148
+
149
+ :param timeStepsPerPeriod: The number of discrete timesteps which describe one period. required
150
+ :type timeStepsPerPeriod: integer
151
+ """
152
+ # set cluster center depending of the representationDict
153
+ clusterCenters = []
154
+ for clusterNum in np.unique(clusterOrder):
155
+ indice = np.where(clusterOrder == clusterNum)
156
+ currentClusterCenter = np.zeros(len(representationDict) * timeStepsPerPeriod)
157
+ for attributeNum in range(len(representationDict)):
158
+ startIdx = attributeNum * timeStepsPerPeriod
159
+ endIdx = (attributeNum + 1) * timeStepsPerPeriod
160
+ if list(representationDict.values())[attributeNum] == "min":
161
+ currentClusterCenter[startIdx:endIdx] = candidates[
162
+ indice, startIdx:endIdx
163
+ ].min(axis=1)
164
+ elif list(representationDict.values())[attributeNum] == "max":
165
+ currentClusterCenter[startIdx:endIdx] = candidates[
166
+ indice, startIdx:endIdx
167
+ ].max(axis=1)
168
+ elif list(representationDict.values())[attributeNum] == "mean":
169
+ currentClusterCenter[startIdx:endIdx] = candidates[
170
+ indice, startIdx:endIdx
171
+ ].mean(axis=1)
172
+ else:
173
+ raise ValueError(
174
+ 'At least one value in the representationDict is neither "min", "max" nor "mean".'
175
+ )
176
+ clusterCenters.append(currentClusterCenter)
177
+ return clusterCenters