Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import numpy as np
  2. import nibabel as nib
  3.  
  4.  
  5. def fit_film_gls(ts_img, X, tcon, mask=None,
  6. smooth_struct=None, smooth_fwhm=None):
  7.  
  8. from numpy.fft import fft, ifft
  9. from numpy.linalg import lstsq, pinv
  10.  
  11. ts_vol = ts_img.get_data()
  12. if mask is None:
  13. mask = ts_vol.var(axis=-1) > 0
  14.  
  15. nvox = mask.sum()
  16. ntp = ts_vol.shape[-1]
  17. nev = X.shape[1]
  18. ncon = len(tcon)
  19. assert X.shape[0] == ntp
  20.  
  21. Y = ts_vol[mask].T
  22. Y = Y - Y.mean(axis=0)
  23.  
  24. # Fit initial iteration OLS model in one step
  25. B_ols, _, _, _ = lstsq(X, Y)
  26. Yhat_ols = X.dot(B_ols)
  27. resid_ols = Y - Yhat_ols
  28. assert resid_ols.shape == (ntp, nvox)
  29.  
  30. # Estimate the residual autocorrelation function
  31. tukey_m = int(np.round(np.sqrt(ntp)))
  32. autocorr_pad = ntp * 2 - 1
  33. resid_fft = fft(resid_ols, n=autocorr_pad, axis=0)
  34. acf_fft = resid_fft * resid_fft.conjugate()
  35. acf = ifft(acf_fft, axis=0).real[:tukey_m]
  36. acf /= acf[0]
  37. assert acf.shape == (tukey_m, nvox)
  38.  
  39. # Regularize the autocorrelation estimates with a tukey taper
  40. lag = np.arange(tukey_m)
  41. window = .5 * (1 + np.cos(np.pi * lag / tukey_m))
  42. acf_tukey = acf * window[:, np.newaxis]
  43. assert acf_tukey.shape == (tukey_m, nvox)
  44.  
  45. # Smooth the autocorrelation estimates
  46. # TODO
  47.  
  48. # Compute the autocorrelation kernel
  49. w_pad = ntp + tukey_m
  50. acf_kernel = np.zeros((w_pad, nvox))
  51. acf_kernel[:tukey_m] = acf_tukey
  52. acf_kernel[-tukey_m + 1:] = acf_tukey[:0:-1]
  53. assert (acf_kernel != 0).sum() == (nvox * (tukey_m * 2 - 1))
  54.  
  55. # Compute the prewhitening kernel in the spectral domain
  56. acf_fft = fft(acf_kernel, axis=0).real
  57. W_fft = np.zeros((w_pad, nvox))
  58. W_fft[1:] = 1 / np.sqrt(np.abs(acf_fft[1:]))
  59. W_fft /= np.sqrt(np.sum(W_fft[1:] ** 2, axis=0, keepdims=True)) / w_pad
  60.  
  61. # Prewhiten the data
  62. Y_fft = fft(Y, axis=0, n=w_pad)
  63. WY = ifft(W_fft * Y_fft.real
  64. + W_fft * Y_fft.imag * 1j,
  65. axis=0).real[:ntp]
  66.  
  67. # Prewhiten the design
  68. X_fft = fft(X, axis=0, n=w_pad)
  69. X_fft_exp = X_fft[:, :, np.newaxis]
  70. W_fft_exp = W_fft[:, np.newaxis, :]
  71. WX = ifft(W_fft_exp * X_fft_exp.real
  72. + W_fft_exp * X_fft_exp.imag * 1j,
  73. axis=0).real[:ntp]
  74.  
  75. # Fit the GLS model at each voxel
  76. B = np.empty((nvox, nev))
  77. G = np.empty((nvox, ncon))
  78. V = np.empty((nvox, ncon))
  79.  
  80. for i in range(nvox):
  81.  
  82. Wyi, WXi = WY[..., i], WX[..., i]
  83. XtXinv = pinv(WXi.T.dot(WXi))
  84. bi = XtXinv.dot(WXi.T).dot(Wyi)
  85. Ri = np.eye(ntp) - WXi.dot(XtXinv).dot(WXi.T)
  86. ri = Ri.dot(Wyi)
  87. ssi = ri.dot(ri) / Ri.trace()
  88.  
  89. for j, cj in enumerate(tcon):
  90.  
  91. keff = cj.dot(XtXinv).dot(cj)
  92.  
  93. gij = cj.dot(bi)
  94. vij = keff * ssi
  95.  
  96. G[i, j] = gij
  97. V[i, j] = vij
  98.  
  99. B[i] = bi
  100.  
  101. T = G / np.sqrt(V)
  102.  
  103. # Deal with the outputs
  104. ni, nj, nk = ts_img.shape[:-1]
  105.  
  106. beta_vol = np.zeros((ni, nj, nk, nev))
  107. beta_vol[mask] = B
  108. beta_img = nib.Nifti1Image(beta_vol, ts_img.affine, ts_img.header)
  109.  
  110. cope_vol = np.zeros((ni, nj, nk, ncon))
  111. cope_vol[mask] = G
  112. cope_img = nib.Nifti1Image(cope_vol, ts_img.affine, ts_img.header)
  113.  
  114. varcope_vol = np.zeros((ni, nj, nk, ncon))
  115. varcope_vol[mask] = V
  116. varcope_img = nib.Nifti1Image(varcope_vol, ts_img.affine, ts_img.header)
  117.  
  118. t_vol = np.zeros((ni, nj, nk, ncon))
  119. t_vol[mask] = T
  120. t_img = nib.Nifti1Image(t_vol, ts_img.affine, ts_img.header)
  121.  
  122. return beta_img, cope_img, varcope_img, t_img
  123.  
  124.  
  125. if __name__ == "__main__":
  126.  
  127. import sys
  128. _, ts_fname, dmat_fname, tcon_fname, thresh = sys.argv
  129.  
  130. ts_img = nib.load(ts_fname)
  131. X = np.loadtxt(dmat_fname, comments="/")
  132. tcon = np.loadtxt(tcon_fname, comments="/")
  133. mask = ts_img.get_data().mean(axis=-1) > float(thresh)
  134.  
  135. results = fit_film_gls(ts_img, X, tcon, mask)
  136.  
  137. beta_img, cope_img, varcope_img, t_img = results
  138.  
  139. beta_img.to_filename("betas.nii.gz")
  140. cope_img.to_filename("copes.nii.gz")
  141. varcope_img.to_filename("varcopes.nii.gz")
  142. t_img.to_filename("tstats.nii.gz")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement