spm_bms_partition.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. function spm_bms_partition(BMS)
  2. % Compute model partitioning for BMS
  3. % FORMAT spm_bms_partition(BMS)
  4. %
  5. % Input:
  6. % BMS structure (BMS.mat)
  7. %
  8. % Output:
  9. % PPM (images) for each of the subsets defined
  10. % xppm_subsetn.img (RFX) and ppm_subsetn.img (FFX)
  11. %__________________________________________________________________________
  12. % Copyright (C) 2009-2011 Wellcome Trust Centre for Neuroimaging
  13. % Maria Joao Rosa
  14. % $Id: spm_bms_partition.m 4492 2011-09-16 12:11:09Z guillaume $
  15. % Contrast vector
  16. % -------------------------------------------------------------------------
  17. spm_input('Specify contrast vector. Example: [1 1 2 2 3 3]',1,'d');
  18. contrast = spm_input('Contrast vector',2,'e',[]);
  19. % Inference method to plot
  20. % -------------------------------------------------------------------------
  21. method = spm_input('Inference method',3,'b','FFX|RFX',['FFX';'RFX']);
  22. nb_subsets = length(unique(contrast));
  23. max_cont = max(contrast);
  24. nb_models = length(contrast);
  25. switch method
  26. case 'FFX'
  27. str_method = 'ffx';
  28. str_output = 'ppm';
  29. case 'RFX'
  30. str_method = 'rfx';
  31. str_output = 'xppm';
  32. otherwise
  33. error('Unknown inference method.');
  34. end
  35. % Check if ffx exists
  36. % -------------------------------------------------------------------------
  37. if ~isfield(BMS.map,str_method)
  38. msgbox(sprintf('No %s analysis in current BMS.mat.',method));
  39. return
  40. end
  41. % Check number of subsets and nb of models
  42. % -------------------------------------------------------------------------
  43. bms_fields = eval(sprintf('BMS.map.%s.ppm',str_method));
  44. nmodels = size(bms_fields,2);
  45. if nb_models ~= nmodels || nb_subsets == 1 || max_cont ~= nb_subsets
  46. msgbox('Invalid contrast vector!')
  47. return
  48. end
  49. % Get data for each subset
  50. % -------------------------------------------------------------------------
  51. data = cell(1,nb_subsets);
  52. for i = 1:nmodels,
  53. num = contrast(i);
  54. data{num} = [data{num};bms_fields{i}];
  55. end
  56. % Create new images by summing old the ppms
  57. % -------------------------------------------------------------------------
  58. pth = fileparts(BMS.fname);
  59. data_vol = cell(nb_subsets,1);
  60. ftmp = cell(nb_subsets,1);
  61. for j = 1:nb_subsets,
  62. data_vol{j} = spm_vol(char(data{j}));
  63. n_models_sub = size(data{j},1);
  64. ftmp{j} = 'i1';
  65. for jj = 1:n_models_sub-1
  66. ftmp{j} = [ftmp{j},sprintf(' + i%d',jj+1)];
  67. end
  68. fname = fullfile(pth,[sprintf('subset%d_%s',j,str_output) spm_file_ext]);
  69. save_fn{j} = fname;
  70. Vo = calc_im(j,data_vol,fname,ftmp);
  71. end
  72. % Save new BMS structure
  73. % -------------------------------------------------------------------------
  74. bms_struct = eval(sprintf('BMS.map.%s',str_method));
  75. bms_struct.subsets = save_fn;
  76. switch method
  77. case 'FFX'
  78. BMS.map.ffx = bms_struct;
  79. case 'RFX'
  80. BMS.map.rfx = bms_struct;
  81. end
  82. file_name = BMS.fname;
  83. BMS.xSPM = [];
  84. save(file_name,'BMS', spm_get_defaults('mat.format'))
  85. % Return to results
  86. %==========================================================================
  87. spm_input('Done',1,'d');
  88. return;
  89. %==========================================================================
  90. % out = calc_im(j,data_vol,fname,ftmp)
  91. %==========================================================================
  92. % Function to sum the data (taken from spm_imcalc)
  93. %--------------------------------------------------------------------------
  94. function out = calc_im(j,data_vol,fname,ftmp)
  95. Vi_tmp = data_vol{j};
  96. Vi = Vi_tmp(1);
  97. Vo(j) = struct(...
  98. 'fname', fname,...
  99. 'dim', Vi.dim,...
  100. 'dt', [spm_type('float32') spm_platform('bigend')],...
  101. 'mat', Vi.mat,...
  102. 'descrip', 'spm - algebra');
  103. hold = 1; mask = 0; dmtx = 0;
  104. Vi = data_vol{j};
  105. n = numel(Vi);
  106. Y = zeros(Vo(j).dim(1:3));
  107. f = ftmp{j};
  108. for p = 1:Vo(j).dim(3),
  109. B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
  110. if dmtx, X=zeros(n,prod(Vo(j).dim(1:2))); end
  111. for i = 1:n
  112. M = inv(B*inv(Vo(j).mat)*Vi(i).mat);
  113. d = spm_slice_vol(Vi(i),M,Vo(j).dim(1:2),[hold,NaN]);
  114. if (mask<0), d(isnan(d))=0; end;
  115. if (mask>0) && ~spm_type(Vi(i).dt(1),'nanrep'), d(d==0)=NaN; end
  116. if dmtx, X(i,:) = d(:)'; else eval(['i',num2str(i),'=d;']); end
  117. end
  118. try
  119. eval(['Yp = ' f ';']);
  120. catch
  121. error(['Can''t evaluate "',f,'".']);
  122. end
  123. if prod(Vo(j).dim(1:2)) ~= numel(Yp),
  124. error(['"',f,'" produced incompatible image.']); end
  125. if (mask<0), Yp(isnan(Yp))=0; end
  126. Y(:,:,p) = reshape(Yp,Vo(j).dim(1:2));
  127. end
  128. temp = Vo(j);
  129. temp = spm_write_vol(temp,Y);
  130. out(j) = temp;