Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove exec wrapper on hclass2/3 modules #1095

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ x86_64
__pycache__
venv
virtualenv
*.o
.python-version
*.o
*.lo

# These files are generated at build time
Expand Down
41 changes: 23 additions & 18 deletions share/lib/python/neuron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,15 @@ def test_rxd(exitOnError=True):
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

import sys, types

# Load the `hclass` factory for the correct Python version 2/3 and prevent the
# incorrect module source code from being opened by creating an empty module.
if sys.version_info[0] == 2:
sys.modules["neuron.hclass3"] = types.ModuleType("neuron.hclass3")
from neuron.hclass2 import hclass
else:
sys.modules["neuron.hclass2"] = types.ModuleType("neuron.hclass2")
from neuron.hclass3 import hclass

# global list of paths already loaded by load_mechanisms
Expand Down Expand Up @@ -402,12 +408,12 @@ def init():

Use h.finitialize() instead, which allows you to specify the membrane potential
to initialize to; via e.g. h.finitialize(-65)

By default, the units used by h.finitialize are in mV, but you can be explicit using
NEURON's unit's library, e.g.

.. code-block:: python

from neuron.units import mV
h.finitialize(-65 * mV)

Expand All @@ -421,28 +427,28 @@ def run(tstop):
function run(tstop)

Run the simulation (advance the solver) until tstop [ms]

`h.run()` and `h.continuerun(tstop)` are more powerful solutions defined in the `stdrun.hoc` library.

** This function exists for historical purposes. Use in new code is not recommended. **

For running a simulation, consider doing the following instead:

Begin your code with

.. code-block:: python

from neuron import h
from neuron.units import ms, mV
h.load_file('stdrun.hoc')

Then when it is time to initialize and run the simulation:

.. code-block:: python

h.finitialize(-65 * mV)
h.continuerun(100 * ms)

where the initial membrane potential and the simulation run time are adjusted as appropriate
for your model.

Expand Down Expand Up @@ -740,7 +746,7 @@ class _RangeVarPlot(_WrapperPlot):
fig.show()

pyplot.show()

# plotnine/ggplot
p9.ggplot() + r.plot(p9)

Expand Down Expand Up @@ -1004,10 +1010,10 @@ def mark(self, segment, marker='or', **kwargs):
if secs is None:
secs = list(h.allsec())


if variable is None:
kwargs.setdefault('color', 'black')

data = []
for sec in secs:
xs = [sec.x3d(i) for i in range(sec.n3d())]
Expand Down Expand Up @@ -1336,7 +1342,7 @@ def _nrnpy_rvp_pyobj_callback(f):
f_type = str(type(f))
if f_type not in ("<class 'neuron.rxd.species.SpeciesOnRegion'>", "<class 'neuron.rxd.species.Species'>"):
return f

# if we're here, f is an rxd variable, and we return a function that looks
# up the weighted average concentration given an x and h.cas()
# this is not particularly efficient so it is probably better to use this for
Expand Down Expand Up @@ -1376,4 +1382,3 @@ def clear_gui_callback():
nrnpy_set_gui_callback(None)
except:
pass

7 changes: 3 additions & 4 deletions share/lib/python/neuron/hclass2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# h.anyclass methods may be overridden. If so the base method can be called
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

from neuron import h, hoc
import nrn

#avoid syntax error if compiled by python 3
exec('''

class MetaHocObject(type):
"""Provides Exception for Inheritance of multiple HocObject"""
def __new__(cls, name, bases, attrs):
Expand All @@ -22,7 +22,7 @@ def __new__(cls, name, bases, attrs):
raise TypeError('Multiple Inheritance of HocObject in %s' % name
+ ' through %s not allowed' % ','.join(m))
#note that join(b.__name__ for b in m) is not valid for Python 2.3

return type.__new__(cls, name, bases, attrs)

def hclass(c):
Expand All @@ -38,4 +38,3 @@ def __new__(cls, *args, **kwds):
return hoc.HocObject.__new__(cls, *args, **kwds2)
setattr(hc, 'htype', c)
return hc
''')
7 changes: 3 additions & 4 deletions share/lib/python/neuron/hclass3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# h.anyclass methods may be overridden. If so the base method can be called
# using the idiom self.basemethod = self.baseattr('methodname')
# ------------------------------------------------------------------------------

from neuron import h, hoc
import nrn

#avoid syntax error if compiled by python 2
exec('''

class MetaHocObject(type):
"""Provides Exception for Inheritance of multiple HocObject"""
def __new__(cls, name, bases, attrs):
Expand All @@ -22,7 +22,7 @@ def __new__(cls, name, bases, attrs):
raise TypeError('Multiple Inheritance of HocObject in %s' % name
+ ' through %s not allowed' % ','.join(m))
#note that join(b.__name__ for b in m) is not valid for Python 2.3

return type.__new__(cls, name, bases, attrs)

def hclass(c):
Expand All @@ -38,4 +38,3 @@ def __new__(cls, *args, **kwds):
return hoc.HocObject.__new__(cls, *args, **kwds2)
setattr(hc, 'htype', c)
return hc
''')