-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_script.py
1962 lines (1474 loc) · 45.5 KB
/
test_script.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
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 script tests the model creation and compilation
# It also test the calculation capabilities
# It uses some simple model and assertions
# import pytest
from mobspy import *
from tellurium import loada as te_load_anti
import numpy as np
from copy import deepcopy
import sys
import os
# Compare results with expected file
def compare_model(comp_results, file_name):
with open(file_name, 'r') as file:
for r, line in zip(comp_results.split('\n'), file.readlines()):
line = line.replace('\n', '')
if r != line:
print('file: ' + line)
print('test: ' + r)
return False
return True
# Model to test the basics
def test_model_1():
A, B, C = BaseSpecies(3)
A + B >> C[1]
MySim = Simulation(A | B | C)
MySim.level = -1
results = MySim.compile()
assert compare_model(results, 'test_tools/model_1.txt')
# Model to test basic inheritance
def test_model_2():
Carnivore, Herbivore = BaseSpecies(2)
Cat, Dog = New(Carnivore, 2)
Carnivore + Herbivore(1 * u.mol) >> Carnivore[1]
Cat(1 * u.mol), Dog(1 * u.mol)
MySim = Simulation(Cat | Dog | Herbivore)
MySim.level = -1
MySim.volume = 1 * u.meter ** 2
results = MySim.compile()
assert compare_model(results, 'test_tools/model_2.txt')
# Model to test species multiplication
def test_model_3():
MGMT, Blue_Oyster_Cult, The_Smiths = BaseSpecies(3)
MGMT.eletric_fell, MGMT.little_dark_age, MGMT.kids
Blue_Oyster_Cult.burning_for_you >> Blue_Oyster_Cult.reaper[1]
The_Smiths.stop_me >> The_Smiths.charming_man[1]
Music = MGMT * Blue_Oyster_Cult * The_Smiths
MySim = Simulation(Music)
MySim.level = -1
results = MySim.compile()
assert compare_model(results, 'test_tools/model_3.txt')
# Model to test inheritance queries
# All bacterias are infected by any virus here
def test_model_4():
Bacteria, Virus = BaseSpecies(2)
B1, B2 = New(Bacteria, 2)
V1, V2 = New(Virus, 2)
Bacteria.not_infected + Virus >> Bacteria.infected[1]
MySim = Simulation(B1 | B2 | V1 | V2)
MySim.level = -1
results = MySim.compile()
assert compare_model(results, 'test_tools/model_4.txt')
# Model to test round-robin and stoichiometry
def test_model_5():
A = BaseSpecies(1)
B, C = New(A, 2)
A >> 2 * A[1]
2 * A >> 3 * A[1]
MySim = Simulation(B | C)
MySim.level = -1
results = MySim.compile()
assert compare_model(results, 'test_tools/model_5.txt')
def test_model_6():
# This model tests species that are not referenced in the reactants (we call them Born Species)
A = BaseSpecies(1)
B = New(A)
C = New(A)
B.b1, B.b2, C.c1, C.c2
Zero >> 2 * A[1]
MySim = Simulation(B | C)
MySim.level = -1
results = MySim.compile()
assert compare_model(results, 'test_tools/model_6.txt')
def test_model_7():
def oscillator(beta_m=5, beta_p=10, gamma_m=1, gamma_p=0.01, k=1, n=4, leaky=0.0001):
Mortal, Creator = BaseSpecies(2)
mRNA = Mortal * Creator
Protein = New(Mortal)
# Repression reactions
for m, p in zip(['m1', 'm2', 'm3'], ['x2', 'x3', 'x1']):
Protein.c(p) >> Protein.c(p) + mRNA.c(m)[lambda pro: f'{beta_m}/(1 + ({pro}/{k})^{n})']
# Production reactions
for m, p in zip(['m1', 'm2', 'm3'], ['x1', 'x2', 'x3']):
mRNA.c(m) >> mRNA.c(m) + Protein.c(p)[beta_p]
# We need the rate of degradation to be different from proteins and mRNA
Mortal >> Zero[lambda r1: gamma_p if r1.is_a(Protein) else gamma_m]
# This is the leaky mRNA expression, it needs to be low
Zero >> Creator[leaky]
MySim = Simulation(mRNA | Protein)
MySim.level = -1
return MySim.compile()
assert compare_model(oscillator(), 'test_tools/model_7.txt')
# Model to test well defined orthogonal spaces
def test_orthogonal_spaces():
try:
A, B = BaseSpecies(2)
A.a, A.b
C = New(B)
C.a, C.b
MySim = Simulation(A | C)
MySim.level = -1
MySim.compile()
assert False
except SystemExit:
assert True
# Model to test dimensional inconsistency
def test_dimensional_inconsistency():
try:
A, B, C = BaseSpecies(3)
A(1 * u.mol / u.meter ** 3) + B(1 * u.mol / u.meter ** 2) >> C[1]
MySim = Simulation(A | B | C)
MySim.level = -1
MySim.compile()
assert False
except SystemExit:
print('Dimensional inconsistency model Ok')
assert True
def test_average_value():
E = BaseSpecies(1)
Zero >> E[12]
E >> Zero[25]
MySim = Simulation(E)
MySim.save_data = False
MySim.plot_data = False
MySim.level = -1
MySim.run()
def test_hybrid_sim():
A, B = BaseSpecies(2)
A >> 2 * A[1]
A(1), B(50)
S1 = Simulation(A)
S1.save_data = False
S1.plot_data = False
S1.duration = 3
S1.level = -1
A.reset_reactions()
A + B >> Zero[0.01]
S2 = Simulation(A | B)
S2.method = 'stochastic'
S2.duration = (A <= 0) | (B <= 0)
S2.level = -1
Sim = S1 + S2
Sim.run()
assert compare_model(Sim.compile(), 'test_tools/model_8.txt')
assert Sim.fres[A][-1] == 0 or Sim.fres[B][-1] == 0
def test_concatenated_simulation():
A, B, C = BaseSpecies(3)
A >> Zero[1]
A(50)
S1 = Simulation(A)
S1.plot_data = False
S1.duration = 5
S1.level = -1
B >> Zero[1]
B(50)
S2 = Simulation(B)
S2.duration = 5
S2.level = -1
C >> Zero[1]
C(50)
S3 = Simulation(C)
S3.duration = 5
S3.level = -1
S = S1 + S2 + S3
S.run()
assert S.fres[A][-1] < 1 and S.fres[B][-1] < 1 and S.fres[C][-1] < 1
def test_event_type():
A, B, C, D, E, F = BaseSpecies(6)
A + B >> Zero[1]
A(50), B(50), C(0)
S = Simulation(A | B | C | D | E | F)
S.plot_data = False
S.level = -1
with S.event_time(0):
F(1)
with S.event_condition((A <= 1) & (B <= 1)):
C(1)
with S.event_condition((A <= 1) & (B <= 1)):
D(1)
with S.event_condition(B <= 1):
E(1)
S.duration = 5
assert compare_model(S.compile(), 'test_tools/model_15.txt')
def test_reacting_species_event():
B = BaseSpecies(1)
B.b1, B.b2
A = New(B)
B >> Zero[1]
A.b2 >> Zero[0.5]
A.a1 >> Zero[1]
A.a1(100), A.b2(100), B.b1(100)
S = Simulation(A | B)
S.level = -1
with S.event_condition((A.a1 <= 10) & (B.b1 <= 10)):
A.a1(100)
S.duration = 5
assert compare_model(S.compile(), 'test_tools/model_9.txt')
def test_unit_event_test():
A = BaseSpecies(1)
A >> Zero[1 / u.s]
A(1 * u.mol)
S = Simulation(A)
S.level = -1
with S.event_condition(A < 0.5 * u.mol):
A(1 * u.mol)
S.duration = 3
assert compare_model(S.compile(), 'test_tools/model_10.txt')
def test_reaction_deactivation():
A, R = BaseSpecies(2)
A + R >> 2 * A + R[1]
# Adding this reaction for compatibility reasons with python versions lower than 3.10
R >> Zero[1e-100]
A(1), R(1)
S1 = Simulation(A | R)
S1.level = -1
S1.duration = 1
S1.plot_data = False
S2 = Simulation(A | R)
S2.duration = 1
S2.level = -1
with S2.event_time(0):
R(0)
Sim = S1 + S2
Sim.run()
assert Sim.fres[A][0] < Sim.fres[A][-1] and Sim.fres[R][0] == 1 and Sim.fres[R][-1] == 0
def test_count_assignment():
A = BaseSpecies(1)
B = New(A)
A.a1, A.a2
B.b1 >> Zero[1]
A.a1(100), A.a2(100)
B.b1(100), B.b2(100)
S = Simulation(A | B)
S.level = -1
S.plot_data = False
S.duration = 5
S.run()
assert compare_model(S.compile(), 'test_tools/model_11.txt') \
and 150 > S.fres[B][-1] > 100 and S.fres[A][-1] == 200
def test_complex_cell_model():
Resource, Phage, Infectable = BaseSpecies(3)
Cell = New(Infectable)
Cell.t1, Cell.t2, Cell.t3
def reproduction_rate(r):
if r.t1:
return 2
elif r.t2:
return 1
elif r.t3:
return 0.5
Infectable.not_infected + Phage >> Infectable.infected[1]
Cell + Resource >> 2 * Cell[lambda r1: reproduction_rate(r1)]
Zero >> Resource[1]
Resource >> Zero[1]
Cell >> Zero[0.1]
Cell.t1(1), Cell.t2(1), Cell.t3(1)
S1 = Simulation(Cell | Resource | Phage)
S1.level = -1
S1.duration = 30
Cell.reset_reactions()
Phage(1000)
S2 = Simulation(Cell | Phage)
S2.level = -1
S2.duration = 10
S = S1 + S2
S.plot_data = False
S.run()
for i, c in enumerate(S.fres['Cell.t1.not_infected']):
if c > 0:
continue
else:
change_index = i
break
boll_1 = round(S.fres[Cell.t1.not_infected][change_index - 1], 2) \
== round(S.fres[Cell.t1.infected][-1], 2)
boll_2 = round(S.fres[Cell.t1.not_infected][change_index - 1], 2) \
== round(S.fres[Cell.t1.infected][-1], 2)
boll_3 = round(S.fres[Cell.t1.not_infected][change_index - 1], 2) \
== round(S.fres[Cell.t1.infected][-1], 2)
assert boll_1 and boll_2 and boll_3
def test_zero_rate_reactions():
A, B = BaseSpecies(2)
A.a1, A.a2, B.b1, B.b2
Combination = A * B
Combination >> Zero[lambda r1: 0 if r1.b2 else 1]
S = Simulation(Combination)
S.level = -1
return compare_model(S.compile(), 'test_tools/model_12.txt')
def test_double_rate():
A, B = BaseSpecies(2)
A.a1, A.a2, B.b1, B.b2
def rate(r1, ball):
factor1 = 0.5 if r1.a1 else 1
factor2 = 0.5 if ball.b1 else 1
return factor1 * factor2
A + B >> Zero[rate]
S = Simulation(A | B)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_13.txt')
def test_single_rate():
A, B = BaseSpecies(2)
A.a1, A.a2, B.b1, B.b2
def rate(r1):
factor1 = 0.5 if r1.a1 else 1
return factor1
A + B >> Zero[rate]
S = Simulation(A | B)
S.level = - 1
assert compare_model(S.compile(), 'test_tools/model_14.txt')
def test_triple_rate():
A, B = BaseSpecies(2)
A.a1, A.a2, B.b1, B.b2
def rate(r1, r2, r3):
factor1 = 0.5 if r1.a1 else 1
factor2 = 0.5 if r2.b1 else 1
factor3 = 0.5 if r3.c1 else 1
return factor1 * factor2 * factor3
A + B >> Zero[rate]
S = Simulation(A | B)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_13.txt')
def test_stochastic_event_duration():
A, B = BaseSpecies(2)
A + B >> Zero[0.01]
A(100), B(100)
S1 = Simulation(A | B)
S1.save_data = False
S1.plot_data = False
S1.method = 'stochastic'
S1.duration = (A <= 0) | (B <= 0)
S1.level = -1
S1.run()
R = S1.fres
assert R[A][0] > 0 and R[B][0] > 0 and R[A][-1] == 0 and R[B][-1] == 0
def test_logic_operator_syntax():
test_failed = False
simlog.global_simlog_level = -1
A, B = BaseSpecies(2)
A.a1, A.a2, A.a3
try:
(10 >= A) >= 10
test_failed = True
except SystemExit:
pass
try:
(10 >= A >= 10 >= A)
test_failed = True
except SystemExit:
pass
try:
(10 >= A * A)
test_failed = True
except SystemExit:
pass
try:
S1 = Simulation(A)
S1.level = -1
with S1.event_condition(B <= 10):
A(100)
S1.compile()
test_failed = True
except SystemExit:
pass
r1 = ((10 >= 2 * A) & (A <= 10)) | (10 >= A)
S = Simulation(A)
S.level = -1
with S.event_condition(r1):
A(100)
assert compare_model(S.compile(), 'test_tools/model_16.txt')
if test_failed:
assert False
def test_stack_position():
Cell = BaseSpecies()
Cell >> 2 * Cell[1]
A, B, C = New(Cell)
S = Simulation(A | B | C)
S.level = -1
compare_model(S.compile(), 'test_tools/model_17.txt')
def hi():
Cell = BaseSpecies()
Cell >> 2 * Cell[1]
A, B, C = New(Cell)
S = Simulation(A | B | C)
S.level = -1
compare_model(S.compile(), 'test_tools/model_17.txt')
hi()
def hi_inside_hi():
hi()
hi_inside_hi()
def test_empty_arguments():
A, B = BaseSpecies()
A >> Zero[lambda: f'{A}*0.01']
S = Simulation(A)
S.duration = 5
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_18.txt')
def test_conditional_between_meta_species():
Cu = BaseSpecies()
Cu.c1, Cu.c2
Azi, Byy = New(Cu)
Azi.a1, Azi.a2, Byy.b1, Byy.b2
Azi >> Zero[1]
Byy >> Zero[0.1]
Azi(200), Byy(50)
S = Simulation(Azi | Byy)
S.plot_data = False
S.level = -1
with S.event_condition(Azi.a1 <= Byy.b1):
Azi(200)
S.duration = 10
S.method = 'stochastic'
assert compare_model(S.compile(), 'test_tools/model_19.txt')
S.run()
for i in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]:
assert S.fres[Azi][i] > S.fres[Byy][i]
def test_conditional_between_meta_species_2():
A, B = BaseSpecies()
A >> Zero[1]
B >> Zero[0.1]
r1 = ((A < B) & (A < B) | (A < B))
A(200), B(50)
S = Simulation(A | B)
with S.event_condition(r1):
A(200)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_20.txt')
def test_event_reaction_not_allowed():
try:
A = BaseSpecies()
A >> Zero[1]
S = Simulation(A)
with S.event_time(0):
Zero >> A[1]
assert False
except SystemExit:
assert True
def all_test():
A, B = BaseSpecies()
A.a1, A.a2
B.b1, B.b2
C = A * B
All[C](100)
C >> All[C][1]
S = Simulation(C)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_21.txt')
def all_test_2():
B = BaseSpecies()
B.b1, B.b2
C, D = New(B)
C.c1, C.c2, D.d1, D.d2
Zero >> All[B.b1][1]
S = Simulation(C | D)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_22.txt')
def test_error_mult():
try:
D = BaseSpecies(1)
A, B, C = D * BaseSpecies(3)
simlog.global_simlog_level = -1
assert False
except SystemExit:
assert True
def test_set_counts():
A, C = BaseSpecies()
A.a1, A.a2
B = New(A)
B.b1, B.b2
model = set_counts({All['B.a1']: 100, C: 200 * u.mols, 'A.a1': 100, A.a2: 50})
S = Simulation(model)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_23.txt')
def test_bool_error():
B = BaseSpecies()
B >> Zero[1]
B(100)
S = Simulation(B)
simlog.global_simlog_level = -1
try:
with S.event_condition(B == 0):
B(100)
assert False
except SystemExit:
pass
try:
S.duration = True
assert False
except SystemExit:
pass
assert True
def test_event_all():
Acka = BaseSpecies()
Acka.a1, Acka.a2
Baka = New(Acka)
Baka.b1, Baka.b2
Baka >> Zero[1]
S = Simulation(Baka)
S.level = -1
S.plot_data = False
with S.event_time(0):
set_counts({All[Baka]: 10, Baka.b1.a2: 20})
Baka.a1.b1(30)
assert compare_model(S.compile(), 'test_tools/model_24.txt')
S.duration = 5
S.step_size = 1
S.run()
assert S.fres[Baka.a1.b1][0] == 30
assert S.fres[Baka.b1.a2][0] == 20
assert S.fres[Baka.a1.b2][0] == 10
assert S.fres['Baka.a1.b1'][0] == 30
assert S.fres['Baka.b1.a2'][0] == 20
assert S.fres['Baka.a1.b2'][0] == 10
for key in S.fres:
if key == 'Time':
continue
assert S.fres[key][-1] < 1
def test_one_value_concatenation_sim():
A, B = BaseSpecies()
B(200)
S2 = Simulation(A | B)
S2.plot_data = False
S2.level = -1
S2.duration = 5
S2.step_size = 1
S2.duration = (A <= 0) | (B <= 0)
S2.run()
assert len(S2.fres[A]) == 1
def test_crash_after_modification():
try:
A = BaseSpecies()
S1 = Simulation(A)
A = BaseSpecies()
A.a1, A.a2
S2 = Simulation(A)
S = S1 + S2
S.level = -1
S.run()
assert False
except SystemExit:
assert True
def test_unit_bi_dimension():
A = BaseSpecies()
A(5 / u.m ** 2)
S = Simulation(A)
S.volume = 2 * u.m ** 2
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_25.txt')
def test_bi_dimensional_rates():
Ball, Child, Bacteria = BaseSpecies(3)
Ball(10 / u.meter ** 2)
Child(1 / u.meter ** 2)
Bacteria(1 * u.mol)
Bacteria >> Zero[1 * u.mol / u.second]
Ball + Child + Child >> Ball + Child[1e-3 * (u.meter ** 4) / u.hour]
Ball + Child >> Ball[1e-3 * (u.meter ** 2) / u.hour]
S = Simulation(Ball | Child | Bacteria)
S.volume = 2 * u.m ** 2
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_26.txt')
def test_dimension_in_function_only():
A = BaseSpecies()
A + A >> 3 * A[lambda: 1 * u.milliliter / u.second]
A(1)
S = Simulation(A)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_27.txt')
def test_multiple_simulation_counts():
Age, Color, Size = BaseSpecies()
Age.young, Age.old,
Color.blue, Color.red,
Size.small, Size.big
Tree = Age * Color * Size
Tree(100), Tree.red.big(150), All[Tree](10), All[Tree.big](100)
S = Simulation(Tree)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_28.txt')
Tree.reset_quantities()
model = set_counts({All[Tree]: 30, 'Tree.blue.old': 100})
S = Simulation(model)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_29.txt')
def test_string_events_assignment():
A = BaseSpecies()
A.a1, A.a2, A.a3
S = Simulation(A)
S.level = -1
with S.event_time(5):
All[A](f'{A} + 1')
with S.event_time(10):
All[A.a1](f'{A} + 1')
with S.event_time(15):
A.a1(f'{A} + 1')
compare_model(S.compile(), 'test_tools/model_30.txt')
def test_plotting():
Color, Disease = BaseSpecies()
Color.blue, Color.red, Color.yellow
Disease.not_sick, Disease.sick
Disease.not_sick >> Disease.sick[1]
Tree = Color * Disease
Tree.yellow(20), Tree.red(20), Tree.blue(20)
S = Simulation(Tree)
S.level = -1
S.method = 'stochastic'
S.plot_data = False
S.repetitions = 3
S.step_size = 0.25
S.duration = 3
S.run()
S.plot_config.save_to = 'test_plot_images/stochastic_tree.png'
S.plot_stochastic(Tree.not_sick, Tree.sick)
S.plot_config.save_to = 'test_plot_images/deterministic_tree.png'
S.plot(Tree.not_sick, Tree.sick)
S.plot_config.save_to = 'test_plot_images/constant_tree.png'
S.plot()
assert os.path.exists('test_plot_images/stochastic_tree.png')
assert os.path.exists('test_plot_images/deterministic_tree.png')
assert os.path.exists('test_plot_images/constant_tree.png')
def test_volume_after_sim():
A = BaseSpecies()
Zero >> A[42 * 1 / (u.s * u.milliliter)]
A >> Zero[1]
S = Simulation(A)
S.plot_data = False
S.output_concentration = False
S.level = -1
S.volume = 1 * u.milliliter
S.run()
assert int(S.fres[A][-1]) == 42
def order_model_str(data_for_sbml):
species_for_sbml = data_for_sbml['species_for_sbml']
mappings_for_sbml = data_for_sbml['mappings']
parameters_for_sbml = data_for_sbml['parameters_for_sbml']
reactions_for_sbml = data_for_sbml['reactions_for_sbml']
events_for_sbml = data_for_sbml['events_for_sbml']
model_str = '\n'
model_str += 'Species' + '\n'
species_alpha = list(sorted(species_for_sbml.keys()))
for spe in species_alpha:
model_str += spe.replace('_dot_', '.') + ',' + str(species_for_sbml[spe]) + '\n'
model_str += '\n'
model_str += 'Mappings' + '\n'
mappings_alpha = list(sorted(mappings_for_sbml.keys()))
for map in mappings_alpha:
model_str += map + ' :' + '\n'
for element in sorted(mappings_for_sbml[map]):
model_str += element + '\n'
model_str += '\n'
model_str += 'Parameters' + '\n'
parameters_alpha = list(sorted(parameters_for_sbml.keys()))
for par in parameters_alpha:
model_str += par + ',' + str(parameters_for_sbml[par][0]) + '\n'
model_str += '\n'
model_str += 'Reactions' + '\n'
remove_phantom_reactions = deepcopy(reactions_for_sbml)
to_remove = []
for reaction in remove_phantom_reactions:
if 'phantom' in reaction:
to_remove.append(reaction)
for r in to_remove:
remove_phantom_reactions.pop(r, None)
reaction_alpha = [str(x[1]).replace('_dot_', '.') for x in
list(sorted(remove_phantom_reactions.items(), key=lambda x: str(x[1])))]
for i, reac in enumerate(reaction_alpha):
model_str += 'reaction_' + str(i) + ',' + reac + '\n'
if events_for_sbml != {}:
model_str += '\n'
model_str += 'Events' + '\n'
list_to_sort = [str(events_for_sbml[key]) for key in events_for_sbml]
list_to_sort = sorted(list_to_sort)
for i in range(len(list_to_sort)):
model_str += ('event_' + str(i) + ',' + list_to_sort[i] + '\n').replace('_dot_', '.')
return model_str
def test_parameters_with_sbml():
A = BaseSpecies()
A.a1, A.a2
a, b, c, d, f, h = ModelParameters([1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2])
A >> 2 * A[lambda: f'5*(b + c)/10']
All[A](a)
S1 = Simulation(A)
with S1.event_time(0):
A.a2(d)
with S1.event_time(2):
A.a1('a + b')
with S1.event_time(f):
A.a1(d)
S1.duration = 3
B = BaseSpecies()
B >> 2 * B[h]
B(a)
S2 = Simulation(A | B)
S2.duration = 2
S = S1 + S2
S.plot_data = False
S.level = -1
S.run()
model_str = ''
for parameter_sweep in S1.sbml_data_list:
for data_for_sbml in parameter_sweep:
model_str += order_model_str(data_for_sbml)
assert compare_model(model_str, 'test_tools/model_31.txt')
def test_shared_parameter_name():
try:
A = BaseSpecies()
a = ModelParameters([1, 2])
a.rename('A')
A >> 2 * A[a]
set_counts({'A': a})
S = Simulation(A)
S.level = -1
S.plot_data = False
S.run()
assert False
except:
assert True
def test_set_counts_parameters():
A = BaseSpecies()
a = ModelParameters([1, 2])
A >> 2 * A[a]
set_counts({'A': a})
S = Simulation(A)
S.level = -1
assert compare_model(S.compile(), 'test_tools/model_32.txt')
def test_repeated_parameters():
try:
A = BaseSpecies()
A.a1, A.a2
a = ModelParameters([1, 2])
A >> 2 * A[a]
All[A](1)
S1 = Simulation(A)
S1.duration = 3
B = BaseSpecies()
a = ModelParameters([3, 4])
B >> 2 * B[a]
B(1)
S2 = Simulation(A | B)
S2.duration = 2
S = S1 + S2
S.plot_data = False
S.level = -1
S.compile()
assert False
except SystemExit:
assert True
def initial_expression_test():
A, B, Hey = BaseSpecies()