Completed
Push — master ( 0ec04d...6ed467 )
by Andrei
01:13
created

cluster_result_representor.get_data()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
"""!
2
3
@brief Module for representing clustering results.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2016
7
@copyright GNU Public License
8
9
@cond GNU_PUBLIC_LICENSE
10
    PyClustering is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
    
15
    PyClustering is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
    
20
    You should have received a copy of the GNU General Public License
21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
@endcond
23
24
"""
25
26
27
from enum import IntEnum;
28
29
30
class type_cluster_result(IntEnum):
31
    ## Results are represented by list of indexes and belonging to the cluster is defined by cluster index and element's position corresponds to object's position in input data, for example [0, 0, 1, 1, 1, 0].
32
    CLUSTER_INDEX_LABELING = 0;
33
    
34
    ## Results are represented by list of lists, where each list consists of object indexes, for example [ [0, 1, 2], [3, 4, 5], [6, 7] ].
35
    CLUSTER_INDEX_LIST_SEPARATION = 1;
36
    
37
    ## Results are represented by list of lists, where each list consists of objects, for example [ [obj1, obj2], [obj3, obj4, obj5], [obj6, obj7] ].
38
    CLUSTER_OBJECT_LIST_SEPARATION = 2;
39
40
41
class cluster_result_representor:
42
    """!
43
    @brief Provides service to change clustering result representation.
44
    
45
    Example:
46
    @code
47
        # load list of points for cluster analysis
48
        sample = read_sample(path);
49
        
50
        # create instance of K-Means algorithm
51
        kmeans_instance = kmeans(sample, [ [0.0, 0.1], [2.5, 2.6] ]);
52
        
53
        # run cluster analysis and obtain results
54
        kmeans_instance.process();
55
        clusters = kmeans_instance.get_clusters();
56
        
57
        # by default k-means returns representation CLUSTER_INDEX_LIST_SEPARATION
58
        type_repr = type_cluster_result.CLUSTER_INDEX_LIST_SEPARATION;
59
        representor = cluster_result_representor(type_repr, clusters, sample);
60
        
61
        # change representation from index list to label list
62
        representor.get_representation(type_cluster_result.CLUSTER_INDEX_LABELING);
63
        
64
        # change representation from label to object list
65
        representor.get_representation(type_cluster_result.CLUSTER_OBJECT_LIST_SEPARATION);
66
    @endcode
67
    """
68
    
69
    def __init__(self, type_representation, clusters, data):
70
        """!
71
        @brief Constructor of cluster result representor.
72
        
73
        @param[in] type_representation (type_cluster_result): Type of clusters representation (index list, object list or labels).
74
        @param[in] clusters (list): Current clusters representation.
75
        @param[in] data (list): Data that corresponds to clusters.
76
        
77
        """
78
    
79
        self.__type_representation = type_representation;
80
        self.__clusters = clusters;
81
        self.__data = data;
82
83
84
    @property
85
    def get_representation(self):
86
        """!
87
        @brief Returns current cluster representation.
88
        
89
        """
90
        return self.__type_representation;
91
92
93
    def get_clusters(self):
94
        """!
95
        @brief Returns clusters representation.
96
        
97
        """
98
        return self.__clusters;
99
100
101
    def get_data(self):
102
        """!
103
        @brief Returns data that corresponds to clusters.
104
        
105
        """
106
        return self.__data;
107
108
109
    def set_representation(self, type_representation):
110
        """!
111
        @brief Change clusters representation to specified type.
112
        
113
        @param[in] type_representation (type_cluster_result): New type of clusters representation.
114
        
115
        """
116
        
117
        if(type_representation == self.__type_representation):
118
            return;
119
        
120
        if (self.__type_representation == type_cluster_result.CLUSTER_INDEX_LABELING):
121
            if (type_representation == type_cluster_result.CLUSTER_INDEX_LIST_SEPARATION):
122
                self.__clusters = self.__convert_label_to_index();
123
            
124
            else:
125
                self.__clusters = self.__convert_label_to_object();
126
        
127
        elif (self.__type_representation == type_cluster_result.CLUSTER_INDEX_LIST_SEPARATION):
128
            if (type_representation == type_cluster_result.CLUSTER_INDEX_LABELING):
129
                self.__clusters = self.__convert_index_to_label();
130
            
131
            else:
132
                self.__clusters = self.__convert_index_to_object();
133
        
134
        else:
135
            if (type_representation == type_cluster_result.CLUSTER_INDEX_LABELING):
136
                self.__clusters = self.__convert_object_to_label();
137
            
138
            else:
139
                self.__clusters = self.__convert_object_to_index();
140
        
141
        self.__type_representation = type_representation;
142
143
144
    def __convert_index_to_label(self):
145
        clusters = [0] * len(self.__data);
146
        index_cluster = 0;
147
        
148
        for cluster in self.__clusters:
149
            for index_object in cluster:
150
                clusters[index_object] = index_cluster;
151
        
152
            index_cluster += 1;
153
        
154
        return clusters;
155
156
157
    def __convert_index_to_object(self):
158
        clusters = [ [] for _ in range(len(self.__clusters)) ];
159
        for index_cluster in range(len(self.__clusters)):
160
            for index_object in self.__clusters[index_cluster]:
161
                data_object = self.__data[index_object];
162
                clusters[index_cluster].append(data_object);
163
164
        return clusters;
165
166
167
    def __convert_object_to_label(self):
168
        positions = dict();
169
        clusters = [0] * len(self.__data);
170
        index_cluster = 0;
171
        
172
        for cluster in self.__clusters:
173
            for data_object in cluster:
174
                index_object = -1;
175
                if (data_object in positions):
176
                    index_object = self.__data.index(data_object, positions[data_object] + 1);
177
                else:
178
                    index_object = self.__data.index(data_object);
179
                    
180
                clusters[index_object] = index_cluster;
181
                positions[data_object] = index_object;
182
            
183
            index_cluster += 1;
184
        
185
        return clusters;
186
187
188
    def __convert_object_to_index(self):
189
        positions = dict();
190
        clusters = [ [] for _ in range(len(self.__clusters)) ];
191
        for index_cluster in range(len(self.__clusters)):
192
            for data_object in self.__clusters[index_cluster]:
193
                index_object = -1;
194
                if (data_object in positions):
195
                    index_object = self.__data.index(data_object, positions[data_object] + 1);
196
                else:
197
                    index_object = self.__data.index(data_object);
198
199
                clusters[index_cluster].append(index_object);
200
                positions[data_object] = index_object;
201
202
        return clusters;
203
204
205
    def __convert_label_to_index(self):
206
        clusters = [ [] for _ in range(max(self.__clusters) + 1) ];
207
        
208
        for index_object in range(len(self.__data)):
209
            index_cluster = self.__clusters[index_object];
210
            clusters[index_cluster].append(index_object);
211
        
212
        return clusters;
213
    
214
    
215
    def __convert_label_to_object(self):
216
        clusters = [ [] for _ in range(max(self.__clusters) + 1) ];
217
        
218
        for index_object in range(len(self.__data)):
219
            index_cluster = self.__clusters[index_object];
220
            clusters[index_cluster].append(self.__data[index_object]);
221
        
222
        return clusters;