-
Notifications
You must be signed in to change notification settings - Fork 1
/
dcm-generate-models.py
executable file
·542 lines (454 loc) · 18.1 KB
/
dcm-generate-models.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/usr/bin/env python
# ------------------------------------------------------------------ #
# This is a script to create a DCM Matlab code from a quasi-verbal
# description of a model.
#
# --- Author ------------------------------------------------------- #
#
# Andrea Stocco,
# Institute for Learning and Brain Sciences
# and Department of Psychology
# University of Washington
#
# --- History ------------------------------------------------------ #
#
# 2017-11-14 : * Added support for TE values
# * Added support for loading model files from different
# folders
#
# 2015-11-08 : * Made sure inputs are not centered (in DCM10). Also,
# : * fixed a bug on the doc string.
#
# 2012-09-20 : * Added documentation string
#
# 2012-08-12 : * First working version.
#
# 2012-08-01 : * File created.
# ------------------------------------------------------------------ #
HLP_MSG="""
Usage
-----
$ dcm-generate-models.py <model_file> <dcm_dir> <subj1> <subj2> ... <subjN>
Where:
<model_file> is the file containing the model description (see
below)
<dcm_dir> is the name of the folder where the single-subject
SPM.mat file is located (and where the DCM model will be
saved)
<subjN> is the name of the folder corresponding to each subject.
Model Description
-----------------
The model description file is a text file that describes a DCM model
in a format that is transparent to the user. A model file contains
a series of statements (one statement per line) that describe the
model's connectivity, as well as user comments.
COMMENTS are introduced by the sharp sign '#', and behave very much
like shell comments: Everything between the sharp sign and the end
of the line is ignored by the script.
A MODEL DESCRIPTION file begins with three statements:
te: <TE value>
vois: <region1> <region2> ... <regionN>
inputs: <input1> <input2> ... <inputN>
Where <TE value> is the scanner's Echo Time (in secs; default is
0.021), <regionX> names an existing VOI, and <inputX> gives a name to
the various conditions that are present in the single-subject SPM.mat
file (i.e., the task model).
Note that the inputs must name the conditions in the order in which
they appear. Furthermore, you need to specify ALL the condition names
UP TO the last one you intend to use, without skipping any condition
in-between. That is, if your SPM.mat file contains four conditions,
A, B, C, and D, and you want to use only A and C, you still need to
include 'B' in-between (e.g., "inputs: A B C") even if you can omit
'D'. The reason is that the names are actually transformed into
indexes by the script, and skipping one name results in giving the
following condition a wrong index.
Also, note that while the name of the VOIs need to be the SAME as the
name of the VOI files (except for the leading 'VOI_' suffix), the
names of the conditions DO NOT need to be the same as the names in the
SPM.mat file. That is, you can call your conditions X, Y, and Z in the
SPM.mat file and refer to them as A, B, and C in the model description
file. However, they will still appear as X, Y, and Z in the final DCM,
because the DCM model will read the conditions names directly from the
SPM file (remember, the DCM specification uses indexes internally).
After declaring inputs and VOIs, the remaining part of the model
description is made of 'statements', i.e. descriptions of
connectivity patterns. A DIRECT connectivity pattern is specified using
the arrow operator '->':
<voi|input> -> <voi>
Note that the first term can be either a VOI or an input, but the
second term can only be a VOI. Also, note that the connectivity is
STRICTLY DIRECTIONAL, so that "A -> B" defines the connection FROM
REGION A TO REGION B, and not viceversa. To specify bidirectional
connections, you need to enter two separate statements, one for each
direction (e.g., "A -> B" and "B -> A").
This type of statement describes direction connection from VOIs to
VOIs and from inputs to VOIs. That is, they describe the 'A' and 'C'
matrices in the DCM file. To describe MODULATORY CONNECTIONS, you
can use the three-term-statement:
<voi|input> -> <voi|input> -> <voi>
In the three-term statement, the first term can be either a VOI or an
input, and the second and third term describe another connection
(either VOI-to-VOI or input-to-VOI) that exists in the model. This
form is used to specify MODULATORY connections between a VOI or
an input and an existing DIRECT connection.
Note that the connection between the second and the third term MUST
have already been specified when you use the three-term statement,
i.e. in order to describe 'A -> B -> C', you need to have specified
'B -> C' first.
Usage
-----
$ dcm-generate-models.py <model_file> <dcm_dir> <subj1> <subj2> ... <subjN>
Where:
<model_file> is the file containing the model description (see
below)
<dcm_dir> is the name of the folder where the single-subject
SPM.mat file is located (and where the DCM model will be
saved)
<subjN> is the name of the folder corresponding to each subject.
"""
import sys, copy, os, ntpath
def isDefinitionString(strng):
"""
A definition string is a string of the form '<inputs|vois|te> : <vals>'
"""
if strng.count(':') is not 1:
return False
else:
tkns=strng.split(':')
if len(tkns) == 2 and \
len(tkns[0].split()) == 1 and \
len(tkns[1].split()) >= 1:
return True
else:
return False
def isConnectivityString(strng):
if strng.count('->') not in [1,2]:
return False
else:
tkns = strng.split('->')
subs = [x.split() for x in tkns]
ones = [x for x in subs if len(x)==1]
if len(tkns) in [2,3] and len(subs) == len(ones):
return True
else:
return False
class Definition(object):
def __init__(self, cmnd, args):
self.command = cmnd.lower()
self.arguments = args
def parseDefinition(ln):
tkns = ln.split(':')
cmnd = tkns[0].strip()
args = tkns[1].split()
return Definition(cmnd, args)
class Connectivity(object):
def __init__(self, frm, to, mod=None):
self.to = to
self.frm = frm
self.mod = mod
if mod == None:
self.len = 2
else:
self.len = 3
self.matrix = self.DetermineMatrix()
def DetermineMatrix(self):
if self.len == 2:
if self.to.nature == "VOI" and self.frm.nature == "VOI":
return 'a'
elif self.to.nature == "VOI" and self.frm.nature == "Input":
return 'c'
else:
raise Exception("Impossible connection from %s to %s" % (self.frm, self.to))
else:
if self.frm.nature == "VOI" and \
self.to.nature == "VOI" and \
self.mod.nature == "VOI":
return 'd'
elif self.frm.nature == "VOI" and\
self.to.nature == "VOI" and \
self.mod.nature == "Input":
return 'b'
else:
raise Exception("Impossible connection from %s to %s modulated by %s" % (self.frm, self.to, self.mod))
def Elements(self):
if self.len == 2:
return [self.frm, self.to]
elif self.len == 3:
return [self.frm, self.to, self.mod]
else:
raise Exception("Wrong number of elements for Connection: %s" % self.len)
def __str__(self):
if self.len == 3:
return "DCM.%s(%d,%d,%d) = 1 %% %s -> %s -> %s" % \
(self.matrix, self.to.index, self.frm.index, self.mod.index,
self.mod.name, self.frm.name, self.to.name)
elif self.len == 2:
return "DCM.%s(%d,%d) = 1 %% %s -> %s" % \
(self.matrix, self.to.index, self.frm.index,
self.frm.name, self.to.name)
def __repr__(self):
return self.__str__()
class Element(object):
def __init__(self, name, nature="VOI", vois=[], inputs=[]):
self.name = name
if nature in ["VOI", "Input"]:
self.nature = nature
else:
raise Exception("Impossible nature for element: " % nature)
if self.nature == "VOI":
self.index = vois.index(self.name) + 1
else:
self.index = inputs.index(self.name) + 1
def __eq__(self, other):
if other != None and \
self.nature == other.nature and \
self.name == other.name and \
self.index == other.index:
return True
else:
return False
def __hash__(self):
return hash((self.name, self.nature, self.index))
def __str__(self):
return "%s<%s,%d>" % (self.name, self.nature, self.index)
def __repr__(self):
return self.__str__()
def parseElement(name, vois=[], inputs=[]):
if name in vois and name not in inputs:
#print " element %s in VOIS" % name
return Element(name, "VOI", vois=vois, inputs=inputs)
elif name in inputs and name not in vois:
#print " element %s in INPUTS" % name
return Element(name, "Input", vois=vois, inputs=inputs)
elif name in inputs and name in vois:
raise Exception("Ambiguous element: %s" % name)
else:
raise Exception("Cannot assign element: %s" % name)
def parseConnectivity(ln, vois=[], inputs=[]):
tkns = ln.split('->')
tkns = [x.strip() for x in tkns]
elms = [parseElement(x, vois=vois, inputs=inputs) for x in tkns]
if len(elms) == 2:
return Connectivity(frm=elms[0], to=elms[1])
elif len(elms) == 3:
return Connectivity(mod=elms[0], frm=elms[1], to=elms[2])
else:
raise Exception("Too many elements for connectivity: " % elms)
class Model(object):
def __init__(self, vois, inputs, te, connections, name="Model1", participant="", dcmFolder="DCM"):
self.vois = vois
self.inputs = inputs
self.te = te
self.connections = connections
self.name = name
self.participant = participant
self.dcmFolder = dcmFolder
def InputsUsed(self):
"""
Analyzes the connections to return a list of all the inputs
that are actually used in the model
"""
U = []
for c in self.connections:
U += c.Elements()
U = [x for x in U if x.nature == "Input"]
#U.sort(key=lambda x: x.index)
#U = [x.name for x in U]
return(sorted(list(set(U)), key=lambda x: x.index))
def IsNonlinear(self):
if len([x for x in self.connections if x.matrix == 'd']):
return True
else:
return False
def Check(self):
"""
Should check for obvious errors
"""
# Once a model is loaded, some things
# need to be recalculated, e.g., the indexes
# of the inputs that are really used.
U = [] # Inputs
N = [] # Names
for c in self.connections:
U += c.Elements()
U = [x for x in U if x.nature == "Input"]
N = [x.name for x in self.InputsUsed()]
for u in U:
u.index = N.index(u.name)+1
def parse_file(fileName):
"""
Parses a file in the Model Definition Format and transforms it into
an abstract representation of the model.
"""
name = ntpath.basename(fileName)
if '.' in name:
name = name[0:name.rindex('.')]
V = [] # VOIs
I = [] # Inputs
C = [] # Connectivity
TE = 0.021
f = open(fileName, 'r')
for line in f.readlines():
line=line.strip()
if '#' in line:
# Remove comments from line. Comments
# Start with '#' (like in C and Python).
line=line[0:line.find('#')]
if len(line) > 0:
#print "Parsing: %s" % line
if isDefinitionString(line):
# A definition string is a line of the form
# <inputs|vois> : <list>
# print("Found dfntn: %s" % line, file=sys.stderr)
dfnt = parseDefinition(line)
if dfnt.command == "vois":
V = dfnt.arguments
#print "VOIS", V
elif dfnt.command == "inputs":
I = dfnt.arguments
#print "INPUTS", I
elif dfnt.command == "te":
TE = float(dfnt.arguments[0])
#print("New TE is: %s" % TE, file=sys.stderr)
else:
raise Exception("Unknown definition parameter: %s" % dfnt.command)
elif isConnectivityString(line):
# A connectivity definition is a string in any of these
# four possible forms:
#
# (a) V1 -> V2
# (b) V1 -> V2 -> V3
# (c) I1 -> V1
# (d) I1 -> V1 -> V2
#
c = parseConnectivity(line, vois=V, inputs=I)
C.append(c)
else:
# If the string is neither a definition nor a
# connectivity string, we have an uninterpretable
# command.
raise Exception("Uninterpretable command: " % line)
#print(line, isDefinitionString(line), isConnectivityString(line))
#print C
return Model(vois=V, inputs=I, te=TE, connections=C, name=name)
def model_to_matlab(model):
"""
Transforms an internal representation of a model into Matlab code
that can be used in an SPM script.
"""
w = model.base
p = model.participant
f = model.dcmFolder
# Starts printing code on STOUT
print("\n% " + "-" * 66 +" %")
print("%% DCM Model (%s) for Subject %s" % (model.name, model.participant))
print("% " + "-" * 66 +" %\n")
print("clear DCM;")
print("load(fullfile('%s', '%s', '%s', 'SPM.mat'));" %
(w, p, f))
# Loads the VOIs
print("\n% --- The VOIs " + '-' * 53 + " %")
for i in range(len(model.vois)):
print("load(fullfile('%s', '%s', '%s', 'VOI_%s_1.mat'), 'xY');" %
(w, p, f, model.vois[i]))
print("DCM.xY(%d) = xY;\n" % (i+1))
# Basic initialization in Matlab
print("DCM.n = length(DCM.xY); % Num of regions")
print("DCM.v = length(DCM.xY(1).u); % Num of time points")
print("DCM.Y.dt = SPM.xY.RT;")
print("DCM.Y.X0 = DCM.xY(1).X0;")
print("for i = 1:DCM.n")
print(" DCM.Y.y(:,i) = DCM.xY(i).u;")
print(" DCM.Y.name{i} = DCM.xY(i).name;")
print("end\n")
print("DCM.Y.Q = spm_Ce(ones(1,DCM.n)*DCM.v);")
print("DCM.U.dt = SPM.Sess.U(1).dt;")
# Now, calculate which inputs are used:
U = [x.name for x in m.InputsUsed()]
U = [m.inputs.index(x)+1 for x in U]
U.sort()
if len(U) == 1:
print("DCM.U.name = [SPM.Sess.U(%d).name];" % U[0])
elif len(U) > 1:
print("DCM.U.name = [SPM.Sess.U(%d).name ..." % U[0])
for j in U[1:-1]:
print(" SPM.Sess.U(%d).name ..." % j)
print(" SPM.Sess.U(%d).name];" % U[-1])
else:
raise Exception("Fatal Error: Not enough inputs in model %s" % model.name)
# The Inputs
print("\n% --- The Inputs " + '-' * 51 + " %\n")
# The time series for each input seem to contain 32 time points more than
# needed (possibly one TR in 16-bin of microtime???). At any rate, it needs
# to be accounted for in the Matlab code.
if len(U) == 1:
print("DCM.U.u = [SPM.Sess.U(%d).u(33:end,1)];" % U[0])
elif len(U) > 1:
print("DCM.U.u = [SPM.Sess.U(%d).u(33:end,1) ..." % U[0])
for j in U[1:-1]:
print(" SPM.Sess.U(%d).u(33:end,1) ... " % j)
print(" SPM.Sess.U(%d).u(33:end,1)];" % U[-1])
else:
raise Exception("Fatal Error: Not enough inputs in model %s" % model.name)
# Set delays and TE
print("\n% Set delays and TE (TE should be gotten from SPM?)\n")
print("DCM.delays = repmat(SPM.xY.RT,%d,1);" % len(model.vois))
print("DCM.TE = %.3f;" % model.te)
# Set other options
if model.IsNonlinear():
print("DCM.options.nonlinear = 1;")
else:
print("DCM.options.nonlinear = 0;")
print("DCM.options.two_state = 0;")
print("DCM.options.stochastic = 0;")
print("DCM.options.centre = 0;")
print("DCM.options.nograph = 1;")
# The Matrices:
print("\n% --- The Matrices " + '-' * 49 + " %")
# Matrix A
print("\nDCM.a = eye(%d,%d);" % (len(model.vois), len(model.vois)))
A = copy.copy(model.connections)
A = [x for x in A if x.matrix == 'a']
for a in A:
print(a)
# Matrix B
print("\nDCM.b = zeros(%d,%d,%d);" % (len(model.vois), len(model.vois), len(model.InputsUsed())))
B = copy.copy(model.connections)
B = [x for x in B if x.matrix == 'b']
for b in B:
print(b)
# Matrix C
print("\nDCM.c = zeros(%d,%d);" % (len(model.vois), len(model.InputsUsed())))
C = copy.copy(model.connections)
C = [x for x in C if x.matrix == 'c']
for c in C:
print(c)
# Matrix D
print("\nDCM.d = zeros(%d,%d,%d);" % (len(model.vois), len(model.vois), len(model.vois)))
D = copy.copy(model.connections)
D = [x for x in D if x.matrix == 'd']
for d in D:
print(d)
# Saving and Estimating
print("\n% --- Saving and estimating " + '-' * 40 + " %\n")
print("save(fullfile('%s', '%s', '%s', 'DCM_%s.mat'));" %
(w, p, f, model.name))
print("disp('Estimating model %s for subject %s');" % (model.name, p))
print("spm_dcm_estimate(fullfile('%s', '%s', '%s', 'DCM_%s.mat'));" %
(w, p, f, model.name))
if __name__ == "__main__":
if len(sys.argv) < 3:
print(HLP_MSG)
else:
m=parse_file(sys.argv[1])
m.Check()
m.base = os.getcwd()
m.dcmFolder = sys.argv[2]
for subj in sys.argv[3:-1]:
m.participant=subj
model_to_matlab(m)
print("\n")
# The last one is ran aside to prevent the trailing "\n"
m.participant=sys.argv[-1]
model_to_matlab(m)