Newer
Older
# mload.py ---
#
# Filename: mload.py
# Description:
# Author: Subhasis
# Maintainer:HarshaRani
# Created: Fri Feb 8 09:38:40 2013 (+0530)
# Version:
# Last-Updated: Wed Oct 5 15:16:35 2017 (+0530)
# By: HarshaRani
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
# Update #: 213
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
# Utility to load models and detect filetype.
#
#
# Change log:
#
#
#
#
# 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 3, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
'''
Oct 5 : check is made for kkit model
-- Atleast one compartment should exists
-- Atleast 2 pool should exists
Oct 4 : clean up for python3
'''
import moose
from moose import neuroml
import mtypes
from mexception import FileLoadError
import posixpath
from os.path import basename
from os.path import splitext
from PyQt4 import QtGui, QtCore, Qt
from plugins.setsolver import *
from moose.SBML import *
from plugins.kkitOrdinateUtil import *
def loadGenCsp(target,filename,solver="gsl"):
target = target.replace(" ", "")
#Moving the model under /modelname/model and graphs under /model/graphs.
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
#This is passed while loading-time which will be easy for setting the stoich path
mpath = '/'+target+'/'+"model"
if moose.exists(mpath):
moose.delete(mpath)
modelpath1 = moose.Neutral('%s' %(target))
modelpath = moose.Neutral('%s/%s' %(modelpath1.path,"model"))
model = moose.loadModel(filename, modelpath.path,solver)
if not moose.exists(modelpath1.path+'/data'):
graphspath = moose.Neutral('%s/%s' %(modelpath1.path,"data"))
dataPath = moose.element(modelpath1.path+'/data')
i =0
nGraphs = moose.wildcardFind(modelpath.path+'/graphs/##[TYPE=Table2]')
for graphs in nGraphs:
if not moose.exists(dataPath.path+'/graph_'+str(i)):
graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i)))
else:
graphspath = moose.element(dataPath.path+'/graph_'+str(i))
moose.move(graphs.path,graphspath)
if len(nGraphs) > 0:
i = i+1
for moregraphs in moose.wildcardFind(modelpath.path+'/moregraphs/##[TYPE=Table2]'):
if not moose.exists(dataPath.path+'/graph_'+str(i)):
graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i)))
else:
graphspath = moose.element(dataPath.path+'/graph_'+str(i))
moose.move(moregraphs.path,graphspath)
if moose.exists(modelpath.path+'/info'):
AnnotatorOld = moose.element(modelpath.path+'/info')
AnnotatorNew = moose.Annotator(modelpath1.path+'/info')
AnnotatorNew.runtime = AnnotatorOld.runtime
AnnotatorNew.solver = AnnotatorOld.solver
moose.delete(AnnotatorOld)
moose.delete(modelpath.path+'/graphs')
moose.delete(modelpath.path+'/moregraphs')
return(modelpath1,modelpath1.path)
def loadFile(filename, target, solver="gsl", merge=True):
"""Try to load a model from specified `filename` under the element
`target`.
if `merge` is True, the contents are just loaded at target. If
false, everything is deleted from the parent of target unless the
parent is root.
Returns
-------
a dict containing at least these three entries:
modeltype: type of the loaded model.
subtype: subtype of the loaded model, None if no specific subtype
modelroot: root element of the model, None if could not be located - as is the case with Python scripts
"""
libsfound = True
model = '/'
newTarget = target
while moose.exists(newTarget):
newTarget = target + "-" + str(num)
num = num + 1
target = newTarget
istext = True
with open(filename, 'rb') as infile:
istext = mtypes.istextfile(infile)
if not istext:
print ('Cannot handle any binary formats yet')
# parent, child = posixpath.split(target)
# p = moose.Neutral(parent)
# if not merge and p.path != '/':
# for ch in p.children:
# moose.delete(ch)
try:
modeltype = mtypes.getType(filename)
subtype = mtypes.getSubtype(filename, modeltype)
except KeyError:
raise FileLoadError('Do not know how to handle this filetype: %s' % (filename))
pwe = moose.getCwe()
#self.statusBar.showMessage('Loading model, please wait')
# app = QtGui.qApp
# app.setOverrideCursor(QtGui.QCursor(Qt.Qt.BusyCursor)) #shows a hourglass - or a busy/working arrow
if modeltype == 'genesis':
if subtype == 'kkit' or subtype == 'prototype':
model,modelpath = loadGenCsp(target,filename,solver)
xcord,ycord = [],[]
if moose.exists(moose.element(modelpath).path):
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
process = True
compt = len(moose.wildcardFind(modelpath+'/##[ISA=CubeMesh]'))
if not compt:
loaderror = "Model has no compartment, atleast one compartment should exist to display the widget"
process = False
else:
p = len(moose.wildcardFind(modelpath+'/##[ISA=PoolBase]'))
if p < 2:
loaderror = "Model has no pool, atleast two pool should exist to display the widget"
process = False
if process:
if moose.exists(moose.element(modelpath).path):
mObj = moose.wildcardFind(moose.element(modelpath).path+'/##[ISA=PoolBase]'+','+
moose.element(modelpath).path+'/##[ISA=ReacBase]'+','+
moose.element(modelpath).path+'/##[ISA=EnzBase]'+','+
moose.element(modelpath).path+'/##[ISA=StimulusTable]')
for p in mObj:
if not isinstance(moose.element(p.parent),moose.CplxEnzBase):
xcord.append(moose.element(p.path+'/info').x)
ycord.append(moose.element(p.path+'/info').y)
recalculatecoordinatesforKkit(mObj,xcord,ycord)
for ememb in moose.wildcardFind(moose.element(modelpath).path+'/##[ISA=EnzBase]'):
objInfo = ememb.path+'/info'
#Enzyme's textcolor (from kkit) will be bgcolor (in moose)
if moose.exists(objInfo):
bgcolor = moose.element(objInfo).color
moose.element(objInfo).color = moose.element(objInfo).textColor
moose.element(objInfo).textColor = bgcolor
moose.Annotator(moose.element(modelpath).path+'/info').modeltype = "kkit"
else:
print (" path doesn't exists")
moose.le(modelpath)
print ('Only kkit and prototype files can be loaded.')
elif modeltype == 'cspace':
model,modelpath = loadGenCsp(target,filename)
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
if moose.exists(modelpath):
moose.Annotator((moose.element(modelpath).path+'/info')).modeltype = "cspace"
addSolver(modelpath,'gsl')
elif modeltype == 'xml':
if subtype == 'neuroml':
popdict, projdict = neuroml.loadNeuroML_L123(filename)
# Circus to get the container of populations from loaded neuroml
for popinfo in popdict.values():
for cell in popinfo[1].values():
solver = moose.HSolve(cell.path + "/hsolve")
solver.target = cell.path
# model = cell.parent
# break
# break
# Moving model to a new location under the model name
# model name is the filename without extension
model = moose.Neutral("/" + splitext(basename(filename))[0])
element = moose.Neutral(model.path + "/model")
if(moose.exists("/cells")) : moose.move("/cells" , element.path)
if(moose.exists("/elec")) : moose.move("/elec" , model.path)
if(moose.exists("/library")): moose.move("/library", model.path)
# moose.move("cells/", cell.path)
elif subtype == 'sbml':
foundLibSBML_ = False
try:
import libsbml
foundLibSBML_ = True
except ImportError:
pass
if foundLibSBML_:
if target != '/':
if moose.exists(target):
moose.delete(target)
model,loaderror = mooseReadSBML(filename,target)
if moose.exists(moose.element(model).path):
moose.Annotator(moose.element(model).path+'/info').modeltype = "sbml"
addSolver(target,'gsl')
libsfound = foundLibSBML_
else:
raise FileLoadError('Do not know how to handle this filetype: %s' % (filename))
moose.setCwe(pwe) # The MOOSE loadModel changes the current working element to newly loaded model. We revert that behaviour
# TODO: check with Aditya how to specify the target for
# neuroml reader
# app.restoreOverrideCursor()
return {'modeltype': modeltype,
'subtype': subtype,
'model': model,
'foundlib' : libsfound,
'loaderror' : loaderror}
#
# mload.py ends here