Exemple #1
0
def calc_faces_verts_dic(subject, atlas, overwrite=False):
    # hemis_plus = HEMIS + ['cortex']
    ply_files = [op.join(MMVT_DIR, subject, 'surf', '{}.pial.npz'.format(hemi)) for hemi in utils.HEMIS]
    out_files = [op.join(MMVT_DIR, subject, 'faces_verts_{}.npy'.format(hemi)) for hemi in utils.HEMIS]
    subcortical_plys = glob.glob(op.join(MMVT_DIR, subject, 'subcortical', '*.ply'))
    errors = {}
    if len(subcortical_plys) > 0:
        faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, 'subcortical', '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in subcortical_plys]
        ply_files.extend(subcortical_plys)
        out_files.extend(faces_verts_dic_fnames)
    for hemi in utils.HEMIS:
        labels_plys = glob.glob(op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '*.ply'))
        if len(labels_plys) > 0:
            faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in labels_plys]
            ply_files.extend(labels_plys)
            out_files.extend(faces_verts_dic_fnames)

    for ply_file, out_file in zip(ply_files, out_files):
        if not overwrite and op.isfile(out_file):
            # print('{} already exist.'.format(out_file))
            continue
        # ply_file = op.join(SUBJECTS_DIR, subject,'surf', '{}.pial.ply'.format(hemi))
        # print('preparing a lookup table for {}'.format(ply_file))
        verts, faces = utils.read_ply_file(ply_file)
        errors = utils.calc_ply_faces_verts(verts, faces, out_file, overwrite, utils.namebase(ply_file), errors)
    if len(errors) > 0:
        for k, message in errors.items():
            print('{}: {}'.format(k, message))
    return len(errors) == 0
Exemple #2
0
def calc_linda_surf(subject, atlas):
    # Check for Linda's output fname
    if not utils.both_hemi_files_exist(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format('{hemi}'))) \
            and not op.isfile(op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl')):
        # Find Linda's files
        linda_volume_fnames = glob.glob(op.join(
            linda_fol.format(subject=subject), linda_vol_template.format(subject=subject)))
        linda_volume_folder = utils.get_parent_fol(linda_volume_fnames[0])
        # project linda files on the surface
        args = fmri.read_cmd_args(dict(
            subject=subject, function='project_volume_to_surface', remote_fmri_dir=linda_volume_folder,
            fmri_file_template=linda_vol_template.format(subject=subject), overwrite_surf_data=True))
        pu.run_on_subjects(args, fmri.main)
        # rename Linda's files
        linda_fnames = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}'.format(
            linda_template_npy.format(subject=subject))))
        for fname in linda_fnames:
            hemi = lu.get_label_hemi(utils.namebase(fname))
            target_file = op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format(hemi))
            if not op.isfile(target_file):
                os.rename(fname, target_file)
        # rename minmax file
        linda_minmax_name = '{}.pkl'.format(utils.namebase(glob.glob(op.join(
            fmri.MMVT_DIR, subject, 'fmri', '{}_minmax.pkl'.format(
                utils.namebase(linda_vol_template.format(subject=subject)))))[0]))
        os.rename(op.join(fmri.MMVT_DIR, subject, 'fmri', linda_minmax_name),
                  op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl'))
        # delete mgz files
        mgz_files = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}_?h.mgz'.format(
            utils.namebase(linda_vol_template.format(subject=subject)))))
        for mgz_file in mgz_files:
            os.remove(mgz_file)
Exemple #3
0
def analyze_graph(subject,
                  fif_fname,
                  band_name,
                  graph_func,
                  modality,
                  overwrite=False,
                  n_jobs=4):
    fol = op.join(MMVT_DIR, subject, 'connectivity')
    con_name = '{}_{}_{}_{}'.format(modality, utils.namebase(fif_fname),
                                    band_name, graph_func)
    output_fname = op.join(fol, '{}_mi.npy'.format(con_name))
    if op.isfile(output_fname) and not overwrite:
        print('{} already exists'.format(utils.namebase(output_fname)))
        return False
    input_fname = op.join(
        fol, '{}_{}_{}_mi.npy'.format(modality, utils.namebase(fif_fname),
                                      band_name))
    if not op.isfile(input_fname):
        print('{} does not exist!'.format(input_fname))
        return False
    print('Loading {}'.format(input_fname))
    con = np.load(input_fname).squeeze()
    values = analyze_graph_data(con, n_jobs)
    print('{}: min={}, max={}, mean={}'.format(con_name, np.min(values),
                                               np.max(values),
                                               np.mean(values)))
    print('Saving {}'.format(output_fname))
    np.save(output_fname, values)
Exemple #4
0
def calc_labels_avg(target_subject, hemi, atlas, fmri_fname, res_dir, cwd, overwrite=True, output_txt_fname='',
                    output_sum_fname='', ret_files_name=False, **kargs):
    def get_labels_names(line):
        label_name = line.split()[4]
        label_nums = utils.find_num_in_str(label_name)
        label_num = label_nums[-1] if len(label_nums) > 0 else ''
        if label_num != '':
            name_len = label_name.find('_{}'.format(label_num)) + len(str(label_num)) + 1
            label_name = '{}-{}'.format(label_name[:name_len], hemi)
        return label_name

    if output_txt_fname == '':
        output_txt_fname = op.join(res_dir, '{}_{}_{}.txt'.format(utils.namebase(fmri_fname), atlas, hemi))
    if output_sum_fname == '':
        output_sum_fname = op.join(res_dir, '{}_{}_{}.sum'.format(utils.namebase(fmri_fname), atlas, hemi))
    if not op.isfile(output_txt_fname) or not op.isfile(output_sum_fname) or overwrite:
        print('Running mri_segstats on {} ({})'.format(fmri_fname, utils.file_modification_time(fmri_fname)))
        utils.partial_run_script(locals(), cwd=cwd)(mri_segstats)
    if not op.isfile(output_txt_fname):
        raise Exception('The output file was not created!')
    labels_data = np.genfromtxt(output_txt_fname).T
    labels_names = utils.read_list_from_file(output_sum_fname, get_labels_names, 'rb')
    if ret_files_name:
        return labels_data, labels_names, output_txt_fname, output_sum_fname
    else:
        return labels_data, labels_names
Exemple #5
0
def calc_rois_connectivity(
        subject, clips, modality, atlas, inverse_method, min_order=1, max_order=20, crop_times=(-0.5, 1),
        onset_time=2, windows_length=0.1, windows_shift=0.05, overwrite=False, n_jobs=4):
    windows_length *= 1000
    windows_shift *= 1000
    params = []
    clusters_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality), 'clusters')
    fwd_usingMEG, fwd_usingEEG = meg.get_fwd_flags(modality)
    bands = {'all': [None, None]}
    crop_times = [t + onset_time for t in crop_times]
    use_functional_rois_atlas = False
    conds = [utils.namebase(clip_fname) for clip_fname in clips['ictal']]
    conds.extend(['{}_baseline'.format(utils.namebase(clip_fname)) for clip_fname in clips['baseline']])
    if not use_functional_rois_atlas:
        labels = lu.read_labels(subject, SUBJECTS_DIR, atlas)
        func_atlas = con_indentifer = atlas
    for clip_fname, cond in zip(clips['ictal'] + clips['baseline'], conds):
        if use_functional_rois_atlas:
            check_connectivity_labels(clips['ictal'], modality, inverse_method, n_jobs=n_jobs)
            labels_fol = op.join(
                clusters_fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format(
                    subject, inverse_method, modality, utils.namebase(clip_fname)))
            labels = lu.read_labels_files(subject, labels_fol, n_jobs=n_jobs)
            # for connectivity we need shorter names
            labels = epi_utils.shorten_labels_names(labels)
            func_atlas = utils.namebase(clip_fname)
            con_indentifer = 'func_rois'
        params.append((
            subject, clip_fname, utils.namebase(clip_fname), func_atlas, labels, inverse_method, fwd_usingMEG,
            fwd_usingEEG, crop_times, bands, min_order, max_order, windows_length, windows_shift, con_indentifer,
            overwrite, 1))

    utils.run_parallel(_calc_clip_rois_connectivity_parallel, params, n_jobs)
Exemple #6
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
Exemple #7
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)
Exemple #8
0
def _calc_zvals_parallel(p):
    subject, clip_fname, baseline_fnames, modality, inverse_method, fol, from_index, to_index, use_abs, overwrite = p
    stc_zvals_fname = op.join(
        fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format(
            subject, inverse_method, modality, utils.namebase(clip_fname)))
    if utils.both_hemi_files_exist('{}-{}.stc'.format(
            stc_zvals_fname, '{hemi}')) and not overwrite:
        return True
    stc_fname = op.join(
        MMVT_DIR, subject, meg.modality_fol(modality),
        'ictal-{}-stcs'.format(inverse_method),
        '{}-epilepsy-{}-{}-{}'.format(subject, inverse_method, modality,
                                      utils.namebase(clip_fname)))
    if not utils.both_hemi_files_exist('{}-{}.stc'.format(stc_fname,
                                                          '{hemi}')):
        print('Error finding {}!'.format(stc_fname))
        return False
    return meg.calc_stc_zvals(
        subject,
        '{}-rh.stc'.format(stc_fname),
        baseline_fnames,
        stc_zvals_fname,  # '{}-rh.stc'.format(baseline_fname)
        use_abs,
        from_index,
        to_index,
        True,
        overwrite)
Exemple #9
0
def _calc_induced_power_zvals_parallel(p):
    subject, modality, window_fname, baseline_name, bands, from_index, to_index, inverse_method, overwrite = p
    module = eeg if modality == 'eeg' else meg
    stc_template = '{}-epilepsy-{}-{}-{}_{}'.format(subject, inverse_method,
                                                    modality, '{window}',
                                                    '{band}')
    root_fol = op.join(MMVT_DIR, subject,
                       'eeg' if modality == 'eeg' else 'meg')
    if all([utils.stc_exist(op.join(root_fol, '{}-zvals'.format(
            stc_template.format(window=utils.namebase(window_fname), band=band)))) \
            for band in bands]) and not overwrite:
        return
    for band in bands:
        window_stc_name = stc_template.format(
            window=utils.namebase(window_fname), band=band)
        args = module.read_cmd_args(
            dict(subject=subject,
                 mri_subject=subject,
                 function='calc_stc_zvals',
                 task='epilepsy',
                 stc_name=window_stc_name,
                 baseline_stc_name=stc_template.format(window=baseline_name,
                                                       band=band),
                 from_index=from_index,
                 to_index=to_index,
                 use_abs=1,
                 overwrite_stc=overwrite))
        module.call_main(args)
Exemple #10
0
def save_sz_pick_values(subject, files_names, func_name, atlas):
    bands = dict(theta=[4, 8],
                 alpha=[8, 15],
                 beta=[15, 30],
                 gamma=[30, 55],
                 high_gamma=[65, 120])
    output_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'labels', 'labels_data'))
    names = np.load(
        op.join(op.join(MMVT_DIR, subject, 'connectivity',
                        'labels_names.npy')))
    labels_indices = np.load(
        op.join(
            op.join(MMVT_DIR, subject, 'connectivity', 'labels_indices.npy')))
    names = names[labels_indices]
    for band_name in bands.keys():
        output_fname = op.join(output_fol,
                               '{}_{}.npz'.format(func_name, band_name))
        # if op.isfile(output_fname):
        #     continue
        fname = [
            fname for fname in files_names
            if utils.namebase(fname).endswith('SZ')
        ][0]
        file_name = utils.namebase(fname)
        con_name = 'meg_{}_mi'.format(band_name)
        input_fname = op.join(MMVT_DIR, subject, 'connectivity', file_name,
                              '{}_{}.npy'.format(con_name, func_name))
        vals = np.load(input_fname)
        data_min, data_max = np.min(vals), np.max(vals)
        t_axis = np.linspace(-2, 5, vals.shape[1])
        print('onset index: {}'.format(np.where(t_axis > 0)[0][0]))
        # Find pick in time
        t_peak = np.argmax(np.max(vals, axis=0))
        max_vals = vals[:, t_peak]
        # all_vals = np.zeros((len(names)))
        # all_vals = [max_vals[list(names).index(l)] if l in names else 0 for l in labels]
        print('Saving {}'.format(output_fname))
        np.savez(output_fname,
                 data=vals,
                 names=names,
                 atlas=atlas,
                 data_min=data_min,
                 data_max=data_max,
                 title='{}-{}'.format(func_name, band_name),
                 cmap='YlOrRd')
        for hemi in utils.HEMIS:
            labels_output_fname = op.join(
                MMVT_DIR, subject, 'meg',
                'labels_data_epilepsy_laus125_{}_{}_{}.npz'.format(
                    band_name, func_name, hemi))
            hemis = np.array([lu.get_hemi_from_name(l) == hemi for l in names])
            np.savez(labels_output_fname,
                     data=vals[hemis],
                     names=names[hemis],
                     conditions=['sz'])
Exemple #11
0
def plot_all_files_graph_max(subject,
                             baseline_fnames,
                             event_fname,
                             func_name,
                             bands_names,
                             modality,
                             input_template,
                             sz_name='',
                             sfreq=None,
                             do_plot=False,
                             overwrite=False):
    if sz_name == '':
        sz_name = utils.namebase(event_fname)
    output_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'connectivity',
                '{}_{}'.format(modality, func_name), 'runs', sz_name))
    if modality == 'ieeg':
        # clip = np.load(event_fname)
        t_start, t_end = -5, 5
    else:
        clip = mne.read_evokeds(event_fname)[0]
        sfreq = clip.info['sfreq']
        t_start, t_end = clip.times[0], clip.times[-1]

    windows_length = 500
    half_window = (1 / sfreq) * (windows_length / 2)  # In seconds
    scores = {}
    for band_name in bands_names:
        band_fol = utils.make_dir(
            op.join(MMVT_DIR, subject, 'connectivity',
                    '{}_{}'.format(modality, func_name), band_name))
        con_name = '{}_{}_mi'.format(modality, band_name)
        figure_name = '{}_{}_{}.jpg'.format(con_name, func_name, sz_name)
        output_fname = op.join(output_fol, figure_name)
        # if op.isfile(output_fname) and not overwrite:
        #     print('{} already exist'.format(figure_name))
        #     continue
        all_files_found = True
        for fname in baseline_fnames + [event_fname]:
            if input_template != '':
                file_name = utils.namebase(fname)
                input_fname = input_template.format(file_name=file_name,
                                                    band_name=band_name)
                if not op.isfile(input_fname):
                    print('{} does not exist!!!'.format(input_fname))
                    all_files_found = False
                    break
        if not all_files_found:
            continue
        scores[band_name] = calc_score(event_fname, baseline_fnames,
                                       input_template, band_name, t_start,
                                       t_end, half_window, output_fname,
                                       band_fol, figure_name, do_plot)
    return scores
Exemple #12
0
def plot_stc_file(stc_fname, figures_fol):
    stc_name = utils.namebase(stc_fname)[:-3]
    fig_fname = op.join(figures_fol, '{}.jpg'.format(stc_name))
    if not op.isfile(fig_fname):
        stc = mne.read_source_estimate(stc_fname)
        data = np.max(stc.data, axis=0)
        plt.figure()
        plt.plot(data.T)
        plt.title(utils.namebase(stc_fname)[:-3])
        print('Saving {}'.format(fig_fname))
        plt.savefig(fig_fname, dpi=300)
        plt.close()
Exemple #13
0
def move_non_zvals_stcs(subject, modality):
    # Move not zvals stc files
    modality_fol = op.join(MMVT_DIR, subject,
                           'eeg' if modality == 'eeg' else 'meg')
    non_zvlas_fol = utils.make_dir(op.join(modality_fol, 'non-zvals'))
    stc_files = [
        f for f in glob.glob(op.join(modality_fol, '*.stc'))
        if '-epilepsy-' in utils.namebase(f)
        and not '-zvals-' in utils.namebase(f)
    ]
    for stc_fname in stc_files:
        utils.move_file(stc_fname, non_zvlas_fol, overwrite=True)
Exemple #14
0
def plot_con(subject, files, band_name):
    fname = [fname for fname in files
             if utils.namebase(fname).endswith('SZ')][0]
    file_name = utils.namebase(fname)
    con_name = 'meg_{}_mi'.format(band_name)
    fol = op.join(MMVT_DIR, subject, 'connectivity', file_name)
    con = np.load(op.join(fol, '{}.npy'.format(con_name))).squeeze()
    print(np.unravel_index(np.argmax(con), con.shape))
    t_axis = np.linspace(-2, 5, con.shape[2])
    plt.plot(t_axis, con[0].T)
    plt.title(con_name)
    plt.show()
Exemple #15
0
def check_connectivity_labels(ictal_clips, modality, inverse_method, n_jobs=1):
    # Check if can calc the labels info (if not, we want to know now...)
    for clip_fname in ictal_clips:
        print('Checking {} labels'.format(utils.namebase(clip_fname)))
        labels_fol = op.join(
            MMVT_DIR, subject, meg.modality_fol(modality), 'clusters', '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format(
                subject, inverse_method, modality, utils.namebase(clip_fname)))
        labels = lu.read_labels_files(subject, labels_fol, n_jobs=n_jobs)
        if len(labels) == 0:
            raise Exception('No labels!')
        else:
            print('{} labels'.format(len(labels)))
        connectivity.calc_lables_info(subject, labels=labels)
Exemple #16
0
def plot_stcs_files(subject, modality, n_jobs=4):
    modality_fol = op.join(MMVT_DIR, subject,
                           'eeg' if modality == 'eeg' else 'meg')
    stc_files = [
        f for f in glob.glob(op.join(modality_fol, '*-lh.stc'))
        if '-epilepsy-' in utils.namebase(f) and '-zvals-' in utils.namebase(f)
        and '-{}-'.format(modality) in utils.namebase(f)
    ]
    print('{} files for {}'.format(len(stc_files), modality))
    figures_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'epilepsy-figures', 'all_stcs', modality))
    utils.run_parallel(_plot_stcs_files_parallel,
                       [(stc_fname, figures_fol) for stc_fname in stc_files],
                       n_jobs)
Exemple #17
0
def calc_faces_verts_dic(subject, atlas, overwrite=False):
    # hemis_plus = HEMIS + ['cortex']
    ply_files = [op.join(MMVT_DIR, subject, 'surf', '{}.pial.npz'.format(hemi)) for hemi in utils.HEMIS]
    out_files = [op.join(MMVT_DIR, subject, 'faces_verts_{}.npy'.format(hemi)) for hemi in utils.HEMIS]
    subcortical_plys = glob.glob(op.join(MMVT_DIR, subject, 'subcortical', '*.ply'))
    errors = []
    if len(subcortical_plys) > 0:
        faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, 'subcortical', '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in subcortical_plys]
        ply_files.extend(subcortical_plys)
        out_files.extend(faces_verts_dic_fnames)
    for hemi in utils.HEMIS:
        labels_plys = glob.glob(op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '*.ply'))
        if len(labels_plys) > 0:
            faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in labels_plys]
            ply_files.extend(labels_plys)
            out_files.extend(faces_verts_dic_fnames)

    for ply_file, out_file in zip(ply_files, out_files):
        if not overwrite and op.isfile(out_file):
            # print('{} already exist.'.format(out_file))
            continue
        # ply_file = op.join(SUBJECTS_DIR, subject,'surf', '{}.pial.ply'.format(hemi))
        # print('preparing a lookup table for {}'.format(ply_file))
        verts, faces = utils.read_ply_file(ply_file)
        _faces = faces.ravel()
        print('{}: verts: {}, faces: {}, faces ravel: {}'.format(utils.namebase(ply_file), verts.shape[0], faces.shape[0], len(_faces)))
        faces_arg_sort = np.argsort(_faces)
        faces_sort = np.sort(_faces)
        faces_count = Counter(faces_sort)
        max_len = max([v for v in faces_count.values()])
        lookup = np.ones((verts.shape[0], max_len)) * -1
        diff = np.diff(faces_sort)
        n = 0
        for ind, (k, v) in enumerate(zip(faces_sort, faces_arg_sort)):
            lookup[k, n] = v
            n = 0 if ind < len(diff) and diff[ind] > 0 else n+1
        # print('writing {}'.format(out_file))
        np.save(out_file, lookup.astype(np.int))
        print('{} max lookup val: {}'.format(utils.namebase(ply_file), int(np.max(lookup))))
        if len(_faces) != int(np.max(lookup)) + 1:
            errors[utils.namebase(ply_file)] = 'Wrong values in lookup table! ' + \
                'faces ravel: {}, max looup val: {}'.format(len(_faces), int(np.max(lookup)))
    if len(errors) > 0:
        for k, message in errors.items():
            print('{}: {}'.format(k, message))
    return len(errors) == 0
Exemple #18
0
def create_eeg_mesh(subject, excludes=[], overwrite_faces_verts=True):
    try:
        from scipy.spatial import Delaunay
        from src.utils import trig_utils
        input_file = op.join(MMVT_DIR, subject, 'eeg', 'eeg_positions.npz')
        mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
        faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
        f = np.load(input_file)
        verts = f['pos']
        excluded_inds = [np.where(f['names'] == e)[0] for e in excludes]
        # verts = np.delete(verts, excluded_inds, 0)
        verts_tup = [(x, y, z) for x, y, z in verts]
        tris = Delaunay(verts_tup)
        faces = tris.convex_hull
        areas = [trig_utils.poly_area(verts[poly]) for poly in tris.convex_hull]
        inds = [k for k, s in enumerate(areas) if s > np.percentile(areas, 97)]
        faces = np.delete(faces, inds, 0)
        utils.write_ply_file(verts, faces, mesh_ply_fname, True)
        utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, overwrite_faces_verts,
                                   utils.namebase(faces_verts_out_fname))
        np.savez(input_file, pos=f['pos'], names=f['names'], tri=faces, excludes=excludes)
    except:
        print('Error in create_eeg_mesh!')
        print(traceback.format_exc())
        return False
    return True
Exemple #19
0
def create_evoked_responses(root_fol,
                            task,
                            atlas,
                            events_id,
                            fname_format,
                            fwd_fol,
                            neccesary_files,
                            remote_subjects_dir,
                            fsaverage,
                            raw_cleaning_method,
                            inverse_method,
                            overwrite_epochs=False,
                            overwrite_evoked=False):
    errors = []
    hc_subjects_epo_filess = glob.glob(op.join(root_fol, 'hc*arc*epo.fif'))
    for subject_epo_fname in hc_subjects_epo_filess:
        try:
            subject = utils.namebase(subject_epo_fname).split('_')[0]
            calc_subject_evoked_response(subject, root_fol, task, atlas,
                                         events_id, fname_format, fwd_fol,
                                         neccesary_files, remote_subjects_dir,
                                         fsaverage, raw_cleaning_method,
                                         inverse_method, overwrite_epochs,
                                         overwrite_evoked)
        except:
            print('******* Error with {} *******'.format(subject))
            print(traceback.format_exc())
            errors.append(subject)

    for subject_err in errors:
        print('Error with {}'.format(subject_err))
Exemple #20
0
def _calc_amplitude_zvals_parallel(p):
    # python3 -m src.preproc.meg -s nmr00857 -f calc_stc_zvals --stc_name nmr00857-epilepsy-dSPM-meeg-43.9s
    #   --baseline_stc_name nmr00857-epilepsy-dSPM-meeg-37.3_BGprSzs --use_abs 1 --overwrite_stc 1
    subject, modality, window_fname, baseline_name, from_index, to_index, inverse_method, overwrite = p
    module = eeg if modality == 'eeg' else meg
    window = utils.namebase(window_fname)
    stc_template = '{}-epilepsy-{}-{}-{}{}'.format(subject, inverse_method,
                                                   modality, '{window}',
                                                   '{suffix}')
    window_stc_name = stc_template.format(window=window, suffix='')
    args = module.read_cmd_args(
        dict(subject=subject,
             mri_subject=subject,
             function='calc_stc_zvals',
             task='epilepsy',
             stc_name=window_stc_name,
             baseline_stc_name=stc_template.format(window=baseline_name,
                                                   suffix=''),
             stc_zvals_name=stc_template.format(window=window,
                                                suffix='_amplitude-zvals'),
             from_index=from_index,
             to_index=to_index,
             use_abs=1,
             overwrite_stc=overwrite))
    module.call_main(args)
Exemple #21
0
def plot_connectivity(subject, clips_dict, modality, inverse_method):
    for clip_fname in clips_dict['ictal']:
        plots.plot_connectivity(
            subject,
            utils.namebase(clip_fname),
            modality,
            120,
            'gc',
            cond_name='',
            node_name='',  # superiorfrontal
            stc_subfolder='ictal-{}-zvals-stcs'.format(inverse_method),
            bands={'all': [1, 120]},
            stc_downsample=1,
            stc_name='{}-epilepsy-{}-{}-{}-amplitude-zvals-rh.stc'.format(
                subject, inverse_method, modality, utils.namebase(clip_fname)),
            con_threshold=0)
Exemple #22
0
def calc_hesheng_surf(subject, atlas):
    subject_fol = op.join(fmri.MMVT_DIR, subject, 'fmri')
    if not (utils.both_hemi_files_exist(
            op.join(subject_fol, 'fmri_hesheng_{hemi}.npy'))
            and op.isfile(op.join(subject_fol, 'hesheng_minmax.pkl'))):
        # Copy and rename Hesheng's files
        hesheng_fnames = glob.glob(
            op.join(hesheng_surf_fol.format(subject=subject),
                    hesheng_template.format(subject=subject)))
        for fname in hesheng_fnames:
            hemi = lu.get_label_hemi_invariant_name(utils.namebase(fname))
            target_file = op.join(fmri.FMRI_DIR, subject,
                                  'hesheng_{}.nii.gz'.format(hemi))
            mgz_target_file = utils.change_fname_extension(target_file, 'mgz')
            if not op.isfile(mgz_target_file):
                shutil.copy(fname, target_file)
                fu.nii_gz_to_mgz(target_file)
                os.remove(target_file)
        # Load Hesheng's files
        args = fmri.read_cmd_args(
            dict(subject=subject,
                 atlas=atlas,
                 function='load_surf_files',
                 overwrite_surf_data=True,
                 fmri_file_template='hesheng_{hemi}.mgz'))
        pu.run_on_subjects(args, fmri.main)
Exemple #23
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)
Exemple #24
0
def calc_vertices_lookup_tables(subject, modality, window, inverse_method,
                                labels, inv):
    output_fname = op.join(MMVT_DIR, subject, 'vertices_lookup_tables.pkl')
    # if op.isfile(output_fname):
    #     vertices_ind_to_no_lookup, vertices_no_to_ind_lookup, vertices_labels_lookup = utils.load(output_fname)
    #     return vertices_lookup, vertices_labels_lookup
    root_dir = op.join(EEG_DIR if modality == 'eeg' else MEG_DIR, subject)
    powers_fol = op.join(
        root_dir,
        '{}-epilepsy-{}-{}-{}-induced_power'.format(subject, inverse_method,
                                                    modality, window))
    powers_files = glob.glob(
        op.join(powers_fol, 'epilepsy_*_induced_power.npy'))
    start_ind = 0
    vertice_label = None
    vertices_ind_to_no_lookup = {}
    vertices_labels_lookup = {}
    for file_ind, powers_fname in enumerate(powers_files):
        label_name = utils.namebase(powers_fname).split('_')[1]
        label = [l for l in labels if l.name == label_name][0]
        vertno, src_sel = mne.minimum_norm.inverse.label_src_vertno_sel(
            label, inv['src'])
        for vert_ind, vert_num in zip(
                range(start_ind, start_ind + len(src_sel)),
                vertno[0] if label.hemi == 'lh' else vertno[1]):
            vertices_ind_to_no_lookup[vert_ind] = vert_num
            vertices_labels_lookup[vert_ind] = label
        # if start_ind <= vertices_ind < start_ind + len(src_sel):
        #     vertice_label = label
        #     break
        start_ind += len(src_sel)
    utils.save((vertices_ind_to_no_lookup, vertices_labels_lookup),
               output_fname)
    return vertices_ind_to_no_lookup, vertices_labels_lookup
Exemple #25
0
def combine_windows_into_epochs(windows, epochs_fname='', overwrite=False):
    if op.isfile(epochs_fname) and not overwrite:
        epochs = mne.read_epochs(epochs_fname)
        return epochs
    epochs_list, info = [], None
    for window_fname in windows:
        window_name = utils.namebase(window_fname)
        evoked = mne.read_evokeds(window_fname)[0]
        if info is None:
            C, T = evoked.data.shape
            info = evoked.info
        else:
            _C, _T = evoked.data.shape
            if _C != C or _T != T:
                print('{}: dims mismatch! {} != {} or {} != {}'.format(
                    window_name, _C, C, _T, T))
                continue
        epoch = mne.EpochsArray(evoked.data.reshape((1, C, T)), evoked.info,
                                np.array([[0, 0, 1]]), 0, 1)[0]
        epochs_list.append(epoch)
    epochs = mne.concatenate_epochs(epochs_list, True)
    if epochs_fname != '':
        print('Saving epochs to {}'.format(epochs_fname))
        epochs.save(epochs_fname)
    return epochs
Exemple #26
0
def find_template_brain_with_annot_file(aparc_name,
                                        fsaverage,
                                        subjects_dir,
                                        find_in_all=True):
    fs_found = False
    if find_in_all:
        fsaverage = [
            utils.namebase(d) for d in glob.glob(op.join(subjects_dir, 'fs*'))
        ]
    elif isinstance(fsaverage, str):
        fsaverage = [fsaverage]
    for fsav in fsaverage:
        fsaverage_annot_files_exist = utils.both_hemi_files_exist(
            op.join(subjects_dir, fsav, 'label',
                    '{}.{}.annot'.format('{hemi}', aparc_name)))
        fsaverage_labels_exist = len(
            glob.glob(
                op.join(subjects_dir, fsav, 'label', aparc_name,
                        '*.label'))) > 0
        if fsaverage_annot_files_exist or fsaverage_labels_exist:
            fsaverage = fsav
            fs_found = True
            break
    if not fs_found:
        print("Can't find the annot file for any of the templates brain!")
        return ''
    else:
        return fsaverage
Exemple #27
0
def concatenate_powers(fol, return_file_names=False):
    print('Concatenate powers in {}'.format(fol))
    powers_files = glob.glob(op.join(fol, 'epilepsy_*_induced_power.npy'))
    if len(powers_files) == 0:
        print('No files in {}!'.format(fol))
        return None
    # if len(powers_files) != 62: # Should calc number of lables
    #     print('{}: Not all the files were created!'.format(fol))
    #     return None
    try:
        powers = np.concatenate([
            np.load(powers_fname).astype(np.float32)
            for powers_fname in powers_files
        ])
    except:
        for powers_fname in powers_files:
            try:
                x = np.load(powers_fname)
                print(utils.namebase(powers_fname), x.shape)
            except:
                print('Can\'t load {}!'.format(powers_fname))
        return None
    if return_file_names:
        return powers, powers_files
    else:
        return powers
Exemple #28
0
def create_eeg_mesh(subject, excludes=[], overwrite_faces_verts=True):
    try:
        from scipy.spatial import Delaunay
        from src.utils import trig_utils
        input_file = op.join(MMVT_DIR, subject, 'eeg', 'eeg_positions.npz')
        mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
        faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg',
                                        'eeg_faces_verts.npy')
        f = np.load(input_file)
        verts = f['pos']
        excluded_inds = [np.where(f['names'] == e)[0] for e in excludes]
        # verts = np.delete(verts, excluded_inds, 0)
        verts_tup = [(x, y, z) for x, y, z in verts]
        tris = Delaunay(verts_tup)
        faces = tris.convex_hull
        areas = [
            trig_utils.poly_area(verts[poly]) for poly in tris.convex_hull
        ]
        inds = [k for k, s in enumerate(areas) if s > np.percentile(areas, 97)]
        faces = np.delete(faces, inds, 0)
        utils.write_ply_file(verts, faces, mesh_ply_fname, True)
        utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname,
                                   overwrite_faces_verts,
                                   utils.namebase(faces_verts_out_fname))
        np.savez(input_file,
                 pos=f['pos'],
                 names=f['names'],
                 tri=faces,
                 excludes=excludes)
    except:
        print('Error in create_eeg_mesh!')
        print(traceback.format_exc())
        return False
    return True
Exemple #29
0
def get_tal_coordaintes(files):
    rois, errors = {}, {}
    driver = None
    for fname in tqdm(files):
        # subject = utils.namebase(utils.get_parent_fol(fname, 3))
        roi = utils.namebase(fname).split('.')[0]
        if roi not in rois:
            rois[roi] = {}
            rois[roi]['tal'] = []
            rois[roi]['mni'] = []
        lines = list(utils.csv_file_reader(fname, delimiter=' '))
        if len(lines) == 0:
            errors[fname] = '{} is empty!'.format(fname)
            continue
        elif len(lines) > 1:
            errors[fname] = 'More than one line in {}!'.format(fname)
            continue
        tal = [int(float(v)) for v in lines[0] if utils.is_float(v)]
        rois[roi]['tal'].append(tal)
        if driver is None:
            driver = tu.yale_get_driver()
        rois[roi]['mni'].append(tu.yale_tal2mni(tal, driver))
    if len(errors) > 0:
        print(errors)
    del driver
    return rois
Exemple #30
0
def change_frames_names(fol,
                        images_prefix,
                        images_type,
                        images_format_len,
                        new_fol_name='new_images'):
    import shutil
    images = glob.glob(
        op.join(fol, '{}*.{}'.format(images_prefix, images_type)))
    images.sort(key=lambda x: int(utils.namebase(x)[len(images_prefix):]))
    images_formats = {
        1: '{0:0>1}',
        2: '{0:0>2}',
        3: '{0:0>3}',
        4: '{0:0>4}',
        5: '{0:0>5}'
    }
    root = op.join(op.sep.join(images[0].split(op.sep)[:-1]))
    new_fol = op.join(root, new_fol_name)
    utils.delete_folder_files(new_fol)
    utils.make_dir(new_fol)
    for num, image_fname in enumerate(images):
        num_str = images_formats[images_format_len].format(num + 1)
        new_image_fname = op.join(
            new_fol, '{}{}.{}'.format(images_prefix, num_str, images_type))
        print('{} -> {}'.format(image_fname, new_image_fname))
        utils.copy_file(image_fname, new_image_fname)
    return new_fol
Exemple #31
0
def calc_baseline_connectivity(ictals_clips, connectivity_template):
    baseline_mat1, baseline_mat2 = None, None
    for clip_fname in ictals_clips['baseline']:
        clip_name = utils.namebase(clip_fname)
        con_ictal_fname = connectivity_template.format(clip_name=clip_name)
        print('concatenate {}'.format(utils.namebase(clip_fname)))
        d = np.load(con_ictal_fname) # C, W, O = d['con_values'].shape
        con_values = connectivity.find_best_ord(d['con_values'], False)
        con_values2 = connectivity.find_best_ord(d['con_values2'], False)
        if baseline_mat1 is None:
            baseline_mat1 = con_values.copy()
            baseline_mat2 = con_values2.copy()
        else:
            baseline_mat1 = np.concatenate((baseline_mat1, con_values), axis=1)
            baseline_mat2 = np.concatenate((baseline_mat2, con_values2), axis=1)
    return baseline_mat1, baseline_mat2
Exemple #32
0
def _plot_modalities_parallel(p):
    subject, modalities, inverse_method, figures_fol, band, window_fname, max_t, overwrite = p
    window_name = utils.namebase(window_fname)
    plt.figure()
    for modality in modalities:
        modality_fol = op.join(MMVT_DIR, subject,
                               'eeg' if modality == 'eeg' else 'meg')
        fig_fname = op.join(figures_fol, '{}-{}.jpg'.format(window_name, band))
        if op.isfile(fig_fname) and not overwrite:
            print('{} already exist'.format(fig_fname))
            break
        stc_band_fname = op.join(
            modality_fol, '{}-epilepsy-{}-{}-{}_{}-zvals-lh.stc'.format(
                subject, inverse_method, modality, window_name, band))
        if not op.isfile(stc_band_fname):
            print('Can\'t find {}!'.format(stc_band_fname))
            break
        print('Loading {} ({})'.format(
            stc_band_fname, utils.file_modification_time(stc_band_fname)))
        stc = mne.read_source_estimate(stc_band_fname)
        data = np.max(stc.data[:, :max_t], axis=0) if max_t > 0 else np.max(
            stc.data, axis=0)
        plt.plot(data.T)
    else:
        plt.title('{} {}'.format(window_name, band))
        plt.legend(modalities)
        print('Saving {} {}'.format(window_name, band))
        plt.savefig(fig_fname, dpi=300)
        plt.close()
Exemple #33
0
def plot_windows(subject, windows, modality, bands, inverse_method):
    modality_fol = op.join(MMVT_DIR, subject,
                           'eeg' if modality == 'eeg' else 'meg')
    for window_fname in windows:
        window_name = utils.namebase(window_fname)
        figures_fol = utils.make_dir(
            op.join(MMVT_DIR, subject, 'epilepsy-figures',
                    'epilepsy-per_window-figures'))
        stc_name = '{}-epilepsy-{}-{}-{}'.format(subject, inverse_method,
                                                 modality, window_name)
        fig_fname = op.join(figures_fol, '{}.jpg'.format(stc_name))
        if op.isfile(fig_fname):
            print('{} already exist'.format(fig_fname))
            continue
        plt.figure()
        all_found = True
        for band in bands:
            stc_band_fname = op.join(
                modality_fol, '{}-epilepsy-{}-{}-{}_{}-zvals-lh.stc'.format(
                    subject, inverse_method, modality, window_name, band))
            if not op.isfile(stc_band_fname):
                print('Can\'t find {}!'.format(stc_band_fname))
                all_found = False
                break
            stc = mne.read_source_estimate(stc_band_fname)
            data = np.max(stc.data, axis=0)
            plt.plot(data.T)
        if all_found:
            plt.title(window_name)
            plt.legend(bands)
            print('Saving {}'.format(window_name))
            plt.savefig(fig_fname, dpi=300)
            plt.close()
Exemple #34
0
def get_t(images, image_index, time_range):
    if images is None:
        return 0
    pic_name = utils.namebase(images[image_index])
    if '_t' in pic_name:
        t = int(pic_name.split('_t')[1])
        # t = time_range[1:-1:4][t]
    else:
        t = int(re.findall('\d+', pic_name)[0])
    return time_range[t]
Exemple #35
0
def copy_evokes(task, root_fol, target_subject, raw_cleaning_method):
    hc_subjects_epo_filess = glob.glob(op.join(root_fol, 'hc*arc*epo.fif'))
    for subject_epo_fname in hc_subjects_epo_filess:
        subject = utils.namebase(subject_epo_fname).split('_')[0]
        events_fname = '{}_arc_rer_{}-epo.csv'.format(subject, raw_cleaning_method)
        indices = find_events_indices(op.join(root_fol, events_fname))
        if not indices is None:
            for hemi in utils.HEMIS:
                shutil.copy(op.join(SUBJECTS_MEG_DIR, task, subject, 'labels_data_{}.npz'.format(hemi)),
                            op.join(BLENDER_ROOT_DIR, target_subject, 'meg_evoked_files', '{}_labels_data_{}.npz'.format(subject, hemi)))
def calc_faces_verts_dic(subject, overwrite=False):
    ply_files = [op.join(SUBJECTS_DIR, subject,'surf', '{}.pial.ply'.format(hemi)) for hemi in HEMIS]
    out_files = [op.join(MMVT_DIR, subject, 'faces_verts_{}.npy'.format(hemi)) for hemi in HEMIS]
    subcortical_plys = glob.glob(op.join(MMVT_DIR, subject, 'subcortical', '*.ply'))
    errors = []
    if len(subcortical_plys) > 0:
        faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, 'subcortical', '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in subcortical_plys]
        ply_files.extend(subcortical_plys)
        out_files.extend(faces_verts_dic_fnames)

    for ply_file, out_file in zip(ply_files, out_files):
        if not overwrite and op.isfile(out_file):
            # print('{} already exist.'.format(out_file))
            continue
        # ply_file = op.join(SUBJECTS_DIR, subject,'surf', '{}.pial.ply'.format(hemi))
        # print('preparing a lookup table for {}'.format(ply_file))
        verts, faces = utils.read_ply_file(ply_file)
        _faces = faces.ravel()
        print('{}: verts: {}, faces: {}, faces ravel: {}'.format(utils.namebase(ply_file), verts.shape[0], faces.shape[0], len(_faces)))
        faces_arg_sort = np.argsort(_faces)
        faces_sort = np.sort(_faces)
        faces_count = Counter(faces_sort)
        max_len = max([v for v in faces_count.values()])
        lookup = np.ones((verts.shape[0], max_len)) * -1
        diff = np.diff(faces_sort)
        n = 0
        for ind, (k, v) in enumerate(zip(faces_sort, faces_arg_sort)):
            lookup[k, n] = v
            n = 0 if ind<len(diff) and diff[ind] > 0 else n+1
        # print('writing {}'.format(out_file))
        np.save(out_file, lookup.astype(np.int))
        print('{} max lookup val: {}'.format(utils.namebase(ply_file), int(np.max(lookup))))
        if len(_faces) != int(np.max(lookup)) + 1:
            errors[utils.namebase(ply_file)] = 'Wrong values in lookup table! ' + \
                'faces ravel: {}, max looup val: {}'.format(len(_faces), int(np.max(lookup)))
    if len(errors) > 0:
        for k, message in errors.items():
            print('{}: {}'.format(k, message))
    return len(errors) == 0
Exemple #37
0
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))
        print('remote dir: {}'.format(remote_subject_dir))
        print('****************************************************************')
        os.environ['SUBJECT'] = subject
        flags = dict()
        if args.necessary_files == '':
            args.necessary_files = dict()
        try:
            if utils.should_run(args, 'prepare_local_subjects_folder'):
                # *) Prepare the local subject's folder
                flags['prepare_local_subjects_folder'] = prepare_local_subjects_folder(
                    subject, remote_subject_dir, args)
                if not flags['prepare_local_subjects_folder'] and not args.ignore_missing:
                    ans = input('Do you which to continue (y/n)? ')
                    if not au.is_true(ans):
                        continue

            flags = main_func(tup, remote_subject_dir, args, flags)
            subjects_flags[subject] = flags
        except:
            subjects_errors[subject] = traceback.format_exc()
            print('Error in subject {}'.format(subject))
            print(traceback.format_exc())

    errors = defaultdict(list)
    for subject, flags in subjects_flags.items():
        print('subject {}:'.format(subject))
        for flag_type, val in flags.items():
            print('{}: {}'.format(flag_type, val))
            if not val:
                errors[subject].append(flag_type)
    if len(errors) > 0:
        print('Errors:')
        for subject, error in errors.items():
            print('{}: {}'.format(subject, error))
Exemple #38
0
def create_evoked_responses(root_fol, task, atlas, events_id, fname_format, fwd_fol, neccesary_files,
            remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method,
            overwrite_epochs=False, overwrite_evoked=False):
    errors = []
    hc_subjects_epo_filess = glob.glob(op.join(root_fol, 'hc*arc*epo.fif'))
    for subject_epo_fname in hc_subjects_epo_filess:
        try:
            subject = utils.namebase(subject_epo_fname).split('_')[0]
            calc_subject_evoked_response(subject, root_fol, task, atlas, events_id, fname_format, fwd_fol, neccesary_files,
                remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method,
                overwrite_epochs, overwrite_evoked)
        except:
            print('******* Error with {} *******'.format(subject))
            print(traceback.format_exc())
            errors.append(subject)

    for subject_err in errors:
        print('Error with {}'.format(subject_err))
Exemple #39
0
def create_electrode_data_file(subject, task, from_t, to_t, stat, conditions, bipolar,
                               electrodes_names_field, moving_average_win_size=0, input_fname='',
                               input_type='ERP', field_cond_template=''):
    if input_fname == '':
        input_fname = 'data.mat'
    input_file = utils.get_file_if_exist(
        [op.join(SUBJECTS_DIR, subject, 'electrodes', input_fname),
         op.join(ELECTRODES_DIR, subject, task, input_fname),
         op.join(SUBJECTS_DIR, subject, 'electrodes', 'evo.mat'),
         op.join(ELECTRODES_DIR, subject, task, 'evo.mat')])
    if not input_file is None:
        input_type = utils.namebase(input_file) if input_type == '' else input_type
    else:
        print('No electrodes data file!!!')
        return
    output_file = op.join(MMVT_DIR, subject, 'electrodes', 'electrodes{}_data_{}.npz'.format(
            '_bipolar' if bipolar else '', STAT_NAME[stat]))
    if field_cond_template == '':
        if input_type.lower() == 'evo':
            field_cond_template = 'AvgERP_{}'
        elif input_type.lower() == 'erp':
            field_cond_template = '{}_ERP'

    #     d = utils.Bag(**sio.loadmat(input_file))
        # pass
    # else:
    if task == 'ECR':
        read_electrodes_data_one_mat(subject, input_file, conditions, stat, output_file, bipolar,
            electrodes_names_field, field_cond_template = field_cond_template, from_t=from_t, to_t=to_t,
            moving_average_win_size=moving_average_win_size)# from_t=0, to_t=2500)
    elif task == 'MSIT':
        if bipolar:
            read_electrodes_data_one_mat(subject, input_file, conditions, stat, output_file, bipolar,
                electrodes_names_field, field_cond_template=field_cond_template, # '{}_bipolar_evoked',
                from_t=from_t, to_t=to_t, moving_average_win_size=moving_average_win_size) #from_t=500, to_t=3000)
        else:
            read_electrodes_data_one_mat(subject, input_file, conditions, stat, output_file, bipolar,
                electrodes_names_field, field_cond_template = '{}_evoked',
                from_t=from_t, to_t=to_t, moving_average_win_size=moving_average_win_size) #from_t=500, to_t=3000)
Exemple #40
0
def sort_pics_key(pic_fname):
    pic_name = utils.namebase(pic_fname)
    if '_t' in pic_name:
        pic_name = pic_name.split('_t')[0]
    return int(re.findall('\d+', pic_name)[0])