spm_mvb_U.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function U = spm_mvb_U(Y,priors,X0,xyz,vox)
  2. % Constructs patterns U for Multivariate Bayesian inversion of a linear model
  3. % FORMAT U = spm_mvb_U(Y,priors,X0,xyz,vox)
  4. % Y - data-feature matrix
  5. % priors - 'null' % no patterns
  6. % - 'compact' % reduced (ns/3); using SVD on local compact support
  7. % - 'sparse' % a pattern is a voxel
  8. % - 'smooth' % patterns are local Gaussian kernels
  9. % - 'singular' % patterns are global singular vectors
  10. % - 'support' % the patterns are the images
  11. %
  12. % X0 - confounds
  13. % xyz - location in mm
  14. % vox - voxel size
  15. %__________________________________________________________________________
  16. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  17. % Karl Friston
  18. % $Id: spm_mvb_U.m 5219 2013-01-29 17:07:07Z spm $
  19. % defaults
  20. %--------------------------------------------------------------------------
  21. try, X0; catch, X0 = []; end
  22. try, xyz; catch, xyz = []; end
  23. % get orders
  24. %--------------------------------------------------------------------------
  25. ns = size(Y,1); % number of samples
  26. nv = size(Y,2); % number of voxels
  27. % confounds
  28. %--------------------------------------------------------------------------
  29. if isempty(X0); X0 = zeros(ns,1); end
  30. % get U: X = Y*P + X0*Q + R
  31. % P = U*E;
  32. %--------------------------------------------------------------------------
  33. % assemble empirical priors
  34. %==========================================================================
  35. switch priors
  36. case 'null'
  37. %------------------------------------------------------------------
  38. U = sparse(nv,0);
  39. case 'sparse'
  40. %------------------------------------------------------------------
  41. U = speye(nv,nv);
  42. case 'smooth'
  43. %------------------------------------------------------------------
  44. sm = 4; % smoothness fixed at 4mm std
  45. dlim = (4*sm)^2; % spatial limit (mm)^2
  46. s = sm^2; % Smoothness variance
  47. xyz = xyz';
  48. nlim = 256; % voxel limit
  49. Vvx = prod(vox); % volume of a voxel
  50. Vlr = 4/3*pi*dlim^(3/2); % VOI around a voxel
  51. Nlr = round(Vlr/Vvx*.85); % # of voxel in VOI; keep 85%
  52. U = spalloc(nv,nv,nv*Nlr); % pre-allocate memory
  53. fprintf('Creating smooth patterns - please wait\n')
  54. kk = floor(nv/4);
  55. fprintf('\n0%%')
  56. for i = 1:nv
  57. if ~rem(i,kk), fprintf('.....%2i%%',25*i/kk); end
  58. u = (xyz(:,1) - xyz(i,1)).^2;
  59. j = find(u < dlim);
  60. u(j) = u(j) + (xyz(j,2) - xyz(i,2)).^2;
  61. j = j((u(j) < dlim));
  62. u(j) = u(j) + (xyz(j,3) - xyz(i,3)).^2;
  63. j = j((u(j) < dlim));
  64. if length(j)>nlim
  65. [q,k] = sort(u(j));
  66. k = k(1:nlim);
  67. j = j(k);
  68. end
  69. U(j,i) = exp(-u(j)/(2*s));
  70. end
  71. fprintf('\nThank you\n')
  72. case 'singular'
  73. % get kernel (singular vectors)
  74. %------------------------------------------------------------------
  75. Y = Y - X0*(pinv(X0)*Y); % remove confounds
  76. [u,s,v] = spm_svd(Y,1/4); % c.f., Kaiser criterion
  77. U = v/s;
  78. case 'support'
  79. % get kernel (image vectors)
  80. %------------------------------------------------------------------
  81. if nv > ns
  82. R = speye(size(X0,1)) - X0*pinv(X0);
  83. U = (R*Y)';
  84. else
  85. U = speye(nv,nv);
  86. end
  87. case 'compact'
  88. % get kernel (compact vectors)
  89. %------------------------------------------------------------------
  90. nu = min(ceil(ns/3),nv); % number of patterns
  91. nc = max(fix(nv/nu),1); % voxels in compact support
  92. X0 = spm_svd(X0);
  93. Y = Y - X0*(X0'*Y); % remove confounds
  94. C = sum(Y.^2); % variance of Y
  95. U = spalloc(nv,nu,nc*nu);
  96. J = 1:nv;
  97. for i = 1:nu
  98. % find maximum variance voxel
  99. %--------------------------------------------------------------
  100. [v,j] = max(C);
  101. d = 0;
  102. for k = 1:size(xyz,1)
  103. d = d + (xyz(k,:) - xyz(k,j)).^2;
  104. end
  105. [d,j] = sort(d);
  106. try
  107. j = j(1:nc);
  108. end
  109. % save principal eigenvector
  110. %--------------------------------------------------------------
  111. k = J(j);
  112. u = spm_svd(Y(:,k)');
  113. U(k,i) = u(:,1);
  114. % remove compact support voxels and start again
  115. %--------------------------------------------------------------
  116. J(j) = [];
  117. C(j) = [];
  118. xyz(:,j) = [];
  119. end
  120. otherwise
  121. disp('unknown prior')
  122. return
  123. end