highspeed-bids-fieldmaps.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # ======================================================================
  4. # SCRIPT INFORMATION:
  5. # ======================================================================
  6. # SCRIPT: UPDATE THE FIELDMAP JSON FILES
  7. # PROJECT: HIGHSPEED
  8. # WRITTEN BY LENNART WITTKUHN, 2018 - 2020
  9. # CONTACT: WITTKUHN AT MPIB HYPHEN BERLIN DOT MPG DOT DE
  10. # MAX PLANCK RESEARCH GROUP NEUROCODE
  11. # MAX PLANCK INSTITUTE FOR HUMAN DEVELOPMENT
  12. # MAX PLANCK UCL CENTRE FOR COMPUTATIONAL PSYCHIATRY AND AGEING RESEARCH
  13. # LENTZEALLEE 94, 14195 BERLIN, GERMANY
  14. # ======================================================================
  15. # IMPORT RELEVANT PACKAGES
  16. # ======================================================================
  17. import os
  18. import glob
  19. import json
  20. import stat
  21. # ======================================================================
  22. # DEFINE PATHS
  23. # ======================================================================
  24. # to run type python3 bids_fieldmaps_info.py $PATH_BIDS
  25. # where $PATH_BIDS is the path to your BIDS directory
  26. # path to the project root:
  27. project_name = 'highspeed-bids'
  28. path_root = os.getcwd().split(project_name)[0] + project_name
  29. path_fmap = os.path.join(path_root, '*', '*', 'fmap', '*.json')
  30. path_func = os.path.join(path_root, '*', '*', 'func', '*.nii.gz')
  31. # ======================================================================
  32. # UPDATE FIELDMAP JSON FILES
  33. # ======================================================================
  34. # get all fieldmap files in the data-set:
  35. files_fmap = glob.glob(path_fmap)
  36. # loop over all field-map files:
  37. for file_path in files_fmap:
  38. # open the .json file of the fieldmap acquisition:
  39. with open(file_path, 'r') as in_file:
  40. json_info = json.load(in_file)
  41. in_file.close()
  42. # get the path to the session folder of a specific participant:
  43. file_base = os.path.dirname(os.path.dirname(file_path))
  44. # get the path to all functional acquisitions in that session:
  45. files_func = glob.glob(os.path.join(file_base, 'func', '*nii.gz'))
  46. session = os.path.basename(file_base)
  47. up_dirs = os.path.join(session, 'func')
  48. intended_for = [os.path.join(up_dirs, os.path.basename(file)) for file in files_func]
  49. json_info["IntendedFor"] = sorted(intended_for)
  50. # change file permissions to read:
  51. permissions = os.stat(file_path).st_mode
  52. os.chmod(path=file_path, mode=permissions | stat.S_IWUSR)
  53. # save updated fieldmap json-file:
  54. with open(file_path, 'w') as out_file:
  55. json.dump(json_info, out_file, indent=2, sort_keys=True)
  56. out_file.close()
  57. # change file permissions back to read-only:
  58. os.chmod(path=file_path, mode=permissions)