| Total Complexity | 87 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
Complex classes like network 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 | """! |
||
| 97 | class network: |
||
| 98 | """! |
||
| 99 | @brief Common network description that consists of information about oscillators and connection between them. |
||
| 100 | |||
| 101 | """ |
||
| 102 | |||
| 103 | _num_osc = 0; |
||
| 104 | |||
| 105 | __osc_conn = None; |
||
| 106 | __conn_represent = None; |
||
| 107 | __conn_type = None; |
||
| 108 | |||
| 109 | __height = 0; |
||
| 110 | __width = 0; |
||
| 111 | |||
| 112 | |||
| 113 | @property |
||
| 114 | def height(self): |
||
| 115 | """! |
||
| 116 | @brief Height of the network grid (that is defined by amout of oscillators in each column), this value is zero in case of non-grid structure. |
||
| 117 | |||
| 118 | @note This property returns valid value only for network with grid structure. |
||
| 119 | |||
| 120 | """ |
||
| 121 | return self.__height; |
||
| 122 | |||
| 123 | |||
| 124 | @property |
||
| 125 | def width(self): |
||
| 126 | """! |
||
| 127 | @brief Width of the network grid, this value is zero in case of non-grid structure. |
||
| 128 | |||
| 129 | @note This property returns valid value only for network with grid structure. |
||
| 130 | |||
| 131 | """ |
||
| 132 | return self.__width; |
||
| 133 | |||
| 134 | |||
| 135 | @property |
||
| 136 | def structure(self): |
||
| 137 | """! |
||
| 138 | @brief Type of network structure that is used for connecting oscillators. |
||
| 139 | |||
| 140 | """ |
||
| 141 | return self.__conn_type; |
||
| 142 | |||
| 143 | |||
| 144 | def __init__(self, num_osc, type_conn = conn_type.ALL_TO_ALL, conn_represent = conn_represent.MATRIX, height = None, width = None): |
||
| 145 | """! |
||
| 146 | @brief Constructor of the network. |
||
| 147 | |||
| 148 | @param[in] num_osc (uint): Number of oscillators in the network that defines size of the network. |
||
| 149 | @param[in] type_conn (conn_type): Type of connections that are used in the network between oscillators. |
||
| 150 | @param[in] conn_represent (conn_represent): Type of representation of connections. |
||
| 151 | @param[in] height (uint): Number of oscillators in column of the network, this argument is used |
||
| 152 | only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. |
||
| 153 | @param[in] width (uint): Number of oscillotors in row of the network, this argument is used only |
||
| 154 | for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. |
||
| 155 | |||
| 156 | """ |
||
| 157 | |||
| 158 | self._num_osc = num_osc; |
||
| 159 | self.__conn_represent = conn_represent; |
||
| 160 | self.__conn_type = type_conn; |
||
| 161 | |||
| 162 | if ( (type_conn == conn_type.GRID_EIGHT) or (type_conn == conn_type.GRID_FOUR) ): |
||
| 163 | if ( (height is not None) and (width is not None) ): |
||
| 164 | self.__height = height; |
||
| 165 | self.__width = width; |
||
| 166 | else: |
||
| 167 | side_size = self._num_osc ** (0.5); |
||
| 168 | if (side_size - math.floor(side_size) > 0): |
||
| 169 | raise NameError('Invalid number of oscillators in the network in case of grid structure');
|
||
| 170 | |||
| 171 | self.__height = int(side_size); |
||
| 172 | self.__width = self.__height; |
||
| 173 | |||
| 174 | if (self.__height * self.__width != self._num_osc): |
||
| 175 | raise NameError('Width (' + str(self.__width) + ') x Height (' + str(self.__height) + ') must be equal to Size (' + str(self._num_osc) + ') in case of grid structure');
|
||
| 176 | |||
| 177 | self._create_structure(type_conn); |
||
| 178 | |||
| 179 | |||
| 180 | def __len__(self): |
||
| 181 | """! |
||
| 182 | @brief Returns size of the network that is defined by amount of oscillators. |
||
| 183 | |||
| 184 | """ |
||
| 185 | return self._num_osc; |
||
| 186 | |||
| 187 | |||
| 188 | def __create_all_to_all_connections(self): |
||
| 189 | """! |
||
| 190 | @brief Creates connections between all oscillators. |
||
| 191 | |||
| 192 | """ |
||
| 193 | |||
| 194 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 195 | for index in range(0, self._num_osc, 1): |
||
| 196 | self.__osc_conn.append([True] * self._num_osc); |
||
| 197 | self.__osc_conn[index][index] = False; |
||
| 198 | |||
| 199 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 200 | for index in range(0, self._num_osc, 1): |
||
| 201 | self.__osc_conn.append([neigh for neigh in range(0, self._num_osc, 1) if index != neigh]); |
||
| 202 | |||
| 203 | |||
| 204 | def __create_grid_four_connections(self): |
||
| 205 | """! |
||
| 206 | @brief Creates network with connections that make up four grid structure. |
||
| 207 | @details Each oscillator may be connected with four neighbors in line with 'grid' structure: right, upper, left, lower. |
||
| 208 | |||
| 209 | """ |
||
| 210 | |||
| 211 | side_size = self.__width; |
||
| 212 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 213 | self.__osc_conn = [[0] * self._num_osc for index in range(0, self._num_osc, 1)]; |
||
| 214 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 215 | self.__osc_conn = [[] for index in range(0, self._num_osc, 1)]; |
||
| 216 | else: |
||
| 217 | raise NameError("Unknown type of representation of connections");
|
||
| 218 | |||
| 219 | for index in range(0, self._num_osc, 1): |
||
| 220 | upper_index = index - side_size; |
||
| 221 | lower_index = index + side_size; |
||
| 222 | left_index = index - 1; |
||
| 223 | right_index = index + 1; |
||
| 224 | |||
| 225 | node_row_index = math.ceil(index / side_size); |
||
| 226 | if (upper_index >= 0): |
||
| 227 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 228 | self.__osc_conn[index][upper_index] = True; |
||
| 229 | else: |
||
| 230 | self.__osc_conn[index].append(upper_index); |
||
| 231 | |||
| 232 | if (lower_index < self._num_osc): |
||
| 233 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 234 | self.__osc_conn[index][lower_index] = True; |
||
| 235 | else: |
||
| 236 | self.__osc_conn[index].append(lower_index); |
||
| 237 | |||
| 238 | if ( (left_index >= 0) and (math.ceil(left_index / side_size) == node_row_index) ): |
||
| 239 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 240 | self.__osc_conn[index][left_index] = True; |
||
| 241 | else: |
||
| 242 | self.__osc_conn[index].append(left_index); |
||
| 243 | |||
| 244 | if ( (right_index < self._num_osc) and (math.ceil(right_index / side_size) == node_row_index) ): |
||
| 245 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 246 | self.__osc_conn[index][right_index] = True; |
||
| 247 | else: |
||
| 248 | self.__osc_conn[index].append(right_index); |
||
| 249 | |||
| 250 | |||
| 251 | def __create_grid_eight_connections(self): |
||
| 252 | """! |
||
| 253 | @brief Creates network with connections that make up eight grid structure. |
||
| 254 | @details Each oscillator may be connected with eight neighbors in line with grid structure: right, right-upper, upper, upper-left, left, left-lower, lower, lower-right. |
||
| 255 | |||
| 256 | """ |
||
| 257 | |||
| 258 | self.__create_grid_four_connections(); # create connection with right, upper, left, lower. |
||
| 259 | side_size = self.__width; |
||
| 260 | |||
| 261 | for index in range(0, self._num_osc, 1): |
||
| 262 | upper_left_index = index - side_size - 1; |
||
| 263 | upper_right_index = index - side_size + 1; |
||
| 264 | |||
| 265 | lower_left_index = index + side_size - 1; |
||
| 266 | lower_right_index = index + side_size + 1; |
||
| 267 | |||
| 268 | node_row_index = math.floor(index / side_size); |
||
| 269 | upper_row_index = node_row_index - 1; |
||
| 270 | lower_row_index = node_row_index + 1; |
||
| 271 | |||
| 272 | if ( (upper_left_index >= 0) and (math.floor(upper_left_index / side_size) == upper_row_index) ): |
||
| 273 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 274 | self.__osc_conn[index][upper_left_index] = True; |
||
| 275 | else: |
||
| 276 | self.__osc_conn[index].append(upper_left_index); |
||
| 277 | |||
| 278 | if ( (upper_right_index >= 0) and (math.floor(upper_right_index / side_size) == upper_row_index) ): |
||
| 279 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 280 | self.__osc_conn[index][upper_right_index] = True; |
||
| 281 | else: |
||
| 282 | self.__osc_conn[index].append(upper_right_index); |
||
| 283 | |||
| 284 | if ( (lower_left_index < self._num_osc) and (math.floor(lower_left_index / side_size) == lower_row_index) ): |
||
| 285 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 286 | self.__osc_conn[index][lower_left_index] = True; |
||
| 287 | else: |
||
| 288 | self.__osc_conn[index].append(lower_left_index); |
||
| 289 | |||
| 290 | if ( (lower_right_index < self._num_osc) and (math.floor(lower_right_index / side_size) == lower_row_index) ): |
||
| 291 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 292 | self.__osc_conn[index][lower_right_index] = True; |
||
| 293 | else: |
||
| 294 | self.__osc_conn[index].append(lower_right_index); |
||
| 295 | |||
| 296 | |||
| 297 | def __create_list_bidir_connections(self): |
||
| 298 | """! |
||
| 299 | @brief Creates network as bidirectional list. |
||
| 300 | @details Each oscillator may be conneted with two neighbors in line with classical list structure: right, left. |
||
| 301 | |||
| 302 | """ |
||
| 303 | |||
| 304 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 305 | for index in range(0, self._num_osc, 1): |
||
| 306 | self.__osc_conn.append([0] * self._num_osc); |
||
| 307 | self.__osc_conn[index][index] = False; |
||
| 308 | if (index > 0): |
||
| 309 | self.__osc_conn[index][index - 1] = True; |
||
| 310 | |||
| 311 | if (index < (self._num_osc - 1)): |
||
| 312 | self.__osc_conn[index][index + 1] = True; |
||
| 313 | |||
| 314 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 315 | for index in range(self._num_osc): |
||
| 316 | self.__osc_conn.append([]); |
||
| 317 | if (index > 0): |
||
| 318 | self.__osc_conn[index].append(index - 1); |
||
| 319 | |||
| 320 | if (index < (self._num_osc - 1)): |
||
| 321 | self.__osc_conn[index].append(index + 1); |
||
| 322 | |||
| 323 | |||
| 324 | def __create_none_connections(self): |
||
| 325 | """! |
||
| 326 | @brief Creates network without connections. |
||
| 327 | |||
| 328 | """ |
||
| 329 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 330 | for index in range(0, self._num_osc, 1): |
||
| 331 | self.__osc_conn.append([False] * self._num_osc); |
||
| 332 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 333 | self.__osc_conn = [[] for index in range(0, self._num_osc, 1)]; |
||
| 334 | |||
| 335 | |||
| 336 | def __create_dynamic_connection(self): |
||
| 337 | """! |
||
| 338 | @brief Prepare storage for dynamic connections. |
||
| 339 | |||
| 340 | """ |
||
| 341 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 342 | for index in range(0, self._num_osc, 1): |
||
| 343 | self.__osc_conn.append([False] * self._num_osc); |
||
| 344 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 345 | self.__osc_conn = [[] for index in range(0, self._num_osc, 1)]; |
||
| 346 | |||
| 347 | |||
| 348 | def _create_structure(self, type_conn = conn_type.ALL_TO_ALL): |
||
| 349 | """! |
||
| 350 | @brief Creates connection in line with representation of matrix connections [NunOsc x NumOsc]. |
||
| 351 | |||
| 352 | @param[in] type_conn (conn_type): Connection type (all-to-all, bidirectional list, grid structure, etc.) that is used by the network. |
||
| 353 | |||
| 354 | """ |
||
| 355 | |||
| 356 | self.__osc_conn = list(); |
||
| 357 | |||
| 358 | if (type_conn == conn_type.NONE): |
||
| 359 | self.__create_none_connections(); |
||
| 360 | |||
| 361 | elif (type_conn == conn_type.ALL_TO_ALL): |
||
| 362 | self.__create_all_to_all_connections(); |
||
| 363 | |||
| 364 | elif (type_conn == conn_type.GRID_FOUR): |
||
| 365 | self.__create_grid_four_connections(); |
||
| 366 | |||
| 367 | elif (type_conn == conn_type.GRID_EIGHT): |
||
| 368 | self.__create_grid_eight_connections(); |
||
| 369 | |||
| 370 | elif (type_conn == conn_type.LIST_BIDIR): |
||
| 371 | self.__create_list_bidir_connections(); |
||
| 372 | |||
| 373 | elif (type_conn == conn_type.DYNAMIC): |
||
| 374 | self.__create_dynamic_connection(); |
||
| 375 | |||
| 376 | else: |
||
| 377 | raise NameError('The unknown type of connections');
|
||
| 378 | |||
| 379 | |||
| 380 | def has_connection(self, i, j): |
||
| 381 | """! |
||
| 382 | @brief Returns True if there is connection between i and j oscillators and False - if connection doesn't exist. |
||
| 383 | |||
| 384 | @param[in] i (uint): index of an oscillator in the network. |
||
| 385 | @param[in] j (uint): index of an oscillator in the network. |
||
| 386 | |||
| 387 | """ |
||
| 388 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 389 | return (self.__osc_conn[i][j]); |
||
| 390 | |||
| 391 | elif (self.__conn_represent == conn_represent.LIST): |
||
| 392 | for neigh_index in range(0, len(self.__osc_conn[i]), 1): |
||
| 393 | if (self.__osc_conn[i][neigh_index] == j): |
||
| 394 | return True; |
||
| 395 | return False; |
||
| 396 | |||
| 397 | else: |
||
| 398 | raise NameError("Unknown type of representation of coupling");
|
||
| 399 | |||
| 400 | |||
| 401 | def set_connection(self, i, j): |
||
| 402 | """! |
||
| 403 | @brief Couples two specified oscillators in the network with dynamic connections. |
||
| 404 | |||
| 405 | @param[in] i (uint): index of an oscillator that should be coupled with oscillator 'j' in the network. |
||
| 406 | @param[in] j (uint): index of an oscillator that should be coupled with oscillator 'i' in the network. |
||
| 407 | |||
| 408 | @note This method can be used only in case of DYNAMIC connections, otherwise it throws expection. |
||
| 409 | |||
| 410 | """ |
||
| 411 | |||
| 412 | if (self.structure != conn_type.DYNAMIC): |
||
| 413 | raise NameError("Connection between oscillators can be changed only in case of dynamic type.");
|
||
| 414 | |||
| 415 | if (self.__conn_represent == conn_represent.MATRIX): |
||
| 416 | self.__osc_conn[i][j] = True; |
||
| 417 | self.__osc_conn[j][i] = True; |
||
| 418 | else: |
||
| 419 | self.__osc_conn[i].append(j); |
||
| 420 | self.__osc_conn[j].append(i); |
||
| 421 | |||
| 422 | |||
| 423 | def get_neighbors(self, index): |
||
| 424 | """! |
||
| 425 | @brief Finds neighbors of the oscillator with specified index. |
||
| 426 | |||
| 427 | @param[in] index (uint): index of oscillator for which neighbors should be found in the network. |
||
| 428 | |||
| 429 | @return (list) Indexes of neighbors of the specified oscillator. |
||
| 430 | |||
| 431 | """ |
||
| 432 | |||
| 433 | if (self.__conn_represent == conn_represent.LIST): |
||
| 434 | return self.__osc_conn[index]; # connections are represented by list. |
||
| 435 | elif (self.__conn_represent == conn_represent.MATRIX): |
||
| 436 | return [neigh_index for neigh_index in range(self._num_osc) if self.__osc_conn[index][neigh_index] == True]; |
||
| 437 | else: |
||
| 438 | raise NameError("Unknown type of representation of connections"); |