Completed
Push — master ( 4b74fd...25863b )
by Andrei
02:05
created

destroy_object()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
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_size_t 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_LIST):
138
        # pointer_package[0].data == pyclustering_package **
139
        pointer_data = cast(pointer_package[0].data, POINTER(POINTER(pyclustering_package)));
140
        
141
        for index in range(0, size):
142
            pointer_package = cast(pointer_data[index], (POINTER(pyclustering_package)));
143
            result.append(extract_pyclustering_package(pointer_package));
144
145
    else:
146
        assert(0);
147
    
148
    if (type_package != pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
149
        for index in range(0, size):
150
            result.append(pointer_data[index]);
151
    
152
    return result;
153
154
155
# Implemented algorithms.
156
def dbscan(sample, eps, min_neighbors, return_noise = False):
157
    "Clustering algorithm DBSCAN returns allocated clusters and noise that are consisted from input data. Calculation is performed via CCORE."
158
    
159
    "(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...
160
    "(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...
161
    "(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...
162
    "(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...
163
    
164
    "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...
165
    "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...
166
    
167
    pointer_data = create_pointer_data(sample);
168
    
169
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
170
    result = ccore.dbscan_algorithm(pointer_data, c_double(eps), c_uint(min_neighbors));
171
172
    list_of_clusters = extract_clusters(result);
173
    ccore.free_clustering_result(result);
174
    
175
    noise = list_of_clusters[len(list_of_clusters) - 1];
176
    list_of_clusters.remove(noise);
177
    
178
    if (return_noise is True):
179
        return (list_of_clusters, noise);
180
    else:
181
        return list_of_clusters;
182
183
def cure(sample, number_clusters, number_represent_points, compression):    
184
    pointer_data = create_pointer_data(sample);
185
    
186
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
187
    result = ccore.cure_algorithm(pointer_data, c_uint(number_clusters), c_uint(number_represent_points), c_double(compression));
188
    
189
    list_of_clusters = extract_clusters(result);
190
    
191
    ccore.free_clustering_result(result);
192
    return list_of_clusters;
193
194
def hierarchical(sample, number_clusters):
195
    "Clustering algorithm hierarchical returns allocated clusters and noise that are consisted from input data. Calculation is performed via CCORE."
196
    
197
    "(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...
198
    "(in) number_clusters    - number of cluster that should be allocated."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
199
    
200
    "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...
201
    
202
    pointer_data = create_pointer_data(sample);
203
    
204
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
205
    result = ccore.hierarchical_algorithm(pointer_data, c_uint(number_clusters));
206
    
207
    list_of_clusters = extract_clusters(result);
208
    
209
    ccore.free_clustering_result(result);
210
    return list_of_clusters;
211
212
213
def kmeans(sample, centers, tolerance):
214
    "Clustering algorithm K-Means returns allocated clusters. Calculation is performed via CCORE."
215
    
216
    "(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...
217
    "(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...
218
    "(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...
219
    
220
    "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...
221
    
222
    pointer_data = create_pointer_data(sample);
223
    pointer_centers = create_pointer_data(centers);
224
    
225
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
226
    result = ccore.kmeans_algorithm(pointer_data, pointer_centers, c_double(tolerance));
227
    
228
    list_of_clusters = extract_clusters(result);
229
    
230
    ccore.free_clustering_result(result);
231
    return list_of_clusters;
232
233
234
def kmedians(sample, centers, tolerance):
235
    pointer_data = create_pointer_data(sample);
236
    pointer_centers = create_pointer_data(centers);
237
    
238
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
239
    result = ccore.kmedians_algorithm(pointer_data, pointer_centers, c_double(tolerance));
240
    
241
    list_of_clusters = extract_clusters(result);
242
    ccore.free_clustering_result(result);
243
    
244
    return list_of_clusters;
245
246
247
def rock(sample, eps, number_clusters, threshold):
248
    "Clustering algorithm ROCK returns allocated clusters and noise that are consisted from input data. Calculation is performed via CCORE."
249
    
250
    "(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...
251
    "(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...
252
    "(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...
253
    "(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...
254
    
255
    "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...
256
    
257
    pointer_data = create_pointer_data(sample);
258
    
259
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
260
    result = ccore.rock_algorithm(pointer_data, c_double(eps), c_uint(number_clusters), c_double(threshold));
261
    
262
    list_of_clusters = extract_clusters(result);
263
    ccore.free_clustering_result(result);
264
    
265
    return list_of_clusters;
266
267
268
def xmeans(sample, centers, kmax, tolerance):
269
    "Clustering algorithm X-Means returns allocated clusters. Calculation is performed via CCORE."
270
    
271
    "(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...
272
    "(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...
273
    "(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...
274
    
275
    "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...
276
    
277
    pointer_data = create_pointer_data(sample);
278
    pointer_centers = create_pointer_data(centers);
279
    
280
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
281
    result = ccore.xmeans_algorithm(pointer_data, pointer_centers, c_uint(kmax), c_double(tolerance));
282
    
283
    list_of_clusters = extract_clusters(result);
284
    
285
    ccore.free_clustering_result(result);
286
    return list_of_clusters;
287
288
289
"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...
290
291
def hsyncnet_create_network(sample, number_clusters, initial_phases):
292
    pointer_data = create_pointer_data(sample);
293
    
294
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
295
    pointer_network = ccore.hsyncnet_create_network(pointer_data, c_uint(number_clusters), c_uint(initial_phases));
296
    
297
    return pointer_network;
298
299
300
def hsyncnet_destroy_network(pointer_network):
301
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
302
    ccore.hsyncnet_destroy_network(pointer_network);
303
304
305
def hsyncnet_process(network_pointer, order, solution, collect_dynamic):
306
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
307
    return ccore.hsyncnet_process(network_pointer, c_double(order), c_uint(solution), c_bool(collect_dynamic));  
308
309
310
def hsyncnet_analyser_destroy(pointer_analyser):
311
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
312
    ccore.syncnet_analyser_destroy(pointer_analyser);     
313
314