Skip to main content

A Pythonic, object-oriented, monkey patch for NEURON

Project description

Build Status codecov Code style: black Documentation Status

*No ducks were punched during the construction of this monkey patch.

Installation

pip install nrn-patch

Inline replacement of NEURON by Patch

Be aware that the interface is currently incomplete, this means that most parts are still "just" NEURON. I've only patched holes I frequently encounter myself when using the h.Section, h.NetStim and h.NetCon functions. Feel free to open an issue or fork this project and open a pull request for missing or broken parts of the interface.

Philosophy

Python interfaces should be Pythonic, this wrapper offers just that:

  • Full Python objects: each wonky C-like NEURON object is wrapped in a full fledged Python object, easily handled and extended through inheritance.
  • Duck typed interface: take a look at the magic methods I use and any object you create with those methods present will work just fine with Patch.
  • Correct garbage collection, objects connected to eachother don't dissapear: Objects that rely on eachother store a reference to eachother. As is the basis for any sane object oriented interface.

Basic usage

Use it like you would use NEURON. The wrapper doesn't make any changes to the interface, it just patches up some of the more frequent and ridiculous gotchas.

Patch supplies a new HOC interpreter p, the PythonHocInterpreter which wraps the standard HOC interpreter h provided by NEURON. Any objects returned will either be PythonHocObject's wrapping their corresponding NEURON object, or whatever NEURON returns.

When using just Patch the difference between NEURON and Patch objects is handled transparently, but if you wish to mix interpreters you can transform all Patch objects back to NEURON objects with obj.__neuron__().

from patch import p
import glia as g

section = p.Section()
point_process = g.insert(section, "AMPA")
stim = p.NetStim()
stim.start = 10
stim.number = 5
stim.interval = 10

# And here comes the magic! This explicitly defined connection
# isn't immediatly garbage collected! What a crazy world we live in.
# Has science gone too far?
p.NetCon(stim, point_process)

# It's fully compatible using __neuron__
from neuron import h
nrn_section = h.Section()
nrn_section.connect(section.__neuron__())

Magic methods

__neuron__

Get the object's NEURON pointer

Whenever an object with this method present is sent to the NEURON HOC interpreter, the result of this method is passed instead. This allows Python methods to encapsulate NEURON pointers transparently

__netcon__

Get the object's NetCon pointer

Whenever an object with this method present is used in a NetCon call, the result of this method is passed instead. The connection is stored on the original object. This allows to simplify the calls to NetCon, or to add more elegant default behavior, like inserting the connection on a random segment of a section and being able to use just p.NetCon(section, synapse)

Known unpatched holes

  • When inserting point processes the returned object is unwrapped. This can be resolved using Glia, or by using this syntax:
  # In neuron
  process = h.MyMechanismName(my_section(0.5), *args, **kwargs)
  # In patch
  point_process = p.PointProcess(p.MyMechanismName, my_section(0.5), *args, **kwargs)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for nrn-patch, version 2.0.3
Filename, size File type Python version Upload date Hashes
Filename, size nrn_patch-2.0.3-py3-none-any.whl (15.6 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page