matrix_animator.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:openanalysis 作者: OpenWeavers 项目源码 文件源码
def animate(self, save=False):
        """
        Animates the Given algorithm with given Graph

        :param save: Boolean indicating weather output has to be written into output/
        """
        result = self.fn(self.graph)
        for matrix, active in result:
            self.frames.append(matrix)
            self.active.append(active)
        # Draw the original matrix
        if self.pos is None:
            self.pos = nx.nx_pydot.graphviz_layout(self.graph)
        nx.draw_networkx_nodes(self.graph, self.pos, ax=self.ax1, node_color='g', alpha=0.8,
                               node_size=self.node_size).set_edgecolor('k')
        nx.draw_networkx_edges(self.graph, self.pos, ax=self.ax1, alpha=0.6)
        if self.weights:
            nx.draw_networkx_edge_labels(self.graph, self.pos, ax=self.ax1,
                                         edge_labels=nx.get_edge_attributes(self.graph, 'weight'))
        if self.lables:
            nx.draw_networkx_labels(self.graph, self.pos, ax=self.ax1)
        # Draw its adjacancy matrix
        vmin = 0
        vmax = np.max(np.ma.array(self.frames[-1], mask=np.isinf(self.frames[-1])))
        cmap = plt.get_cmap('jet')
        cmap.set_bad('white', 1.)
        masked_array = np.ma.array(self.frames[0], mask=np.isinf(self.frames[0]))
        self.ax2.imshow(masked_array, interpolation='nearest', vmin=vmin, vmax=vmax, alpha=0.7)
        if self.matrix_labels:
            self.__plot_matrix_labels(self.frames[0], self.ax2)
        # Now start the animation
        x = animation.FuncAnimation(self.fig, self.__update, interval=1000, blit=False,
                                    repeat=False, init_func=self.__init_animation, frames=len(self.frames))
        if save:
            import errno
            import os
            path = "output"
            try:
                os.makedirs(path)
            except OSError as exc:
                if exc.errno == errno.EEXIST and os.path.isdir(path):
                    pass
                else:
                    raise
            Writer = animation.writers['ffmpeg']
            writer = Writer(fps=1, metadata=dict(artist='V'), bitrate=1800)
            from multiprocessing import Process
            import os
            path = os.path.join('output', '%s.mp4' % self.fn.__name__)
            Process(target=x.save, args=(path,), kwargs={'writer': writer}).start()
        plt.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号