-
Notifications
You must be signed in to change notification settings - Fork 0
/
glmodel.py
442 lines (418 loc) · 17.3 KB
/
glmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
##################################
#
# glmodel.py
# Copyright (C) Louisiana State University, Health Sciences Center
# Written by 2011-2014 Ruben Tikidji-Hamburyan <[email protected]>
# Will Coleman 2013 [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
# NON INFRINGEMENT. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###################################
from PyQt4 import QtGui, QtCore
from xml.sax import handler, make_parser
import glprc, glrprc, glpopulation, glneurons, glconnection, glnoisyneurons, glrepeaters
import gloutput, glrun, icons
import glphasetable
import os
class glmodel:
class parcerpxm(handler.ContentHandler):
def __init__(self,model = None):
self.model = model
self.workobj = None
def startElement(self, name, attr):
if self.model == None: return
if name =="model":
self.workobj = self.model
self.workobj.startpoint(name, attr)
elif self.workobj != None:
self.workobj.startpoint(name, attr)
return
def endElement(self, name):
if self.workobj == None: return
elif name == "model" and self.workobj == self.model:
self.workobj = None
else:
self.workobj.stoppoint(name)
return
def __init__(self,parent=None, mainwnd = None, menubar = None, toolbar = None,filemenu = None):
#GUI paramters
self.parent = parent
self.model = None
#Acctions:
#New Model
newmdl = QtGui.QAction(QtGui.QIcon(':/new.png'), 'New Model', mainwnd)
newmdl.setShortcut('Ctrl+N')
newmdl.setStatusTip('Start New Model')
mainwnd.connect(newmdl, QtCore.SIGNAL('triggered()'), self.newmodel)
#Open Model
openmdl = QtGui.QAction(QtGui.QIcon(':/open.png'), 'Open Model', mainwnd)
openmdl.setShortcut('Ctrl+O')
openmdl.setStatusTip('Open Existing Model' )
mainwnd.connect(openmdl, QtCore.SIGNAL('triggered()'), self.openmodel)
#Save Model
savemdl = QtGui.QAction(QtGui.QIcon(':/save.png'), 'Save Model', mainwnd)
savemdl.setShortcut('Ctrl+S')
savemdl.setStatusTip('Save Current Model')
mainwnd.connect(savemdl, QtCore.SIGNAL('triggered()'), self.savemodel)
savemdlas = QtGui.QAction(QtGui.QIcon(':/save.png'), 'Save Model As', mainwnd)
savemdlas.setStatusTip('Save Current Model As')
mainwnd.connect(savemdlas, QtCore.SIGNAL('triggered()'), self.savemodelas)
#Close Model
exit = QtGui.QAction(QtGui.QIcon(':/close.png'), 'Close Model', mainwnd)
exit.setShortcut('Ctrl+W')
exit.setStatusTip('Close Model')
mainwnd.connect(exit, QtCore.SIGNAL('triggered()'), self.close)
#Run Model
runmdl = QtGui.QAction(QtGui.QIcon(':/media-playback-start.png'), 'Save and Run Model', mainwnd)
runmdl.setShortcut('Ctrl+R')
runmdl.setStatusTip('Save and Run Model')
mainwnd.connect(runmdl, QtCore.SIGNAL('triggered()'), self.runmodel)
showrst = QtGui.QAction(QtGui.QIcon(':/grath.png'), 'Show graphs', mainwnd)
showrst.setShortcut('Ctrl+G')
showrst.setStatusTip('Show graphical outputs')
mainwnd.connect(showrst, QtCore.SIGNAL('triggered()'), self.show)
showphs = QtGui.QAction(QtGui.QIcon(':/steadyphases.png'), 'Show phases', mainwnd)
#showrst.setShortcut('Ctrl+S')
showphs.setStatusTip('Show steady state phase relations')
mainwnd.connect(showphs, QtCore.SIGNAL('triggered()'), self.phases)
#insert output
#insoutmdl = QtGui.QAction(QtGui.QIcon(':/insert.png'), 'Insert new output from model', mainwnd)
#insoutmdl.setShortcut('Ctrl+Shift+O')
#insoutmdl.setStatusTip('Insert New output stream from model')
#mainwnd.connect(insoutmdl, QtCore.SIGNAL('triggered()'), self.newoutput)
#remove output
#rmoutmdl = QtGui.QAction(QtGui.QIcon(':/remove.png'), 'Remove output', mainwnd)
#rmoutmdl.setShortcut('Alt+Shift+O')
#rmoutmdl.setStatusTip('Remove output stream ')
#mainwnd.connect(rmoutmdl, QtCore.SIGNAL('triggered()'), self.removeoutput)
#remove
rmobj = QtGui.QAction(QtGui.QIcon(':/close.png'), 'Remove Object', mainwnd)
rmobj.setShortcut('Delete')
rmobj.setStatusTip('Remove selected object')
mainwnd.connect(rmobj, QtCore.SIGNAL('triggered()'), self.removeobject)
mainwnd.connect(mainwnd.tree, QtCore.SIGNAL('itemDoubleClicked(QTreeWidgetItem *,int)'), self.click)
filemenu.addAction(newmdl)
filemenu.addAction(openmdl)
filemenu.addAction(savemdl)
filemenu.addAction(savemdlas)
filemenu.addAction(exit)
toolbar.addAction(newmdl)
toolbar.addAction(openmdl)
toolbar.addAction(savemdl)
toolbar.addAction(exit)
toolbar.addSeparator()
editmu = menubar.addMenu('&Edit')
self.glprc = glprc.glprc( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glrprc = glrprc.glrprc( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glpopulation = glpopulation.glpopulation( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glneurons = glneurons.glneurons( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glnoisyneurons = glnoisyneurons.glnoisyneurons( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glrepeaters = glrepeaters.glrepeaters( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.glconnection = glconnection.glconnection( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.gloutput = gloutput.gloutput( parent=self, mainwnd=mainwnd, menubar=editmu, toolbar=toolbar )
self.registered = (self.glprc, self.glrprc, self.glpopulation, self.glneurons, self.glnoisyneurons,self.glrepeaters,self.glconnection,self.gloutput)
editmu.addAction(rmobj)
toolbar.addAction(rmobj)
toolbar.addSeparator()
run = menubar.addMenu('&Run')
run.addAction(runmdl)
toolbar.addAction(runmdl)
tools = menubar.addMenu('&Tools')
tools.addAction(showrst)
tools.addAction(showphs)
#Data
self.isactive = 0
self.name = "***"
self.maxspikes = -1
self.filename = None
self.workobj = None
self.ischanged = False
self.outfilename= None
# self.prclst = []
# self.neuronlst = []
# self.connectlst = []
def newmodel(self):
# print "newmodel"
if self.isactive:
msgBox = QtGui.QMessageBox.information(self.parent,"The Model is Active.","Do you want to save current model?",
QtGui.QMessageBox.Save, QtGui.QMessageBox.Discard, QtGui.QMessageBox.Cancel)
if msgBox == QtGui.QMessageBox.Cancel:
return
if msgBox == QtGui.QMessageBox.Save:
self.savemodel()
self.isactive = 0
self.close()
self.name, ok = QtGui.QInputDialog.getText(self.parent,
'uniPRCsim: New Model Name', 'Enter the name for new model:')
if not ok: return
self.root = QtGui.QTreeWidgetItem(self.parent.tree)
self.parent.tree.addTopLevelItem(self.root)
self.root.setText(1,'Model:')
self.root.setIcon(0,QtGui.QIcon(':/model.png'))
self.nameedit=QtGui.QLineEdit(self.parent.tree)
self.parent.tree.setItemWidget(self.root,2,self.nameedit)
self.nameedit.setText(self.name)
self.parent.connect(self.nameedit, QtCore.SIGNAL('editingFinished()'), self.selected)
self.version = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.version)
self.version.setText(1,'Version:')
self.version.setText(2,'0.1')
self.countspike = QtGui.QSpinBox(self.parent.tree)
self.countspike.setMinimum(-1)
self.countspike.setMaximum(100000000)
self.countspike.setValue(5000)
self.countspikewid = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.countspikewid)
self.countspikewid.setText(1,'Number of spikes:')
self.parent.tree.setItemWidget(self.countspikewid,2,self.countspike)
self.seededit = QtGui.QSpinBox(self.parent.tree)
self.seededit.setMinimum(-1)
self.seededit.setMaximum(100000000)
self.seededit.setValue(-1)
self.seededitwid = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.seededitwid)
self.seededitwid.setText(1,'SEED:')
self.parent.tree.setItemWidget(self.seededitwid,2,self.seededit)
self.parent.setWindowTitle(self.name+' :: uniPRCsim')
self.isactive = 1
def readfile(self, filename):
self.name = "<----->"
if len(filename) == 0: return
dirname,self.filename = os.path.split( filename.toUtf8().data() )
if len(dirname) > 2 :os.chdir(dirname)
pparser = self.parcerpxm(model=self)
parser=make_parser()
parser.setContentHandler(pparser)
parser.parse(self.filename)
self.parent.setWindowTitle(self.name+' :: uniPRCsim')
self.isactive = 1
self.parent.tree.expandAll()
def openmodel(self):
self.close()
filename = QtGui.QFileDialog.getOpenFileName(self.parent, 'Open file', '', "Prc Xml Model(*.pxm)")
if filename.length() < 1: return
self.readfile(filename)
def savemodel(self):
#self.maxspikes = self
if self.filename == None:
self.savemodelas()
return
fd = open(self.filename,"w")
fd.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n")
fd.write("<model version=\"0.1\" name=\"%s\">\n"%self.name)
fd.write("\t<simulation ")
if self.countspike.value() >= 1:
fd.write("maxspikes=\"%d\" "%self.countspike.value())
if self.seededit.value() >= 1:
fd.write("SEED=\"%d\" "%self.seededit.value())
fd.write(" />\n")
for prog in self.registered:
for prn in prog.save(): fd.write("\t"+prn+"\n")
fd.write("</model>")
self.ischanged = False
def savemodelas(self):
filename = QtGui.QFileDialog.getSaveFileName(self.parent, 'Open file', '', "Prc Xml Model(*.pxm)")
if filename.length() < 1: return
dirname,self.filename = os.path.split( filename.toUtf8().data() )
if len(dirname) > 2 :os.chdir(dirname)
self.parent.setWindowTitle(self.name+' :: uniPRCsim')
self.savemodel()
def close(self):
ischanged = self.ischanged | reduce(lambda x,y: x+y.ischanged, self.registered, False)
if ischanged:
msgBox = QtGui.QMessageBox.information(self.parent,"uniPRCsim The Model is Active.","The model \"%s\" has been modified.\nDo you want to save current model?"%self.name,
QtGui.QMessageBox.Save, QtGui.QMessageBox.Discard, QtGui.QMessageBox.Cancel)
if msgBox == QtGui.QMessageBox.Cancel: return
elif msgBox == QtGui.QMessageBox.Save:
self.savemodel()
self.isactive = 0
for item in self.registered:
item.clean()
self.parent.tree.clear()
self.isactive = 0
self.name = "***"
self.maxspikes = -1
self.filename = None
self.workobj = None
self.ischanged = False
self.parent.setWindowTitle('uniPRCsim')
self.countspike = None
self.seededit = None
def runmodel(self):
####RTH: There is a bug in this code.
#### Should be fixed soon!!!!!
#hasWarningMessageBeenDisplayed = 0
#for con in self.glconnection.cnntlst:
#for nrn in self.glneurons.nrnlst:
#gsyn_sum_max = con.gsyn * nrn.number
#if(con.to != nrn.name): continue
#for prc in self.glprc.prclst:
#if prc.name != con.prc: continue
#if not hasattr(prc, 'data'): continue
#for iter in xrange(1, len(prc.data[0])):
#if prc.data[0][iter] : # Check for empty lists
#order+=1
#count=0
#while(count<order):
#datum = 0
#while datum < len(prc.data):
#if(0> float(prc.data[datum][0]) + (gsyn_sum_max*float(prc.data[datum][count+1][0]))):
##print (float(prc.data[datum][0]) + (gsyn_sum_max*float(prc.data[datum][count+1][0])))
#if(hasWarningMessageBeenDisplayed == 0):
#reply = QtGui.QMessageBox.warning(self.parent, 'Warning',\
#"You are about to run a model in which the causal limit has the potential to be crossed.\n\nDo you want to proceed?", \
#QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok)
#if reply != QtGui.QMessageBox.Ok: return
#hasWarningMessageBeenDisplayed = 1
#datum+=1
#count += 1
if not self.isactive: return
saveflg = self.ischanged
for comp in self.registered:
saveflg |= comp.ischanged
####DB####
#for comp in self.registered:
# print comp.object,": ischanged=",comp.ischanged
##########
if saveflg:
msgBox = QtGui.QMessageBox.information(self.parent,"uniPRCsim The Model is Active.","The model \"%s\" has been modified.\nDo you want to save current model?"%self.name,
QtGui.QMessageBox.Save, QtGui.QMessageBox.Discard, QtGui.QMessageBox.Cancel)
if msgBox == QtGui.QMessageBox.Cancel: return
elif msgBox == QtGui.QMessageBox.Save:
self.savemodel()
self.ischanged = False
if len(self.glprc.prclst) == 0 and len(self.glrprc.prclst) == 0:
QtGui.QMessageBox.critical(self.parent,"Critical ERROR!"," You should have at least one PRC function! ",QtGui.QMessageBox.Ok,0)
return
if len(self.glpopulation.poplst) == 0 and len(self.glneurons.nrnlst) == 0 and len(self.glnoisyneurons.nrnlst) == 0:
QtGui.QMessageBox.critical(self.parent,"Critical ERROR!"," You should have at least one Population or neuron set! ",QtGui.QMessageBox.Ok,0)
return
if len(self.glconnection.cnntlst) == 0:
QtGui.QMessageBox.critical(self.parent,"Critical ERROR!"," You should have at least one connection! ",QtGui.QMessageBox.Ok,0)
return
if len(self.gloutput.outlst) == 0:
QtGui.QMessageBox.critical(self.parent,"Critical ERROR!"," You should have at least one Output stream! ",QtGui.QMessageBox.Ok,0)
return
runer = glrun.glrun(self.name,self.filename,self.countspike.value())
if not runer.exec_(): return
self.gloutput.show()
def show(self):
self.gloutput.show()
def phases(self):
for out in self.gloutput.outlst:
if out.watch != "spikes": continue
if out.format != "excel": continue
if out.id == None: continue
filename = self.gloutput.getfilename(out)
phasetbl = glphasetable.glphasetable(filename=filename,parent=self.parent)
phasetbl.exec_()
def newoutput(self):
print "new output"
def removeoutput(self):
print "new output"
def removeobject(self):
if not self.isactive: return
item = self.parent.tree.currentItem()
for gl in self.registered:
if item.data(1,QtCore.Qt.UserRole) == gl.object:
gl.remove(item)
self.root.removeChild(item)
def selected(self):
self.name = self.nameedit.text()
self.parent.setWindowTitle(self.name+' :: uniPRCsim')
def click(self,item,col):
if item == None: return
for prog in self.registered:
if item.data(1, QtCore.Qt.UserRole) == prog.object:
prog.click()
break
####TODO: ADD Other
def startpoint(self, name, attr ={}):
if self.workobj != None:
self.workobj.startpoint(name,attr)
elif name == "model":
self.workobj = None
if attr.get("version",0) == 0 or attr["version"] != "0.1":
QtGui.QMessageBox.critical(self.parent,"Critical ERROR!","Bad version of model\n ABORT! ",QtGui.QMessageBox.Ok,0)
return
if attr.get("name",0): self.name = attr["name"]
else:self.name = "<--*-->"
self.root = QtGui.QTreeWidgetItem(self.parent.tree)
self.parent.tree.addTopLevelItem(self.root)
self.root.setText(1,'Model:')
self.root.setIcon(0,QtGui.QIcon(':/model.png'))
self.nameedit=QtGui.QLineEdit(self.parent.tree)
self.parent.tree.setItemWidget(self.root,2,self.nameedit)
self.nameedit.setText(self.name)
self.parent.connect(self.nameedit, QtCore.SIGNAL('editingFinished()'), self.selected)
self.version = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.version)
self.version.setText(1,'Version:')
self.version.setText(2,'0.1')
self.countspike = QtGui.QSpinBox(self.parent.tree)
self.countspike.setMinimum(-1)
self.countspike.setMaximum(100000000)
self.countspike.setValue(5000)
self.countspikewid = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.countspikewid)
self.countspikewid.setText(1,'Number of spikes:')
self.parent.tree.setItemWidget(self.countspikewid,2,self.countspike)
self.seededit = QtGui.QSpinBox(self.parent.tree)
self.seededit.setMinimum(-1)
self.seededit.setMaximum(100000000)
self.seededit.setValue(-1)
self.seededitwid = QtGui.QTreeWidgetItem(self.root)
self.root.addChild(self.seededitwid)
self.seededitwid.setText(1,'SEED:')
self.parent.tree.setItemWidget(self.seededitwid,2,self.seededit)
elif name == "simulation" :
if attr.get("maxspikes",0):
if int(attr["maxspikes"]) >= 1 :
self.countspike.setValue( int(attr["maxspikes"]) )
if attr.get("SEED",0):
if int(attr["SEED"]) >= 1 :
self.seededit.setValue( int(attr["SEED"]) )
elif self.workobj == None:
for prog in self.registered:
if name == prog.object:
prog.startpoint(name,attr)
self.workobj = prog
break
# if self.workobj == None
return
def stoppoint(self, name):
if self.workobj != None and name == self.workobj.object:
self.workobj.stoppoint(name)
self.workobj = None
elif name == "model" :#or name == "simulation" or name == "output"
return
elif self.workobj != None:
self.workobj.stoppoint(name)
return
#QtGui.QMessageBox.critical(self.parent,"Critical ERROR!","Bad or unexpected tag slosing <%s>!"%name,QtGui.QMessageBox.Ok,0)
#print "close item <%s>"%name
return
def getnames(self, lst=[]):
result = []
if len(lst) == 0:
for prog in self.registered:
result += prog.getnames()
else:
for name in lst:
for prog in self.registered:
if name != prog.object: continue
result += prog.getnames()
return result