f_ns = [f for f in os.listdir(plane_folder) if f[-14:] == '_corrected.tif']
    f_ns.sort()
    print('\n'.join(f_ns))

    mov_join = []
    for f_n in f_ns:

        curr_mov = tf.imread(os.path.join(plane_folder, f_n))

        if curr_mov.shape[0] % t_downsample_rate != 0:
            print(
                'the frame number of {} ({}) is not divisible by t_downsample_rate ({}).'
                .format(f_n, curr_mov.shape[0], t_downsample_rate))

        curr_mov_d = ia.z_downsample(curr_mov,
                                     downSampleRate=t_downsample_rate)
        mov_join.append(curr_mov_d)

    mov_join = np.concatenate(mov_join, axis=0)
    add_to_mov = 10 - np.amin(mov_join)

    save_name = '{}_d1_{}_d2_{}_d3_1_order_C_frames_{}_.mmap'\
        .format(base_name, mov_join.shape[2], mov_join.shape[1], mov_join.shape[0])

    mov_join = mov_join.reshape(
        (mov_join.shape[0], mov_join.shape[1] * mov_join.shape[2]),
        order='F').transpose()
    mov_join_mmap = np.memmap(os.path.join(plane_folder, save_name),
                              shape=mov_join.shape,
                              order='C',
                              dtype=np.float32,
                                                             td_rate)

            for save_file_id in range(num_file_to_save):
                save_chunk = total_movs[ch_i][save_file_id *
                                              (frames_per_file *
                                               td_rate):(save_file_id + 1) *
                                              (frames_per_file * td_rate)]
                save_path = os.path.join(
                    save_folders[ch_i],
                    '{}_{:05d}_reorged.tif'.format(file_identifier,
                                                   save_ids[ch_i]))
                if td_rate != 1:
                    print('\tdown sampling for {} ...'.format(
                        os.path.split(save_path)[1]))
                    save_chunk = ia.z_downsample(save_chunk,
                                                 downSampleRate=td_rate,
                                                 is_verbose=False)

                print('\tsaving {} ...'.format(os.path.split(save_path)[1]))
                tf.imsave(save_path, save_chunk)
                save_ids[ch_i] = save_ids[ch_i] + 1

            if total_movs[ch_i].shape[0] % (frames_per_file * td_rate) == 0:
                total_movs[ch_i] = None
            else:
                frame_num_left = total_movs[ch_i].shape[0] % (frames_per_file *
                                                              td_rate)
                total_movs[ch_i] = total_movs[ch_i][-frame_num_left:]

print('\nprocessing residual frames ...')
Ejemplo n.º 3
0
file_id_save = 0
total_mov = None
base_name = '_'.join(file_list[0].split('_')[:-1])
save_folder = os.path.join(data_folder, 'reorged')
if not os.path.isdir(save_folder):
    os.makedirs(save_folder)

for file_path in file_paths:
    print('\nprocessing {} ...'.format(os.path.split(file_path)[1]))

    curr_mov = tf.imread(file_path)
    curr_mov = curr_mov.transpose((0, 2, 1))[:, ::-1, :]

    if temporal_downsample_rate != 1:
        curr_mov = ia.z_downsample(curr_mov,
                                   downSampleRate=temporal_downsample_rate)

    if total_mov is None:
        total_mov = curr_mov
    else:
        total_mov = np.concatenate((total_mov, curr_mov), axis=0)

    while (total_mov is not None) and (total_mov.shape[0] >= frames_per_file):

        num_file_to_save = total_mov.shape[0] // frames_per_file

        for save_file_id in range(num_file_to_save):
            save_chunk = total_mov[save_file_id *
                                   frames_per_file:(save_file_id + 1) *
                                   frames_per_file]
            save_path = os.path.join(
    f_ns.sort()
    print('\n'.join(f_ns))

    mov_join = []
    for f_n in f_ns:
        print('processing plane: {}; file: {} ...'.format(plane_n, f_n))

        curr_mov = tf.imread(os.path.join(plane_folder, f_n))

        if curr_mov.shape[0] % t_downsample_rate != 0:
            print(
                'the frame number of {} ({}) is not divisible by t_downsample_rate ({}).'
                .format(f_n, curr_mov.shape[0], t_downsample_rate))

        curr_mov_d = ia.z_downsample(curr_mov,
                                     downSampleRate=t_downsample_rate,
                                     is_verbose=False)
        mov_join.append(curr_mov_d)

    mov_join = np.concatenate(mov_join, axis=0)
    add_to_mov = 10 - np.amin(mov_join)

    save_name = '{}_d1_{}_d2_{}_d3_1_order_C_frames_{}_.mmap'\
        .format(base_name, mov_join.shape[2], mov_join.shape[1], mov_join.shape[0])

    mov_join = mov_join.reshape(
        (mov_join.shape[0], mov_join.shape[1] * mov_join.shape[2]),
        order='F').transpose()
    mov_join_mmap = np.memmap(os.path.join(plane_folder, save_name),
                              shape=mov_join.shape,
                              order='C',
Ejemplo n.º 5
0
def downsample_folder(working_folder,
                      td_rate,
                      file_identifier,
                      frames_per_file=500):

    file_list = [
        f for f in os.listdir(working_folder)
        if file_identifier in f and f[-14:] == '_corrected.tif'
    ]
    file_list.sort()
    print('\t\tall files:')
    print '\n'.join(['\t\t' + f for f in file_list])

    print('\n\t\tmoving files to "not_downsampled" folder:')
    file_paths = [os.path.join(working_folder, f) for f in file_list]
    print

    not_downsampled_folder = os.path.join(working_folder, 'not_downsampled')
    os.mkdir(not_downsampled_folder)
    for file_path in file_paths:
        fn = os.path.split(file_path)[1]
        shutil.move(file_path, os.path.join(not_downsampled_folder, fn))

    file_paths_original = [
        os.path.join(not_downsampled_folder, fn) for fn in file_list
    ]
    file_paths_original.sort()

    save_id = 0
    total_mov = None
    for file_path_o in file_paths_original:
        print('\t\tprocessing {} ...'.format(os.path.split(file_path_o)[1]))
        curr_mov = tf.imread(file_path_o)

        if total_mov is None:
            total_mov = curr_mov
        else:
            total_mov = np.concatenate((total_mov, curr_mov), axis=0)

        while total_mov is not None and \
                (total_mov.shape[0] >= frames_per_file * td_rate):

            num_file_to_save = total_mov.shape[0] // (frames_per_file *
                                                      td_rate)

            for save_file_id in range(num_file_to_save):
                save_chunk = total_mov[save_file_id *
                                       (frames_per_file *
                                        td_rate):(save_file_id + 1) *
                                       (frames_per_file * td_rate)]
                save_path = os.path.join(
                    working_folder,
                    '{}_{:05d}_corrected_downsampled.tif'.format(
                        file_identifier, save_id))
                save_chunk = ia.z_downsample(save_chunk,
                                             downSampleRate=td_rate,
                                             is_verbose=False)

                print('\t\t\tsaving {} ...'.format(
                    os.path.split(save_path)[1]))
                tf.imsave(save_path, save_chunk)
                save_id = save_id + 1

            if total_mov.shape[0] % (frames_per_file * td_rate) == 0:
                total_mov = None
            else:
                frame_num_left = total_mov.shape[0] % (frames_per_file *
                                                       td_rate)
                total_mov = total_mov[-frame_num_left:]

    if total_mov is not None:
        save_path = os.path.join(
            working_folder, '{}_{:05d}_corrected_downsampled.tif'.format(
                file_identifier, save_id))
        save_chunk = ia.z_downsample(total_mov,
                                     downSampleRate=td_rate,
                                     is_verbose=False)
        print('\t\t\tsaving {} ...'.format(os.path.split(save_path)[1]))
        tf.imsave(save_path, save_chunk)

    return
Ejemplo n.º 6
0
import os
import tifffile as tf
import corticalmapping.core.ImageAnalysis as ia

xy_downsample_rate = 2
t_downsample_rate = 10

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

f_ns = [
    f for f in os.listdir(curr_folder)
    if f[-4:] == '.tif' and 'downsampled' not in f
]
f_ns.sort()
print('\n'.join(f_ns))

for f_n in f_ns:
    print('processing {} ...'.format(f_n))
    mov = tf.imread(f_n)
    mov_d = ia.rigid_transform_cv2(img=mov, zoom=(1. / xy_downsample_rate))
    mov_d = ia.z_downsample(mov_d, downSampleRate=t_downsample_rate)
    save_n = os.path.splitext(f_n)[0] + '_downsampled.tif'
    tf.imsave(save_n, mov_d)