1
|
|
|
"""!
|
2
|
|
|
|
3
|
|
|
@brief Wrapper for CCORE library (part of this project).
|
4
|
|
|
|
5
|
|
|
@authors Andrei Novikov ([email protected])
|
6
|
|
|
@date 2014-2017
|
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
|
|
|
from ctypes import *;
|
|
|
|
|
27
|
|
|
|
28
|
|
|
from pyclustering.core.definitions import *;
|
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
# API that is required for interaction with DLL.
|
32
|
|
|
def create_pointer_data(sample):
|
33
|
|
|
"Allocates memory for representing input data for processing that is described by structure 'data_representation' and returns pointer this structure."
|
34
|
|
|
|
35
|
|
|
"(in) sample - dataset for processing."
|
|
|
|
|
36
|
|
|
|
37
|
|
|
"Returns pointer to the data for processing."
|
|
|
|
|
38
|
|
|
|
39
|
|
|
input_data = data_representation();
|
40
|
|
|
input_data.number_objects = len(sample);
|
41
|
|
|
input_data.dimension = len(sample[0]);
|
42
|
|
|
|
43
|
|
|
pointer_objects = (POINTER(c_double) * input_data.number_objects)();
|
44
|
|
|
|
45
|
|
|
for index in range(0, input_data.number_objects):
|
46
|
|
|
point = (c_double * input_data.dimension)();
|
47
|
|
|
for dimension in range(0, input_data.dimension):
|
48
|
|
|
point[dimension] = sample[index][dimension];
|
49
|
|
|
|
50
|
|
|
pointer_objects[index] = cast(point, POINTER(c_double));
|
51
|
|
|
|
52
|
|
|
input_data.pointer_objects = cast(pointer_objects, POINTER(POINTER(c_double)));
|
53
|
|
|
input_data = pointer(input_data);
|
54
|
|
|
|
55
|
|
|
return input_data;
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def extract_clusters(ccore_result):
|
59
|
|
|
"Parse clustering result that is provided by the CCORE. Return Python list of clusters."
|
60
|
|
|
|
61
|
|
|
"(in) ccore_result - pointer to clustering result that has been returned by CCORE."
|
|
|
|
|
62
|
|
|
|
63
|
|
|
"Returns Python list of clusters."
|
|
|
|
|
64
|
|
|
|
65
|
|
|
pointer_clustering_result = cast(ccore_result, POINTER(clustering_result)); # clustering_result * clusters
|
66
|
|
|
number_clusters = pointer_clustering_result[0].number_clusters;
|
67
|
|
|
|
68
|
|
|
list_of_clusters = [];
|
69
|
|
|
|
70
|
|
|
for index_cluster in range(0, number_clusters):
|
71
|
|
|
clusters = cast(pointer_clustering_result[0].pointer_clusters, POINTER(cluster_representation)); # cluster_representation * cluster
|
72
|
|
|
|
73
|
|
|
objects = cast(clusters[index_cluster].pointer_objects, POINTER(c_uint)); # cluster->objects (unsigned int *)
|
74
|
|
|
|
75
|
|
|
list_of_clusters.append([]);
|
76
|
|
|
pointer_container = list_of_clusters[index_cluster];
|
77
|
|
|
|
78
|
|
|
for index_object in range(0, clusters[index_cluster].number_objects):
|
79
|
|
|
pointer_container.append(objects[index_object]);
|
80
|
|
|
|
81
|
|
|
return list_of_clusters;
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def extract_dynamics(ccore_result):
|
85
|
|
|
"Parse dynamic result that is provided by the CCORE. Return Python tuple that represent dynamics (times, dynamic)."
|
86
|
|
|
|
87
|
|
|
"(in) ccore_result - pointer to dynamic result that has been returned by CCORE."
|
|
|
|
|
88
|
|
|
|
89
|
|
|
"Returns Python tuple dynamic (times, dynamic)."
|
|
|
|
|
90
|
|
|
|
91
|
|
|
pointer_dynamic_result = cast(ccore_result, POINTER(dynamic_result)); # dynamic_result * pointer_dynamic_result
|
92
|
|
|
size_dynamic = pointer_dynamic_result[0].size_dynamic;
|
93
|
|
|
size_network = pointer_dynamic_result[0].size_network;
|
94
|
|
|
|
95
|
|
|
pointer_time = cast(pointer_dynamic_result[0].times, POINTER(c_double));
|
96
|
|
|
pointer_pointer_dynamic = cast(pointer_dynamic_result[0].dynamic, POINTER(POINTER(c_double)));
|
97
|
|
|
|
98
|
|
|
times = [];
|
99
|
|
|
dynamic = [];
|
100
|
|
|
|
101
|
|
|
for index in range(0, size_dynamic):
|
102
|
|
|
times.append(pointer_time[index]);
|
103
|
|
|
dynamic.append([]);
|
104
|
|
|
|
105
|
|
|
pointer_dynamic = cast(pointer_pointer_dynamic[index], POINTER(c_double));
|
106
|
|
|
|
107
|
|
|
for object_dynamic in range(0, size_network):
|
108
|
|
|
dynamic[index].append(pointer_dynamic[object_dynamic]);
|
109
|
|
|
|
110
|
|
|
return (times, dynamic);
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def create_pyclustering_package(dataset):
|
114
|
|
|
dataset_package = pyclustering_package();
|
115
|
|
|
|
116
|
|
|
c_data_type = None;
|
117
|
|
|
dataset_package.size = len(dataset);
|
118
|
|
|
|
119
|
|
|
if (isinstance(dataset[0], list)):
|
120
|
|
|
dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LIST;
|
121
|
|
|
c_data_type = POINTER(pyclustering_package);
|
122
|
|
|
|
123
|
|
|
elif (isinstance(dataset[0], int)):
|
124
|
|
|
dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_INT;
|
125
|
|
|
c_data_type = c_int;
|
126
|
|
|
|
127
|
|
|
elif (isinstance(dataset[0], long)):
|
|
|
|
|
128
|
|
|
dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LONG;
|
129
|
|
|
c_data_type = c_long;
|
130
|
|
|
|
131
|
|
|
elif (isinstance(dataset[0], float)):
|
132
|
|
|
dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_FLOAT;
|
133
|
|
|
c_data_type = c_float;
|
134
|
|
|
|
135
|
|
|
else:
|
136
|
|
|
raise NameError("Not supported type of pyclustering package.");
|
137
|
|
|
|
138
|
|
|
if (dataset_package.type == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
|
139
|
|
|
dataset_package.data = (POINTER(c_data_type) * len(dataset))();
|
140
|
|
|
for index in range(len(dataset)):
|
141
|
|
|
dataset_package.data[index] = create_pyclustering_package(dataset[index]);
|
142
|
|
|
else:
|
143
|
|
|
array_object = (c_data_type * len(dataset))(*dataset);
|
144
|
|
|
dataset_package.data = cast(array_object, POINTER(c_void_p));
|
145
|
|
|
|
146
|
|
|
return pointer(dataset_package);
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def extract_pyclustering_package(ccore_package_pointer):
|
150
|
|
|
if (ccore_package_pointer == 0):
|
151
|
|
|
return [];
|
152
|
|
|
|
153
|
|
|
pointer_package = cast(ccore_package_pointer, POINTER(pyclustering_package));
|
154
|
|
|
size = pointer_package[0].size;
|
155
|
|
|
type_package = pointer_package[0].type;
|
156
|
|
|
|
157
|
|
|
result = [];
|
158
|
|
|
pointer_data = None;
|
159
|
|
|
|
160
|
|
|
if (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_INT):
|
161
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_int));
|
162
|
|
|
|
163
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_INT):
|
164
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_uint));
|
165
|
|
|
|
166
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_FLOAT):
|
167
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_float));
|
168
|
|
|
|
169
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_DOUBLE):
|
170
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_double));
|
171
|
|
|
|
172
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LONG):
|
173
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_long));
|
174
|
|
|
|
175
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_LONG):
|
176
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_ulong));
|
177
|
|
|
|
178
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_SIZE_T):
|
179
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(c_size_t));
|
180
|
|
|
|
181
|
|
|
elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
|
182
|
|
|
# pointer_package[0].data == pyclustering_package **
|
183
|
|
|
pointer_data = cast(pointer_package[0].data, POINTER(POINTER(pyclustering_package)));
|
184
|
|
|
|
185
|
|
|
for index in range(0, size):
|
186
|
|
|
pointer_package = cast(pointer_data[index], (POINTER(pyclustering_package)));
|
187
|
|
|
result.append(extract_pyclustering_package(pointer_package));
|
188
|
|
|
|
189
|
|
|
else:
|
190
|
|
|
assert(0);
|
191
|
|
|
|
192
|
|
|
if (type_package != pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
|
193
|
|
|
for index in range(0, size):
|
194
|
|
|
result.append(pointer_data[index]);
|
195
|
|
|
|
196
|
|
|
return result;
|
197
|
|
|
|
198
|
|
|
|
199
|
|
|
# Implemented algorithms.
|
200
|
|
|
"CCORE Interface for HSYNCNET oscillatory network"
|
|
|
|
|
201
|
|
|
|
202
|
|
|
def hsyncnet_create_network(sample, number_clusters, initial_phases, initial_neighbors, increase_persent):
|
203
|
|
|
pointer_data = create_pointer_data(sample);
|
204
|
|
|
|
205
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
206
|
|
|
ccore.hsyncnet_create_network.restype = POINTER(c_void_p);
|
207
|
|
|
pointer_network = ccore.hsyncnet_create_network(pointer_data, c_uint(number_clusters), c_uint(initial_phases), c_uint(initial_neighbors), c_double(increase_persent));
|
208
|
|
|
|
209
|
|
|
return pointer_network;
|
210
|
|
|
|
211
|
|
|
|
212
|
|
|
def hsyncnet_destroy_network(pointer_network):
|
213
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
214
|
|
|
ccore.hsyncnet_destroy_network(pointer_network);
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
def hsyncnet_process(network_pointer, order, solution, collect_dynamic):
|
218
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
219
|
|
|
ccore.hsyncnet_process.restype = POINTER(c_void_p);
|
220
|
|
|
return ccore.hsyncnet_process(network_pointer, c_double(order), c_uint(solution), c_bool(collect_dynamic));
|
221
|
|
|
|
222
|
|
|
|
223
|
|
|
def hsyncnet_analyser_destroy(pointer_analyser):
|
224
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
225
|
|
|
ccore.syncnet_analyser_destroy(pointer_analyser);
|
226
|
|
|
|
227
|
|
|
|