Exemplo n.º 1
0
def combine_images(fol, movie_name, frame_rate=10, start_number=-1, images_prefix='', images_format='',
                            images_type='', ffmpeg_cmd='ffmpeg', **kwargs):
    if images_type == '':
        images_types = set([utils.file_type(image) for image in glob.glob(op.join(fol, '{}*.*'.format(images_prefix)))])
        for opt_type in ['png', 'jpg', 'bmp', 'gif']:
            if opt_type in images_types:
                images_type = opt_type
                print('Images type is {}'.format(images_type))
                break
        if images_type == '':
            raise Exception("Can't find the images type!")
    images = glob.glob(op.join(fol, '{}*.{}'.format(images_prefix, images_type)))
    image_nb = utils.namebase(images[0])
    number = utils.read_numbers_rx(image_nb)[0]
    if images_prefix == '':
        images_prefix = image_nb[:-len(number)]
    if images_format == '':
        images_format = '%0{}d'.format(len(number))
    if start_number == -1:
        start_number = min([int(utils.namebase(image)[len(images_prefix):]) for image in images])
    images_prefix = op.join(fol, images_prefix)
    movie_name = op.join(fol, movie_name)
    combine_images_cmd = '{ffmpeg_cmd} -framerate {frame_rate} '
    if start_number > 1:
        # You might want to use a static ffmpeg if your ffmepg version doesn't support the start_number flag, like:
        # ffmpeg_cmd = '~/space1/Downloads/ffmpeg-git-static/ffmpeg'
        combine_images_cmd += '-start_number {start_number} '
    combine_images_cmd += '-i {images_prefix}{images_format}.{images_type} '
    # http://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    combine_images_cmd += '-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" '
    combine_images_cmd += '-c:v libx264 -r 30 -pix_fmt yuv420p {movie_name}.mp4'
    rs = utils.partial_run_script(locals())
    rs(combine_images_cmd)
Exemplo n.º 2
0
def init_clusters(subject, contrast_name, input_fol):
    input_fname = op.join(input_fol,
                          'fmri_{}_{}.npy'.format(contrast_name, '{hemi}'))
    contrast_per_hemi, verts_per_hemi = {}, {}
    for hemi in utils.HEMIS:
        fmri_fname = input_fname.format(hemi=hemi)
        if utils.file_type(input_fname) == 'npy':
            x = np.load(fmri_fname)
            contrast_per_hemi[hemi] = x[:, 0]
        else:
            # try nibabel
            x = nib.load(fmri_fname)
            contrast_per_hemi[hemi] = x.get_data().ravel()
        pial_npz_fname = op.join(BLENDER_ROOT_DIR, subject,
                                 '{}.pial.npz'.format(hemi))
        if not op.isfile(pial_npz_fname):
            print('No pial npz file (), creating one'.format(pial_npz_fname))
            verts, faces = utils.read_ply_file(
                op.join(BLENDER_ROOT_DIR, subject, '{}.pial.ply'.format(hemi)))
            np.savez(pial_npz_fname[:-4], verts=verts, faces=faces)
        d = np.load(pial_npz_fname)
        verts_per_hemi[hemi] = d['verts']
    connectivity_fname = op.join(BLENDER_ROOT_DIR, subject,
                                 'spatial_connectivity.pkl')
    if not op.isfile(connectivity_fname):
        from src.preproc import anatomy_preproc
        anatomy_preproc.create_spatial_connectivity(subject)
    connectivity_per_hemi = utils.load(connectivity_fname)
    return contrast_per_hemi, connectivity_per_hemi, verts_per_hemi
Exemplo n.º 3
0
def init_clusters(subject, contrast_name, input_fol):
    input_fname = op.join(input_fol, 'fmri_{}_{}.npy'.format(contrast_name, '{hemi}'))
    contrast_per_hemi, verts_per_hemi = {}, {}
    for hemi in utils.HEMIS:
        fmri_fname = input_fname.format(hemi=hemi)
        if utils.file_type(input_fname) == 'npy':
            x = np.load(fmri_fname)
            contrast_per_hemi[hemi] = x[:, 0]
        else:
            # try nibabel
            x = nib.load(fmri_fname)
            contrast_per_hemi[hemi] = x.get_data().ravel()
        pial_npz_fname = op.join(BLENDER_ROOT_DIR, subject, '{}.pial.npz'.format(hemi))
        if not op.isfile(pial_npz_fname):
            print('No pial npz file (), creating one'.format(pial_npz_fname))
            verts, faces = utils.read_ply_file(op.join(BLENDER_ROOT_DIR, subject, '{}.pial.ply'.format(hemi)))
            np.savez(pial_npz_fname[:-4], verts=verts, faces=faces)
        d = np.load(pial_npz_fname)
        verts_per_hemi[hemi] = d['verts']
    connectivity_fname = op.join(BLENDER_ROOT_DIR, subject, 'spatial_connectivity.pkl')
    if not op.isfile(connectivity_fname):
        from src.preproc import anatomy_preproc
        anatomy_preproc.create_spatial_connectivity(subject)
    connectivity_per_hemi = utils.load(connectivity_fname)
    return contrast_per_hemi, connectivity_per_hemi, verts_per_hemi
Exemplo n.º 4
0
def project_and_calc_clusters(args):
    if not op.isdir(args.root_fol):
        print('You should first set args.root_fol!')
        return False
    img_files = [
        f for f in glob.glob(op.join(args.root_fol, '*.img')) if op.isfile(f)
    ]
    for img_fname in img_files:
        mgz_fname = fu.mri_convert_to(img_fname, 'mgz', overwrite=False)
        if ' ' in utils.namebase(mgz_fname):
            mgz_new_fname = op.join(
                utils.get_parent_fol(mgz_fname),
                utils.namebase_with_ext(mgz_fname).replace(' ', '_').replace(
                    ',', '').lower())
            os.rename(mgz_fname, mgz_new_fname)
    nii_files = [
        f for f in glob.glob(op.join(args.root_fol, '*'))
        if op.isfile(f) and utils.file_type(f) in ('nii', 'nii.gz', 'mgz')
    ]
    for fname in nii_files:
        fmri_args = fmri.read_cmd_args(
            dict(subject=args.subject,
                 function='project_volume_to_surface,find_clusters',
                 fmri_file_template=fname,
                 threshold=args.cluster_threshold))
        pu.run_on_subjects(fmri_args, fmri.main)
Exemplo n.º 5
0
def find_images_props(fol,
                      start_number=-1,
                      images_prefix='',
                      images_format='',
                      images_type=''):
    if images_type == '':
        images_types = set([
            utils.file_type(image)
            for image in glob.glob(op.join(fol, '{}*.*'.format(images_prefix)))
        ])
        for opt_type in ['png', 'jpg', 'jpeg', 'bmp', 'gif']:
            if opt_type in images_types:
                images_type = opt_type
                print('Images type is {}'.format(images_type))
                break
        if images_type == '':
            raise Exception("Can't find the images type!")
    images = glob.glob(
        op.join(fol, '{}*.{}'.format(images_prefix, images_type)))
    image_nb = utils.namebase(images[0])
    number = utils.read_numbers_rx(image_nb)[0]
    if images_prefix == '':
        images_prefix = image_nb[:-len(number)]
    if images_format == '':
        images_format = '%0{}d'.format(len(number))
    if start_number == -1:
        start_number = min([
            int(utils.namebase(image)[len(images_prefix):]) for image in images
        ])
    return images_type, images_prefix, images_format, len(number), start_number
Exemplo n.º 6
0
def create_dup_frames_links(subject, dup_frames, fol):
    fol = op.join(MMVT_DIR, subject, 'figures', fol)
    utils.delete_folder_files(fol)
    utils.make_dir(fol)
    for ind, frame in enumerate(dup_frames):
        utils.make_link(
            frame, op.join(fol, 'dup_{}.{}'.format(ind,
                                                   utils.file_type(frame))))
    return fol
Exemplo n.º 7
0
def project_and_calc_clusters(args):
    if not op.isdir(args.root_fol):
        print('You should first set args.root_fol!')
        return False
    nii_files = [
        f for f in glob.glob(op.join(args.root_fol, '*'))
        if op.isfile(f) and utils.file_type(f) in ('nii', 'nii.gz', 'mgz')
    ]
    for fname in nii_files:
        args = fmri.read_cmd_args(
            dict(subject=args.subject,
                 function='project_volume_to_surface,find_clusters',
                 fmri_file_template=fname))
        pu.run_on_subjects(args, fmri.main)
Exemplo n.º 8
0
Arquivo: ct.py Projeto: keshava/mmvt
def isotropization(subject,
                   ct_fname,
                   ct_fol,
                   new_image_fname='',
                   isotropization_type=1,
                   iso_vector_override=None):
    if not op.isdir(ct_fol):
        ct_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'ct'))
    if new_image_fname == '':
        new_image_fname = 'iso_ct.{}'.format(utils.file_type(ct_fname))
    ct_fname = op.join(ct_fol, ct_fname)
    iso_img = ctu.isotropization(ct_fname,
                                 isotropization_type=isotropization_type,
                                 iso_vector_override=iso_vector_override)
    nib.save(iso_img, op.join(ct_fol, new_image_fname))
Exemplo n.º 9
0
def combine_two_figures_with_cb(fname1, fname2, data_max, data_min, cb_cm, cb_ticks=[], crop_figures=True,
                                background='black', cb_ticks_font_size=10):
    if crop_figures:
        crop_image(fname1, fname1, dx=150, dy=0, dw=50, dh=70)
        crop_image(fname2, fname2, dx=150 + 50, dy=0, dw=0, dh=70)
    new_image_fname = op.join(utils.get_parent_fol(fname1), '{}_{}.{}'.format(
        utils.namebase(fname1), utils.namebase(fname2), utils.file_type(fname1)))
    combine_two_images(fname1, fname2, new_image_fname, facecolor=background, dpi=400, w_fac=1, h_fac=1)
    if len(cb_ticks) == 0:
        cb_ticks = [data_min, data_max]
    fol = utils.get_parent_fol(fname1)
    plot_color_bar(data_max, data_min, cb_cm, do_save=True, cb_ticks=cb_ticks, fol=fol, background_color=background,
                   cb_ticks_font_size=cb_ticks_font_size)
    cb_fname = op.join(fol, '{}_colorbar.jpg'.format(cb_cm))
    cb_img = Image.open(cb_fname)
    return combine_brain_with_color_bar(new_image_fname, cb_img, overwrite=True)
Exemplo n.º 10
0
def combine_images(fol,
                   movie_name,
                   frame_rate=10,
                   start_number=-1,
                   images_prefix='',
                   images_format='',
                   images_type='',
                   ffmpeg_cmd='ffmpeg',
                   **kwargs):
    if images_type == '':
        images_types = set([
            utils.file_type(image)
            for image in glob.glob(op.join(fol, '{}*.*'.format(images_prefix)))
        ])
        for opt_type in ['png', 'jpg', 'bmp', 'gif']:
            if opt_type in images_types:
                images_type = opt_type
                print('Images type is {}'.format(images_type))
                break
        if images_type == '':
            raise Exception("Can't find the images type!")
    images = glob.glob(
        op.join(fol, '{}*.{}'.format(images_prefix, images_type)))
    image_nb = utils.namebase(images[0])
    number = utils.read_numbers_rx(image_nb)[0]
    if images_prefix == '':
        images_prefix = image_nb[:-len(number)]
    if images_format == '':
        images_format = '%0{}d'.format(len(number))
    if start_number == -1:
        start_number = min([
            int(utils.namebase(image)[len(images_prefix):]) for image in images
        ])
    images_prefix = op.join(fol, images_prefix)
    movie_name = op.join(fol, movie_name)
    combine_images_cmd = '{ffmpeg_cmd} -framerate {frame_rate} '
    if start_number > 1:
        # You might want to use a static ffmpeg if your ffmepg version doesn't support the start_number flag, like:
        # ffmpeg_cmd = '~/space1/Downloads/ffmpeg-git-static/ffmpeg'
        combine_images_cmd += '-start_number {start_number} '
    combine_images_cmd += '-i {images_prefix}{images_format}.{images_type} '
    # http://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    combine_images_cmd += '-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" '
    combine_images_cmd += '-c:v libx264 -r 30 -pix_fmt yuv420p {movie_name}.mp4'
    rs = utils.partial_run_script(locals())
    rs(combine_images_cmd)
Exemplo n.º 11
0
Arquivo: ct.py Projeto: keshava/mmvt
def save_subject_ct_trans(subject,
                          ct_name='ct_reg_to_mr.mgz',
                          overwrite=False):
    fol = utils.make_dir(op.join(MMVT_DIR, subject, 'ct'))
    output_fname = op.join(fol, 'ct_trans.npz')
    if op.isfile(output_fname) and not overwrite:
        return True
    ct_fname, ct_exist = utils.locating_file(
        ct_name, ['*.mgz', '*.nii', '*.nii.gz'], [
            op.join(MMVT_DIR, subject, 'ct'),
            op.join(SUBJECTS_DIR, subject, 'ct')
        ])
    # ct_fname = op.join(MMVT_DIR, subject, 'ct', ct_name)
    if not ct_exist:  # op.isfile(ct_fname):
        # subjects_ct_fname = op.join(SUBJECTS_DIR, subject, 'mri', ct_name)
        ct_fname, ct_exist = utils.locating_file(
            ct_name, ['*.mgz', '*.nii', '*.nii.gz'],
            op.join(SUBJECTS_DIR, subject, 'mri'))
        if ct_exist:  #op.isfile(subjects_ct_fname):
            utils.make_dir(op.join(MMVT_DIR, subject, 'ct'))
            ct_fname = utils.copy(ct_fname, op.join(MMVT_DIR, subject, 'ct'))
        else:
            print("Can't find subject's CT! ({})".format(ct_fname))
            return False
    if utils.file_type(ct_fname) != 'mgz':
        ct_fname = fu.mri_convert_to(ct_fname, 'mgz')
    if ct_fname != op.join(MMVT_DIR, subject, 'ct', ct_name):
        utils.make_link(ct_fname, op.join(MMVT_DIR, subject, 'ct', ct_name))
    print('save_subject_ct_trans: loading {}'.format(ct_fname))
    header = nib.load(ct_fname).header
    ras_tkr2vox, vox2ras_tkr, vox2ras, ras2vox = anat.get_trans_functions(
        header)
    print('save_subject_ct_trans: Saving {}'.format(output_fname))
    np.savez(output_fname,
             ras_tkr2vox=ras_tkr2vox,
             vox2ras_tkr=vox2ras_tkr,
             vox2ras=vox2ras,
             ras2vox=ras2vox)
    return op.isfile(output_fname)