Пример #1
0
def install_blender_reqs(gui=True):
    # http://stackoverflow.com/questions/9956741/how-to-install-multiple-python-packages-at-once-using-pip
    try:
        blender_fol = utils.get_link_dir(utils.get_links_dir(), 'blender')
        resource_fol = utils.get_resources_fol()
        blender_parent_fol = utils.get_parent_fol(blender_fol)

        # Get pip
        bin_template = op.join(utils.get_parent_fol(blender_fol),  'Resources', '2.7?', 'python') if utils.is_osx() else \
            op.join(blender_fol, '2.7?', 'python')
        blender_bin_folders = sorted(glob.glob(bin_template))
        if len(blender_bin_folders) == 0:
            print("Couldn't find Blender's bin folder! ({})".format(
                bin_template))
            blender_bin_fol = ''
            choose_folder = mmvt_input(
                'Please choose the Blender bin folder where python file exists',
                gui) == 'Ok'
            if choose_folder:
                fol = utils.choose_folder_gui(
                    blender_parent_fol,
                    'Blender bin folder') if gui else input()
                if fol != '':
                    blender_bin_fol = glob.glob(op.join(fol, '2.7?',
                                                        'python'))[-1]
            if blender_bin_fol == '':
                return
        else:
            # todo: let the use select the folder if more than one
            blender_bin_fol = blender_bin_folders[-1]
        python_exe = 'python.exe' if utils.is_windows() else 'python3.5m'
        current_dir = os.getcwd()
        os.chdir(blender_bin_fol)
        # if utils.is_osx():
        # install blender reqs:
        pip_cmd = '{} {}'.format(op.join('bin', python_exe),
                                 op.join(resource_fol, 'get-pip.py'))
        reqs = 'matplotlib zmq pizco scipy mne joblib tqdm nibabel pdfkit decorator scikit-learn'
        if not utils.is_windows():
            utils.run_script(pip_cmd)
            install_cmd = '{} install {}'.format(op.join('bin', 'pip'), reqs)
            utils.run_script(install_cmd)
        else:
            # https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script
            install_cmd = '{} install '.format(op.join('Scripts', 'pip'), reqs)
            print(
                'Sorry, automatically installing external python libs in python will be implemented in the future.\n'
                + 'Meanwhile, you can do the following:\n' +
                '1) Open a terminal window as administrator: ' +
                'Right click on the "Command Prompt" shortcut from the star menu and choose "Run as administrator"\n'
                +
                '2) Change the directory to "{}".\n'.format(blender_bin_fol) +
                '3) Run "{}"\n'.format(pip_cmd) +
                '4) Run "{}"\nGood luck!'.format(install_cmd))
            # from src.mmvt_addon.scripts import install_blender_reqs
            # install_blender_reqs.wrap_blender_call(args.only_verbose)
        os.chdir(current_dir)
    except:
        print(traceback.format_exc())
        print("*** Can't install pizco ***")
Пример #2
0
def install_blender_reqs():
    # http://stackoverflow.com/questions/9956741/how-to-install-multiple-python-packages-at-once-using-pip
    try:
        blender_fol = utils.get_link_dir(utils.get_links_dir(), 'blender')
        resource_fol = utils.get_resources_fol()
        # Get pip
        blender_bin_fol = op.join(utils.get_parent_fol(blender_fol), 'Resources', '2.78', 'python', 'bin') if utils.is_osx() else \
            glob.glob(op.join(blender_fol, '2.7?', 'python'))[0]
        python_exe = 'python.exe' if utils.is_windows() else 'python3.5m'
        current_dir = os.getcwd()
        os.chdir(blender_bin_fol)
        # if utils.is_osx():
        cmd = '{} {}'.format(op.join('bin', python_exe),
                             op.join(resource_fol, 'get-pip.py'))
        # elif utils.is_linux():
        #     cmd = '{} {}'.format(op.join(blender_bin_fol, 'python3.5m'), op.join(resource_fol, 'get-pip.py'))
        # else:
        #     print('No pizco for windows yet...')
        #     return
        utils.run_script(cmd)
        # install blender reqs:
        if not utils.is_windows():
            cmd = '{} install zmq pizco scipy mne joblib tqdm nibabel matplotlib'.format(
                op.join('bin', 'pip'))
            utils.run_script(cmd)
        else:
            print(
                'Sorry, installing external python libs in python will be implemented in the future'
            )
            # from src.mmvt_addon.scripts import install_blender_reqs
            # install_blender_reqs.wrap_blender_call(args.only_verbose)
        os.chdir(current_dir)
    except:
        print(traceback.format_exc())
        print("*** Can't install pizco ***")
Пример #3
0
def install_blender_reqs(blender_fol='', gui=True):
    # http://stackoverflow.com/questions/9956741/how-to-install-multiple-python-packages-at-once-using-pip
    try:
        if blender_fol == '':
            blender_fol = utils.get_link_dir(utils.get_links_dir(), 'blender')
        resource_fol = utils.get_resources_fol()
        blender_bin_fol, python_exe = get_blender_python_exe(blender_fol, gui)
        current_dir = os.getcwd()
        os.chdir(blender_bin_fol)
        # install blender reqs:
        pip_cmd = '{} {}'.format(op.join('bin', python_exe),
                                 op.join(resource_fol, 'get-pip.py'))
        reqs = 'matplotlib zmq pizco scipy mne joblib tqdm nibabel pdfkit decorator scikit-learn'
        if not utils.is_windows():
            utils.run_script(pip_cmd)
            install_cmd = '{} install {}'.format(op.join('bin', 'pip'), reqs)
            utils.run_script(install_cmd)
        else:
            # https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script
            install_cmd = '{} install '.format(op.join('Scripts', 'pip'), reqs)
            print(
                'Sorry, automatically installing external python libs in python will be implemented in the future.\n'
                + 'Meanwhile, you can do the following:\n' +
                '1) Open a terminal window as administrator: ' +
                'Right click on the "Command Prompt" shortcut from the star menu and choose "Run as administrator"\n'
                +
                '2) Change the directory to "{}".\n'.format(blender_bin_fol) +
                '3) Run "{}"\n'.format(pip_cmd) +
                '4) Run "{}"\nGood luck!'.format(install_cmd))
            # from src.mmvt_addon.scripts import install_blender_reqs
            # install_blender_reqs.wrap_blender_call(args.only_verbose)
        os.chdir(current_dir)
    except:
        print(traceback.format_exc())
        print("*** Can't install pizco ***")
Пример #4
0
def create_empty_links_csv(links_fol_name='links',
                           links_file_name='links.csv'):
    links_fol = utils.get_links_dir(links_fol_name)
    links_names = [
        'mmvt', 'subjects', 'blender', 'eeg', 'meg', 'fMRI', 'electrodes'
    ]
    links = {link_name: '' for link_name in links_names}
    write_links_into_csv_file(links, links_fol, links_file_name)
Пример #5
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)
Пример #6
0
def main(args):
    # 1) Install dependencies from requirements.txt (created using pipreqs)
    if utils.should_run(args, 'install_reqs'):
        install_reqs(args.only_verbose)

    # 2) Create links
    if utils.should_run(args, 'create_links'):
        links_created = create_links(args.links, args.gui,
                                     args.default_folders, args.only_verbose,
                                     args.links_file_name,
                                     args.overwrite_links)
        if not links_created:
            print(
                'Not all the links were created! Make sure all the links are created before running MMVT.'
            )

    # 2,5) Create fsaverage folder link
    if utils.should_run(args, 'create_fsaverage_link'):
        create_fsaverage_link(args.links)

    # 3) Copy resources files
    if utils.should_run(args, 'copy_resources_files'):
        links_dir = utils.get_links_dir(args.links)
        mmvt_root_dir = utils.get_link_dir(links_dir, 'mmvt')
        resource_file_exist = copy_resources_files(mmvt_root_dir,
                                                   args.only_verbose)
        if not resource_file_exist:
            input(
                'Not all the resources files were copied to the MMVT folder ({}).\n'
                .format(mmvt_root_dir) +
                'Please copy them manually from the mmvt_code/resources folder.\n'
                + 'Press any key to continue...')

    # 4) Install the addon in Blender (depends on resources and links)
    if utils.should_run(args, 'install_addon'):
        from src.mmvt_addon.scripts import install_addon
        install_addon.wrap_blender_call(args.only_verbose)

    # 5) Install python packages in Blender
    if utils.should_run(args, 'install_blender_reqs'):
        install_blender_reqs()

    if utils.should_run(args, 'send_email'):
        send_email()

    if 'create_links_csv' in args.function:
        create_empty_links_csv()

    if 'create_csv' in args.function:
        write_links_into_csv_file(get_all_links())

    if 'find_blender' in args.function:
        find_blender()

    print('Finish!')
Пример #7
0
def write_links_into_csv_file(links,
                              links_fol=None,
                              links_file_name='links.csv',
                              links_fol_name='links'):
    import csv
    if links_fol is None:
        links_fol = utils.get_links_dir(links_fol_name)
    with open(op.join(links_fol, links_file_name), 'w') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',')
        for link_name, link_dir in links.items():
            csv_writer.writerow([link_name, link_dir])
Пример #8
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
Пример #9
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
    ])
Пример #10
0
def main(args):
    # If python version is < 3.5, use Blender's python
    if sys.version_info[0] < 3 or sys.version_info[
            0] == 3 and sys.version_info[1] < 5:
        blender_fol = find_blender()
        blender_bin_fol, python_exe = get_blender_python_exe(blender_fol)
        blender_python_exe = op.join(blender_bin_fol, 'bin', python_exe)
        if not op.isfile(blender_python_exe):
            print('You must use python 3.5 or newer, or install first Blender')
        else:
            # rerun setup with Blender's python
            args.blender_fol = blender_fol
            call_args = utils.create_call_args(args)
            setup_cmd = '{} -m src.setup {}'.format(blender_python_exe,
                                                    call_args)
            utils.run_script(setup_cmd, print_only=False)
        return

    print(args)

    # 1) Install dependencies from requirements.txt (created using pipreqs)
    if utils.should_run(args, 'install_reqs'):
        install_reqs(args.upgrade_reqs_libs, args.only_verbose)

    # 2) Create links
    if utils.should_run(args, 'create_links'):
        links_created = create_links(args.links, args.gui,
                                     args.default_folders, args.only_verbose,
                                     args.links_file_name,
                                     args.overwrite_links)
        if not links_created:
            print(
                'Not all the links were created! Make sure all the links are created before running MMVT.'
            )

    # 2,5) Create fsaverage folder link
    if utils.should_run(args, 'create_fsaverage_link'):
        create_fsaverage_link(args.links)

    # 3) Copy resources files
    if utils.should_run(args, 'copy_resources_files'):
        links_dir = utils.get_links_dir(args.links)
        mmvt_root_dir = utils.get_link_dir(links_dir, 'mmvt')
        resource_file_exist = copy_resources_files(mmvt_root_dir,
                                                   args.overwrite,
                                                   args.only_verbose)
        if not resource_file_exist:
            input(
                'Not all the resources files were copied to the MMVT folder ({}).\n'
                .format(mmvt_root_dir) +
                'Please copy them manually from the mmvt_code/resources folder.\n'
                + 'Press any key to continue...')

    # 4) Install the addon in Blender (depends on resources and links)
    if utils.should_run(args, 'install_addon'):
        from src.mmvt_addon.scripts import install_addon
        install_addon.wrap_blender_call(args.only_verbose)

    # 5) Install python packages in Blender
    if utils.should_run(args, 'install_blender_reqs'):
        install_blender_reqs(args.blender_fol)

    if utils.should_run(args, 'send_email'):
        send_email()

    if 'create_links_csv' in args.function:
        create_empty_links_csv()

    if 'create_csv' in args.function:
        write_links_into_csv_file(get_all_links())

    if 'find_blender' in args.function:
        find_blender()

    if 'get_pip_update_cmd' in args.function:
        get_pip_update_cmd()

    if 'read_blender_link' in args.function:
        read_blender_link()
Пример #11
0
def get_pip_update_cmd(package='numpy'):
    blender_fol = utils.get_link_dir(utils.get_links_dir(), 'blender')
    blender_bin_fol, python_exe = get_blender_python_exe(blender_fol, False)
    install_cmd = '{} install --upgrade {}'.format(
        op.join(blender_bin_fol, 'bin', 'pip'), package)
    print(install_cmd)
Пример #12
0
def read_blender_link():
    print('Blender link: {}'.format(
        utils.get_link_dir(utils.get_links_dir(), 'blender')))