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