File: wxFrame1.py

package info (click to toggle)
model-builder 0.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,056 kB
  • sloc: python: 3,916; fortran: 690; makefile: 18; sh: 11
file content (1490 lines) | stat: -rw-r--r-- 62,405 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
# -*- coding:latin-1 -*-
#-----------------------------------------------------------------------------
# Name:        wxFrame1.py
# Purpose:
#
# Author:      <Flavio Codeco Coelho>
#
# Created:     2003/02/04
# RCS-ID:      $Id: wxFrame1.py,v 1.11 2004/01/13 10:51:43 fccoelho Exp $
# Copyright:   (c) 2003 Flavio Codeco Coelho <fccoelho@fiocruz.br>
# Licence:     This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#-----------------------------------------------------------------------------
#Boa:Frame:wxFrame1
from __future__ import division
import wxversion
wxversion.select('2.8')
import wx
import wx.stc
#from Numeric import *
from threading import *
from scipy import integrate
#from RandomArray import *
from numpy.random import *

import pickle
#from MLab import *
import matplotlib
matplotlib.use('WXAgg')
from pylab import *
from matplotlib.cbook import *
from wxPython.tools import helpviewer
#from xml.dom import Node

import os,sys
import wxFrame2, about


from odexml import Model
import PlotFigure as PF
from Bayes import Melding as meld
from time import *
import icones
import quivVarFrame as QF
from lhsframe import LHS


os.chdir(os.getcwd())

def create(parent):
    return wxFrame1(parent)

[wxID_WXFRAME1, wxID_WXFRAME1CONVCHECKBOX, wxID_WXFRAME1CRITTIMETEXT,
 wxID_WXFRAME1ENDTEXT, wxID_WXFRAME1EQNEDIT, wxID_WXFRAME1EQTEXT,
 wxID_WXFRAME1FIRSTSTEPTEXT, wxID_WXFRAME1FOCHECKBOX, wxID_WXFRAME1GAUGE1,
 wxID_WXFRAME1INITVALINPUT, wxID_WXFRAME1INITVALTEXT,
 wxID_WXFRAME1MAXSTEPTEXT, wxID_WXFRAME1MINSTEPTEXT, wxID_WXFRAME1PANEL1,
 wxID_WXFRAME1PARAMTEXT, wxID_WXFRAME1PAREDIT, wxID_WXFRAME1PROGRESSTEXT,
 wxID_WXFRAME1SEPARALINE, wxID_WXFRAME1STARTTEXT, wxID_WXFRAME1STATUSBAR1,
 wxID_WXFRAME1TEXTCTRL2, wxID_WXFRAME1TEXTCTRL3, wxID_WXFRAME1TEXTCTRL4,
 wxID_WXFRAME1TEXTCTRL5, wxID_WXFRAME1TEXTCTRL6, wxID_WXFRAME1TEXTCTRL7,
 wxID_WXFRAME1TEXTCTRL8, wxID_WXFRAME1TIMESTEPTEXT, wxID_WXFRAME1TOOLBAR1,
] = [wx.NewId() for _init_ctrls in xrange(29)]

[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2,
 wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4,
] = [wx.NewId() for _init_coll_menu1_Items in xrange(5)]

[wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1,
 wxID_WXFRAME1TOOLBAR1TOOLS2,
] = [wx.NewId() for _init_coll_toolBar1_Tools in xrange(3)]

[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL,
] = [wx.NewId() for _init_coll_menu2_Items in xrange(2)]

[wxID_WXFRAME1MENU3ITEMS0] = [wx.NewId() for _init_coll_menu3_Items in xrange(1)]

[wxID_WXFRAME1TOOLBAR1OPENTOOL, wxID_WXFRAME1TOOLBAR1SAVETOOL,
 wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1,
 wxID_WXFRAME1TOOLBAR1TOOLS2,
] = [wx.NewId() for _init_coll_toolBar1_Tools in range(5)]

[wxID_WXFRAME1, wxID_WXFRAME1CONVCHECKBOX, wxID_WXFRAME1CRITTIMETEXT,
 wxID_WXFRAME1ENDTEXT, wxID_WXFRAME1EQNEDIT, wxID_WXFRAME1EQTEXT,
 wxID_WXFRAME1FIRSTSTEPTEXT, wxID_WXFRAME1FOCHECKBOX, wxID_WXFRAME1GAUGE1,
 wxID_WXFRAME1INITVALINPUT, wxID_WXFRAME1INITVALTEXT,
 wxID_WXFRAME1MAXSTEPTEXT, wxID_WXFRAME1MINSTEPTEXT, wxID_WXFRAME1PANEL1,
 wxID_WXFRAME1PARAMTEXT, wxID_WXFRAME1PAREDIT, wxID_WXFRAME1PROGRESSTEXT,
 wxID_WXFRAME1SEPARALINE, wxID_WXFRAME1STARTTEXT, wxID_WXFRAME1STATUSBAR1,
 wxID_WXFRAME1TEXTCTRL2, wxID_WXFRAME1TEXTCTRL3, wxID_WXFRAME1TEXTCTRL4,
 wxID_WXFRAME1TEXTCTRL5, wxID_WXFRAME1TEXTCTRL6, wxID_WXFRAME1TEXTCTRL7,
 wxID_WXFRAME1TEXTCTRL8, wxID_WXFRAME1TIMESTEPTEXT, wxID_WXFRAME1TOOLBAR1,
] = [wx.NewId() for _init_ctrls in range(29)]

[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL,
] = [wx.NewId() for _init_coll_menu2_Items in range(2)]

[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2,
 wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4,
] = [wx.NewId() for _init_coll_menu1_Items in range(5)]

[wxID_WXFRAME1MENU3ITEMS0, wxID_WXFRAME1MENU3MBHELP,
] = [wx.NewId() for _init_coll_menu3_Items in range(2)]

class wxFrame1(wx.Frame):

    def _init_coll_boxSizerH_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.gridBagSizer1, 0, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.separaLine, 0, border=0, flag=0)
        parent.AddSizer(self.boxSizerV2, 0, border=0, flag=wx.EXPAND)

    def _init_coll_bsPanel_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND)

    def _init_coll_gridBagSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.eqText, (0, 0), border=0, flag=0, span=(1,3))
        parent.AddWindow(self.eqnEdit, (1, 0), border=0, flag=wx.EXPAND,
              span=(1, 3))
        parent.AddWindow(self.initValText, (2, 0), border=0, flag=0, span=(1,3))
        parent.AddWindow(self.initValInput, (3, 0), border=0, flag=0, span=(1,
              3))
        parent.AddWindow(self.startText, (4, 0), border=0, flag=0, span=(1,1))
        parent.AddWindow(self.endText, (4, 1), border=0, flag=0, span=(1,1))
        parent.AddWindow(self.timeStepText, (4, 2), border=0, flag=0, span=(1,
              1))
        parent.AddWindow(self.textCtrl2, (5, 0), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.textCtrl3, (5, 1), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.textCtrl4, (5, 2), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.critTimeText, (6, 0), border=0, flag=0, span=(1,
              1))
        parent.AddWindow(self.firstStepText, (6, 2), border=0, flag=0, span=(1,
              1))
        parent.AddWindow(self.textCtrl5, (7, 0), border=0, flag=0, span=(1, 2))
        parent.AddWindow(self.textCtrl6, (7, 2), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.minStepText, (8, 0), border=0, flag=0, span=(1,1))
        parent.AddWindow(self.maxStepText, (8, 1), border=0, flag=0, span=(1,1))
        parent.AddWindow(self.progressText, (8, 2), border=0, flag=0, span=(1,
              1))
        parent.AddWindow(self.textCtrl7, (9, 0), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.textCtrl8, (9, 1), border=0, flag=0, span=(1, 1))
        parent.AddWindow(self.gauge1, (9, 2), border=0, flag=0, span=(1, 1))

    def _init_coll_boxSizerV2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.paramText, 1, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.parEdit, 7, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.focheckBox, 1, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.convcheckBox, 1, border=0, flag=wx.EXPAND)

    def _init_coll_menuBar1_Menus(self, parent):
        # generated method, don't edit

        parent.Append(menu=self.menu1, title='File')
        parent.Append(menu=self.menu2, title='Analysis')
        parent.Append(menu=self.menu3, title='Help')

    def _init_coll_menu3_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='Open the main help file',
              id=wxID_WXFRAME1MENU3MBHELP, kind=wx.ITEM_NORMAL,
              text='Model Builder Help')
        parent.AppendSeparator()
        parent.Append(help='General Information about PyMM',
              id=wxID_WXFRAME1MENU3ITEMS0, kind=wx.ITEM_NORMAL, text='About')
        self.Bind(wx.EVT_MENU, self.OnMenu3items0Menu,
              id=wxID_WXFRAME1MENU3ITEMS0)
        self.Bind(wx.EVT_MENU, self.OnMenu3MbhelpMenu,
              id=wxID_WXFRAME1MENU3MBHELP)

    def _init_coll_menu1_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='Open a saved model.', id=wxID_WXFRAME1MENU1ITEMS0,
              kind=wx.ITEM_NORMAL, text='Open')
        parent.Append(help='Save your model.', id=wxID_WXFRAME1MENU1ITEMS1,
              kind=wx.ITEM_NORMAL, text='Save')
        parent.Append(help='Save your model on another file.',
              id=wxID_WXFRAME1MENU1ITEMS2, kind=wx.ITEM_NORMAL, text='Save As')
        parent.Append(help='Close your model.', id=wxID_WXFRAME1MENU1ITEMS3,
              kind=wx.ITEM_NORMAL, text='Close')
        parent.Append(help='Exit PyMM.', id=wxID_WXFRAME1MENU1ITEMS4,
              kind=wx.ITEM_NORMAL, text='Exit')
        self.Bind(wx.EVT_MENU, self.OnMenu1items0Menu,
              id=wxID_WXFRAME1MENU1ITEMS0)
        self.Bind(wx.EVT_MENU, self.OnMenu1items1Menu,
              id=wxID_WXFRAME1MENU1ITEMS1)
        self.Bind(wx.EVT_MENU, self.OnMenu1items2Menu,
              id=wxID_WXFRAME1MENU1ITEMS2)
        self.Bind(wx.EVT_MENU, self.OnMenu1items3Menu,
              id=wxID_WXFRAME1MENU1ITEMS3)
        self.Bind(wx.EVT_MENU, self.OnMenu1items4Menu,
              id=wxID_WXFRAME1MENU1ITEMS4)

    def _init_coll_menu2_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='Enter uncertainty analysis mode',
              id=wxID_WXFRAME1MENU2ITEMS0, kind=wx.ITEM_CHECK,
              text='Uncertainty analysis')
        parent.Append(help='Generates a State Space quiver plot.',
              id=wxID_WXFRAME1MENU2QUIVERPL, kind=wx.ITEM_NORMAL,
              text='State Diagram...')
        self.Bind(wx.EVT_MENU, self.OnMenu2items0Menu,
              id=wxID_WXFRAME1MENU2ITEMS0)
        self.Bind(wx.EVT_MENU, self.OnMenu2Items1Menu,
              id=wxID_WXFRAME1MENU2QUIVERPL)

    def _init_coll_toolBar1_Tools(self, parent):
        # generated method, don't edit

        parent.DoAddTool(bitmap=icones.getOpenBitmap(),
              bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME1TOOLBAR1OPENTOOL,
              kind=wx.ITEM_NORMAL, label='Open',
              longHelp='Click Here to open a new model.',
              shortHelp='Open Model')
        parent.DoAddTool(bitmap=icones.getSaveBitmap(),
              bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME1TOOLBAR1SAVETOOL,
              kind=wx.ITEM_NORMAL, label='Save', longHelp='Click here to save.',
              shortHelp='Save Model')
        parent.AddSeparator()
        parent.AddTool(bitmap=icones.getrunBitmap(),
              id=wxID_WXFRAME1TOOLBAR1TOOLS0, isToggle=False,
              longHelpString='Start your simulation.',
              pushedBitmap=wx.NullBitmap, shortHelpString='Start')
        parent.AddTool(bitmap=icones.getequationsBitmap(),
              id=wxID_WXFRAME1TOOLBAR1TOOLS1, isToggle=False,
              longHelpString='Show typeset Equations',
              pushedBitmap=wx.NullBitmap, shortHelpString='Show equations')
        parent.AddTool(bitmap=icones.getspreadsheetBitmap(),
              id=wxID_WXFRAME1TOOLBAR1TOOLS2, isToggle=False,
              longHelpString='Show table with results',
              pushedBitmap=wx.NullBitmap, shortHelpString='Results')
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools0Tool,
              id=wxID_WXFRAME1TOOLBAR1TOOLS0)
        self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolbar1tools0ToolRclicked,
              id=wxID_WXFRAME1TOOLBAR1TOOLS0)
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools1Tool,
              id=wxID_WXFRAME1TOOLBAR1TOOLS1)
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools2Tool,
              id=wxID_WXFRAME1TOOLBAR1TOOLS2)
        self.Bind(wx.EVT_TOOL, self.OnToolBar1OpentoolTool,
              id=wxID_WXFRAME1TOOLBAR1OPENTOOL)
        self.Bind(wx.EVT_TOOL, self.OnToolBar1SavetoolTool,
              id=wxID_WXFRAME1TOOLBAR1SAVETOOL)

        parent.Realize()

    def _init_coll_statusBar1_Fields(self, parent):
        # generated method, don't edit
        parent.SetFieldsCount(1)

        parent.SetStatusText(number=0, text='Status')

        parent.SetStatusWidths([-1])

    def _init_sizers(self):
        # generated method, don't edit
        self.gridBagSizer1 = wx.GridBagSizer(hgap=3, vgap=2)
        self.gridBagSizer1.SetCols(2)
        self.gridBagSizer1.SetRows(10)
        self.gridBagSizer1.SetFlexibleDirection(12)
        self.gridBagSizer1.SetNonFlexibleGrowMode(1)

        self.bsPanel = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizerH = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizerV2 = wx.BoxSizer(orient=wx.VERTICAL)

        self._init_coll_gridBagSizer1_Items(self.gridBagSizer1)
        self._init_coll_bsPanel_Items(self.bsPanel)
        self._init_coll_boxSizerH_Items(self.boxSizerH)
        self._init_coll_boxSizerV2_Items(self.boxSizerV2)

        self.SetSizer(self.bsPanel)

    def _init_utils(self):
        # generated method, don't edit
        self.menu1 = wx.Menu(title='File')

        self.menu3 = wx.Menu(title='Help')

        self.menuBar1 = wx.MenuBar()

        self.menu2 = wx.Menu(title='Analysis')

        self._init_coll_menu1_Items(self.menu1)
        self._init_coll_menu3_Items(self.menu3)
        self._init_coll_menuBar1_Menus(self.menuBar1)
        self._init_coll_menu2_Items(self.menu2)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
              pos=wx.Point(362, 472), size=wx.Size(730, 480),
              style=wx.DEFAULT_FRAME_STYLE, title='Model Builder - ODE')
        self._init_utils()
        self.SetClientSize(wx.Size(730, 480))
        self.SetMenuBar(self.menuBar1)
        self.SetToolTipString('Model Builder')
        self.SetAutoLayout(True)
        self.SetBestFittingSize(wx.Size(730, 480))
        self.SetMaxSize(wx.Size(730, 480))
        self.SetIcon(wx.Icon('/usr/share/pixmaps/MB.ico',wx.BITMAP_TYPE_ICO))
        self.Bind(wx.EVT_CLOSE, self.OnWxFrame1Close)

        self.statusBar1 = wx.StatusBar(id=wxID_WXFRAME1STATUSBAR1,
              name='statusBar1', parent=self, style=0)
        self.statusBar1.SetSize(wx.Size(80, 19))
        self.statusBar1.SetPosition(wx.Point(-1, -1))
        self._init_coll_statusBar1_Fields(self.statusBar1)
        self.SetStatusBar(self.statusBar1)

        self.toolBar1 = wx.ToolBar(id=wxID_WXFRAME1TOOLBAR1, name='toolBar1',
              parent=self, pos=wx.Point(0, 31), size=wx.Size(248, 50),
              style=wx.TRANSPARENT_WINDOW | wx.TB_HORIZONTAL | wx.NO_BORDER)
        self.toolBar1.SetToolTipString('')
        self.toolBar1.SetThemeEnabled(True)
        self.SetToolBar(self.toolBar1)

        self.panel1 = wx.Panel(id=wxID_WXFRAME1PANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(730, 449),
              style=wx.TAB_TRAVERSAL)

        self.initValInput = wx.TextCtrl(id=wxID_WXFRAME1INITVALINPUT,
              name='initValInput', parent=self.panel1, pos=wx.Point(0, 208),
              size=wx.Size(465, 22), style=0, value='')
        self.initValInput.SetToolTipString('Initial conditions: values separated by spaces.')
        self.initValInput.SetBestFittingSize(wx.Size(465, 22))

        self.startText = wx.StaticText(id=wxID_WXFRAME1STARTTEXT,
              label='Start time:', name='startText', parent=self.panel1,
              pos=wx.Point(0, 232), size=wx.Size(153, 20), style=0)

        self.textCtrl2 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL2,
              name='textCtrl2', parent=self.panel1, pos=wx.Point(0, 254),
              size=wx.Size(153, 22), style=0, value='0')
        self.textCtrl2.SetToolTipString('Time value at the start of simulation')

        self.endText = wx.StaticText(id=wxID_WXFRAME1ENDTEXT, label='End Time:',
              name='endText', parent=self.panel1, pos=wx.Point(160, 232),
              size=wx.Size(153, 20), style=0)
        self.endText.SetToolTipString('Time value to end simulation')

        self.textCtrl3 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL3,
              name='textCtrl3', parent=self.panel1, pos=wx.Point(160, 254),
              size=wx.Size(153, 22), style=0, value='10')
        self.textCtrl3.SetToolTipString('Time value to end simulation')

        self.timeStepText = wx.StaticText(id=wxID_WXFRAME1TIMESTEPTEXT,
              label='Time Step:', name='timeStepText', parent=self.panel1,
              pos=wx.Point(320, 232), size=wx.Size(153, 20), style=0)

        self.textCtrl4 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL4,
              name='textCtrl4', parent=self.panel1, pos=wx.Point(320, 254),
              size=wx.Size(153, 22), style=0, value='0.1')
        self.textCtrl4.SetToolTipString('Time step for the output')

        self.critTimeText = wx.StaticText(id=wxID_WXFRAME1CRITTIMETEXT,
              label='Critical Time Steps:', name='critTimeText',
              parent=self.panel1, pos=wx.Point(0, 278), size=wx.Size(153, 20),
              style=0)

        self.textCtrl5 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL5,
              name='textCtrl5', parent=self.panel1, pos=wx.Point(0, 300),
              size=wx.Size(309, 22), style=0, value='')
        self.textCtrl5.SetToolTipString('Time points where integration care should be taken.')

        self.firstStepText = wx.StaticText(id=wxID_WXFRAME1FIRSTSTEPTEXT,
              label='First Step:', name='firstStepText', parent=self.panel1,
              pos=wx.Point(320, 278), size=wx.Size(153, 20), style=0)

        self.textCtrl6 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL6,
              name='textCtrl6', parent=self.panel1, pos=wx.Point(320, 300),
              size=wx.Size(153, 22), style=0, value='0')
        self.textCtrl6.SetToolTipString('Size of the first step (0=auto)')

        self.textCtrl7 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL7,
              name='textCtrl7', parent=self.panel1, pos=wx.Point(0, 348),
              size=wx.Size(153, 22), style=0, value='0')
        self.textCtrl7.SetToolTipString('Minimum absolute step size allowed (0=auto).')

        self.maxStepText = wx.StaticText(id=wxID_WXFRAME1MAXSTEPTEXT,
              label='Max Step Size:', name='maxStepText', parent=self.panel1,
              pos=wx.Point(160, 324), size=wx.Size(153, 22), style=0)

        self.textCtrl8 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL8,
              name='textCtrl8', parent=self.panel1, pos=wx.Point(160, 348),
              size=wx.Size(153, 22), style=0, value='0')
        self.textCtrl8.SetToolTipString('Maximum absolute step size allowed (0=auto)')

        self.separaLine = wx.StaticLine(id=wxID_WXFRAME1SEPARALINE,
              name='separaLine', parent=self.panel1, pos=wx.Point(477, 0),
              size=wx.Size(30, 780),
              style=wx.MAXIMIZE_BOX | wx.LI_VERTICAL | wx.THICK_FRAME | wx.LI_VERTICAL| 1)
        self.separaLine.SetBackgroundColour(wx.Colour(0, 0, 0))
        self.separaLine.SetToolTipString('')
        self.separaLine.SetThemeEnabled(True)
        self.separaLine.SetAutoLayout(True)
        self.separaLine.SetExtraStyle(0)

        self.focheckBox = wx.CheckBox(id=wxID_WXFRAME1FOCHECKBOX,
              label='Full Output', name='focheckBox', parent=self.panel1,
              pos=wx.Point(507, 174), size=wx.Size(208, 22), style=0)
        self.focheckBox.SetValue(True)
        self.focheckBox.SetToolTipString('Check if you want the full output.')

        self.paramText = wx.StaticText(id=wxID_WXFRAME1PARAMTEXT,
              label='Parameters:', name='paramText', parent=self.panel1,
              pos=wx.Point(507, 0), size=wx.Size(88, 16), style=0)

        self.parEdit = wx.TextCtrl(id=wxID_WXFRAME1PAREDIT, name='parEdit',
              parent=self.panel1, pos=wx.Point(507, 16), size=wx.Size(208, 158),
              style=wx.HSCROLL | wx.TE_PROCESS_TAB | wx.VSCROLL | wx.TE_MULTILINE,
              value='')
        self.parEdit.SetToolTipString('Enter parameter values or expressions, one per line. Parameter should be refered to as p[0], p[1],... in the DEs.')
        self.parEdit.SetHelpText('Each line corresponds to one Parameter (p[0], p[1], ...)')

        self.gauge1 = wx.Gauge(id=wxID_WXFRAME1GAUGE1, name='gauge1',
              parent=self.panel1, pos=wx.Point(320, 348), range=100,
              size=wx.Size(153, 22), style=wx.GA_HORIZONTAL,
              validator=wx.DefaultValidator)
        self.gauge1.SetLabel('Percent Done')
        self.gauge1.SetValue(0)
        self.gauge1.SetToolTipString('Percent Done')
        self.gauge1.SetHelpText('Show the percentage of the simulation done.')
        self.gauge1.SetShadowWidth(10)
        self.gauge1.SetBezelFace(1)

        self.eqnEdit = wx.TextCtrl(id=wxID_WXFRAME1EQNEDIT, name='eqnEdit',
              parent=self.panel1, pos=wx.Point(0, 24), size=wx.Size(477, 158),
              style=wx.HSCROLL | wx.TE_PROCESS_TAB | wx.TE_MULTILINE, value='')
        self.eqnEdit.SetToolTipString('ODE box. Enter one equation per line. Right hand side only.')

        self.eqText = wx.StaticText(id=wxID_WXFRAME1EQTEXT,
              label='Differential Equations:', name='eqText',
              parent=self.panel1, pos=wx.Point(0, 0), size=wx.Size(465, 22),
              style=0)
        self.eqText.SetThemeEnabled(True)
        self.eqText.SetBestFittingSize(wx.Size(465, 22))

        self.initValText = wx.StaticText(id=wxID_WXFRAME1INITVALTEXT,
              label='Initial values:', name='initValText', parent=self.panel1,
              pos=wx.Point(0, 184), size=wx.Size(465, 22), style=0)
        self.initValText.SetBestFittingSize(wx.Size(465, 22))

        self.minStepText = wx.StaticText(id=wxID_WXFRAME1MINSTEPTEXT,
              label='Min. Step Size:', name='minStepText', parent=self.panel1,
              pos=wx.Point(0, 324), size=wx.Size(153, 22), style=0)

        self.progressText = wx.StaticText(id=wxID_WXFRAME1PROGRESSTEXT,
              label='Progress:', name='progressText', parent=self.panel1,
              pos=wx.Point(320, 324), size=wx.Size(153, 22), style=0)

        self.convcheckBox = wx.CheckBox(id=wxID_WXFRAME1CONVCHECKBOX,
              label='Show Convergence Message', name='convcheckBox',
              parent=self.panel1, pos=wx.Point(507, 323), size=wx.Size(208, 22),
              style=0)
        self.convcheckBox.SetValue(False)
        self.convcheckBox.SetToolTipString('Check if you want the convergence message to be displayed.')
        self.convcheckBox.Enable(True)
        self.convcheckBox.SetThemeEnabled(True)

        self._init_coll_toolBar1_Tools(self.toolBar1)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.FileName = None
        self.ModLoaded = None #no model has been loaded
        self.Neq = None
        self.modict = {'modelname':''}
        self.modtree = Model() #Object containing dom tree with model specification
        self.modbuilt = 0 #model has never been built
        self.modRan = 0 #model has never been ran
        self.plot = 0 #no plot has ever been generated
        self.gauge1.SetRange(100)
        self.uncertainty = 0 # uncertainty analysis is off
        self.ceqs = [] #compiled equations
        self.cpars = [] #compiled parameters
        self.curdir = ''


    def OnMenu1items0Menu(self, event):
        """
        Load a model file.
        """
        self.modtree = Model() #reset the modtree object upon the opening of a new model
        if self.curdir:
            os.chdir(self.curdir)
        dlg = wx.FileDialog(self, "Choose a file", self.curdir, "", "*.ode", wx.OPEN)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                a = self.modtree.readFile(filename)
                if a==1: #model is not a xml file
                    f = open(filename)
                    self.modict = pickle.load(f)
                    self.fillGui()
                    self.FileName = filename
                    self.SetTitle('Model Builder - '+filename)
                    f.close()
                elif a == 2:
                    return
                else:
                    self.modict.update(self.modtree.modict)
                    self.fillGui()
                    self.FileName = filename
                    self.SetTitle('Model Builder - '+os.path.split(filename)[1])
                    #load data
                    try:
                        d = pickle.load(self.checkExt(filename,'.dat'))
                        self.modict['results']=d[0]
                        self.modict['trange']=d[1]
                    except: pass
                self.ModLoaded = 1
                self.curdir = os.path.split(filename)[0]
        finally:
            self.statusBar1.SetStatusText('Model loaded.')
            #print self.modict.keys()
            dlg.Destroy()

    def fillGui(self):
        """
        fill GUI from model dictionary
        """
        self.eqnEdit.SetValue(self.modict['equations'])
        self.textCtrl2.SetValue(str(self.modict['start']))
        self.textCtrl3.SetValue(str(self.modict['end']))
        self.textCtrl4.SetValue(str(self.modict['step']))
        self.initValInput.SetValue(str(self.modict['init']))
        self.parEdit.SetValue(self.modict['parameters'])
        self.BuildModel(r=0) # call BuildModel without running the model

    def checkExt(self,fn,ext):
        """
        Add a filename extension to the filename or replace
        original extension.
        fn: file name
        ext: desired extension
        """
        head,tail = os.path.split(fn)
        orext = '.'+tail.split('.')[-1]
        if not orext:
            tail += ext
        if orext and (ext != orext):
            tail = tail.replace(orext,ext)
        return head+tail


    def OnMenu1items1Menu(self, event):
        """
        Save model.
        """
        self.modtree = Model()
        if not self.FileName:
            return self.OnMenu1items2Menu(event)
        else:
            #print self.modict.keys()
            filename = os.path.split(self.FileName)[1]
            self.modict['modelname'] = filename
            self.modtree.path = self.curdir
            self.BuildModel(r=0) # call BuildModel without running the model
            #save data
            if self.modict.has_key('results')and self.modict.has_key('trange'):
                fna = open(self.checkExt(filename,'.dat'),'w')
                pickle.dump((self.modict['results'], self.modict['trange']),fna)
                fna.close()
            # Saving in xml format
            try:
                self.modict.pop('results')
                self.modict.pop('trange')
            except: pass
            self.modtree.Create(**self.modict)
            self.modtree.saveFile(self.checkExt(filename,'.ode'))
            self.ModLoaded = 1
            self.statusBar1.SetStatusText('Model saved.')
            #print self.modict.keys()


    def OnMenu1items2Menu(self, event):
        """
        Save as menu entry
        """
        self.modtree = Model()
        dlg = wx.FileDialog(self, "Save File As", self.curdir, "", "*.ode", wx.SAVE)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                self.FileName = dlg.GetPath()
                filename = os.path.split(self.FileName)[-1]
                self.modtree.path = self.curdir = os.path.split(self.FileName)[0]
                if not filename.endswith('.ode'):
                    filename += '.ode'
                if not self.modbuilt: #check to see if model's dictionary has been built
                    self.BuildModel(r=0) # call BuildModel without running the model
                    self.modict['modelname'] = filename
                self.SetTitle('Model Builder - '+filename)
                #save data
                if self.modict.has_key('results'):
                    os.chdir(self.curdir)
                    fna = open(self.checkExt(filename,'.dat'),'w')
                    pickle.dump((self.modict['results'], self.modict['trange']),fna)
                    fna.close()
                # Saving in xml format
                #print self.modict.keys()
                try:
                    self.modict.pop('results')
                    self.modict.pop('trange')
                except:
                    pass
                self.modtree.Create(**self.modict)
                self.modtree.saveFile(self.checkExt(filename,'.ode'))
                self.ModLoaded = 1
                self.statusBar1.SetStatusText('Model saved.')

        finally:
            dlg.Destroy()


    def OnMenu1items3Menu(self, event):
        """
        Close model menu entry.
        """
        self.modtree = Model()
        self.FileName = None
        self.ModLoaded = None
        self.modict = {}
        self.eqnEdit.SetValue('')
        self.textCtrl2.SetValue('')
        self.textCtrl3.SetValue('')
        self.textCtrl4.SetValue('')
        self.initValInput.SetValue('')
        self.parEdit.SetValue('')
        self.SetTitle('Model Builder - ODE')


    def OnMenu1items4Menu(self, event):
        """
        Exit the program
        """

        self.Close()
        self.Destroy()


    def OnMenu2items0Menu(self, event):
        """
        If Uncertainty Analysis has been selected, it will Open the uncertainty panel to set
        the parameters for the analysis.
        """
        if not self.ModLoaded:
            self.OnMenu1items0Menu(event)
            return
        #--Checks if the Uncertainty item in the analysis menu (2) has been checked------

        if self.menu2.IsChecked(wxID_WXFRAME1MENU2ITEMS0):
            self.uncertaintyPanel = LHS(None)
            #self.uncertaintyPanel=uncertaintyMiniFrame.create(None)
            self.initUncertainty()
            self.uncertaintyPanel.Show()
            self.uncertainty = 1 #raises uncertainty flag
        else:
            self.uncertainty = 0 #Uncertainty mode off
            try:
                self.uncertaintyPanel.Close()
            except:
                pass


    def checkErrors(self):
        """
        Check for normal editing errors in model definition
        """
        while not self.eqnEdit.GetValue()=='':
            val = self.eqnEdit.GetValue().strip().splitlines()
            Neq = len(val)
            break
##~         Neq = int(self.eqnEdit.GetNumberOfLines()) #get number of ODEs
##~         while strip(self.eqnEdit.GetLineText(Neq-1)) == '': # avoid getting empty lines at the end of the eq. box
##~             Neq = Neq-1
##~             if Neq == 1:nty
##~                 break

        if not self.parEdit.GetValue() == '':
            valp = self.parEdit.GetValue().strip().splitlines()
            Npar = len(valp)
        else:
            Npar = 0

##~         Npar = int(self.parEdit.GetNumberOfLines()) #get Number of  Parameters
##~         while strip(self.parEdit.GetLineText(Npar-1)) == '':# avoid getting empty lines at the end of the eq. box
##~             Npar=Npar-1
##~             if Npar == 1:
##~                 break
##~             if Npar == 0:
##~                 Npar = 1
##~                 break

#---Check number of initial conditions----------------------------------------------------------------------------
        if self.initValInput.GetValue().strip() == '':
            ni = 0
        else:
            ni = len(self.initValInput.GetValue().strip().split(' '))
        if not ni == Neq:
            return 1

#---Check that all initial conditions are numbers----------------------------------------------------------------------------
        for i in xrange(ni):
            try:
                float(self.initValInput.GetValue().strip().split(' ')[i])
            except ValueError:
                e = 2
                return 2
#---Check syntax of equations----------------------------------------------------------------------------
        y=zeros(Neq)#fake equation array
        p=zeros(Npar)#fake parameter array
        t=0
        eqs = self.eqnEdit.GetValue().strip().split('\n')
        eql = [i.split('=')[-1] for i in self.eqnEdit.GetValue().strip().split('\n')]
        vnames = [i.split('=')[0] for i in self.eqnEdit.GetValue().strip().split('\n') if '=' in i]
        pnames = [i.split('=')[0] for i in self.parEdit.GetValue().strip().splitlines() if '=' in i]
        #replace ocurrences of variable names in equations.
        if vnames:
            #create fake variables
            for v in vnames:
                exec('%s = 0'%v)
        #replace occurrences of parameter names in equations by p[i]
        if pnames:
            for p in pnames:
                exec('%s = 0'%p)
        for k in eql:
            try:
                eval(k) #dy(k)/dt
            except (SyntaxError, NameError), details:
                print details
                return 3

        return 0


    def OnToolbar1tools0Tool(self, event):
        """
        Run button event.
        Call BuildModel with r=1, and times how long the model takes to run.
        The time elapsed is shown in a message dialog.
        """
        if not self.ModLoaded:
            if self.eqnEdit.GetValue().strip() == '':
                return self.OnMenu1items0Menu(event)
#----compile equations and store for later use
        eql = [i.split('=')[-1] for i in self.eqnEdit.GetValue().strip().split('\n')]
        self.vnames = [v.split('=')[0] for v in self.eqnEdit.GetValue().strip().split('\n') if '=' in v]

        try:
            self.ceqs = [compile(i,'<string>','eval') for i in eql]
        except SyntaxError:
            dlg = wx.MessageDialog(self, 'There is a syntax error in the Equation Box.\nPlease fix it and try again.',
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
        self.compilePars()
#---checking for errors----------------------------------------------------------------------------
        e = self.checkErrors()
        if e == 1:
            dlg = wx.MessageDialog(self, 'Wrong number of initial condition values.',
              'Initial Condition Error', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
            return
        elif e == 2:
            dlg = wx.MessageDialog(self, 'There is a syntax error on the initial conditions box.',
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
            return
        elif e==3:
            dlg = wx.MessageDialog(self, 'There is a syntax error in the Equation Box.\nPlease fix it and try again.',
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
        else:
            pass
        self.modbuilt = 0
        self.updateGauge(0)
        if self.uncertainty == 0:
            self.BuildModel(r=1) #regular run
            self.modRan = 1 #set the flag indicating model has been run
        else:
            if self.uncertaintyPanel.Done:
                self.statusBar1.SetStatusText('Simulation Started!\n')
                self.BuildModel(r=1) # Melding setup
                self.modRan = 1 #set the flag indicating model has been run
            else:
                dlg = wx.MessageDialog(self, 'Set the parameters for the Uncertainty Analysis\nin its panel and press the "OK" button',
                  'Uncertain Parameters', wx.OK | wx.ICON_INFORMATION)
                try:
                    dlg.ShowModal()
                finally:
                    dlg.Destroy()



    def OnToolbar1tools0ToolRclicked(self, event):
        event.Skip()

    def updateGauge(self,t):
        """
        This function simply updates the progress gauge
        """
        pd = (100*t)/(float(str(self.textCtrl3.GetValue()).strip())-float(str(self.textCtrl2.GetValue()).strip())) #calcutates percentage done
        self.gauge1.SetValue(int(pd))
        #self.gauge1.SetToolTipString(str(pd)+'%')
        self.gauge1.Refresh() #update the gauge

    def Equations(self, y, t):
        """
        This function defines the system of differential equations, evaluating
        each line of the equation text box as ydot[i]

        returns ydot
        """
        self.updateGauge(t)

        Neq=self.Neq
        Npar = self.Npar
        par = self.par
        #print "vnames:", self.vnames, self.pnames
        if self.vnames:
            #print y,type(y)
            exec('%s=%s'%(','.join(self.vnames),list(y)))

        ydot = zeros((Neq),'d') #initialize ydot
        p = zeros((Npar),'d') #initialize p
    #---Create Parameter Array----------------------------------------------------------------------------

        if self.uncertainty == 0:
            pars = self.cpars
            #par.GetValue().strip().split('\n')
            if pars: #only if there is at least one parameter
                for j in xrange(len(pars)):
                    if self.pnames:
                        exec(pars[j])
                    else:
                        p[j] = eval(pars[j]) #initialize parameter values

        else:
            if Npar:
                for i in xrange(Npar):
                    if self.pnames:
                        exec("%s=%s"%(self.pnames[i],par[i][self.run]))
                    else:
                        p[i] = par[i][self.run] # Get a new set of parameters for each repeated run
    #---Create equation array----------------------------------------------------------------------------
        eqs = self.ceqs
        #print par[0][0].shape,type(par[0][0]),par[0].shape,type(par[0]),type(par[0])
        for k in xrange(Neq):
            ydot[k] = eval(eqs[k]) #dy(k)/dt

        return ydot

    def compilePars(self):
        """
        compile parameter expressions
        """
        if self.parEdit.GetValue().strip() =="":
            self.cpars = []
            return
        pars = self.parEdit.GetValue().strip().splitlines()
        self.pnames = [p.split('=')[0] for p in pars if '=' in p]
        if self.pnames:
            #in this case returns the compete expression, including the '='
            #print pars
            try:
                self.cpars = [compile(i,'<parameter>','exec') for i in pars]
            except (SyntaxError),details:
                self.cpars =pars
                dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box:\n%s\nPlease fix it and try again.'%details,
                  'Syntax Error', wx.OK | wx.ICON_INFORMATION)
                try:
                    dlg.ShowModal()
                finally:
                    dlg.Destroy()
        else:
            try:
                self.cpars = [compile(i,'<parameter>','eval') for i in pars]
            except SyntaxError:
                dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box.\nPlease fix it and try again.',
                  'Syntax Error', wx.OK | wx.ICON_INFORMATION)
                try:
                    dlg.ShowModal()
                finally:
                    dlg.Destroy()


    def OnToolbar1tools1Tool(self, event):
        """
        Show a figure with the equations typeset
        """
#---translate equations to pseudo-TeX notation----------------------------------------------------------------------------
        texdict = {
        '**':r'^','sqrt':r'\sqrt','[':r'_{',']':r'}',
        'sin':r'\rm{sin}','cos':r'\rm{cos}',
        'alpha':r'\alpha ','beta ':r'\beta ','gamma':r'\gamma '
        } #Can be extended to acomodate new strings
        texdict2 = {'*':r'\times '}# to take care of the multiplication sign *
        xlat = Xlator(texdict)
        xlat2 = Xlator(texdict2)
        if not self.modbuilt:
            self.BuildModel(r=0)
        eq = range(self.Neq) #initialize
        eqs = self.eqnEdit.GetValue().strip().split('\n')
        for i in xrange(self.Neq):
            e = eqs[i].split('=')[-1]
            eq[i] = xlat2.xlat(xlat.xlat(e))
#-------------------------------------------------------------------------------
        #getting variable names
        vnames = [n.split('=')[0].strip() for n in eqs if '=' in n]
        eqlist=[]
        for i in xrange(self.Neq):
            if len(vnames)==self.Neq:
                eq[i] = r'$d%s/dt = '%vnames[i] + eq[i] + r'$'
            else:
                eq[i] = r'$dY_%s/dt = '%i + eq[i] +r'$'
            eqlist.append(eq[i])
#---plot equations----------------------------------------------------------------------------
        DP = PF.create(None)
        DP.SetTitle('Model '+str(self.FileName)[:-4]+': Equations')
        DP.plotEquation(eqlist)
        DP.Show()


    def plotOutput(self, x, y):
        """
        Plots the results of a single run.
        Raises the flag that the plot has been created
        """
        self.PF = PF.create(None)
        self.PF.plot_data(x,y,self.vnames)
        self.PF.Show()


        self.plot = 1

    def plotMelding(self,x,y,tit='Results'):
        """
        This function preprocesses the output of the repeated Runs, calls Bayesian Melding run and sends the data to
        PlotFigure.plotStats to be plotted.
        x contains the time values and y is a list of outputs from odeint, one from each run.
        """
        self.statusBar1.SetStatusText('Preparing data for plotting...\n')
        yt = [] # initializes list of transposed runs
        nvar = min(y[0].shape) # Number of Variables
        runlen = max(y[0].shape) #Lenght of runs

        nruns = len(y) # Number of runs

##        SPF = PF.create(None)
##        SPF.plot_data(x,y)
##        SPF.Show()

        self.nruns = nruns
        runs_byvar = [] #List of arrays that will contain all runs for each given variable

        for i in y:
            yt.append(transpose(i)) #Extracts the time series arrays and transpose them (the median function needs to have the series in rows, not in columns).

        for v in xrange(nvar):
            runs_byvar.append(array([yt[i][v] for i in xrange(nruns)]))



        # TODO: Turn medianruns into a function
        medianRuns = [median(i) for i in runs_byvar]
        self.statusBar1.SetStatusText('Done!\n\n')



        #---95% Limits----------------------------------------------------------
        # TODO: Turn calculation of limits into a function
        self.statusBar1.SetStatusText('Calculating credibility intervals...\n')
        sorted=[]
        ll = []
        ul = []
        lc = int(runs_byvar[0].shape[0]*0.025) #column containing the lower boundary for the 95% interval
        hc = int(runs_byvar[0].shape[0]*0.975) #column containing the upper boundary for the 95% interval
        for l in xrange(nvar):
            sorted.append(msort(runs_byvar[l]))
            ll.append(sorted[l][lc])
            ul.append(sorted[l][hc])

        ts = (medianRuns,ll,ul)
        self.statusBar1.SetStatusText('Done!\n\n')

#---testing---------------------------------------------------------------------
##        med = medianRuns[0]-medianRuns[1]
##        SPF = PF.create(None)
##        SPF.plot(x,med)
##        SPF.Show()
#-------------------------------------------------------------------------------


        TP = PF.create(None)
        TP.SetTitle(tit)
        TP.plotStats(x,ts,self.vnames)
        TP.Show()


        self.plot = 1
        return (x,runs_byvar)


    def OnToolbar1tools2Tool(self, event):
        """
        Show Output Table
        """
        if self.modRan:
            self.TableOut = wxFrame2.create(None)
            output = self.modict['results'] # get output from model's dictionary
            x = self.modict['trange']
            y = output[0]
            info = output[1] #extra information dictionary
            infoarray = array([info['hu'],info['tcur'],info['tsw'],info['nqu'],info['mused']]).transpose()
            infoarray = concatenate((zeros((1,5)),infoarray),axis=0)
            #print infoarray.shape, y.shape
            r,c = y.shape # get size of t_course array
            self.TableOut.grid1.CreateGrid(r+1,c+6)

            # Filling the grid
            x.shape = (len(x),1)
            data = concatenate((x,y,infoarray),axis=1)
            self.TableOut.Show()
            self.TableOut.fillGrid(data)
            self.TableOut.table.SetColLabelValue(0,'Time')
            for j in xrange(1,c+1): #fill column labels
                if self.vnames:
                    self.TableOut.table.SetColLabelValue(j,self.vnames[j-1])
                else:
                    self.TableOut.table.SetColLabelValue(j,'y[%s]'%(j-1))
            #Adding info column labels
            clabels = ['Step sizes','Time reached','Last method switch','Method order','Method nused']
            for z in xrange(1,6):
                self.TableOut.table.SetColLabelValue(c+z,clabels[z-1])




    def BuildModel(self, r=0):
        """
        Constructs the model from the input fields and runs it if r==1
        """
        try:
            if self.modict:pass
        except:
            self.modict = {}
        try:
            self.modict['modelname'] = os.path.split(self.FileName)[-1]
        except AttributeError:
            self.modict['modelname'] = ''
        self.modict['equations'] = str(self.eqnEdit.GetValue()) #put equations into model's dictionary
        self.modict['parameters'] = str(self.parEdit.GetValue()) # put parameters into model's dictionary
        t_start = float(str(self.textCtrl2.GetValue()).strip())
        self.modict['start'] = str(self.textCtrl2.GetValue()).strip() #store start time
        t_end = float(str(self.textCtrl3.GetValue()).strip())
        self.modict['end'] = str(self.textCtrl3.GetValue()).strip() #store end time
        t_step = float(str(self.textCtrl4.GetValue()).strip())
        self.modict['step'] = str(self.textCtrl4.GetValue()).strip() # Store step size
        init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != ''])
        self.modict['init'] = str(self.initValInput.GetValue()).strip() # Store initial conditions
        t_range = arange(t_start, t_end+t_step, t_step)
        self.modict['trange'] = t_range
        if self.focheckBox.GetValue():
            fo = 1
        else:
            fo = 0
        if self.convcheckBox.GetValue():
            cm = 1
        else:
            cm = 0
        eqs = self.eqnEdit.GetValue().strip().splitlines()
        self.vnames = [n.split('=')[0].strip() for n in eqs if '=' in n]
        self.pnames = [n.split('=')[0].strip() for n in self.parEdit.GetValue().strip().splitlines() if '=' in n]


        while not self.eqnEdit.GetValue()=='':#get number of ODEs
            val = self.eqnEdit.GetValue().strip().split('\n')
            Neq = len(val)
            break


        self.Neq = Neq#to be used by Equations
        if r==1:
            self.sw = wx.StopWatch()
            self.gauge1.SetValue(0)
            self.sw.Start()
            #-----------------------------------------------------
            if self.uncertainty == 0:#regular (single) run
                if not self.parEdit.GetValue() == '':#get Number of  Parameters
                    valp = self.parEdit.GetValue().strip().split('\n')
                    Npar = len(valp)
                else:
                    Npar = 0

                self.Npar = Npar#to be used by Equations
                self.par = self.parEdit #to be used by Equations
                t_course = integrate.odeint(self.Equations,init_conds,t_range, full_output=fo, printmessg=cm)
                self.plotOutput(t_range,t_course)
                self.modict['results'] = t_course
                self.modRan = 1
            else:
#-------------Multiple runs (uncertainty analysis)------------------------------------
                self.uncRun(init_conds,t_range,cm)


            t = gmtime(self.sw.Time()/1000)

            dlg = wx.MessageDialog(self, 'The simulation was completed in '+str(t[3])+' hours, '+str(t[4])+' minutes and '+str(t[5])+' seconds.',
          'Time Elapsed', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
            if self.uncertainty == 1:
                self.statusBar1.SetStatusText('The simulation was completed in: '+str(t[3])+' hours, '+str(t[4])+' minutes and '+str(t[5])+' seconds.')
#-------------------------------------------------------------------------------

        self.modbuilt = 1

    def uncRun(self, init_conds,t_range,cm):
        """
        Runs the model in uncertainty mode
        """
        Npar  = len(self.uncertaintyPanel.theta)
        nr= int(self.uncertaintyPanel.specs['samples']) # Get K from uncertaity panel
        t_courseList = []

        if self.uncertaintyPanel.anaChoice.GetStringSelection() == 'Uncertainty':
            self.parMeld = self.uncertaintyPanel.priords#[:Npar]
        else: #Sensitivity analysis
            self.parMeld = self.uncertaintyPanel.priords
        self.par = self.parMeld #to be used by Equations
        self.Npar = len(self.par)
        #print 'runs:%s'%nr
        for i in xrange(nr):
            self.run = i
#---tcourselist for multiple runs does not contain the full output of odeint, just the time series--------------
            t_courseList.append(integrate.odeint(self.Equations,init_conds,t_range, full_output=0, printmessg=cm))
            #if self.run%20 == 0:
             #   self.statusBar1.SetStatusText(str(self.run)+'\n')
            init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != '']) #reset initial conditions between runs

        self.modRan = 1
        te = gmtime(self.sw.Time()/1000)#time elapsed so far (first stage of melding)
        if self.uncertaintyPanel.anaChoice.GetStringSelection() == 'Uncertainty':
            self.statusBar1.SetStatusText('First stage of simulation completed in:'+str(te[3])+' hours, '+str(te[4])+' minutes and '+str(te[5])+' seconds.'+'\n')
            (x,runs_byvar) = self.plotMelding(t_range,t_courseList,tit='Median Runs and 95 percent intervals. n=%s'%nr)
            self.Bmeld(x,runs_byvar) # Perform the rest of the melding calculations
            self.modict['meldruns'] = t_courseList
        else: #Sensitivity Analysis
            self.statusBar1.SetStatusText('Simulations completed in:'+str(te[3])+' hours, '+str(te[4])+' minutes and '+str(te[5])+' seconds.'+'\n')
            (x,runs_byvar) = self.plotMelding(t_range,t_courseList,tit='Median Runs and 95 percent intervals. n=%s'%nr)


    def Bmeld(self,t,ModOut):
        """
        Performs the Melding.
        t is an array of time values.
        Modout is a list of n arrays of time courses where n is the number of variables.
        """
        nvar = len(ModOut)
        dlg = wx.TextEntryDialog(self, 'for which time do you want to run the Melding', 'Choose Time', str(t[-1]))
        try:
            if dlg.ShowModal() == wx.ID_OK:
                answer = dlg.GetValue()
                if answer == '':
                    time = t[-1]
                else:
                    time  = eval(answer)
        finally:
            dlg.Destroy()


        Phis = [ModOut[i][:,int(time)] for i in xrange(nvar)] # list with all the specified values for each variable
        q2phis = self.uncertaintyPanel.priords[-nvar:] # priors (pre-model) for the phis
        q1thetas = tuple(self.uncertaintyPanel.priords[:-nvar]) # priors for the thetas (parameters)
        plimits = self.uncertaintyPanel.priors[1][-nvar:] #limits of the prior distributions of the phis
        ptype = self.uncertaintyPanel.priors[0][:-nvar] #types of the prior distributions
        lik = self.uncertaintyPanel.lhoods
        alpha = self.uncertaintyPanel.alpha
        L = int(self.nruns*0.1)
#---Run SIR----------------------------------------------------------------------------
#---meldout=(w, post_theta, qtilphi, q1est)----------------------------------------------------------------------------
        meldout = meld.SIR(alpha,q2phis,plimits,ptype, q1thetas,Phis,L,lik) # output of meld.SIR: (w,qtiltheta,qtilphi,q1est)
#---If SIR fails, don't proceed----------------------------------------------------------------------------
        if meldout == None:
            return


#---Calculate posterior of phis-----------------------------------------------------
        (x,post_phi) = self.postPHI(meldout, L)
#---Plotting results---------------------------------------------------------------------------
        nplots = len(self.uncertaintyPanel.priords)
        allpriors = self.uncertaintyPanel.priords
        nvp = self.uncertaintyPanel.theta + self.uncertaintyPanel.phi# Names of variable + parameters in the model)
        nlik = len (lik) # Get number of likelihood functions
        vname = ['prior of %s' % i for i in nvp]

        DP = PF.create(None)
        DP.SetTitle('Prior distributions for the parameters')
        DP.plotDist(allpriors,vname)
        DP.Show()

#---Plot posteriors of theta----------------------------------------------------------------------------
        MP = PF.create(None)
        MP.SetTitle('Theta posteriors')
        MP.plotMeldout(meldout, self.pnames)
        MP.Show()
#---Plot posteriors of phi----------------------------------------------------------------------------
# TODO: extract the values of post_phi at time time for each variable to plot.
        self.plotMelding(x,post_phi,tit='Series After Melding Calibration')
        yt = []
        runs_byvar = []
        for i in post_phi:
            yt.append(transpose(i)) #Extracts the time series arrays and transpose them (the median function needs to have the series in rows, not in columns).

        for v in xrange(nvar):
            runs_byvar.append(array([yt[i][v] for i in xrange(L)]))
        data = [runs_byvar[i][:,-1] for i in xrange(nvar)]
        #vname2 = ['v[%s]' % i for i in xrange(nvar)]
        MP = PF.create(None)
        MP.SetTitle('Phi posteriors at time t='+str(time))
        MP.plotDist(data,self.vnames)
        MP.Show()

    def postPHI(self,meldout,L):
        """
        this function takes the output of the SIR algorithm and calculates the posterior
        distributions of the Phis from the posteriors of the thetas.
        """
        self.post_theta = meldout[1]
        init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != ''])
        t_start = float(self.modict['start'])
        t_end = float(self.modict['end'])
        t_step = float(self.modict['step'])
        t_range = arange(t_start, t_end+t_step, t_step)
        t_courseList = []
        for i in xrange(L):
            self.run = i
            t_courseList.append(integrate.odeint(self.Equations2,init_conds,t_range, full_output=0, printmessg=0))
            #self.statusBar1.SetStatusText(str(self.run)+'\n')
            init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != '']) #reset initial conditions between runs

        return (t_range,t_courseList)
    def Equations2(self,y,t):
        """
        Variation of the Equations function to calculate the posterior of phi
        """
        Neq = self.Neq
        pars = self.post_theta
        Npar = len(pars) #get number of parameters
        if self.vnames:
            exec('%s=%s'%(','.join(self.vnames),list(y)))
        ydot = zeros((Neq),'d') #initialize ydot
        p = zeros((Npar),'d') #initialize p
    #---Create Parameter Array----------------------------------------------------------------------------
        for i in xrange(Npar):
            if self.pnames:
                exec("%s=%s"%(self.pnames[i],pars[i][self.run]))
            else:
                p[i] = pars[i][self.run] # Get a new set of parameters for each repeated run
    #---Create equation array----------------------------------------------------------------------------
        eqs = self.ceqs#self.eqnEdit.GetValue().strip().split('\n')
        for k in xrange(Neq):
            ydot[k] = eval(eqs[k]) #dy(k)/dt


        return ydot





    def OnMenu3items0Menu(self, event):
        """
        Opens the about window
        """
        dlg = about.wxDialog1(self)
        try:
            dlg.ShowModal()
        finally:
            dlg.Destroy()

    def initUncertainty(self):
        """
        Initializes the values on the uncertainty Analysis Panel based on
        model specification
        """

        while not self.eqnEdit.GetValue()=='':#get number of ODEs
            val = self.eqnEdit.GetValue().strip().split('\n')
            Neq = len(val)
            break

        if not self.parEdit.GetValue() == '':#get number of parameters
            valp = self.parEdit.GetValue().strip().split('\n')
            Npar = len(valp)
        else:
            Npar = 0

        if self.vnames:
            phi = self.vnames
        else:
            phi = ["Y[%s]" % str(i) for i in xrange(Neq)]

        if Npar > 0:
            if self.pnames:
                theta = self.pnames
            else:
                theta = ['P[%s]' % str(i) for i in xrange(Npar)]

        self.uncertaintyPanel.phi = phi
        self.uncertaintyPanel.theta = theta
        self.uncertaintyPanel.grid_1.CreateGrid(Npar+Neq,Npar+Neq)
        self.uncertaintyPanel.parsCB.AppendItems(theta)
        #self.uncertaintyPanel.createVarList(items) # re-create varList on uncertaintyMiniFrame
        self.uncertaintyPanel.fileName = self.FileName #create local variable on Uncertainty panel with filename
        dir,fname = os.path.split(self.FileName)#[1]
        fname = fname[:-4]+'_unc.spec'
        if fname in os.listdir(dir):
            self.uncertaintyPanel.loadSpecs(fname)

    def OnWxFrame1Close(self, event):
        """
        Things to do before Exiting.
        """
        # Tries to close other windows if they exist
        try:
            DP.Close()
        except (NameError, AttributeError):
            pass
        try:
            self.PF.Close()
        except (NameError, AttributeError):
            pass
        try:
            MP.Close()
        except (NameError, AttributeError):
            pass
        try:
            self.TableOut.Close()
        except (AttributeError, NameError):
            pass
        try:
            self.uncertaintyPanel.Close()
        except (AttributeError, NameError):
            pass
        sys.exit()

    def OnMenu2Items1Menu(self, event):
        """
        Call the quiver plot menu
        """
        if not self.ModLoaded:
            dlg = wx.MessageDialog(self, 'Please Open or Create a Model First.',
              'No Model Available', wx.OK | wx.ICON_INFORMATION)
            try:
                dlg.ShowModal()
            finally:
                dlg.Destroy()
                return


        if not self.modbuilt:
            self.BuildModel()
        if self.vnames:
            QF.ch = self.vnames
        else:
            QF.ch = ['y[%s]'%i for i in xrange(self.Neq)]
        #print self.Neq, self.FileName
        Vector = QF.create(None)
        Vector.modict = self.modict
        Vector.initsCtrl.SetValue(self.modict['init'])
        inits = array([float(i) for i in self.modict['init'].strip().split(' ')])
        Vector.limitsCtrl.SetValue("%s %s %s %s"%(min(inits),min(inits)*10,min(inits),min(inits)*10))
        Vector.Show()

    def OnToolBar1OpentoolTool(self, event):
        return self.OnMenu1items0Menu(event)

    def OnToolBar1SavetoolTool(self, event):
        return self.OnMenu1items1Menu(event)

    def OnMenu3MbhelpMenu(self, event):
        helpviewer.main(['','/usr/share/model-builder/help/MB.hhp'])

class Future:
    """
    By David Perry - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/84317
    To run a function in a separate thread, simply put it in a Future:

    >>> A=Future(longRunningFunction, arg1, arg2 ...)

    It will continue on its merry way until you need the result of your function.
    You can read the result by calling the Future like a function, for example:

    >>> print A()

    If the Future has completed executing, the call returns immediately.
    If it is still running, then the call blocks until the function completes.
    The result of the function is stored in the Future, so subsequent calls to
    it return immediately.

    A few caveats:
    Since one wouldn't expect to be able to change the result of a function,
    Futures are not meant to be mutable. This is enforced by requiring the
    Future to be "called", rather than directly reading __result. If desired,
    stronger enforcement of this rule can be achieved by playing
    with __getattr__ and __setattr__.

    The Future only runs the function once, no matter how many times you
    read it. You will have to re-create the Future if you want to re-run your
    function; for example, if the function is sensitive to the time of day.

    For more information on Futures, and other useful parallel programming
    constructs, read Gregory V. Wilson's _Practical Parallel Programming_.
    """
    def __init__(self,func,*param):
        # Constructor
        self.__done=0
        self.__result=None
        self.__status='working'

        self.__C=Condition()   # Notify on this Condition when result is ready

        # Run the actual function in a separate thread
        self.__T=Thread(target=self.Wrapper,args=(func,param))
        self.__T.setName("FutureThread")
        self.__T.start()

    def __repr__(self):
        return '<Future at '+hex(id(self))+':'+self.__status+'>'

    def __call__(self):
        self.__C.acquire()
        while self.__done==0:
            self.__C.wait()
        self.__C.release()
        # We deepcopy __result to prevent accidental tampering with it.
        a=copy.deepcopy(self.__result)
        return a

    def Wrapper(self, func, param):
        # Run the actual function, and let us housekeep around it
        self.__C.acquire()
        try:
            self.__result=func(*param)
        except:
            self.__result="Exception raised within Future"
        self.__done=1
        self.__status=`self.__result`
        self.__C.notify()
        self.__C.release()