ofgulban / bvbabel

A lightweight Python library for reading & writing BrainVoyager file formats.
MIT License
19 stars 11 forks source link

PRT to BIDS style TSV conversion #24

Open ofgulban opened 1 year ago

ofgulban commented 1 year ago

Submitted by Judith:

I don’t know whether you have implemented the PRT -> TSV function already. Just in case you haven’t, I wanted to send you this script using bvbabel for writing TSV files.

# -*- coding: utf-8 -*-
"""
Save PRT as TSV file
Assuming the data has been saved according to the BIDS definition and that 
the PRTs are saved in the rawdata/func folder of each participant and session
and are named sub-ID_ses-ID_task-name_run-ID.prt
"""

__author__ = "Judith Eck"
__version__ = "0.1.0"
__date__ = "06-12-2022"
__name__ = "ConvertPRTtoTSV.py"

# =============================================================================
# Import required packages
# =============================================================================

import numpy as np
import bvbabel

# Define folder structure and file identifiers
project_path = 'C:/Users/JEck/Documents/BrainVoyager/Projects/GSG/rawdata/'
subjects = [1]
runs = [1]
sessions = [4]
task_name = 'task-blocked'

# specify the repetion time
TR = 2 # specified in seconds

# loop over all subjects, sessions and runs
for sub in range(len(subjects)):
    sub_id = 'sub-' + f'{subjects[sub]:02d}'
    for ses in range(len(sessions)):
        ses_id = 'ses-' + f'{sessions[ses]:02d}'

        for run in range(len(runs)):
            run_id = 'run-' + f'{runs[run]:02d}'

# Read the PRT file, convert it to _events.tsv and save in the same folder
            prtfile = project_path + sub_id + '/' + ses_id + '/func/' + sub_id + '_' + ses_id +  '_' + task_name + '_' + run_id + '.prt'
            prt_header, prt_data = bvbabel.prt.read_prt(prtfile)
            temp = np.empty(shape=(0,3), dtype = str)

            for cond in range(len(prt_data)):
                if prt_header['ResolutionOfTime'] == 'msec':
                    prt_data[cond]['Time start'] = np.int_(prt_data[cond]['Time start'])/1000
                    prt_data[cond]['Time stop'] = np.int_(prt_data[cond]['Time stop'])/1000
                else:
                    prt_data[cond]['Time start'] = ((prt_data[cond]['Time start'])-1)*TR
                    prt_data[cond]['Time stop'] = prt_data[cond]['Time stop']*TR
                duration = np.round(np.subtract(prt_data[cond]['Time stop'],prt_data[cond]['Time start']), decimals = 4)
                onset = prt_data[cond]['Time start']
                event_name = np.repeat(prt_data[cond]['NameOfCondition'],len(prt_data[cond]['Time start']))
                temp  = np.append(temp, np.array([onset.astype(str),duration.astype(str),event_name.astype(str)]).T, axis=0) 
                del (duration, onset, event_name)
            np.savetxt((prtfile.split('.')[0] + '_events.tsv'), temp, fmt='%s', delimiter='\t', header='onset\tduration\ttrial_type',comments='')
            del(prt_header, prt_data, prtfile, temp)