Completed
Push — master ( 5497dc...4c6f9d )
by Andrei
01:31
created

hsyncnet.__init__()   B

Complexity

Conditions 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 26
rs 8.8571
1
"""!
2
3
@brief Cluster analysis algorithm: Hierarchical Sync (HSyncNet)
4
@details Based on article description:
5
         - J.Shao, X.He, C.Bohm, Q.Yang, C.Plant. Synchronization-Inspired Partitioning and Hierarchical Clustering. 2013.
6
7
@authors Andrei Novikov ([email protected])
8
@date 2014-2016
9
@copyright GNU Public License
10
11
@cond GNU_PUBLIC_LICENSE
12
    PyClustering is free software: you can redistribute it and/or modify
13
    it under the terms of the GNU General Public License as published by
14
    the Free Software Foundation, either version 3 of the License, or
15
    (at your option) any later version.
16
    
17
    PyClustering is distributed in the hope that it will be useful,
18
    but WITHOUT ANY WARRANTY; without even the implied warranty of
19
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
    GNU General Public License for more details.
21
    
22
    You should have received a copy of the GNU General Public License
23
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
@endcond
25
26
"""
27
28
import pyclustering.core.wrapper as wrapper;
29
30
from pyclustering.nnet import initial_type, solve_type;
31
32
from pyclustering.cluster.syncnet import syncnet, syncnet_analyser;
33
from pyclustering.utils import average_neighbor_distance;
34
35
class hsyncnet(syncnet):
36
    """!
37
    @brief Class represents clustering algorithm HSyncNet. HSyncNet is bio-inspired algorithm that is based on oscillatory network that uses modified Kuramoto model.
38
    
39
    Example:
40
    @code
41
        # read list of points for cluster analysis
42
        sample = read_sample(file);
43
        
44
        # create network for allocation three clusters using CCORE (C++ implementation)
45
        network = hsyncnet(sample, 3, ccore = True);
46
        
47
        # run cluster analysis and output dynamic of the network
48
        (time, dynamic) = network.process(0.995, collect_dynamic = True);
49
        
50
        # get allocated clusters
51
        clusters = network.get_clusters();
52
        
53
        # show output dynamic of the network
54
        draw_dynamics(time, dynamic);
55
    @endcode
56
    """
57
    
58
    def __init__(self, source_data, number_clusters, osc_initial_phases = initial_type.RANDOM_GAUSSIAN, initial_neighbors = 3, increase_persent = 0.15, ccore = False):
59
        """!
60
        @brief Costructor of the oscillatory network hSyncNet for cluster analysis.
61
62
        @param[in] source_data (list): Input data set defines structure of the network.
63
        @param[in] number_clusters (uint): Number of clusters that should be allocated.
64
        @param[in] osc_initial_phases (initial_type): Type of initialization of initial values of phases of oscillators.
65
        @param[in] initial_neighbors (uint): Defines initial radius connectivity by calculation average distance to connect specify number of oscillators.
66
        @param[in] increase_persent (double): Percent of increasing of radius connectivity on each step (input values in range (0.0; 1.0) correspond to (0%; 100%)).
67
        @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
68
        
69
        """
70
        
71
        self.__ccore_network_pointer = None;
72
        
73
        if (initial_neighbors >= len(source_data)):
74
            initial_neighbors = len(source_data) - 1;
75
        
76
        if (ccore is True):
77
            self.__ccore_network_pointer = wrapper.hsyncnet_create_network(source_data, number_clusters, osc_initial_phases, initial_neighbors, increase_persent);
78
        else: 
79
            super().__init__(source_data, 0, initial_phases = osc_initial_phases);
80
            
81
            self.__initial_neighbors = initial_neighbors;
82
            self.__increase_persent = increase_persent;
83
            self._number_clusters = number_clusters;
84
    
85
    
86
    def __del__(self):
87
        """!
88
        @brief Destructor of oscillatory network HSyncNet.
89
        
90
        """
91
        
92
        if (self.__ccore_network_pointer is not None):
93
            wrapper.hsyncnet_destroy_network(self.__ccore_network_pointer);
94
            self.__ccore_network_pointer = None;
95
            
96
            
97
    def process(self, order = 0.998, solution = solve_type.FAST, collect_dynamic = False):
98
        """!
99
        @brief Performs clustering of input data set in line with input parameters.
100
        
101
        @param[in] order (double): Level of local synchronization between oscillator that defines end of synchronization process, range [0..1].
102
        @param[in] solution (solve_type) Type of solving differential equation.
103
        @param[in] collect_dynamic (bool): If True - returns whole history of process synchronization otherwise - only final state (when process of clustering is over).
104
        
105
        @return (tuple) Returns dynamic of the network as tuple of lists on each iteration (time, oscillator_phases) that depends on collect_dynamic parameter. 
106
        
107
        @see get_clusters()
108
        
109
        """
110
        
111
        if (self.__ccore_network_pointer is not None):
112
            analyser = wrapper.hsyncnet_process(self.__ccore_network_pointer, order, solution, collect_dynamic);
113
            return syncnet_analyser(None, None, analyser);
114
        
115
        number_neighbors = self.__initial_neighbors;
116
        current_number_clusters = float('inf');
117
        
118
        dyn_phase = [];
119
        dyn_time = [];
120
        
121
        radius = average_neighbor_distance(self._osc_loc, number_neighbors);
122
        
123
        increase_step = int(len(self._osc_loc) * self.__increase_persent);
124
        if (increase_step < 1):
125
            increase_step = 1;
126
        
127
        
128
        analyser = None;
129
        while(current_number_clusters > self._number_clusters):
130
            self._create_connections(radius);
131
        
132
            analyser = self.simulate_dynamic(order, solution, collect_dynamic);
133
            if (collect_dynamic == True):
134
                dyn_phase += analyser.output;
135
                
136
                if (len(dyn_time) > 0):
137
                    point_time_last = dyn_time[len(dyn_time) - 1];
138
                    dyn_time += [time_point + point_time_last for time_point in analyser.time];
139
                else:
140
                    dyn_time += analyser.time;
141
            
142
            clusters = analyser.allocate_sync_ensembles(0.05);
143
            
144
            # Get current number of allocated clusters
145
            current_number_clusters = len(clusters);
146
            
147
            # Increase number of neighbors that should be used
148
            number_neighbors += increase_step;
149
            
150
            # Update connectivity radius and check if average function can be used anymore
151
            if (number_neighbors >= len(self._osc_loc)):
152
                radius = radius * self.__increase_persent + radius;
153
            else:
154
                radius = average_neighbor_distance(self._osc_loc, number_neighbors);
155
        
156
        if (collect_dynamic != True):
157
            dyn_phase = analyser.output;
158
            dyn_time = analyser.time;
159
        
160
        return syncnet_analyser(dyn_phase, dyn_time, None);
161