Completed
Push — master ( 4169ec...001455 )
by Andrei
01:41
created

hierarchical()   A

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 17
rs 9.4285
1
'''
2
3
Wrapper for CCORE library (part of this project).
4
5
Copyright (C) 2015    Andrei Novikov ([email protected])
6
7
pyclustering is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
pyclustering is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
'''
21
22
from ctypes import *;
0 ignored issues
show
Unused Code introduced by
string_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllGetClassObject was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
byref was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_short was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Union was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
memset was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ArgumentError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
resize was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Array was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
HRESULT was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PYFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_LOCAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ushort was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_byte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longlong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DEFAULT_MODE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BigEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PyDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
GetLastError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LittleEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ubyte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
addressof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_string_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
alignment was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pydll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Structure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
SetPointerType was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_voidp was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ssize_t was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
py_object was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OleDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
FormatError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllCanUnloadNow was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
windll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
kind was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
memmove was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ulonglong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
sizeof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WINFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LibraryLoader was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oledll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ARRAY was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_void_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_GLOBAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_unicode_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
wstring_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longdouble was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pythonapi was imported with wildcard, but is not used.
Loading history...
23
24
from pyclustering.core.definitions import *;
0 ignored issues
show
Unused Code introduced by
core was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
os was imported with wildcard, but is not used.
Loading history...
25
26
27
# API that is required for interaction with DLL.
28
def create_pointer_data(sample):
29
    "Allocates memory for representing input data for processing that is described by structure 'data_representation' and returns pointer this structure."
30
    
31
    "(in) sample    - dataset for processing."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
32
    
33
    "Returns pointer to the data for processing."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
34
    
35
    input_data = data_representation();
36
    input_data.number_objects = len(sample);
37
    input_data.dimension = len(sample[0]);
38
    
39
    pointer_objects = (POINTER(c_double) * input_data.number_objects)();
40
41
    for index in range(0, input_data.number_objects):
42
        point = (c_double * input_data.dimension)();
43
        for dimension in range(0, input_data.dimension):
44
            point[dimension] = sample[index][dimension];
45
            
46
        pointer_objects[index] = cast(point, POINTER(c_double));
47
       
48
    input_data.pointer_objects = cast(pointer_objects, POINTER(POINTER(c_double)));
49
    input_data = pointer(input_data);
50
    
51
    return input_data;
52
53
54
def extract_clusters(ccore_result):
55
    "Parse clustering result that is provided by the CCORE. Return Python list of clusters."
56
    
57
    "(in) ccore_result    - pointer to clustering result that has been returned by CCORE."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
58
    
59
    "Returns Python list of clusters."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
60
    
61
    pointer_clustering_result = cast(ccore_result, POINTER(clustering_result));    # clustering_result * clusters
62
    number_clusters = pointer_clustering_result[0].number_clusters;
63
    
64
    list_of_clusters = [];
65
    
66
    for index_cluster in range(0, number_clusters):
67
        clusters = cast(pointer_clustering_result[0].pointer_clusters, POINTER(cluster_representation));  # cluster_representation * cluster
68
        
69
        objects = cast(clusters[index_cluster].pointer_objects, POINTER(c_uint));   # cluster->objects (unsigned int *)
70
        
71
        list_of_clusters.append([]);
72
        pointer_container = list_of_clusters[index_cluster];
73
74
        for index_object in range(0, clusters[index_cluster].number_objects):
75
            pointer_container.append(objects[index_object]);
76
    
77
    return list_of_clusters;
78
79
80
def extract_dynamics(ccore_result):
81
    "Parse dynamic result that is provided by the CCORE. Return Python tuple that represent dynamics (times, dynamic)."
82
    
83
    "(in) ccore_result    - pointer to dynamic result that has been returned by CCORE."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
84
    
85
    "Returns Python tuple dynamic (times, dynamic)."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
86
    
87
    pointer_dynamic_result = cast(ccore_result, POINTER(dynamic_result));   # dynamic_result * pointer_dynamic_result
88
    size_dynamic = pointer_dynamic_result[0].size_dynamic;
89
    size_network = pointer_dynamic_result[0].size_network;
90
    
91
    pointer_time = cast(pointer_dynamic_result[0].times, POINTER(c_double));
92
    pointer_pointer_dynamic = cast(pointer_dynamic_result[0].dynamic, POINTER(POINTER(c_double)));
93
    
94
    times = [];
95
    dynamic = [];
96
    
97
    for index in range(0, size_dynamic):
98
        times.append(pointer_time[index]);
99
        dynamic.append([]);
100
        
101
        pointer_dynamic = cast(pointer_pointer_dynamic[index], POINTER(c_double));
102
        
103
        for object_dynamic in range(0, size_network):
104
            dynamic[index].append(pointer_dynamic[object_dynamic]);
105
        
106
    return (times, dynamic);
107
108
def extract_pyclustering_package(ccore_package_pointer):
109
    if (ccore_package_pointer == 0):
110
        return [];
111
    
112
    pointer_package = cast(ccore_package_pointer, POINTER(pyclustering_package));
113
    size = pointer_package[0].size;
114
    type_package = pointer_package[0].type;
115
    
116
    result = [];
117
    pointer_data = None;
118
    
119
    if (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_INT):
120
        pointer_data = cast(pointer_package[0].data, POINTER(c_int));
121
    
122
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_INT):
123
        pointer_data = cast(pointer_package[0].data, POINTER(c_uint));
124
    
125
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_FLOAT):
126
        pointer_data = cast(pointer_package[0].data, POINTER(c_float));
127
    
128
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_DOUBLE):
129
        pointer_data = cast(pointer_package[0].data, POINTER(c_double));
130
    
131
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LONG):
132
        pointer_data = cast(pointer_package[0].data, POINTER(c_long));
133
    
134
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_LONG):
135
        pointer_data = cast(pointer_package[0].data, POINTER(c_ulong));
136
    
137
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_SIZE_T):
138
        pointer_data = cast(pointer_package[0].data, POINTER(c_size_t));
139
    
140
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
141
        # pointer_package[0].data == pyclustering_package **
142
        pointer_data = cast(pointer_package[0].data, POINTER(POINTER(pyclustering_package)));
143
        
144
        for index in range(0, size):
145
            pointer_package = cast(pointer_data[index], (POINTER(pyclustering_package)));
146
            result.append(extract_pyclustering_package(pointer_package));
147
148
    else:
149
        assert(0);
150
    
151
    if (type_package != pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
152
        for index in range(0, size):
153
            result.append(pointer_data[index]);
154
    
155
    return result;
156
157
158
# Implemented algorithms.
159
def dbscan(sample, eps, min_neighbors, return_noise = False):
160
    "Clustering algorithm DBSCAN returns allocated clusters and noise that are consisted from input data. Calculation is performed via CCORE."
161
    
162
    "(in) data            - input data that is presented as list of points (objects), each point should be represented by list or tuple."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
163
    "(in) eps             - connectivity radius between points, points may be connected if distance between them less then the radius."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
164
    "(in) min_neighbors   - minimum number of shared neighbors that is requied for establish links between points."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
165
    "(in) return_noise    - if True than list of points that have been marked as noise will be returned."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
166
    
167
    "If return_noise is False: Returns list of allocated clusters, each cluster contains indexes of objects in list of data."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
168
    "If return_noise is True: Returns tuple of list of allicated clusters and list of points that are marked as noise."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
169
    
170
    pointer_data = create_pointer_data(sample);
171
    
172
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
173
    result = ccore.dbscan_algorithm(pointer_data, c_double(eps), c_uint(min_neighbors));
174
175
    list_of_clusters = extract_clusters(result);
176
    ccore.free_clustering_result(result);
177
    
178
    noise = list_of_clusters[len(list_of_clusters) - 1];
179
    list_of_clusters.remove(noise);
180
    
181
    if (return_noise is True):
182
        return (list_of_clusters, noise);
183
    else:
184
        return list_of_clusters;
185
186
def cure(sample, number_clusters, number_represent_points, compression):    
187
    pointer_data = create_pointer_data(sample);
188
    
189
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
190
    result = ccore.cure_algorithm(pointer_data, c_uint(number_clusters), c_uint(number_represent_points), c_double(compression));
191
    
192
    list_of_clusters = extract_clusters(result);
193
    
194
    ccore.free_clustering_result(result);
195
    return list_of_clusters;
196
197
198
def kmeans(sample, centers, tolerance):
199
    "Clustering algorithm K-Means returns allocated clusters. Calculation is performed via CCORE."
200
    
201
    "(in) data        - input data that is presented as list of points (objects), each point should be represented by list or tuple."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
202
    "(in) centers     - initial coordinates of centers of clusters that are represented by list: [center1, center2, ...]."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
203
    "(in) tolerance   - stop condition: if maximum value of change of centers of clusters is less than tolerance than algorithm will stop processing."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
204
    
205
    "Returns list of allocated clusters, each cluster contains indexes of objects in list of data."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
206
    
207
    pointer_data = create_pointer_data(sample);
208
    pointer_centers = create_pointer_data(centers);
209
    
210
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
211
    result = ccore.kmeans_algorithm(pointer_data, pointer_centers, c_double(tolerance));
212
    
213
    list_of_clusters = extract_clusters(result);
214
    
215
    ccore.free_clustering_result(result);
216
    return list_of_clusters;
217
218
219
def kmedians(sample, centers, tolerance):
220
    pointer_data = create_pointer_data(sample);
221
    pointer_centers = create_pointer_data(centers);
222
    
223
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
224
    result = ccore.kmedians_algorithm(pointer_data, pointer_centers, c_double(tolerance));
225
    
226
    list_of_clusters = extract_clusters(result);
227
    ccore.free_clustering_result(result);
228
    
229
    return list_of_clusters;
230
231
232
def rock(sample, eps, number_clusters, threshold):
233
    "Clustering algorithm ROCK returns allocated clusters and noise that are consisted from input data. Calculation is performed via CCORE."
234
    
235
    "(in) data                - input data - list of points where each point is represented by list of coordinates."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
236
    "(in) eps                 - connectivity radius (similarity threshold), points are neighbors if distance between them is less than connectivity radius."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
237
    "(in) number_clusters     - defines number of clusters that should be allocated from the input data set."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
238
    "(in) threshold           - value that defines degree of normalization that influences on choice of clusters for merging during processing."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
239
    
240
    "Returns list of allocated clusters, each cluster contains indexes of objects in list of data."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
241
    
242
    pointer_data = create_pointer_data(sample);
243
    
244
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
245
    result = ccore.rock_algorithm(pointer_data, c_double(eps), c_uint(number_clusters), c_double(threshold));
246
    
247
    list_of_clusters = extract_clusters(result);
248
    ccore.free_clustering_result(result);
249
    
250
    return list_of_clusters;
251
252
253
def xmeans(sample, centers, kmax, tolerance):
254
    "Clustering algorithm X-Means returns allocated clusters. Calculation is performed via CCORE."
255
    
256
    "(in) data        - input data that is presented as list of points (objects), each point should be represented by list or tuple."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
257
    "(in) centers     - initial coordinates of centers of clusters that are represented by list: [center1, center2, ...]."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
258
    "(in) kmax        - maximum number of clusters that can be allocated."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
259
    
260
    "Returns list of allocated clusters, each cluster contains indexes of objects in list of data."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
261
    
262
    pointer_data = create_pointer_data(sample);
263
    pointer_centers = create_pointer_data(centers);
264
    
265
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
266
    result = ccore.xmeans_algorithm(pointer_data, pointer_centers, c_uint(kmax), c_double(tolerance));
267
    
268
    list_of_clusters = extract_clusters(result);
269
    
270
    ccore.free_clustering_result(result);
271
    return list_of_clusters;
272
273
274
"CCORE Interface for HSYNCNET oscillatory network"
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
275
276
def hsyncnet_create_network(sample, number_clusters, initial_phases, initial_neighbors, increase_persent):
277
    pointer_data = create_pointer_data(sample);
278
    
279
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
280
    pointer_network = ccore.hsyncnet_create_network(pointer_data, c_uint(number_clusters), c_uint(initial_phases), c_uint(initial_neighbors), c_double(increase_persent));
281
    
282
    return pointer_network;
283
284
285
def hsyncnet_destroy_network(pointer_network):
286
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
287
    ccore.hsyncnet_destroy_network(pointer_network);
288
289
290
def hsyncnet_process(network_pointer, order, solution, collect_dynamic):
291
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
292
    return ccore.hsyncnet_process(network_pointer, c_double(order), c_uint(solution), c_bool(collect_dynamic));  
293
294
295
def hsyncnet_analyser_destroy(pointer_analyser):
296
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
297
    ccore.syncnet_analyser_destroy(pointer_analyser);     
298
299