-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.py
More file actions
1516 lines (1195 loc) · 61.2 KB
/
Copy pathSimulation.py
File metadata and controls
1516 lines (1195 loc) · 61.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
"""
This file contains all the required functions necessary for the running of a simulation.
The parameters file, as specified in the readme contains A list of parameters corresponding to the initial weights of the synaptic
connections (including lower and upper neuroplastic bounds)
Biophysical bladder model adapted from Lister et al. 2024 (DOI: 10.1101/2024.11.21.624716)
"""
# First import required packages
# Base
from brian2 import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from itertools import cycle
from tqdm import tqdm
from scipy import optimize
import sys
# External data (Input/Output)
from scipy import io
# Data Smoothing
from scipy.signal import butter, sosfilt
from scipy.interpolate import interp1d
# Plotting
from matplotlib.lines import Line2D
# Computational efficiency
prefs.codegen.target = 'numpy'
# Define a class to handle simulation progress reporting
class ProgressBar(object):
def __init__(self, toolbar_width=40):
self.toolbar_width = toolbar_width
self.ticks = 0
def __call__(self, elapsed, complete, start, duration):
if complete == 0.0:
# setup toolbar
sys.stdout.write("[%s]" % (" " * self.toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (self.toolbar_width + 1)) # return to start of line, after '['
else:
ticks_needed = int(round(complete * self.toolbar_width))
if self.ticks < ticks_needed:
sys.stdout.write("-" * (ticks_needed-self.ticks))
sys.stdout.flush()
self.ticks = ticks_needed
if complete == 1.0:
sys.stdout.write("\n")
# Define Biophysical Bladder Model
class LUT:
def __init__(self):
self.V_B = 0
self.f_aD_s = 0
self.f_aS_s = 0
self.r_U = 0
self.Q = 0
self.Q_in = 0
self.p_D = 0
self.p_S = 0
self.voiding = False
self.w_s_s = 0
self.w_i_s = 0
self.w_e_s = 0
self.u_D = 0
self.bladder_args = {
# Constants
'alpha' : 2,
'A_BN' : 7.07 * 10 ** -4,
'A_C' : 8.0 * 10 ** -5,
'A_muscleS' : 8.0 * 10 ** -6,
'A_nomD' : 2.78 * 10 ** -4,
'A_tissueS' : 4.0 * 10 ** -5,
'C_l' : 5.0 * 10 ** 3,
'C_u' : 2.0 * 10 ** 2,
'C_Qin' : 5.0 * 10 ** -8,
'C_p' : 1.5,
'dr' : 1.0 * 10 ** -4,
'h_D' : 8.19 * 10 ** -4,
'h_nomS' : 2.65 * 10 ** -4,
'k' : 0.3,
'l_optD' : 4.68 * 10 ** -6,
'l_optS' : 2.23 * 10 ** -6,
'p_0S' : 100,
'p_nomS' : 6.0 * 10 ** 3,
'p_theta' : 3.0 * 10 ** 3,
'rho' : 1.0 * 10 ** 3,
'r_BN' : 1.5 * 10 ** -2,
'r_optD' : 5.4 * 10 ** -2,
'r_optS' : 4.8 * 10 ** -3,
'r_0D' : 2.7 * 10 ** -2,
'r_0S' : 4.8 * 10 ** -3,
'R_1' : 3.0 * 10 ** 8,
'R_2' : 2.4 * 10 ** 8,
'sigma_isoD' : 4.0 * 10 ** 5,
'sigma_isoS' : 2.0 * 10 ** 5,
'tau_D' : 1.0,
'tau_S' : 0.2,
'u_maxD' : 0.2,
'u_maxS' : 1.0,
'V_muscleD' : 3.0 * 10 ** -5,
'V_tissueD' : 2.0 * 10 ** -5,
'tissue_pressed_in_BN' : False
}
self.bladder_args['max_V_B'] = 5 * 10 ** -4
self.bladder_args['voiding_threshold'] = 1 * self.bladder_args['max_V_B']
self.bladder_args['neuron_threshold'] = 0.50*self.bladder_args['voiding_threshold']
self.bladder_args['filling_phase_I'] = 0.04 * self.bladder_args['max_V_B']
self.bladder_args['filling_phase_II'] = 0.75 * self.bladder_args['max_V_B']
self.bladder_args['filling_phase_III'] = 0.9 * self.bladder_args['max_V_B']
self.n = 1
self.t = 0
self.inputvals = [] #Placeholder, external values I'm going to play with
def get_p_S(self, A_U, f_aS_s, r_U):
r_outS = ((1 / np.pi) * (A_U + self.bladder_args['A_tissueS'] + self.bladder_args['A_muscleS']) ) ** (1/2) # Eq. (B.10)
r_inS = ((1 / np.pi) * (A_U + self.bladder_args['A_tissueS']) ) ** (1/2) # Eq. (B.11)
r_S = (r_outS + r_inS) / 2 # Eq. (B.9)
h_S = r_outS - r_inS # Mentioned below Eq. (7)
dru = (r_U - self.r_U) / self.bladder_args['dT'] # Eq. (B.49)
u_S = - r_U / (2 * self.bladder_args['r_optS']) * (1 / r_outS + 1 / r_inS) * dru # Eq. (B.50)
u_S_s = u_S / self.bladder_args['u_maxS']
l_S = (self.bladder_args['l_optS'] / self.bladder_args['r_optS']) * r_S # Eq. (B.12)
sigma_nom_actS = f_aS_s * self.bladder_args['sigma_isoS'] * self.sigma_u_s(u_S_s) * self.sigma_lS_s(l_S) # Eq. (7) removed passive component
sigma_actS = (self.bladder_args['h_nomS'] / h_S) * sigma_nom_actS # Eq. (8) removed passive component
p_actS = sigma_actS * np.log(r_outS / r_inS) # Eq. (11)
p_pasS = self.bladder_args['p_0S'] * (self.bladder_args['p_nomS'] / self.bladder_args['p_0S']) ** (r_U / self.bladder_args['r_0S']) # Eq. (B.33)
p_S = p_actS + p_pasS # Eq. (12)
return p_S
def get_Q2(self, A_U, A_T, p_S):
RA2 = self.bladder_args['R_1'] * A_U + (self.bladder_args['R_2'] / self.bladder_args['A_C']) * (A_U ** 2) # Eq. (B.48)
Q2 = (p_S * A_U ** 2) / ((self.bladder_args['rho'] / 2) * (1 - (A_U ** 2 / A_T ** 2)) + RA2) # Eq. (B.52)
return Q2
def get_Qin(self, t):
"""
This function generates a stochastic inflow to the bladder.
"""
# Stochastic inflow
## Parameters
a = 0.025
b = 2 * np.pi / 24
c = 0.05
p = 1/(60*60)
o = 8 * 2 * np.pi / 24 * 1/p
Q = a * np.sin(p * (b * t - o)) + c
# Noise
## Noise is randomised every minute of operation to prevent overly smooth noise when dt is small
if t % 60 == 0:
self.n = np.random.uniform(-1, 1)
Q = (Q * 10 ** -6) # Scale inflow from ml to m^3
Q += self.n * self.bladder_args['C_Qin']
return Q
def f_0(self, V_B, f_aD_s, f_aS_s, r_U):
A_U = np.pi * r_U ** 2 # Eq. (B.8)
A_T = np.pi * (r_U + self.bladder_args['dr']) ** 2 # Eq. (B.15)
A_BN = np.pi * self.bladder_args['r_BN'] ** 2 # Eq. (B.14)
r_inD = ((3 / (4 * np.pi)) * (V_B + self.bladder_args['V_tissueD']) ) ** (1/3) # Eq. (B.4)
A_inD = 4 * np.pi * r_inD ** 2 # Eq. (B.39)
p_S = self.get_p_S(A_U, f_aS_s, r_U)
Q2 = self.get_Q2(A_U, A_T, p_S)
p_D = self.get_p_D(V_B, f_aD_s, np.sqrt(Q2))
p_BN = p_D - ((self.bladder_args['rho'] * Q2) / 2) * (1 / A_BN ** 2 - 1 / A_inD ** 2) # Eq. (B.44)
r_B = (3 * V_B / (4 * np.pi)) ** (1/3)
A_B = 4 * np.pi * r_B ** 2 # Mentioned below Eq. (B.43)
dp = self.bladder_args['C_p'] * p_BN * ((A_BN - A_B) / A_BN) ** 2 if self.bladder_args['tissue_pressed_in_BN'] else 0 # Eq. (B.43)
p_T = p_D - (self.bladder_args['rho'] * Q2) / (2 * A_T ** 2) - dp # Eq. (B.42)
return p_T - p_S # Eq. (B.53)
def f1(self, V_B, f_aD_s, Q):
self.p_D = self.get_p_D(V_B, f_aD_s, Q)
return self.Q_in - Q # Eq. (3)
def f2(self, f_aD_s, w_e_s, w_i_s):
return 1 / self.bladder_args['tau_D'] * (w_e_s- f_aD_s - w_i_s * f_aD_s) # df_aD_s - Eq. (1)
def f3(self, f_aS_s, w_s_s):
return (1 / self.bladder_args['tau_S']) * (w_s_s - f_aS_s) # df_aS_s - Eq. (2)
def fmap(self, V_B, f_aD_s, f_aS_s):
try:
r_U = optimize.bisect(lambda r_U: self.f_0(V_B, f_aD_s, f_aS_s, r_U), 0, 5 * 10 ** -3)
except ValueError:
r_U = 0
return r_U
def sigma_u_s(self, u_s):
if u_s < 0:
sigma = 1.8 - (0.8 * (1 + u_s))/(1 - 7.56 * u_s / self.bladder_args['k'])
elif u_s == 0:
sigma = 1
else:
sigma = (1 - u_s)/(1 + (u_s / self.bladder_args['k']))
return sigma
def sigma_upasD(self, u_D_s):
return self.bladder_args['C_u'] * u_D_s # Eq. (B.34)
def sigma_lD_s(self, l_D):
l_D_s = l_D / self.bladder_args['l_optD']
if l_D_s <= 0.35:
sigma = 0
elif l_D_s <= 0.45:
sigma = 5.5 * l_D_s - 1.925
elif l_D_s <= 1.1:
sigma = 0.643 * l_D_s + 0.293
elif l_D_s <= 1.4:
sigma = -3.33333 * l_D_s + 4.66667
else:
sigma = 0
return sigma
def line_eq(self, x1, y1, x2, y2):
"Find equation of line between two (x,y) points"
m = (y2 - y1) / (x2 - x1)
c = y1 - m * x1
return m, c
def sigma_lS_s(self, l_S):
l_S_s = l_S / self.bladder_args['l_optS']
lower_l_S, upper_l_S = 1.73 * 10 ** -6, 2.89 * 10 ** -6
lower_l_S_s, upper_l_S_s = lower_l_S / self.bladder_args['l_optS'], upper_l_S / self.bladder_args['l_optS']
points = [(0.55, 0), (lower_l_S_s, 0.8), (1, 1), (1.1, 1), (1.75, 0)]
for i in range(len(points) - 1):
if l_S_s >= points[i][0] and l_S_s < points[i + 1][0]:
m, c = self.line_eq(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1])
sigma = m * l_S_s + c
return sigma
return 0
def get_p_D(self, V_B, f_aD_s, Q):
r_outD = ((3 / (4 * np.pi)) * (V_B + self.bladder_args['V_tissueD'] + self.bladder_args['V_muscleD']) ) ** (1/3) # Eq. (B.3)
r_inD = ((3 / (4 * np.pi)) * (V_B + self.bladder_args['V_tissueD']) ) ** (1/3) # Eq. (B.4)
r_D = (r_outD + r_inD) / 2 # Eq. (B.2)
u_D = (Q / (8 * np.pi * self.bladder_args['r_optD'])) * (1 / r_outD ** 2 + 1 / r_inD ** 2) # Eq. (B.22), assume Q_in = 0 as mentioned in B.37
self.u_D = u_D #Save this so I can analyse it.
u_D_s = u_D / self.bladder_args['u_maxD'] # Mentioned in paragraph above B.3.3
sigma_uD_s = self.sigma_u_s(u_D_s) # Eq. (B.31)
l_D = (self.bladder_args['l_optD'] / self.bladder_args['r_optD']) * r_D # Eq. (B.5)
sigma_lpasD = 0 if r_D < self.bladder_args['r_0D'] else self.bladder_args['C_l'] * ((r_D - self.bladder_args['r_0D']) / self.bladder_args['r_0D']) ** self.bladder_args['alpha'] # Eq. (B.32)
sigma_nomD = f_aD_s * self.bladder_args['sigma_isoD'] * sigma_uD_s * self.sigma_lD_s(l_D) + sigma_lpasD + self.sigma_upasD(u_D_s) # Eq. (4). Active + passive elastic + passive viscoelastic tensile stress
A_D = np.pi * (r_outD ** 2 - r_inD ** 2) # Eq. (5)
sigma_D = (self.bladder_args['A_nomD'] / A_D) * sigma_nomD # Eq. (6)
p_D = sigma_D * np.log(r_outD / r_inD) # Eq. (10)
if p_D < 0:
p_D = 0
return p_D
def get_Q(self, f_aS_s, r_U):
A_T = np.pi * (r_U + self.bladder_args['dr']) ** 2 # Eq. (B.15)
A_U = np.pi * r_U ** 2 # Eq. (B.8)
p_S = self.get_p_S(A_U, f_aS_s, r_U)
self.p_S = p_S
Q2 = self.get_Q2(A_U, A_T, p_S)
return np.sqrt(Q2)
#Function that updates hypogastric input - aka w_i_s
#Here modified to allow integration with neural sim.
def update_sympathetic_input(self):
w=self.w_i_s
return w
#Function that updates pelvic input - aka w_e_s
def update_parasympathetic_input(self):
w=self.w_e_s
return w
#Function that updates pudendal input - aka w_s_s
def update_somatic_input(self):
w=self.w_s_s
return w
#Function that determines if voiding state is true/false
def is_voiding(self):
if self.voiding and self.V_B < 1 * 10 ** -8:
return False # Reset voiding if bladder is empty
elif self.p_D >= self.bladder_args['p_theta']:
return True # If pressure exceeds threshold, voiding
else:
return self.voiding # Otherwise, maintain voiding state
#Main function, takes paramters for bladder input, and outputs biophysical parameters
def process_neural_input(self, w_e_s, w_i_s, w_s_s, maxTime, dT):
self.Q_in = self.bladder_args['C_Qin']
self.w_e_s, self.w_i_s, self.w_s_s = w_e_s, w_i_s, w_s_s
datadict = [{'V_B': self.V_B, 'f_aD_s': self.f_aD_s, 'f_aS_s': self.f_aS_s, 'r_U': self.r_U, 'Q': self.Q, 'p_D': self.p_D, 'p_S': self.p_S, 'Q_in': self.Q_in}]
self.bladder_args['dT'] = dT
ts = np.arange(0, maxTime, dT)
for stamp, t in enumerate(ts):
self.timestamp = stamp #the step of the simulation (for indexing external data)
self.t = t #the actual time of the simulation
self.voiding = self.is_voiding()
d_w_e_s = self.update_parasympathetic_input()
d_w_i_s = self.update_sympathetic_input()
d_w_s_s = self.update_somatic_input()
self.w_e_s = d_w_e_s
self.w_i_s = d_w_i_s
self.w_s_s = d_w_s_s
self.f_aD_s += dT * self.f2(self.f_aD_s, self.w_e_s, self.w_i_s)
self.f_aS_s += dT * self.f3(self.f_aS_s, self.w_s_s)
self.Q_in = self.get_Qin(t)
self.V_B += dT * self.f1(self.V_B, self.f_aD_s, self.Q)
self.V_B = min(max(self.V_B, 0), 5 * 10 ** -4)
self.r_U = self.fmap(self.V_B, self.f_aD_s, self.f_aS_s)
self.Q = self.get_Q(self.f_aS_s, self.r_U)
datadict.append({'t': t, 'V_B': self.V_B, 'f_aD_s': self.f_aD_s, 'f_aS_s': self.f_aS_s, 'r_U': self.r_U, 'Q': self.Q, 'p_D': self.p_D, 'p_S': self.p_S, 'Q_in': self.Q_in, 'w_e_s': self.w_e_s, 'w_i_s': self.w_i_s, 'w_s_s': self.w_s_s, 'voiding': self.voiding})
df = pd.DataFrame(datadict)
return df
#-----------------------------------------------------------
"""
Now shall define the main simulation function
"""
#-----------------------------------------------------------
def bladder_sim(
time,
tibial_parameters,
parameters,
pag = True,
pmc = True,
spine_aff = True,
random_gen = False,
seed = 1
):
"""
This is the main function to be called when running a simulation of the bladder and neural network.
Arguments:
------------
time (float): The duration (in seconds) of bladder and circuit behaviour that should be produced.
tibial_parameters (list): The tibial nerve stimulation that is to be applied to the system, formatted as [frequency(Hz), duration(s)] e.g. [1, 1000]
parameters (dataframe): The parameters specified in the readme, dictating the weights of the synaptic connections within the model. Imported from params.py
"from params import parameters".
pag (bool, default True): If tibial nerve projections to the Periaquaductal Grey (PAG) should be maintained (True/False)
pmc (bool, default True): If tibial nerve projections to the Pontine Micturition Centre (PMC) should be maintained (True/False)
spine_aff (bool, default True): If tibial nerve projections to spinal afferents should be maintained (True/False)
random_gen (bool, default False): If the simulation should be provided a seed (i.e., returning the same results for any noise calculation).
NOTE: If this is set to true and supplied a seed the simulation will act in a non-random manner!
rand_seed (int, default 1): Seed for any random noise calculation
Returns:
-------------
results (list): A list of key results from the simulation, formatted as so:
+-------------------------------+------------------------------------------------------------------------------------------------------------+
| Variable | Definition |
+-------------------------------+------------------------------------------------------------------------------------------------------------+
| pressure | Bladder pressure (cmh20) for the simulation run (array) |
| volume | Bladder volume (m3) for the simulation run (array) |
| bladder_pressure_mon.t/second | Timestamps associated with the bladder pressure array (for plotting as seconds without needing to convert) |
| pel_aff | Firing rate of pelvic afferents over the course of the simulation (hz) |
| pel_out | Firing rate of pelvic efferents over the course of the simulation (hz) |
| hyp_out | Firing rate of hypogastric efferents over the course of the simulation (hz) |
| pud_out | Firing rate of pudendal efferents over the course of the simulation (hz) |
| asc | Firing rate of second order spinal afferents over the course of the simulation (hz) |
| asc_timestamps | Timestamps for the second order spinal afferents to allow plotting without unit conversion |
| tib | Firing rate of the tibial nerve projections (hz) |
| pel_spikes | Timestamps for individual spikes from one pelvic efferent |
| hyp_spikes | Timestamps for individual spikes from one hypogastric efferent |
| pud_spikes | Timestamps for individual spikes from one pudendal efferent |
| des_mon | Firing rate for primary brainstem output (hz) |
| des_spikes | Timestamps for individual spikes from one primary brainstem efferent |
| bladder_secondary_state_mon | All biophysical information recorded from the bladder during the simulation |
+-------------------------------+------------------------------------------------------------------------------------------------------------+
"""
###########Neuronal Network Definition#################
# Initiate neuronal network related variables
# Import weights from before
runtime=time
params = parameters["parameters"] #Model weights (externally supplied)
min_wgt = parameters["min_weights"] #Lower bound of the synaptic weights
max_wgt = parameters["max_weights"] #Upper bound of the synaptic weights
cores = 1 #Number of cores to use when running simulation (Defaults to one unless otherwise specified)
tibial_params = tibial_parameters #tibial stimulation (rate, duration)
monitors = None #Variable to hold sim monitors after network construction.
results = None #Hold results of simulation
global model
model=LUT() #define outside of function, to store variables
# Define function to smooth firing rate
def fr_smooth(monitor, crit_freq, sampling_freq):
"""
Function takes raw firing rate data from a PopulationFiringRate class (Brian2)
and filters/smooths it to return a continuous average firing rate for the population.
Function uses a 1st order Butterworth filter to smooth the data.
Arguments:
-----------
monitor : PopulationRateMonitor object,
The monitor containing the raw instantaneous firing rates recorded from the target population.
NOTE: Must be supplied AFTER running a simulation.
crit_freq : float,
The desired cutoff frequency for the filter.
sampling_frequ : float,
The sampling frequency of the monitor data (Usuall default clock time for simulation).
"""
# Import monitor and extract raw data
dat = monitor.get_states()["rate"]
# Define filter numerator and denomenator
sos = butter(1, Wn=crit_freq, fs=sampling_freq, output="sos", analog=False)
# Apply the filter to the data
return sosfilt(sos, dat)
###################################Experimental Parameters###################################
# Build the device preferences, specifying any multicore processing required
# Will run the simulation in standalone mode.
device.reinit()
device.activate()
# Define default clock cycle (50Hz, or 20ms per step)
defaultclock.dt = 20*ms
if random_gen is True:
# Specify seed for pseudorandom number generation (For testing/plotting only, disable this if running anlysis!)
device.seed(seed=rand_seed) #Keep for reproduceable raster images!
# Specify experimental parameters
update_time = 20 #The rate at which the timedarrays shall be updated (Hz)
# Tibial connectivity
PMC_connected = pmc #Does the TN connect to PMC
PAG_connected = pag #Does the TN connect to the PAG
Spine_connected = spine_aff #Does the TN connect to the spine?
# Tibial Strength.
tib_weight = [2, 4] #Weight of tibial connections (spine, brain), fit from data.
tib_rate = tibial_params[0] #frequency of tibial input (Hz)
tib_dur = tibial_params[1] #duration of tibial input (s)
###################################Define Model Parameters###################################
# Dynamics of a basic adaptive firing neuron
# Membrane dynamics determined by Gorski et al.
Cm = 200*pF #Membrane capacitance, relatively similar across neurons
E_A = -70*mV #Reversal potential of adaption conductance (outward current that drives hyperpolarisation)
E_L = -60*mV #Resting potential (reversal potential of leak current)
E_L_tonic = -70*mV
g_L = 10*nS #Resting conductance
v_reset = -55*mV #Post-spike reset potential
v_th = -50*mV #Spike threshold
delta_t = 10*mV #Slope of the spike initiation (i.e, depolarisation)
delta_t_ref = 5*ms #refractory period
# Subthreshold adaption parameters
v_A = -50*mV #Activation threshold for sub-spike adaption (drives changing firing rate)
v_A_tonic = -45*mV
delta_A = 5*mV #Slope of subthreshold activation (severity of spike rate adaption)
delta_g_A = 1*nS #Stepwise increase in adaption current (set to zero for non-adaption)
delta_g_A_tonic = 0*nS
g_A_max = params[45]*nS
g_A_max_tonic = 2*nS
tau_A = 200*msecond #Decay rate for adaption current
tau_A_tonic = 40*msecond
# Tonic firing parameter (causes self sustained firing)
Iap = params[44]*pA
# Synaptic parameters
E_ex = 0*mV #Reversal potential of excitatory current
E_in = -80*mV #Reversal potential of inhibitory current
# Opioidergic parameters (To be fit!)
E_op = -80*mV #Reversal potential of enkephalinergic current
tau_op = 10*ms #Rate of enkephalinergic conductance decay, short term lingering inhibition.
g_op_increment = 1.5*nS #Baseline conductance of enkephalinergic synapse
##Learning parameters, determined by Vogels et. al., 2011
# First decay constants (For conductance)
tau_glut = 5*ms #Excitatory rate of conductance decay (Glutamatergic transmission)
tau_gaba = 10*ms #Inhibitory rate of conductance decay (GABAergic transmission)
tau_stdp = 20*ms #Rate of memory trace decay
# Then weight increments (To be fit)
g_ex_increment = params[42]*nS #Baseline excitatory conductance
g_in_increment = params[43]*nS #Baseline inhibitory conductance
# Max weights
ex_w_max = 100 # Max excitatory weight
in_w_max = 100 #Max inhibitory weigth
# Learning rates and depression parameters
learn_rate = 5*10**-3 #Learning rate of synapse
p_o = 1*Hz #Target postsynaptic firing rate
dep_fac = 2*tau_stdp*p_o #Rate of synaptic depression
# Population parameter
pop = 100 #Number of neurons in each unit, NOTE: Be careful when editing this, it is memory intensive!
# Initial bladder simulation parameters
global w_e_s
global w_i_s
global w_s_s
w_e_s = 0
w_i_s = 0
w_s_s = 0
############################################################################################
# Define Model Equations
# First, set equation for tonically active (nonadaptive) neurons within the circuit
# These neurons possess both an external current (Iap) and synaptic input (Is) for modulation
# Iap is presumed to be constant
tonic_eqs = """
dv/dt = (g_L*(E_L_tonic - v) + g_L*delta_t*exp((v - v_th)/delta_t) + g_A*(E_A - v) + Iap + Is) / Cm : volt (unless refractory)
dg_A/dt = ((g_A_max_tonic / (1 + exp((v_A_tonic - v)/delta_A))) - g_A) / tau_A_tonic : siemens
Is = g_ex*(E_ex - v) + g_in*(E_in - v) + g_op*(E_op - v): amp
dg_ex/dt = -g_ex / tau_glut : siemens
dg_in/dt = -g_in / tau_gaba : siemens
dg_op/dt = -g_op / tau_op : siemens
"""
# Then, define equation for normal adaptive neurons
# NOTE: the rate of adaption is determined by delta_g_A (the change in adaption with each spike)
adapt_eqs = """
dv/dt = (g_L*(E_L - v) + g_L*delta_t*exp((v - v_th)/delta_t) + g_A*(E_A - v) + Is) / Cm : volt (unless refractory)
dg_A/dt = ((g_A_max / (1 + exp((v_A - v)/delta_A))) - g_A) / tau_A : siemens
Is = g_ex*(E_ex - v) + g_in*(E_in - v) + g_op*(E_op - v): amp
dg_ex/dt = -g_ex / tau_glut : siemens
dg_in/dt = -g_in / tau_gaba : siemens
dg_op/dt = -g_op / tau_op : siemens
"""
#####################################Synaptic Equations###################################
"""
NOTE: Must clip weight within a range of 0 <= W <= ex_w_max
NOTE: Weight change however, has no limit!
"""
# Define effects of subthreshold adaption
# Delta_g_A determines rate of adaption
on_spike = """
v = v_reset
g_A += delta_g_A
"""
on_spike_tonic = """
v = v_reset
g_A += delta_g_A_tonic
"""
# Define synaptic model to allow for learning
# Importantly, this constrains the weight of each connection to a range specified when the sim is built.
# NOTE: a_pr & a_pst are synaptic traces (memory)
# NOTE: traces updated only on spiking, to save memory
syn_model = """
max_weight : 1
min_weight : 1
weight : 1
da_pr/dt = -a_pr/tau_stdp : 1 (event-driven)
da_pst/dt = -a_pst/tau_stdp : 1 (event-driven)
"""
# Define effects of presynaptic spiking
# First for excitatory synapses
on_pre_ex = """
a_pr += 1 #Increase presynaptic memory trace
weight = clip(weight + (learn_rate * (a_pst - dep_fac)), min_weight, max_weight) #Increment the weight based on postsynaptic activity
g_ex += g_ex_increment * weight #Increase conductance representing transmission
"""
# Then for inhibitory
on_pre_in = """
a_pr += 1 #Increase presynaptic memory trace
weight = clip(weight + (learn_rate * (a_pst - dep_fac)), min_weight, max_weight) #Increment the weight based on postsynaptic activity
g_in += g_in_increment * weight #Increase conductance representing transmission
"""
# Define effects of postsynaptic spiking
# First for excitatory synapses
on_post_ex = """
a_pst += 1 #Increase postsynaptic memory trace
weight = clip(weight + (learn_rate * a_pr), min_weight, max_weight) #Increment weight based on presynaptic activity
"""
# Then inhibitory
on_post_in = """
a_pst += 1 #Increase postsynaptic memory trace
weight = clip(weight + (learn_rate * a_pr), min_weight, max_weight) #Increment weight based on presynaptic activity
"""
# Finally, set up new equations for opioidergic transmission (inhibitory)
on_pre_op="""
a_pr += 1 #Increase memory trace (STDP)
weight = clip(weight + (learn_rate * (a_pst - dep_fac)), min_weight, max_weight)
g_op += g_op_increment * weight
"""
on_post_op="""
a_pst += 1
weight = clip(weight + (learn_rate * a_pr), min_weight, max_weight)
"""
##################################BLADDER#################################################
# First, led the bladder be defined as a single neuronal unit with no set equation
# Instead with a series of dimensionless parameters:
# Pressure (Pb)
# Volume (V)
######Define Bladder Function###########
bladder_equations = """
pressure : 1
volume : 1
f_aD_s_val : 1
f_aS_s_val : 1
r_U_val : 1
Q_val : 1
p_S_val : 1
Q_in_val : 1
w_e_s_val : 1
w_i_s_val : 1
w_s_s_val : 1
voiding_state : 1
u_D_val : 1
"""
global bladder
bladder = NeuronGroup(1, model=bladder_equations)
global bladder_pressure_mon
bladder_pressure_mon = StateMonitor(bladder, "pressure", record=True, when="end")
global bladder_volume_mon
bladder_volume_mon = StateMonitor(bladder, "volume", record=True, when="end")
global bladder_secondary_state_mon
bladder_secondary_state_mon = StateMonitor(
bladder,
[
"f_aD_s_val",
"f_aS_s_val",
"r_U_val",
"Q_val",
"p_S_val",
"Q_in_val",
"w_e_s_val",
"w_i_s_val",
"w_s_s_val",
"voiding_state",
"u_D_val"
],
record=True,
when="end",
)
@network_operation(when="start")
def bladder_model_run():
"""
This function runs the bladder sim calculation at each timestep.
Updates the model object, which allows other functions to take use of it.
"""
global w_e_s
global w_i_s
global w_s_s
global model
global bladder
#Run bladder model and extract pressure/volume
model_state=model.process_neural_input(w_e_s, w_i_s, w_s_s, 0.2, 0.2)
volume_state=model_state["V_B"].tail(1)
pressure_state = model_state["p_D"].tail(1)
#Update bladder states at each timestep
bladder.pressure[:] = pressure_state[1]
bladder.volume[:] = volume_state[1]
#Other less important parameters
bladder.f_aD_s_val[:] = model_state["f_aD_s"].tail(1)[1]
bladder.f_aS_s_val[:] = model_state["f_aS_s"].tail(1)[1]
bladder.r_U_val[:] = model_state["r_U"].tail(1)[1]
bladder.Q_val[:] = model_state["Q"].tail(1)[1]
bladder.p_S_val[:] = model_state["p_S"].tail(1)[1]
bladder.Q_in_val[:] = model_state["Q_in"].tail(1)[1]
bladder.w_e_s_val[:] = model_state["w_e_s"].tail(1)[1]
bladder.w_i_s_val[:] = model_state["w_i_s"].tail(1)[1]
bladder.w_s_s_val[:] = model_state["w_s_s"].tail(1)[1]
bladder.voiding_state[:] = model_state["voiding"].tail(1)[1]
bladder.u_D_val[:] = model.u_D
# -------------------Calculate Afferent Parameters---------------------
# Generate a function, that calculates the afferent rate based on current bladder pressure.
afferent_rate = [0]
@network_operation(when="start") #Run before all other calculations performed.
def afferent_rate_calculation():
#Get value of bladder pressure for time t
global bladder_pressure_mon
global afferent_rate
#Set to zero for first timestep
if bladder_pressure_mon.pressure.size == 0:
current_pressure=0
else:
current_pressure = bladder_pressure_mon.pressure[0][-1]
#Calculate the rate for a given bladder pressure From McGee et al.
afferent_rate = (-3*10**-8)*current_pressure**5 + (1*10**-5)*current_pressure**4 - (1.5*10**-3)*current_pressure**3 + (7.9*10**-2)*current_pressure**2 - 0.6*current_pressure
# Calculate desired tibial nerve input
# Create array of zeroes length of bladder data
tib_dat = np.zeros(runtime)
# Replace N values with rate
num_cycles = int(tib_dur/(update_time/1000)) #convert from seconds to clock cycles (divide by s)
tib_dat[0:num_cycles] = tib_rate
# Convert to brian-readable timed array.
in_input = TimedArray(tib_dat*Hz, dt=update_time*ms)
# -----------------------------Define Bladder and Afferents------------------
# Define the internal urethral sphincter
global ius
ius = PoissonGroup(1, rates=0*Hz, name="IUS")
ius_mon = PopulationRateMonitor(ius)
global eus
eus = PoissonGroup(1, rates=0*Hz, name="EUS")
eus_mon = PopulationRateMonitor(eus)
@network_operation
def update_ius(when="start"):
#Set the rate of the afferent to
global bladder_secondary_state_mon
global ius
if bladder_secondary_state_mon.f_aS_s_val.size == 0:
ius.rates=0*Hz
else:
ius.rates=bladder_secondary_state_mon.f_aS_s_val[0][-1]*Hz
@network_operation
def update_eus(when="start"):
#Set the rate of the afferent
global bladder_secondary_state_mon
global eus
if bladder_secondary_state_mon.f_aS_s_val.size == 0:
eus.rates=0*Hz
else:
eus.rates=bladder_secondary_state_mon.f_aS_s_val[0][-1]*Hz
##################################Neuronal Group Definition#####################################
# Tibial Nerve
# -----------------------------------------------------------------------
tibial_nerve = PoissonGroup(pop, rates="in_input(t)", name="TibialNerve")
# Spinal Interneuron (GABAergic)
tibial_classic = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
tibial_classic.v = "E_L + rand()*(v_th - E_L)"
# Ascending Opioidergic
tibial_op = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
tibial_op.v = "E_L + rand()*(v_th - E_L)"
# Poisson Neurons
# -----------------------------------------------------------------------
# The Pelvic Nerve (NOTE: This is a poisson group whose firing rate is determined by Pb!)
# Updates its firing rate with each timestep (starts at zero Hz at t=0)
global pelvic_aff
pelvic_aff = PoissonGroup(pop, rates=0*Hz, name="PelvicAfferent")
# -----------------------------------------------------------------------
# Define a function to update the pelvic afferent firing rate for each step of the simulation
@network_operation
def update_afferent(when="start"):
#Set the rate of the afferent to
global pelvic_aff
global afferent_rate
pelvic_aff.rates=afferent_rate*Hz
##Brainstem Neurons##
# First, Independent tonic neurons
tonic_ind_pop = pop
tonic_ind = NeuronGroup(pop, tonic_eqs, threshold="v>0*mV", reset=on_spike_tonic, refractory=delta_t_ref, method="exponential_euler", name = "TestGroup")
tonic_ind.v = "E_L + rand()*(v_th - E_L)"
tonic_ind_PMC_pop = pop
tonic_ind_PMC = NeuronGroup(pop, tonic_eqs, threshold="v>0*mV", reset=on_spike_tonic, refractory=delta_t_ref, method="exponential_euler", name = "TestGroup2")
tonic_ind_PMC.v = "E_L + rand()*(v_th - E_L)"
# Then inverse neurons
inv_neuron_pop = pop
inv_neuron = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler", name = "TestGroup3")
inv_neuron.v = "E_L + rand()*(v_th - E_L)"
# Type I direct neurons (RENAMED TO des_in from t1_direct)
des_in_pop = pop
des_in = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler", name = "TestGroup4")
des_in.v = "E_L + rand()*(v_th - E_L)"
# Type II direct neurons
t2_direct_pop = pop
t2_direct = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
t2_direct.v = "E_L + rand()*(v_th - E_L)"
# Transient neurons
transient_pop = pop
transient = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
transient.v = "E_L + rand()*(v_th - E_L)"
# Finally, Relay neurons
# First pathway A (upper) relay
relay_A_pop = pop
relay_A = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
relay_A.v = "E_L + rand()*(v_th - E_L)"
# Then pathway B (lower) relay
relay_B_pop = pop
relay_B = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
relay_B.v = "E_L + rand()*(v_th - E_L)"
##Pudendal Loop##
# Pudendal motorneurons (Onuf's Nucleus)
onuf = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
onuf.v = "E_L + rand()*(v_th - E_L)"
# Preganglionic neurons within the spine
pgn = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pgn.v = "E_L + rand()*(v_th - E_L)"
# Pudendal afferent
pud_aff = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pud_aff.v = "E_L + rand()*(v_th - E_L)"
# Pudendal Process
pud_proc = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pud_proc.v = "E_L + rand()*(v_th - E_L)"
# Circuit connectivity
pgn_stim = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pgn_stim.v = "E_L + rand()*(v_th - E_L)"
# Then Circuit IO
# Ascending output
asc_out = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
asc_out.v = "E_L + rand()*(v_th - E_L)"
# Onuf inhibition
onuf_inhib = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
onuf_inhib.v = "E_L + rand()*(v_th - E_L)"
##Hypogastric loop##
# First major hypogastric neuron groups
hyp_nerve = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
hyp_nerve.v = "E_L + rand()*(v_th - E_L)"
img = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
img.v = "E_L + rand()*(v_th - E_L)"
hyp_eff = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
hyp_eff.v = "E_L + rand()*(v_th - E_L)"
# Descending inhibition
hyp_inhib = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
hyp_inhib.v = "E_L + rand()*(v_th - E_L)"
##Pelvic Loop##
# The last reflex loop to be constructed is the connections within the pelvic nerve
# First major neuron groups
####################
# Defined new pelvic intermediate.
pelvic_gang = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pelvic_gang.v = "E_L + rand()*(v_th - E_L)"
pelvic_eff = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pelvic_eff.v = "E_L + rand()*(v_th - E_L)"
pelvic_proc = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pelvic_proc.v = "E_L + rand()*(v_th - E_L)"
# Including other connections
hyp_proc = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
hyp_proc.v = "E_L + rand()*(v_th - E_L)"
##Remaining PGN Connections##
# Inhibitory neuron driven by PGN
pgn_inhib = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pgn_inhib.v = "E_L + rand()*(v_th - E_L)"
# Inverse neuron that inhibits PGN via descending input
des_inverse = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
des_inverse.v = "E_L + rand()*(v_th - E_L)"
# PGN reciprocal inhibitor
pgn_reciprocal = NeuronGroup(pop, adapt_eqs, threshold="v>0*mV", reset=on_spike, refractory=delta_t_ref, method="exponential_euler")
pgn_reciprocal.v = "E_L + rand()*(v_th - E_L)"
##################################Synapses#################################################
##Tibial Nerve
# ----------------------------------------------------------------------
# First from Tibial nerve to two interneurons (Excitatory, classical)
tib_tib_classic = Synapses(tibial_nerve, tibial_classic, model=syn_model, on_pre=on_pre_ex, on_post=on_post_ex)
tib_tib_classic.connect(condition='i!=j', p = 1)
tib_tib_classic.weight = tib_weight[0]
tib_tib_classic.min_weight = tib_weight[0]
tib_tib_classic.max_weight = tib_weight[0]
tib_tib_op = Synapses(tibial_nerve, tibial_op, model=syn_model, on_pre=on_pre_ex, on_post=on_post_ex)
tib_tib_op.connect(condition='i!=j', p = 1)
tib_tib_op.weight = tib_weight[1]
tib_tib_op.max_weight = tib_weight[1]
tib_tib_op.min_weight = tib_weight[1]
if Spine_connected is True:
# Then from classical spinal branch to ascending output (Inhibitory, classical)
tib_classic_asc_out = Synapses(tibial_classic, asc_out, model=syn_model, on_pre=on_pre_in, on_post=on_post_in)
tib_classic_asc_out.connect(condition='i!=j', p = 1)
tib_classic_asc_out.weight = tib_weight[0]
tib_classic_asc_out.max_weight = tib_weight[0]