Example #1
0
def ask_and_create_link(links_fol,
                        link_name,
                        message,
                        gui=True,
                        create_default_dir=False,
                        overwrite=True):
    fol = ''
    ret = False
    if not overwrite and utils.is_link(op.join(links_fol, link_name)):
        fol = utils.get_link_dir(links_fol, link_name)
        ret = True
    else:
        choose_folder = mmvt_input(message, gui) == 'Ok'
        if choose_folder:
            root_fol = utils.get_parent_fol(links_fol)
            fol = utils.choose_folder_gui(root_fol,
                                          message) if gui else input()
            if fol != '':
                create_real_folder(fol)
                ret = utils.create_folder_link(fol,
                                               op.join(links_fol, link_name),
                                               overwrite=overwrite)
                if create_default_dir:
                    utils.make_dir(op.join(fol, 'default'))
    return fol, ret
Example #2
0
def create_real_folder(real_fol):
    try:
        if real_fol == '':
            real_fol = utils.get_resources_fol()
        utils.make_dir(real_fol)
    except:
        print('Error with creating the folder "{}"'.format(real_fol))
        print(traceback.format_exc())
Example #3
0
def copy_resources_files(mmvt_root_dir, overwrite=True, only_verbose=False):
    resource_dir = utils.get_resources_fol()
    utils.make_dir(op.join(op.join(mmvt_root_dir, 'color_maps')))
    files = [
        'aparc.DKTatlas40_groups.csv', 'atlas.csv', 'sub_cortical_codes.txt',
        'FreeSurferColorLUT.txt', 'empty_subject.blend', 'high_level_atlas.csv'
    ]
    cm_files = glob.glob(op.join(resource_dir, 'color_maps', '*.npy'))
    all_files_exist = utils.all(
        [op.isfile(op.join(mmvt_root_dir, file_name)) for file_name in files])
    all_cm_files_exist = utils.all([
        op.isfile(
            op.join(mmvt_root_dir, 'color_maps',
                    '{}.npy'.format(utils.namebase(fname))))
        for fname in cm_files
    ])
    if all_files_exist and all_cm_files_exist and not overwrite:
        if only_verbose:
            print('All files exist!')
        return True
    if not all_cm_files_exist or overwrite:
        for color_map_file in glob.glob(
                op.join(resource_dir, 'color_maps', '*.npy')):
            new_file_name = op.join(mmvt_root_dir, 'color_maps',
                                    color_map_file.split(op.sep)[-1])
            # print('Copy {} to {}'.format(color_map_file, new_file_name))
            print('Coping {} to {}'.format(color_map_file, new_file_name))
            if not only_verbose:
                try:
                    shutil.copy(color_map_file, new_file_name)
                except:
                    print('Can\'t copy {} to {}!'.format(
                        color_map_file, new_file_name))
    if not all_files_exist or overwrite:
        for file_name in files:
            print('Copying {} to {}'.format(op.join(resource_dir, file_name),
                                            op.join(mmvt_root_dir, file_name)))
            if not only_verbose:
                local_fname = op.join(resource_dir, file_name)
                if op.isfile(op.join(resource_dir, file_name)):
                    try:
                        shutil.copy(local_fname,
                                    op.join(mmvt_root_dir, file_name))
                    except:
                        print('Can\'t copy {}'.format(file_name))
                else:
                    print(
                        '{} is missing, please update your code from github (git pull)'
                        .format(op.join(resource_dir, file_name)))
    return utils.all(
        [op.isfile(op.join(mmvt_root_dir, file_name)) for file_name in files])
Example #4
0
def create_default_link(links_fol,
                        link_name,
                        default_fol_name,
                        create_default_dir=False,
                        overwrite=True):
    root_fol = utils.get_parent_fol(levels=3)
    fol = op.join(root_fol, default_fol_name)
    create_real_folder(fol)
    ret = utils.create_folder_link(fol,
                                   op.join(links_fol, link_name),
                                   overwrite=overwrite)
    if create_default_dir:
        utils.make_dir(op.join(fol, 'default'))
    return fol, ret
Example #5
0
def copy_resources_files(mmvt_root_dir, only_verbose=False):
    resource_dir = utils.get_resources_fol()
    utils.make_dir(op.join(op.join(mmvt_root_dir, 'color_maps')))
    files = [
        'aparc.DKTatlas40_groups.csv', 'atlas.csv', 'sub_cortical_codes.txt',
        'FreeSurferColorLUT.txt', 'empty_subject.blend', 'high_level_atlas.csv'
    ]
    cm_files = glob.glob(op.join(resource_dir, 'color_maps', '*.npy'))
    all_files_exist = utils.all(
        [op.isfile(op.join(mmvt_root_dir, file_name)) for file_name in files])
    all_cm_files_exist = utils.all([
        op.isfile(
            op.join(mmvt_root_dir, 'color_maps',
                    '{}.npy'.format(utils.namebase(fname))))
        for fname in cm_files
    ])
    if all_files_exist and all_cm_files_exist:
        if only_verbose:
            print('All files exist!')
        return True
    if not all_cm_files_exist:
        for color_map_file in glob.glob(
                op.join(resource_dir, 'color_maps', '*.npy')):
            new_file_name = op.join(mmvt_root_dir, 'color_maps',
                                    color_map_file.split(op.sep)[-1])
            # print('Copy {} to {}'.format(color_map_file, new_file_name))
            if only_verbose:
                print('Coping {} to {}'.format(color_map_file, new_file_name))
            else:
                shutil.copy(color_map_file, new_file_name)
    if not all_files_exist:
        for file_name in files:
            if only_verbose:
                print('Copying {} to {}'.format(
                    op.join(resource_dir, file_name),
                    op.join(mmvt_root_dir, file_name)))
            else:
                shutil.copy(op.join(resource_dir, file_name),
                            op.join(mmvt_root_dir, file_name))
    return utils.all(
        [op.isfile(op.join(mmvt_root_dir, file_name)) for file_name in files])
Example #6
0
def create_links(links_fol_name='links',
                 gui=True,
                 default_folders=False,
                 only_verbose=False,
                 links_file_name='links.csv',
                 overwrite=True):
    links_fol = utils.get_links_dir(links_fol_name)
    if only_verbose:
        print('making links dir {}'.format(links_fol))
    else:
        utils.make_dir(links_fol)
    links_names = [
        'blender', 'mmvt', 'subjects', 'eeg', 'meg', 'fMRI', 'electrodes'
    ]
    # if not utils.is_windows():
    #     links_names.insert(1, 'subjects')
    if not overwrite:
        all_links_exist = utils.all([
            utils.is_link(op.join(links_fol, link_name))
            for link_name in links_names
        ])
        if all_links_exist:
            print('All links exist!')
            links = {
                link_name: utils.get_link_dir(links_fol, link_name)
                for link_name in links_names
            }
            write_links_into_csv_file(links, links_fol, links_file_name)
            return True
    if not utils.is_windows() and not utils.is_link(
            op.join(links_fol, 'freesurfer')):
        if os.environ.get('FREESURFER_HOME', '') == '':
            print(
                'If you are going to use FreeSurfer locally, please source it and rerun'
            )
            # If you choose to continue, you'll need to create a link to FreeSurfer manually")
            cont = input("Do you want to continue (y/n)?")
            if cont.lower() != 'y':
                return
        else:
            freesurfer_fol = os.environ['FREESURFER_HOME']
            if not only_verbose:
                create_real_folder(freesurfer_fol)

    mmvt_message = 'Please select where do you want to put the blend files '
    subjects_message = \
        'Please select where do you want to store the FreeSurfer recon-all files neccessary for MMVT.\n' + \
        '(It is prefered to create a local folder, because MMVT is going to save files to this directory) '
    blender_message = 'Please select where did you install Blender '
    meg_message = 'Please select where do you want to put the MEG files (Cancel if you are not going to use MEG data) '
    eeg_message = 'Please select where do you want to put the EEG files (Cancel if you are not going to use EEG data) '
    fmri_message = 'Please select where do you want to put the fMRI files (Cancel if you are not going to use fMRI data) '
    electrodes_message = 'Please select where do you want to put the electrodes files (Cancel if you are not going to use electrodes data) '

    blender_fol = find_blender()
    if blender_fol != '':
        utils.create_folder_link(blender_fol, op.join(links_fol, 'blender'),
                                 overwrite)
    else:
        ask_and_create_link(links_fol, 'blender', blender_message, gui,
                            overwrite)
    default_message = "Would you like to set default links to the MMVT's folders?\n" + \
        "You can always change that later by running\n" + \
        "python -m src.setup -f create_links"
    create_default_folders = default_folders or mmvt_input(
        default_message, gui, 4) == 'Yes'

    messages = [
        mmvt_message, subjects_message, eeg_message, meg_message, fmri_message,
        electrodes_message
    ]
    deafault_fol_names = [
        'mmvt_blend', 'subjects', 'eeg', 'meg', 'fMRI', 'electrodes'
    ]
    # if not utils.is_windows():
    #     messages.insert(0, subjects_message)
    create_default_dirs = [False] * 3 + [True] * 2 + [False] * 2

    links = {}
    if not only_verbose:
        for link_name, default_fol_name, message, create_default_dir in zip(
                links_names[1:], deafault_fol_names, messages,
                create_default_dirs):
            fol = ''
            if not create_default_folders:
                fol, ret = ask_and_create_link(links_fol,
                                               link_name,
                                               message,
                                               gui,
                                               create_default_dir,
                                               overwrite=overwrite)
            if fol == '' or create_default_folders:
                fol, ret = create_default_link(links_fol,
                                               link_name,
                                               default_fol_name,
                                               create_default_dir,
                                               overwrite=overwrite)
            if ret:
                print('The "{}" link was created to {}'.format(link_name, fol))
            links[link_name] = fol

    links = get_all_links(links, links_fol)
    write_links_into_csv_file(links, links_fol, links_file_name)
    return utils.all([
        utils.is_link(op.join(links_fol, link_name))
        for link_name in links_names
    ])