rename.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. from glob import glob
  3. def rename_func(basename):
  4. name, ext = os.path.splitext(basename)
  5. assert ext in ['.npy', '.tsv'], f'{basename} {ext}'
  6. info = {}
  7. chunks = os.path.basename(name).split('_')
  8. if chunks[-1] == 'timeseries':
  9. extra = '_timeseries'
  10. chunks = chunks[:-1]
  11. else:
  12. extra = ''
  13. for chunk in chunks:
  14. key, val = chunk.split('-')
  15. info[key] = val
  16. for key in info:
  17. if key not in ['sub', 'ses', 'task', 'run', 'desc']:
  18. raise ValueError
  19. sid, ses, task, run = info['sub'], info['ses'], info['task'], info['run']
  20. if ses == 'movie' and task == 'movie':
  21. new_run = int(run)
  22. new_task = 'forrest'
  23. elif task in ['objectcategories', 'retmapccw', 'retmapclw', 'retmapcon', 'retmapexp', 'movielocalizer']:
  24. new_run = int(run)
  25. new_task = task
  26. else:
  27. raise ValueError
  28. label = f'sub-{sid}_task-{new_task}_run-{new_run:02d}'
  29. if 'desc' in info:
  30. label += f'_desc-{info["desc"]}'
  31. new_name = label + extra + ext
  32. return new_name
  33. if __name__ == '__main__':
  34. fns = sorted(glob(os.path.join('*', 'resampled', '*', '*', '*', 'sub-*.npy')))
  35. print(len(fns))
  36. for fn in fns:
  37. parts = []
  38. head = fn
  39. while head:
  40. head, tail = os.path.split(head)
  41. parts.insert(0, tail)
  42. fmriprep_version, _, space, structure, resample, basename = parts
  43. out_basename = rename_func(basename)
  44. out_fn = os.path.join(fmriprep_version, 'renamed', space, structure, resample, out_basename)
  45. relpath = os.path.relpath(fn, os.path.dirname(out_fn))
  46. print(relpath)
  47. os.makedirs(os.path.dirname(out_fn), exist_ok=True)
  48. if not os.path.lexists(out_fn):
  49. os.symlink(relpath, out_fn)
  50. fns = sorted(glob(os.path.join('*', 'confounds', 'sub-*.npy')) + glob(os.path.join('*', 'confounds', 'sub-*.tsv')))
  51. print(len(fns))
  52. for fn in fns:
  53. parts = []
  54. head = fn
  55. while head:
  56. head, tail = os.path.split(head)
  57. parts.insert(0, tail)
  58. print(parts)
  59. fmriprep_version, _, basename = parts
  60. out_basename = rename_func(basename)
  61. out_fn = os.path.join(fmriprep_version, 'renamed_confounds', out_basename)
  62. relpath = os.path.relpath(fn, os.path.dirname(out_fn))
  63. print(relpath)
  64. os.makedirs(os.path.dirname(out_fn), exist_ok=True)
  65. if not os.path.lexists(out_fn):
  66. os.symlink(relpath, out_fn)