From 138fcdb977f2b6c8faab2fd0f30b59b8b93343b9 Mon Sep 17 00:00:00 2001 From: Jeff Stout Date: Tue, 2 Jul 2024 13:25:31 -0400 Subject: [PATCH] Create a standardized ID input for BIDS --- nih2mne/utilities/standardize_eventID_list.py | 69 +++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 70 insertions(+) create mode 100755 nih2mne/utilities/standardize_eventID_list.py diff --git a/nih2mne/utilities/standardize_eventID_list.py b/nih2mne/utilities/standardize_eventID_list.py new file mode 100755 index 0000000..9678f21 --- /dev/null +++ b/nih2mne/utilities/standardize_eventID_list.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Jul 2 11:06:33 2024 + +@author: jstout +""" + +import pandas as pd +import mne +import os, os.path as op +import glob + + +def write_standardized_event_list(meg_dir, task=None): + ''' + Read out the annotations from all runs of a task a make a standardized + list. This function will write out a task_TASK_standardized_EventId_list.csv + file for each task set in the folder. + + Parameters + ---------- + meg_dir : str + CTF meg dataset directory. + task : str + Specify task or leave empty and it will iterate over all tasks + + Returns + ------- + None. + + ''' + if task==None: + tmp_ = glob.glob(op.join(meg_dir, '*.ds')) + task_list = [op.basename(i).split('_')[1] for i in tmp_] + else: + task_list = [task] + + for task in task_list: + print(f'Generating unique entry ID list for {task}') + task_dsets = glob.glob(op.join(meg_dir, f'*_{task}_*.ds')) + sorted(task_dsets) + + id_list = [] + for dset in task_dsets: + raw = mne.io.read_raw_ctf(dset, system_clock='ignore') + id_list.extend(list(raw.annotations.description)[:]) + + id_list = sorted(set(id_list)) #Sort unique entries + id_list = pd.DataFrame(zip(id_list,range(1, 1+len(id_list))), + columns=['ID_names', 'ID_vals']) + id_list.to_csv(op.join(meg_dir, f'task_{task}_standardized_EventId_list.csv')) + +def main_parser(): + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('-megdir', + help='CTF acquisition folder - generally a date') + parser.add_argument('-task', + help='''Specify a task to write the standardized IDs. + Leave blank to process all tasks''', + default=None) + args = parser.parse_args() + write_standardized_event_list(args.megdir, task=args.task) + + + + + diff --git a/pyproject.toml b/pyproject.toml index ae22362..a36b9f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ testing = ['datalad','pytest','pygit2'] "fix_dsname.py"='nih2mne.utilities.fix_dsname:main' "megcore_prep_mri_bids.py"='nih2mne.megcore_prep_mri_bids:main' "qa_fids.py"='nih2mne.utilities.qa_fids:main' +"standardize_eventID_list.py"='nih2mne.utilities.standardize_eventID_list:main_parser' #'nih2mne/eyetracking_preprocessing.py', #'cmdline/make_bids_fs_swarm.sh',