コード例 #1
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)
コード例 #2
0
ファイル: init_files.py プロジェクト: hooray1164/mmvt
def find_room_noise(fol):
    files = glob.glob(op.join(fol, '*roomnoise_raw.fif'))
    if len(files) >= 1:
        return utils.select_one_file(files, 'room noise')
    files = glob.glob(op.join(fol, '*noiseroom_raw.fif'))
    if len(files) >= 1:
        return utils.select_one_file(files, 'room noise')
    file_name = input(
        'Can\'t find the room noise file, please input its name: ')
    fname = op.join(fol, file_name)
    if not op.isfile(fname):
        print('*** No room noise! ***')
        return ''
    else:
        return fname
コード例 #3
0
def meg_clin(args):
    # python -m src.preproc.examples.meg -s nmr01426 -f meg_clin
    from src.examples.epilepsy import pipeline
    from src.preproc import eeg
    fs_root = '/space/megraid/clinical/MEG-MRI/seder/freesurfer'
    meg_root = '/autofs/space/frieda_003/users/valia/epilepsy_clin'
    overwrite = True
    run_num = 1
    modality = 'meg'
    for subject in args.subject:
        subject_meg_clin_fol = '6966926_1426'
        subject_remote_dir = op.join(fs_root, subject)
        raw_fname = op.join(meg_root, subject_meg_clin_fol, '200618/6966926_01_raw_ssst.fif')
        bad_channels = ['MEG{}'.format(c.strip()) for c in
            '1213, 1223, 0941, 1411, 1231, 0142, 0733, 0721'.split(',')]
        bad_channels.extend(['EEG{}'.format(c.strip()) for c in
            '029, 040, 050, 049, 041, 051, 065'.split(',')])
        empty_fname = utils.select_one_file(glob.glob(op.join(meg_root, subject_meg_clin_fol, '**', '*empty*.fif')))
        if not op.isfile(empty_fname):
            raise Exception('No empty room!')
        pipeline.calc_fwd_inv(subject, modality, run_num, raw_fname, empty_fname, bad_channels,
                              overwrite_inv=overwrite, overwrite_fwd=overwrite, n_jobs=args.n_jobs)
        pipeline.check_inv_fwd(subject, modality, run_num)
        # -s nmr01426 -f read_sensors_layout --raw_template "/autofs/space/frieda_003/users/valia/epilepsy_clin/6966926_1426/200618/*raw*.fif"
        args = eeg.read_cmd_args(dict(
            subject=subject,
            mri_subject=subject,
            function='read_sensors_layout',
            raw_fname=raw_fname,
        ))
        eeg.call_main(args)

        clin_fif_fname =
        pipeline.calc_amplitude(subject, modality, windows_fnames, inverse_method='dSPM', downsample_r=1, overwrite=False,
                   rename=True, n_jobs=4)
コード例 #4
0
ファイル: freeview.py プロジェクト: waffle-iron/mmvt
def read_electrodes_pos(subject, args):
    # electrodes_file = args.electrodes_pos_fname if args.electrodes_pos_fname != '' else op.join(
    #     SUBJECTS_DIR, subject, 'electrodes', 'electrodes{}_positions.npz'.format('_bipolar' if args.bipolar else ''))
    if args.electrodes_pos_fname != '':
        electrodes_file = args.electrodes_pos_fname
    else:
        # electrodes_file = op.join(SUBJECTS_DIR, subject, 'electrodes', 'electrodes{}_{}positions.npz'.format(
        #     '_bipolar' if args.bipolar else '', 'snap_' if args.snap else ''))
        electrodes_files = glob.glob(
            op.join(MMVT_DIR, subject, 'electrodes',
                    'electrodes*_*positions.npz'))
        if len(electrodes_files) == 0:
            electrodes_files = glob.glob(
                op.join(SUBJECTS_DIR, subject, 'electrodes',
                        'electrodes*_*positions.npz'))
        if len(electrodes_files) == 0:
            print('No electrodes pos files were found!')
            return [], []
        electrodes_file = utils.select_one_file(electrodes_files,
                                                'electrodes*_*positions.npz',
                                                'electrodes')

    if op.isfile(electrodes_file):
        elecs = np.load(electrodes_file)
        elecs_pos, elecs_names = elecs['pos'], [
            name.astype(str) for name in elecs['names']
        ]
        return elecs_pos, elecs_names
    else:
        # raise Exception("Can't find the electrode coordinates file! ({})".format(electrodes_file))
        print("Can't find the electrode coordinates file! ({})".format(
            electrodes_file))
        return [], []
コード例 #5
0
ファイル: init_files.py プロジェクト: hooray1164/mmvt
def find_raw_fname(meg_fol, run):
    raw_fname = ''
    run_num = re.sub('\D', ',', run).split(',')[-1]
    raw_files = glob.glob(
        op.join(meg_fol, '*_{}_*raw*.fif'.format(str(run_num).zfill(2))))
    if not op.isfile(raw_fname):
        raw_run_files = [
            f for f in raw_files if 'annot' not in utils.namebase(f)
            and 'eve' not in utils.namebase(f)
        ]
        if len(raw_run_files) == 0:
            return '', run_num
        ssst_raw_files = [
            f for f in raw_run_files if 'ssst' in utils.namebase(f)
        ]
        if len(ssst_raw_files) > 0:
            raw_fname = utils.select_one_file(
                ssst_raw_files, 'raw file for run {}'.format(run_num))
        else:
            raw_fname = utils.select_one_file(
                raw_run_files, 'raw file for run {}'.format(run_num))
    return raw_fname, run_num
コード例 #6
0
def import_subs(movie_fol, subs_name='subs', delim=' '):
    if op.isfile(op.join(movie_fol, subs_name)):
        subs_fname = op.join(movie_fol, subs_name)
    else:
        subs_fnames = glob.glob(op.join(movie_fol, '{}.*'.format(subs_name)))
        subs_fname = utils.select_one_file(subs_fnames)
        if subs_fname is None:
            return
    subs = []
    for line in utils.csv_file_reader(subs_fname, delim):
        from_t, to_t = [utils.time_to_seconds(t, '%M:%S') for t in line[0].split('-')]
        subs.append(((from_t, to_t), ' '.join(line[1:])))
    return subs
コード例 #7
0
def language(args):
    # -f language -s nmr01361 --clinical_dir clin_4090354
    # -s nmr01353 -f clean_4d_data --fsd sycabs --remote_fmri_dir "/space/megraid/clinical/MEG-MRI/seder/freesurfer" --nconditions 4
    if args.clinical_dir == '':
        print('You should set the clinical_dir first. Example: clin_4090354')
        return
    clinical_root_dir = op.join(args.remote_fmri_dir, args.clinical_dir)
    if not op.isdir(clinical_root_dir):
        print('{} does not exist!'.format(clinical_root_dir))

    task = 'sycabs'
    fwhm = 6
    subject = args.subject[0]
    remote_mri_dir = args.remote_clinical_subjects_dir
    subject_mri_dir = op.join(remote_mri_dir, subject)
    mri_subject_task_dir = utils.make_dir(op.join(subject_mri_dir, task))
    clinical_dirs = glob.glob(op.join(clinical_root_dir, '*'))
    clinical_dirs = [
        d for d in clinical_dirs if utils.namebase(d) != 'mne_dcm'
    ]
    remote_fmri_dir = utils.select_one_file(clinical_dirs)
    fmri_fols = sorted(glob.glob(op.join(remote_fmri_dir, '*_SyCAbs')))
    par_fol = utils.make_dir(op.join(remote_mri_dir, subject, 'par'))
    par_files = glob.glob(op.join(par_fol, '*.par'))
    sessions = sorted(
        [utils.find_num_in_str(utils.namebase(d))[0] for d in fmri_fols])

    # Warning: You first need to put the original ones in the following folder:
    if len(par_files) == 0:
        print('\n *** Please put the original par files in {} and rerun ***'.
              format(op.join(remote_mri_dir, subject, 'par')))
        return

    par_files.sort(key=lambda x: int(utils.namebase(x).split('_')[-1]))
    ret = input('''
        Patient: {}
        MRI folder: {}
        fMRI root folder: {}
        fMRI sessions: {}
        Session and pars: {}
        Do you want to continue (y/n)? '''.format(
        subject, subject_mri_dir, remote_fmri_dir,
        [utils.namebase(d) for d in fmri_fols],
        list(zip([utils.namebase(f) for f in par_files], sessions))))
    if not au.is_true(ret):
        return

    # You need first to run src.preproc.anatomy
    if not op.isfile(anat.get_blend_fname(subject, args.atlas)):
        args = anat.read_cmd_args(
            dict(
                subject=subject,
                remote_subject_dir=subject_mri_dir,
                ignore_missing=True,
            ))
        pu.run_on_subjects(args, anat.main)

    # convert the fMRI dicom files to nii
    for fmri_fol in fmri_fols:
        ses_num = utils.find_num_in_str(utils.namebase(fmri_fol))[0]
        ses_files = glob.glob(op.join(fmri_fol, '**', '*.*'), recursive=True)
        output_fname = op.join(
            utils.make_dir(op.join(mri_subject_task_dir, ses_num)), 'f.nii.gz')
        if not op.isfile(output_fname):
            fu.mri_convert(ses_files[0], output_fname)

    # Convert and arrange the par file
    from src.misc.fmri_scripts import convert_par
    for par_file, session in zip(par_files, sessions):
        fs_par_fname = op.join(mri_subject_task_dir, session,
                               '{}.par'.format(task))
        # if not op.isfile(fs_par_fname):
        warnings = convert_par.sycabs(par_file, fs_par_fname)
        if warnings != '':
            print(
                '\n *** Please fix the problems with the par convertion ({}) and rerun ***\n'
                .format(par_file))
            return

    for hemi in utils.HEMIS:
        utils.delete_folder_files(
            op.join(remote_mri_dir, '{}_sm{}_{}'.format(task, fwhm, hemi)))

    # Run the FreeSurfer analysis
    args = fmri.read_cmd_args(
        dict(subject=subject,
             atlas=args.atlas,
             function='clean_4d_data',
             fsd=task,
             fwhm=fwhm,
             remote_fmri_dir=remote_mri_dir,
             nconditions=4,
             ignore_missing=True,
             print_only=False,
             overwrite_4d_preproc=False))
    pu.run_on_subjects(args, fmri.main)

    # Load the fMRI results
    args = fmri.read_cmd_args(
        dict(
            subject=subject,
            atlas=args.atlas,
            function='load_surf_files',
            fmri_file_template=op.join(MMVT_DIR, subject, 'fmri',
                                       'words_v_symbols_{hemi}.mgz'),
        ))
    pu.run_on_subjects(args, fmri.main)
コード例 #8
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)