123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #!/usr/bin/python3
- '''
- created on Mon 18 2020
- author: Christian Olaf Haeusler
- '''
- from glob import glob
- import argparse
- import os
- import shutil
- import re
- CONFOUNDPATTERN = 'fg_a*seg*.*'
- def parse_arguments():
- '''
- '''
- parser = argparse.ArgumentParser(
- description='rename and copy the auditory \
- confound files to onset directories')
- parser.add_argument('-i',
- default='events/segments',
- help='directory that contains auditory confound files')
- parser.add_argument('-o',
- default='events/onsets',
- help='the output directory for onset files')
- args = parser.parse_args()
- inDir = args.i
- outDir = args.o
- return inDir, outDir
- def rms_lrdiff_luminance(confoFpath, outFpath):
- '''
- '''
- try:
- os.makedirs(os.path.dirname(outFpath))
- except OSError:
- if not os.path.isdir(os.path.dirname(outFpath)):
- raise
- # copy that shit
- shutil.copyfile(confoFpath, outFpath)
- print('copied\t', confoFpath)
- print('to:\t', outFpath, '\n')
- def perceptual_distance(confoFpath, outFpath):
- '''
- '''
- outFpath = outFpath.replace('.txt', '_pd.txt')
- with open(confoFpath) as f:
- content = f.readlines()
- # delete header
- content = content[1:]
- #
- # split lines by comma
- content = [line.split(',') for line in content]
- #
- # insert tabs as separator and movie frame duration in between
- pattern = '%s\t0.04\t%s'
- content = [pattern % (line[0], line[1]) for line in content]
- with open(outFpath, 'w') as f:
- f.writelines(content)
- print('wrote modified \t', confoFpath)
- print('to:\t\t', outFpath, '\n')
- #### main program #####
- if __name__ == "__main__":
- inDir, outDir = parse_arguments()
- inPattern = os.path.join('events/segments', '**', CONFOUNDPATTERN)
- confoFpathes = sorted(glob(inPattern, recursive=True))
- for confoFpath in confoFpathes:
- # get the segment from the filename
- segString = re.search(r'_seg\d{1}', confoFpath)
- segString = segString.group()
- segment = segString.split('_seg')[1]
- # convert segment number to run number
- run = str(int(segment) + 1)
- # rename name of the input file to correct output file name
- inFname = os.path.basename(confoFpath)
- outFname = inFname.replace(segString, '')
- outFname = outFname.replace(os.path.splitext(outFname)[1], '.txt')
- # handle different output directories for movie and audio-description
- if 'fg_ad_seg' in inFname:
- outFpath = os.path.join(outDir, 'aomovie', 'run-' + run, outFname)
- elif 'fg_av_ger_seg' in inFname:
- outFpath = os.path.join(outDir, 'avmovie', 'run-' + run, outFname)
- else:
- print('unknown stimulus')
- # create the output path in case it does not exist
- try:
- os.makedirs(os.path.dirname(outFpath))
- except OSError:
- if not os.path.isdir(os.path.dirname(outFpath)):
- raise
- # process root mean square power and left-right difference
- if 'rms' in inFname or 'lrdiff' in inFname:
- rms_lrdiff_luminance(confoFpath, outFpath)
- # process luminance confounds
- if '_lr.' in confoFpath or '_ml.' in confoFpath or '_ud.' in confoFpath:
- rms_lrdiff_luminance(confoFpath, outFpath)
- # process perceptual distance (csv files)
- if 'distance' in confoFpath:
- perceptual_distance(confoFpath, outFpath)
|