55# Maintainer: Dilawar Singh, Harsha Rani, Upi Bhalla
66
77import warnings
8+ import os
89import pydoc
910from io import StringIO
10- from os .path import splitext
1111from contextlib import closing
1212
1313# Import function from C++ module into moose namespace.
5858 mergechemImport_ = False
5959 mergechemError_ = '%s' % e
6060
61- def loadModel (filename , target ,method = None ):
62- solverClass = 'Neutral'
63- if method != None :
64- solverClass = method
65- try :
66- f = open (filename ,'r' )
67- f .close ()
68- except IOError as e :
69- print (e )
70- return
71- else :
72- file_name ,extension = splitext (filename )
73- if extension in [".swc" ,".p" ]:
74- ret = _moose .loadModel (filename ,target ,"Neutral" )
75- elif extension in [".g" ,".cspace" ]:
76- #only if genesis or cspace file, then mooseAddChemSolver is called
77- ret = _moose .loadModel (filename ,target ,"ee" )
78-
61+ def loadModel (filename , modelpath , solverclass = "gsl" ):
62+ """loadModel: Load model from a file to a specified path.
63+
64+ Parameters
65+ ----------
66+ filename: str
67+ model description file.
68+ modelpath: str
69+ moose path for the top level element of the model to be created.
70+ method: str
71+ solver type to be used for simulating the model.
72+ TODO: Link to detailed description of solvers?
73+
74+ Returns
75+ -------
76+ object
77+ moose.element if succcessful else None.
78+ """
79+
80+ if not os .path .isfile ( os .path .realpath (filename ) ):
81+ mu .warn ( "Model file '%s' does not exists or is not readable." % filename )
82+ return None
83+
84+ extension = os .path .splitext (filename )[1 ]
85+ if extension in [".swc" , ".p" ]:
86+ return _moose .loadModelInternal (filename , modelpath , "Neutral" )
87+
88+ if extension in [".g" , ".cspace" ]:
89+ # only if genesis or cspace file and method != ee then only
90+ # mooseAddChemSolver is called.
91+ ret = _moose .loadModelInternal (filename , modelpath , "ee" )
92+ sc = solverclass .lower ()
93+ if sc in ["gssa" ,"gillespie" ,"stochastic" ,"gsolve" ]:
94+ method = "gssa"
95+ elif sc in ["gsl" ,"runge kutta" ,"deterministic" ,"ksolve" ,"rungekutta" ,"rk5" ,"rkf" ,"rk" ]:
96+ method = "gsl"
97+ elif sc in ["exponential euler" ,"exponentialeuler" ,"neutral" ]:
98+ method = "ee"
99+ else :
79100 method = "ee"
80- if solverClass .lower () in ["gssa" ,"gillespie" ,"stochastic" ,"gsolve" ]:
81- method = "gssa"
82- elif solverClass .lower () in ["gsl" ,"runge kutta" ,"deterministic" ,"ksolve" ,"rungekutta" ,"rk5" ,"rkf" ,"rk" ]:
83- method = "gsl"
84- elif solverClass .lower () in ["exponential euler" ,"exponentialeuler" ,"neutral" ]:
85- method = "ee"
86-
87- if method != 'ee' :
88- chemError_ = _chemUtil .add_Delete_ChemicalSolver .mooseAddChemSolver (target ,method )
101+
102+ if method != 'ee' :
103+ chemError = _chemUtil .add_Delete_ChemicalSolver .mooseAddChemSolver (modelpath , method )
89104 return ret
105+ else :
106+ mu .error ( "Unknown model extenstion '%s'" % extension )
107+ return None
90108
91109# Version
92110def version ( ):
@@ -123,36 +141,42 @@ def version( ):
123141def mooseReadSBML (filepath , loadpath , solver = 'ee' ,validate = "on" ):
124142 """Load SBML model.
125143
126- keyword arguments: \n
127-
128- filepath -- filepath to be loaded \n
129- loadpath -- Root path for this model e.g. /model/mymodel \n
130- solver -- Solver to use (default 'ee' ) \n
131-
144+ Parameter
145+ --------
146+ filepath: str
147+ filepath to be loaded.
148+ loadpath : str
149+ Root path for this model e.g. /model/mymodel
150+ solver : str
151+ Solver to use (default 'ee').
152+ Available options are "ee", "gsl", "stochastic", "gillespie"
153+ "rk", "deterministic"
154+ For full list see ??
132155 """
133156 global sbmlImport_
134157 if sbmlImport_ :
135- return _readSBML .mooseReadSBML ( filepath , loadpath , solver ,validate )
158+ return _readSBML .mooseReadSBML (filepath , loadpath , solver , validate )
136159 else :
137160 print ( sbmlError_ )
138161 return False
139162
140-
141163def mooseWriteSBML (modelpath , filepath , sceneitems = {}):
142- """Writes loaded model under modelpath to a file in SBML format.
143-
144- keyword arguments:\n
145-
146- modelpath -- model path in moose e.g /model/mymodel \n
147- filepath -- Path of output file. \n
148- sceneitems -- dictlist (UserWarning: user need not worry about this) \n
149- layout position is saved in Annotation field of all the moose Object (pool,Reaction,enzyme)\n
150- If this function is called from \n
151- -- GUI, the layout position of moose object is passed \n
152- -- command line, \n
153- ---if genesis/kkit model is loaded then layout position is taken from the file \n
154- --- else, auto-coordinates is used for layout position and passed
155-
164+ """mooseWriteSBML: Writes loaded model under modelpath to a file in SBML format.
165+
166+ Parameters
167+ ----------
168+ modelpath : str
169+ model path in moose e.g /model/mymodel \n
170+ filepath : str
171+ Path of output file. \n
172+ sceneitems : dict
173+ UserWarning: user need not worry about this layout position is saved in
174+ Annotation field of all the moose Object (pool,Reaction,enzyme).
175+ If this function is called from
176+ * GUI - the layout position of moose object is passed
177+ * command line - NA
178+ * if genesis/kkit model is loaded then layout position is taken from the file
179+ * otherwise auto-coordinates is used for layout position.
156180 """
157181 if sbmlImport_ :
158182 return _writeSBML .mooseWriteSBML (modelpath , filepath , sceneitems )
@@ -161,13 +185,15 @@ def mooseWriteSBML(modelpath, filepath, sceneitems={}):
161185 return False
162186
163187
164- def mooseWriteKkit (modelpath , filepath ,sceneitems = {}):
188+ def mooseWriteKkit (modelpath , filepath , sceneitems = {}):
165189 """Writes loded model under modelpath to a file in Kkit format.
166190
167- keyword arguments:\n
168-
169- modelpath -- model path in moose \n
170- filepath -- Path of output file.
191+ Parameters
192+ ----------
193+ modelpath : str
194+ Model path in moose.
195+ filepath : str
196+ Path of output file.
171197 """
172198 global kkitImport_ , kkitImport_err_
173199 if not kkitImport_ :
@@ -178,10 +204,14 @@ def mooseWriteKkit(modelpath, filepath,sceneitems={}):
178204
179205
180206def mooseDeleteChemSolver (modelpath ):
181- """ deletes solver on all the compartment and its children.
182- This is neccesary while created a new moose object on a pre-existing modelpath,\n
183- this should be followed by mooseAddChemSolver for add solvers on to compartment to simulate else
184- default is Exponential Euler (ee)
207+ """mooseDeleteChemSolver
208+ deletes solver on all the compartment and its children.
209+
210+ Notes
211+ -----
212+ This is neccesary while created a new moose object on a pre-existing modelpath,
213+ this should be followed by mooseAddChemSolver for add solvers on to compartment
214+ to simulate else default is Exponential Euler (ee)
185215 """
186216 if chemImport_ :
187217 return _chemUtil .add_Delete_ChemicalSolver .mooseDeleteChemSolver (modelpath )
@@ -191,15 +221,17 @@ def mooseDeleteChemSolver(modelpath):
191221
192222
193223def mooseAddChemSolver (modelpath , solver ):
194- """ Add solver on chemical compartment and its children for calculation
224+ """mooseAddChemSolver:
225+ Add solver on chemical compartment and its children for calculation
195226
196- keyword arguments:\n
197-
198- modelpath -- model path that is loaded into moose \n
199- solver -- "Exponential Euler" (ee) (default), \n
200- "Gillespie" ("gssa"), \n
201- "Runge Kutta" ("gsl")
227+ Parameters
228+ ----------
202229
230+ modelpath : str
231+ Model path that is loaded into moose.
232+ solver : str
233+ Exponential Euler "ee" is default. Other options are Gillespie ("gssa"),
234+ Runge Kutta ("gsl") etc. Link to documentation?
203235 """
204236 if chemImport_ :
205237 chemError_ = _chemUtil .add_Delete_ChemicalSolver .mooseAddChemSolver (modelpath , solver )
@@ -209,9 +241,8 @@ def mooseAddChemSolver(modelpath, solver):
209241 return False
210242
211243def mergeChemModel (src , des ):
212- """ Merges two chemical model, \n
213- File or filepath can be passed
214- source is merged to destination
244+ """mergeChemModel: Merges two chemical model.
245+ File or filepath can be passed source is merged to destination
215246 """
216247 #global mergechemImport_
217248 if mergechemImport_ :
@@ -279,7 +310,6 @@ def le(el=None):
279310
280311ce = _moose .setCwe # ce is a GENESIS shorthand for change element.
281312
282-
283313def syncDataHandler (target ):
284314 """Synchronize data handlers for target.
285315
@@ -358,7 +388,6 @@ def showfield(el, field='*', showtype=False):
358388
359389def showfields (el , showtype = False ):
360390 """Convenience function. Should be deprecated if nobody uses it.
361-
362391 """
363392 warnings .warn (
364393 'Deprecated. Use showfield(element, field="*", showtype=True) instead.' ,
@@ -372,7 +401,6 @@ def showfields(el, showtype=False):
372401 ('sharedFinfo' , 'shared message field' ),
373402 ('lookupFinfo' , 'lookup field' )]
374403
375-
376404def listmsg (el ):
377405 """Return a list containing the incoming and outgoing messages of
378406 `el`.
0 commit comments