neuroscout/tasks/utils/build.py
def writeout_events(analysis, pes, outdir, run_ids=None):
    """ Writeout predictor_events into BIDS event files """
    analysis_runs = analysis.get('runs', [])
    if run_ids is not None:
        analysis_runs = [r for r in analysis_runs if r['id'] in run_ids]

    desc = {
        'Name': analysis['hash_id'],
        'BIDSVersion': '1.1.1',
        'PipelineDescription': {'Name': 'Neuroscout Events'}
        }

    desc_path = (outdir / 'dataset_description.json')
    paths = [(str(desc_path), 'dataset_description.json')]
    json.dump(desc, desc_path.open('w'))

    outdir = outdir / "func"
    outdir.mkdir(exist_ok=True)

    # Load events and rename columns to human-readable
    pes = pd.DataFrame(pes)
    predictor_names = {p['id']: p['name'] for p in analysis['predictors']}
    pes.predictor_id = pes.predictor_id.map(predictor_names)

    # Write out event files
    for run in analysis_runs:
        # Write out event files for each run_id
        run_events = pes[pes.run_id == run['id']].drop('run_id', axis=1)
        entities = _get_entities(run)

        out_cols = {}
        if run_events.empty is False:
            for name, df in run_events.groupby('predictor_id'):
                df_col = df.groupby(['onset', 'duration'])['value'].max()
                df_col = df_col.reset_index().rename(
                    columns={'value': name})
                out_cols[name] = df_col

        # For any columns that don't have events, output n/a file
        for name in set(predictor_names.values()) - out_cols.keys():
            df_col = pd.DataFrame([[0, 1, 'n/a']],
                                  columns=['onset', 'duration', name])
            out_cols[name] = df_col

        # Write out files
        for name, df_col in out_cols.items():
            # Write out BIDS path
            fname = outdir / name / build_path(
                entities, path_patterns=PATHS)
            fname.parent.mkdir(exist_ok=True)
            paths.append(
                (str(fname), 'events/{}/{}'.format(name, fname.name)))
            df_col.to_csv(fname, sep='\t', index=False)

    return paths
 Writeout predictor_events into BIDS event files 
Writeout predictor_events into BIDS event files
Writeout predictor_events into BIDS event files
def writeout_events(analysis, pes, outdir, run_ids=None):
    analysis_runs = analysis.get('runs', [])
    if run_ids is not None:
        analysis_runs = [r for r in analysis_runs if r['id'] in run_ids]
    desc = {
        'Name': analysis['hash_id'],
        'BIDSVersion': '1.1.1',
        'PipelineDescription': {'Name': 'Neuroscout Events'}
        }
    desc_path = (outdir / 'dataset_description.json')
    paths = [(str(desc_path), 'dataset_description.json')]
    json.dump(desc, desc_path.open('w'))
    outdir = outdir / "func"
    outdir.mkdir(exist_ok=True)
    pes = pd.DataFrame(pes)
    predictor_names = {p['id']: p['name'] for p in analysis['predictors']}
    pes.predictor_id = pes.predictor_id.map(predictor_names)
    for run in analysis_runs:
         for each run_id
        run_events = pes[pes.run_id == run['id']].drop('run_id', axis=1)
        entities = _get_entities(run)
        out_cols = {}
        if run_events.empty is False:
            for name, df in run_events.groupby('predictor_id'):
                df_col = df.groupby(['onset', 'duration'])['value'].max()
                df_col = df_col.reset_index().rename(
                    columns={'value': name})
                out_cols[name] = df_col
        for name in set(predictor_names.values()) - out_cols.keys():
            df_col = pd.DataFrame([[0, 1, 'n/a']],
                                  columns=['onset', 'duration', name])
            out_cols[name] = df_col
        for name, df_col in out_cols.items():
            fname = outdir / name / build_path(
                entities, path_patterns=PATHS)
            fname.parent.mkdir(exist_ok=True)
            paths.append(
                (str(fname), 'events/{}/{}'.format(name, fname.name)))
            df_col.to_csv(fname, sep='\t', index=False)
    return paths
def writeout_events ( analysis , pes , outdir , run_ids = None ) : analysis_runs = analysis . get ( 'runs' , [ ] ) if run_ids is not None : analysis_runs = [ r for r in analysis_runs if r [ 'id' ] in run_ids ] desc = { 'Name' : analysis [ 'hash_id' ] , 'BIDSVersion' : '1.1.1' , 'PipelineDescription' : { 'Name' : 'Neuroscout Events' } } desc_path = ( outdir / 'dataset_description.json' ) paths = [ ( str ( desc_path ) , 'dataset_description.json' ) ] json . dump ( desc , desc_path . open ( 'w' ) ) outdir = outdir / "func" outdir . mkdir ( exist_ok = True ) pes = pd . DataFrame ( pes ) predictor_names = { p [ 'id' ] : p [ 'name' ] for p in analysis [ 'predictors' ] } pes . predictor_id = pes . predictor_id . map ( predictor_names ) for run in analysis_runs : run_events = pes [ pes . run_id == run [ 'id' ] ] . drop ( 'run_id' , axis = 1 ) entities = _get_entities ( run ) out_cols = { } if run_events . empty is False : for name , df in run_events . groupby ( 'predictor_id' ) : df_col = df . groupby ( [ 'onset' , 'duration' ] ) [ 'value' ] . max ( ) df_col = df_col . reset_index ( ) . rename ( columns = { 'value' : name } ) out_cols [ name ] = df_col for name in set ( predictor_names . values ( ) ) - out_cols . keys ( ) : df_col = pd . DataFrame ( [ [ 0 , 1 , 'n/a' ] ] , columns = [ 'onset' , 'duration' , name ] ) out_cols [ name ] = df_col for name , df_col in out_cols . items ( ) : fname = outdir / name / build_path ( entities , path_patterns = PATHS ) fname . parent . mkdir ( exist_ok = True ) paths . append ( ( str ( fname ) , 'events/{}/{}' . format ( name , fname . name ) ) ) df_col . to_csv ( fname , sep = '\t' , index = False ) return paths
Writeout predictor_events into BIDS event files
Writeout predictor_events into BIDS event files
""" Writeout predictor_events into BIDS event files """ # Load events and rename columns to human-readable # Write out event files # Write out event files for each run_id # For any columns that don't have events, output n/a file # Write out files # Write out BIDS path
analysis
pes
outdir
run_ids