RetMap_phaseshift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # Shift the phase of the input nifti.
  3. # It is useful to correct for different wedge or ring starting points,
  4. # ie. if the polar angle wedge doesn't start from the upper vertical
  5. # meridian and your further processing expects it to start at the upper
  6. # vertical meridian.
  7. def make_phaseshift(niftipath, shift):
  8. import nibabel as nib
  9. import numpy as np
  10. import os
  11. cond = os.path.basename(niftipath).split("_")[0]
  12. print "condition: ", cond
  13. ## get condition
  14. #dsback2niftisave = os.path.dirname(os.path.dirname(niftipath))+'/post_processing/'+cond+"_phShift.nii.gz"
  15. dsback2niftisave = 'post_processing/'+cond+"_phShift.nii.gz"
  16. ## set shift depending on condition
  17. #~ if cond == 'ccw':
  18. #~ shift = -281.25
  19. #~ elif cond == 'clw':
  20. #~ shift = 101.25
  21. #~ elif cond == "con":
  22. #~ shift = -337.5
  23. #~ elif cond == "exp":
  24. #~ shift = 0
  25. shift=float(shift)
  26. print "shifting by degree: ", shift
  27. ## get nifti
  28. nifti= nib.load(niftipath)
  29. data = nifti.get_data()
  30. # do phase shift depending on condition
  31. tmp0 = (data[:,:,:,0] + shift)%360
  32. #tmp0[tmp0 >= 360] = tmp0[tmp0 >= 360] - 360
  33. #tmp0[tmp0 < 0] = tmp0[tmp0 < 0] + 360
  34. #~ if cond == 'ccw' or cond == "con":
  35. #~ tmp0 = 360-tmp0
  36. data[:,:,:,0]=tmp0
  37. dsback2nifti = nib.Nifti1Image(data, nifti.get_affine())
  38. nib.save(dsback2nifti, dsback2niftisave)
  39. if __name__ == "__main__":
  40. import sys
  41. niftipath = sys.argv[1]
  42. shift = sys.argv[2]
  43. print "processing file: \n", niftipath
  44. make_phaseshift(niftipath, shift)