Example #1
0
def project_on_surface(subject, volume_file, surf_output_fname, target_subject=None, overwrite_surf_data=False,
                       modality='fmri', subjects_dir='', mmvt_dir='', **kargs):
    if target_subject is None:
        target_subject = subject
    if subjects_dir == '':
        subjects_dir = utils.get_link_dir(utils.get_links_dir(), 'subjects', 'SUBJECTS_DIR')
    if mmvt_dir == '':
        mmvt_dir = utils.get_link_dir(utils.get_links_dir(), 'mmvt')
    utils.make_dir(op.join(mmvt_dir, subject, 'fmri'))
    os.environ['SUBJECTS_DIR'] = subjects_dir
    os.environ['SUBJECT'] = subject
    for hemi in utils.HEMIS:
        if not op.isfile(surf_output_fname.format(hemi=hemi)) or overwrite_surf_data:
            print('project {} to {}'.format(volume_file, hemi))
            if modality != 'pet':
                surf_data = project_volume_data(volume_file, hemi, subject_id=subject, surf="pial", smooth_fwhm=3,
                    target_subject=target_subject, output_fname=surf_output_fname.format(hemi=hemi))
            else:
                surf_data = project_pet_volume_data(subject, volume_file, hemi, surf_output_fname.format(hemi=hemi))
            nans = np.sum(np.isnan(surf_data))
            if nans > 0:
                print('there are {} nans in {} surf data!'.format(nans, hemi))
        surf_data = np.squeeze(nib.load(surf_output_fname.format(hemi=hemi)).get_data())
        output_fname = op.join(mmvt_dir, subject, modality, '{}_{}'.format(modality, op.basename(
            surf_output_fname.format(hemi=hemi))))
        npy_output_fname = op.splitext(output_fname)[0]
        if not op.isfile('{}.npy'.format(npy_output_fname)) or overwrite_surf_data:
            print('Saving surf data in {}.npy'.format(npy_output_fname))
            utils.make_dir(utils.get_parent_fol(npy_output_fname))
            np.save(npy_output_fname, surf_data)
Example #2
0
def get_links():
    links_dir = utils.get_links_dir()
    subjects_dir = utils.get_link_dir(links_dir, 'subjects', 'SUBJECTS_DIR')
    freesurfer_home = utils.get_link_dir(links_dir, 'freesurfer',
                                         'FREESURFER_HOME')
    mmvt_dir = utils.get_link_dir(links_dir, 'mmvt')
    return subjects_dir, mmvt_dir, freesurfer_home
Example #3
0
def calc_dipoles_rois(subject, atlas='laus125', overwrite=False, n_jobs=4):
    links_dir = utils.get_links_dir()
    subjects_dir = utils.get_link_dir(links_dir, 'subjects')
    mmvt_dir = utils.get_link_dir(links_dir, 'mmvt')
    diploes_rois_output_fname = op.join(mmvt_dir, subject, 'meg', 'dipoles_rois.pkl')
    if op.isfile(diploes_rois_output_fname) and not overwrite:
        diploes_rois = utils.load(diploes_rois_output_fname)
        for dip in diploes_rois.keys():
            diploes_rois[dip]['cortical_probs'] *= 1/sum(diploes_rois[dip]['cortical_probs'])
            diploes_rois[dip]['subcortical_probs'] = []
            diploes_rois[dip]['subcortical_rois'] = []
        # coritcal_labels = set(utils.flat_list_of_lists([diploes_rois[k]['cortical_rois'] for k in diploes_rois.keys()]))
        utils.save(diploes_rois, diploes_rois_output_fname)
        return True

    diploes_input_fname = op.join(mmvt_dir, subject, 'meg', 'dipoles.pkl')
    if not op.isfile(diploes_input_fname):
        print('No dipoles file!')
        return False

    labels = lu.read_labels(subject, subjects_dir, atlas, n_jobs=n_jobs)
    labels = list([{'name': label.name, 'hemi': label.hemi, 'vertices': label.vertices}
                   for label in labels])
    if len(labels) == 0:
        print('Can\'t find the labels for atlas {}!'.format(atlas))
        return False

    # find the find_rois package
    mmvt_code_fol = utils.get_mmvt_code_root()
    ela_code_fol = op.join(utils.get_parent_fol(mmvt_code_fol), 'electrodes_rois')
    if not op.isdir(ela_code_fol) or not op.isfile(op.join(ela_code_fol, 'find_rois', 'main.py')):
        print("Can't find ELA folder!")
        print('git pull https://github.com/pelednoam/electrodes_rois.git')
        return False

    # load the find_rois package
    try:
        import sys
        if ela_code_fol not in sys.path:
            sys.path.append(ela_code_fol)
        from find_rois import main as ela
    except:
        print('Can\'t load find_rois package!')
        utils.print_last_error_line()
        return False

    dipoles_dict = utils.load(diploes_input_fname)
    diploles_names, dipoles_pos = [], []
    for cluster_name, dipoles in dipoles_dict.items():
        for begin_t, _, x, y, z, _, _, _, _, _ in dipoles:
            dipole_name = '{}_{}'.format(cluster_name, begin_t) if len(dipoles) > 1 else cluster_name
            diploles_names.append(dipole_name.replace(' ', ''))
            dipoles_pos.append([k * 1e3 for k in [x, y, z]])
    dipoles_rois = ela.identify_roi_from_atlas(
        atlas, labels, diploles_names, dipoles_pos, approx=3, elc_length=0, hit_only_cortex=True,
        subjects_dir=subjects_dir, subject=subject, n_jobs=n_jobs)
    # Convert the list to a dict
    dipoles_rois_dict = {dipoles_rois['name']: dipoles_rois for dipoles_rois in dipoles_rois}
    utils.save(dipoles_rois_dict, diploes_rois_output_fname)
Example #4
0
def main(args):
    # 1) Create links
    if utils.should_run(args, 'create_links'):
        links_created = create_links(args.links, args.gui)
        if not links_created:
            print('Not all the links were created! Make sure all the links are created before running MMVT.')

    # 2) 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)
        if not resource_file_exist:
            print('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')

    # 3) 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()

    # 4) Install dependencies from requirements.txt (created using pipreqs)
    if utils.should_run(args, 'install_reqs'):
        install_reqs()
        print('Finish!')
Example #5
0
def create_seghead(subject, subjects_dir=None, print_only=False, **kargs):
    if subjects_dir is None:
        subjects_dir = utils.get_link_dir(utils.get_links_dir(), 'subjects',
                                          'SUBJECTS_DIR')
    os.environ['SUBJECTS_DIR'] = subjects_dir
    rs = utils.partial_run_script(locals(), print_only=print_only)
    rs(mkheadsurf)
Example #6
0
def pre_blender_call(args):
    from src.mmvt_addon import load_results_panel
    from src.preproc import fMRI as fmri
    from src.utils import preproc_utils as pu
    from src.utils import utils

    user_fol = op.join(pu.MMVT_DIR, args.subject)
    nii_fname = args.nii
    if not op.isfile(args.nii):
        args.nii = op.join(user_fol, 'fmri', nii_fname)
    if not op.isfile(args.nii):
        fmri_fol = op.join(utils.get_link_dir(utils.get_links_dir(), 'fMRI'),
                           args.subject)
        args.nii = op.join(fmri_fol, nii_fname)
    if not op.isfile(args.nii):
        raise Exception("Can't find the nii file!")
    fmri_file_template, _, _ = load_results_panel.load_surf_files(
        args.nii, run_fmri_preproc=False, user_fol=user_fol)
    preproc_args = fmri.read_cmd_args(
        dict(subject=args.subject,
             atlas=args.atlas,
             function='load_surf_files',
             fmri_file_template=fmri_file_template,
             ignore_missing=True))
    ret = pu.run_on_subjects(preproc_args, fmri.main)
    if ret:
        load_results_panel.clean_nii_temp_files(fmri_file_template, user_fol)
        args.fmri_file_template = fmri_file_template
    else:
        raise Exception("Couldn't load the surface files!")
    return args
Example #7
0
def combine_images(fol,
                   movie_name,
                   frame_rate=10,
                   start_number=-1,
                   images_prefix='',
                   images_format='',
                   images_type='',
                   ffmpeg_cmd='',
                   movie_name_full_path=False,
                   debug=False,
                   copy_files=False,
                   add_reverse_frames=False,
                   **kwargs):
    if ffmpeg_cmd == '':
        ffmpeg_dir = utils.get_link_dir(utils.get_links_dir(), 'ffmpeg')
        ffmpeg_dir = op.join(ffmpeg_dir,
                             'bin') if utils.is_windows() else ffmpeg_dir
        ffmpeg_cmd = op.join(ffmpeg_dir,
                             'ffmpeg') if op.isdir(ffmpeg_dir) else 'ffmpeg'
    print('ffmpeg_cmd: {}'.format(ffmpeg_cmd))
    images_type, images_prefix, images_format, images_format_len, start_number = find_images_props(
        fol, start_number, images_prefix, images_format, images_type)
    if movie_name == '' and images_prefix != '':
        movie_name = images_prefix
    elif movie_name == '':
        movie_name = 'output_video'
    org_fol = fol
    if utils.is_windows() or copy_files:
        fol = change_frames_names(fol, images_prefix, images_type,
                                  images_format_len)
    if add_reverse_frames:
        add_reverse_frames_fol(fol, images_prefix, images_type)
    images_prefix = op.join(fol, images_prefix)
    if not movie_name_full_path:
        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} '
    # Not working in windows:
    # combine_images_cmd += '-pattern_type glob -i "*.{images_type}" '
    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'
    if debug:
        combine_images_cmd += ' -loglevel debug'
    rs = utils.partial_run_script(locals())
    rs(combine_images_cmd)
    with open(op.join(org_fol, 'combine_images_cmd.txt'), 'w') as f:
        f.write(combine_images_cmd.format(**locals()))
    return '{}.mp4'.format(movie_name)
Example #8
0
def read_cmd_args(argv=None, subject='', mri_subject='', atlas=''):
    if argv is None and subject != '':
        mri_subject = subject if mri_subject == '' else mri_subject
        argv = ['-s', subject, '-m', mri_subject]
    args = meg.read_cmd_args(argv)
    if atlas != '' and args.atlas != atlas:
        args.atlas = atlas
    args.pick_meg = False
    args.pick_eeg = True
    args.reject = False
    args.fwd_usingMEG = False
    args.fwd_usingEEG = True
    args.modality = 'eeg'
    args.meg_dir = utils.get_link_dir(LINKS_DIR, 'eeg')
    if not op.isdir(args.meg_dir):
        raise Exception('EEG dir can\'t be found! Please rerun src.setup with -f create_links')
    return args
Example #9
0
def copy_fmri_ts_files(subjects, delete_previous_files=True):
    FMRI_DIR = utils.get_link_dir(utils.get_links_dir(), 'fMRI')

    for sub in subjects:
        # ts_files = glob.glob(op.join(ts_root_fol, sub, 'rest', 'laus125_*.txt'))
        ts_files = glob.glob(
            op.join(ts_root_fol, '{}_laus125_*.txt'.format(sub)))
        if len(ts_files) == 0:
            print('No ts files for {}!'.format(sub))
        for ts_file in ts_files:
            target_fname = op.join(FMRI_DIR, sub,
                                   utils.namebase_with_ext(ts_file))
            if delete_previous_files:
                for old_fname in glob.glob(
                        op.join(FMRI_DIR, sub, '*laus125_*.txt')):
                    os.remove(old_fname)
            print('Copy {} to {}'.format(ts_file, target_fname))
            utils.copy_file(ts_file, target_fname)
Example #10
0
import argparse
import os.path as op
from src.preproc import connectivity as con
from src.utils import utils
from src.utils import args_utils as au
from src.utils import preproc_utils as pu

LINKS_DIR = utils.get_links_dir()
ELECTRODES_DIR = utils.get_link_dir(LINKS_DIR, 'electrodes')
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')


def example1(subject):
    # subject = 'fsaverage5c'
    args = con.read_cmd_args(['-s', subject])
    args.atlas = 'laus125'
    args.mat_fname = '/cluster/neuromind/npeled/linda/figure_file1.mat'
    args.mat_field = 'figure_file'
    args.windows = 1
    args.stat = con.STAT_DIFF
    args.threshold_percentile = 99
    args.conditions = ['task', 'rest']
    con.save_rois_connectivity(subject, args)


def example2(subject):
    # subject = 'fsaverage5c'
    args = con.read_cmd_args(['-s', subject])
    args.atlas = 'laus125'
    args.mat_fname = '/cluster/neuromind/npeled/linda/figure_file3.mat'
    args.mat_field = 'matrix_cognition_ratio_fpn_dmn'
Example #11
0
from src.mmvt_addon.dell import find_electrodes_in_ct as fect
import numpy as np
import os.path as op
import nibabel as nib
from itertools import product
from scipy.spatial.distance import cdist

from src.utils import utils

MMVT_DIR = utils.get_link_dir(utils.get_links_dir(), 'mmvt')


def find_local_maxima_from_voxels(voxel,
                                  ct_data,
                                  threshold,
                                  find_nei_maxima=True):
    voxels = np.array([voxel])
    maxs = fect.find_all_local_maxima(ct_data, voxels, threshold,
                                      find_nei_maxima)
    print(maxs)


def test2(ct_data, ct_header, brain, aseg, threshold, min_distance):
    ct_voxels = fect.find_voxels_above_threshold(ct_data, threshold)
    ct_voxels = fect.mask_voxels_outside_brain(ct_voxels, ct_header, brain,
                                               aseg)
    voxels = fect.find_all_local_maxima(ct_data,
                                        ct_voxels,
                                        threshold,
                                        find_nei_maxima=True,
                                        max_iters=100)
Example #12
0
from pymesh import obj
import os.path as op
from src.utils import utils

MMVT_DIR = utils.get_link_dir(utils.get_links_dir(), 'mmvt')


def create_eeg_mesh_from_obj(subject, obj_fname):
    mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
    # mesh = obj.Obj(obj_fname)
    # verts, faces = mesh.vertices, mesh.faces
    verts, faces = utils.read_obj_file(obj_fname)
    utils.write_ply_file(verts, faces, mesh_ply_fname)
    faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
    utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, True,
                               utils.namebase(faces_verts_out_fname))

def recreate_mesh_faces_verts(subject, ply_fname):
    verts, faces = utils.read_ply_file(ply_fname)
    faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
    utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, True,
                               utils.namebase(faces_verts_out_fname))


if __name__ == '__main__':
    subject = 'ep001'
    # create_eeg_mesh_from_obj(subject, '/homes/5/npeled/space1/mmvt/ep001/eeg/eeg_helmet.obj')
    recreate_mesh_faces_verts(subject, '/homes/5/npeled/space1/mmvt/ep001/eeg/eeg_helmet.ply')
    print('Finish!')
Example #13
0
import os
import os.path as op
from collections import defaultdict
import glob
import traceback

from src.utils import utils
from src.utils import args_utils as au

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
MMVT_DIR = op.join(LINKS_DIR, 'mmvt')


def run_on_subjects(args, main_func, subjects_itr=None, subject_func=None):
    if subjects_itr is None:
        subjects_itr = args.subject
    subjects_flags, subjects_errors = {}, {}
    args.n_jobs = utils.get_n_jobs(args.n_jobs)
    args.sftp_password = utils.get_sftp_password(
        args.subject, SUBJECTS_DIR, args.necessary_files, args.sftp_username, args.overwrite_fs_files) \
        if args.sftp else ''
    if '*' in args.subject:
        args.subject = [utils.namebase(fol) for fol in glob.glob(op.join(SUBJECTS_DIR, args.subject))]
    os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
    for tup in subjects_itr:
        subject = get_subject(tup, subject_func)
        utils.make_dir(op.join(MMVT_DIR, subject, 'mmvt'))
        remote_subject_dir = utils.build_remote_subject_dir(args.remote_subject_dir, subject)
        print('****************************************************************')
        print('subject: {}, atlas: {}'.format(subject, args.atlas))
Example #14
0
from functools import partial
import os.path as op
import shutil
import mne
import scipy.io as sio
import scipy
from collections import defaultdict, OrderedDict, Iterable
import matplotlib.pyplot as plt

from src.utils import utils
from src.mmvt_addon import colors_utils as cu
from src.utils import matlab_utils as mu
from src.utils import preproc_utils as pu

SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links()
ELECTRODES_DIR = utils.get_link_dir(utils.get_links_dir(), 'electrodes')
HEMIS = utils.HEMIS
STAT_AVG, STAT_DIFF = range(2)
STAT_NAME = {STAT_DIFF: 'diff', STAT_AVG: 'avg'}
DEPTH, GRID = range(2)


def montage_to_npy(montage_file, output_file):
    sfp = mne.channels.read_montage(montage_file)
    np.savez(output_file, pos=np.array(sfp.pos), names=sfp.ch_names)


def electrodes_csv_to_npy(ras_file, output_file, bipolar=False, delimiter=','):
    data = np.genfromtxt(ras_file, dtype=str, delimiter=delimiter)
    data = fix_str_items_in_csv(data)
    # Check if the electrodes coordinates has a header
Example #15
0
import os.path as op
import numpy as np
from src.utils import utils
from src.utils import freesurfer_utils as fu
from src.preproc import meg

LINKS_DIR = utils.get_links_dir()
FMRI_DIR = utils.get_link_dir(utils.get_links_dir(), 'fMRI')
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')


def morph_fmri(morph_from, morph_to, nii_template):
    utils.make_dir(op.join(MMVT_DIR, morph_to, 'fmri'))
    for hemi in utils.HEMIS:
        fu.surf2surf(
            morph_from, morph_to, hemi,
            op.join(MMVT_DIR, morph_from, 'fmri',
                    nii_template.format(hemi=hemi)),
            op.join(MMVT_DIR, morph_to, 'fmri',
                    nii_template.format(hemi=hemi)))


def morph_stc(subject,
              events,
              morph_to_subject,
              inverse_method='dSPM',
              grade=5,
              smoothing_iterations=None,
              overwrite=False,
              n_jobs=6):
Example #16
0
import os.path as op
from src.utils import utils
import nibabel as nib
import numpy as np

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
DTI_DIR = utils.get_link_dir(LINKS_DIR, 'dti')
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')


def load_tck_file(subject, tck_file_name):
    input_fname = op.join(DTI_DIR, subject, tck_file_name)
    if not op.isfile(input_fname):
        print('Cannot find the input file! {}!'.format(input_fname))
        return False
    tck_file = nib.streamlines.load(input_fname)
    tracks = tck_file.streamlines.get_data()
    header = tck_file.header
    return tracks, header


def save_to_mmvt(subject, tracks, header, tracks_name):
    dti_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'dti'))
    np.save(op.join(dti_fol, '{}_tracks.npy'.format(tracks_name)), tracks)
    utils.save(header, op.join(dti_fol, '{}_header.pkl'.format(tracks_name)))


def plot_tracks(tracks):
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
Example #17
0
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import os.path as op

from src.utils import utils
from src.utils import figures_utils as figu

LINKS_DIR = utils.get_links_dir()
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')


def create_BuPu_YlOrRd_cm(n=128):
    colors1 = plt.cm.PuBu(np.linspace(1, 0, n))
    colors2 = plt.cm.YlOrRd(np.linspace(0, 1, n))
    colors = np.vstack((colors1, colors2))
    colors_map = mcolors.LinearSegmentedColormap.from_list(
        'BuPu_YlOrRd', colors)
    return colors_map


def create_PuBu_RdOrYl_cm(n=128):
    colors1 = plt.cm.PuBu(np.linspace(0.2, 1, n))
    colors2 = plt.cm.YlOrRd(np.linspace(1, 0.2, n))
    colors = np.vstack((colors1, colors2))
    colors_map = mcolors.LinearSegmentedColormap.from_list(
        'PuBu_RdOrYl', colors)
    return colors_map


def combine_two_colormaps(cm1_name,
Example #18
0
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

from src.preproc import eeg
from src.preproc import meg
from src.utils import utils
import glob
import os.path as op
import mne
import numpy as np
import re

LINKS_DIR = utils.get_links_dir()
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')


def calc_fwd_inv(subject,
                 modality,
                 run_num,
                 raw_fname,
                 empty_fname,
                 bad_channels,
                 overwrite_inv=False,
                 overwrite_fwd=False):
    # python -m src.preproc.eeg -s nmr00857 -f calc_inverse_operator,make_forward_solution
    #     --overwrite_inv 0 --overwrite_fwd 0 -t epilepsy
    #     --raw_fname  /autofs/space/frieda_001/users/valia/epilepsy/5241495_00857/subj_5241495/190123/5241495_01_raw.fif
    #     --empty_fname /autofs/space/frieda_001/users/valia/epilepsy/5241495_00857/subj_5241495/190123/5241495_roomnoise_raw.fif
Example #19
0
import os.path as op
import glob
from src.utils import utils

FFMPEG_DIR = utils.get_link_dir(utils.get_links_dir(), 'ffmpeg')
FFMPEG_DIR = op.join(FFMPEG_DIR, 'bin') if utils.is_windows() else FFMPEG_DIR
FFMPEG_CMD = op.join(FFMPEG_DIR,
                     'ffmpeg') if op.isdir(FFMPEG_DIR) else 'ffmpeg'

# from moviepy.config import change_settings
# change_settings({"IMAGEMAGICK_BINARY": r"/usr/bin/convert"})

# https://www.vultr.com/docs/install-imagemagick-on-centos-6
# https://github.com/BVLC/caffe/issues/3884


def check_movipy():
    try:
        import moviepy.config as conf
        if conf.try_cmd([conf.FFMPEG_BINARY])[0]:
            print("MoviePy : ffmpeg successfully found.")
        else:
            print("MoviePy : can't find or access ffmpeg.")

        if conf.try_cmd([conf.IMAGEMAGICK_BINARY])[0]:
            print("MoviePy : ImageMagick successfully found.")
        else:
            print("MoviePy : can't find or access ImageMagick.")
    except:
        print("Can't import moviepy")
Example #20
0
import argparse
import os.path as op
import shutil
import glob

from src.preproc import meg as meg
from src.utils import utils
from src.utils import args_utils as au
from src.utils import preproc_utils as pu

LINKS_DIR = utils.get_links_dir()
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
FMRI_DIR = utils.get_link_dir(utils.get_links_dir(), 'fMRI')
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')


def read_epoches_and_calc_activity(subject, mri_subject):
    args = meg.read_cmd_args(['-s', subject, '-m', mri_subject])
    args.function = ['calc_stc', 'calc_labels_avg_per_condition', 'smooth_stc', 'save_activity_map']
    args.pick_ori = 'normal'
    args.colors_map = 'jet'
    meg.run_on_subjects(args)


def calc_single_trial_labels_msit(subject, mri_subject):
    args = meg.read_cmd_args(['-s', subject, '-m', mri_subject])
    args.task = 'MSIT'
    args.atlas = 'laus250'
    args.function = 'calc_stc,calc_single_trial_labels_per_condition'
    args.t_tmin = -0.5
    args.t_tmax = 2
Example #21
0
try:
    from sklearn.neighbors import BallTree
except:
    print('No sklearn!')

import shutil

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from src.utils import utils
from src.utils import freesurfer_utils as fu
from src.preproc import meg_preproc as meg

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
SUBJECTS_MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
FREE_SURFER_HOME = utils.get_link_dir(LINKS_DIR, 'freesurfer', 'FREESURFER_HOME')
BLENDER_ROOT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
FMRI_DIR = utils.get_link_dir(LINKS_DIR, 'fMRI')
os.environ['FREESURFER_HOME'] = FREE_SURFER_HOME
os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
# SUBJECTS_DIR = '/homes/5/npeled/space3/subjects'
# # SUBJECTS_DIR = '/autofs/space/lilli_001/users/DARPA-MEG/freesurfs'
# # SUBJECTS_DIR =  '/home/noam/subjects/mri'
# # SUBJECT = 'ep001'
# os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
# ROOT_DIR = [f for f in ['/homes/5/npeled/space3/fMRI/MSIT', '/home/noam/fMRI/MSIT'] if op.isdir(f)][0]
# BLENDER_DIR = '/homes/5/npeled/space3/visualization_blender'
# FREE_SURFER_HOME = utils.get_exisiting_dir([os.environ.get('FREESURFER_HOME', ''),
#     '/usr/local/freesurfer/stable5_3_0', '/home/noam/freesurfer'])
Example #22
0
import os.path as op
import numpy as np
import mne.io
import traceback

from src.utils import utils
from src.utils import preproc_utils as pu
from src.preproc import meg as meg

SUBJECTS_MRI_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links()

LINKS_DIR = utils.get_links_dir()
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
SUBJECTS_EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')
if SUBJECTS_EEG_DIR == '':
    print('No EEG folder, using MEG folder')
    SUBJECTS_EEG_DIR = MEG_DIR
if SUBJECTS_EEG_DIR == '':
    raise Exception('No EEG folder (not MEG)!')
SUBJECT_EEG_DIR = ''


def read_eeg_sensors_layout(subject, mri_subject, args):
    return meg.read_sensors_layout(subject, mri_subject, args, pick_meg=False, pick_eeg=True)


def save_evoked_to_blender(mri_subject, events, args, evoked=None):
    fol = op.join(MMVT_DIR, mri_subject, 'eeg')
    utils.make_dir(fol)
    if '{cond}' in meg.EVO:
        for event_ind, event_id in enumerate(events.keys()):
Example #23
0
import os
import os.path as op
from pizco import Proxy

os.chdir(os.environ['MMVT_CODE'])
from src.mmvt_addon.scripts import run_mmvt
from src.utils import utils

subject, atlas = 'DC', 'laus250'
run_mmvt.run(subject, atlas, run_in_background=False, debug=False)
mmvt = Proxy('tcp://127.0.0.1:8001')
meg_dir = utils.get_link_dir(utils.get_links_dir(), 'meg')
stc_fname = op.join(meg_dir, subject, 'left-MNE-1-15-lh.stc')
mmvt.plot_stc(stc_fname, 1270, threshold=0.1)
Example #24
0
import os.path as op
import numpy as np
import mne.io
import traceback
from functools import partial

from src.utils import utils
from src.utils import preproc_utils as pu
from src.preproc import meg as meg

SUBJECTS_MRI_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links()

LINKS_DIR = utils.get_links_dir()
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
SUBJECTS_EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')
if SUBJECTS_EEG_DIR == '':
    print('No EEG folder, using MEG folder')
    SUBJECTS_EEG_DIR = MEG_DIR
if SUBJECTS_EEG_DIR == '':
    raise Exception('No EEG folder (not MEG)!')
SUBJECT_EEG_DIR = ''

calc_evokes = meg.calc_evokes_wrapper
calc_fwd_inv = meg.calc_fwd_inv_wrapper
calc_stc_per_condition = meg.calc_stc_per_condition_wrapper
plot_evoked = meg.plot_evoked
plot_topomap = meg.plot_topomap


def read_sensors_layout_args(mri_subject, args):
    return meg.read_sensors_layout(mri_subject,
Example #25
0
import os.path as op
import scipy.io as sio
import argparse
import numpy as np
from src.preproc import electrodes as elecs
from src.utils import utils
from src.utils import args_utils as au
from src.utils import preproc_utils as pu
from src.utils import matlab_utils as mu

ELECTRODES_DIR = utils.get_link_dir(utils.get_links_dir(), 'electrodes')


def read_electrodes_coordiantes_from_specific_xlsx_sheet(subject, bipolar):
    args = elecs.read_cmd_args(['-s', subject, '-b', str(bipolar)])
    args.ras_xls_sheet_name = 'RAS_Snapped'
    elecs.main(subject, args)


def save_msit_single_trials_data(subject, bipolar):
    args = elecs.read_cmd_args(['-s', subject, '-b', str(bipolar)])
    args.task = 'MSIT'
    args.function = 'create_electrode_data_file'
    args.input_matlab_fname = 'electrodes_data_trials.mat'
    args.electrodes_names_field = 'electrodes'
    args.field_cond_template = '{}'
    elecs.main(subject, args)


def load_edf_data_seizure(args):
    args = elecs.read_cmd_args(
Example #26
0
import numpy as np
import mne
import os.path as op
from src.preproc import meg
from src.preproc import anatomy
from src.utils import utils
import glob
import shutil
from collections import defaultdict
import traceback

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
FREE_SURFER_HOME = utils.get_link_dir(LINKS_DIR, 'freesurfer', 'FREESURFER_HOME')
SUBJECTS_MEG_DIR = op.join(LINKS_DIR, 'meg')
BLENDER_ROOT_DIR = op.join(LINKS_DIR, 'mmvt')


def find_events_indices(events_fname):
    data = np.genfromtxt(events_fname, dtype=np.int, delimiter=',', skip_header=1, usecols=(0, 2, 6), names=None, autostrip=True)
    data[:, 0] = range(len(data))
    data = data[data[:, -1] == 1]
    indices = {}
    event_names = {1:'low_risk', 2:'med_risk', 3:'high_risk'}
    for risk, event_name in event_names.items():
        event_indices = data[data[:, 1] == risk, 0]
        indices[event_name] = event_indices
        if len(event_indices) == 0:
            print('No events for {}!'.format(event_name))
            return None
    return indices
to compute an inverse solution and apply it on event related fields.
"""
# Author: Denis A. Engemann
# License: BSD 3 clause

import os.path as op
import mne
import hcp

from mne.minimum_norm import (make_inverse_operator, apply_inverse,
                              write_inverse_operator, read_inverse_operator)

from src.utils import utils

links_dir = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(links_dir, 'subjects', 'SUBJECTS_DIR')
MMVT_DIR = utils.get_link_dir(links_dir, 'mmvt')
HCP_DIR = utils.get_link_dir(links_dir, 'hcp')
MEG_DIR = utils.get_link_dir(links_dir, 'meg')


# from hcp import preprocessing as preproc

##############################################################################
# we assume our data is inside a designated folder under $HOME
# storage_dir = op.expanduser('~/mne-hcp-data')
recordings_path = op.join(HCP_DIR, 'hcp-meg')
subject = '100307'  # our test subject
task = 'task_working_memory'
meg_sub_fol = op.join(MEG_DIR, subject)
fwd_fname = op.join(meg_sub_fol, '{}-fwd.fif'.format(subject))
Example #28
0
from src.utils import utils
from src.utils import labels_utils as lu
from src.examples.epilepsy import pipeline
from src.examples.epilepsy import utils as epi_utils
from src.examples.epilepsy import plots
from src.preproc import meg, eeg, connectivity
from src.preproc import anatomy as anat
import glob
import traceback
import mne
import numpy as np
from tqdm import tqdm


LINKS_DIR = utils.get_links_dir()
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
ELECTRODES_DIR = utils.get_link_dir(LINKS_DIR, 'electrodes')


def find_remote_subject_dir(subject):
    remote_subjects_dir = '/space/megraid/clinical/MEG-MRI/seder/freesurfer'
    if not op.isdir(op.join(remote_subjects_dir, subject)):
        remote_subjects_dir = SUBJECTS_DIR
    remote_subject_dir = op.join(remote_subjects_dir, subject)
    if not op.isdir(remote_subject_dir):
        raise Exception('No reocon-all files!')
    # print(('No reocon-all files!'))
    return remote_subject_dir
Example #29
0
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import os.path as op

from src.utils import utils
from src.utils import figures_utils as figu

LINKS_DIR = utils.get_links_dir()
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')


def create_BuPu_YlOrRd_cm(n=128):
    colors1 = plt.cm.PuBu(np.linspace(1, 0, n))
    colors2 = plt.cm.YlOrRd(np.linspace(0, 1, n))
    colors = np.vstack((colors1, colors2))
    colors_map = mcolors.LinearSegmentedColormap.from_list('BuPu_YlOrRd', colors)
    return colors_map


def create_jet_cm(n=256):
    colors = plt.cm.jet(np.linspace(0, 1, n))
    colors_map = mcolors.LinearSegmentedColormap.from_list('jet', colors)
    return colors_map


def create_YlOrRd_cm(n=256):
    colors = plt.cm.YlOrRd(np.linspace(0, 1, n))
    colors_map = mcolors.LinearSegmentedColormap.from_list('YlOrRd', colors)
    return colors_map
Example #30
0
import os
import os.path as op
from collections import defaultdict
import glob
import traceback
import shutil
import collections
import logging

from src.utils import utils
from src.utils import args_utils as au

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
MMVT_DIR = op.join(LINKS_DIR, 'mmvt')


def decode_subjects(subjects):
    for sub in subjects:
        if '*' in sub:
            subjects.remove(sub)
            subjects.extend([
                utils.namebase(fol)
                for fol in glob.glob(op.join(SUBJECTS_DIR, sub))
            ])
        elif 'file:' in sub:
            subjects.remove(sub)
            subjects.extend(utils.read_list_from_file(sub[len('file:'):]))
    return subjects

Example #31
0
try:
    from sklearn.neighbors import BallTree
except:
    print('No sklearn!')

import shutil

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from src.utils import utils
from src.utils import freesurfer_utils as fu
from src.preproc import meg_preproc as meg

LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
SUBJECTS_MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
FREE_SURFER_HOME = utils.get_link_dir(LINKS_DIR, 'freesurfer',
                                      'FREESURFER_HOME')
BLENDER_ROOT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
FMRI_DIR = utils.get_link_dir(LINKS_DIR, 'fMRI')
os.environ['FREESURFER_HOME'] = FREE_SURFER_HOME
os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
# SUBJECTS_DIR = '/homes/5/npeled/space3/subjects'
# # SUBJECTS_DIR = '/autofs/space/lilli_001/users/DARPA-MEG/freesurfs'
# # SUBJECTS_DIR =  '/home/noam/subjects/mri'
# # SUBJECT = 'ep001'
# os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
# ROOT_DIR = [f for f in ['/homes/5/npeled/space3/fMRI/MSIT', '/home/noam/fMRI/MSIT'] if op.isdir(f)][0]
# BLENDER_DIR = '/homes/5/npeled/space3/visualization_blender'
# FREE_SURFER_HOME = utils.get_exisiting_dir([os.environ.get('FREESURFER_HOME', ''),
Example #32
0
from collections import Counter, defaultdict

import mne
import numpy as np
import scipy.io as sio
import nibabel as nib

from src.utils import labels_utils as lu
from src.utils import matlab_utils
from src.utils import utils
from src.utils import freesurfer_utils as fu
from src.mmvt_addon import colors_utils as cu


LINKS_DIR = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
FREE_SURFER_HOME = utils.get_link_dir(LINKS_DIR, 'freesurfer', 'FREESURFER_HOME')
MMVT_DIR = op.join(LINKS_DIR, 'mmvt')
os.environ['SUBJECTS_DIR'] = SUBJECTS_DIR
BRAINDER_SCRIPTS_DIR = op.join(utils.get_parent_fol(utils.get_parent_fol()), 'brainder_scripts')
ASEG_TO_SRF = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf -s "{}"') # -o {}'
HEMIS = ['rh', 'lh']


def subcortical_segmentation(subject, overwrite_subcortical_objs=False):
    # Must source freesurfer. You need to have write permissions on SUBJECTS_DIR
    if not op.isfile(op.join(SUBJECTS_DIR, subject, 'mri', 'norm.mgz')):
        return False
    script_fname = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf')
    if not op.isfile(script_fname):
        raise Exception('The subcortical segmentation script is missing! {}'.format(script_fname))
Example #33
0
import numpy as np
import os.path as op
import glob
import time
import traceback

from src.utils import utils
from src.utils import labels_utils as lu

links_dir = utils.get_links_dir()
SUBJECTS_DIR = utils.get_link_dir(links_dir, 'subjects', 'SUBJECTS_DIR')
MMVT_DIR = utils.get_link_dir(
    links_dir,
    'mmvt',
)


def mode(arr):
    return np.bincount(arr).argmax()
    #from collections import Counter
    #return Counter(arr).most_common(1)


# @utils.profileit(root_folder=op.join(MMVT_DIR, 'profileit'))
def parcelate(subject,
              atlas,
              hemi,
              surface_type,
              vertices_labels_ids_lookup=None,
              overwrite_vertices_labels_lookup=False):
    output_fol = op.join(MMVT_DIR, subject, 'labels',
Example #34
0
def get_links():
    links_dir = utils.get_links_dir()
    subjects_dir = utils.get_link_dir(links_dir, 'subjects', 'SUBJECTS_DIR')
    freesurfer_home = utils.get_link_dir(links_dir, 'freesurfer', 'FREESURFER_HOME')
    mmvt_dir = utils.get_link_dir(links_dir, 'mmvt')
    return subjects_dir, mmvt_dir, freesurfer_home
Example #35
0
import os.path as op
import glob
import shutil

from src.preproc import eeg
from src.preproc import meg
from src.utils import utils
from src.mmvt_addon.scripts import import_sensors


LINKS_DIR = utils.get_links_dir()
EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')


def init_flags(subject, task, conditions, remote_subject_dir=''):
    args = eeg.read_cmd_args(subject=subject)
    args.task = task
    args.sub_dirs_for_tasks = True
    eeg.init(subject, args)
    args.remote_subject_dir = remote_subject_dir
    args.conditions = list(conditions.values())
    args.t_min, args.t_max = -0.2, 0.2
    args.l_freq, args.h_freq = 1, 40
    args.inverse_method = ['MNE', 'sLORETA']
    args.inv_loose = 0.6
    args.calc_max_min_diff = False
    args.calc_evoked_for_all_epoches = args.calc_stc_for_all = False
    args.calc_stc_diff = False
    args.normalize_data = False
    args.use_raw_for_noise_cov = True
    args.overwrite_epochs, args.overwrite_evoked, args.overwrite_sensors, args.overwrite_stc = False, True, False, False
Example #36
0
import numpy as np
from itertools import product

try:
    import mplcursors
    MPLCURSORS_EXIST = True
except:
    MPLCURSORS_EXIST = False

from src.preproc import eeg
from src.preproc import meg
from src.utils import utils
from src.examples.epilepsy import utils as epi_utils

LINKS_DIR = utils.get_links_dir()
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')
SUBJECTS_DIR = utils.get_link_dir(LINKS_DIR, 'subjects')


def plot_topomaps(subject, modality, windows, bad_channels, parallel=True):
    figs_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'epilepsy-figures', 'topomaps'))
    params = [(subject, modality, window_fname, bad_channels, figs_fol)
              for window_fname in windows]
    utils.run_parallel(_plot_topomaps_parallel, params,
                       len(windows) if parallel else 1)


def _plot_topomaps_parallel(p):
Example #37
0
import os
import os.path as op
import numpy as np
import mne.io
import glob

from src.utils import utils
from src.preproc import meg_preproc as meg

LINKS_DIR = utils.get_links_dir()
SUBJECTS_MEG_DIR = utils.get_link_dir(LINKS_DIR, 'meg')
SUBJECTS_EEG_DIR = utils.get_link_dir(LINKS_DIR, 'eeg')
if SUBJECTS_EEG_DIR == '':
    print('No EEG folder, using MEG folder')
    SUBJECTS_EEG_DIR = SUBJECTS_MEG_DIR
if SUBJECTS_EEG_DIR == '':
    raise Exception('No EEG folder (not MEG)!')
SUBJECT_EEG_DIR = ''
SUBJECTS_MRI_DIR = utils.get_link_dir(LINKS_DIR, 'subjects', 'SUBJECTS_DIR')
FREESURFER_HOME = utils.get_link_dir(LINKS_DIR, 'freesurfer',
                                     'FREESURFER_HOME')
print('FREE_SURFER_HOME: {}'.format(FREESURFER_HOME))
MMVT_DIR = utils.get_link_dir(LINKS_DIR, 'mmvt')
os.environ['SUBJECTS_DIR'] = SUBJECTS_MRI_DIR


def read_eeg_sensors_layout(mri_subject):
    if not op.isfile(meg.INFO):
        raw = mne.io.read_raw_fif(meg.RAW)
        info = raw.info
        utils.save(info, meg.INFO)