| @@ 230-267 (lines=38) @@ | ||
| 227 | @brief Animates clustering process that is performed by K-Means algorithm. |
|
| 228 | ||
| 229 | @param[in] data (list): Dataset that is used for clustering. |
|
| 230 | @param[in] observer (kmeans_observer): EM observer that was used for collection information about clustering process. |
|
| 231 | @param[in] animation_velocity (uint): Interval between frames in milliseconds (for run-time animation only). |
|
| 232 | @param[in] movie_fps (uint): Defines frames per second (for rendering movie only). |
|
| 233 | @param[in] save_movie (string): If it is specified then animation will be stored to file that is specified in this parameter. |
|
| 234 | ||
| 235 | """ |
|
| 236 | figure = plt.figure(); |
|
| 237 | ||
| 238 | def init_frame(): |
|
| 239 | return frame_generation(0); |
|
| 240 | ||
| 241 | def frame_generation(index_iteration): |
|
| 242 | figure.clf(); |
|
| 243 | ||
| 244 | figure.suptitle("K-Means algorithm (iteration: " + str(index_iteration) + ")", fontsize=18, fontweight='bold');
|
|
| 245 | ||
| 246 | clusters = observer.get_clusters(index_iteration); |
|
| 247 | centers = observer.get_centers(index_iteration); |
|
| 248 | kmeans_visualizer.show_clusters(data, clusters, centers, None, figure=figure, display=False); |
|
| 249 | ||
| 250 | figure.subplots_adjust(top=0.85); |
|
| 251 | ||
| 252 | return [figure.gca()]; |
|
| 253 | ||
| 254 | iterations = len(observer); |
|
| 255 | cluster_animation = animation.FuncAnimation(figure, frame_generation, iterations, interval=animation_velocity, |
|
| 256 | init_func=init_frame, repeat_delay=5000); |
|
| 257 | ||
| 258 | if save_movie is not None: |
|
| 259 | cluster_animation.save(save_movie, writer='ffmpeg', fps=movie_fps, bitrate=3000); |
|
| 260 | else: |
|
| 261 | plt.show(); |
|
| 262 | ||
| 263 | ||
| 264 | ||
| 265 | class kmeans: |
|
| 266 | """! |
|
| 267 | @brief Class represents K-Means clustering algorithm. |
|
| 268 | @details CCORE option can be used to use the pyclustering core - C/C++ shared library for processing that significantly increases performance. |
|
| 269 | ||
| 270 | CCORE implementation of the algorithm uses thread pool to parallelize the clustering process. |
|
| @@ 617-652 (lines=36) @@ | ||
| 614 | plt.show(); |
|
| 615 | ||
| 616 | ||
| 617 | @staticmethod |
|
| 618 | def animate_phase_matrix(sync_output_dynamic, grid_width = None, grid_height = None, animation_velocity = 75, colormap = 'jet', save_movie = None): |
|
| 619 | """! |
|
| 620 | @brief Shows animation of phase matrix between oscillators during simulation on 2D stage. |
|
| 621 | @details If grid_width or grid_height are not specified than phase matrix size will by calculated automatically by square root. |
|
| 622 | ||
| 623 | @param[in] sync_output_dynamic (sync_dynamic): Output dynamic of the Sync network. |
|
| 624 | @param[in] grid_width (uint): Width of the phase matrix. |
|
| 625 | @param[in] grid_height (uint): Height of the phase matrix. |
|
| 626 | @param[in] animation_velocity (uint): Interval between frames in milliseconds. |
|
| 627 | @param[in] colormap (string): Name of colormap that is used by matplotlib ('gray', 'pink', 'cool', spring', etc.).
|
|
| 628 | @param[in] save_movie (string): If it is specified then animation will be stored to file that is specified in this parameter. |
|
| 629 | ||
| 630 | """ |
|
| 631 | ||
| 632 | figure = plt.figure(); |
|
| 633 | ||
| 634 | def init_frame(): |
|
| 635 | return frame_generation(0); |
|
| 636 | ||
| 637 | def frame_generation(index_dynamic): |
|
| 638 | figure.clf(); |
|
| 639 | axis = figure.add_subplot(111); |
|
| 640 | ||
| 641 | phase_matrix = sync_output_dynamic.allocate_phase_matrix(grid_width, grid_height, index_dynamic); |
|
| 642 | axis.imshow(phase_matrix, cmap = plt.get_cmap(colormap), interpolation='kaiser', vmin = 0.0, vmax = 2.0 * math.pi); |
|
| 643 | artist = figure.gca(); |
|
| 644 | ||
| 645 | return [ artist ]; |
|
| 646 | ||
| 647 | phase_animation = animation.FuncAnimation(figure, frame_generation, len(sync_output_dynamic), init_func = init_frame, interval = animation_velocity , repeat_delay = 1000); |
|
| 648 | ||
| 649 | if (save_movie is not None): |
|
| 650 | phase_animation.save(save_movie, writer = 'ffmpeg', fps = 15, bitrate = 1500); |
|
| 651 | else: |
|
| 652 | plt.show(); |
|
| 653 | ||
| 654 | ||
| 655 | @staticmethod |
|
| @@ 583-614 (lines=32) @@ | ||
| 580 | plt.show(); |
|
| 581 | ||
| 582 | ||
| 583 | @staticmethod |
|
| 584 | def animate_correlation_matrix(sync_output_dynamic, animation_velocity = 75, colormap = 'cool', save_movie = None): |
|
| 585 | """! |
|
| 586 | @brief Shows animation of correlation matrix between oscillators during simulation. |
|
| 587 | ||
| 588 | @param[in] sync_output_dynamic (sync_dynamic): Output dynamic of the Sync network. |
|
| 589 | @param[in] animation_velocity (uint): Interval between frames in milliseconds. |
|
| 590 | @param[in] colormap (string): Name of colormap that is used by matplotlib ('gray', 'pink', 'cool', spring', etc.).
|
|
| 591 | @param[in] save_movie (string): If it is specified then animation will be stored to file that is specified in this parameter. |
|
| 592 | ||
| 593 | """ |
|
| 594 | ||
| 595 | figure = plt.figure(); |
|
| 596 | ||
| 597 | correlation_matrix = sync_output_dynamic.allocate_correlation_matrix(0); |
|
| 598 | artist = plt.imshow(correlation_matrix, cmap = plt.get_cmap(colormap), interpolation='kaiser', hold = True, vmin = 0.0, vmax = 1.0); |
|
| 599 | ||
| 600 | def init_frame(): |
|
| 601 | return [ artist ]; |
|
| 602 | ||
| 603 | def frame_generation(index_dynamic): |
|
| 604 | correlation_matrix = sync_output_dynamic.allocate_correlation_matrix(index_dynamic); |
|
| 605 | artist.set_data(correlation_matrix); |
|
| 606 | ||
| 607 | return [ artist ]; |
|
| 608 | ||
| 609 | correlation_animation = animation.FuncAnimation(figure, frame_generation, len(sync_output_dynamic), init_func = init_frame, interval = animation_velocity , repeat_delay = 1000, blit = True); |
|
| 610 | ||
| 611 | if (save_movie is not None): |
|
| 612 | correlation_animation.save(save_movie, writer = 'ffmpeg', fps = 15, bitrate = 1500); |
|
| 613 | else: |
|
| 614 | plt.show(); |
|
| 615 | ||
| 616 | ||
| 617 | @staticmethod |
|