123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #!/usr/bin/python3
- '''
- created on Fri Feb 14 2020
- author: Christian Olaf Haeusler
- '''
- from glob import glob
- import argparse
- import os
- import re
- import shutil
- def parse_arguments():
- '''
- '''
- parser = argparse.ArgumentParser(
- description='copy confound files to subject-specific directories')
- parser.add_argument('-fmri',
- default='inputs/studyforrest-data-aligned/sub-??/in_bold3Tp2/sub-??_task-aomovie_run-1_bold.nii.gz',
- help='''pattern of pathes/files that contains 4D fmri files
- (e.g. 'inputs/studyforrest-data-aligned/sub-??/in_bold3Tp2/sub-??_task-aomovie_run-1_bold.nii.gz');
- needed to get subjects for which used 4D data exist''')
- parser.add_argument('-onsets',
- default='./events/onsets/aomovie/run-?/*.txt',
- help='''pattern for input path/filenames of\n
- onsets files (e.g. './events/onsets/aomovie/run-?/*.txt')''')
- parser.add_argument('-o',
- default='./',
- help='the directory in which individual subject directories will be created'
- )
- args = parser.parse_args()
- fmriPattern = args.fmri
- onsetPattern = args.onsets
- outDir = args.o
- return fmriPattern, onsetPattern, outDir
- def get_subjects(pattern):
- '''
- searches a given directory (e.g. a directory with functional data) for
- subdirs to get name / number of available subjects
- Output:
- list of subdirectory (str; format 'sub-xy')
- '''
- pathes = sorted(glob(pattern))
- subs = []
- for path in pathes:
- x = re.search(r'sub-\d{2}', path)
- sub = x.group()[-2:]
- subs.append(int(sub))
- return subs
- def copy_sources_2_destinations(outDir, sub, stim, sources):
- '''
- '''
- # create the list of destinations
- dests = [os.path.join(outDir, sub, 'onsets', stim, 'run'+ x.split('run')[1])
- for x in sources]
- sourcesDests = zip(sources, dests)
- for source, dest in sourcesDests:
- # create folders if necessary
- try:
- os.makedirs(os.path.dirname(dest))
- except OSError:
- if not os.path.isdir(os.path.dirname(dest)):
- raise
- # copy that shit
- shutil.copyfile(source, dest)
- return None
- if __name__ == "__main__":
- fmriPattern, onsetPattern, outDir = parse_arguments()
- # get subject numbers from available subject folders
- subs = get_subjects(fmriPattern)
- regex = re.search(r'[a-z]{2}movie', onsetPattern)
- stim = regex.group()
- onsetFiles = sorted(glob(onsetPattern))
- for sub in subs:
- sub = 'sub-' + str(sub).zfill(2)
- # copy event files for the audio-only movie
- copy_sources_2_destinations(outDir, sub, stim, onsetFiles)
|