Completed
Push — master ( 417552...916c89 )
by Andrei
01:28
created

hsyncnet.__del__()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 9
rs 9.6666
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 (ccore is True):
74
            self.__ccore_network_pointer = wrapper.hsyncnet_create_network(source_data, number_clusters, osc_initial_phases, initial_neighbors, increase_persent);
75
        else: 
76
            super().__init__(source_data, 0, initial_phases = osc_initial_phases);
77
            
78
            self.__initial_neighbors = initial_neighbors;
79
            self.__increase_persent = increase_persent;
80
            self._number_clusters = number_clusters;
81
    
82
    
83
    def __del__(self):
84
        """!
85
        @brief Destructor of oscillatory network HSyncNet.
86
        
87
        """
88
        
89
        if (self.__ccore_network_pointer is not None):
90
            wrapper.hsyncnet_destroy_network(self.__ccore_network_pointer);
91
            self.__ccore_network_pointer = None;
92
            
93
            
94
    def process(self, order = 0.998, solution = solve_type.FAST, collect_dynamic = False):
95
        """!
96
        @brief Performs clustering of input data set in line with input parameters.
97
        
98
        @param[in] order (double): Level of local synchronization between oscillator that defines end of synchronization process, range [0..1].
99
        @param[in] solution (solve_type) Type of solving differential equation.
100
        @param[in] collect_dynamic (bool): If True - returns whole history of process synchronization otherwise - only final state (when process of clustering is over).
101
        
102
        @return (tuple) Returns dynamic of the network as tuple of lists on each iteration (time, oscillator_phases) that depends on collect_dynamic parameter. 
103
        
104
        @see get_clusters()
105
        
106
        """
107
        
108
        if (self.__ccore_network_pointer is not None):
109
            analyser = wrapper.hsyncnet_process(self.__ccore_network_pointer, order, solution, collect_dynamic);
110
            return syncnet_analyser(None, None, analyser);
111
        
112
        number_neighbors = self.__initial_neighbors;
113
        current_number_clusters = float('inf');
114
        
115
        dyn_phase = [];
116
        dyn_time = [];
117
        
118
        radius = average_neighbor_distance(self._osc_loc, number_neighbors);
119
        
120
        increase_step = int(len(self._osc_loc) * self.__increase_persent);
121
        if (increase_step < 1):
122
            increase_step = 1;
123
        
124
        
125
        analyser = None;
126
        while(current_number_clusters > self._number_clusters):
127
            self._create_connections(radius);
128
        
129
            analyser = self.simulate_dynamic(order, solution, collect_dynamic);
130
            if (collect_dynamic == True):
131
                dyn_phase += analyser.output;
132
                
133
                if (len(dyn_time) > 0):
134
                    point_time_last = dyn_time[len(dyn_time) - 1];
135
                    dyn_time += [time_point + point_time_last for time_point in analyser.time];
136
                else:
137
                    dyn_time += analyser.time;
138
            
139
            clusters = analyser.allocate_sync_ensembles(0.05);
140
            
141
            # Get current number of allocated clusters
142
            current_number_clusters = len(clusters);
143
            
144
            # Increase number of neighbors that should be used
145
            number_neighbors += increase_step;
146
            
147
            # Update connectivity radius and check if average function can be used anymore
148
            if (number_neighbors >= len(self._osc_loc)):
149
                radius = radius * self.__increase_persent + radius;
150
            else:
151
                radius = average_neighbor_distance(self._osc_loc, number_neighbors);
152
        
153
        if (collect_dynamic != True):
154
            dyn_phase = analyser.output;
155
            dyn_time = analyser.time;
156
        
157
        return syncnet_analyser(dyn_phase, dyn_time, None);
158