如何加快根据数据库结果制作的matplotlib动画?

时间:2018-10-29 14:07:38

标签: python matplotlib sqlalchemy

我正在使用matplotlib对我存储在数据库中的某些地图进行轮廓图绘制,但是制作每部电影要花费几个小时。我用来制作这部电影的代码是:

def load_img(option, obs_id, columnshape):
    '''
    This function will load the image
    from the database and make the 
    conversion from string to nparray
    '''
    #starting a db session
    session = makesession()

    #defining the cases to query the db
    case = {'Bz': Observations.mean_bz,
            'Es': Observations.poyn_Es,
            'En': Observations.poyn_En,
            'Et': Observations.poyn_Et}

    #checking if the option selected was
    #a valid one
    while option not in case.keys():
        #feedback
        print('Invalid option. Select a valid one: ', case.keys())
        option = str(input())

    #querying the db
    s = sql.select([case[option]]).where(Observations.id == obs_id)

    #fetching the result
    rp = session.execute(s)
    result = rp.fetchone()

    #restoring the image
    img = ajuste(result[0],columnshape)

    return(img)

def db_animation(ar_id, option):
    '''
    Description
    '''
    vmin = -1e17
    vmax = 1e17
    levels = [vmin, 0.8*vmin, 0.6*vmin, 0.4*vmin, 0.2*vmin, 
              0.2*vmax, 0.4*vmax, 0.6*vmax,0.8*vmax,vmax]

    #getting the observation ids
    obs_ids = scout_obs_ids(ar_id)
    #obs_ids = [x for x in range(400,450)]

    #getting the columnshape
    columnshape = scout_colshape(obs_ids[0])

    #creating the figure objects
    fig, ax = plt.subplots(figsize = (12,8))

    #loading the first data
    data_bz = load_img('Bz', obs_ids[0], columnshape)
    data_E = load_img(option, obs_ids[0], columnshape)

    #making the image objects
    img1 = ax.imshow(data_bz, origin = 'lower', cmap = plt.cm.gray, 
                     animated = True)

    img2 = [ax.contourf(data_E, alpha = 0.35, 
                        #vmax = 1e17, vmin = -1e17,
                        levels = levels,
                        origin = 'lower',
                        cmap = 'PiYG')]

    #adding a colorbar
    fig.colorbar(img2[0], shrink = 0.75, label = 'W')


    def refresher(frame_number, img1,img2):
        '''
        description
        '''
        #taking the new data
        new_data_bz = load_img('Bz', obs_id = obs_ids[frame_number+1],
                               columnshape = columnshape)
        new_data_E = load_img(option, obs_id =  obs_ids[frame_number+1],
                              columnshape = columnshape)

        #setting the new data
        img1.set_data(new_data_bz)

        #removing the contours to start anew
        for tp in img2[0].collections:
            tp.remove()

        img2[0] = ax.contourf(new_data_E, alpha = 0.35, 
                              levels = levels,
                              origin = 'lower', cmap = 'PiYG')

        return(img1, img2[0].collections,)

    #using the animation function
    ani = FuncAnimation(fig, refresher,
                        frames=range(len(obs_ids)-1),
                        interval = 100,
                        #blit = True,
                        fargs = [img1,img2])

    #saving
    ani.save("test.mp4")

    return

这些电影平均为数据库中的每个img对象拍摄1200张图像(总计约2400张)。每对图像分别进行加载和还原,以制作背景图像和轮廓图。

我想知道为什么随着我增加制作电影的图像数量而导致处理时间迅速增加的原因,但我自己无法得出结论。我特别感兴趣的是,当我将blit设置为True(根据documentation可以帮助提高性能)时,出现以下错误:

AttributeError: 'silent_list' object has no attribute 'set_animated'

我想我的查询或构造动画功能的方式都效率很低。但是我对后者有更多的怀疑,因为当我通常使用数据库时,结果会在我认为是合理的时间内加载。

有人可以为我在这场斗争中有所作为吗?

0 个答案:

没有答案