Beispiel #1
0
def expand_cell_ranges(range_string):
    """
    Expand cell ranges to a sequence of addresses.
    Reverse of collapse_cell_addresses
    Eg. converts "A1:A2 B1:B2" to (A1, A2, B1, B2)
    """
    cells = []
    for rs in range_string.split():
        cells.extend(cells_from_range(rs))
    return list(chain.from_iterable(cells))
Beispiel #2
0
def expand_cell_ranges(range_string):
    """
    Expand cell ranges to a sequence of addresses.
    Reverse of collapse_cell_addresses
    Eg. converts "A1:A2 B1:B2" to (A1, A2, B1, B2)
    """
    cells = []
    for rs in range_string.split():
        cells.extend(cells_from_range(rs))
    return list(chain.from_iterable(cells))
Beispiel #3
0
   os.path.basename(xlsx_file))
 
 x = np.array([0, 10, 20, 30, 40, 50])
 y = np.array([0, 10, 20, 30, 40])
 
 if smoothing:
     x = scipy.ndimage.zoom(x, smoothing)
     y = scipy.ndimage.zoom(y, smoothing)
 
 T = []
 total_frames = fps * record_seconds
 
 for column_range in cells_range:
     t = []
     
     for c in cells_from_range(column_range):
         t.append(ws.cell(c[0]).value)
     
     t = np.array(t).reshape(5,6)
     if smoothing: t = scipy.ndimage.zoom(t, smoothing)
     T.append(t)
 
 T = np.array(T)
 print T.shape
 N = total_frames // len(T)
 #print N
 #print T.shape
 tot_T =  scipy.ndimage.zoom(T, [N,1,1])
 #print scipy.ndimage.zoom(T, 500).shape
 #print scipy.ndimage.zoom(T[0:2], total_frames/(1.*len(T[0]))).shape
 #print total_frames
def create_animation(xlsx_file,
                     sheet_name,
                     cells_range,
                     title,
                     smoothing=10,
                     fps=24,
                     rectime_secs=60):
    ur'''Створює анімацію
    version: 0.1.0
    args:
    - xlsx_file(basestring) - назва XLSX-файлу;
    - sheet_name(basestring, int) - назва або індекс аркуша в робочій книзі;
    - cells_range(tuple) - діапазон клітинок, де розміщуються дані (тіло таблиці 
      без боковиків);
    - title(basestring) - назва графіку;
    - smoothing(int) - кількість рівнів згладжування;
    - fps(int) - частота кадрів (кількість кадрів за секунду);
    - rectime_secs(int) - тривалість створеного відео.
    '''

    #try:
    #   shutil.rmtree(dirout_with_file, ignore_errors=False, onerror=None)
    #except WindowsError:
    #   print u'''Якийсь процес використовує файли в папці "%s".
    #Закрийте всі программи, в яких відкриті або використовуються файли в папці, і \
    #повторите попытку!''' % os.path.basename(dirout_with_file).encode('utf-8'))
    #       continue

    # Завантаження даних з XLSX-аркуша.
    wb = load_workbook(xlsx_file)

    # Визначення робочого аркуша по індексу або імені.
    ws = wb.get_sheet_by_name(wb.get_sheet_names(
    )[sheet_name] if isinstance(sheet_name, int) else sheet_name)

    #print ws.title
    #print os.path.basename(xlsx_file).encode('cp866')
    print u'Відкриваю аркуш "%s" робочої книги "%s"' % (
        ws.title, os.path.basename(xlsx_file))

    # Масив осей.
    x = np.array([0, 10, 20, 30, 40, 50])
    y = np.array([0, 10, 20, 30, 40])

    # Розширення масиву в smoothing раз в кожному напрямку завдяки інтерполяції
    # проміжних значень.
    if smoothing:
        x = scipy.ndimage.zoom(x, smoothing)
        y = scipy.ndimage.zoom(y, smoothing)

    T = []
    # Загальна кількість кадрів за всю тривалість відео.
    total_frames = fps * rectime_secs

    for column_range in cells_range:
        t = []

        for c in cells_from_range(column_range):
            t.append(ws.cell(c[0]).value)

        # Переформатування масиву.
        t = np.array(t).reshape(5, 6)
        # Розширення матриці в smoothing раз в кожному напрямку завдяки
        # інтерполяції проміжних значень.
        if smoothing: t = scipy.ndimage.zoom(t, smoothing)
        T.append(t)

    T = np.array(T)
    print T.shape
    N = total_frames // len(T)
    #print N
    #print T.shape
    tot_T = scipy.ndimage.zoom(T, [N, 1, 1])
    #print scipy.ndimage.zoom(T, 500).shape
    #print scipy.ndimage.zoom(T[0:2], total_frames/(1.*len(T[0]))).shape
    #print total_frames
    #T = scipy.ndimage.zoom(T, total_frames)
    #print T.shape

    y = y[::-1]

    print u'Всього створюється %d кадрiв формою %dx%d точок...' \
      % (len(tot_T), T[0].shape[0], T[0].shape[1])

    progress_char = '#'
    progress_width = 50

    fig = plt.figure()
    fig.hold(False)

    print u'Запуск процесу...'

    for i, T_i in enumerate(tot_T):
        fig.clear()
        frame_i = i + 1
        #CS = plt.contour(x, y, t)

        #CS = plt.contour(X, Y, T, 15, nchunk=5, linewidths=1.5)
        #plt.clabel(CS, inline=True, fmt='%.1f')
        plt.imshow(T_i,
                   interpolation='bilinear',
                   origin='upper',
                   extent=(0, 50, 40, 0))
        fig.savefig(os.path.join(workdir, 'fig_img%d.png' % frame_i),
                    format='png')
        #plt.title(title, fontsize=14)

        fraction = round(frame_i / len(tot_T), 2)
        progress_line = progress_char * int(round(fraction * progress_width,
                                                  0))

        progress_line = u'Створено %d кадрiв: %s' % (
            frame_i, '%s %d%%' % (progress_line, 100 * fraction))
        sys.stdout.write(progress_line)
        sys.stdout.flush()
        sys.stdout.write('\b' * len(progress_line))

    print u'Процес завершено!'
Beispiel #5
0
def gen_animation(xlsx_file,
                  sheet_name,
                  cells_range,
                  title,
                  smoothing=10,
                  fps=24,
                  record_seconds=60):

    #try:
    #   shutil.rmtree(dirout_with_file, ignore_errors=False, onerror=None)
    #except WindowsError:
    #   print u'''Якийсь процес використовує файли в папці "%s".
    #Закрийте всі программи, в яких відкриті або використовуються файли в папці, і \
    #повторите попытку!''' % os.path.basename(dirout_with_file).encode('utf-8'))
    #       continue

    wb = load_workbook(xlsx_file)

    ws = wb.get_sheet_by_name(wb.get_sheet_names(
    )[sheet_name] if isinstance(sheet_name, int) else sheet_name)

    #print ws.title
    #print os.path.basename(xlsx_file).encode('cp866')
    print u'Відкриваю аркуш "%s" робочої книги "%s"' % (
        ws.title, os.path.basename(xlsx_file))

    x = np.array([0, 10, 20, 30, 40, 50])
    y = np.array([0, 10, 20, 30, 40])

    if smoothing:
        x = scipy.ndimage.zoom(x, smoothing)
        y = scipy.ndimage.zoom(y, smoothing)

    T = []
    total_frames = fps * record_seconds

    for column_range in cells_range:
        t = []

        for c in cells_from_range(column_range):
            t.append(ws.cell(c[0]).value)

        t = np.array(t).reshape(5, 6)
        if smoothing: t = scipy.ndimage.zoom(t, smoothing)
        T.append(t)

    T = np.array(T)
    print T.shape
    N = total_frames // len(T)
    #print N
    #print T.shape
    tot_T = scipy.ndimage.zoom(T, [N, 1, 1])
    #print scipy.ndimage.zoom(T, 500).shape
    #print scipy.ndimage.zoom(T[0:2], total_frames/(1.*len(T[0]))).shape
    #print total_frames
    #T = scipy.ndimage.zoom(T, total_frames)
    #print T.shape

    y = y[::-1]

    print u'Всього створюється %d кадрiв формою %dx%d точок...' \
      % (len(tot_T), T[0].shape[0], T[0].shape[1])

    progress_char = '#'
    progress_width = 50

    fig = plt.figure()
    fig.hold(False)

    print u'Запуск процесу...'

    for i, T_i in enumerate(tot_T):
        fig.clear()
        frame_i = i + 1
        #CS = plt.contour(x, y, t)

        #CS = plt.contour(X, Y, T, 15, nchunk=5, linewidths=1.5)
        #plt.clabel(CS, inline=True, fmt='%.1f')
        plt.imshow(T_i,
                   interpolation='bilinear',
                   origin='upper',
                   extent=(0, 50, 40, 0))
        fig.savefig(os.path.join(workdir, 'fig_img%d.png' % frame_i),
                    format='png')
        #plt.title(title, fontsize=14)

        fraction = round(frame_i / len(tot_T), 2)
        progress_line = progress_char * int(round(fraction * progress_width,
                                                  0))

        progress_line = u'Створено %d кадрiв: %s' % (
            frame_i, '%s %d%%' % (progress_line, 100 * fraction))
        sys.stdout.write(progress_line)
        sys.stdout.flush()
        sys.stdout.write('\b' * len(progress_line))

    print u'Процес завершено!'
Beispiel #6
0
def create_contour(xlsx_file, sheet_name, cells_range, title, smoothing=10):
    ur'''Створює анімацію
    version: 0.1.0
    args:
    - xlsx_file(basestring) - назва XLSX-файлу;
    - sheet_name(basestring, int) - назва або індекс аркуша в робочій книзі;
    - cells_range(tuple) - діапазон клітинок, де розміщуються дані (тіло таблиці 
      без боковиків);
    - title(basestring) - назва графіку;
    - smoothing(int) - кількість рівнів згладжування.
    '''
    
    #try:
     #   shutil.rmtree(dirout_with_file, ignore_errors=False, onerror=None)
    #except WindowsError:
     #   print u'''Якийсь процес використовує файли в папці "%s". 
#Закрийте всі программи, в яких відкриті або використовуються файли в папці, і \
#повторите попытку!''' % os.path.basename(dirout_with_file).encode('utf-8'))
 #       continue
    
    # Завантаження даних з XLSX-аркуша.
    wb = load_workbook(xlsx_file)
    
    # Визначення робочого аркуша по індексу або імені.
    ws = wb.get_sheet_by_name(wb.get_sheet_names()[sheet_name] if isinstance(
      sheet_name, int) else sheet_name)
    
    #print ws.title
    #print os.path.basename(xlsx_file).encode('cp866')
    #print u'Відкриваю аркуш "%s" робочої книги "%s"' % (ws.title, 
     #                                os.path.basename(xlsx_file))
    
    # Масив осей.
    x = np.array([0, 10, 20, 30, 40, 50])
    y = np.array([0, 10, 20, 30, 40])
    
    # Розширення масиву в smoothing раз в кожному напрямку завдяки інтерполяції 
    # проміжних значень.
    if smoothing:
        x = scipy.ndimage.zoom(x, smoothing, order=3)
        y = scipy.ndimage.zoom(y, smoothing, order=3)
    
    T = []
    
    for column_range in cells_range:
        t = []
        
        for c in cells_from_range(column_range):
            t.append(float(ws.cell(c[0]).value))
        
        # Переформатування масиву.
        t = np.array(t).reshape(5, 6)
        # Розширення матриці в smoothing раз в кожному напрямку завдяки 
        # інтерполяції проміжних значень.
        if smoothing: t = scipy.ndimage.zoom(t, smoothing, order=3)
       # T.append(t)
    
    #T = np.array(T)
    #print T.shape
    #print N
    #print T.shape
    #print scipy.ndimage.zoom(T, 500).shape
    #print scipy.ndimage.zoom(T[0:2], total_frames/(1.*len(T[0]))).shape
    #print total_frames
    #T = scipy.ndimage.zoom(T, total_frames)
    #print T.shape
    
    #y = y[: : -1]
    
    #fig = plt.figure()
    #fig.hold(False)
    CS = plt.contour(x, y, t, 15, nchunk=5, linewidths=1.5)
    #plt.clabel(CS, inline=True, fmt='%.1f')
    plt.show()
def create_animation(xlsx_file,
                     sheet_name,
                     cells_range,
                     total_time,
                     main_title,
                     lower_title,
                     smoothing=10,
                     fps=24,
                     rectime_secs=60,
                     is_grid=False,
                     is_contour=False):
    ur'''Створює анімацію
    version: 0.1.0
    args:
    - xlsx_file(basestring) - назва XLSX-файлу;
    - sheet_name(basestring, int) - назва або індекс аркуша в робочій книзі;
    - cells_range(tuple) - діапазон клітинок, де розміщуються дані (тіло таблиці 
      без боковиків);
    - title(basestring) - назва графіку;
    - smoothing(int) - кількість рівнів згладжування;
    - fps(int) - частота кадрів (кількість кадрів за секунду);
    - rectime_secs(int) - тривалість створеного відео.
    '''

    #try:
    #   shutil.rmtree(dirout_with_file, ignore_errors=False, onerror=None)
    #except WindowsError:
    #   print u'''Якийсь процес використовує файли в папці "%s".
    #Закрийте всі программи, в яких відкриті або використовуються файли в папці, і \
    #повторите попытку!''' % os.path.basename(dirout_with_file).encode('utf-8'))
    #       continue

    # Завантаження даних з XLSX-аркуша.
    wb = load_workbook(xlsx_file)

    # Визначення робочого аркуша по індексу або імені.
    ws = wb.get_sheet_by_name(wb.get_sheet_names(
    )[sheet_name] if isinstance(sheet_name, int) else sheet_name)

    #print ws.title
    #print os.path.basename(xlsx_file).encode('cp866')
    print u'Відкриваю аркуш "%s" робочої книги "%s"' % (
        ws.title, os.path.basename(xlsx_file))

    # Масив осей.
    x = np.array([0, 10, 20, 30, 40, 50])
    y = np.array([0, 10, 20, 30, 40])

    def extvec(vector, di=1):
        new_vector = []
        previous_value = vector[0]

        for value in vector[1:]:
            new_vector += np.linspace(previous_value, value, di,
                                      False).tolist()
            previous_value = value

        new_vector.append(value)

        return np.array(new_vector)

    X = extvec(x)
    Y = extvec(y)

    # Розширення масиву в smoothing раз в кожному напрямку завдяки інтерполяції
    # проміжних значень.
    if smoothing:
        X = scipy.ndimage.zoom(X, smoothing)
        Y = scipy.ndimage.zoom(Y, smoothing)

    #X = []
    #Y = []
    T = []
    # Загальна кількість кадрів за всю тривалість відео.
    total_frames = fps * rectime_secs

    for column_range in cells_range:
        t = []

        for c in cells_from_range(column_range):
            t.append(ws.cell(c[0]).value)

        # Переформатування масиву.
        t = np.array(t).reshape(5, 6)
        # Розширення матриці в smoothing раз в кожному напрямку завдяки
        # інтерполяції проміжних значень.
        if smoothing:
            t = scipy.ndimage.zoom(t, smoothing)
        T.append(t)

    T = np.array(T)
    #print T.shape
    N = total_frames // len(T)
    #print N
    #print T.shape
    tot_T = scipy.ndimage.zoom(T, [N, 1, 1])
    #print scipy.ndimage.zoom(T, 500).shape
    #print scipy.ndimage.zoom(T[0:2], total_frames/(1.*len(T[0]))).shape
    #print total_frames
    #T = scipy.ndimage.zoom(T, total_frames)
    #print T.shape

    Y = Y[::-1]

    frame_fname_format = '%0' + str(len(str(len(tot_T)))) + 'd.png'
    print u'Всього створюється %d кадрiв формою %dx%d точок...' \
      % (len(tot_T), T[0].shape[0], T[0].shape[1])

    progress_char = '#'
    progress_width = 50

    fig = plt.figure(**fig_data)
    #fig.hold(False)

    font = lambda fs: mpl.font_manager.FontProperties(family='Times New Roman',
                                                      style='normal',
                                                      size=fs,
                                                      weight='normal',
                                                      stretch='normal')

    T_min = tot_T.min()
    T_max = tot_T.max()
    cb_k = 5
    cb_value_min = divmod(T_min, cb_k)[0] * cb_k
    cb_value_max = divmod(T_max, cb_k)[0] * (cb_k + 1)
    cb_ticks = np.arange(cb_value_min, cb_value_max + cb_k, cb_k)
    norm = mpl.colors.Normalize(vmin=cb_value_min, vmax=cb_value_max)

    print u'Запуск процесу...'

    for i, T_i in enumerate(tot_T):
        fig.clear()
        try:
            del ax
        except UnboundLocalError:
            pass
        ax = fig.add_subplot(111)
        ax.figure.subplots_adjust(left=.01, bottom=.16, top=.92, right=.92)

        frame_i = i + 1
        #CS = plt.contour(x, y, t)

        #CS = plt.contour(X, Y, T, 15, nchunk=5, linewidths=1.5)
        #plt.clabel(CS, inline=True, fmt='%.1f')

        if is_contour:
            CS = plt.contour(X, Y, T_i, linewidths=1.5,
                             colors='k')  #, nchunk=5
            fmt = dict()
            for lbl in CS.levels:
                fmt[lbl] = (u'{:.2f}'.format(lbl)).replace('.', ',')
            ax.clabel(CS, CS.levels, inline=True, fmt=fmt)
            for cont_label in CS.labelTexts:
                # Всі надписи - чорні.
                #if discolor: cont_label.set_color('k')

                cont_label.set_fontproperties(font(14))

        im = ax.imshow(T_i,
                       interpolation='bilinear',
                       origin='upper',
                       extent=(0, 50, 0, 40),
                       cmap='jet')

        ax1 = fig.add_axes([0.9, 0.16, 0.025, 0.92 - .16])
        cb = mpl.colorbar.ColorbarBase(ax1,
                                       cmap='jet',
                                       norm=norm,
                                       orientation='vertical')
        cb.set_label(ur'Температура на поверхні, ${}^{\mathrm{\circ}}$C',
                     fontproperties=font(14))
        #cb = fig.colorbar(im, anchor=(.7, .35), ticks=cb_ticks)
        #cb.set_clim(cb_T_min, cb_T_max)
        #cb.ax.set_yticks(cb_ticks)
        #cb.ax.set_yticklabels([str(x) for x in cb_ticks])#(cb_T_min, cb_T_max)
        #plt.title(title, fontsize=14)
        # Назва графіку.
        title = ax.set_title(main_title, ha='center', fontproperties=font(14))
        title.set_position([title.get_position()[0], -0.125])

        # Нижня назва графіку.
        lwr_title = ax.text(title.get_position()[0],
                            -.15,
                            lower_title,
                            ha='center',
                            va='top',
                            fontproperties=font(14),
                            transform=ax.transAxes)

        ax.set_xticklabels(['0', '0,1', '0,2', '0,3', '0,4', '0,5'],
                           fontproperties=font(14))
        ax.set_yticks([0, 10, 20, 30, 40])
        ax.set_yticklabels(['0', '0,1', '0,2', '0,3', '0,4'],
                           fontproperties=font(14))
        # Мітка осі x.
        xl = ax.set_xlabel(u'a, м',
                           ha='left',
                           va='top',
                           fontproperties=font(14))
        ticklab = ax.xaxis.get_ticklabels()[0]
        trans = ticklab.get_transform()
        ax.xaxis.set_label_coords(52, 0, transform=trans)

        yl = ax.set_ylabel(u'b, м',
                           rotation=0,
                           ha='right',
                           va='bottom',
                           fontproperties=font(14))
        ticklab = ax.yaxis.get_ticklabels()[1]
        trans = ticklab.get_transform()
        ax.yaxis.set_label_coords(0, 42, transform=trans)

        al = 7  # arrow length in points
        arrowprops = dict(  #clip_on=False, # plotting outside axes on purpose
            frac=1.,  # make end arrowhead the whole size of arrow
            headwidth=al,  # in points
            facecolor='g')
        kwargs = dict(xycoords='axes fraction',
                      textcoords='axes fraction',
                      arrowprops=dict(arrowstyle="->"),
                      size=20.)

        # Розміщення міток підписів осей.
        ax.annotate("", xy=(1.085, 0), xytext=(1, 0),
                    **kwargs)  # bottom spine arrow
        ax.annotate("", (0, 1.085), xytext=(0, 1), **kwargs)  # left spin arrow

        # Видимість сітки.
        if is_grid:
            ax.grid(b=True, color='#00ff00', linestyle='-.', linewidth=.5)
        else:
            ax.grid(False)

        fig.text(.5,
                 .93,
                 ur'Пройдений час: $\mathrm{{\tau}}$ = {}'.format(
                     timedelta(seconds=round((i + 1) / fps * total_time * 60 /
                                             rectime_secs))),
                 fontweight='bold',
                 fontproperties=font(14),
                 ha='center',
                 va='bottom',
                 transform=ax.transAxes)

        plt.savefig(os.path.join(workdir, frame_fname_format % frame_i),
                    dpi=fig_data['dpi'],
                    format='png')
        # Очищає пам'ять.
        ##ax.figure.clf()
        ##plt.close()
        ##gc.collect()

        fraction = round(frame_i / len(tot_T), 2)
        progress_line = progress_char * int(round(fraction * progress_width,
                                                  0))

        progress_line = u'Створено %d кадрiв: %s' % (
            frame_i, '%s %d%%' % (progress_line, 100 * fraction))
        sys.stdout.write(progress_line)
        sys.stdout.flush()
        sys.stdout.write('\b' * len(progress_line))

    sys.stdout.flush()
    video_settings = {
        'vcodec': 'mpeg4',
        'vtag': 'xvid',
        'vqscale': 3,
        'acodec': 'libmp3lame',
        'aqscale': 4,
        'container': 'avi'
    }
    print u'Створення відео з налаштуваннями {}: ...'.format(', '.join(
        '{} = {}'.format(key, value) for key, value in video_settings.items()))
    path_ffmpeg = ur'ffmpeg-20180424-d9706f7-win64-static\bin\ffmpeg.exe'
    files_imresc = os.path.join(workdir, frame_fname_format)
    video_file = os.path.join(workdir,
                              'animation.' + video_settings['container'])

    try:
        os.remove(video_file)
    except WindowsError:
        pass

    command = (ur'{ffmpeg} -framerate {framerate} -i {files_imresc} '
               ur'-c:v {vcodec} -vtag {vtag} -qscale:v {vqscale} '
               ur'-c:a {acodec} -qscale:a {aqscale} {video_file}'.format(
                   ffmpeg=path_ffmpeg,
                   framerate=fps,
                   files_imresc=files_imresc,
                   video_file=video_file,
                   **video_settings))
    process = subprocess.Popen(command,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               bufsize=1,
                               universal_newlines=True)
    while True:
        line = process.stdout.readline()
        if line != b'':
            os.write(1, line)
        else:
            break

    sys.stdout.flush()
    print u'Процес завершено!'