1
# -*- coding:latin-1 -*-
2
#-----------------------------------------------------------------------------
6
# Author: <Flavio Codeco Coelho>
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.
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.
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.
25
#-----------------------------------------------------------------------------
27
from __future__ import division
29
wxversion.select('2.6')
32
#from Numeric import *
33
from threading import *
34
from scipy import integrate
35
#from RandomArray import *
36
from numpy.random import *
41
matplotlib.use('WXAgg')
43
from matplotlib.cbook import *
44
from wxPython.tools import helpviewer
45
#from xml.dom import Node
48
import wxFrame2, about
51
from odexml import Model
52
import PlotFigure as PF
53
from Bayes import Melding as meld
56
import quivVarFrame as QF
57
from lhsframe import LHS
63
return wxFrame1(parent)
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)]
77
[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2,
78
wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4,
79
] = [wx.NewId() for _init_coll_menu1_Items in xrange(5)]
81
[wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1,
82
wxID_WXFRAME1TOOLBAR1TOOLS2,
83
] = [wx.NewId() for _init_coll_toolBar1_Tools in xrange(3)]
85
[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL,
86
] = [wx.NewId() for _init_coll_menu2_Items in xrange(2)]
88
[wxID_WXFRAME1MENU3ITEMS0] = [wx.NewId() for _init_coll_menu3_Items in xrange(1)]
90
[wxID_WXFRAME1TOOLBAR1OPENTOOL, wxID_WXFRAME1TOOLBAR1SAVETOOL,
91
wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1,
92
wxID_WXFRAME1TOOLBAR1TOOLS2,
93
] = [wx.NewId() for _init_coll_toolBar1_Tools in range(5)]
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)]
107
[wxID_WXFRAME1MENU2ITEMS0, wxID_WXFRAME1MENU2QUIVERPL,
108
] = [wx.NewId() for _init_coll_menu2_Items in range(2)]
110
[wxID_WXFRAME1MENU1ITEMS0, wxID_WXFRAME1MENU1ITEMS1, wxID_WXFRAME1MENU1ITEMS2,
111
wxID_WXFRAME1MENU1ITEMS3, wxID_WXFRAME1MENU1ITEMS4,
112
] = [wx.NewId() for _init_coll_menu1_Items in range(5)]
114
[wxID_WXFRAME1MENU3ITEMS0, wxID_WXFRAME1MENU3MBHELP,
115
] = [wx.NewId() for _init_coll_menu3_Items in range(2)]
117
class wxFrame1(wx.Frame):
119
def _init_coll_boxSizerH_Items(self, parent):
120
# generated method, don't edit
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)
126
def _init_coll_bsPanel_Items(self, parent):
127
# generated method, don't edit
129
parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND)
131
def _init_coll_gridBagSizer1_Items(self, parent):
132
# generated method, don't edit
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,
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,
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,
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,
149
parent.AddWindow(self.firstStepText, (6, 2), border=0, flag=0, span=(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,
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))
161
def _init_coll_boxSizerV2_Items(self, parent):
162
# generated method, don't edit
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)
169
def _init_coll_menuBar1_Menus(self, parent):
170
# generated method, don't edit
172
parent.Append(menu=self.menu1, title='File')
173
parent.Append(menu=self.menu2, title='Analysis')
174
parent.Append(menu=self.menu3, title='Help')
176
def _init_coll_menu3_Items(self, parent):
177
# generated method, don't edit
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)
190
def _init_coll_menu1_Items(self, parent):
191
# generated method, don't edit
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)
214
def _init_coll_menu2_Items(self, parent):
215
# generated method, don't edit
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)
228
def _init_coll_toolBar1_Tools(self, parent):
229
# generated method, don't edit
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)
268
def _init_coll_statusBar1_Fields(self, parent):
269
# generated method, don't edit
270
parent.SetFieldsCount(1)
272
parent.SetStatusText(number=0, text='Status')
274
parent.SetStatusWidths([-1])
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)
284
self.bsPanel = wx.BoxSizer(orient=wx.VERTICAL)
286
self.boxSizerH = wx.BoxSizer(orient=wx.HORIZONTAL)
288
self.boxSizerV2 = wx.BoxSizer(orient=wx.VERTICAL)
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)
295
self.SetSizer(self.bsPanel)
297
def _init_utils(self):
298
# generated method, don't edit
299
self.menu1 = wx.Menu(title='File')
301
self.menu3 = wx.Menu(title='Help')
303
self.menuBar1 = wx.MenuBar()
305
self.menu2 = wx.Menu(title='Analysis')
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)
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')
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)
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)
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)
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)
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))
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)
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')
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')
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')
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)
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')
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),
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.')
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)
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)')
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).')
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)
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)')
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)
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.')
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)
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,
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], ...)')
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)
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.')
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),
459
self.eqText.SetThemeEnabled(True)
460
self.eqText.SetBestFittingSize(wx.Size(465, 22))
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))
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)
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)
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),
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)
484
self._init_coll_toolBar1_Tools(self.toolBar1)
488
def __init__(self, parent):
489
self._init_ctrls(parent)
491
self.ModLoaded = None #no model has been loaded
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
505
def OnMenu1items0Menu(self, event):
509
self.modtree = Model() #reset the modtree object upon the opening of a new model
511
os.chdir(self.curdir)
512
dlg = wx.FileDialog(self, "Choose a file", self.curdir, "", "*.ode", wx.OPEN)
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
519
self.modict = pickle.load(f)
521
self.FileName = filename
522
self.SetTitle('Model Builder - '+filename)
527
self.modict.update(self.modtree.modict)
529
self.FileName = filename
530
self.SetTitle('Model Builder - '+os.path.split(filename)[1])
533
d = pickle.load(self.checkExt(filename,'.dat'))
534
self.modict['results']=d[0]
535
self.modict['trange']=d[1]
538
self.curdir = os.path.split(filename)[0]
540
self.statusBar1.SetStatusText('Model loaded.')
541
#print self.modict.keys()
546
fill GUI from model dictionary
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
556
def checkExt(self,fn,ext):
558
Add a filename extension to the filename or replace
561
ext: desired extension
563
head,tail = os.path.split(fn)
564
orext = '.'+tail.split('.')[-1]
567
if orext and (ext != orext):
568
tail = tail.replace(orext,ext)
572
def OnMenu1items1Menu(self, event):
576
self.modtree = Model()
577
if not self.FileName:
578
return self.OnMenu1items2Menu(event)
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
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)
590
# Saving in xml format
592
self.modict.pop('results')
593
self.modict.pop('trange')
595
self.modtree.Create(**self.modict)
596
self.modtree.saveFile(self.checkExt(filename,'.ode'))
598
self.statusBar1.SetStatusText('Model saved.')
599
#print self.modict.keys()
602
def OnMenu1items2Menu(self, event):
606
self.modtree = Model()
607
dlg = wx.FileDialog(self, "Save File As", self.curdir, "", "*.ode", wx.SAVE)
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'):
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)
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)
625
# Saving in xml format
626
#print self.modict.keys()
628
self.modict.pop('results')
629
self.modict.pop('trange')
632
self.modtree.Create(**self.modict)
633
self.modtree.saveFile(self.checkExt(filename,'.ode'))
635
self.statusBar1.SetStatusText('Model saved.')
641
def OnMenu1items3Menu(self, event):
643
Close model menu entry.
645
self.modtree = Model()
647
self.ModLoaded = None
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')
658
def OnMenu1items4Menu(self, event):
667
def OnMenu2items0Menu(self, event):
669
If Uncertainty Analysis has been selected, it will Open the uncertainty panel to set
670
the parameters for the analysis.
672
if not self.ModLoaded:
673
self.OnMenu1items0Menu(event)
675
#--Checks if the Uncertainty item in the analysis menu (2) has been checked------
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
684
self.uncertainty = 0 #Uncertainty mode off
686
self.uncertaintyPanel.Close()
691
def checkErrors(self):
693
Check for normal editing errors in model definition
695
while not self.eqnEdit.GetValue()=='':
696
val = self.eqnEdit.GetValue().strip().splitlines()
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
705
if not self.parEdit.GetValue() == '':
706
valp = self.parEdit.GetValue().strip().splitlines()
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
720
#---Check number of initial conditions----------------------------------------------------------------------------
721
if self.initValInput.GetValue().strip() == '':
724
ni = len(self.initValInput.GetValue().strip().split(' '))
728
#---Check that all initial conditions are numbers----------------------------------------------------------------------------
731
float(self.initValInput.GetValue().strip().split(' ')[i])
735
#---Check syntax of equations----------------------------------------------------------------------------
736
y=zeros(Neq)#fake equation array
737
p=zeros(Npar)#fake parameter array
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.
745
#create fake variables
748
#replace occurrences of parameter names in equations by p[i]
755
except (SyntaxError, NameError), details:
762
def OnToolbar1tools0Tool(self, 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.
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]
776
self.ceqs = [compile(i,'<string>','eval') for i in eql]
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)
785
#---checking for errors----------------------------------------------------------------------------
786
e = self.checkErrors()
788
dlg = wx.MessageDialog(self, 'Wrong number of initial condition values.',
789
'Initial Condition Error', wx.OK | wx.ICON_INFORMATION)
796
dlg = wx.MessageDialog(self, 'There is a syntax error on the initial conditions box.',
797
'Syntax Error', wx.OK | wx.ICON_INFORMATION)
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)
814
if self.uncertainty == 0:
815
self.BuildModel(r=1) #regular run
816
self.modRan = 1 #set the flag indicating model has been run
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
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)
832
def OnToolbar1tools0ToolRclicked(self, event):
835
def updateGauge(self,t):
837
This function simply updates the progress gauge
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
844
def Equations(self, y, t):
846
This function defines the system of differential equations, evaluating
847
each line of the equation text box as ydot[i]
856
#print "vnames:", self.vnames, self.pnames
859
exec('%s=%s'%(','.join(self.vnames),list(y)))
861
ydot = zeros((Neq),'d') #initialize ydot
862
p = zeros((Npar),'d') #initialize p
863
#---Create Parameter Array----------------------------------------------------------------------------
865
if self.uncertainty == 0:
867
#par.GetValue().strip().split('\n')
868
if pars: #only if there is at least one parameter
869
for j in xrange(len(pars)):
873
p[j] = eval(pars[j]) #initialize parameter values
877
for i in xrange(Npar):
879
exec("%s=%s"%(self.pnames[i],par[i][self.run]))
881
p[i] = par[i][self.run] # Get a new set of parameters for each repeated run
882
#---Create equation array----------------------------------------------------------------------------
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
890
def compilePars(self):
892
compile parameter expressions
894
if self.parEdit.GetValue().strip() =="":
897
pars = self.parEdit.GetValue().strip().splitlines()
898
self.pnames = [p.split('=')[0] for p in pars if '=' in p]
900
#in this case returns the compete expression, including the '='
903
self.cpars = [compile(i,'<parameter>','exec') for i in pars]
904
except (SyntaxError),details:
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)
914
self.cpars = [compile(i,'<parameter>','eval') for i in pars]
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)
924
def OnToolbar1tools1Tool(self, event):
926
Show a figure with the equations typeset
928
#---translate equations to pseudo-TeX notation----------------------------------------------------------------------------
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:
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]
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'$'
952
eq[i] = r'$dY_%s/dt = '%i + eq[i] +r'$'
954
#---plot equations----------------------------------------------------------------------------
956
DP.SetTitle('Model '+str(self.FileName)[:-4]+': Equations')
957
DP.plotEquation(eqlist)
961
def plotOutput(self, x, y):
963
Plots the results of a single run.
964
Raises the flag that the plot has been created
966
self.PF = PF.create(None)
967
self.PF.plot_data(x,y,self.vnames)
973
def plotMelding(self,x,y,tit='Results'):
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.
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
984
nruns = len(y) # Number of runs
986
## SPF = PF.create(None)
987
## SPF.plot_data(x,y)
991
runs_byvar = [] #List of arrays that will contain all runs for each given variable
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).
996
for v in xrange(nvar):
997
runs_byvar.append(array([yt[i][v] for i in xrange(nruns)]))
1001
# TODO: Turn medianruns into a function
1002
medianRuns = [median(i) for i in runs_byvar]
1003
self.statusBar1.SetStatusText('Done!\n\n')
1007
#---95% Limits----------------------------------------------------------
1008
# TODO: Turn calculation of limits into a function
1009
self.statusBar1.SetStatusText('Calculating credibility intervals...\n')
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])
1020
ts = (medianRuns,ll,ul)
1021
self.statusBar1.SetStatusText('Done!\n\n')
1023
#---testing---------------------------------------------------------------------
1024
## med = medianRuns[0]-medianRuns[1]
1025
## SPF = PF.create(None)
1028
#-------------------------------------------------------------------------------
1031
TP = PF.create(None)
1033
TP.plotStats(x,ts,self.vnames)
1038
return (x,runs_byvar)
1041
def OnToolbar1tools2Tool(self, event):
1046
self.TableOut = wxFrame2.create(None)
1047
output = self.modict['results'] # get output from model's dictionary
1048
x = self.modict['trange']
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)
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
1065
self.TableOut.table.SetColLabelValue(j,self.vnames[j-1])
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])
1076
def BuildModel(self, r=0):
1078
Constructs the model from the input fields and runs it if r==1
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():
1104
if self.convcheckBox.GetValue():
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]
1113
while not self.eqnEdit.GetValue()=='':#get number of ODEs
1114
val = self.eqnEdit.GetValue().strip().split('\n')
1119
self.Neq = Neq#to be used by Equations
1121
self.sw = wx.StopWatch()
1122
self.gauge1.SetValue(0)
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')
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
1139
#-------------Multiple runs (uncertainty analysis)------------------------------------
1140
self.uncRun(init_conds,t_range,cm)
1143
t = gmtime(self.sw.Time()/1000)
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)
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
#-------------------------------------------------------------------------------
1157
def uncRun(self, init_conds,t_range,cm):
1159
Runs the model in uncertainty mode
1161
Npar = len(self.uncertaintyPanel.theta)
1162
nr= int(self.uncertaintyPanel.specs['samples']) # Get K from uncertaity panel
1165
if self.uncertaintyPanel.anaChoice.GetStringSelection() == 'Uncertainty':
1166
self.parMeld = self.uncertaintyPanel.priords[:Npar] # List of parameter priors. Excluding state variables]
1168
self.parMeld = self.uncertaintyPanel.priords
1169
self.par = self.parMeld #to be used by Equations
1170
self.Npar = len(self.par)
1172
for i in xrange(nr):
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
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
1187
def Bmeld(self,t,ModOut):
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.
1194
dlg = wx.TextEntryDialog(self, 'for which time do you want to run the Melding', 'Choose Time', str(t[-1]))
1196
if dlg.ShowModal() == wx.ID_OK:
1197
answer = dlg.GetValue()
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----------------------------------------------------------------------------
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]
1231
DP = PF.create(None)
1232
DP.SetTitle('Prior distributions for the parameters')
1233
DP.plotDist(allpriors,vname)
1236
#---Plot posteriors of theta----------------------------------------------------------------------------
1237
MP = PF.create(None)
1238
MP.SetTitle('Theta posteriors')
1239
MP.plotMeldout(meldout, self.pnames)
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')
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).
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)
1258
def postPHI(self,meldout,L):
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.
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)
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
1276
return (t_range,t_courseList)
1277
def Equations2(self,y,t):
1279
Variation of the Equations function to calculate the posterior of phi
1282
pars = self.post_theta
1283
Npar = len(pars) #get number of parameters
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):
1291
exec("%s=%s"%(self.pnames[i],pars[i][self.run]))
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
1306
def OnMenu3items0Menu(self, event):
1308
Opens the about window
1310
dlg = about.wxDialog1(self)
1316
def initUncertainty(self):
1318
Initializes the values on the uncertainty Analysis Panel based on
1322
while not self.eqnEdit.GetValue()=='':#get number of ODEs
1323
val = self.eqnEdit.GetValue().strip().split('\n')
1327
if not self.parEdit.GetValue() == '':#get number of parameters
1328
valp = self.parEdit.GetValue().strip().split('\n')
1336
phi = ["Y[%s]" % str(i) for i in xrange(Neq)]
1342
theta = ['P[%s]' % str(i) for i in xrange(Npar)]
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)
1355
def OnWxFrame1Close(self, event):
1357
Things to do before Exiting.
1359
# Tries to close other windows if they exist
1362
except (NameError, AttributeError):
1366
except (NameError, AttributeError):
1370
except (NameError, AttributeError):
1373
self.TableOut.Close()
1374
except (AttributeError, NameError):
1377
self.uncertaintyPanel.Close()
1378
except (AttributeError, NameError):
1382
def OnMenu2Items1Menu(self, event):
1384
Call the quiver plot menu
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)
1396
if not self.modbuilt:
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))
1410
def OnToolBar1OpentoolTool(self, event):
1411
return self.OnMenu1items0Menu(event)
1413
def OnToolBar1SavetoolTool(self, event):
1414
return self.OnMenu1items1Menu(event)
1416
def OnMenu3MbhelpMenu(self, event):
1417
helpviewer.main(['','MB.hhp'])
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:
1424
>>> A=Future(longRunningFunction, arg1, arg2 ...)
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:
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.
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__.
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.
1447
For more information on Futures, and other useful parallel programming
1448
constructs, read Gregory V. Wilson's _Practical Parallel Programming_.
1450
def __init__(self,func,*param):
1454
self.__status='working'
1456
self.__C=Condition() # Notify on this Condition when result is ready
1458
# Run the actual function in a separate thread
1459
self.__T=Thread(target=self.Wrapper,args=(func,param))
1460
self.__T.setName("FutureThread")
1464
return '<Future at '+hex(id(self))+':'+self.__status+'>'
1468
while self.__done==0:
1471
# We deepcopy __result to prevent accidental tampering with it.
1472
a=copy.deepcopy(self.__result)
1475
def Wrapper(self, func, param):
1476
# Run the actual function, and let us housekeep around it
1479
self.__result=func(*param)
1481
self.__result="Exception raised within Future"
1483
self.__status=`self.__result`