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
|
|
|
# Implemented algorithms.
|
114
|
|
|
"CCORE Interface for HSYNCNET oscillatory network"
|
|
|
|
|
115
|
|
|
|
116
|
|
|
def hsyncnet_create_network(sample, number_clusters, initial_phases, initial_neighbors, increase_persent):
|
117
|
|
|
pointer_data = create_pointer_data(sample);
|
118
|
|
|
|
119
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
120
|
|
|
ccore.hsyncnet_create_network.restype = POINTER(c_void_p);
|
121
|
|
|
pointer_network = ccore.hsyncnet_create_network(pointer_data, c_uint(number_clusters), c_uint(initial_phases), c_uint(initial_neighbors), c_double(increase_persent));
|
122
|
|
|
|
123
|
|
|
return pointer_network;
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
def hsyncnet_destroy_network(pointer_network):
|
127
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
128
|
|
|
ccore.hsyncnet_destroy_network(pointer_network);
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def hsyncnet_process(network_pointer, order, solution, collect_dynamic):
|
132
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
133
|
|
|
ccore.hsyncnet_process.restype = POINTER(c_void_p);
|
134
|
|
|
return ccore.hsyncnet_process(network_pointer, c_double(order), c_uint(solution), c_bool(collect_dynamic));
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def hsyncnet_analyser_destroy(pointer_analyser):
|
138
|
|
|
ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
|
139
|
|
|
ccore.syncnet_analyser_destroy(pointer_analyser);
|
140
|
|
|
|
141
|
|
|
|