spm_dcm_fit.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. function [P] = spm_dcm_fit(P,use_parfor)
  2. % Bayesian inversion of DCMs using Variational Laplace
  3. % FORMAT [DCM] = spm_dcm_fit(P)
  4. %
  5. % P - {N x M} DCM structure array (or filenames) from N subjects
  6. % use_parfor - if true, will attempt to run in parallel (default: false)
  7. % NB: all DCMs are loaded into memory
  8. %
  9. % DCM - Inverted (1st level) DCM structures with posterior densities
  10. %__________________________________________________________________________
  11. %
  12. % This routine is just a wrapper that calls the appropriate dcm inversion
  13. % routine for a set a pre-specifed DCMs.
  14. %
  15. % If called with a cell array, each column is assumed to contain 1st level
  16. % DCMs inverted under the same model. Each row contains a different data
  17. % set (or subject).
  18. %__________________________________________________________________________
  19. % Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging
  20. % Karl Friston
  21. % $Id: spm_dcm_fit.m 7364 2018-07-03 14:02:46Z peter $
  22. if nargin < 2, use_parfor = false; end
  23. % get filenames and set up
  24. %--------------------------------------------------------------------------
  25. if ~nargin
  26. [P, sts] = spm_select([1 Inf],'^DCM.*\.mat$','Select DCM*.mat files');
  27. if ~sts, return; end
  28. end
  29. if ischar(P), P = cellstr(P); end
  30. if isstruct(P), P = {P}; end
  31. % Number of subjects (data) and models (of those data)
  32. %--------------------------------------------------------------------------
  33. [Ns,Nm] = size(P);
  34. % Find model class and modality
  35. %--------------------------------------------------------------------------
  36. try, load(P{1}); catch, DCM = P{1}; end
  37. model = spm_dcm_identify(DCM);
  38. if isempty(model)
  39. warning('unknown inversion scheme');
  40. return
  41. end
  42. % Get data structure for each subject (column)
  43. %--------------------------------------------------------------------------
  44. for i = 1:Ns
  45. for j = 2:Nm
  46. switch model
  47. case{'DEM'}
  48. P{i, j}.xY = P{i, 1}.Y;
  49. otherwise
  50. P{i, j}.xY = P{i, 1}.xY;
  51. end
  52. end
  53. end
  54. % Estimate
  55. %--------------------------------------------------------------------------
  56. if use_parfor
  57. % Estimate DCMs in parallel using parfor
  58. P = spm_dcm_load(P);
  59. parfor i = 1:numel(P)
  60. P{i} = fit_dcm(P{i}, model);
  61. end
  62. else
  63. % Estimate DCMs without parfor
  64. for i = 1:numel(P)
  65. try
  66. DCM = load(P{i});
  67. DCM = DCM.DCM;
  68. catch
  69. DCM = P{i};
  70. end
  71. P{i} = fit_dcm(DCM, model);
  72. end
  73. end
  74. % -------------------------------------------------------------------------
  75. function DCM = fit_dcm(DCM, model)
  76. % Inverts a DCM.
  77. % DCM - the DCM structure
  78. % model - a string identifying the model type (see spm_dcm_identify)
  79. switch model
  80. % fMRI model
  81. %----------------------------------------------------------------------
  82. case{'fMRI'}
  83. DCM = spm_dcm_estimate(DCM);
  84. % conventional neural-mass and mean-field models
  85. %----------------------------------------------------------------------
  86. case{'fMRI_CSD'}
  87. DCM = spm_dcm_fmri_csd(DCM);
  88. % conventional neural-mass and mean-field models
  89. %----------------------------------------------------------------------
  90. case{'ERP'}
  91. DCM = spm_dcm_erp(DCM);
  92. % cross-spectral density model (complex)
  93. %----------------------------------------------------------------------
  94. case{'CSD'}
  95. DCM = spm_dcm_csd(DCM);
  96. % cross-spectral density model (steady-state responses)
  97. %----------------------------------------------------------------------
  98. case{'TFM'}
  99. DCM = spm_dcm_tfm(DCM);
  100. % induced responses
  101. %----------------------------------------------------------------------
  102. case{'IND'}
  103. DCM = spm_dcm_ind(DCM);
  104. % phase coupling
  105. %----------------------------------------------------------------------
  106. case{'PHA'}
  107. DCM = spm_dcm_phase(DCM);
  108. % cross-spectral density model (steady-state responses)
  109. %----------------------------------------------------------------------
  110. case{'NFM'}
  111. DCM = spm_dcm_nfm(DCM);
  112. % behavioural Markov decision process model
  113. %----------------------------------------------------------------------
  114. case{'MDP'}
  115. DCM = spm_dcm_mdp(DCM);
  116. % generic nonlinear system identification
  117. %----------------------------------------------------------------------
  118. case{'NLSI'}
  119. [Ep,Cp,Eh,F] = spm_nlsi_GN(DCM.M,DCM.xU,DCM.xY);
  120. DCM.Ep = Ep;
  121. DCM.Eh = Eh;
  122. DCM.Cp = Cp;
  123. DCM.F = F;
  124. % hierarchical dynamic mmodel
  125. %----------------------------------------------------------------------
  126. case{'DEM'}
  127. DCM = spm_DEM(DCM);
  128. % default
  129. %----------------------------------------------------------------------
  130. otherwise
  131. try
  132. DCM = feval(model, DCM);
  133. catch
  134. error('unknown DCM');
  135. end
  136. end