Beispiel #1
0
def _bids2nipypeinfo(in_file,
                     events_file,
                     regressors_file,
                     regressors_names=None,
                     motion_columns=None,
                     decimals=3,
                     amplitude=1.0,
                     removeTR=4,
                     lastTR=496,
                     thr=0.5):
    from pathlib import Path
    import numpy as np
    import pandas as pd
    from scrubFunc import scrub
    from nipype.interfaces.base.support import Bunch
    # Process the events file
    events = pd.read_csv(events_file)
    bunch_fields = ['onsets', 'durations', 'amplitudes']
    if not motion_columns:
        from itertools import product
        motion_columns = [
            '_'.join(v) for v in product(('trans', 'rot'), 'xyz')
        ]
    out_motion = Path('motion.par').resolve()
    #regress_data = pd.read_csv(regressors_file, sep=r'\s+')
    regress_data = scrub(regressors_file,
                         thr)  # grab also per which will be saved as file
    np.savetxt(
        out_motion,
        regress_data[motion_columns].values[removeTR:lastTR + removeTR, ],
        '%g')
    if regressors_names is None:
        regressors_names = sorted(
            set(regress_data.columns) - set(motion_columns))
    if regressors_names:
        bunch_fields += ['regressor_names']
        bunch_fields += ['regressors']
    runinfo = Bunch(scans=in_file,
                    conditions=list(set(events.trial_type.values)),
                    **{k: []
                       for k in bunch_fields})
    for condition in runinfo.conditions:
        event = events[events.trial_type.str.match(condition)]
        runinfo.onsets.append(
            np.round(event.onset.values - removeTR, 3).tolist()
        )  # added -removeTR to align to the onsets after removing X number of TRs from the scan
        runinfo.durations.append(np.round(event.duration.values, 3).tolist())
        if 'amplitudes' in events.columns:
            runinfo.amplitudes.append(
                np.round(event.amplitudes.values, 3).tolist())
        else:
            runinfo.amplitudes.append([amplitude] * len(event))
    if 'regressor_names' in bunch_fields:
        runinfo.regressor_names = regressors_names
        runinfo.regressors = regress_data[regressors_names].fillna(0.0).values[
            removeTR:lastTR +
            removeTR, ].T.tolist()  # adding removeTR to cut the first rows
    return runinfo, str(out_motion)
Beispiel #2
0
def saveScrub(regressors_file, thr):
    from pathlib import Path
    import numpy as np
    import pandas as pd
    from scrubFunc import scrub
    # this function will call scrub and save a file with precentage of scrubbed framewise_displacement
    perFile = Path('percentScrub.txt').resolve()
    regress_data = pd.read_csv(regressors_file, sep=r'\s+')
    regress_data  = scrub(regressors_file, thr) # grab also per which will be saved as file
    x = regress_data.scrub
    per = np.array([sum(x)/len(x)])
    np.savetxt(perFile, per, '%g')
    return str(perFile)