Completed
Push — master ( 8d4101...9fef16 )
by Andrei
02:27
created

xmeans()   A

Complexity

Conditions 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hsyncnet_create_network() 0 8 1
A hsyncnet_destroy_network() 0 3 1
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 *;
0 ignored issues
show
Unused Code introduced by
string_at 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
create_string_buffer 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
pydll 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
get_last_error 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
c_ushort 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
windll 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
FormatError 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_wchar_p 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
c_uint16 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_ubyte 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
set_errno 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
set_last_error 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
OleDLL 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_int8 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
WinError 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
resize 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
ARRAY 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
SetPointerType 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
get_errno 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
create_unicode_buffer 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...
Unused Code introduced by
DllCanUnloadNow 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_voidp 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
RTLD_LOCAL 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
WinDLL 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
DEFAULT_MODE 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
BigEndianStructure 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
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
c_int16 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_char 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
CDLL 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
c_byte 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
alignment 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
Structure 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
addressof 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...
27
28
from pyclustering.core.definitions import *;
0 ignored issues
show
Unused Code introduced by
os was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
core was imported with wildcard, but is not used.
Loading history...
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."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
36
    
37
    "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...
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."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
62
    
63
    "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...
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."
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
88
    
89
    "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...
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
def extract_pyclustering_package(ccore_package_pointer):
113
    if (ccore_package_pointer == 0):
114
        return [];
115
    
116
    pointer_package = cast(ccore_package_pointer, POINTER(pyclustering_package));
117
    size = pointer_package[0].size;
118
    type_package = pointer_package[0].type;
119
    
120
    result = [];
121
    pointer_data = None;
122
    
123
    if (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_INT):
124
        pointer_data = cast(pointer_package[0].data, POINTER(c_int));
125
    
126
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_INT):
127
        pointer_data = cast(pointer_package[0].data, POINTER(c_uint));
128
    
129
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_FLOAT):
130
        pointer_data = cast(pointer_package[0].data, POINTER(c_float));
131
    
132
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_DOUBLE):
133
        pointer_data = cast(pointer_package[0].data, POINTER(c_double));
134
    
135
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LONG):
136
        pointer_data = cast(pointer_package[0].data, POINTER(c_long));
137
    
138
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_UNSIGNED_LONG):
139
        pointer_data = cast(pointer_package[0].data, POINTER(c_ulong));
140
    
141
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_SIZE_T):
142
        pointer_data = cast(pointer_package[0].data, POINTER(c_size_t));
143
    
144
    elif (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
145
        # pointer_package[0].data == pyclustering_package **
146
        pointer_data = cast(pointer_package[0].data, POINTER(POINTER(pyclustering_package)));
147
        
148
        for index in range(0, size):
149
            pointer_package = cast(pointer_data[index], (POINTER(pyclustering_package)));
150
            result.append(extract_pyclustering_package(pointer_package));
151
152
    else:
153
        assert(0);
154
    
155
    if (type_package != pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
156
        for index in range(0, size):
157
            result.append(pointer_data[index]);
158
    
159
    return result;
160
161
162
# Implemented algorithms.
163
"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...
164
165
def hsyncnet_create_network(sample, number_clusters, initial_phases, initial_neighbors, increase_persent):
166
    pointer_data = create_pointer_data(sample);
167
    
168
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
169
    ccore.hsyncnet_create_network.restype = POINTER(c_void_p);
170
    pointer_network = ccore.hsyncnet_create_network(pointer_data, c_uint(number_clusters), c_uint(initial_phases), c_uint(initial_neighbors), c_double(increase_persent));
171
    
172
    return pointer_network;
173
174
175
def hsyncnet_destroy_network(pointer_network):
176
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
177
    ccore.hsyncnet_destroy_network(pointer_network);
178
179
180
def hsyncnet_process(network_pointer, order, solution, collect_dynamic):
181
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
182
    ccore.hsyncnet_process.restype = POINTER(c_void_p);
183
    return ccore.hsyncnet_process(network_pointer, c_double(order), c_uint(solution), c_bool(collect_dynamic));
184
185
186
def hsyncnet_analyser_destroy(pointer_analyser):
187
    ccore = cdll.LoadLibrary(PATH_DLL_CCORE_64);
188
    ccore.syncnet_analyser_destroy(pointer_analyser);
189
190