Skip to content

Commit 2624d4b

Browse files
dilawarhrani
authored andcommitted
Continuation to PR #308 (#340)
* Fixed docstrings of some functions. * Fixed attribute tests.
1 parent 0997a46 commit 2624d4b

File tree

5 files changed

+119
-85
lines changed

5 files changed

+119
-85
lines changed

pymoose/moosemodule.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,10 +1765,12 @@ PyObject * moose_exists(PyObject * dummy, PyObject * args)
17651765
return Py_BuildValue("i", Id(path) != Id() || string(path) == "/" || string(path) == "/root");
17661766
}
17671767

1768-
PyDoc_STRVAR(moose_loadModel_documentation,
1769-
"loadModel(filename, modelpath, solverclass) -> vec\n"
1768+
PyDoc_STRVAR(moose_loadModelInternal_documentation,
1769+
"loadModelInternal(filename, modelpath, solverclass) -> vec\n"
17701770
"\n"
17711771
"Load model from a file to a specified path.\n"
1772+
"Note: This function should not be used by users. It is meants for developers. \n"
1773+
"Please see `moose.loadModel` function.\n"
17721774
"\n"
17731775
"Parameters\n"
17741776
"----------\n"
@@ -1785,11 +1787,11 @@ PyDoc_STRVAR(moose_loadModel_documentation,
17851787
" loaded model container vec.\n"
17861788
);
17871789

1788-
PyObject * moose_loadModel(PyObject * dummy, PyObject * args)
1790+
PyObject * moose_loadModelInternal(PyObject * dummy, PyObject * args)
17891791
{
17901792
char * fname = NULL, * modelpath = NULL, * solverclass = NULL;
17911793

1792-
if(!PyArg_ParseTuple(args, "ss|s:moose_loadModel", &fname, &modelpath, &solverclass))
1794+
if(!PyArg_ParseTuple(args, "ss|s:moose_loadModelInternal", &fname, &modelpath, &solverclass))
17931795
{
17941796
cout << "here in moose load";
17951797
return NULL;
@@ -1812,7 +1814,8 @@ PyObject * moose_loadModel(PyObject * dummy, PyObject * args)
18121814
PyObject * ret = reinterpret_cast<PyObject*>(model);
18131815
return ret;
18141816
}
1815-
/*
1817+
1818+
#if 0
18161819
PyDoc_STRVAR(moose_saveModel_documentation,
18171820
"saveModel(source, filename) -> None\n"
18181821
"\n"
@@ -1869,7 +1872,8 @@ PyObject * moose_saveModel(PyObject * dummy, PyObject * args)
18691872
SHELLPTR->doSaveModel(model, filename);
18701873
Py_RETURN_NONE;
18711874
}
1872-
*/
1875+
#endif
1876+
18731877
PyObject * moose_setCwe(PyObject * dummy, PyObject * args)
18741878
{
18751879
PyObject * element = NULL;
@@ -3013,7 +3017,7 @@ static PyMethodDef MooseMethods[] =
30133017
{"stop", (PyCFunction)moose_stop, METH_VARARGS, "Stop simulation"},
30143018
{"isRunning", (PyCFunction)moose_isRunning, METH_VARARGS, "True if the simulation is currently running."},
30153019
{"exists", (PyCFunction)moose_exists, METH_VARARGS, "True if there is an object with specified path."},
3016-
{"loadModel", (PyCFunction)moose_loadModel, METH_VARARGS, moose_loadModel_documentation},
3020+
{"loadModelInternal", (PyCFunction)moose_loadModelInternal, METH_VARARGS, moose_loadModelInternal_documentation},
30173021
//{"saveModel", (PyCFunction)moose_saveModel, METH_VARARGS, moose_saveModel_documentation},
30183022
{"connect", (PyCFunction)moose_connect, METH_VARARGS, moose_connect_documentation},
30193023
{"getCwe", (PyCFunction)moose_getCwe, METH_VARARGS, "Get the current working element. 'pwe' is an alias of this function."},

pymoose/moosemodule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ PyObject * moose_reinit(PyObject * dummy, PyObject * args);
234234
PyObject * moose_stop(PyObject * dummy, PyObject * args);
235235
PyObject * moose_isRunning(PyObject * dummy, PyObject * args);
236236
PyObject * moose_exists(PyObject * dummy, PyObject * args);
237-
PyObject * moose_loadModel(PyObject * dummy, PyObject * args);
237+
PyObject * moose_loadModelInternal(PyObject * dummy, PyObject * args);
238238
//PyObject * moose_saveModel(PyObject * dummy, PyObject * args);
239239
PyObject * moose_setCwe(PyObject * dummy, PyObject * args);
240240
PyObject * moose_getCwe(PyObject * dummy, PyObject * args);

python/moose/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
# related module is imported.
66
LOGGING_FORMAT = '%(asctime)s %(message)s'
77

8-
# Bring everything from moose.py to global namespace.
9-
from moose.moose import *
10-
118
# Bring everything from c++ module to global namespace. Not everything is
129
# imported by the pervios import statement.
1310
from moose._moose import *
1411

12+
# Bring everything from moose.py to global namespace. It will overwrite any c++
13+
# function with the same name.
14+
from moose.moose import *
15+
16+
1517
# create a shorthand for version() call here.
1618
__version__ = version()
1719

python/moose/moose.py

Lines changed: 100 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# Maintainer: Dilawar Singh, Harsha Rani, Upi Bhalla
66

77
import warnings
8+
import os
89
import pydoc
910
from io import StringIO
10-
from os.path import splitext
1111
from contextlib import closing
1212

1313
# Import function from C++ module into moose namespace.
@@ -58,35 +58,53 @@
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
92110
def version( ):
@@ -123,36 +141,42 @@ def version( ):
123141
def 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-
141163
def 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

180206
def 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

193223
def 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

211243
def 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

280311
ce = _moose.setCwe # ce is a GENESIS shorthand for change element.
281312

282-
283313
def syncDataHandler(target):
284314
"""Synchronize data handlers for target.
285315
@@ -358,7 +388,6 @@ def showfield(el, field='*', showtype=False):
358388

359389
def 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-
376404
def listmsg(el):
377405
"""Return a list containing the incoming and outgoing messages of
378406
`el`.

tests/python/test_moose_attribs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444
'element', 'exists', 'finfotypes', 'fixXreacs', 'genesis', 'getCwe',
4545
'getField', 'getFieldDict', 'getFieldNames', 'getfielddoc',
4646
'getmoosedoc', 'isRunning',
47-
'known_types', 'le', 'listmsg', 'loadModel', 'melement',
47+
'known_types', 'le', 'listmsg', 'loadModelInternal', 'melement',
4848
'mergeChemModel', 'moose',
4949
'mooseAddChemSolver', 'mooseDeleteChemSolver', 'mooseReadNML2',
5050
'mooseReadSBML', 'mooseWriteKkit', 'mooseWriteNML2', 'mooseWriteSBML',
5151
'moose_constants', 'moose_test', 'move',
5252
'nml2Import_', 'pager', 'print_utils',
5353
'pwe', 'pydoc', 'rand', 'reinit',
5454
'seed', 'sequence_types', 'setClock', 'setCwe', 'showfield',
55-
'showfields', 'showmsg', 'splitext', 'start', 'stop', 'syncDataHandler',
55+
'showfields', 'showmsg', 'start', 'stop', 'syncDataHandler',
5656
'test', 'testSched', 'toUnicode', 'useClock', 'utils', 'vec', 'version',
5757
'warnings', 'wildcardFind']
5858

0 commit comments

Comments
 (0)