Completed
Branch master (5cc02c)
by Andrei
01:30
created

pyclustering.nnet.tests.Test.templateSyncEnsemblesAllocation()   F

Complexity

Conditions 16

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 16
dl 0
loc 45
rs 2.7326

How to fix   Complexity   

Complexity

Complex classes like pyclustering.nnet.tests.Test.templateSyncEnsemblesAllocation() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""!
2
3
@brief Unit-tests for Pulse Coupled Neural Network.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2016
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
import unittest;
27
28
from pyclustering.nnet.pcnn import pcnn_network, pcnn_parameters;
29
from pyclustering.nnet import *;
30
31
class Test(unittest.TestCase):
32
    def templateDynamicLength(self, num_osc, steps, type_conn, repr_type, stimulus, ccore):
33
        net = pcnn_network(num_osc, None, type_conn, repr_type, None, None, ccore);
34
        dynamic = net.simulate(steps, stimulus);
35
        
36
        assert steps == len(dynamic);
37
        assert num_osc == len(dynamic.output[0]);
38
        assert steps == len(dynamic.allocate_time_signal());
39
    
40
    def testDynamicLengthNoneConnection(self):
41
        self.templateDynamicLength(10, 20, conn_type.NONE, conn_represent.MATRIX, [0] * 10, False);
42
    
43
    def testDynamicLengthNoneConnectionByCore(self):
44
        self.templateDynamicLength(10, 20, conn_type.NONE, None, [0] * 10, True);
45
     
46
    def testDynamicLengthGridFourConnection(self):
47
        self.templateDynamicLength(25, 20, conn_type.GRID_FOUR, conn_represent.MATRIX, [0] * 25, False);
48
 
49
    def testDynamicLengthGridFourConnectionByCore(self):
50
        self.templateDynamicLength(25, 20, conn_type.GRID_FOUR, None, [0] * 25, True);        
51
 
52
    def testDynamicLengthGridEightConnection(self):
53
        self.templateDynamicLength(25, 20, conn_type.GRID_EIGHT, conn_represent.MATRIX, [0] * 25, False);
54
 
55
    def testDynamicLengthGridEightConnectionByCore(self):
56
        self.templateDynamicLength(25, 20, conn_type.GRID_EIGHT, None, [0] * 25, True); 
57
 
58
    def testDynamicLengthListBidirConnection(self):
59
        self.templateDynamicLength(10, 20, conn_type.LIST_BIDIR, conn_represent.MATRIX, [0] * 10, False);
60
 
61
    def testDynamicLengthListBidirConnectionByCore(self):
62
        self.templateDynamicLength(10, 20, conn_type.LIST_BIDIR, None, [0] * 10, True); 
63
 
64
    def testDynamicLengthAllToAllConnection(self):
65
        self.templateDynamicLength(10, 20, conn_type.ALL_TO_ALL, conn_represent.MATRIX, [0] * 10, False);
66
 
67
    def testDynamicLengthAllToAllConnectionByCore(self):
68
        self.templateDynamicLength(10, 20, conn_type.ALL_TO_ALL, None, [0] * 10, True); 
69
    
70
    def testDynamicLengthListRepresentation(self):
71
        self.templateDynamicLength(25, 30, conn_type.NONE, conn_represent.LIST, [0] * 25, False);
72
        self.templateDynamicLength(25, 30, conn_type.GRID_EIGHT, conn_represent.LIST, [0] * 25, False);
73
        self.templateDynamicLength(25, 30, conn_type.GRID_FOUR, conn_represent.LIST, [0] * 25, False);
74
        self.templateDynamicLength(25, 30, conn_type.LIST_BIDIR, conn_represent.LIST, [0] * 25, False);
75
        self.templateDynamicLength(25, 30, conn_type.ALL_TO_ALL, conn_represent.LIST, [0] * 25, False);
76
    
77
    
78
    def templateGridRectangleDynamicLength(self, num_osc, steps, type_conn, repr_type, height, width, stimulus, ccore):
79
        net = pcnn_network(num_osc, None, type_conn, repr_type, height, width, ccore);
80
        dynamic = net.simulate(steps, stimulus);
81
        
82
        assert steps == len(dynamic);
83
        assert num_osc == len(dynamic.output[0]);
84
        assert steps == len(dynamic.allocate_time_signal());
85
    
86
    def testDynamicLengthGridRectangle25FourConnection(self):
87
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_FOUR, conn_represent.MATRIX, 1, 25, [0] * 25, False);
88
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_FOUR, conn_represent.MATRIX, 25, 1, [0] * 25, False);
89
 
90
    def testDynamicLengthGridRectangle25FourConnectionByCore(self):
91
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_FOUR, None, 1, 25, [0] * 25, True);
92
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_FOUR, None, 25, 1, [0] * 25, True);   
93
 
94
    def testDynamicLengthGridRectangle25EightConnection(self):
95
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_EIGHT, conn_represent.MATRIX, 1, 25, [0] * 25, False);
96
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_EIGHT, conn_represent.MATRIX, 25, 1, [0] * 25, False);
97
 
98
    def testDynamicLengthGridRectangle25EightConnectionByCore(self):
99
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_EIGHT, None, 1, 25, [0] * 25, True);
100
        self.templateGridRectangleDynamicLength(25, 20, conn_type.GRID_EIGHT, None, 25, 1, [0] * 25, True); 
101
    
102
    
103
    def templateSyncEnsemblesAllocation(self, num_osc, type_conn, steps, stimulus, ccore, ensembles):
104
        net = pcnn_network(num_osc, None, type_conn, conn_represent.MATRIX, None, None, ccore);
105
        dynamic = net.simulate(steps, stimulus);
106
        
107
        assert steps == len(dynamic);
108
        
109
        sync_ensembles = dynamic.allocate_sync_ensembles();
110
        
111
        if (ensembles is not None):
112
            assert len(ensembles) == len(sync_ensembles);
113
            
114
            for expected_ensemble in ensembles:
115
                ensemble_correct = False;
116
                
117
                for index_ensemble in range(len(sync_ensembles)):
118
                    sorted_expected_ensemble = expected_ensemble.sort();
119
                    sorted_ensemble = sync_ensembles[index_ensemble].sort();
120
                    
121
                    if (sorted_expected_ensemble == sorted_ensemble):
122
                        ensemble_correct = True;
123
                        break;
124
                
125
                assert (True == ensemble_correct);
126
                
127
        unique_indexes = set();
128
        
129
        time_signal = dynamic.allocate_time_signal();
130
        spike_ensembles = dynamic.allocate_spike_ensembles();
131
        sync_ensembles = dynamic.allocate_sync_ensembles();
132
        
133
        for ensemble in spike_ensembles:
134
            assert len(ensemble) in time_signal;
135
        
136
        for ensemble in sync_ensembles:           
137
            spike_ensembles_exist = False;
138
            for index in range(len(spike_ensembles)): 
139
                if (ensemble == spike_ensembles[index]):
140
                    spike_ensembles_exist = True;
141
                    break;
142
            
143
            assert (True == spike_ensembles_exist);
144
            
145
            for index_oscillator in ensemble:
146
                assert index_oscillator not in unique_indexes;
147
                unique_indexes.add(index_oscillator);
148
    
149
    def testSyncEnsemblesAllStimulated(self):
150
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, [1] * 25, False, [ list(range(25)) ]);
151
    
152
    def testSyncEnsemblesAllStimulatedByCore(self):
153
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, [1] * 25, True, [ list(range(25)) ]);
154
    
155
    def testSyncEnsemblesAllUnstimulated(self):
156
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, [0] * 25, False, []);
157
158
    def testSyncEnsemblesAllUnstimulatedByCore(self):
159
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, [0] * 25, True, []);
160
    
161
    def testSyncEnsemblesPartialStimulation(self):
162
        stimulus = ([0] * 5) + ([1] * 5) + ([0] * 5) + ([1] * 5) + ([0] * 5);
163
        expected_ensemble = [5, 6, 7, 8, 9, 15, 16, 17, 18, 19];
164
        
165
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, stimulus, False, [ expected_ensemble ]);
166
    
167
    def testSyncEnsemblesPartialStimulationByCore(self):
168
        stimulus = ([0] * 5) + ([1] * 5) + ([0] * 5) + ([1] * 5) + ([0] * 5);
169
        expected_ensemble = [5, 6, 7, 8, 9, 15, 16, 17, 18, 19];
170
        
171
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 20, stimulus, True, [ expected_ensemble ]);
172
    
173
    def testSyncEnsemblesAllStimulatedWithVariousConnection(self):
174
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 50, [20] * 25, False, None);
175
        self.templateSyncEnsemblesAllocation(25, conn_type.GRID_EIGHT, 50, [20] * 25, False, None);
176
        self.templateSyncEnsemblesAllocation(25, conn_type.GRID_FOUR, 50, [20] * 25, False, None);
177
        self.templateSyncEnsemblesAllocation(25, conn_type.LIST_BIDIR, 50, [20] * 25, False, None);
178
        self.templateSyncEnsemblesAllocation(25, conn_type.NONE, 50, [20] * 25, False, None);
179
180
    def testSyncEnsemblesAllStimulatedWithVariousConnectionByCore(self):
181
        self.templateSyncEnsemblesAllocation(25, conn_type.ALL_TO_ALL, 50, [20] * 25, True, None);
182
        self.templateSyncEnsemblesAllocation(25, conn_type.GRID_EIGHT, 50, [20] * 25, True, None);
183
        self.templateSyncEnsemblesAllocation(25, conn_type.GRID_FOUR, 50, [20] * 25, True, None);
184
        self.templateSyncEnsemblesAllocation(25, conn_type.LIST_BIDIR, 50, [20] * 25, True, None);
185
        self.templateSyncEnsemblesAllocation(25, conn_type.NONE, 50, [20] * 25, True, None);
186
187
188
if __name__ == "__main__":
189
    unittest.main();