-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_tools.py
More file actions
1053 lines (890 loc) · 39.2 KB
/
sim_tools.py
File metadata and controls
1053 lines (890 loc) · 39.2 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# sim_tools.py - Routines for running a model simulation
#
# Author: Stefan Fuertinger [stefan.fuertinger@esi-frankfurt.de]
# Created: June 23 2014
# Last modified: <2017-10-19 16:40:42>
from __future__ import division
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import ipdb; import sys
import imp
import h5py
import os
import re
import psutil
from datetime import datetime
from texttable import Texttable
from scipy import ndimage
from nipy.modalities.fmri import hrf, utils
import shutil
import tempfile
from recipes import moveit
try:
from the_model import par, solve_model
except:
print "\n\tWARNING: Could not import the model - `run_model` will not work!"
print "\tTry running `make all` in a terminal first"
##########################################################################################
def run_model(V0, Z0, DA0, task, outfile, \
seed=None, paramfile='parameters.py', symsyn=True, verbose=True, ram_use=0.2,\
**kwargs):
"""
Run a simulation using the neural population model
Parameters
----------
V0 : NumPy 1darray
Initial conditions for excitatory neurons. If `N` regions are simulated then `V0` has
to have length `N`.
Z0 : NumPy 1darray
Initial conditions for inhibitory neurons. If `N` regions are simulated then `Z0` has
to have length `N`.
DA0 : NumPy 1darray
Initial conditions for dopamine levels. If `N` regions are simulated then `DA0` has
to have length `N`.
task : str
Specify which task should be simulated. Currently, only 'rest' and 'speech' are supported.
outfile : str
File-name (including path if not in working directory) of HDF5 container that will be created to
save simulation results. See Notes for the structure of the generated container. Any existing
file will be renamed. The user has to have writing permissions for the given location.
seed : int
Random number generator seed. To make meaningful comparisons between successive simulation
runs, the random number seed should be fixed so that the solver uses the same Wiener process
realizations. Also, if synaptic coupling strengths are sampled from a probability distribution,
simulation results will vary from run to run unless the seed is fixed.
paramfile : str
Parameter file-name (including path if not in working directory) that should be used for simulation.
The parameter file has to be a Python file (.py extension). For more details refer to `Examples`
below. You should have received a sample parameter file (`parameters.py`) with this copy
of `sim_tools.py`.
symsyn : bool
Boolean switch determining whether synaptic coupling strengths should be symmetrized between
hemispheres (`symsyn=True`) or not.
verbose : bool
If `True` the code will print a summary of the most important parameters and all used
keyword arguments (see below) in the simulation together with a progress bar to (roughly)
estimate run time (requires the `progressbar` module).
ram_use : float
Fraction of memory to use for caching simulation results before writing to disk
(0 < `ram_use` < 1). More available memory means fewer disk-writes and thus better performance,
i.e, the larger `ram_use` the faster this code runs. However, if too much RAM is allocated by
this routine it may stall the executing computer. By default, `ram_use = 0.2`, i.e., around 20%
of available memory is used.
kwargs : additional keyword arguments
Instead of relying solely on a static file to define parameter values, it is also possible
to pass on parameters to the code using keyword arguments (see `Examples` below). Note: parameters
given as keyword arguments have higher priority than values set in `paramfile`, i.e., if
`p1 = 1` is defined in `paramfile` but `p1 = 2` is a keyword argument, the code will use
`p1 = 2` in the simulation. This behavior was intentionally implemented to enable the use
of this function within a parameter identification framework.
Returns
-------
Nothing : None
Simulation results are saved in the HDF5 container specified by `outfile`. See `Notes` for details.
Notes
-----
Due to the (usually) high temporal resolution of simulations, results are not kept in memory (and
thus returned as variable in the caller's work-space) but saved directly to disk using the HDF5
container `outfile`. The code uses the HDF library's data chunking feature to save entire
segments on disk while running. By default the code will allocate around 20% of available
memory to cache simulation results. Hence, more memory leads to fewer disk-writes during
run-time and thus faster performance.
The structure of the generated output container is as follows: all state variables and the dopaminergic
gain `Beta` are stored at the top-level of the file. Additionally, the employed coupling
matrix `C` and dopamine connection matrix `D` are also saved in the top level group. All used parameters
are saved in the subgroup `params`.
Examples
--------
Let `V0`, `Z0`, and `DA0` (NumPy 1darrays of length `N`) be initial conditions of the
model. Assuming that a valid parameter file (called `parameters.py`) is located in the current
working directory, the following call will run a resting state simulation and save the output
in the HDF5 container `sim_rest.h5`
>>> run_model(V0,Z0,DA0,'rest','sim_rest.h5')
Assume another parameter file, say, `par_patho.py` hold parameter settings simulating a
certain pathology. Then the command
>>> run_model(V0,Z0,DA0,'rest','patho/sim_rest_patho.h5',paramfile='par_patho.py')
runs a resting state simulation with the same initial conditions and saves the result in
the container `sim_rest_patho.h5` in the sub-directory `patho` (which must already exist, otherwise
an error is raised).
If only one or two parameters should be changed from their values found in a given parameter file,
it is probably more handy to change the value of these parameters from the command line, rather
than to write a separate parameter file (that is identical to the original one except for two
values). Thus, assume the values of `VK` and `VL` should be -0.4 and -0.9 respectively, i.e.,
different than those found in (the otherwise fine) `par_patho.py`. Then the command
>>> run_model(V0,Z0,DA0,'rest','patho/sim_rest_patho.h5',paramfile='par_patho.py',VK=-0.4,VL=-0.9)
runs the same resting state simulation as above but with `VK=-0.4` and `VL=-0.9`. This feature
can also be used to efficiently embed `run_model` in a parameter identification framework.
See also
--------
plot_sim : plot simulations generated by run_model
References
----------
.. [1] S. Fuertinger, J. C. Zinn, and K. Simonyan. A Neural Population Model Incorporating
Dopaminergic Neurotransmission during Complex Voluntary Behaviors. PLoS Computational Biology,
10(11), 2014.
"""
# Sanity checks for initial conditions
n = np.zeros((3,))
vnames = ['V0','Z0','DA0']
for i, vec in enumerate([V0, Z0, DA0]):
arrcheck(vec,'vector',vnames[i])
n[i] = vec.size
if np.unique(n).size > 1:
raise ValueError('The initial conditions for `V`, `Z`, and `DA` have to have the same length!')
# Check the `task` string
if not isinstance(task,(str,unicode)):
raise TypeError('Task has to be specified as string, not '+type(task).__name__+'!')
task = str(task)
if task != 'rest' and task != 'speech':
raise ValueError("The value of `task` has to be either 'rest' or 'speech'!")
# The path to the output file should be a valid
if not isinstance(outfile,(str,unicode)):
raise TypeError('Output filename has to be a string!')
outfile = str(outfile)
if outfile.find("~") == 0:
outfile = os.path.expanduser('~') + outfile[1:]
slash = outfile.rfind(os.sep)
if slash >= 0 and not os.path.isdir(outfile[:outfile.rfind(os.sep)]):
raise ValueError('Invalid path for output file: '+outfile+'!')
# Set or get random number generator seed
if seed is not None:
scalarcheck(seed,'seed',kind='int')
else:
seed = np.random.get_state()[1][0]
seed = int(seed)
# Make sure `paramfile` is a valid path
if not isinstance(paramfile,(str,unicode)):
raise TypeError('Parameter file has to be specified using a string!')
paramfile = str(paramfile)
if paramfile.find("~") == 0:
paramfile = os.path.expanduser('~') + paramfile[1:]
if not os.path.isfile(paramfile):
raise ValueError('Parameter file: '+paramfile+' does not exist!')
# Make sure `symsyn` and `verbose` are Boolean
if not isinstance(symsyn,bool):
raise TypeError("The switch `symsyn` has to be Boolean!")
if not isinstance(verbose,bool):
raise TypeError("The switch `verbose` has to be Boolean!")
# Finally, check `ram_use`
scalarcheck(ram_use,'ram_use',bounds=[0,1])
# Append '.h5' extension to `outfile` if necessary
if outfile[-3:] != '.h5':
outfile = outfile + '.h5'
# Check if `paramfile` has an extension, if yes, rip it off
if paramfile[-3:] == '.py':
paramfile = paramfile[0:-3]
# Divide `paramfile` into file-name and path
slash = paramfile.rfind(os.sep)
if slash < 0:
pth = '.'
fname = paramfile
else:
pth = paramfile[0:slash+1]
fname = paramfile[slash+1:]
# Import parameters module and initialize corresponding dictionary (remove `__file__`, etc)
param_py = imp.load_module(fname,*imp.find_module(fname,[pth]))
p_dict = {}
for key, value in param_py.__dict__.items():
if key[0:2] != "__":
p_dict[key] = value
# Try to load coupling and dopamine pathway matrices
mfile = "None"
vnames = ['C','D']
for mat_str in vnames:
if kwargs.has_key(mat_str):
p_dict[mat_str] = kwargs[mat_str]
else:
try:
p_dict[mat_str] = h5py.File(param_py.matrices,'r')[mat_str].value
mfile = p_dict['matrices']
except:
raise ValueError("Error reading `"+param_py.matrices+"`!")
arrcheck(p_dict[mat_str],'matrix',mat_str)
# Try to load ROI names
try:
names = h5py.File(param_py.matrices,'r')['names'].value
mfile = p_dict['matrices']
except:
try:
names = kwargs['names']
except:
raise ValueError("A NumPy 1darray or Python list of ROI names has to be either specified "+\
"in a matrix container or provided as keyword argument!")
p_dict['names'] = names
# See if we have an (optional) list/array of ROI-shorthand labels
try:
p_dict['labels'] = h5py.File(param_py.matrices,'r')['labels'].value
mfile = p_dict['matrices']
except:
if kwargs.has_key('labels'):
p_dict['labels'] = kwargs['labels']
# Put ones on the diagonal of the coupling matrix to ensure compatibility with the code
np.fill_diagonal(p_dict['C'],1.0)
# Get dimension of matrix and check correspondence
N = p_dict['C'].shape[0]
if N != p_dict['D'].shape[0]:
raise ValueError("Dopamine and coupling matrices don't have the same dimension!")
if len(names) != N:
raise ValueError("Matrix is "+str(N)+"-by-"+str(N)+" but `names` has length "+str(len(names))+"!")
for nm in names:
if not isinstance(nm,(str,unicode)):
raise ValueError("Names have to be provided as Python list/NumPy array of strings!")
# If user provided some additional parameters as keyword arguments, copy them to `p_dict`
for key, value in kwargs.items():
p_dict[key] = value
# Get synaptic couplings (and set seed of random number generator)
np.random.seed(seed)
if kwargs.has_key('aei'):
aei = kwargs['aei']
else:
aei = eval(param_py.aei)
if kwargs.has_key('aie'):
aie = kwargs['aie']
else:
aie = eval(param_py.aie)
if kwargs.has_key('ani'):
ani = kwargs['ani']
else:
ani = eval(param_py.ani)
if kwargs.has_key('ane'):
ane = kwargs['ane']
else:
ane = eval(param_py.ane)
# If wanted, make sure left/right hemispheres have balanced coupling strengths
if symsyn:
# Get indices of left-hemispheric regions and throw a warning if left/right don't match up
regex = re.compile("[Ll]_*")
match = np.vectorize(lambda x:bool(regex.match(x)))(names)
l_ind = np.where(match == True)[0]
r_ind = np.where(match == False)[0]
if l_ind.size != r_ind.size:
print "WARNING: Number of left-side regions = "+str(l_ind.size)+\
" not equal to number of right-side regions = "+str(r_ind.size)
# Equalize coupling strengths
aei[l_ind] = aei[r_ind]
aie[l_ind] = aie[r_ind]
ani[l_ind] = ani[r_ind]
ane[l_ind] = ane[r_ind]
# Save updated coupling strengths and random number generator seed in dictionary
p_dict['aei'] = aei
p_dict['aie'] = aie
p_dict['ani'] = ani
p_dict['ane'] = ane
p_dict['seed'] = seed
# If a resting state simulation is done, make sure dopamine doesn't kick in, i.e., enforce `rmax == rmin`
if task == 'rest':
rmax = np.ones((N,))*p_dict['rmin']
else:
if not kwargs.has_key('rmax'):
p_dict['rmax'] = eval(param_py.rmax)
# Save given task in dictionary
p_dict['task'] = task
# Get ion channel parameters
if not kwargs.has_key('TCa'):
p_dict['TCa'] = eval(param_py.TCa)
# Compute length for simulation and speech on-/offset times
len_cycle = p_dict['stimulus'] + p_dict['production'] + p_dict['acquisition']
speechon = p_dict['stimulus']
speechoff = p_dict['stimulus'] + p_dict['production']
# Save that stuff
p_dict['len_cycle'] = len_cycle
p_dict['speechon'] = speechon
p_dict['speechoff'] = speechoff
# Set/get initial time for simulation
if p_dict.has_key('tstart'):
tstart = p_dict['tstart'] # Use `p_dict` here, since `tstart` could be a kwarg!
if verbose: print "WARNING: Using custom initial time of "+str(tstart)+" (has to be in ms)!"
else:
tstart = 0
# Set/get step-size for simulation
if p_dict.has_key('dt'):
dt = p_dict['dt']
if verbose: print "WARNING: Using custom step-size of "+str(dt)+" (has to be in ms)!"
else:
dt = 1e-1
# Get sampling step size (in ms) and check if "original" step-size makes sense
ds = 1/p_dict['s_rate']*1000
if dt > ds:
print "WARNING: Step-size dt = "+str(dt)+\
" larger than chosen sampling frequency of "+str(s_rate)+"Hz."+\
" Using dt = "+str(ds)+"ms instead. "
dt = ds
# Compute sampling rate (w.r.t `dt`)
s_step = int(np.round(ds/dt))
# Save step-size and sampling rate in dictionary for later reference
p_dict['dt'] = dt
p_dict['s_step'] = s_step
# Compute end time for simulation (in ms) and allocate time-step array
tend = tstart + len_cycle*p_dict['n_cycles']*1000
tsteps = np.arange(tstart,tend,dt)
# Get the size of the time-array
tsize = tsteps.size
# Before laying out output HDF5 container, rename existing files to not accidentally overwrite 'em
moveit(outfile)
# Chunk outifle depending on available memory (eat up ~ 100*`ram_use`% of RAM)
datype = np.dtype('float64')
meminfo = psutil.virtual_memory()
maxmem = int(meminfo.available*ram_use/(5*N)/datype.itemsize)
maxmem += s_step - np.mod(maxmem,s_step)
# If the whole array fits into memory load it once, otherwise chunk it up
if tsteps.size <= maxmem:
blocksize = [tsize]
csize = int(np.ceil(tsize/s_step))
chunksize = [csize]
chunks = True
else:
bsize = int(tsize//maxmem)
rest = int(np.mod(tsize,maxmem))
blocksize = [maxmem]*bsize
if rest > 0: blocksize = blocksize + [rest]
numblocks = len(blocksize)
csize = int(np.ceil(maxmem/s_step))
restc = int(np.ceil(blocksize[-1]/s_step))
chunksize = [csize]*(numblocks - 1) + [restc]
chunks = (N,csize)
# Convert blocksize and chunksize to NumPy arrays
blocksize = np.array(blocksize)
chunksize = np.array(chunksize)
# Get the number of elements that will be actually saved
n_elems = chunksize.sum()
# Create output HDF5 container
f = h5py.File(outfile)
# Create datasets for numeric variables
f.create_dataset('C',data=p_dict['C'],dtype=datype)
f.create_dataset('D',data=p_dict['D'],dtype=datype)
f.create_dataset('V',shape=(N,n_elems),chunks=chunks,dtype=datype)
f.create_dataset('Z',shape=(N,n_elems),chunks=chunks,dtype=datype)
f.create_dataset('DA',shape=(N,n_elems),chunks=chunks,dtype=datype)
f.create_dataset('QV',shape=(N,n_elems),chunks=chunks,dtype=datype)
f.create_dataset('Beta',shape=(N,n_elems),chunks=chunks,dtype=datype)
f.create_dataset('t',data=np.linspace(tstart,tend,n_elems),dtype=datype)
# Save parameters (but exclude stuff imported in the parameter file)
pg = f.create_group('params')
for key,value in p_dict.items():
valuetype = type(value).__name__
if valuetype != 'instance' and valuetype != 'module' and valuetype != 'function':
pg.create_dataset(key,data=value)
# Close container and write to disk
f.close()
# Initialize parameter C-class (struct) for the model
params = par(p_dict)
# Concatenate initial conditions for the "calibration" run
VZD0 = np.hstack([V0.squeeze(),Z0.squeeze(),DA0.squeeze()])
# Set up parameters for an initial `len_init` (in ms) long resting state simulation to "calibrate" the model
len_init = 100
dt = 0.1
s_step = 10
rmax = np.zeros((N,))
tinit = np.arange(0,len_init,dt)
tsize = tinit.size
csize = int(np.ceil(tsize/s_step))
# Update `p_dict` (we don't use it anymore, so just overwrite stuff)
p_dict['dt'] = dt
p_dict['s_step'] = s_step
p_dict['rmax'] = rmax
parinit = par(p_dict)
# Create a temporary container for the simulation
tmpname = tempfile.mktemp() + '.h5'
tmpfile = h5py.File(tmpname)
tmpfile.create_dataset('V',shape=(N,csize),dtype=datype)
tmpfile.create_dataset('Z',shape=(N,csize),dtype=datype)
tmpfile.create_dataset('DA',shape=(N,csize),dtype=datype)
tmpfile.create_dataset('QV',shape=(N,csize),dtype=datype)
tmpfile.create_dataset('Beta',shape=(N,csize),dtype=datype)
tmpfile.flush()
# Run 100ms of resting state to get model to a "steady state" for the initial conditions
solve_model(VZD0,tinit,parinit,np.array([tsize]),np.array([csize]),seed,0,str(tmpfile.filename))
# Use final values of `V`, `Z` and `DA` as initial conditions for the "real" simulation
V0 = tmpfile['V'][:,-1]
Z0 = tmpfile['Z'][:,-1]
DA0 = tmpfile['DA'][:,-1]
VZD0 = np.hstack([V0.squeeze(),Z0.squeeze(),DA0.squeeze()])
# Close and delete the temporary container
tmpfile.close()
os.remove(tmpname)
# Let the user know what's going to happen...
pstr = "--"
if len(kwargs) > 0:
pstr = str(kwargs.keys())
pstr = pstr.replace("[","")
pstr = pstr.replace("]","")
pstr = pstr.replace("'","")
table = Texttable()
table.set_deco(Texttable.HEADER)
table.set_cols_align(["l", "l"])
table.add_rows([["Simulating ",task.upper()],\
["#cycles: ",str(p_dict['n_cycles'])],\
["parameter file:",paramfile+".py"],\
["keyword args:",pstr],\
["matrix file:",mfile],\
["output:",outfile]])
if verbose: print "\n"+table.draw()+"\n"
# Finally... run the actual simulation
solve_model(VZD0,tsteps,params,blocksize,chunksize,seed,int(verbose),outfile)
# Done!
if verbose: print "\nDone\n"
##########################################################################################
def plot_sim(fname,names="all",raw=True,bold=False,figname=None):
"""
Plot a simulation generated by `run_model`
Parameters
----------
fname : str
File-name (including path if not in working directory) of HDF5 container that was generated
by `run_model`.
names : str or Python list/NumPy 1darray
Specify regions to plot. Either use the region's name as found in the `params` group of
the HDF5 container given by `fname` (e.g., `names='L_IFG'`) or its index in the `names` list
(e.g., `names = 3`). Use a list or NumPy 1darray to specify more than one region
(e.g., `names = ['L_IFG','R_IFG']` or `names = [3,15]`). By default, all regions are plotted.
raw : bool
If `True` then the raw model output will be plotted. Depending on the setting of `names` (see
above) the simulation length, and the model dimension (i.e., the number of modeled regions)
this may result in a very 'busy' plot.
bold : bool
If True then the previously converted simulated BOLD signals will be plotted (if no BOLD
signal is found in the input container specified by `fname`, an error is raised).
figname : str
String to be used as window title for generated figures.
Returns
-------
Nothing : None
Notes
-----
None
See also
--------
run_model : used to run a simulation
make_bold : convert raw simulation output to a BOLD signal
"""
# Make sure `fname` is a valid file-path
f = checkhdf(fname,peek=True)
# Make sure `raw` and `bold` are either `True` or `False`
if not isinstance(raw,bool):
raise TypeError("The flag `raw` has to be Boolean!")
if not isinstance(bold,bool):
raise TypeError("The flag `bold` has to be Boolean!")
# Try to access given HDF5 container
try:
rois_infile = f['params']['names'].value.tolist()
f.close()
except:
raise ValueError("HDF5 file "+fname+" does not have the required fields!")
# Check if list of ROI-names to plot was provided. If yes, make sure they make sense
if not isinstance(names,(str,unicode)):
doleg = True
try:
names = list(names)
except:
raise TypeError("Regions to plot have to be provided as Python list or NumPy 1darray!")
idx = []
for name in names:
try:
idx.append(rois_infile.index(name))
except:
raise ValueError("Region "+name+"not found in file!")
else:
if names == "all":
idx = range(len(rois_infile))
doleg = False
else:
try:
idx = rois_infile.index(names)
except:
raise ValueError("Region "+names+"not found in file!")
doleg = True
# Check if `figname` is actually printable
if figname != None:
if not isinstance(figname,(str,unicode)):
raise TypeError("Figure name has to be a string!"+type(figname).__name__+"!")
figname = str(figname)
# Fix sorting of idx so that smart indexing below works
if doleg:
idx = np.array(idx)
names = np.array(names)
sorted = idx.argsort()
names = names[sorted]
idx = idx[sorted]
# After all the error checking, reopen the file
f = h5py.File(fname,'r')
# Turn on interactive plotting
plt.ion()
# Plot raw model output
if (raw):
# Compute plotting step size s.t. we plot every 100ms (=10Hz) (if possible)
s_rate = f['params']['s_rate'].value
if s_rate > 10:
p_rate = int(s_rate//10)
else:
p_rate = s_rate
# Get quantities for plotting
t = f['t'][::p_rate]
V = f['V'][idx,::p_rate].T
QV = f['QV'][idx,::p_rate].T
Beta = f['Beta'][idx,::p_rate].T
DA = f['DA'][idx,::p_rate].T
tmin = t.min() - t[1]
tmax = t.max() + t[1]
# Prepare window and plot stuff
fig = plt.figure(figsize=(10,7.5))
if figname != None: fig.canvas.set_window_title(figname)
plt.suptitle("Raw Model Output from "+fname,fontsize=18)
sp = plt.subplot(4,1,1)
plt.plot(t,V)
if doleg:
plt.legend(names)
plt.ylabel('mV',fontsize=16)
[ymin,ymax] = sp.get_ylim()
plt.yticks([ymin,0,ymax],fontsize=10)
sp.set_xlim(left=tmin,right=tmax)
plt.xticks([],fontsize=8)
plt.title("V",fontsize=16)
plt.draw()
sp = plt.subplot(4,1,2)
plt.plot(t,QV)
plt.ylabel('Firing',fontsize=16)
[ymin,ymax] = sp.get_ylim()
plt.yticks([0,ymax],fontsize=10)
sp.set_xlim(left=tmin,right=tmax)
plt.xticks([],fontsize=8)
plt.title("QV",fontsize=16)
plt.draw()
sp = plt.subplot(4,1,3)
plt.plot(t,Beta)
plt.ylabel('Gain Factor',fontsize=16)
[ymin,ymax] = sp.get_ylim()
plt.yticks([ymin,ymax],fontsize=10)
sp.set_xlim(left=tmin,right=tmax)
plt.xticks([],fontsize=8)
plt.title(r"$\beta$",fontsize=16)
plt.draw()
sp = plt.subplot(4,1,4)
plt.plot(t,DA)
plt.ylabel('mM',fontsize=16)
[ymin,ymax] = sp.get_ylim()
plt.yticks([ymin,ymax],fontsize=10)
sp.set_xlim(left=tmin,right=tmax)
plt.xticks(fontsize=10)
plt.xlabel("ms")
plt.title("DA",fontsize=16)
plt.draw()
# Plot raw model output
if (bold):
# Try to load the BOLD data from file
try:
BOLD = f['BOLD'][idx,:].T
except:
f.close()
raise ValueError("No BOLD data found in file "+fname+"!")
# Get x-extent of data and create x-ticks vector
xmax = BOLD.shape[0] + 1
xtv = np.arange(-1,xmax)
# Prepare window and plot stuff
fig = plt.figure(figsize=(10,7.5))
if figname != None: fig.canvas.set_window_title(figname)
plt.title("BOLD data "+fname,fontsize=18)
sp = plt.subplot(111)
plt.plot(BOLD)
if doleg:
plt.legend(names)
plt.xticks(xtv,fontsize=10)
[ymin,ymax] = sp.get_ylim()
plt.yticks([ymin,0,ymax],fontsize=10)
# Close file and return
f.close()
##########################################################################################
def make_D(target,source,names,values=None):
"""
Create matrix of afferent/efferent dopamine regions in the model
Parameters
----------
target : Python list or NumPy 1darray
Python list or NumPy 1darray of region names that are affected by dopamine release
(has to be the same length as `source`).
source : Python list or NumPy 1darray
Python list or NumPy 1darray of region names that steer dopamine release.
(has to be the same length as `target`).
names : Python list or NumPy 1darray
Names of all regions in the model. If `names` has length `N`, the resulting
dopamine connection matrix will by `N`-by-`N`.
values : Python list or NumPy 1darray
By default, dopamine links are binary, i.e., all links have unit weight. By passing
a `values` array, certain links can be emphasized (weight > 1) or weakened (weight < 1).
Entries of the `values` array have to be calibrated based on the values of `b_hi`, `b_lo`,
and `a`.
Returns
-------
D : NumPy 2darray
A `N`-by-`N` array. Every row that has a non-zero entry signifies a dopamine target,
and every non-zero column corresponds to a dopamine source.
Notes
-----
None
Examples
--------
For the sake of simplicity consider a brain "parcellation" consisting of three (bilateral) regions,
called `A`, `B`, and `C`. Thus, we define the following `names` array
>>> names = ['L_A','L_B','L_C','R_A','R_B','R_C']
Assume that in the left hemisphere dopamine is mainly concentrated in region `B`, its release is
steered by neural firing in region `A`. In the right hemisphere, dopamine release in region `C` is
depending on neural activity in area `B`. Then the `target` and `source` arrays are given by
>>> target = ['L_B','R_C']
>>> source = ['L_A','R_B']
Then the call
>>> D = make_D(target,source,names)
>>> D
array([[ 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0.]])
generates a 6-by-6 NumPy array `D`, that has non-zero entries in rows 2 and 6 (because
'L_B' is the second, and 'R_C' the sixth element of the list `names`), and columns 1 and 5
(because 'L_A' is the first, and 'R_B' the 5th element of the list `names`). Thus, the row/column
index of each non-zero entry in `D` has the format target-by-source.
See also
--------
None
"""
# Sanity checks
vnames = ["target","source","names"]
for tk, tsn in enumerate([target,source,names]):
try:
tsn = list(tsn)
except:
msg = "Inputs target, source and names have to be NumPy 1darrays or Python lists, not "+\
type(tsn).__name__
TypeError(msg)
for el in tsn:
if not isinstance(el,(str,unicode)):
raise ValueError("All elements of `"+vnames[tk]+"` have to be strings!")
if len(source) != len(target):
raise ValueError("Length of source and target lists/arrays does not match up!")
for tk, tsn in enumerate([target,source]):
for el in tsn:
if el not in names:
raise ValueError("Element `"+el+"` not found in `"+vnames[tk]+"`!")
if values != None:
values = np.array(values)
arrcheck(values,'vector','values')
if len(values) != len(target):
raise ValueError("Length of `values` list/array does not match up!")
else:
values = np.ones((len(target),))
# Convert (if we're having a NumPy array) `names` to a Python list
names = list(names)
# Get dimension we're dealing with here
N = len(names)
# Create dopamine matrix
D = np.zeros((N,N))
# Fill the matrix
for i in xrange(len(source)):
# Get row and column indices
row = names.index(target[i])
col = names.index(source[i])
# Matrix is targets-by-sources
D[row,col] = values[i]
return D
##########################################################################################
def make_bold(fname, stim_onset=None):
"""
Convert raw model output to BOLD signal
Parameters
----------
fname : str
File-name (including path if not in working directory) of HDF5 container that was generated
by `run_model`.
stim_onset : float
Time (in seconds) of stimulus onset. By default, onset/offset timings of
stimuli are stored in the HDF5 container generated by `run_model`. Only override
this setting, if you know what you are doing.
Returns
-------
Nothing : None
The computed BOLD signal is stored as dataset `BOLD` at the top level of the HDF5 container
specified by `fname`.
Notes
-----
Regional neural voltages are converted to BOLD time-series using the linear hemodynamic response
function proposed by Glover [1]_. For details consult the supporting information of our
paper [2]_.
References
----------
.. [1] Glover G. Deconvolution of Impulse Response in Event-Related BOLD FMRI.
NeuroImage 9: 416-429, 1999.
.. [2] S. Fuertinger, J. C. Zinn, and K. Simonyan. A Neural Population Model Incorporating
Dopaminergic Neurotransmission during Complex Voluntary Behaviors. PLoS Computational Biology,
10(11), 2014.
"""
# Make sure container exists and is valid
f = checkhdf(fname,peek=True)
try:
V = f['V'].value
except:
f.close()
raise ValueError("HDF5 file "+fname+" does not have the required fields!")
# Compute cycle length based on the sampling rate used to generate the file
N = f['params']['names'].size
s_rate = f['params']['s_rate'].value
n_cycles = f['params']['n_cycles'].value
len_cycle = f['params']['len_cycle'].value
cycle_idx = int(np.round(s_rate*len_cycle))
# Make sure `stim_onset` makes sense
if stim_onset != None:
scalarcheck(stim_onset,'stim_onset',bounds=[0,len_cycle])
# Get task from file to start sub-sampling procedure
task = f['params']['task'].value
# Compute cycle length based on the sampling rate used to generate the file
N = f['params']['names'].size
n_cycles = f['params']['n_cycles'].value
len_cycle = f['params']['len_cycle'].value
cycle_idx = int(np.round(s_rate*len_cycle))
# Compute step size and (if not provided by the user) compute stimulus onset time
dt = 1/s_rate
if stim_onset == None:
stim_onset = f['params']['stimulus'].value
# Use Glover's Hemodynamic response function as convolution kernel (with default length 32)
hrft = utils.lambdify_t(hrf.glover(utils.T))
hrf_kernel = np.hstack((np.zeros((int(s_rate*stim_onset),)),hrft(np.arange(0,32,dt))))
# Convolve the de-meaned model time-series with the kernel
convV = ndimage.filters.convolve1d((V.T - V.mean(axis=1)).T,hrf_kernel,mode='constant')
# Allocate space for BOLD signal
BOLD = np.zeros((N,n_cycles))
# Sub-sample convoluted data depending on task to get BOLD signal
if task == 'speech':
# Get interval to be considered for boldification
start = int(np.round(f['params']['speechoff'].value*s_rate))
stop = start + int(np.round(f['params']['acquisition'].value*s_rate))
elif task == 'rest':
# Get interval to be considered for boldification
start = 0
stop = int(np.round(f['params']['stimulus'].value*s_rate))
else:
raise ValueError("Don't know what to do for task "+task)
# Compute BOLD signal for all time points
for j in xrange(n_cycles):
BOLD[:,j] = convV[:,start:stop].mean(axis=1)
start += cycle_idx
stop += cycle_idx
# Re-scale the the signal
BOLD = BOLD*0.02
# Save it to the file
try:
f.create_dataset('BOLD',data=BOLD)
except:
f['BOLD'].write_direct(BOLD)
f.close()
##########################################################################################
def show_params(fname):
"""
Pretty-print all parameters used in a simulation
Parameters
----------
fname : str
Filename (including path if not in working directory) of HDF5 container that was generated
by `run_model`.
Returns
-------
Nothing : None
Notes
-----
None
See also
--------
None
"""
# Make sure container exists and is valid
f = checkhdf(fname,peek=True)
try:
par_grp = f['params']
f.close()
except:
raise ValueError("HDF5 file "+fname+" does not have the required fields!")
# Create a list for Texttable
tlist = []
for key in f['params'].keys():
tlist.append([key,f['params'][key].value])
# Close file
f.close()
# Print table
print"\nShowing parameters of file "+fname
table = Texttable()
table.set_deco(Texttable.HEADER)
table.set_cols_align(["l", "l"])
table.add_rows([["Parameter","Value"]],header=True)
table.add_rows(tlist,header=False)
print "\n"+table.draw()+"\n"
##########################################################################################
def arrcheck(arr,kind,varname,bounds=None):
"""
Local helper function performing sanity checks on arrays (1d/2d/3d)
"""
if not isinstance(arr,np.ndarray):
raise TypeError('Input `'+varname+'` must be a NumPy array, not '+type(arr).__name__+'!')
sha = arr.shape
if kind == 'tensor':
if len(sha) != 3:
raise ValueError('Input `'+varname+'` must be a `N`-by-`N`-by-`k` NumPy array')
if (min(sha[0],sha[1])==1) or (sha[0]!=sha[1]):
raise ValueError('Input `'+varname+'` must be a `N`-by-`N`-by-`k` NumPy array!')
dim_msg = '`N`-by-`N`-by-`k`'
elif kind == 'matrix':
if len(sha) != 2:
raise ValueError('Input `'+varname+'` must be a `N`-by-`N` NumPy array')
if (min(sha)==1) or (sha[0]!=sha[1]):
raise ValueError('Input `'+varname+'` must be a `N`-by-`N` NumPy array!')
dim_msg = '`N`-by-`N`'
elif kind == 'vector':
sha = arr.squeeze().shape
if len(sha) != 1:
raise ValueError('Input `'+varname+'` must be a NumPy 1darray')
if sha[0] <= 1:
raise ValueError('Input `'+varname+'` must be a NumPy 1darray of length `N`!')
dim_msg = ''