calc_ao_av_corr 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/python
  2. """
  3. FILL ME IN
  4. """
  5. import os
  6. import sys
  7. if not len(sys.argv) == 3:
  8. print __doc__
  9. sys.exit(1)
  10. # subj
  11. subj = int(sys.argv[1])
  12. # run
  13. run = int(sys.argv[2])
  14. from os.path import join as _opj
  15. import numpy as np
  16. from scipy.stats import spearmanr, pearsonr
  17. from mvpa2.base.hdf5 import h5save
  18. from nilearn.image import smooth_img
  19. import nibabel as nb
  20. from mvpa2.mappers.base import ChainMapper
  21. from mvpa2.mappers.detrend import PolyDetrendMapper
  22. from mvpa2.mappers.filters import IIRFilterMapper
  23. from scipy import signal
  24. from mvpa2.datasets.mri import fmri_dataset
  25. from mvpa2.datasets.mri import map2nifti
  26. mc_reg_names = ('mc_xtrans', 'mc_ytrans', 'mc_ztrans', 'mc_xrot',
  27. 'mc_yrot', 'mc_zrot')
  28. # TODO put this into pymvpa
  29. def preprocessed_fmri_dataset(
  30. bold_fname, preproc_img=None, preproc_ds=None, add_sa=None,
  31. **kwargs):
  32. """
  33. Parameters
  34. ----------
  35. bold_fname : str
  36. File name of BOLD scan
  37. preproc_img : callable or None
  38. See get_bold_run_dataset() documentation
  39. preproc_ds : callable or None
  40. If not None, this callable will be called with each run bold dataset
  41. as an argument before ``modelfx`` is executed. The callable must
  42. return a dataset.
  43. add_sa : dict or None
  44. Returns
  45. -------
  46. Dataset
  47. """
  48. # open the BOLD image
  49. bold_img = nb.load(bold_fname)
  50. if not preproc_img is None:
  51. bold_img = preproc_img(bold_img)
  52. # load (and mask) data
  53. ds = fmri_dataset(bold_img, **kwargs)
  54. if not add_sa is None:
  55. if hasattr(add_sa, 'dtype') and not add_sa.dtype.names is None:
  56. # this is a recarray
  57. iter_ = add_sa.dtype.names
  58. else:
  59. # assume dict
  60. iter_ = add_sa
  61. for sa in iter_:
  62. ds.sa[sa] = add_sa[sa]
  63. if not preproc_ds is None:
  64. ds = preproc_ds(ds)
  65. return ds
  66. def movie_run_ds(subj, run, task, imgspace, **kwargs):
  67. if task == 'aomovie':
  68. mcfile = \
  69. 'src/phase1/sub-%.2i/BOLD/task001_run%.3i/bold_dico_moco.txt' \
  70. % (subj, run)
  71. elif task == 'avmovie':
  72. mcfile = \
  73. 'sub-%.2i/in_%s/sub-%.2i_task-%s_run-%i_bold_mcparams.txt' \
  74. % (subj, imgspace, subj, task, run)
  75. else:
  76. raise ValueError("unknown task '%s'" % task)
  77. mc = np.recfromtxt(mcfile, names=mc_reg_names)
  78. ds = preprocessed_fmri_dataset(
  79. 'sub-%.2i/in_%s/sub-%.2i_task-%s_run-%i_bold.nii.gz'
  80. % (subj, imgspace, subj, task, run),
  81. add_sa=mc,
  82. **kwargs)
  83. return ds
  84. def fisher_xfm(arr, n):
  85. arr = np.asanyarray(arr)
  86. # z-transform
  87. z = 0.5 * np.log((1 + arr) / (1 - arr))
  88. # standardize to unit variance
  89. z = z / (1. / np.sqrt(n - 3))
  90. return z
  91. def smooth(img):
  92. # we need to preserve the original header because the smoothing function
  93. # fucks the TR up
  94. nimg = smooth_img(img, fwhm=4.0)
  95. return nb.Nifti1Image(nimg.get_data(),
  96. img.get_affine(),
  97. header=img.get_header())
  98. # template files are intensity normalized, >100 should be a good liberal brain
  99. # mask
  100. mask = nb.load(
  101. 'src/tnt/sub-%.2i/bold7Tp1/in_bold3Tp2/brain.nii.gz' % subj).get_data() > 100
  102. # time series pre-processing
  103. # regress out motion
  104. pdm = PolyDetrendMapper(0, opt_regs=mc_reg_names, auto_train=True)
  105. # spectral filter
  106. sfm = IIRFilterMapper(*signal.butter(8, (0.016, 0.25), btype='bandpass'))
  107. tspreproc = ChainMapper([pdm, sfm])
  108. aomovie = movie_run_ds(subj, run, 'aomovie','bold3Tp2',
  109. mask=mask, preproc_img=smooth, preproc_ds=tspreproc)
  110. avmovie = movie_run_ds(subj, run, 'avmovie','bold3Tp2',
  111. mask=mask, preproc_img=smooth, preproc_ds=tspreproc)
  112. # compute voxelwise correlation between audio and audio-visual data
  113. sr = np.array(
  114. [spearmanr(aomovie.samples[:,i], avmovie.samples[:,i])[0]
  115. for i in xrange(aomovie.nfeatures)])
  116. pr = np.array(
  117. [pearsonr(aomovie.samples[:,i], avmovie.samples[:,i])[0]
  118. for i in xrange(aomovie.nfeatures)])
  119. # Fisher transformation to yield normal distribution
  120. sz = fisher_xfm(sr, len(aomovie))
  121. pz = fisher_xfm(pr, len(aomovie))
  122. nb.save(map2nifti(aomovie, sz),
  123. 'sub-%.2i/in_bold3Tp2/sub-%.2i_aoav_run-%i_spearmanZ.nii.gz'
  124. % (subj, subj, run))
  125. nb.save(map2nifti(aomovie, pz),
  126. 'sub-%.2i/in_bold3Tp2/sub-%.2i_aoav_run-%i_pearsonZ.nii.gz'
  127. % (subj, subj, run))