mk_movie_ds.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from mvpa2.suite import fmri_dataset, vstack
  2. import numpy as np
  3. import nibabel as nb
  4. def preprocessed_fmri_dataset(
  5. bold_fname, preproc_img=None, preproc_ds=None, add_sa=None,
  6. **kwargs):
  7. """
  8. Parameters
  9. ----------
  10. bold_fname : str
  11. File name of BOLD scan
  12. preproc_img : callable or None
  13. See get_bold_run_dataset() documentation
  14. preproc_ds : callable or None
  15. If not None, this callable will be called with each run bold dataset
  16. as an argument before ``modelfx`` is executed. The callable must
  17. return a dataset.
  18. add_sa : dict or None
  19. Returns
  20. -------
  21. Dataset
  22. """
  23. # open the BOLD image
  24. bold_img = nb.load(bold_fname)
  25. if not preproc_img is None:
  26. bold_img = preproc_img(bold_img)
  27. # load (and mask) data
  28. ds = fmri_dataset(bold_img, **kwargs)
  29. if not add_sa is None:
  30. for sa in add_sa:
  31. ds.sa[sa] = add_sa[sa]
  32. if not preproc_ds is None:
  33. ds = preproc_ds(ds)
  34. return ds
  35. def movie_dataset(subj, task, label, **kwargs):
  36. # ds = movie_dataset(
  37. # 2,
  38. # 'avmovie',
  39. # 'bold3Tp2',
  40. # mask='src/tnt/sub-02/bold3Tp2/brain_mask.nii.gz')
  41. cur_max_time = 0
  42. segments = []
  43. if not 'add_sa' in kwargs:
  44. add_sa = {}
  45. for seg in range(1, 9):
  46. print 'Seg', seg
  47. mc = np.recfromtxt(
  48. 'sub-%.2i/in_%s/sub-%.2i_task-%s_run-%i_bold_mcparams.txt'
  49. % (subj, label, subj, task, seg),
  50. names=('mc_xtrans', 'mc_ytrans', 'mc_ztrans', 'mc_xrot',
  51. 'mc_yrot', 'mc_zrot'))
  52. for i in mc.dtype.fields:
  53. add_sa[i] = mc[i]
  54. ds = preprocessed_fmri_dataset(
  55. 'sub-%.2i/in_%s/sub-%.2i_task-%s_run-%i_bold.nii.gz'
  56. % (subj, label, subj, task, seg),
  57. add_sa=add_sa,
  58. **kwargs)
  59. ds.sa['movie_segment'] = [seg] * len(ds)
  60. TR = np.diff(ds.sa.time_coords).mean()
  61. ## truncate segment time series to remove overlap
  62. if seg > 1:
  63. ds = ds[4:]
  64. if seg < 8:
  65. ds = ds[:-4]
  66. ds.sa['movie_time'] = np.arange(len(ds)) * TR + cur_max_time
  67. cur_max_time = ds.sa.movie_time[-1] + TR
  68. segments.append(ds)
  69. return vstack(segments)