spm_resss.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function Vo = spm_resss(Vi,Vo,R,flags)
  2. % Create residual sum of squares image (ResSS)
  3. % FORMAT Vo = spm_resss(Vi,Vo,R,flags)
  4. % Vi - vector of mapped image volumes to work on (from spm_vol)
  5. % Vo - handle structure for mapped output image volume
  6. % R - residual forming matrix
  7. % flags - 'm' for implicit zero masking
  8. % Vo (output) - handle structure of output image volume after modifications
  9. % for writing
  10. %
  11. % Note that spm_create_vol needs to be called external to this function -
  12. % the header is not created.
  13. %__________________________________________________________________________
  14. %
  15. % Residuals are computed as R*Y, where Y is the data vector read from
  16. % images mapped as Vi. The residual sum of squares image (mapped as Vo)
  17. % is written.
  18. %
  19. %--------------------------------------------------------------------------
  20. %
  21. % For a simple linear model Y = X*B * E, with design matrix X,
  22. % (unknown) parameter vector(s) B, and data matrix Y, the least squares
  23. % estimates of B are given by b = inv(X'*X)*X'*Y. If X is rank
  24. % deficient, then the Moore-Penrose pseudoinverse may be used to obtain
  25. % the least squares parameter estimates with the lowest L2 norm: b =
  26. % pinv(X)*Y.
  27. %
  28. % The fitted values are then y = X*b = X*inv(X'*X)*X'*Y, (or
  29. % y=X*pinv(X)*Y). Since the fitted values y are usually known as
  30. % "y-hat", X*inv(X'*X)*X' is known as the "hat matrix" for this model,
  31. % denoted H.
  32. %
  33. % The residuals for this fit (estimates of E) are e = Y - y.
  34. % Substituting from the above, e = (I-H)*Y, where I is the identity
  35. % matrix (see eye). (I-H) is called the residual forming matrix,
  36. % denoted R.
  37. %
  38. % Geometrically, R is a projection matrix, projecting the data into the
  39. % subspace orthogonal to the design space.
  40. %
  41. % ----------------
  42. %
  43. % For temporally smoothed fMRI models with convolution matrix K, R is a
  44. % little more complicated:
  45. % K*Y = K*X * B + K*E
  46. % KY = KX * B + KE
  47. % ...a little working shows that hat matrix is H = KX*inv(KX'*KX)*KX'
  48. % (or KX*pinv(KX)), where KX=K*X. The smoothed residuals KE (=K*E) are
  49. % then given from the temporally smoothed data KY (=K*Y) by y=H*KY.
  50. % Thus the residualising matrix for the temporally smoothed residuals
  51. % from the temporally smoothed data is then (I-H).
  52. %
  53. % Usually the image time series is not temporally smoothed, in which
  54. % case the hat and residualising matrices must incorporate the temporal
  55. % smoothing. The hat matrix for the *raw* (unsmoothed) time series Y is
  56. % H*K, and the corresponding residualising matrix is R=(K-H*K).
  57. % In full, that's
  58. % R = (K - KX*inv(KX'*KX)*KX'*K)
  59. % or R = (K - KX*pinv(KX)*K) when using a pseudoinverse
  60. %
  61. %--------------------------------------------------------------------------
  62. %
  63. % This function can also be used when the b's are images. The residuals
  64. % are then e = Y - X*b, so let Vi refer to the vector of images and
  65. % parameter estimates ([Y;b]), and then R is ([eye(n),-X]), where n is
  66. % the number of Y images.
  67. %
  68. %--------------------------------------------------------------------------
  69. %
  70. % Don't forget to either apply any image scaling (grand mean or
  71. % proportional scaling global normalisation) to the image scalefactors,
  72. % or to combine the global scaling factors in the residual forming
  73. % matrix.
  74. %__________________________________________________________________________
  75. % Copyright (C) 1999-2012 Wellcome Trust Centre for Neuroimaging
  76. % Andrew Holmes & John Ashburner
  77. % $Id: spm_resss.m 5097 2012-12-06 16:08:16Z guillaume $
  78. %-Argument checks
  79. %--------------------------------------------------------------------------
  80. if nargin<4, flags=''; end, if isempty(flags), flags='-'; end
  81. mask = any(flags=='m');
  82. if nargin<3, error('insufficient arguments'); end
  83. ni = size(R,2); %-ni = #images
  84. if ni~=numel(Vi), error('incompatible dimensions'); end
  85. if Vo.dt(1)~=spm_type('float32')
  86. error('only float output images supported');
  87. end
  88. %-Image dimension, orientation and voxel size checks
  89. %--------------------------------------------------------------------------
  90. spm_check_orientations([Vi Vo]);
  91. %==========================================================================
  92. % - C O M P U T A T I O N
  93. %==========================================================================
  94. Y = zeros([Vo.dim(1:2),ni]); %-PlaneStack data
  95. im = false(ni,1);
  96. for j=1:ni
  97. im(j)=~spm_type(Vi(j).dt(1),'NaNrep'); %-Images without NaNrep
  98. end
  99. %-Loop over planes computing ResSS
  100. for p=1:Vo.dim(3)
  101. M = spm_matrix([0 0 p]); %-Sampling matrix
  102. %-Read plane data
  103. for j=1:ni, Y(:,:,j) = spm_slice_vol(Vi(j),M,Vi(j).dim(1:2),0); end
  104. %-Apply implicit zero mask for image types without a NaNrep
  105. if mask, Y(Y(:,:,im)==0)=NaN; end
  106. e = R*reshape(Y,prod(Vi(1).dim(1:2)),ni)'; %-residuals as DataMtx
  107. ss = reshape(sum(e.^2,1),Vi(1).dim(1:2)); %-ResSS plane
  108. Vo = spm_write_plane(Vo,ss,p); %-Write plane
  109. end