~ubuntu-branches/ubuntu/karmic/model-builder/karmic

« back to all changes in this revision

Viewing changes to model_builder/wxFrame1.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-04-10 17:05:04 UTC
  • Revision ID: james.westby@ubuntu.com-20070410170504-y884ntvt656218me
Tags: upstream-0.4.0
ImportĀ upstreamĀ versionĀ 0.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding:latin-1 -*-
 
2
#-----------------------------------------------------------------------------
 
3
# Name:        wxFrame1.py 
 
4
# Purpose:
 
5
#
 
6
# Author:      <Flavio Codeco Coelho>
 
7
#
 
8
# Created:     2003/02/04
 
9
# RCS-ID:      $Id: wxFrame1.py,v 1.11 2004/01/13 10:51:43 fccoelho Exp $
 
10
# Copyright:   (c) 2003 Flavio Codeco Coelho <fccoelho@fiocruz.br>
 
11
# Licence:     This program is free software; you can redistribute it and/or
 
12
# modify it under the terms of the GNU General Public License
 
13
# as published by the Free Software Foundation; either version 2
 
14
# of the License, or (at your option) any later version.
 
15
#
 
16
# This program is distributed in the hope that it will be useful,
 
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
# GNU General Public License for more details.
 
20
#
 
21
# You should have received a copy of the GNU General Public License
 
22
# along with this program; if not, write to the Free Software
 
23
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
24
#
 
25
#-----------------------------------------------------------------------------
 
26
#Boa:Frame:wxFrame1
 
27
from __future__ import division
 
28
import wxversion
 
29
wxversion.select('2.6')
 
30
import wx
 
31
import wx.stc
 
32
#from Numeric import *
 
33
from threading import *
 
34
from scipy import integrate
 
35
#from RandomArray import *
 
36
from numpy.random import *
 
37
 
 
38
import pickle
 
39
#from MLab import *
 
40
import matplotlib
 
41
matplotlib.use('WXAgg')
 
42
from pylab import *
 
43
from matplotlib.cbook import *
 
44
from wxPython.tools import helpviewer
 
45
#from xml.dom import Node
 
46
 
 
47
import os,sys
 
48
import wxFrame2, about
 
49
 
 
50
 
 
51
from odexml import Model
 
52
import PlotFigure as PF
 
53
from Bayes import Melding as meld
 
54
from time import *
 
55
import icones
 
56
import quivVarFrame as QF
 
57
from lhsframe import LHS
 
58
 
 
59
 
 
60
os.chdir(os.getcwd())
 
61
 
 
62
def create(parent):
 
63
    return wxFrame1(parent)
 
64
 
 
65
[wxID_WXFRAME1, wxID_WXFRAME1CONVCHECKBOX, wxID_WXFRAME1CRITTIMETEXT, 
 
66
 wxID_WXFRAME1ENDTEXT, wxID_WXFRAME1EQNEDIT, wxID_WXFRAME1EQTEXT, 
 
67
 wxID_WXFRAME1FIRSTSTEPTEXT, wxID_WXFRAME1FOCHECKBOX, wxID_WXFRAME1GAUGE1, 
 
68
 wxID_WXFRAME1INITVALINPUT, wxID_WXFRAME1INITVALTEXT, 
 
69
 wxID_WXFRAME1MAXSTEPTEXT, wxID_WXFRAME1MINSTEPTEXT, wxID_WXFRAME1PANEL1, 
 
70
 wxID_WXFRAME1PARAMTEXT, wxID_WXFRAME1PAREDIT, wxID_WXFRAME1PROGRESSTEXT, 
 
71
 wxID_WXFRAME1SEPARALINE, wxID_WXFRAME1STARTTEXT, wxID_WXFRAME1STATUSBAR1, 
 
72
 wxID_WXFRAME1TEXTCTRL2, wxID_WXFRAME1TEXTCTRL3, wxID_WXFRAME1TEXTCTRL4, 
 
73
 wxID_WXFRAME1TEXTCTRL5, wxID_WXFRAME1TEXTCTRL6, wxID_WXFRAME1TEXTCTRL7, 
 
74
 wxID_WXFRAME1TEXTCTRL8, wxID_WXFRAME1TIMESTEPTEXT, wxID_WXFRAME1TOOLBAR1, 
 
75
] = [wx.NewId() for _init_ctrls in xrange(29)]
 
76
 
 
77
[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2, 
 
78
 wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4, 
 
79
] = [wx.NewId() for _init_coll_menu1_Items in xrange(5)]
 
80
 
 
81
[wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1, 
 
82
 wxID_WXFRAME1TOOLBAR1TOOLS2, 
 
83
] = [wx.NewId() for _init_coll_toolBar1_Tools in xrange(3)]
 
84
 
 
85
[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL, 
 
86
] = [wx.NewId() for _init_coll_menu2_Items in xrange(2)]
 
87
 
 
88
[wxID_WXFRAME1MENU3ITEMS0] = [wx.NewId() for _init_coll_menu3_Items in xrange(1)]
 
89
 
 
90
[wxID_WXFRAME1TOOLBAR1OPENTOOL, wxID_WXFRAME1TOOLBAR1SAVETOOL, 
 
91
 wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1, 
 
92
 wxID_WXFRAME1TOOLBAR1TOOLS2, 
 
93
] = [wx.NewId() for _init_coll_toolBar1_Tools in range(5)]
 
94
 
 
95
[wxID_WXFRAME1, wxID_WXFRAME1CONVCHECKBOX, wxID_WXFRAME1CRITTIMETEXT, 
 
96
 wxID_WXFRAME1ENDTEXT, wxID_WXFRAME1EQNEDIT, wxID_WXFRAME1EQTEXT, 
 
97
 wxID_WXFRAME1FIRSTSTEPTEXT, wxID_WXFRAME1FOCHECKBOX, wxID_WXFRAME1GAUGE1, 
 
98
 wxID_WXFRAME1INITVALINPUT, wxID_WXFRAME1INITVALTEXT, 
 
99
 wxID_WXFRAME1MAXSTEPTEXT, wxID_WXFRAME1MINSTEPTEXT, wxID_WXFRAME1PANEL1, 
 
100
 wxID_WXFRAME1PARAMTEXT, wxID_WXFRAME1PAREDIT, wxID_WXFRAME1PROGRESSTEXT, 
 
101
 wxID_WXFRAME1SEPARALINE, wxID_WXFRAME1STARTTEXT, wxID_WXFRAME1STATUSBAR1, 
 
102
 wxID_WXFRAME1TEXTCTRL2, wxID_WXFRAME1TEXTCTRL3, wxID_WXFRAME1TEXTCTRL4, 
 
103
 wxID_WXFRAME1TEXTCTRL5, wxID_WXFRAME1TEXTCTRL6, wxID_WXFRAME1TEXTCTRL7, 
 
104
 wxID_WXFRAME1TEXTCTRL8, wxID_WXFRAME1TIMESTEPTEXT, wxID_WXFRAME1TOOLBAR1, 
 
105
] = [wx.NewId() for _init_ctrls in range(29)]
 
106
 
 
107
[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL, 
 
108
] = [wx.NewId() for _init_coll_menu2_Items in range(2)]
 
109
 
 
110
[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2, 
 
111
 wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4, 
 
112
] = [wx.NewId() for _init_coll_menu1_Items in range(5)]
 
113
 
 
114
[wxID_WXFRAME1MENU3ITEMS0, wxID_WXFRAME1MENU3MBHELP, 
 
115
] = [wx.NewId() for _init_coll_menu3_Items in range(2)]
 
116
 
 
117
class wxFrame1(wx.Frame):
 
118
 
 
119
    def _init_coll_boxSizerH_Items(self, parent):
 
120
        # generated method, don't edit
 
121
 
 
122
        parent.AddSizer(self.gridBagSizer1, 0, border=0, flag=wx.EXPAND)
 
123
        parent.AddWindow(self.separaLine, 0, border=0, flag=0)
 
124
        parent.AddSizer(self.boxSizerV2, 0, border=0, flag=wx.EXPAND)
 
125
 
 
126
    def _init_coll_bsPanel_Items(self, parent):
 
127
        # generated method, don't edit
 
128
 
 
129
        parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND)
 
130
 
 
131
    def _init_coll_gridBagSizer1_Items(self, parent):
 
132
        # generated method, don't edit
 
133
 
 
134
        parent.AddWindow(self.eqText, (0, 0), border=0, flag=0, span=(1,3))
 
135
        parent.AddWindow(self.eqnEdit, (1, 0), border=0, flag=wx.EXPAND,
 
136
              span=(1, 3))
 
137
        parent.AddWindow(self.initValText, (2, 0), border=0, flag=0, span=(1,3))
 
138
        parent.AddWindow(self.initValInput, (3, 0), border=0, flag=0, span=(1,
 
139
              3))
 
140
        parent.AddWindow(self.startText, (4, 0), border=0, flag=0, span=(1,1))
 
141
        parent.AddWindow(self.endText, (4, 1), border=0, flag=0, span=(1,1))
 
142
        parent.AddWindow(self.timeStepText, (4, 2), border=0, flag=0, span=(1,
 
143
              1))
 
144
        parent.AddWindow(self.textCtrl2, (5, 0), border=0, flag=0, span=(1, 1))
 
145
        parent.AddWindow(self.textCtrl3, (5, 1), border=0, flag=0, span=(1, 1))
 
146
        parent.AddWindow(self.textCtrl4, (5, 2), border=0, flag=0, span=(1, 1))
 
147
        parent.AddWindow(self.critTimeText, (6, 0), border=0, flag=0, span=(1,
 
148
              1))
 
149
        parent.AddWindow(self.firstStepText, (6, 2), border=0, flag=0, span=(1,
 
150
              1))
 
151
        parent.AddWindow(self.textCtrl5, (7, 0), border=0, flag=0, span=(1, 2))
 
152
        parent.AddWindow(self.textCtrl6, (7, 2), border=0, flag=0, span=(1, 1))
 
153
        parent.AddWindow(self.minStepText, (8, 0), border=0, flag=0, span=(1,1))
 
154
        parent.AddWindow(self.maxStepText, (8, 1), border=0, flag=0, span=(1,1))
 
155
        parent.AddWindow(self.progressText, (8, 2), border=0, flag=0, span=(1,
 
156
              1))
 
157
        parent.AddWindow(self.textCtrl7, (9, 0), border=0, flag=0, span=(1, 1))
 
158
        parent.AddWindow(self.textCtrl8, (9, 1), border=0, flag=0, span=(1, 1))
 
159
        parent.AddWindow(self.gauge1, (9, 2), border=0, flag=0, span=(1, 1))
 
160
 
 
161
    def _init_coll_boxSizerV2_Items(self, parent):
 
162
        # generated method, don't edit
 
163
 
 
164
        parent.AddWindow(self.paramText, 1, border=0, flag=wx.EXPAND)
 
165
        parent.AddWindow(self.parEdit, 7, border=0, flag=wx.EXPAND)
 
166
        parent.AddWindow(self.focheckBox, 1, border=0, flag=wx.EXPAND)
 
167
        parent.AddWindow(self.convcheckBox, 1, border=0, flag=wx.EXPAND)
 
168
 
 
169
    def _init_coll_menuBar1_Menus(self, parent):
 
170
        # generated method, don't edit
 
171
 
 
172
        parent.Append(menu=self.menu1, title='File')
 
173
        parent.Append(menu=self.menu2, title='Analysis')
 
174
        parent.Append(menu=self.menu3, title='Help')
 
175
 
 
176
    def _init_coll_menu3_Items(self, parent):
 
177
        # generated method, don't edit
 
178
 
 
179
        parent.Append(help='Open the main help file',
 
180
              id=wxID_WXFRAME1MENU3MBHELP, kind=wx.ITEM_NORMAL,
 
181
              text='Model Builder Help')
 
182
        parent.AppendSeparator()
 
183
        parent.Append(help='General Information about PyMM',
 
184
              id=wxID_WXFRAME1MENU3ITEMS0, kind=wx.ITEM_NORMAL, text='About')
 
185
        self.Bind(wx.EVT_MENU, self.OnMenu3items0Menu,
 
186
              id=wxID_WXFRAME1MENU3ITEMS0)
 
187
        self.Bind(wx.EVT_MENU, self.OnMenu3MbhelpMenu,
 
188
              id=wxID_WXFRAME1MENU3MBHELP)
 
189
 
 
190
    def _init_coll_menu1_Items(self, parent):
 
191
        # generated method, don't edit
 
192
 
 
193
        parent.Append(help='Open a saved model.', id=wxID_WXFRAME1MENU1ITEMS0,
 
194
              kind=wx.ITEM_NORMAL, text='Open')
 
195
        parent.Append(help='Save your model.', id=wxID_WXFRAME1MENU1ITEMS1,
 
196
              kind=wx.ITEM_NORMAL, text='Save')
 
197
        parent.Append(help='Save your model on another file.',
 
198
              id=wxID_WXFRAME1MENU1ITEMS2, kind=wx.ITEM_NORMAL, text='Save As')
 
199
        parent.Append(help='Close your model.', id=wxID_WXFRAME1MENU1ITEMS3,
 
200
              kind=wx.ITEM_NORMAL, text='Close')
 
201
        parent.Append(help='Exit PyMM.', id=wxID_WXFRAME1MENU1ITEMS4,
 
202
              kind=wx.ITEM_NORMAL, text='Exit')
 
203
        self.Bind(wx.EVT_MENU, self.OnMenu1items0Menu,
 
204
              id=wxID_WXFRAME1MENU1ITEMS0)
 
205
        self.Bind(wx.EVT_MENU, self.OnMenu1items1Menu,
 
206
              id=wxID_WXFRAME1MENU1ITEMS1)
 
207
        self.Bind(wx.EVT_MENU, self.OnMenu1items2Menu,
 
208
              id=wxID_WXFRAME1MENU1ITEMS2)
 
209
        self.Bind(wx.EVT_MENU, self.OnMenu1items3Menu,
 
210
              id=wxID_WXFRAME1MENU1ITEMS3)
 
211
        self.Bind(wx.EVT_MENU, self.OnMenu1items4Menu,
 
212
              id=wxID_WXFRAME1MENU1ITEMS4)
 
213
 
 
214
    def _init_coll_menu2_Items(self, parent):
 
215
        # generated method, don't edit
 
216
 
 
217
        parent.Append(help='Enter uncertainty analysis mode',
 
218
              id=wxID_WXFRAME1MENU2ITEMS0, kind=wx.ITEM_CHECK,
 
219
              text='Uncertainty analysis')
 
220
        parent.Append(help='Generates a State Space quiver plot.',
 
221
              id=wxID_WXFRAME1MENU2QUIVERPL, kind=wx.ITEM_NORMAL,
 
222
              text='State Diagram...')
 
223
        self.Bind(wx.EVT_MENU, self.OnMenu2items0Menu,
 
224
              id=wxID_WXFRAME1MENU2ITEMS0)
 
225
        self.Bind(wx.EVT_MENU, self.OnMenu2Items1Menu,
 
226
              id=wxID_WXFRAME1MENU2QUIVERPL)
 
227
 
 
228
    def _init_coll_toolBar1_Tools(self, parent):
 
229
        # generated method, don't edit
 
230
 
 
231
        parent.DoAddTool(bitmap=icones.getOpenBitmap(),
 
232
              bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME1TOOLBAR1OPENTOOL,
 
233
              kind=wx.ITEM_NORMAL, label='Open',
 
234
              longHelp='Click Here to open a new model.',
 
235
              shortHelp='Open Model')
 
236
        parent.DoAddTool(bitmap=icones.getSaveBitmap(),
 
237
              bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME1TOOLBAR1SAVETOOL,
 
238
              kind=wx.ITEM_NORMAL, label='Save', longHelp='Click here to save.',
 
239
              shortHelp='Save Model')
 
240
        parent.AddSeparator()
 
241
        parent.AddTool(bitmap=icones.getrunBitmap(),
 
242
              id=wxID_WXFRAME1TOOLBAR1TOOLS0, isToggle=False,
 
243
              longHelpString='Start your simulation.',
 
244
              pushedBitmap=wx.NullBitmap, shortHelpString='Start')
 
245
        parent.AddTool(bitmap=icones.getequationsBitmap(),
 
246
              id=wxID_WXFRAME1TOOLBAR1TOOLS1, isToggle=False,
 
247
              longHelpString='Show typeset Equations',
 
248
              pushedBitmap=wx.NullBitmap, shortHelpString='Show equations')
 
249
        parent.AddTool(bitmap=icones.getspreadsheetBitmap(),
 
250
              id=wxID_WXFRAME1TOOLBAR1TOOLS2, isToggle=False,
 
251
              longHelpString='Show table with results',
 
252
              pushedBitmap=wx.NullBitmap, shortHelpString='Results')
 
253
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools0Tool,
 
254
              id=wxID_WXFRAME1TOOLBAR1TOOLS0)
 
255
        self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolbar1tools0ToolRclicked,
 
256
              id=wxID_WXFRAME1TOOLBAR1TOOLS0)
 
257
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools1Tool,
 
258
              id=wxID_WXFRAME1TOOLBAR1TOOLS1)
 
259
        self.Bind(wx.EVT_TOOL, self.OnToolbar1tools2Tool,
 
260
              id=wxID_WXFRAME1TOOLBAR1TOOLS2)
 
261
        self.Bind(wx.EVT_TOOL, self.OnToolBar1OpentoolTool,
 
262
              id=wxID_WXFRAME1TOOLBAR1OPENTOOL)
 
263
        self.Bind(wx.EVT_TOOL, self.OnToolBar1SavetoolTool,
 
264
              id=wxID_WXFRAME1TOOLBAR1SAVETOOL)
 
265
 
 
266
        parent.Realize()
 
267
 
 
268
    def _init_coll_statusBar1_Fields(self, parent):
 
269
        # generated method, don't edit
 
270
        parent.SetFieldsCount(1)
 
271
 
 
272
        parent.SetStatusText(number=0, text='Status')
 
273
 
 
274
        parent.SetStatusWidths([-1])
 
275
 
 
276
    def _init_sizers(self):
 
277
        # generated method, don't edit
 
278
        self.gridBagSizer1 = wx.GridBagSizer(hgap=3, vgap=2)
 
279
        self.gridBagSizer1.SetCols(2)
 
280
        self.gridBagSizer1.SetRows(10)
 
281
        self.gridBagSizer1.SetFlexibleDirection(12)
 
282
        self.gridBagSizer1.SetNonFlexibleGrowMode(1)
 
283
 
 
284
        self.bsPanel = wx.BoxSizer(orient=wx.VERTICAL)
 
285
 
 
286
        self.boxSizerH = wx.BoxSizer(orient=wx.HORIZONTAL)
 
287
 
 
288
        self.boxSizerV2 = wx.BoxSizer(orient=wx.VERTICAL)
 
289
 
 
290
        self._init_coll_gridBagSizer1_Items(self.gridBagSizer1)
 
291
        self._init_coll_bsPanel_Items(self.bsPanel)
 
292
        self._init_coll_boxSizerH_Items(self.boxSizerH)
 
293
        self._init_coll_boxSizerV2_Items(self.boxSizerV2)
 
294
 
 
295
        self.SetSizer(self.bsPanel)
 
296
 
 
297
    def _init_utils(self):
 
298
        # generated method, don't edit
 
299
        self.menu1 = wx.Menu(title='File')
 
300
 
 
301
        self.menu3 = wx.Menu(title='Help')
 
302
 
 
303
        self.menuBar1 = wx.MenuBar()
 
304
 
 
305
        self.menu2 = wx.Menu(title='Analysis')
 
306
 
 
307
        self._init_coll_menu1_Items(self.menu1)
 
308
        self._init_coll_menu3_Items(self.menu3)
 
309
        self._init_coll_menuBar1_Menus(self.menuBar1)
 
310
        self._init_coll_menu2_Items(self.menu2)
 
311
 
 
312
    def _init_ctrls(self, prnt):
 
313
        # generated method, don't edit
 
314
        wx.Frame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
 
315
              pos=wx.Point(362, 472), size=wx.Size(730, 480),
 
316
              style=wx.DEFAULT_FRAME_STYLE, title='Model Builder - ODE')
 
317
        self._init_utils()
 
318
        self.SetClientSize(wx.Size(730, 480))
 
319
        self.SetMenuBar(self.menuBar1)
 
320
        self.SetToolTipString('Model Builder')
 
321
        self.SetAutoLayout(True)
 
322
        self.SetBestFittingSize(wx.Size(730, 480))
 
323
        self.SetMaxSize(wx.Size(730, 480))
 
324
        self.SetIcon(wx.Icon('MB.ico',wx.BITMAP_TYPE_ICO))
 
325
        self.Bind(wx.EVT_CLOSE, self.OnWxFrame1Close)
 
326
 
 
327
        self.statusBar1 = wx.StatusBar(id=wxID_WXFRAME1STATUSBAR1,
 
328
              name='statusBar1', parent=self, style=0)
 
329
        self.statusBar1.SetSize(wx.Size(80, 19))
 
330
        self.statusBar1.SetPosition(wx.Point(-1, -1))
 
331
        self._init_coll_statusBar1_Fields(self.statusBar1)
 
332
        self.SetStatusBar(self.statusBar1)
 
333
 
 
334
        self.toolBar1 = wx.ToolBar(id=wxID_WXFRAME1TOOLBAR1, name='toolBar1',
 
335
              parent=self, pos=wx.Point(0, 31), size=wx.Size(248, 50),
 
336
              style=wx.TRANSPARENT_WINDOW | wx.TB_HORIZONTAL | wx.NO_BORDER)
 
337
        self.toolBar1.SetToolTipString('')
 
338
        self.toolBar1.SetThemeEnabled(True)
 
339
        self.SetToolBar(self.toolBar1)
 
340
 
 
341
        self.panel1 = wx.Panel(id=wxID_WXFRAME1PANEL1, name='panel1',
 
342
              parent=self, pos=wx.Point(0, 0), size=wx.Size(730, 449),
 
343
              style=wx.TAB_TRAVERSAL)
 
344
 
 
345
        self.initValInput = wx.TextCtrl(id=wxID_WXFRAME1INITVALINPUT,
 
346
              name='initValInput', parent=self.panel1, pos=wx.Point(0, 208),
 
347
              size=wx.Size(465, 22), style=0, value='')
 
348
        self.initValInput.SetToolTipString('Initial conditions: values separated by spaces.')
 
349
        self.initValInput.SetBestFittingSize(wx.Size(465, 22))
 
350
 
 
351
        self.startText = wx.StaticText(id=wxID_WXFRAME1STARTTEXT,
 
352
              label='Start time:', name='startText', parent=self.panel1,
 
353
              pos=wx.Point(0, 232), size=wx.Size(153, 20), style=0)
 
354
 
 
355
        self.textCtrl2 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL2,
 
356
              name='textCtrl2', parent=self.panel1, pos=wx.Point(0, 254),
 
357
              size=wx.Size(153, 22), style=0, value='0')
 
358
        self.textCtrl2.SetToolTipString('Time value at the start of simulation')
 
359
 
 
360
        self.endText = wx.StaticText(id=wxID_WXFRAME1ENDTEXT, label='End Time:',
 
361
              name='endText', parent=self.panel1, pos=wx.Point(160, 232),
 
362
              size=wx.Size(153, 20), style=0)
 
363
        self.endText.SetToolTipString('Time value to end simulation')
 
364
 
 
365
        self.textCtrl3 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL3,
 
366
              name='textCtrl3', parent=self.panel1, pos=wx.Point(160, 254),
 
367
              size=wx.Size(153, 22), style=0, value='10')
 
368
        self.textCtrl3.SetToolTipString('Time value to end simulation')
 
369
 
 
370
        self.timeStepText = wx.StaticText(id=wxID_WXFRAME1TIMESTEPTEXT,
 
371
              label='Time Step:', name='timeStepText', parent=self.panel1,
 
372
              pos=wx.Point(320, 232), size=wx.Size(153, 20), style=0)
 
373
 
 
374
        self.textCtrl4 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL4,
 
375
              name='textCtrl4', parent=self.panel1, pos=wx.Point(320, 254),
 
376
              size=wx.Size(153, 22), style=0, value='0.1')
 
377
        self.textCtrl4.SetToolTipString('Time step for the output')
 
378
 
 
379
        self.critTimeText = wx.StaticText(id=wxID_WXFRAME1CRITTIMETEXT,
 
380
              label='Critical Time Steps:', name='critTimeText',
 
381
              parent=self.panel1, pos=wx.Point(0, 278), size=wx.Size(153, 20),
 
382
              style=0)
 
383
 
 
384
        self.textCtrl5 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL5,
 
385
              name='textCtrl5', parent=self.panel1, pos=wx.Point(0, 300),
 
386
              size=wx.Size(309, 22), style=0, value='')
 
387
        self.textCtrl5.SetToolTipString('Time points where integration care should be taken.')
 
388
 
 
389
        self.firstStepText = wx.StaticText(id=wxID_WXFRAME1FIRSTSTEPTEXT,
 
390
              label='First Step:', name='firstStepText', parent=self.panel1,
 
391
              pos=wx.Point(320, 278), size=wx.Size(153, 20), style=0)
 
392
 
 
393
        self.textCtrl6 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL6,
 
394
              name='textCtrl6', parent=self.panel1, pos=wx.Point(320, 300),
 
395
              size=wx.Size(153, 22), style=0, value='0')
 
396
        self.textCtrl6.SetToolTipString('Size of the first step (0=auto)')
 
397
 
 
398
        self.textCtrl7 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL7,
 
399
              name='textCtrl7', parent=self.panel1, pos=wx.Point(0, 348),
 
400
              size=wx.Size(153, 22), style=0, value='0')
 
401
        self.textCtrl7.SetToolTipString('Minimum absolute step size allowed (0=auto).')
 
402
 
 
403
        self.maxStepText = wx.StaticText(id=wxID_WXFRAME1MAXSTEPTEXT,
 
404
              label='Max Step Size:', name='maxStepText', parent=self.panel1,
 
405
              pos=wx.Point(160, 324), size=wx.Size(153, 22), style=0)
 
406
 
 
407
        self.textCtrl8 = wx.TextCtrl(id=wxID_WXFRAME1TEXTCTRL8,
 
408
              name='textCtrl8', parent=self.panel1, pos=wx.Point(160, 348),
 
409
              size=wx.Size(153, 22), style=0, value='0')
 
410
        self.textCtrl8.SetToolTipString('Maximum absolute step size allowed (0=auto)')
 
411
 
 
412
        self.separaLine = wx.StaticLine(id=wxID_WXFRAME1SEPARALINE,
 
413
              name='separaLine', parent=self.panel1, pos=wx.Point(477, 0),
 
414
              size=wx.Size(30, 780),
 
415
              style=wx.MAXIMIZE_BOX | wx.LI_VERTICAL | wx.THICK_FRAME | wx.LI_VERTICAL| 1)
 
416
        self.separaLine.SetBackgroundColour(wx.Colour(0, 0, 0))
 
417
        self.separaLine.SetToolTipString('')
 
418
        self.separaLine.SetThemeEnabled(True)
 
419
        self.separaLine.SetAutoLayout(True)
 
420
        self.separaLine.SetExtraStyle(0)
 
421
 
 
422
        self.focheckBox = wx.CheckBox(id=wxID_WXFRAME1FOCHECKBOX,
 
423
              label='Full Output', name='focheckBox', parent=self.panel1,
 
424
              pos=wx.Point(507, 174), size=wx.Size(208, 22), style=0)
 
425
        self.focheckBox.SetValue(True)
 
426
        self.focheckBox.SetToolTipString('Check if you want the full output.')
 
427
 
 
428
        self.paramText = wx.StaticText(id=wxID_WXFRAME1PARAMTEXT,
 
429
              label='Parameters:', name='paramText', parent=self.panel1,
 
430
              pos=wx.Point(507, 0), size=wx.Size(88, 16), style=0)
 
431
 
 
432
        self.parEdit = wx.TextCtrl(id=wxID_WXFRAME1PAREDIT, name='parEdit',
 
433
              parent=self.panel1, pos=wx.Point(507, 16), size=wx.Size(208, 158),
 
434
              style=wx.HSCROLL | wx.TE_PROCESS_TAB | wx.VSCROLL | wx.TE_MULTILINE,
 
435
              value='')
 
436
        self.parEdit.SetToolTipString('Enter parameter values or expressions, one per line. Parameter should be refered to as p[0], p[1],... in the DEs.')
 
437
        self.parEdit.SetHelpText('Each line corresponds to one Parameter (p[0], p[1], ...)')
 
438
 
 
439
        self.gauge1 = wx.Gauge(id=wxID_WXFRAME1GAUGE1, name='gauge1',
 
440
              parent=self.panel1, pos=wx.Point(320, 348), range=100,
 
441
              size=wx.Size(153, 22), style=wx.GA_HORIZONTAL,
 
442
              validator=wx.DefaultValidator)
 
443
        self.gauge1.SetLabel('Percent Done')
 
444
        self.gauge1.SetValue(0)
 
445
        self.gauge1.SetToolTipString('Percent Done')
 
446
        self.gauge1.SetHelpText('Show the percentage of the simulation done.')
 
447
        self.gauge1.SetShadowWidth(10)
 
448
        self.gauge1.SetBezelFace(1)
 
449
 
 
450
        self.eqnEdit = wx.TextCtrl(id=wxID_WXFRAME1EQNEDIT, name='eqnEdit',
 
451
              parent=self.panel1, pos=wx.Point(0, 24), size=wx.Size(477, 158),
 
452
              style=wx.HSCROLL | wx.TE_PROCESS_TAB | wx.TE_MULTILINE, value='')
 
453
        self.eqnEdit.SetToolTipString('ODE box. Enter one equation per line. Right hand side only.')
 
454
 
 
455
        self.eqText = wx.StaticText(id=wxID_WXFRAME1EQTEXT,
 
456
              label='Differential Equations:', name='eqText',
 
457
              parent=self.panel1, pos=wx.Point(0, 0), size=wx.Size(465, 22),
 
458
              style=0)
 
459
        self.eqText.SetThemeEnabled(True)
 
460
        self.eqText.SetBestFittingSize(wx.Size(465, 22))
 
461
 
 
462
        self.initValText = wx.StaticText(id=wxID_WXFRAME1INITVALTEXT,
 
463
              label='Initial values:', name='initValText', parent=self.panel1,
 
464
              pos=wx.Point(0, 184), size=wx.Size(465, 22), style=0)
 
465
        self.initValText.SetBestFittingSize(wx.Size(465, 22))
 
466
 
 
467
        self.minStepText = wx.StaticText(id=wxID_WXFRAME1MINSTEPTEXT,
 
468
              label='Min. Step Size:', name='minStepText', parent=self.panel1,
 
469
              pos=wx.Point(0, 324), size=wx.Size(153, 22), style=0)
 
470
 
 
471
        self.progressText = wx.StaticText(id=wxID_WXFRAME1PROGRESSTEXT,
 
472
              label='Progress:', name='progressText', parent=self.panel1,
 
473
              pos=wx.Point(320, 324), size=wx.Size(153, 22), style=0)
 
474
 
 
475
        self.convcheckBox = wx.CheckBox(id=wxID_WXFRAME1CONVCHECKBOX,
 
476
              label='Show Convergence Message', name='convcheckBox',
 
477
              parent=self.panel1, pos=wx.Point(507, 323), size=wx.Size(208, 22),
 
478
              style=0)
 
479
        self.convcheckBox.SetValue(False)
 
480
        self.convcheckBox.SetToolTipString('Check if you want the convergence message to be displayed.')
 
481
        self.convcheckBox.Enable(True)
 
482
        self.convcheckBox.SetThemeEnabled(True)
 
483
 
 
484
        self._init_coll_toolBar1_Tools(self.toolBar1)
 
485
 
 
486
        self._init_sizers()
 
487
 
 
488
    def __init__(self, parent):
 
489
        self._init_ctrls(parent)
 
490
        self.FileName = None
 
491
        self.ModLoaded = None #no model has been loaded
 
492
        self.Neq = None
 
493
        self.modict = {'modelname':''}
 
494
        self.modtree = Model() #Object containing dom tree with model specification
 
495
        self.modbuilt = 0 #model has never been built
 
496
        self.modRan = 0 #model has never been ran
 
497
        self.plot = 0 #no plot has ever been generated
 
498
        self.gauge1.SetRange(100)
 
499
        self.uncertainty = 0 # uncertainty analysis is off
 
500
        self.ceqs = [] #compiled equations
 
501
        self.cpars = [] #compiled parameters
 
502
        self.curdir = ''
 
503
        
 
504
 
 
505
    def OnMenu1items0Menu(self, event):
 
506
        """
 
507
        Load a model file.
 
508
        """
 
509
        self.modtree = Model() #reset the modtree object upon the opening of a new model
 
510
        if self.curdir:
 
511
            os.chdir(self.curdir)
 
512
        dlg = wx.FileDialog(self, "Choose a file", self.curdir, "", "*.ode", wx.OPEN)
 
513
        try:
 
514
            if dlg.ShowModal() == wx.ID_OK:
 
515
                filename = dlg.GetPath()
 
516
                a = self.modtree.readFile(filename)
 
517
                if a==1: #model is not a xml file
 
518
                    f = open(filename)
 
519
                    self.modict = pickle.load(f)
 
520
                    self.fillGui()
 
521
                    self.FileName = filename
 
522
                    self.SetTitle('Model Builder - '+filename)
 
523
                    f.close()
 
524
                elif a == 2:
 
525
                    return
 
526
                else:
 
527
                    self.modict.update(self.modtree.modict)
 
528
                    self.fillGui()
 
529
                    self.FileName = filename
 
530
                    self.SetTitle('Model Builder - '+os.path.split(filename)[1])
 
531
                    #load data
 
532
                    try:
 
533
                        d = pickle.load(self.checkExt(filename,'.dat'))
 
534
                        self.modict['results']=d[0]
 
535
                        self.modict['trange']=d[1]
 
536
                    except: pass
 
537
                self.ModLoaded = 1
 
538
                self.curdir = os.path.split(filename)[0]
 
539
        finally:
 
540
            self.statusBar1.SetStatusText('Model loaded.')
 
541
            #print self.modict.keys()
 
542
            dlg.Destroy()
 
543
 
 
544
    def fillGui(self):
 
545
        """
 
546
        fill GUI from model dictionary
 
547
        """
 
548
        self.eqnEdit.SetValue(self.modict['equations'])
 
549
        self.textCtrl2.SetValue(str(self.modict['start']))
 
550
        self.textCtrl3.SetValue(str(self.modict['end']))
 
551
        self.textCtrl4.SetValue(str(self.modict['step']))
 
552
        self.initValInput.SetValue(str(self.modict['init']))
 
553
        self.parEdit.SetValue(self.modict['parameters'])
 
554
        self.BuildModel(r=0) # call BuildModel without running the model
 
555
    
 
556
    def checkExt(self,fn,ext):
 
557
        """
 
558
        Add a filename extension to the filename or replace 
 
559
        original extension.
 
560
        fn: file name
 
561
        ext: desired extension
 
562
        """
 
563
        head,tail = os.path.split(fn)
 
564
        orext = '.'+tail.split('.')[-1]
 
565
        if not orext:
 
566
            tail += ext
 
567
        if orext and (ext != orext):
 
568
            tail = tail.replace(orext,ext)
 
569
        return head+tail
 
570
        
 
571
    
 
572
    def OnMenu1items1Menu(self, event):
 
573
        """
 
574
        Save model.
 
575
        """
 
576
        self.modtree = Model()
 
577
        if not self.FileName:
 
578
            return self.OnMenu1items2Menu(event)
 
579
        else:
 
580
            #print self.modict.keys()
 
581
            filename = os.path.split(self.FileName)[1]
 
582
            self.modict['modelname'] = filename
 
583
            self.modtree.path = self.curdir
 
584
            self.BuildModel(r=0) # call BuildModel without running the model
 
585
            #save data
 
586
            if self.modict.has_key('results')and self.modict.has_key('trange'):
 
587
                fna = open(self.checkExt(filename,'.dat'),'w')
 
588
                pickle.dump((self.modict['results'], self.modict['trange']),fna)
 
589
                fna.close()
 
590
            # Saving in xml format
 
591
            try:
 
592
                self.modict.pop('results')
 
593
                self.modict.pop('trange')
 
594
            except: pass
 
595
            self.modtree.Create(**self.modict)
 
596
            self.modtree.saveFile(self.checkExt(filename,'.ode'))
 
597
            self.ModLoaded = 1
 
598
            self.statusBar1.SetStatusText('Model saved.')
 
599
            #print self.modict.keys()
 
600
 
 
601
 
 
602
    def OnMenu1items2Menu(self, event):
 
603
        """
 
604
        Save as menu entry
 
605
        """
 
606
        self.modtree = Model()
 
607
        dlg = wx.FileDialog(self, "Save File As", self.curdir, "", "*.ode", wx.SAVE)
 
608
        try:
 
609
            if dlg.ShowModal() == wx.ID_OK:
 
610
                self.FileName = dlg.GetPath()
 
611
                filename = os.path.split(self.FileName)[-1]
 
612
                self.modtree.path = self.curdir = os.path.split(self.FileName)[0]
 
613
                if not filename.endswith('.ode'):
 
614
                    filename += '.ode' 
 
615
                if not self.modbuilt: #check to see if model's dictionary has been built
 
616
                    self.BuildModel(r=0) # call BuildModel without running the model
 
617
                    self.modict['modelname'] = filename
 
618
                self.SetTitle('Model Builder - '+filename)
 
619
                #save data
 
620
                if self.modict.has_key('results'):
 
621
                    os.chdir(self.curdir)
 
622
                    fna = open(self.checkExt(filename,'.dat'),'w')
 
623
                    pickle.dump((self.modict['results'], self.modict['trange']),fna)
 
624
                    fna.close()
 
625
                # Saving in xml format
 
626
                #print self.modict.keys()
 
627
                try:
 
628
                    self.modict.pop('results')
 
629
                    self.modict.pop('trange')
 
630
                except:
 
631
                    pass
 
632
                self.modtree.Create(**self.modict)
 
633
                self.modtree.saveFile(self.checkExt(filename,'.ode'))
 
634
                self.ModLoaded = 1
 
635
                self.statusBar1.SetStatusText('Model saved.')
 
636
 
 
637
        finally:
 
638
            dlg.Destroy()
 
639
 
 
640
 
 
641
    def OnMenu1items3Menu(self, event):
 
642
        """
 
643
        Close model menu entry.
 
644
        """
 
645
        self.modtree = Model()
 
646
        self.FileName = None
 
647
        self.ModLoaded = None
 
648
        self.modict = {}
 
649
        self.eqnEdit.SetValue('')
 
650
        self.textCtrl2.SetValue('')
 
651
        self.textCtrl3.SetValue('')
 
652
        self.textCtrl4.SetValue('')
 
653
        self.initValInput.SetValue('')
 
654
        self.parEdit.SetValue('')
 
655
        self.SetTitle('Model Builder - ODE')
 
656
 
 
657
 
 
658
    def OnMenu1items4Menu(self, event):
 
659
        """
 
660
        Exit the program
 
661
        """
 
662
 
 
663
        self.Close()
 
664
        self.Destroy()
 
665
 
 
666
 
 
667
    def OnMenu2items0Menu(self, event):
 
668
        """
 
669
        If Uncertainty Analysis has been selected, it will Open the uncertainty panel to set
 
670
        the parameters for the analysis.
 
671
        """
 
672
        if not self.ModLoaded:
 
673
            self.OnMenu1items0Menu(event)
 
674
            return
 
675
        #--Checks if the Uncertainty item in the analysis menu (2) has been checked------
 
676
 
 
677
        if self.menu2.IsChecked(wxID_WXFRAME1MENU2ITEMS0):
 
678
            self.uncertaintyPanel = LHS(None)
 
679
            #self.uncertaintyPanel=uncertaintyMiniFrame.create(None)
 
680
            self.initUncertainty()
 
681
            self.uncertaintyPanel.Show()
 
682
            self.uncertainty = 1 #raises uncertainty flag
 
683
        else:
 
684
            self.uncertainty = 0 #Uncertainty mode off
 
685
            try:
 
686
                self.uncertaintyPanel.Close()
 
687
            except:
 
688
                pass
 
689
 
 
690
 
 
691
    def checkErrors(self):
 
692
        """
 
693
        Check for normal editing errors in model definition
 
694
        """
 
695
        while not self.eqnEdit.GetValue()=='':
 
696
            val = self.eqnEdit.GetValue().strip().splitlines()
 
697
            Neq = len(val)
 
698
            break
 
699
##~         Neq = int(self.eqnEdit.GetNumberOfLines()) #get number of ODEs
 
700
##~         while strip(self.eqnEdit.GetLineText(Neq-1)) == '': # avoid getting empty lines at the end of the eq. box
 
701
##~             Neq = Neq-1
 
702
##~             if Neq == 1:nty
 
703
##~                 break
 
704
 
 
705
        if not self.parEdit.GetValue() == '':
 
706
            valp = self.parEdit.GetValue().strip().splitlines()
 
707
            Npar = len(valp)
 
708
        else:
 
709
            Npar = 0
 
710
 
 
711
##~         Npar = int(self.parEdit.GetNumberOfLines()) #get Number of  Parameters
 
712
##~         while strip(self.parEdit.GetLineText(Npar-1)) == '':# avoid getting empty lines at the end of the eq. box
 
713
##~             Npar=Npar-1
 
714
##~             if Npar == 1:
 
715
##~                 break
 
716
##~             if Npar == 0:
 
717
##~                 Npar = 1
 
718
##~                 break
 
719
 
 
720
#---Check number of initial conditions----------------------------------------------------------------------------
 
721
        if self.initValInput.GetValue().strip() == '':
 
722
            ni = 0
 
723
        else:
 
724
            ni = len(self.initValInput.GetValue().strip().split(' '))
 
725
        if not ni == Neq:
 
726
            return 1
 
727
 
 
728
#---Check that all initial conditions are numbers----------------------------------------------------------------------------
 
729
        for i in xrange(ni):
 
730
            try:
 
731
                float(self.initValInput.GetValue().strip().split(' ')[i])
 
732
            except ValueError:
 
733
                e = 2
 
734
                return 2
 
735
#---Check syntax of equations----------------------------------------------------------------------------
 
736
        y=zeros(Neq)#fake equation array
 
737
        p=zeros(Npar)#fake parameter array
 
738
        t=0
 
739
        eqs = self.eqnEdit.GetValue().strip().split('\n')
 
740
        eql = [i.split('=')[-1] for i in self.eqnEdit.GetValue().strip().split('\n')]
 
741
        vnames = [i.split('=')[0] for i in self.eqnEdit.GetValue().strip().split('\n') if '=' in i]
 
742
        pnames = [i.split('=')[0] for i in self.parEdit.GetValue().strip().splitlines() if '=' in i]
 
743
        #replace ocurrences of variable names in equations.
 
744
        if vnames: 
 
745
            #create fake variables
 
746
            for v in vnames:
 
747
                exec('%s = 0'%v)
 
748
        #replace occurrences of parameter names in equations by p[i]
 
749
        if pnames:
 
750
            for p in pnames:
 
751
                exec('%s = 0'%p)
 
752
        for k in eql:
 
753
            try:
 
754
                eval(k) #dy(k)/dt
 
755
            except (SyntaxError, NameError), details:
 
756
                print details
 
757
                return 3
 
758
 
 
759
        return 0
 
760
 
 
761
 
 
762
    def OnToolbar1tools0Tool(self, event):
 
763
        """
 
764
        Run button event.
 
765
        Call BuildModel with r=1, and times how long the model takes to run.
 
766
        The time elapsed is shown in a message dialog.
 
767
        """
 
768
        if not self.ModLoaded:
 
769
            if self.eqnEdit.GetValue().strip() == '':
 
770
                return self.OnMenu1items0Menu(event)
 
771
#----compile equations and store for later use
 
772
        eql = [i.split('=')[-1] for i in self.eqnEdit.GetValue().strip().split('\n')]
 
773
        self.vnames = [v.split('=')[0] for v in self.eqnEdit.GetValue().strip().split('\n') if '=' in v]
 
774
        
 
775
        try:
 
776
            self.ceqs = [compile(i,'<string>','eval') for i in eql]
 
777
        except SyntaxError:
 
778
            dlg = wx.MessageDialog(self, 'There is a syntax error in the Equation Box.\nPlease fix it and try again.',
 
779
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
 
780
            try:
 
781
                dlg.ShowModal()
 
782
            finally:
 
783
                dlg.Destroy()
 
784
        self.compilePars()
 
785
#---checking for errors----------------------------------------------------------------------------
 
786
        e = self.checkErrors()
 
787
        if e == 1:
 
788
            dlg = wx.MessageDialog(self, 'Wrong number of initial condition values.',
 
789
              'Initial Condition Error', wx.OK | wx.ICON_INFORMATION)
 
790
            try:
 
791
                dlg.ShowModal()
 
792
            finally:
 
793
                dlg.Destroy()
 
794
            return
 
795
        elif e == 2:
 
796
            dlg = wx.MessageDialog(self, 'There is a syntax error on the initial conditions box.',
 
797
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
 
798
            try:
 
799
                dlg.ShowModal()
 
800
            finally:
 
801
                dlg.Destroy()
 
802
            return
 
803
        elif e==3:
 
804
            dlg = wx.MessageDialog(self, 'There is a syntax error in the Equation Box.\nPlease fix it and try again.',
 
805
              'Syntax Error', wx.OK | wx.ICON_INFORMATION)
 
806
            try:
 
807
                dlg.ShowModal()
 
808
            finally:
 
809
                dlg.Destroy()
 
810
        else:
 
811
            pass
 
812
        self.modbuilt = 0
 
813
        self.updateGauge(0)
 
814
        if self.uncertainty == 0:
 
815
            self.BuildModel(r=1) #regular run
 
816
            self.modRan = 1 #set the flag indicating model has been run
 
817
        else:
 
818
            if self.uncertaintyPanel.Done:
 
819
                self.statusBar1.SetStatusText('Simulation Started!\n')
 
820
                self.BuildModel(r=1) # Melding setup
 
821
                self.modRan = 1 #set the flag indicating model has been run
 
822
            else:
 
823
                dlg = wx.MessageDialog(self, 'Set the parameters for the Uncertainty Analysis\nin its panel and press the "OK" button',
 
824
                  'Uncertain Parameters', wx.OK | wx.ICON_INFORMATION)
 
825
                try:
 
826
                    dlg.ShowModal()
 
827
                finally:
 
828
                    dlg.Destroy()
 
829
 
 
830
 
 
831
 
 
832
    def OnToolbar1tools0ToolRclicked(self, event):
 
833
        event.Skip()
 
834
        
 
835
    def updateGauge(self,t):
 
836
        """
 
837
        This function simply updates the progress gauge
 
838
        """
 
839
        pd = (100*t)/(float(str(self.textCtrl3.GetValue()).strip())-float(str(self.textCtrl2.GetValue()).strip())) #calcutates percentage done
 
840
        self.gauge1.SetValue(int(pd))
 
841
        #self.gauge1.SetToolTipString(str(pd)+'%')
 
842
        self.gauge1.Refresh() #update the gauge
 
843
 
 
844
    def Equations(self, y, t):
 
845
        """
 
846
        This function defines the system of differential equations, evaluating
 
847
        each line of the equation text box as ydot[i]
 
848
 
 
849
        returns ydot
 
850
        """
 
851
        self.updateGauge(t)
 
852
 
 
853
        Neq=self.Neq
 
854
        Npar = self.Npar
 
855
        par = self.par
 
856
        #print "vnames:", self.vnames, self.pnames
 
857
        if self.vnames:
 
858
            #print y,type(y)
 
859
            exec('%s=%s'%(','.join(self.vnames),list(y)))
 
860
 
 
861
        ydot = zeros((Neq),'d') #initialize ydot
 
862
        p = zeros((Npar),'d') #initialize p
 
863
    #---Create Parameter Array----------------------------------------------------------------------------
 
864
        
 
865
        if self.uncertainty == 0:
 
866
            pars = self.cpars
 
867
            #par.GetValue().strip().split('\n')
 
868
            if pars: #only if there is at least one parameter
 
869
                for j in xrange(len(pars)):
 
870
                    if self.pnames:
 
871
                        exec(pars[j])
 
872
                    else:
 
873
                        p[j] = eval(pars[j]) #initialize parameter values
 
874
                        
 
875
        else:
 
876
            if Npar:
 
877
                for i in xrange(Npar):
 
878
                    if self.pnames:
 
879
                        exec("%s=%s"%(self.pnames[i],par[i][self.run]))
 
880
                    else:
 
881
                        p[i] = par[i][self.run] # Get a new set of parameters for each repeated run
 
882
    #---Create equation array----------------------------------------------------------------------------
 
883
        eqs = self.ceqs
 
884
        #print par[0][0].shape,type(par[0][0]),par[0].shape,type(par[0]),type(par[0])
 
885
        for k in xrange(Neq):
 
886
            ydot[k] = eval(eqs[k]) #dy(k)/dt
 
887
 
 
888
        return ydot
 
889
 
 
890
    def compilePars(self):
 
891
        """
 
892
        compile parameter expressions
 
893
        """
 
894
        if self.parEdit.GetValue().strip() =="":
 
895
            self.cpars = []
 
896
            return
 
897
        pars = self.parEdit.GetValue().strip().splitlines()
 
898
        self.pnames = [p.split('=')[0] for p in pars if '=' in p]
 
899
        if self.pnames:
 
900
            #in this case returns the compete expression, including the '='
 
901
            #print pars
 
902
            try:
 
903
                self.cpars = [compile(i,'<parameter>','exec') for i in pars]
 
904
            except (SyntaxError),details:
 
905
                self.cpars =pars
 
906
                dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box:\n%s\nPlease fix it and try again.'%details,
 
907
                  'Syntax Error', wx.OK | wx.ICON_INFORMATION)
 
908
                try:
 
909
                    dlg.ShowModal()
 
910
                finally:
 
911
                    dlg.Destroy()
 
912
        else:
 
913
            try:
 
914
                self.cpars = [compile(i,'<parameter>','eval') for i in pars]
 
915
            except SyntaxError:
 
916
                dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box.\nPlease fix it and try again.',
 
917
                  'Syntax Error', wx.OK | wx.ICON_INFORMATION)
 
918
                try:
 
919
                    dlg.ShowModal()
 
920
                finally:
 
921
                    dlg.Destroy()
 
922
            
 
923
    
 
924
    def OnToolbar1tools1Tool(self, event):
 
925
        """
 
926
        Show a figure with the equations typeset
 
927
        """
 
928
#---translate equations to pseudo-TeX notation----------------------------------------------------------------------------
 
929
        texdict = {
 
930
        '**':r'^','sqrt':r'\sqrt','[':r'_{',']':r'}',
 
931
        'sin':r'\rm{sin}','cos':r'\rm{cos}',
 
932
        'alpha':r'\alpha ','beta ':r'\beta ','gamma':r'\gamma '
 
933
        } #Can be extended to acomodate new strings
 
934
        texdict2 = {'*':r'\times '}# to take care of the multiplication sign *
 
935
        xlat = Xlator(texdict)
 
936
        xlat2 = Xlator(texdict2)
 
937
        if not self.modbuilt:
 
938
            self.BuildModel(r=0)
 
939
        eq = range(self.Neq) #initialize
 
940
        eqs = self.eqnEdit.GetValue().strip().split('\n')
 
941
        for i in xrange(self.Neq):
 
942
            e = eqs[i].split('=')[-1]
 
943
            eq[i] = xlat2.xlat(xlat.xlat(e))
 
944
#-------------------------------------------------------------------------------
 
945
        #getting variable names
 
946
        vnames = [n.split('=')[0].strip() for n in eqs if '=' in n]
 
947
        eqlist=[]
 
948
        for i in xrange(self.Neq):
 
949
            if len(vnames)==self.Neq:
 
950
                eq[i] = r'$d%s/dt = '%vnames[i] + eq[i] + r'$'
 
951
            else:
 
952
                eq[i] = r'$dY_%s/dt = '%i + eq[i] +r'$'
 
953
            eqlist.append(eq[i])
 
954
#---plot equations----------------------------------------------------------------------------
 
955
        DP = PF.create(None)
 
956
        DP.SetTitle('Model '+str(self.FileName)[:-4]+': Equations')
 
957
        DP.plotEquation(eqlist)
 
958
        DP.Show()
 
959
 
 
960
 
 
961
    def plotOutput(self, x, y):
 
962
        """
 
963
        Plots the results of a single run.
 
964
        Raises the flag that the plot has been created
 
965
        """
 
966
        self.PF = PF.create(None)
 
967
        self.PF.plot_data(x,y,self.vnames)
 
968
        self.PF.Show()
 
969
 
 
970
 
 
971
        self.plot = 1
 
972
 
 
973
    def plotMelding(self,x,y,tit='Results'):
 
974
        """
 
975
        This function preprocesses the output of the repeated Runs, calls Bayesian Melding run and sends the data to
 
976
        PlotFigure.plotStats to be plotted.
 
977
        x contains the time values and y is a list of outputs from odeint, one from each run.
 
978
        """
 
979
        self.statusBar1.SetStatusText('Preparing data for plotting...\n')
 
980
        yt = [] # initializes list of transposed runs
 
981
        nvar = min(y[0].shape) # Number of Variables
 
982
        runlen = max(y[0].shape) #Lenght of runs
 
983
 
 
984
        nruns = len(y) # Number of runs
 
985
 
 
986
##        SPF = PF.create(None)
 
987
##        SPF.plot_data(x,y)
 
988
##        SPF.Show()
 
989
 
 
990
        self.nruns = nruns
 
991
        runs_byvar = [] #List of arrays that will contain all runs for each given variable
 
992
 
 
993
        for i in y:
 
994
            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).
 
995
 
 
996
        for v in xrange(nvar):
 
997
            runs_byvar.append(array([yt[i][v] for i in xrange(nruns)]))
 
998
 
 
999
 
 
1000
 
 
1001
        # TODO: Turn medianruns into a function
 
1002
        medianRuns = [median(i) for i in runs_byvar]
 
1003
        self.statusBar1.SetStatusText('Done!\n\n')
 
1004
 
 
1005
 
 
1006
 
 
1007
        #---95% Limits----------------------------------------------------------
 
1008
        # TODO: Turn calculation of limits into a function
 
1009
        self.statusBar1.SetStatusText('Calculating credibility intervals...\n')
 
1010
        sorted=[]
 
1011
        ll = []
 
1012
        ul = []
 
1013
        lc = int(runs_byvar[0].shape[0]*0.025) #column containing the lower boundary for the 95% interval
 
1014
        hc = int(runs_byvar[0].shape[0]*0.975) #column containing the upper boundary for the 95% interval
 
1015
        for l in xrange(nvar):
 
1016
            sorted.append(msort(runs_byvar[l]))
 
1017
            ll.append(sorted[l][lc])
 
1018
            ul.append(sorted[l][hc])
 
1019
 
 
1020
        ts = (medianRuns,ll,ul)
 
1021
        self.statusBar1.SetStatusText('Done!\n\n')
 
1022
 
 
1023
#---testing---------------------------------------------------------------------
 
1024
##        med = medianRuns[0]-medianRuns[1]
 
1025
##        SPF = PF.create(None)
 
1026
##        SPF.plot(x,med)
 
1027
##        SPF.Show()
 
1028
#-------------------------------------------------------------------------------
 
1029
 
 
1030
 
 
1031
        TP = PF.create(None)
 
1032
        TP.SetTitle(tit)
 
1033
        TP.plotStats(x,ts,self.vnames)
 
1034
        TP.Show()
 
1035
 
 
1036
 
 
1037
        self.plot = 1
 
1038
        return (x,runs_byvar)
 
1039
 
 
1040
 
 
1041
    def OnToolbar1tools2Tool(self, event):
 
1042
        """
 
1043
        Show Output Table
 
1044
        """
 
1045
        if self.modRan:
 
1046
            self.TableOut = wxFrame2.create(None)
 
1047
            output = self.modict['results'] # get output from model's dictionary
 
1048
            x = self.modict['trange']
 
1049
            y = output[0]
 
1050
            info = output[1] #extra information dictionary
 
1051
            infoarray = array([info['hu'],info['tcur'],info['tsw'],info['nqu'],info['mused']]).transpose()
 
1052
            infoarray = concatenate((zeros((1,5)),infoarray),axis=0)
 
1053
            #print infoarray.shape, y.shape
 
1054
            r,c = y.shape # get size of t_course array
 
1055
            self.TableOut.grid1.CreateGrid(r+1,c+6)
 
1056
            
 
1057
            # Filling the grid
 
1058
            x.shape = (len(x),1)
 
1059
            data = concatenate((x,y,infoarray),axis=1)
 
1060
            self.TableOut.Show()
 
1061
            self.TableOut.fillGrid(data)
 
1062
            self.TableOut.table.SetColLabelValue(0,'Time')
 
1063
            for j in xrange(1,c+1): #fill column labels
 
1064
                if self.vnames:
 
1065
                    self.TableOut.table.SetColLabelValue(j,self.vnames[j-1])
 
1066
                else:
 
1067
                    self.TableOut.table.SetColLabelValue(j,'y[%s]'%(j-1))
 
1068
            #Adding info column labels
 
1069
            clabels = ['Step sizes','Time reached','Last method switch','Method order','Method nused']
 
1070
            for z in xrange(1,6):
 
1071
                self.TableOut.table.SetColLabelValue(c+z,clabels[z-1])
 
1072
                
 
1073
 
 
1074
 
 
1075
 
 
1076
    def BuildModel(self, r=0):
 
1077
        """
 
1078
        Constructs the model from the input fields and runs it if r==1
 
1079
        """
 
1080
        try:
 
1081
            if self.modict:pass
 
1082
        except:
 
1083
            self.modict = {}
 
1084
        try:
 
1085
            self.modict['modelname'] = os.path.split(self.FileName)[-1]
 
1086
        except AttributeError:
 
1087
            self.modict['modelname'] = ''
 
1088
        self.modict['equations'] = str(self.eqnEdit.GetValue()) #put equations into model's dictionary
 
1089
        self.modict['parameters'] = str(self.parEdit.GetValue()) # put parameters into model's dictionary
 
1090
        t_start = float(str(self.textCtrl2.GetValue()).strip())
 
1091
        self.modict['start'] = str(self.textCtrl2.GetValue()).strip() #store start time
 
1092
        t_end = float(str(self.textCtrl3.GetValue()).strip())
 
1093
        self.modict['end'] = str(self.textCtrl3.GetValue()).strip() #store end time
 
1094
        t_step = float(str(self.textCtrl4.GetValue()).strip())
 
1095
        self.modict['step'] = str(self.textCtrl4.GetValue()).strip() # Store step size
 
1096
        init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != ''])
 
1097
        self.modict['init'] = str(self.initValInput.GetValue()).strip() # Store initial conditions
 
1098
        t_range = arange(t_start, t_end+t_step, t_step)
 
1099
        self.modict['trange'] = t_range
 
1100
        if self.focheckBox.GetValue():
 
1101
            fo = 1
 
1102
        else:
 
1103
            fo = 0
 
1104
        if self.convcheckBox.GetValue():
 
1105
            cm = 1
 
1106
        else:
 
1107
            cm = 0
 
1108
        eqs = self.eqnEdit.GetValue().strip().splitlines()
 
1109
        self.vnames = [n.split('=')[0].strip() for n in eqs if '=' in n]
 
1110
        self.pnames = [n.split('=')[0].strip() for n in self.parEdit.GetValue().strip().splitlines() if '=' in n]
 
1111
 
 
1112
 
 
1113
        while not self.eqnEdit.GetValue()=='':#get number of ODEs
 
1114
            val = self.eqnEdit.GetValue().strip().split('\n')
 
1115
            Neq = len(val)
 
1116
            break
 
1117
 
 
1118
 
 
1119
        self.Neq = Neq#to be used by Equations
 
1120
        if r==1:
 
1121
            self.sw = wx.StopWatch()
 
1122
            self.gauge1.SetValue(0)
 
1123
            self.sw.Start()
 
1124
            #-----------------------------------------------------
 
1125
            if self.uncertainty == 0:#regular (single) run
 
1126
                if not self.parEdit.GetValue() == '':#get Number of  Parameters
 
1127
                    valp = self.parEdit.GetValue().strip().split('\n')
 
1128
                    Npar = len(valp)
 
1129
                else:
 
1130
                    Npar = 0
 
1131
 
 
1132
                self.Npar = Npar#to be used by Equations
 
1133
                self.par = self.parEdit #to be used by Equations
 
1134
                t_course = integrate.odeint(self.Equations,init_conds,t_range, full_output=fo, printmessg=cm)
 
1135
                self.plotOutput(t_range,t_course)
 
1136
                self.modict['results'] = t_course
 
1137
                self.modRan = 1
 
1138
            else:
 
1139
#-------------Multiple runs (uncertainty analysis)------------------------------------
 
1140
                self.uncRun(init_conds,t_range,cm)
 
1141
                
 
1142
                
 
1143
            t = gmtime(self.sw.Time()/1000)
 
1144
 
 
1145
            dlg = wx.MessageDialog(self, 'The simulation was completed in '+str(t[3])+' hours, '+str(t[4])+' minutes and '+str(t[5])+' seconds.',
 
1146
          'Time Elapsed', wx.OK | wx.ICON_INFORMATION)
 
1147
            try:
 
1148
                dlg.ShowModal()
 
1149
            finally:
 
1150
                dlg.Destroy()
 
1151
            if self.uncertainty == 1:
 
1152
                self.statusBar1.SetStatusText('The simulation was completed in: '+str(t[3])+' hours, '+str(t[4])+' minutes and '+str(t[5])+' seconds.')
 
1153
#-------------------------------------------------------------------------------
 
1154
 
 
1155
        self.modbuilt = 1
 
1156
 
 
1157
    def uncRun(self, init_conds,t_range,cm):
 
1158
        """
 
1159
        Runs the model in uncertainty mode
 
1160
        """
 
1161
        Npar  = len(self.uncertaintyPanel.theta)
 
1162
        nr= int(self.uncertaintyPanel.specs['samples']) # Get K from uncertaity panel
 
1163
        t_courseList = []
 
1164
 
 
1165
        if self.uncertaintyPanel.anaChoice.GetStringSelection() == 'Uncertainty':
 
1166
            self.parMeld = self.uncertaintyPanel.priords[:Npar] # List of parameter priors. Excluding state variables]
 
1167
        else:
 
1168
            self.parMeld = self.uncertaintyPanel.priords
 
1169
        self.par = self.parMeld #to be used by Equations
 
1170
        self.Npar = len(self.par)
 
1171
        #print 'runs:%s'%nr
 
1172
        for i in xrange(nr):
 
1173
            self.run = i
 
1174
#---tcourselist for multiple runs does not contain the full output of odeint, just the time series--------------
 
1175
            t_courseList.append(integrate.odeint(self.Equations,init_conds,t_range, full_output=0, printmessg=cm))
 
1176
            #if self.run%20 == 0:
 
1177
             #   self.statusBar1.SetStatusText(str(self.run)+'\n')
 
1178
            init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != '']) #reset initial conditions between runs
 
1179
 
 
1180
        self.modRan = 1
 
1181
        te = gmtime(self.sw.Time()/1000)#time elapsed so far (first stage of melding)
 
1182
        self.statusBar1.SetStatusText('First stage of simulation completed in:'+str(te[3])+' hours, '+str(te[4])+' minutes and '+str(te[5])+' seconds.'+'\n')
 
1183
        (x,runs_byvar) = self.plotMelding(t_range,t_courseList,tit='Median Runs and 95 percent intervals. n=%s'%nr)
 
1184
        self.Bmeld(x,runs_byvar) # Perform the rest of the melding calculations
 
1185
        self.modict['meldruns'] = t_courseList
 
1186
    
 
1187
    def Bmeld(self,t,ModOut):
 
1188
        """
 
1189
        Performs the Melding.
 
1190
        t is an array of time values.
 
1191
        Modout is a list of n arrays of time courses where n is the number of variables.
 
1192
        """
 
1193
        nvar = len(ModOut)
 
1194
        dlg = wx.TextEntryDialog(self, 'for which time do you want to run the Melding', 'Choose Time', str(t[-1]))
 
1195
        try:
 
1196
            if dlg.ShowModal() == wx.ID_OK:
 
1197
                answer = dlg.GetValue()
 
1198
                if answer == '':
 
1199
                    time = t[-1]
 
1200
                else:
 
1201
                    time  = eval(answer)
 
1202
        finally:
 
1203
            dlg.Destroy()
 
1204
 
 
1205
 
 
1206
        Phis = [ModOut[i][:,int(time)] for i in xrange(nvar)] # list with all the specified values for each variable
 
1207
        q2phis = self.uncertaintyPanel.priords[-nvar:] # priors (pre-model) for the phis
 
1208
        q1thetas = tuple(self.uncertaintyPanel.priords[:-nvar]) # priors for the thetas (parameters)
 
1209
        plimits = self.uncertaintyPanel.priors[1][-nvar:] #limits of the prior distributions of the phis
 
1210
        ptype = self.uncertaintyPanel.priors[0][:-nvar] #types of the prior distributions
 
1211
        lik = self.uncertaintyPanel.lhoods
 
1212
        alpha = self.uncertaintyPanel.alpha
 
1213
        L = int(self.nruns*0.1)
 
1214
#---Run SIR----------------------------------------------------------------------------
 
1215
#---meldout=(w, post_theta, qtilphi, q1est)----------------------------------------------------------------------------
 
1216
        meldout = meld.SIR(alpha,q2phis,plimits,ptype, q1thetas,Phis,L,lik) # output of meld.SIR: (w,qtiltheta,qtilphi,q1est)
 
1217
#---If SIR fails, don't proceed----------------------------------------------------------------------------
 
1218
        if meldout == None:
 
1219
            return
 
1220
 
 
1221
 
 
1222
#---Calculate posterior of phis-----------------------------------------------------
 
1223
        (x,post_phi) = self.postPHI(meldout, L)
 
1224
#---Plotting results---------------------------------------------------------------------------
 
1225
        nplots = len(self.uncertaintyPanel.priords)
 
1226
        allpriors = self.uncertaintyPanel.priords
 
1227
        nvp = self.uncertaintyPanel.theta + self.uncertaintyPanel.phi# Names of variable + parameters in the model)
 
1228
        nlik = len (lik) # Get number of likelihood functions
 
1229
        vname = ['prior of %s' % i for i in nvp]
 
1230
 
 
1231
        DP = PF.create(None)
 
1232
        DP.SetTitle('Prior distributions for the parameters')
 
1233
        DP.plotDist(allpriors,vname)
 
1234
        DP.Show()
 
1235
 
 
1236
#---Plot posteriors of theta----------------------------------------------------------------------------
 
1237
        MP = PF.create(None)
 
1238
        MP.SetTitle('Theta posteriors')
 
1239
        MP.plotMeldout(meldout, self.pnames)
 
1240
        MP.Show()
 
1241
#---Plot posteriors of phi----------------------------------------------------------------------------
 
1242
# TODO: extract the values of post_phi at time time for each variable to plot.
 
1243
        self.plotMelding(x,post_phi,tit='Series After Melding Calibration')
 
1244
        yt = []
 
1245
        runs_byvar = []
 
1246
        for i in post_phi:
 
1247
            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).
 
1248
 
 
1249
        for v in xrange(nvar):
 
1250
            runs_byvar.append(array([yt[i][v] for i in xrange(L)]))
 
1251
        data = [runs_byvar[i][:,-1] for i in xrange(nvar)]
 
1252
        #vname2 = ['v[%s]' % i for i in xrange(nvar)]
 
1253
        MP = PF.create(None)
 
1254
        MP.SetTitle('Phi posteriors at time t='+str(time))
 
1255
        MP.plotDist(data,self.vnames)
 
1256
        MP.Show()
 
1257
 
 
1258
    def postPHI(self,meldout,L):
 
1259
        """
 
1260
        this function takes the output of the SIR algorithm and calculates the posterior
 
1261
        distributions of the Phis from the posteriors of the thetas.
 
1262
        """
 
1263
        self.post_theta = meldout[1]
 
1264
        init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != ''])
 
1265
        t_start = float(self.modict['start'])
 
1266
        t_end = float(self.modict['end'])
 
1267
        t_step = float(self.modict['step'])
 
1268
        t_range = arange(t_start, t_end+t_step, t_step)
 
1269
        t_courseList = []
 
1270
        for i in xrange(L):
 
1271
            self.run = i
 
1272
            t_courseList.append(integrate.odeint(self.Equations2,init_conds,t_range, full_output=0, printmessg=0))
 
1273
            #self.statusBar1.SetStatusText(str(self.run)+'\n')
 
1274
            init_conds = array([float(i) for i in self.initValInput.GetValue().strip().split(' ') if i != '']) #reset initial conditions between runs
 
1275
 
 
1276
        return (t_range,t_courseList)
 
1277
    def Equations2(self,y,t):
 
1278
        """
 
1279
        Variation of the Equations function to calculate the posterior of phi
 
1280
        """
 
1281
        Neq = self.Neq
 
1282
        pars = self.post_theta
 
1283
        Npar = len(pars) #get number of parameters
 
1284
        if self.vnames:
 
1285
            exec('%s=%s'%(','.join(self.vnames),list(y)))
 
1286
        ydot = zeros((Neq),'d') #initialize ydot
 
1287
        p = zeros((Npar),'d') #initialize p
 
1288
    #---Create Parameter Array----------------------------------------------------------------------------
 
1289
        for i in xrange(Npar):
 
1290
            if self.pnames:
 
1291
                exec("%s=%s"%(self.pnames[i],pars[i][self.run]))
 
1292
            else:
 
1293
                p[i] = pars[i][self.run] # Get a new set of parameters for each repeated run
 
1294
    #---Create equation array----------------------------------------------------------------------------
 
1295
        eqs = self.ceqs#self.eqnEdit.GetValue().strip().split('\n')
 
1296
        for k in xrange(Neq):
 
1297
            ydot[k] = eval(eqs[k]) #dy(k)/dt
 
1298
 
 
1299
 
 
1300
        return ydot
 
1301
 
 
1302
 
 
1303
 
 
1304
 
 
1305
 
 
1306
    def OnMenu3items0Menu(self, event):
 
1307
        """
 
1308
        Opens the about window
 
1309
        """
 
1310
        dlg = about.wxDialog1(self)
 
1311
        try:
 
1312
            dlg.ShowModal()
 
1313
        finally:
 
1314
            dlg.Destroy()
 
1315
 
 
1316
    def initUncertainty(self):
 
1317
        """
 
1318
        Initializes the values on the uncertainty Analysis Panel based on
 
1319
        model specification
 
1320
        """
 
1321
 
 
1322
        while not self.eqnEdit.GetValue()=='':#get number of ODEs
 
1323
            val = self.eqnEdit.GetValue().strip().split('\n')
 
1324
            Neq = len(val)
 
1325
            break
 
1326
 
 
1327
        if not self.parEdit.GetValue() == '':#get number of parameters
 
1328
            valp = self.parEdit.GetValue().strip().split('\n')
 
1329
            Npar = len(valp)
 
1330
        else:
 
1331
            Npar = 0
 
1332
            
 
1333
        if self.vnames:
 
1334
            phi = self.vnames
 
1335
        else:
 
1336
            phi = ["Y[%s]" % str(i) for i in xrange(Neq)]
 
1337
 
 
1338
        if Npar > 0:
 
1339
            if self.pnames:
 
1340
                theta = self.pnames
 
1341
            else:
 
1342
                theta = ['P[%s]' % str(i) for i in xrange(Npar)]
 
1343
 
 
1344
        self.uncertaintyPanel.phi = phi
 
1345
        self.uncertaintyPanel.theta = theta
 
1346
        self.uncertaintyPanel.grid_1.CreateGrid(Neq+Npar,Neq+Npar)
 
1347
        self.uncertaintyPanel.parsCB.AppendItems(theta)
 
1348
        #self.uncertaintyPanel.createVarList(items) # re-create varList on uncertaintyMiniFrame
 
1349
        self.uncertaintyPanel.fileName = self.FileName #create local variable on Uncertainty panel with filename
 
1350
        dir,fname = os.path.split(self.FileName)#[1]
 
1351
        fname = fname[:-4]+'_unc.spec'
 
1352
        if fname in os.listdir(dir):
 
1353
            self.uncertaintyPanel.loadSpecs(fname)
 
1354
 
 
1355
    def OnWxFrame1Close(self, event):
 
1356
        """
 
1357
        Things to do before Exiting.
 
1358
        """
 
1359
        # Tries to close other windows if they exist
 
1360
        try:
 
1361
            DP.Close()
 
1362
        except (NameError, AttributeError):
 
1363
            pass
 
1364
        try:
 
1365
            self.PF.Close()
 
1366
        except (NameError, AttributeError):
 
1367
            pass
 
1368
        try:
 
1369
            MP.Close()
 
1370
        except (NameError, AttributeError):
 
1371
            pass
 
1372
        try:
 
1373
            self.TableOut.Close()
 
1374
        except (AttributeError, NameError):
 
1375
            pass
 
1376
        try:
 
1377
            self.uncertaintyPanel.Close()
 
1378
        except (AttributeError, NameError):
 
1379
            pass
 
1380
        sys.exit()
 
1381
 
 
1382
    def OnMenu2Items1Menu(self, event):
 
1383
        """
 
1384
        Call the quiver plot menu
 
1385
        """
 
1386
        if not self.ModLoaded:
 
1387
            dlg = wx.MessageDialog(self, 'Please Open or Create a Model First.',
 
1388
              'No Model Available', wx.OK | wx.ICON_INFORMATION)
 
1389
            try:
 
1390
                dlg.ShowModal()
 
1391
            finally:
 
1392
                dlg.Destroy()
 
1393
                return
 
1394
            
 
1395
            
 
1396
        if not self.modbuilt:
 
1397
            self.BuildModel()
 
1398
        if self.vnames:
 
1399
            QF.ch = self.vnames
 
1400
        else:
 
1401
            QF.ch = ['y[%s]'%i for i in xrange(self.Neq)]
 
1402
        #print self.Neq, self.FileName
 
1403
        Vector = QF.create(None)
 
1404
        Vector.modict = self.modict
 
1405
        Vector.initsCtrl.SetValue(self.modict['init'])
 
1406
        inits = array([float(i) for i in self.modict['init'].strip().split(' ')])
 
1407
        Vector.limitsCtrl.SetValue("%s %s %s %s"%(min(inits),min(inits)*10,min(inits),min(inits)*10))
 
1408
        Vector.Show()
 
1409
 
 
1410
    def OnToolBar1OpentoolTool(self, event):
 
1411
        return self.OnMenu1items0Menu(event)
 
1412
 
 
1413
    def OnToolBar1SavetoolTool(self, event):
 
1414
        return self.OnMenu1items1Menu(event)
 
1415
 
 
1416
    def OnMenu3MbhelpMenu(self, event):
 
1417
        helpviewer.main(['','MB.hhp'])
 
1418
        
 
1419
class Future:
 
1420
    """
 
1421
    By David Perry - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/84317
 
1422
    To run a function in a separate thread, simply put it in a Future:
 
1423
 
 
1424
    >>> A=Future(longRunningFunction, arg1, arg2 ...)
 
1425
 
 
1426
    It will continue on its merry way until you need the result of your function. 
 
1427
    You can read the result by calling the Future like a function, for example:
 
1428
 
 
1429
    >>> print A()
 
1430
 
 
1431
    If the Future has completed executing, the call returns immediately. 
 
1432
    If it is still running, then the call blocks until the function completes. 
 
1433
    The result of the function is stored in the Future, so subsequent calls to 
 
1434
    it return immediately.
 
1435
 
 
1436
    A few caveats:
 
1437
    Since one wouldn't expect to be able to change the result of a function, 
 
1438
    Futures are not meant to be mutable. This is enforced by requiring the 
 
1439
    Future to be "called", rather than directly reading __result. If desired, 
 
1440
    stronger enforcement of this rule can be achieved by playing 
 
1441
    with __getattr__ and __setattr__.
 
1442
 
 
1443
    The Future only runs the function once, no matter how many times you 
 
1444
    read it. You will have to re-create the Future if you want to re-run your 
 
1445
    function; for example, if the function is sensitive to the time of day.
 
1446
 
 
1447
    For more information on Futures, and other useful parallel programming 
 
1448
    constructs, read Gregory V. Wilson's _Practical Parallel Programming_.
 
1449
    """
 
1450
    def __init__(self,func,*param):
 
1451
        # Constructor
 
1452
        self.__done=0
 
1453
        self.__result=None
 
1454
        self.__status='working'
 
1455
 
 
1456
        self.__C=Condition()   # Notify on this Condition when result is ready
 
1457
 
 
1458
        # Run the actual function in a separate thread
 
1459
        self.__T=Thread(target=self.Wrapper,args=(func,param))
 
1460
        self.__T.setName("FutureThread")
 
1461
        self.__T.start()
 
1462
 
 
1463
    def __repr__(self):
 
1464
        return '<Future at '+hex(id(self))+':'+self.__status+'>'
 
1465
 
 
1466
    def __call__(self):
 
1467
        self.__C.acquire()
 
1468
        while self.__done==0:
 
1469
            self.__C.wait()
 
1470
        self.__C.release()
 
1471
        # We deepcopy __result to prevent accidental tampering with it.
 
1472
        a=copy.deepcopy(self.__result)
 
1473
        return a
 
1474
 
 
1475
    def Wrapper(self, func, param):
 
1476
        # Run the actual function, and let us housekeep around it
 
1477
        self.__C.acquire()
 
1478
        try:
 
1479
            self.__result=func(*param)
 
1480
        except:
 
1481
            self.__result="Exception raised within Future"
 
1482
        self.__done=1
 
1483
        self.__status=`self.__result`
 
1484
        self.__C.notify()
 
1485
        self.__C.release()