Demo SerieOfArrays

This notebook focuses on demonstrating how the classes fluiddyn.util.serieofarrays.SeriesOfArrays can be used. This class can be used to create subsets from series of files. Let’s first import it:

from fluiddyn.util.serieofarrays import SeriesOfArrays

This class works with a serie of files (or a file containing a serie of arrays) so we first need to create files. For this demo, we just create emtpy files.

import tempfile
from pathlib import Path
from pprint import pprint
from shutil import rmtree
path_dir = Path(tempfile.mkdtemp('_singleframe'))

for i0 in range(6):
    with open(path_dir / f'image{i0}.png', 'w'):
        pass

print(sorted(p.name for p in path_dir.rglob("*")))
['image0.png', 'image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png']

We write a simple function to print the subsets of files that we are going to create…

def print_subsets(series):
    print(series)
    for serie in series:
        print(serie.get_name_arrays())

We show that we can create many different subsets quite easily:

series = SeriesOfArrays(path_dir, 'i:i+2')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 2)]))
('image0.png', 'image1.png')
('image1.png', 'image2.png')
('image2.png', 'image3.png')
('image3.png', 'image4.png')
('image4.png', 'image5.png')
series = SeriesOfArrays(path_dir, 'i:i+2', ind_step=2)
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 2)]))
('image0.png', 'image1.png')
('image2.png', 'image3.png')
('image4.png', 'image5.png')
series = SeriesOfArrays(path_dir, 'i:i+3', ind_stop=3)
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 3)]))
('image0.png', 'image1.png', 'image2.png')
('image1.png', 'image2.png', 'image3.png')
('image2.png', 'image3.png', 'image4.png')
series = SeriesOfArrays(path_dir, 'i:i+3:2')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 3, 2)]))
('image0.png', 'image2.png')
('image1.png', 'image3.png')
('image2.png', 'image4.png')
('image3.png', 'image5.png')
series = SeriesOfArrays(path_dir, 'pairs')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 2)]))
('image0.png', 'image1.png')
('image1.png', 'image2.png')
('image2.png', 'image3.png')
('image3.png', 'image4.png')
('image4.png', 'image5.png')
series = SeriesOfArrays(path_dir, 'all1by1')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmp2_3lq8ej_singleframe', slicing_tuples=[(0, 1)]))
('image0.png',)
('image1.png',)
('image2.png',)
('image3.png',)
('image4.png',)
('image5.png',)
rmtree(path_dir, ignore_errors=True)

Let’s consider another serie of files this time with two indices:

path_dir = Path(tempfile.mkdtemp('_doubleframe'))

for i0 in range(3):
    for letter in "ab":
        with open(path_dir / f'im_{i0}{letter}.png', 'w'):
            pass

print(sorted(p.name for p in path_dir.rglob("*")))
['im_0a.png', 'im_0b.png', 'im_1a.png', 'im_1b.png', 'im_2a.png', 'im_2b.png']

Creating subsets of files is still very simple:

series = SeriesOfArrays(path_dir, 'i, 0:2')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmpr3ybq0x5_doubleframe', slicing_tuples=[(0, 1), (0, 2)]))
('im_0a.png', 'im_0b.png')
('im_1a.png', 'im_1b.png')
('im_2a.png', 'im_2b.png')
series = SeriesOfArrays(path_dir, '0:2, i')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmpr3ybq0x5_doubleframe', slicing_tuples=[(0, 2), (0, 1)]))
('im_0a.png', 'im_1a.png')
('im_0b.png', 'im_1b.png')
series = SeriesOfArrays(path_dir, 'pairs')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmpr3ybq0x5_doubleframe', slicing_tuples=[(0, 1), (0, 2)]))
('im_0a.png', 'im_0b.png')
('im_1a.png', 'im_1b.png')
('im_2a.png', 'im_2b.png')
series = SeriesOfArrays(path_dir, 'all1by1')
print_subsets(series)
SeriesOfArrays(SerieOfArraysFromFiles('/tmp/tmpr3ybq0x5_doubleframe', slicing_tuples=[(0, 1), (0, 1)]))
('im_0a.png',)
('im_0b.png',)
('im_1a.png',)
('im_1b.png',)
('im_2a.png',)
('im_2b.png',)

Of course we can do many more things with these objects:

pprint([name for name in dir(series) if not name.startswith('__')])
['check_all_arrays_serie_exist',
 'get_name_all_arrays',
 'get_name_all_files',
 'get_next_serie',
 'get_serie_from_index',
 'ind_start',
 'ind_step',
 'ind_stop',
 'index_series',
 'iserie',
 'items',
 'nb_series',
 'serie',
 'set_index_series',
 'slicing_tuples_from_indserie']

Internally, fluiddyn.util.serieofarrays.SeriesOfArrays uses an instance (its attribute serie) of the class fluiddyn.util.serieofarrays.SerieOfArraysFromFiles.

pprint([name for name in dir(series.serie) if not name.startswith('_') and not "index_slices" in name])
['base_name',
 'check_all_arrays_exist',
 'check_all_files_exist',
 'compute_indices_from_name',
 'compute_name_from_indices',
 'compute_str_indices_from_indices',
 'extension_file',
 'filename_given',
 'get_array_from_index',
 'get_array_from_indices',
 'get_array_from_name',
 'get_arrays',
 'get_index_separators',
 'get_indices_from_index',
 'get_name_arrays',
 'get_name_files',
 'get_name_path_arrays',
 'get_nb_arrays',
 'get_nb_files',
 'get_path_all_files',
 'get_path_arrays',
 'get_path_files',
 'get_separator_base_index',
 'get_slicing_tuples',
 'get_slicing_tuples_all_files',
 'get_str_for_name_from_idim_idx',
 'get_tuple_array_name_from_index',
 'get_tuples_indices',
 'isfile',
 'iter_arrays',
 'iter_indices',
 'iter_name_arrays',
 'iter_name_files',
 'iter_path_files',
 'nb_indices',
 'nb_indices_name_file',
 'path_dir',
 'set_slicing_tuples',
 'set_slicing_tuples_from_str']
rmtree(path_dir, ignore_errors=True)