confounds2onsets.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python3
  2. '''
  3. created on Mon 18 2020
  4. author: Christian Olaf Haeusler
  5. '''
  6. from glob import glob
  7. import argparse
  8. import os
  9. import shutil
  10. import re
  11. CONFOUNDPATTERN = 'fg_a*seg*.*'
  12. def parse_arguments():
  13. '''
  14. '''
  15. parser = argparse.ArgumentParser(
  16. description='rename and copy the auditory \
  17. confound files to onset directories')
  18. parser.add_argument('-i',
  19. default='events/segments',
  20. help='directory that contains auditory confound files')
  21. parser.add_argument('-o',
  22. default='events/onsets',
  23. help='the output directory for onset files')
  24. args = parser.parse_args()
  25. inDir = args.i
  26. outDir = args.o
  27. return inDir, outDir
  28. def rms_lrdiff_luminance(confoFpath, outFpath):
  29. '''
  30. '''
  31. try:
  32. os.makedirs(os.path.dirname(outFpath))
  33. except OSError:
  34. if not os.path.isdir(os.path.dirname(outFpath)):
  35. raise
  36. # copy that shit
  37. shutil.copyfile(confoFpath, outFpath)
  38. print('copied\t', confoFpath)
  39. print('to:\t', outFpath, '\n')
  40. def perceptual_distance(confoFpath, outFpath):
  41. '''
  42. '''
  43. outFpath = outFpath.replace('.txt', '_pd.txt')
  44. with open(confoFpath) as f:
  45. content = f.readlines()
  46. # delete header
  47. content = content[1:]
  48. #
  49. # split lines by comma
  50. content = [line.split(',') for line in content]
  51. #
  52. # insert tabs as separator and movie frame duration in between
  53. pattern = '%s\t0.04\t%s'
  54. content = [pattern % (line[0], line[1]) for line in content]
  55. with open(outFpath, 'w') as f:
  56. f.writelines(content)
  57. print('wrote modified \t', confoFpath)
  58. print('to:\t\t', outFpath, '\n')
  59. #### main program #####
  60. if __name__ == "__main__":
  61. inDir, outDir = parse_arguments()
  62. inPattern = os.path.join('events/segments', '**', CONFOUNDPATTERN)
  63. confoFpathes = sorted(glob(inPattern, recursive=True))
  64. for confoFpath in confoFpathes:
  65. # get the segment from the filename
  66. segString = re.search(r'_seg\d{1}', confoFpath)
  67. segString = segString.group()
  68. segment = segString.split('_seg')[1]
  69. # convert segment number to run number
  70. run = str(int(segment) + 1)
  71. # rename name of the input file to correct output file name
  72. inFname = os.path.basename(confoFpath)
  73. outFname = inFname.replace(segString, '')
  74. outFname = outFname.replace(os.path.splitext(outFname)[1], '.txt')
  75. # handle different output directories for movie and audio-description
  76. if 'fg_ad_seg' in inFname:
  77. outFpath = os.path.join(outDir, 'aomovie', 'run-' + run, outFname)
  78. elif 'fg_av_ger_seg' in inFname:
  79. outFpath = os.path.join(outDir, 'avmovie', 'run-' + run, outFname)
  80. else:
  81. print('unknown stimulus')
  82. # create the output path in case it does not exist
  83. try:
  84. os.makedirs(os.path.dirname(outFpath))
  85. except OSError:
  86. if not os.path.isdir(os.path.dirname(outFpath)):
  87. raise
  88. # process root mean square power and left-right difference
  89. if 'rms' in inFname or 'lrdiff' in inFname:
  90. rms_lrdiff_luminance(confoFpath, outFpath)
  91. # process luminance confounds
  92. if '_lr.' in confoFpath or '_ml.' in confoFpath or '_ud.' in confoFpath:
  93. rms_lrdiff_luminance(confoFpath, outFpath)
  94. # process perceptual distance (csv files)
  95. if 'distance' in confoFpath:
  96. perceptual_distance(confoFpath, outFpath)