Completed
Push — 0.7.dev ( 125b80...91561c )
by Andrei
01:13
created

create_pyclustering_package()   C

Complexity

Conditions 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 34
rs 5.5
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hsyncnet_create_network() 0 8 1
A hsyncnet_destroy_network() 0 3 1
A hsyncnet_analyser_destroy() 0 3 1
A hsyncnet_process() 0 4 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...
Unused Code introduced by
c_float was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ulong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_long 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...
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
113
# Implemented algorithms.
114
"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...
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