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 find_blender_in_linux(fol, look_for_dirs=True):
    blender_fol = ''
    if look_for_dirs:
        output = utils.get_command_output(
            "find {} -name '*blender*' -type d".format(fol))
        blender_fols = output.split('\n')
        blender_fols = [
            fol for fol in blender_fols
            if op.isfile(op.join(fol, 'blender.svg')) or 'blender.app' in fol
        ]
        if len(blender_fols) >= 1:
            # todo: let the user select which one
            blender_fol = blender_fols[0]
    else:
        output = utils.get_command_output(
            "find {} -name '*blender*'".format(fol))
        blender_fols = output.split('\n')
        blender_fols = [
            fol for fol in blender_fols
            if utils.is_link(fol) and op.isfile(op.join(fol, 'blender.svg'))
        ]
        if len(blender_fols) >= 1:
            # todo: let the user select which one
            blender_fol = blender_fols[0]
    return blender_fol
Example #3
0
def create_fsaverage_link(links_fol_name='links'):
    freesurfer_home = os.environ.get('FREESURFER_HOME', '')
    if freesurfer_home != '':
        links_fol = utils.get_links_dir(links_fol_name)
        subjects_dir = utils.get_link_dir(links_fol, 'subjects',
                                          'SUBJECTS_DIR')
        fsaverage_link = op.join(subjects_dir, 'fsaverage')
        if not utils.is_link(fsaverage_link):
            fsveareg_fol = op.join(freesurfer_home, 'subjects', 'fsaverage')
            utils.create_folder_link(fsveareg_fol, fsaverage_link)
Example #4
0
def get_all_links(links={}, links_fol=None, links_fol_name='links'):
    if links_fol is None:
        links_fol = utils.get_links_dir(links_fol_name)
    all_links = [
        utils.namebase(f) for f in glob.glob(op.join(links_fol, '*'))
        if utils.is_link(f)
    ]
    all_links = {
        link_name: utils.get_link_dir(links_fol, link_name)
        for link_name in all_links if link_name not in links
    }
    links = utils.merge_two_dics(links, all_links)
    return links
Example #5
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
    ])