示例#1
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)
示例#2
0
def init_meg(subject):
    utils.make_dir(op.join(MEG_DIR, subject))
    utils.make_link(
        op.join(args.remote_subject_dir.format(subject=subject), 'bem'),
        op.join(MEG_DIR, subject, 'bem'))
    utils.make_link(op.join(MEG_DIR, subject, 'bem'),
                    op.join(SUBJECTS_DIR, subject, 'bem'))
示例#3
0
def prepare_files(args):
    ret = {}
    for subject in args.subject:
        ret[subject] = True
        for task in args.tasks:
            fol = utils.make_dir(op.join(MEG_DIR, task, subject))
            local_epo_fname = op.join(
                fol, '{}_{}_Onset-epo.fif'.format(subject, task))
            if not op.isfile(local_epo_fname):
                remote_epo_fname = op.join(
                    args.meg_dir, subject,
                    '{}_{}_Onset-epo.fif'.format(subject, task))
                print('Creating a local link to {}'.format(remote_epo_fname))
                utils.make_link(remote_epo_fname, local_epo_fname)
            local_raw_fname = op.join(fol,
                                      '{}_{}-raw.fif'.format(subject, task))
            if not op.isfile(local_raw_fname):
                remote_raw_fname = op.join(
                    utils.get_parent_fol(args.meg_dir), 'ica', subject,
                    '{}_{}-raw.fif'.format(subject, task))
                print('Creating a local link to {}'.format(remote_raw_fname))
                utils.make_link(remote_raw_fname, local_raw_fname)
        ret[subject] = ret[subject] and op.isfile(
            local_epo_fname) and op.isfile(local_raw_fname)
    print('Good subjects:')
    print([s for s, r in ret.items() if r])
    print('Bad subjects:')
    print([s for s, r in ret.items() if not r])
示例#4
0
文件: ct.py 项目: ltirrell/mmvt
def save_subject_ct_trans(subject,
                          ct_name='ct_reg_to_mr.mgz',
                          overwrite=False):
    output_fname = op.join(MMVT_DIR, subject, 'ct', 'ct_trans.npz')
    if op.isfile(output_fname) and not overwrite:
        return True
    ct_fname, ct_exist = utils.locating_file(ct_name,
                                             ['*.mgz', '*.nii', '*.nii.gz'],
                                             op.join(MMVT_DIR, subject, 'ct'))
    # ct_fname = op.join(MMVT_DIR, subject, 'ct', ct_name)
    if not ct_exist:  # op.isfile(ct_fname):
        # subjects_ct_fname = op.join(SUBJECTS_DIR, subject, 'mri', ct_name)
        ct_fname, ct_exist = utils.locating_file(
            ct_name, ['*.mgz', '*.nii', '*.nii.gz'],
            op.join(SUBJECTS_DIR, subject, 'mri'))
        if ct_exist:  #op.isfile(subjects_ct_fname):
            utils.make_dir(op.join(MMVT_DIR, subject, 'ct'))
            ct_fname = utils.copy(ct_fname, op.join(MMVT_DIR, subject, 'ct'))
        else:
            print("Can't find subject's CT! ({})".format(ct_fname))
            return False
    if ct_fname != op.join(MMVT_DIR, subject, 'ct', ct_name):
        utils.make_link(ct_fname, op.join(MMVT_DIR, subject, 'ct', ct_name))
    print('save_subject_ct_trans: loading {}'.format(ct_fname))
    header = nib.load(ct_fname).header
    ras_tkr2vox, vox2ras_tkr, vox2ras, ras2vox = anat.get_trans_functions(
        header)
    print('save_subject_ct_trans: Saving {}'.format(output_fname))
    np.savez(output_fname,
             ras_tkr2vox=ras_tkr2vox,
             vox2ras_tkr=vox2ras_tkr,
             vox2ras=vox2ras,
             ras2vox=ras2vox)
    return op.isfile(output_fname)
示例#5
0
def create_dup_frames_links(subject, dup_frames, fol):
    fol = op.join(MMVT_DIR, subject, 'figures', fol)
    utils.delete_folder_files(fol)
    utils.make_dir(fol)
    for ind, frame in enumerate(dup_frames):
        utils.make_link(
            frame, op.join(fol, 'dup_{}.{}'.format(ind,
                                                   utils.file_type(frame))))
    return fol
示例#6
0
def add_reverse_frames_fol(fol, images_prefix, images_type):
    images = sorted(glob.glob(op.join(fol, '*.{}'.format(images_type))), key=utils.natural_keys)
    last_frame = int(utils.find_num_in_str(utils.namebase(images[-1]))[0])
    for org_frame_ind, new_frame_ind in zip(range(last_frame, -1, -1),
                                            range(last_frame + 1, len(images) + last_frame + 1)):
        org_image_fname = op.join(fol, '{}{}.{}'.format(images_prefix, org_frame_ind, images_type))
        new_image_fname = op.join(fol, '{}{}.{}'.format(images_prefix, new_frame_ind, images_type))
        if not utils.is_link(org_image_fname):
            utils.make_link(org_image_fname, new_image_fname)
示例#7
0
def prepare_files(args):
    for subject in args.subject:
        for task in args.tasks:
            fol = utils.make_dir(op.join(MEG_DIR, task, subject))
            local_fname = op.join(fol,
                                  '{}_{}_Onset-epo.fif'.format(subject, task))
            if not op.isfile(local_fname):
                remote_fname = op.join(
                    args.meg_dir, subject,
                    '{}_{}_Onset-epo.fif'.format(subject, task))
                utils.make_link(remote_fname, local_fname)
示例#8
0
文件: ct.py 项目: ltirrell/mmvt
def convert_ct_to_mgz(subject,
                      ct_raw_input_fol,
                      ct_fol='',
                      output_name='ct_org.mgz',
                      overwrite=False,
                      print_only=False,
                      ask_before=False):
    if not op.isdir(ct_fol):
        ct_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'ct'))
    if op.isfile(op.join(ct_fol, 'ct_reg_to_mr.mgz')) and not overwrite:
        return True
    if op.isfile(op.join(ct_fol, output_name)):
        return True
    ct_fname = utils.select_one_file(glob.glob(op.join(ct_fol, '*.mgz')))
    if op.isfile(ct_fname):
        if utils.namebase(ct_fname) != 'ct_org':
            utils.make_link(ct_fname, op.join(ct_fol, 'ct_org.mgz'))
        return True
    output_fname = op.join(ct_fol, output_name)
    if op.isfile(output_fname):
        if not overwrite:
            return True
        else:
            os.remove(output_fname)
    if op.isfile(op.join(SUBJECTS_DIR, subject, 'ct', 'ct.nii.gz')):
        ct_files = [op.join(SUBJECTS_DIR, subject, 'ct', 'ct.nii.gz')]
    elif op.isfile(op.join(SUBJECTS_DIR, subject, 'mri', 'ct.nii.gz')):
        ct_files = [op.join(SUBJECTS_DIR, subject, 'mri', 'ct.nii.gz')]
    else:
        if not op.isdir(ct_raw_input_fol):
            print('{} does not exist!'.format(ct_fol))
            return False
        ct_files = glob.glob(op.join(ct_raw_input_fol, '*.dcm'))
        if len(ct_files) == 0:
            sub_folders = [
                d for d in glob.glob(op.join(ct_raw_input_fol, '*'))
                if op.isdir(d)
            ]
            if len(sub_folders) == 0:
                print('Cannot find CT files in {}!'.format(ct_raw_input_fol))
                return False
            fol = utils.select_one_file(sub_folders, '', 'CT', is_dir=True)
            ct_files = glob.glob(op.join(fol, '*.dcm'))
            if len(ct_files) == 0:
                print('Cannot find CT files in {}!'.format(fol))
                return False
        ct_files.sort(key=op.getmtime)
        if ask_before:
            ret = input('convert {} to {}? '.format(ct_files[0], output_fname))
            if not au.is_true(ret):
                return False
    fu.mri_convert(ct_files[0], output_fname, print_only=print_only)
    return True if print_only else op.isfile(output_fname)
示例#9
0
def get_empty_fnames(subject, tasks, args):
    utils.make_dir(op.join(MEG_DIR, subject))
    utils.make_link(op.join(args.remote_subject_dir.format(subject=subject), 'bem'),
                    op.join(MEG_DIR, subject, 'bem'))
    for task in tasks:
        utils.make_dir(op.join(MEG_DIR, task, subject))
        utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(MEG_DIR, task, subject, 'bem'))
    utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(SUBJECTS_DIR, subject, 'bem'))

    remote_meg_fol = '/autofs/space/lilli_003/users/DARPA-TRANSFER/meg/{}'.format(subject)
    csv_fname = op.join(remote_meg_fol, 'cfg.txt')
    if not op.isfile(csv_fname):
        print('No cfg file!')
        return {task:'' for task in tasks}
    days, empty_fnames = {}, {}
    for line in utils.csv_file_reader(csv_fname, ' '):
        for task in tasks:
            if line[4].lower() == task.lower():
                days[task] = line[2]
    print(days)
    for line in utils.csv_file_reader(csv_fname, ' '):
        if line[4] == 'empty':
            for task in tasks:
                empty_fnames[task] = op.join(MEG_DIR, task, subject, '{}_empty_raw.fif'.format(subject))
                if op.isfile(empty_fnames[task]):
                    continue
                task_day = days[task]
                if line[2] == task_day:
                    empty_fname = op.join(remote_meg_fol, line[0].zfill(3), line[-1])
                    if not op.isfile(empty_fname):
                        raise Exception('empty file does not exist! {}'.format(empty_fname[task]))
                    utils.make_link(empty_fname, empty_fnames[task])
    return empty_fnames
示例#10
0
def get_empty_fnames(subject, tasks, args, overwrite=False):
    utils.make_dir(op.join(MEG_DIR, subject))
    utils.make_link(op.join(args.remote_subject_dir.format(subject=subject),
                            'bem'),
                    op.join(MEG_DIR, subject, 'bem'),
                    overwrite=overwrite)
    for task in tasks:
        utils.make_dir(op.join(MEG_DIR, task, subject))
        utils.make_link(op.join(MEG_DIR, subject, 'bem'),
                        op.join(MEG_DIR, task, subject, 'bem'),
                        overwrite=overwrite)
    utils.make_link(op.join(MEG_DIR, subject, 'bem'),
                    op.join(SUBJECTS_DIR, subject, 'bem'),
                    overwrite=overwrite)

    remote_meg_fol = op.join(args.remote_meg_dir, subject)
    csv_fname = op.join(remote_meg_fol, 'cfg.txt')
    empty_fnames, cors, days = '', '', ''

    if not op.isfile(csv_fname):
        print('No cfg file!')
        return '', '', ''
    days, empty_fnames, cors = {}, {}, {}
    for line in utils.csv_file_reader(csv_fname, ' '):
        for task in tasks:
            if line[4].lower() == task.lower():
                days[task] = line[2]
    # print(days)
    for line in utils.csv_file_reader(csv_fname, ' '):
        if line[4] == 'empty':
            for task in tasks:
                empty_fnames[task] = op.join(
                    MEG_DIR, task, subject, '{}_empty_raw.fif'.format(subject))
                if op.isfile(empty_fnames[task]):
                    continue
                task_day = days[task]
                if line[2] == task_day:
                    empty_fname = op.join(remote_meg_fol, line[0].zfill(3),
                                          line[-1])
                    if not op.isfile(empty_fname):
                        raise Exception('empty file does not exist! {}'.format(
                            empty_fname[task]))
                    utils.make_link(empty_fname, empty_fnames[task])
    cor_dir = op.join(args.remote_subject_dir.format(subject=subject), 'mri',
                      'T1-neuromag', 'sets')
    for task in tasks:
        if op.isfile(
                op.join(cor_dir, 'COR-{}-{}.fif'.format(subject,
                                                        task.lower()))):
            cors[task] = op.join(
                cor_dir, 'COR-{}-{}.fif'.format('{subject}', task.lower()))
        elif op.isfile(
                op.join(cor_dir,
                        'COR-{}-day{}.fif'.format(subject, days[task]))):
            cors[task] = op.join(
                cor_dir, 'COR-{}-day{}.fif'.format('{subject}', days[task]))
    return empty_fnames, cors, days
示例#11
0
文件: cics.py 项目: bdthombre/mmvt
def project_cbf_on_cortex(subject,
                          site,
                          scan_rescan,
                          overwrite=False,
                          print_only=False):
    mmvt_subject = subject if scan_rescan == SCAN else '{}_rescan'.format(
        subject)
    fmri_fol = utils.make_dir(op.join(FMRI_DIR, mmvt_subject))
    mmvt_cbf_fname = op.join(fmri_fol, 'CBF_{}.nii'.format(scan_rescan))
    cics_cbf_fname = op.join(HOME_FOL, site, subject, scan_rescan, 'CBF.nii')
    reg_file = op.join(HOME_FOL, site, subject, scan_rescan,
                       'control_to_T1.lta')
    if not op.islink(mmvt_cbf_fname) and op.isfile(cics_cbf_fname):
        utils.make_link(cics_cbf_fname, mmvt_cbf_fname)
    if not op.islink(mmvt_cbf_fname):
        print('Cannot file the link to CBF! ({}-{})'.format(
            cics_cbf_fname, mmvt_cbf_fname))
        return False
    if not op.isfile(reg_file):
        print('Cannot find the registration file! ({})'.format(reg_file))
        return False
    verts = utils.load_surf(subject, MMVT_DIR, SUBJECTS_DIR)
    for hemi in utils.HEMIS:
        surf_output_fname = op.join(FMRI_DIR, subject,
                                    'CBF_{}_{}.mgz'.format(scan_rescan, hemi))
        npy_output_fname = op.join(
            MMVT_DIR, subject, 'fmri',
            'fmri_CBF_{}_{}.npy'.format(scan_rescan, hemi))
        if op.isfile(npy_output_fname) and not overwrite:
            continue
        fu.project_volume_data(cics_cbf_fname,
                               hemi,
                               reg_file=reg_file,
                               smooth_fwhm=0,
                               output_fname=surf_output_fname,
                               print_only=print_only)
        if print_only:
            continue
        surf_data = np.squeeze(nib.load(surf_output_fname).get_data())
        np.save(npy_output_fname, surf_data)
        if len(verts[hemi]) != len(surf_data):
            print(
                '*** Wrong number of vertices in {} data! surf vertices ({}) != data vertices ({})'
                .format(hemi, len(verts[hemi]), len(surf_data)))
示例#12
0
def _plot_evokes_parallel(p):
    subject, modality, window_fname, bad_channels, figs_fol, overwrite = p
    module = eeg if modality == 'eeg' else meg
    window = utils.namebase(window_fname)
    evo_fname = op.join(MMVT_DIR, subject, 'evoked', '{}.fif'.format(window))
    if not op.isfile(evo_fname):
        utils.make_link(window_fname, evo_fname)
    fig_fname = op.join(figs_fol, '{}.jpg'.format(window))
    if op.isfile(fig_fname) and not overwrite:
        return
    if bad_channels != 'bads':
        bad_channels = bad_channels.split(',')
    module.plot_evoked(subject,
                       evo_fname,
                       window_title=window,
                       exclude=bad_channels,
                       save_fig=True,
                       fig_fname=fig_fname,
                       overwrite=overwrite)
示例#13
0
def prepare_files(args):
    # todo: should look in the dict for files locations
    ret = {}
    for subject in args.subject:
        ret[subject] = True
        for task in args.tasks:
            fol = utils.make_dir(op.join(MEG_DIR, task, subject))
            local_epo_fname = op.join(
                fol, '{}_{}_Onset-epo.fif'.format(subject, task))
            local_raw_fname = op.join(fol,
                                      '{}_{}-raw.fif'.format(subject, task))
            if not args.overwrite and (op.islink(local_epo_fname) or op.isfile(local_epo_fname)) and \
                    (op.islink(local_raw_fname) or op.isfile(local_raw_fname)):
                continue

            if op.islink(local_epo_fname) or op.isfile(local_epo_fname):
                os.remove(local_epo_fname)
            remote_epo_fname = op.join(
                args.meg_dir, subject,
                '{}_{}_meg_Onset-epo.fif'.format(subject, task))
            if not op.isfile(remote_epo_fname):
                print('{} does not exist!'.format(remote_epo_fname))
                ret[subject] = False
                continue
            print('Creating a local link to {}'.format(remote_epo_fname))
            utils.make_link(remote_epo_fname, local_epo_fname)
            if op.islink(local_raw_fname) or op.isfile(local_raw_fname):
                os.remove(local_raw_fname)
            remote_raw_fname = op.join(
                utils.get_parent_fol(args.meg_dir), 'raw_preprocessed',
                subject, '{}_{}_meg_ica-raw.fif'.format(subject, task))
            if not op.isfile(remote_epo_fname):
                print('{} does not exist!'.format(remote_raw_fname))
                ret[subject] = False
                continue
            print('Creating a local link to {}'.format(remote_raw_fname))
            utils.make_link(remote_raw_fname, local_raw_fname)
        ret[subject] = ret[subject] and (op.isfile(local_epo_fname) or op.islink(local_epo_fname)) and \
                       (op.isfile(local_raw_fname) or op.islink(local_raw_fname))
    print('Good subjects:')
    print([s for s, r in ret.items() if r])
    print('Bad subjects:')
    print([s for s, r in ret.items() if not r])
示例#14
0
def get_meg_empty_fnames(subject, remote_fol, args):
    csv_fname = op.join(remote_fol, 'cfg.txt')
    if not op.isfile(csv_fname):
        print('No cfg file!')
        return '', '', ''
    day, empty_fname, cor_fname, local_rest_raw_fname = '', '', '', ''
    for line in utils.csv_file_reader(csv_fname, ' '):
        if line[4].lower() == 'resting':
            day = line[2]
            remote_rest_raw_fname = op.join(remote_fol, line[0].zfill(3),
                                            line[-1])
            if not op.isfile(remote_rest_raw_fname):
                raise Exception('rest file does not exist! {}'.format(
                    remote_rest_raw_fname))
            local_rest_raw_fname = op.join(
                MEG_DIR, subject, '{}_resting_raw.fif'.format(subject))
            if not op.isfile(local_rest_raw_fname):
                utils.make_link(remote_rest_raw_fname, local_rest_raw_fname)
            break
    if day == '':
        print('Couldn\'t find the resting day in the cfg!')
        return '', '', ''
    for line in utils.csv_file_reader(csv_fname, ' '):
        if line[4] == 'empty':
            empty_fname = op.join(MEG_DIR, subject,
                                  '{}_empty_raw.fif'.format(subject))
            if op.isfile(empty_fname):
                continue
            if line[2] == day:
                remote_empty_fname = op.join(remote_fol, line[0].zfill(3),
                                             line[-1])
                if not op.isfile(remote_empty_fname):
                    raise Exception('empty file does not exist! {}'.format(
                        remote_empty_fname))
                utils.make_link(remote_empty_fname, empty_fname)
    cor_dir = op.join(args.remote_subject_dir.format(subject=subject), 'mri',
                      'T1-neuromag', 'sets')
    if op.isfile(op.join(cor_dir, 'COR-{}-resting.fif'.format(subject))):
        cor_fname = op.join(cor_dir, 'COR-{}-resting.fif'.format(subject))
    elif op.isfile(op.join(cor_dir, 'COR-{}-day{}.fif'.format(subject, day))):
        cor_fname = op.join(cor_dir, 'COR-{}-day{}.fif'.format(subject, day))
    return local_rest_raw_fname, empty_fname, cor_fname
示例#15
0
文件: plots.py 项目: keshava/mmvt
def _plot_topomaps_parallel(p):
    subject, modality, window_fname, bad_channels, figs_fol = p
    module = eeg if modality == 'eeg' else meg
    window = utils.namebase(window_fname)
    evo_fname = op.join(MMVT_DIR, subject, 'evoked', '{}.fif'.format(window))
    if not op.isfile(evo_fname):
        utils.make_link(window_fname, evo_fname)
    fig_fname = op.join(figs_fol, '{}.jpg'.format(window))
    if op.isfile(fig_fname):
        return
    # if bad_channels != 'bads':
    #     bad_channels = bad_channels.split(',')
    module.plot_topomap(subject,
                        evo_fname,
                        times=[0],
                        find_peaks=True,
                        same_peaks=False,
                        n_peaks=5,
                        bad_channels=bad_channels,
                        title=window,
                        save_fig=True,
                        fig_fname=fig_fname)
示例#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)
示例#17
0
def calc_meg_connectivity(args):
    inv_method, em = 'dSPM', 'mean_flip'
    prepare_files(args)
    good_subjects = get_good_subjects(args)
    args.subject = good_subjects

    for subject in good_subjects:
        args.subject = subject
        for task in args.tasks:

            output_fname = op.join(
                MMVT_DIR, subject, 'connectivity',
                '{}_{}_coh_cwt_morlet.npz'.format(task.lower(), em))
            if op.isfile(output_fname):
                file_mod_time = utils.file_modification_time_struct(
                    output_fname)
                if file_mod_time.tm_year >= 2018 and (file_mod_time.tm_mon == 10 and file_mod_time.tm_mday >= 10) or \
                        (file_mod_time.tm_mon > 10):
                    print('{} already exist!'.format(output_fname))
                    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):
                utils.make_link(remote_epo_fname, local_epo_fname)

            con_args = meg.read_cmd_args(
                utils.Bag(
                    subject=args.subject,
                    mri_subject=args.subject,
                    task=task,
                    inverse_method=inv_method,
                    extract_mode=em,
                    atlas=args.atlas,
                    # meg_dir=args.meg_dir,
                    remote_subject_dir=args.
                    remote_subject_dir,  # Needed for finding COR
                    get_task_defaults=False,
                    data_per_task=True,
                    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],
                    function='calc_labels_connectivity',
                    conditions=task.lower(),
                    overwrite_connectivity=True,  #args.overwrite_connectivity,
                    # cor_fname=cors[task].format(subject=subject),
                    # ica_overwrite_raw=False,
                    # normalize_data=False,
                    # 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_evoked=args.overwrite,
                    # overwrite_inv=args.overwrite,
                    # overwrite_stc=args.overwrite,
                    # overwrite_labels_data=args.overwrite,
                    n_jobs=args.n_jobs))
            meg.call_main(con_args)
示例#18
0
def arrange_data(subjects):
    files_list = []
    for sub in subjects:
        utils.make_dir(op.join(fmri_root_data, sub))
        sub_root_anat_fol = utils.make_dir(op.join(fmri_root_data, sub,
                                                   'anat'))
        sub_root_bold_fol = utils.make_dir(op.join(fmri_root_data, sub,
                                                   'bold'))
        anat_fols = [
            f for f in glob.glob(op.join(subject_fmri_fold, sub, 'anat', '*'))
            if op.isdir(f)
        ]
        if len(anat_fols) == 0:
            print('No anat folder for {}!'.format(sub))
            continue
        elif len(anat_fols) > 1:
            print('More than one anat folder for {}!'.format(sub))
            continue
        anat_fol = anat_fols[0]
        anat_number = int(utils.namebase(anat_fol))
        anat_files = glob.glob(op.join(anat_fol, '*nii.gz'))
        if len(anat_files) == 1:
            sub_anat_fol = utils.make_dir(
                op.join(sub_root_anat_fol, utils.namebase(anat_fol)))
            utils.make_link(
                anat_files[0],
                op.join(sub_anat_fol,
                        '{}.nii.gz'.format(utils.namebase(anat_files[0]))))
        elif len(anat_files) == 0:
            print('No nii.gz files were found in {}!'.format(anat_fol))
            continue
        else:
            print('Too many nii.gz files were found in {}!'.format(anat_fol))
            continue
        bold_fols = [
            f for f in glob.glob(op.join(subject_fmri_fold, sub, 'bold', '*'))
            if op.isdir(f)
        ]
        bolds_numbers = ','.join(
            [str(int(utils.namebase(bold_fol))) for bold_fol in bold_fols])
        for bold_fol in bold_fols:
            fmri_files = glob.glob(
                op.join(bold_fol, '*.nii')
            )  # '{}_bld{}_rest.nii'.format(sub, utils.namebase(bold_fol))))
            if len(fmri_files) == 1:
                sub_fmri_fol = utils.make_dir(
                    op.join(sub_root_bold_fol, utils.namebase(bold_fol)))
                # utils.delete_folder_files(sub_fmri_fol)
                # target_file = op.join(sub_fmri_fol, '{}_bld{}_rest_reorient.nii.gz'.format(sub, utils.namebase(bold_fol)))
                target_file = op.join(
                    sub_fmri_fol,
                    '{}_bld{}_rest.nii'.format(sub, utils.namebase(bold_fol)))
                utils.make_link(fmri_files[0], target_file)
                new_target_file = op.join(
                    sub_fmri_fol, '{}_bld{}_rest_reorient.nii.gz'.format(
                        sub, utils.namebase(bold_fol)))
                if not op.isfile(new_target_file):
                    output = utils.run_script('mri_convert {} {}'.format(
                        target_file, new_target_file))
                # utils.remove_link(target_file)
                # if not op.isfile(target_file):
                #     utils.copy_file(fmri_files[0], target_file)
            elif len(fmri_files) == 0:
                print(
                    'No *_rest_reorient_skip_faln_mc.nii.gz files were found in {}!'
                    .format(bold_fol))
                continue
            else:
                print(
                    'Too many *_rest_reorient_skip_faln_mc.nii.gz files were found in {}!'
                    .format(bold_fol))
                continue
        tr = int(fu.get_tr(target_file) * 1000) / 1000.0
        files_list.append('{} {} {} {}'.format(
            sub, bolds_numbers,
            str(anat_number).zfill(3), tr))  #int(tr))) # int(trs[sub])))
        utils.make_dir(op.join(fmri_root_data, sub, 'scripts'))
        params_fname = op.join(fmri_root_data, sub, 'scripts',
                               '{}.params'.format(sub))
        with open(params_fname, 'w') as f:
            f.write(
                params_file_text.format(subject=sub,
                                        bolds=bolds_numbers.replace(',', ' '),
                                        tr=tr))

    return files_list
示例#19
0
def calc_meg_source_psd(args):
    from src.misc import meg_buddy
    bad_subjects = ['hc004', 'hc012', 'hc029']
    subjects = args.subject
    for subject in subjects:
        if subject in bad_subjects:
            continue
        fol = op.join(MMVT_DIR, subject, 'meg')
        file_name = '{cond}_dSPM_mean_flip_vertices_power_spectrum.pkl'
        if all([op.isfile(op.join(fol, file_name.format(cond=cond.lower())))
                for cond in MSIT_CONDS]) and not args.overwrite:
            print('{}: already has MSIT power stcs'.format(subject))
            continue
        args.subject = subject
        local_raw_fname = op.join(MEG_DIR, args.task, subject, args.raw_template.format(
            subject=subject, task=args.task))
        remote_raw_fname = op.join(args.remote_root_dir, 'raw_preprocessed', subject, args.raw_template.format(
            subject=subject, task=args.task))
        if not op.isfile(remote_raw_fname) and not args.ignore:
            print('Can\'t find remote raw file! {}'.format(remote_raw_fname))
            continue
        if op.isfile(local_raw_fname):
            os.remove(local_raw_fname)
        utils.make_link(remote_raw_fname, local_raw_fname)
        if not op.islink(local_raw_fname) and not args.ignore:
            print('Can\'t create a link to the remote raw!')
            continue
        inv_fname = op.join(MEG_DIR, args.task, subject, args.inv_template.format(subject=subject, task=args.task))
        if not op.isfile(inv_fname):
            inv_fnames = glob.glob(op.join(MEG_DIR, args.task, subject, '*inv.fif'))
            if len(inv_fnames) > 0:
                inv_fname = utils.select_one_file(inv_fnames)
                print('New inv fname: {}'.format(inv_fname))

        _args = meg.read_cmd_args(dict(
            subject=subject, mri_subject=subject,
            function='make_forward_solution,calc_inverse_operator',
            task='MSIT', data_per_task=True,
            fmin=1, fmax=120,
            raw_fname=local_raw_fname, inv_fname=inv_fname,
            remote_subject_dir=args.remote_subject_dir,
            n_jobs=args.n_jobs
        ))
        ret = meg.call_main(_args)
        if not ret[subject].get('calc_inverse_operator', True):
            continue

        # Load the eopchs and calc source power spectrum for both conditions.
        # The epochs are being splitted first
        data = meg_buddy.get_data(subject, tasks=['MSIT'], modalities=['MEG'])['MSIT']['MEG']
        if subject not in data:
            print('{} not in msit_data!'.format(subject))
            continue
        data = data[subject]
        subject_epochs = data._load_epochs('Onset', ar=True)
        indices = data._get_indices(subject_epochs, 'Condition', MSIT_CONDS)
        for cond in MSIT_CONDS:
            epochs = subject_epochs[indices[cond]]
            meg.calc_source_power_spectrum(
                subject, cond.lower(), epochs=epochs, max_epochs_num=50, inv_fname=inv_fname,
                overwrite=args.overwrite, n_jobs=args.n_jobs)
示例#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))