Example #1
0
def copy_files(fol, raw_file, eve_fname):
    if not op.isfile(op.join(fol, utils.namebase_with_ext(raw_file))):
        utils.copy_file(raw_file, fol)
    if not op.isfile(op.join(fol, utils.namebase_with_ext(eve_fname))):
        utils.copy_file(eve_fname, fol)
    raw_file = op.join(fol, utils.namebase_with_ext(raw_file))
    eve_fname = op.join(fol, utils.namebase_with_ext(eve_fname))
    return raw_file, eve_fname
Example #2
0
def create_evokeds_links(subject, windows):
    fol = utils.make_dir(op.join(MMVT_DIR, subject, 'evoked'))
    for window_fname in windows:
        new_window_fname = op.join(fol, utils.namebase_with_ext(window_fname))
        if op.isfile(new_window_fname) or op.islink(new_window_fname):
            continue
        utils.make_link(window_fname, new_window_fname)
def export_into_csv(template_system,
                    mmvt_dir,
                    bipolar=False,
                    prefix='',
                    input_fname=''):
    template = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    if input_fname == '':
        input_name = '{}electrodes{}_positions.npz'.format(
            prefix, '_bipolar' if bipolar else '')
        input_fname = op.join(mmvt_dir, template, 'electrodes', input_name)
    electrodes_dict = utils.Bag(np.load(input_fname))
    fol = utils.make_dir(op.join(MMVT_DIR, template, 'electrodes'))
    csv_fname = op.join(
        fol, '{}{}_{}RAS.csv'.format(prefix, template,
                                     'bipolar_' if bipolar else ''))
    print('Writing csv file to {}'.format(csv_fname))
    with open(csv_fname, 'w') as csv_file:
        wr = csv.writer(csv_file, quoting=csv.QUOTE_NONE)
        wr.writerow(['Electrode Name', 'R', 'A', 'S'])
        for elc_name, elc_coords in zip(electrodes_dict.names,
                                        electrodes_dict.pos):
            wr.writerow([
                elc_name, *['{:.2f}'.format(x) for x in elc_coords.squeeze()]
            ])
    fol = utils.make_dir(op.join(MMVT_DIR, template, 'electrodes'))
    csv_fname2 = op.join(fol, utils.namebase_with_ext(csv_fname))
    if csv_fname != csv_fname2:
        utils.copy_file(csv_fname, csv_fname2)
    print('export_into_csv: {}'.format(
        op.isfile(csv_fname) and op.isfile(csv_fname2)))
    return csv_fname
Example #4
0
def create_aparc_aseg_file(subject, atlas, overwrite_aseg_file=False, print_only=False, args={}):
    if not utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas))):
        print('No annot file was found for {}!'.format(atlas))
        print('Run python -m src.preproc.anatomy -s {} -a {} -f create_surfaces,create_annotation'.format(subject, atlas))
        return False

    # aparc_aseg_fname
    ret = fu.create_aparc_aseg_file(
        subject, atlas, SUBJECTS_DIR, overwrite_aseg_file, print_only, mmvt_args=args)
    if isinstance(ret, Iterable):
        ret, aparc_aseg_fname = ret
    if not ret:
        return False

    aparc_aseg_file = utils.namebase_with_ext(aparc_aseg_fname)
    utils.make_dir(op.join(MMVT_DIR, subject, 'freeview'))
    blender_file = op.join(MMVT_DIR, subject, 'freeview', aparc_aseg_file)
    utils.remove_file(blender_file)
    shutil.copyfile(aparc_aseg_fname, blender_file)
    atlas_mat_fname = utils.change_fname_extension(blender_file, 'npy')
    if not op.isfile(atlas_mat_fname) or overwrite_aseg_file:
        d = nib.load(blender_file)
        x = d.get_data()
        np.save(atlas_mat_fname, x)
    return op.isfile(blender_file) and op.isfile(atlas_mat_fname)
Example #5
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)
Example #6
0
def remove_odd_files(root, ft):
    files = glob.glob(op.join(root, '*.{}'.format(ft)))
    first = True
    for fname in files:
        file_num = int(utils.namebase(fname).split('_')[1])
        if file_num % 2 == 1:
            if first:
                backup_fol = utils.make_dir(op.join(root, 'backup'))
                first = False
            shutil.move(fname,
                        op.join(backup_fol, utils.namebase_with_ext(fname)))
Example #7
0
def post_meg_preproc(args):
    inv_method, em = 'MNE', 'mean_flip'
    atlas = 'darpa_atlas'
    bands = dict(theta=[4, 8],
                 alpha=[8, 15],
                 beta=[15, 30],
                 gamma=[30, 55],
                 high_gamma=[65, 200])
    times = (-2, 4)

    subjects = args.subject
    res_fol = utils.make_dir(
        op.join(utils.get_parent_fol(MMVT_DIR), 'msit-ecr'))
    for subject in subjects:
        args.subject = subject
        for task in args.tasks:
            task = task.lower()
            if not utils.both_hemi_files_exist(
                    op.join(
                        MMVT_DIR, subject, 'meg',
                        'labels_data_{}_{}_{}_{}_lh.npz'.format(
                            task, atlas, inv_method, em, '{hemi}'))):
                print('label data can\'t be found for {} {}'.format(
                    subject, task))
                continue
            utils.make_dir(op.join(res_fol, subject))
            meg.calc_labels_func(subject,
                                 task,
                                 atlas,
                                 inv_method,
                                 em,
                                 tmin=0,
                                 tmax=0.5,
                                 times=times,
                                 norm_data=False)
            meg.calc_labels_mean_power_bands(subject,
                                             task,
                                             atlas,
                                             inv_method,
                                             em,
                                             tmin=times[0],
                                             tmax=times[1],
                                             overwrite=True)

        for fname in [
                f for f in glob.glob(
                    op.join(MMVT_DIR, subject, 'labels', 'labels_data', '*'))
                if op.isfile(f)
        ]:
            shutil.copyfile(
                fname, op.join(res_fol, subject,
                               utils.namebase_with_ext(fname)))
Example #8
0
def convert_darpa_ct(args):
    bads, goods = [], []
    if args.print_only:
        args.ignore_missing = True
    args.subject = pu.decode_subjects(args.subject)
    for subject in args.subject:
        local_ct_fol = utils.make_dir(op.join(pu.SUBJECTS_DIR, subject, 'ct'))
        ct_fname = op.join(local_ct_fol, 'ct', 'ct_org.mgz')
        if op.isfile(ct_fname) and not args.overwrite:
            goods.append(subject)
            continue
        darpa_subject = subject[:2].upper() + subject[2:]
        files = glob.glob(op.join(
            f'/homes/5/npeled/space1/Angelique/recon-alls/{darpa_subject}/',
            '**', 'ct.*'),
                          recursive=True)
        if len(files) > 0:
            for fname in files:
                output_fname = op.join(local_ct_fol,
                                       utils.namebase_with_ext(fname))
                print('Coping {} to {}'.format(fname, output_fname))
                utils.copy_file(fname, output_fname)
            goods.append(subject)
            continue
        fols = glob.glob(
            op.join('/space/huygens/1/users/kara', f'{darpa_subject}_CT*'))
        ct_raw_input_fol = fols[0] if len(fols) == 1 else ''
        if not op.isdir(ct_raw_input_fol):
            fols = glob.glob(op.join(
                f'/homes/5/npeled/space1/Angelique/recon-alls/{darpa_subject}/',
                '**', f'{darpa_subject}_CT*'),
                             recursive=True)
            ct_raw_input_fol = fols[0] if len(fols) == 1 else ''
        if not op.isdir(ct_raw_input_fol):
            bads.append(subject)
            continue
        args = ct.read_cmd_args(
            utils.Bag(subject=subject,
                      function='convert_ct_to_mgz',
                      ct_raw_input_fol=ct_raw_input_fol,
                      print_only=args.print_only,
                      ignore_missing=args.ignore_missing,
                      overwrite=args.overwrite,
                      ask_before=args.ask_before))
        ret = pu.run_on_subjects(args, ct.main)
        if ret:
            goods.append(subject)
        else:
            bads.append(subject)
    print('Good subjects:\n {}'.format(goods))
    print('Bad subjects:\n {}'.format(bads))
Example #9
0
def main(html_template_fname, scan_fol, patient_name, task_name, mrn, scan_date, img_prefix=None,
         report_name_suffix='', output_fname=''):
    if utils.get_parent_fol(html_template_fname) != scan_fol:
        shutil.copy(html_template_fname, scan_fol)
    if output_fname == '':
        output_fname = op.join(scan_fol, '{}{}.pdf'.format(mrn, '_{}'.format(report_name_suffix) \
            if report_name_suffix != '' else ''))
    new_html_fname =  utils.change_fname_extension(output_fname, 'html')
    if img_prefix == 'auto':
        img_prefix = utils.find_common_start([utils.namebase(f) for f in glob.glob(op.join(scan_fol, '*.png'))])
        img_prefix = img_prefix[:-1] if img_prefix[-1] == '_' else img_prefix
        img_prefix = img_prefix[:-2] if img_prefix[-2:] == '_l' else img_prefix
    html = read_html(html_template_fname)
    html = replace_fields(html, patient_name, task_name, mrn, scan_date, img_prefix)
    create_new_html(html, new_html_fname)
    create_pdf(new_html_fname, output_fname)
    os.remove(op.join(scan_fol, utils.namebase_with_ext(html_template_fname)))
Example #10
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 #11
0
def example6():
    figures_fol = '/home/npeled/mmvt/nmr01216/figures'
    colors_map = 'RdOrYl'
    data_max, data_min = 2, 6
    background = '#393939'

    files = glob.glob(op.join(figures_fol, '*.png'))
    images_hemi_inv_list = set([
        utils.namebase(fname)[3:] for fname in files
        if utils.namebase(fname)[:2] in ['rh', 'lh']
    ])
    files = [[
        fname for fname in files if utils.namebase(fname)[3:] == img_hemi_inv
    ] for img_hemi_inv in images_hemi_inv_list]
    for files_coup in files:
        hemi = 'rh' if utils.namebase(files_coup[0]).startswith('rh') else 'lh'
        coup_template = files_coup[0].replace(hemi, '{hemi}')
        coup = {}
        for hemi in utils.HEMIS:
            coup[hemi] = coup_template.format(hemi=hemi)
        new_image_fname = op.join(utils.get_fname_folder(files_coup[0]),
                                  utils.namebase_with_ext(files_coup[0])[3:])

        fu.crop_image(coup['lh'], coup['lh'], dx=150, dy=0, dw=150, dh=0)
        fu.crop_image(coup['rh'], coup['rh'], dx=150, dy=0, dw=0, dh=0)
        fu.combine_two_images(coup['lh'],
                              coup['rh'],
                              new_image_fname,
                              facecolor=background)
        fu.combine_brain_with_color_bar(data_max,
                                        data_min,
                                        new_image_fname,
                                        colors_map,
                                        dpi=200,
                                        overwrite=True,
                                        w_fac=1.2,
                                        h_fac=1.2,
                                        ddh=0.7,
                                        dy=0.13,
                                        ddw=0.4,
                                        dx=-0.08)
        for hemi in utils.HEMIS:
            utils.remove_file(coup[hemi])
Example #12
0
def read_meg_layouts(_args, remote_raw_fname=''):
    args = copy.copy(_args)
    bad_subjects = []
    output_fol = utils.make_dir(op.join(MMVT_DIR, 'sensors'))
    subjects = args.subject
    for subject in subjects:
        args.subject = [subject]
        _, _, trans_fname = get_meg_empty_fnames(subject, args.remote_meg_dir,
                                                 args)
        if remote_raw_fname == '':
            remote_raw_fname = op.join(
                args.raw_rest_remote_fol, subject,
                '{}_Resting_meg_ica-raw.fif'.format(subject))
        if not op.isfile(remote_raw_fname):
            print('No Cor fname: {}!!!'.format(remote_raw_fname))
            continue
        meg_args = meg.read_cmd_args(
            utils.Bag(subject=subject,
                      data_per_task=False,
                      remote_subject_dir=args.remote_subject_dir,
                      raw_fname=remote_raw_fname,
                      function='read_sensors_layout',
                      trans_fname=trans_fname,
                      overwrite_sensors=True,
                      read_info_file=False,
                      n_jobs=args.n_jobs))
        meg.call_main(meg_args)
        for sensor_type in ['mag', 'planar1', 'planar2']:
            sensors_fname = op.join(
                MMVT_DIR, subject, 'meg',
                'meg_{}_sensors_positions.npz'.format(sensor_type))
            if not op.isfile(sensors_fname):
                bad_subjects.append(subject)
                break
            utils.make_dir(op.join(output_fol, subject))
            output_fname = op.join(output_fol, subject,
                                   utils.namebase_with_ext(sensors_fname))
            if not op.isfile(output_fname):
                utils.copy_file(sensors_fname, output_fname)

    print('{}/{} subjects with no sensors:'.format(len(bad_subjects),
                                                   len(args.subject)))
    print(bad_subjects)
def export_into_csv(template_system, mmvt_dir, prefix=''):
    template = 'fsaverage5' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    electrodes_dict = utils.Bag(
        np.load(
            op.join(mmvt_dir, template, 'electrodes',
                    '{}electrodes_positions.npz'.format(prefix))))
    fol = utils.make_dir(op.join(MMVT_DIR, template, 'electrodes'))
    csv_fname = op.join(fol, '{}{}_RAS.csv'.format(prefix, template))
    print('Writing csv file to {}'.format(csv_fname))
    with open(csv_fname, 'w') as csv_file:
        wr = csv.writer(csv_file, quoting=csv.QUOTE_NONE)
        wr.writerow(['Electrode Name', 'R', 'A', 'S'])
        for elc_name, elc_coords in zip(electrodes_dict.names,
                                        electrodes_dict.pos):
            wr.writerow([
                elc_name, *['{:.2f}'.format(x) for x in elc_coords.squeeze()]
            ])
    fol = utils.make_dir(op.join(SUBJECTS_DIR, template, 'electrodes'))
    csv_fname2 = op.join(fol, utils.namebase_with_ext(csv_fname))
    shutil.copy(csv_fname, csv_fname2)
    print('export_into_csv: {}'.format(
        op.isfile(csv_fname) and op.isfile(csv_fname2)))
Example #14
0

if __name__ == '__main__':
    subject = 'mg106'
    frames_fol = 'MG106_HGP_sess2_4'
    fname = 'MG106_HGP_sess2_2'
    times_per_frame = 0.1
    cut_in_break = False
    # last_frame_len = 5 * 60
    times_field = 'time2'

    times = load_times(subject, '{}.mat'.format(fname), times_field)
    last_frame_len = np.diff(times)[0]
    frames = load_frames(subject, frames_fol_name=frames_fol)
    break_ind = get_breakind(times) if cut_in_break else len(times)
    print('max time: {} minutes'.format(
        times[break_ind if cut_in_break else len(times) - 1] / 60))
    new_frames = duplicate_frames(frames,
                                  times,
                                  times_per_frame,
                                  last_frame_len,
                                  max_frames_num=break_ind)
    dup_fol = create_dup_frames_links(subject, new_frames,
                                      '{}_dup'.format(frames_fol))
    movie_fname = movu.combine_images(dup_fol,
                                      frames_fol,
                                      frame_rate=int(1 / times_per_frame))
    new_movie_fname = op.join(MMVT_DIR, subject, 'figures',
                              utils.namebase_with_ext(movie_fname))
    utils.remove_file(new_movie_fname)
    shutil.move(movie_fname, new_movie_fname)
def morph_electrodes_volume(electrodes,
                            template_system,
                            subjects_dir,
                            mmvt_dir,
                            overwrite=False,
                            print_only=False):
    subject_to = 'fsaverage5' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    template_electrodes = defaultdict(list)
    header = nib.load(op.join(subjects_dir, subject_to, 'mri',
                              'T1.mgz')).header
    for subject in electrodes.keys():
        if not op.isfile(
                op.join(subjects_dir, subject, 'mri_cvs_register_to_colin27',
                        'final_CVSmorph_tocolin27.m3z')):
            # print(f'The m3z morph matrix does not exist for subject {subject}!')
            continue
        electrodes_fname = op.join(subjects_dir, subject, 'electrodes',
                                   'stim_electrodes.nii.gz')
        if not op.isfile(electrodes_fname):
            # print(f"Can't find volumetric electrodes file for {subject}")
            continue
        for stim_file in glob.glob(
                op.join(subjects_dir, subject, 'electrodes',
                        'stim_????.nii.gz')):
            elcs_file_name = utils.namebase_with_ext(stim_file)
            output_name = utils.namebase(stim_file)
            output_fname = op.join(subjects_dir, subject, 'electrodes',
                                   f'{output_name}_to_colin27.nii.gz')
            if not op.isfile(output_fname) or overwrite:
                rs = utils.partial_run_script(locals(), print_only=print_only)
                rs(mri_elcs2elcs)
        for morphed_fname in glob.glob(
                op.join(subjects_dir, subject, 'electrodes',
                        'stim_????_to_colin27.nii.gz')):
            print(f'Loading {morphed_fname}')
            x = nib.load(morphed_fname).get_data()
            inds = np.array(np.where(x > 0)).T
            vol = inds[np.argmax([x[tuple(ind)] for ind in inds])]
            tkreg = fu.apply_trans(header.get_vox2ras_tkr(), vol)[0]
            elc_name = utils.namebase(morphed_fname).split('_')[1]
            template_electrodes[subject].append(
                (f'{subject}_{elc_name}', tkreg))
            # utils.plot_3d_scatter(inds, names=[x[tuple(ind)] for ind in inds])
            # print(subject, utils.namebase(morphed_fname), len(inds))
        # morphed_output_fname = op.join(subjects_dir, subject, 'electrodes', 'stim_electrodes_to_colin27.nii.gz')
        # if not op.isfile(morphed_output_fname):
        #     elcs_file_name = 'stim_electrodes.nii.gz'
        #     rs = utils.partial_run_script(locals(), print_only=print_only)
        #     rs(mri_elcs2elcs)
        # if not op.isfile(morphed_output_fname):
        #     print('Error in morphing the electrodes volumetric file!')
        #     continue
        # elecs = get_tkreg_from_volume(subject, electrodes_fname)
        # tkreg, pairs, dists = get_electrodes_from_morphed_volume(template_system, morphed_output_fname, len(elecs), subjects_dir, 0)
        # print([dists[p[0],p[1]] for p in pairs])
        # utils.plot_3d_scatter(tkreg, names=range(len(tkreg)), labels_indices=range(len(tkreg)), title=subject)
        # print(f'{subject} has {len(elecs)} electrodes')
        # print(f'{subject} after morphing as {len(tkreg)} electrodes:')
        # print(f'freeview -v {subjects_dir}/{subject}/electrodes/stim_electrodes.nii.gz {subjects_dir}/colin27/mri/T1.mgz')
        # for ind, pair in enumerate(pairs):
        #     pair_name = chr(ord('A') + ind)
        #     for elc_ind in range(2):
        #         template_electrodes[subject].append((f'{subject}_{pair_name}{elc_ind + 1}', tkreg[pair[elc_ind]]))
    utils.save(
        template_electrodes,
        op.join(mmvt_dir, subject_to, 'electrodes', 'template_electrodes.pkl'))
    return template_electrodes
Example #16
0
def meg_preproc_power(args):
    inv_method, em, atlas = 'dSPM', 'mean_flip', 'laus125'  # 'darpa_atlas'
    # bands = dict(theta=[4, 8], alpha=[8, 15], beta=[15, 30], gamma=[30, 55], high_gamma=[65, 200])
    times = (-2, 4)
    subjects_with_error = []
    good_subjects = get_good_subjects(args)
    args.subject = good_subjects
    prepare_files(args)

    for subject in good_subjects:
        args.subject = subject
        empty_fnames, cors, days = get_empty_fnames(subject, args.tasks, args)
        input_fol = utils.make_dir(
            op.join(MEG_DIR, subject, 'labels_induced_power'))
        for task in args.tasks:

            # output_fname = op.join(
            #     MMVT_DIR, subject, 'meg', '{}_{}_{}_power_spectrum.npz'.format(task.lower(), inv_method, em))
            # if op.isfile(output_fname) and args.check_file_modification_time:
            #     file_mod_time = utils.file_modification_time_struct(output_fname)
            #     if file_mod_time.tm_year >= 2018 and (file_mod_time.tm_mon == 9 and file_mod_time.tm_mday >= 21) or \
            #             (file_mod_time.tm_mon > 9):
            #         print('{} already exist!'.format(output_fname))
            #         continue

            input_fnames = glob.glob(
                op.join(
                    input_fol, '{}_*_{}_{}_{}_induced_power.npz'.format(
                        task.lower(), atlas, inv_method, em)))
            if len(input_fnames) == 28:
                print('{} has already all the results for {}'.format(
                    subject, task))
                continue

            remote_epo_fname = op.join(
                args.meg_dir, subject,
                args.epo_template.format(subject=subject, task=task))
            local_epo_fname = op.join(
                MEG_DIR, task, subject,
                args.epo_template.format(subject=subject, task=task))
            if not op.isfile(local_epo_fname) and not op.isfile(
                    remote_epo_fname):
                print('Can\'t find {}!'.format(local_epo_fname))
                continue
            if not op.isfile(local_epo_fname):
                utils.make_link(remote_epo_fname, local_epo_fname)

            meg_args = meg.read_cmd_args(
                dict(
                    subject=args.subject,
                    mri_subject=args.subject,
                    task=task,
                    inverse_method=inv_method,
                    extract_mode=em,
                    atlas=atlas,
                    # meg_dir=args.meg_dir,
                    remote_subject_dir=args.
                    remote_subject_dir,  # Needed for finding COR
                    get_task_defaults=False,
                    fname_format='{}_{}_Onset'.format('{subject}', task),
                    raw_fname=op.join(MEG_DIR, task, subject,
                                      '{}_{}-raw.fif'.format(subject, task)),
                    epo_fname=local_epo_fname,
                    empty_fname=empty_fnames[task]
                    if empty_fnames != '' else '',
                    function=
                    'make_forward_solution,calc_inverse_operator,calc_labels_induced_power',  #,
                    conditions=task.lower(),
                    cor_fname=cors[task].format(
                        subject=subject) if cors != '' else '',
                    average_per_event=False,
                    data_per_task=True,
                    pick_ori=
                    'normal',  # very important for calculation of the power spectrum
                    # fmin=4, fmax=120, bandwidth=2.0,
                    max_epochs_num=args.max_epochs_num,
                    ica_overwrite_raw=False,
                    normalize_data=False,
                    fwd_recreate_source_space=True,
                    t_min=times[0],
                    t_max=times[1],
                    read_events_from_file=False,
                    stim_channels='STI001',
                    use_empty_room_for_noise_cov=True,
                    read_only_from_annot=False,
                    # pick_ori='normal',
                    overwrite_labels_power_spectrum=args.
                    overwrite_labels_power_spectrum,
                    overwrite_evoked=args.overwrite,
                    overwrite_fwd=args.overwrite,
                    overwrite_inv=args.overwrite,
                    overwrite_stc=args.overwrite,
                    overwrite_labels_data=args.overwrite,
                    n_jobs=args.n_jobs))
            ret = meg.call_main(meg_args)
            output_fol = utils.make_dir(
                op.join(MMVT_DIR, subject, 'labels', 'labels_data'))
            join_res_fol = utils.make_dir(
                op.join(utils.get_parent_fol(MMVT_DIR), 'msit-ecr', subject))
            for res_fname in glob.glob(
                    op.join(
                        output_fol, '{}_labels_{}_{}_*_power.npz'.format(
                            task.lower(), inv_method, em))):
                shutil.copyfile(
                    res_fname,
                    op.join(join_res_fol, utils.namebase_with_ext(res_fname)))
            if not ret:
                if args.throw:
                    raise Exception("errors!")
                else:
                    subjects_with_error.append(subject)

    good_subjects = [
        s for s in good_subjects if op.isfile(
            op.join(
                MMVT_DIR, subject, 'meg',
                'labels_data_msit_{}_{}_{}_minmax.npz'.format(
                    atlas, inv_method, em)))
        and op.isfile(
            op.join(
                MMVT_DIR, subject, 'meg', 'labels_data_ecr_{}_{}_{}_minmax.npz'
                .format(atlas, inv_method, em)))
    ]
    print('Good subjects:')
    print(good_subjects)
    print('subjects_with_error:')
    print(subjects_with_error)
Example #17
0
def average_norm_powers(subject,
                        windows_fnames,
                        modality,
                        average_window_name='',
                        inverse_method='dSPM',
                        avg_time_crop=0,
                        figures_type='jpg',
                        high_gamma_max=120,
                        save_fig=True,
                        overwrite=False):
    root_dir = op.join(EEG_DIR if modality == 'eeg' else MEG_DIR, subject)
    output_fname = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-induced_norm_power.npz'.format(
            subject, inverse_method, modality, '{window}'))
    figs_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'epilepsy-figures', 'power-spectrum'))
    figures_template = op.join(
        figs_fol, '{}-epilepsy-{}-{}-{}-induced_{}_norm_power.{}'.format(
            subject, inverse_method, modality, '{window}', '{method}',
            figures_type))
    norm_powers_mins, norm_powers_maxs, min_f_inds = [], [], []
    window_ind = 0
    print('Averaging the power specturm over:')
    for window_fname in windows_fnames:
        window = utils.namebase(window_fname)
        window_output_fname = output_fname.format(window=window)
        if average_window_name not in utils.namebase(window_output_fname):
            continue
        if not op.isfile(window_output_fname):
            print('{} does not exist!'.format(window_output_fname))
            continue
        window_ind += 1
        print('{}) {}'.format(window_ind, window))
        d = np.load(window_output_fname)
        norm_powers_min, norm_powers_max = d['min'], d['max']
        min_f_ind = d['min_f_ind'] if 'min_f_ind' in d else 0
        norm_powers_mins.append(norm_powers_min)
        norm_powers_maxs.append(norm_powers_max)
        min_f_inds.append(min_f_ind)
    if not np.array_equal(np.array(min_f_inds)[1:], np.array(min_f_inds)[:-1]):
        print('Not all min_f are the same!')
    norm_powers_mins = np.array(norm_powers_mins).mean(
        0)[:, avg_time_crop:-avg_time_crop]
    norm_powers_maxs = np.array(norm_powers_maxs).mean(
        0)[:, avg_time_crop:-avg_time_crop]

    times = epi_utils.get_window_times(
        window_fname, downsample=2)[avg_time_crop:-avg_time_crop]
    freqs = epi_utils.get_freqs(min_f_ind + 1, high_gamma_max)
    max_f, max_t = np.unravel_index(
        np.flip(norm_powers_maxs, 0).argmax(), norm_powers_maxs.shape)
    min_f, min_t = np.unravel_index(
        np.flip(norm_powers_mins, 0).argmin(), norm_powers_mins.shape)
    print('norm_powers_maxs: {:.3f} at {:.2f}s and {}Hz'.format(
        np.max(norm_powers_maxs), times[max_t], freqs[max_f - min_f_ind]))
    print('norm_powers_mins: {:.3f} at {:.2f}s and {}Hz'.format(
        np.min(norm_powers_mins), times[min_t], freqs[min_f - min_f_ind]))

    max_vertices = epi_utils.calc_max_vertice(norm_powers_maxs)
    min_vertices = epi_utils.calc_min_vertice(norm_powers_mins)
    avg_output_fname = output_fname.format(
        window='{}-avg'.format(average_window_name))
    print('Saving avg in {}'.format(avg_output_fname))
    np.savez(avg_output_fname.replace('npy', 'npz'),
             min=norm_powers_mins,
             max=norm_powers_maxs,
             min_vertices=min_vertices,
             max_vertices=max_vertices,
             min_f_ind=min_f_ind)

    average_window_name = average_window_name if average_window_name != '' else 'average'
    figure_fname = figures_template.format(
        window=average_window_name, method='pos_and_neg') if save_fig else ''
    fig_files = glob.glob(op.join(figs_fol, '**',
                                  utils.namebase_with_ext(figure_fname)),
                          recursive=True)
    if len(fig_files) == 0 or overwrite:
        plot_positive_and_negative_power_spectrum(
            norm_powers_mins,
            norm_powers_maxs,
            times,
            '{} {}'.format(modality, average_window_name),
            figure_fname=figure_fname,
            high_gamma_max=high_gamma_max,
            min_f=min_f_ind + 1,
            show_only_sig_in_graph=True)
Example #18
0
def plot_norm_powers(subject,
                     windows_fnames,
                     baseline_window,
                     modality,
                     inverse_method='dSPM',
                     figures_type='jpg',
                     high_gamma_max=120,
                     overwrite=False):
    root_dir = op.join(EEG_DIR if modality == 'eeg' else MEG_DIR, subject)
    output_fname = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-induced_norm_power.npz'.format(
            subject, inverse_method, modality, '{window}'))
    baseline_stat_fname = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-induced_norm_power_stat.npz'.format(
            subject, inverse_method, modality,
            utils.namebase(baseline_window)))
    figs_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'epilepsy-figures', 'power-spectrum'))
    figures_template = op.join(
        figs_fol, '{}-epilepsy-{}-{}-{}-induced_{}_norm_power.{}'.format(
            subject, inverse_method, modality, '{window}', '{method}',
            figures_type))
    if not op.isfile(baseline_stat_fname) or overwrite or \
            not utils.file_mod_after_date(baseline_stat_fname, 12, 7, 2019):
        baseline_fol = op.join(
            root_dir, '{}-epilepsy-{}-{}-{}-induced_power'.format(
                subject, inverse_method, modality,
                utils.namebase(baseline_window)))
        baseline = epi_utils.concatenate_powers(
            baseline_fol)  # (vertices x freqs x time)
        if baseline is None:
            print('plot_norm_powers: No baseline!!')
            return
        baseline_std = np.std(
            baseline, axis=2, keepdims=True
        )  # the standard deviation (over time) of log baseline values
        baseline_mean = np.mean(
            baseline, axis=2,
            keepdims=True)  # the mean (over time) of log baseline values
        np.savez(baseline_stat_fname, mean=baseline_mean, std=baseline_std)
    else:
        d = np.load(baseline_stat_fname)
        baseline_mean, baseline_std = d['mean'], d['std']

    for window_fname in windows_fnames:
        window = utils.namebase(window_fname)
        figure_fname = figures_template.format(window=window,
                                               method='pos_and_neg')
        if op.isfile(
                figure_fname) and not overwrite and utils.file_mod_after_date(
                    figure_fname, 12, 7, 2019):
            print('{} already exist'.format(figure_fname))
            continue
        print('Normalizing {}'.format(window))
        window_output_fname = output_fname.format(window=window)
        fol = op.join(
            root_dir, '{}-epilepsy-{}-{}-{}-induced_power'.format(
                subject, inverse_method, modality, window))
        if not op.isfile(window_output_fname) or overwrite or not \
                utils.file_mod_after_date(window_output_fname, 12, 7, 2019):
            powers = epi_utils.concatenate_powers(fol)
            # dividing by the mean of baseline values, taking the log, and  dividing by the standard deviation of
            # log baseline values ('zlogratio')
            powersF, baselineF = powers.shape[1], baseline_mean.shape[1]
            min_f_ind = baselineF - powersF
            norm_powers = (powers - baseline_mean[:, min_f_ind:, :]
                           ) / baseline_std[:, min_f_ind:, :]
            norm_powers_min, norm_powers_max, min_vertices, max_vertices = epi_utils.calc_powers_abs_minmax(
                norm_powers, both_min_and_max=True)
            np.savez(window_output_fname,
                     min=norm_powers_min,
                     max=norm_powers_max,
                     min_vertices=min_vertices,
                     max_vertices=max_vertices,
                     min_f_ind=min_f_ind)
        else:
            d = np.load(window_output_fname)
            norm_powers_min, norm_powers_max = d['min'], d['max']
            min_f_ind = d['min_f_ind'] if 'min_f_ind' in d else 0
            # if calc_also_non_norm_powers:
            #     powers_abs_minmax = np.load(window_not_norm_fname)

        fig_files = glob.glob(op.join(figs_fol, '**',
                                      utils.namebase_with_ext(figure_fname)),
                              recursive=True)
        if len(fig_files) == 0 or overwrite:
            times = epi_utils.get_window_times(window_fname, downsample=2)
            plot_positive_and_negative_power_spectrum(
                norm_powers_min,
                norm_powers_max,
                times,
                '{} {}'.format(modality, window),
                figure_fname=figure_fname,
                high_gamma_max=high_gamma_max,
                min_f=min_f_ind + 1,
                show_only_sig_in_graph=True)
Example #19
0
def post_blender_call(args):
    if not args.add_cb and not args.join_hemis:
        return

    from src.utils import figures_utils as fu
    from src.utils import utils
    from PIL import Image

    if args.call_mmvt_calls:
        su.waits_for_file(args.log_fname)
    with open(args.images_log_fname, 'r') as text_file:
        images_names = text_file.readlines()
    images_names = [l.strip() for l in images_names]
    data_max, data_min = list(map(float, args.cb_vals))
    ticks = list(map(float,
                     args.cb_ticks)) if args.cb_ticks is not None else None
    background = args.background_color  # '#393939'
    if args.join_hemis:
        images_hemi_inv_list = set([
            utils.namebase(fname)[3:] for fname in images_names
            if utils.namebase(fname)[:2] in ['rh', 'lh']
        ])
        files = [[
            fname for fname in images_names
            if utils.namebase(fname)[3:] == img_hemi_inv
        ] for img_hemi_inv in images_hemi_inv_list]
        fol = utils.get_fname_folder(files[0][0])
        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(args.cb_cm))
        # if not op.isfile(cb_fname):
        fu.plot_color_bar(data_max,
                          data_min,
                          args.cb_cm,
                          do_save=True,
                          ticks=ticks,
                          fol=fol,
                          background_color=background,
                          cb_ticks_font_size=args.cb_ticks_font_size)
        cb_img = Image.open(cb_fname)
        for files_coup in files:
            hemi = 'rh' if utils.namebase(
                files_coup[0]).startswith('rh') else 'lh'
            coup_template = files_coup[0].replace(hemi, '{hemi}')
            coup = {
                hemi: coup_template.format(hemi=hemi)
                for hemi in utils.HEMIS
            }
            new_image_fname = op.join(
                fol,
                utils.namebase_with_ext(files_coup[0])[3:])
            if args.add_cb:
                if args.crop_figures:
                    fu.crop_image(coup['lh'],
                                  coup['lh'],
                                  dx=150,
                                  dy=0,
                                  dw=50,
                                  dh=70)
                    fu.crop_image(coup['rh'],
                                  coup['rh'],
                                  dx=150 + 50,
                                  dy=0,
                                  dw=0,
                                  dh=70)
                fu.combine_two_images(coup['lh'],
                                      coup['rh'],
                                      new_image_fname,
                                      facecolor=background,
                                      dpi=200,
                                      w_fac=1,
                                      h_fac=1)
                fu.combine_brain_with_color_bar(new_image_fname,
                                                cb_img,
                                                overwrite=True)
            else:
                if args.crop_figures:
                    fu.crop_image(coup['lh'],
                                  coup['lh'],
                                  dx=150,
                                  dy=0,
                                  dw=150,
                                  dh=0)
                    fu.crop_image(coup['rh'],
                                  coup['rh'],
                                  dx=150,
                                  dy=0,
                                  dw=150,
                                  dh=0)
                fu.combine_two_images(coup['lh'],
                                      coup['rh'],
                                      new_image_fname,
                                      facecolor=background)
            if args.remove_temp_figures:
                for hemi in utils.HEMIS:
                    utils.remove_file(coup[hemi])
    elif args.add_cb and not args.join_hemis:
        fol = utils.get_fname_folder(images_names[0])
        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(args.cb_cm))
        if not op.isfile(cb_fname):
            fu.plot_color_bar(data_max,
                              data_min,
                              args.cb_cm,
                              do_save=True,
                              ticks=ticks,
                              fol=fol,
                              background_color=background)
        cb_img = Image.open(cb_fname)
        for fig_name in images_names:
            fu.combine_brain_with_color_bar(fig_name, cb_img, overwrite=True)
Example #20
0
def analyze_rest_fmri(gargs):
    good_subjects = []
    for subject in gargs.mri_subject:
        # remote_rest_fol = get_fMRI_rest_fol(subject, gargs.remote_fmri_dir)
        # print('{}: {}'.format(subject, remote_rest_fol))
        # if remote_rest_fol == '':
        #     continue
        # local_rest_fname = convert_rest_dicoms_to_mgz(subject, remote_rest_fol, overwrite=True)
        # if local_rest_fname == '':
        #     continue
        # if not op.isfile(local_rest_fname):
        #     print('{}: Can\'t find {}!'.format(subject, local_rest_fname))
        #     continue
        # args = fmri.read_cmd_args(dict(
        #     subject=subject,
        #     atlas=gargs.atlas,
        #     remote_subject_dir=gargs.remote_subject_dir,
        #     function='clean_4d_data',
        #     fmri_file_template=local_rest_fname,
        #     overwrite_4d_preproc=True
        # ))
        # flags = fmri.call_main(args)
        # if subject not in flags or not flags[subject]['clean_4d_data']:
        #     continue

        output_fname = op.join(MMVT_DIR, subject, 'connectivity',
                               'fmri_corr.npz')
        if op.isfile(output_fname):
            print('{} already exist!'.format(output_fname))
            good_subjects.append(subject)
            continue
        remote_fname = op.join(
            gargs.remote_fmri_rest_dir, subject, 'rest_001', '001',
            'fmcpr.siemens.sm6.{}.{}.nii.gz'.format(subject, '{hemi}'))
        if not utils.both_hemi_files_exist(remote_fname):
            print('Couldn\t find fMRI rest data for {} ({})'.format(
                subject, remote_fname))
            continue
        local_fmri_fol = utils.make_dir(op.join(FMRI_DIR, subject))
        local_fmri_template = op.join(local_fmri_fol,
                                      utils.namebase_with_ext(remote_fname))
        if utils.both_hemi_files_exist(local_fmri_template):
            print('{} al')
        for hemi in utils.HEMIS:
            local_fmri_fname = op.join(local_fmri_fol,
                                       local_fmri_template.format(hemi=hemi))
            if op.isfile(local_fmri_fname):
                os.remove(local_fmri_fname)
            utils.make_link(remote_fname.format(hemi=hemi), local_fmri_fname)

        args = fmri.read_cmd_args(
            dict(subject=subject,
                 atlas=gargs.atlas,
                 function='analyze_4d_data',
                 fmri_file_template=local_fmri_template,
                 labels_extract_mode='mean',
                 overwrite_labels_data=True))
        flags = fmri.call_main(args)
        if subject not in flags or not flags[subject]['analyze_4d_data']:
            continue

        args = connectivity.read_cmd_args(
            dict(
                subject=subject,
                atlas=gargs.atlas,
                function='calc_lables_connectivity',
                connectivity_modality='fmri',
                connectivity_method='corr',
                labels_extract_mode='mean',
                windows_length=34,  # tr = 3 -> 100s
                windows_shift=4,  # 12s
                save_mmvt_connectivity=True,
                calc_subs_connectivity=False,
                recalc_connectivity=True,
                n_jobs=gargs.n_jobs))
        flags = connectivity.call_main(args)
        if subject in flags and flags[subject]['calc_lables_connectivity']:
            good_subjects.append(subject)

    print('{}/{} good subjects'.format(len(good_subjects),
                                       len(gargs.mri_subject)))
    print('Good subject: ', good_subjects)
    print('Bad subjects: ', set(gargs.mri_subject) - set(good_subjects))
def calc_per_session(subject, condition, ctf_raw_data, inverse_method, args,
                     all_eog_inds, eog_channel, calc_stc_per_session,
                     only_examine_ica, overwrite_raw, plot_evoked,
                     filter_raw_data, raw_data_filter_freqs):
    session = ctf_raw_data[-4]
    cond = list(condition.keys())[0]
    freqs_str = '-{}-{}'.format(
        raw_data_filter_freqs[0],
        raw_data_filter_freqs[1]) if filter_raw_data else ''
    args.raw_fname = op.join(MEG_DIR, subject,
                             '{}-session{}-raw.fif'.format(cond, session))
    new_raw_no_filter_fname = op.join(
        MEG_DIR, subject, '{}-session{}-ica-raw.fif'.format(cond, session))
    new_raw_fname = op.join(
        MEG_DIR, subject,
        '{}-session{}{}-ica-raw.fif'.format(cond, session, freqs_str))
    args.epo_fname = op.join(
        MEG_DIR, subject,
        '{}-session{}{}-epo.fif'.format(cond, session, freqs_str))
    args.evo_fname = op.join(
        MEG_DIR, subject,
        '{}-session{}{}-ave.fif'.format(cond, session, freqs_str))
    args.inv_fname = op.join(MEG_DIR, subject,
                             '{}-session{}-inv.fif'.format(cond, session))
    args.fwd_fname = op.join(MEG_DIR, subject,
                             '{}-session{}-fwd.fif'.format(cond, session))
    args.noise_cov_fname = op.join(
        MEG_DIR, subject, '{}-session{}-noise-cov.fif'.format(cond, session))
    args.stc_template = op.join(
        MEG_DIR, subject,
        '{cond}-session' + session + '-{method}' + freqs_str + '.stc')
    stc_hemi_template = meg.get_stc_hemi_template(args.stc_template)
    if check_if_all_done(new_raw_fname, cond, inverse_method,
                         calc_stc_per_session, stc_hemi_template,
                         args.labels_data_template, args):
        return
    ica_fname = op.join(MEG_DIR, subject,
                        '{}-session{}-ica.fif'.format(cond, session))
    if len(all_eog_inds) > 0:
        session_ind = np.where(
            all_eog_inds[:, 0] == utils.namebase_with_ext(ica_fname))[0][0]
        eog_inds = [int(all_eog_inds[session_ind, 1])]
    else:
        eog_inds = []
    if only_examine_ica:
        meg.fit_ica(ica_fname=ica_fname,
                    do_plot=True,
                    examine_ica=True,
                    n_jobs=args.n_jobs)
        return
    if not op.isfile(new_raw_fname) or overwrite_raw or not op.isfile(
            ica_fname):
        if not op.isfile(args.raw_fname):
            raw = mne.io.read_raw_ctf(op.join(MEG_DIR, subject, 'raw',
                                              ctf_raw_data),
                                      preload=True)
            raw.save(args.raw_fname)
        if not op.isfile(new_raw_no_filter_fname):
            raw = mne.io.read_raw_fif(args.raw_fname, preload=True)
            raw = meg.remove_artifacts(raw,
                                       remove_from_raw=True,
                                       overwrite_ica=args.overwrite_ica,
                                       save_raw=True,
                                       raw_fname=new_raw_fname,
                                       new_raw_fname=new_raw_no_filter_fname,
                                       ica_fname=ica_fname,
                                       do_plot=args.do_plot_ica,
                                       eog_inds=eog_inds,
                                       eog_channel=eog_channel,
                                       n_jobs=args.n_jobs)
        else:
            raw = mne.io.read_raw_fif(new_raw_no_filter_fname, preload=True)
        meg.calc_noise_cov(None, args.noise_t_min, args.noise_t_max,
                           args.noise_cov_fname, args, raw)
        if filter_raw_data:
            raw.filter(raw_data_filter_freqs[0],
                       raw_data_filter_freqs[1],
                       h_trans_bandwidth='auto',
                       filter_length='auto',
                       phase='zero')
        print('Saving new raw file in {}'.format(new_raw_fname))
        if overwrite_raw or not op.isfile(new_raw_fname):
            raw.save(new_raw_fname, overwrite=True)
    else:
        raw = mne.io.read_raw_fif(new_raw_fname, preload=True)
    evoked, epochs = None, None
    if not op.isfile(args.epo_fname) or not op.isfile(args.evo_fname):
        _, evoked, epochs = meg.calc_evokes_wrapper(subject,
                                                    condition,
                                                    args,
                                                    raw=raw)
    if evoked is not None and plot_evoked:
        fig = evoked[0].plot_joint(times=[-0.5, 0.05, 0.150, 0.250, 0.6])
        plt.show()
    if calc_stc_per_session:
        meg.calc_fwd_inv_wrapper(subject, args, condition)
        # stcs_conds = None
        if not utils.both_hemi_files_exist(
                stc_hemi_template.format(
                    cond=cond, method=inverse_method, hemi='{hemi}')):
            _, stcs_conds, stcs_num = meg.calc_stc_per_condition_wrapper(
                subject, condition, inverse_method, args)