pybids icon indicating copy to clipboard operation
pybids copied to clipboard

Add function for retrieving electrode dataframe given a subject and channel type

Open timonmerk opened this issue 5 years ago • 0 comments

Hello everyone, thanks for the great tool!

A primary analysis in iEEG research is to retrieve electrode files. This can be done using pybids in a manner that the electrode is read with the respective extension and suffix. A handy function would be to merge all electrode files for a patient of a given type and return them as a dataframe. I would such a function:

def get_electrodes(sub, ch_type, space, bids_path):
    """Returns pandas dataframe of electrodes of a given subject in a BIDS root directory

    Args:
        sub (string): BIDS subject
        ch_type (string): BIDS specific channel type in channels.tsv
        space (string) : BIDS specific electrode space
        bids_path (string): BIDS root directory

    Returns:
        pandas dataframe: run concatenated electrode tsv dataframe for given subject and channel type
    """
    layout = BIDSLayout(bids_path)

    channels = layout.get(subject=sub, extension='.tsv', suffix='channels')
    electrodes = layout.get(subject=sub, space=space, extension='.tsv', suffix='electrodes')

    if len(channels) != len(electrodes):
        assert False, "channel.tsv length and electrodes.tsv does not match"

    df_electrodes = pd.DataFrame()

    for channel_file, electrode_file in zip(channels, electrodes):
        df_run = electrode_file.get_df()[np.array(channel_file.get_df()["type"] == ch_type)]
        df_electrodes = pd.concat([df_electrodes,df_run]).drop_duplicates().reset_index(drop=True)    
        
    return df_electrodes

Maybe a functionality like this could be useful. If this is not the scope of pyBIDS feel free to close the issue :)

timonmerk avatar Mar 08 '21 17:03 timonmerk