Skip to content

Commit

Permalink
replace bluecellulab.neuron -> neuron (#122)
Browse files Browse the repository at this point in the history
* replace bluecellulab.neuron -> neuron

Import neuron directly for simplicity.
  • Loading branch information
anilbey authored Feb 1, 2024
1 parent b07b4e2 commit 9b307e9
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 211 deletions.
31 changes: 16 additions & 15 deletions bluecellulab/cell/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
from typing import Optional
from typing_extensions import deprecated

import neuron
import numpy as np
import pandas as pd

import bluecellulab
from bluecellulab import neuron, psection
from bluecellulab import psection
from bluecellulab.cell.injector import InjectableMixin
from bluecellulab.cell.plotting import PlottableMixin
from bluecellulab.cell.section_distance import EuclideanSectionDistance
Expand Down Expand Up @@ -328,9 +329,9 @@ def area(self) -> float:
x_s = np.arange(1.0 / (2 * section.nseg), 1.0,
1.0 / (section.nseg))
for x in x_s:
area += bluecellulab.neuron.h.area(x, sec=section)
area += neuron.h.area(x, sec=section)
# for segment in section:
# area += bluecellulab.neuron.h.area(segment.x, sec=section)
# area += neuron.h.area(segment.x, sec=section)
return area

def add_recording(self, var_name: str, dt: Optional[float] = None) -> None:
Expand All @@ -346,11 +347,11 @@ def add_recording(self, var_name: str, dt: Optional[float] = None) -> None:
# This float_epsilon stuff is some magic from M. Hines to make
# the time points fall exactly on the dts
recording.record(
eval_neuron(var_name, self=self, neuron=bluecellulab.neuron),
eval_neuron(var_name, self=self, neuron=neuron),
self.get_precise_record_dt(dt),
)
else:
recording.record(eval_neuron(var_name, self=self, neuron=bluecellulab.neuron))
recording.record(eval_neuron(var_name, self=self, neuron=neuron))
self.recordings[var_name] = recording

@staticmethod
Expand Down Expand Up @@ -506,7 +507,7 @@ def create_netcon_spikedetector(self, target: HocObjectType, location: str, thre
source = public_hoc_cell(self.cell).axon[1](0.5)._ref_v
else:
raise ValueError("Spike detection location must be soma or AIS")
netcon = bluecellulab.neuron.h.NetCon(source, target, sec=sec)
netcon = neuron.h.NetCon(source, target, sec=sec)
netcon.threshold = threshold
return netcon

Expand All @@ -519,7 +520,7 @@ def start_recording_spikes(self, target: HocObjectType, location: str, threshold
threshold: spike detection threshold
"""
nc = self.create_netcon_spikedetector(target, location, threshold)
spike_vec = bluecellulab.neuron.h.Vector()
spike_vec = neuron.h.Vector()
nc.record(spike_vec)
self.recordings[f"spike_detector_{location}_{threshold}"] = spike_vec

Expand Down Expand Up @@ -576,18 +577,18 @@ def add_replay_minis(self,
if spont_minis_rate is not None and spont_minis_rate > 0:
sec = self.get_hsection(post_sec_id)
# add the *minis*: spontaneous synaptic events
self.ips[synapse_id] = bluecellulab.neuron.h.\
self.ips[synapse_id] = neuron.h.\
InhPoissonStim(location, sec=sec)

self.syn_mini_netcons[synapse_id] = bluecellulab.neuron.h.\
self.syn_mini_netcons[synapse_id] = neuron.h.\
NetCon(self.ips[synapse_id], synapse.hsynapse, sec=sec)
self.syn_mini_netcons[synapse_id].delay = 0.1
self.syn_mini_netcons[synapse_id].weight[0] = weight * weight_scalar
# set netcon type
nc_param_name = 'nc_type_param_{}'.format(
synapse.hsynapse).split('[')[0]
if hasattr(bluecellulab.neuron.h, nc_param_name):
nc_type_param = int(getattr(bluecellulab.neuron.h, nc_param_name))
if hasattr(neuron.h, nc_param_name):
nc_type_param = int(getattr(neuron.h, nc_param_name))
# NC_SPONTMINI
self.syn_mini_netcons[synapse_id].weight[nc_type_param] = 1

Expand All @@ -602,10 +603,10 @@ def add_replay_minis(self,
self.cell_id.id + 250,
seed2 + 350)
else:
exprng = bluecellulab.neuron.h.Random()
exprng = neuron.h.Random()
self.persistent.append(exprng)

uniformrng = bluecellulab.neuron.h.Random()
uniformrng = neuron.h.Random()
self.persistent.append(uniformrng)

if self.rng_settings.mode == 'Compatibility':
Expand Down Expand Up @@ -636,9 +637,9 @@ def add_replay_minis(self,

self.ips[synapse_id].setRNGs(exprng, uniformrng)

tbins_vec = bluecellulab.neuron.h.Vector(1)
tbins_vec = neuron.h.Vector(1)
tbins_vec.x[0] = 0.0
rate_vec = bluecellulab.neuron.h.Vector(1)
rate_vec = neuron.h.Vector(1)
rate_vec.x[0] = spont_minis_rate
self.persistent.append(tbins_vec)
self.persistent.append(rate_vec)
Expand Down
46 changes: 23 additions & 23 deletions bluecellulab/cell/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import warnings
import logging

import neuron
import numpy as np
from typing_extensions import deprecated

import bluecellulab
from bluecellulab.cell.stimuli_generator import (
gen_ornstein_uhlenbeck,
gen_shotnoise_signal,
Expand Down Expand Up @@ -60,7 +60,7 @@ def relativity_proportion(self, stim_mode: ClampMode) -> float:

def add_pulse(self, stimulus):
"""Inject pulse stimulus for replay."""
tstim = bluecellulab.neuron.h.TStim(0.5, sec=self.soma)
tstim = neuron.h.TStim(0.5, sec=self.soma)
tstim.train(stimulus.delay,
stimulus.duration,
stimulus.amp_start,
Expand All @@ -74,7 +74,7 @@ def add_step(self, start_time, stop_time, level, section=None, segx=0.5):
if section is None:
section = self.soma

tstim = bluecellulab.neuron.h.TStim(segx, sec=section)
tstim = neuron.h.TStim(segx, sec=section)
duration = stop_time - start_time
tstim.pulse(start_time, duration, level)
self.persistent.append(tstim)
Expand All @@ -86,7 +86,7 @@ def add_ramp(self, start_time, stop_time, start_level, stop_level,
if section is None:
section = self.soma

tstim = bluecellulab.neuron.h.TStim(segx, sec=section)
tstim = neuron.h.TStim(segx, sec=section)

tstim.ramp(
0.0,
Expand Down Expand Up @@ -133,7 +133,7 @@ def add_voltage_clamp(
section = self.soma
if current_record_dt is None:
current_record_dt = self.record_dt
vclamp = bluecellulab.neuron.h.SEClamp(segx, sec=section)
vclamp = neuron.h.SEClamp(segx, sec=section)
self.persistent.append(vclamp)

vclamp.amp1 = level
Expand All @@ -142,7 +142,7 @@ def add_voltage_clamp(
if rs is not None:
vclamp.rs = rs

current = bluecellulab.neuron.h.Vector()
current = neuron.h.Vector()
if current_record_dt is None:
current.record(vclamp._ref_i)
else:
Expand All @@ -156,16 +156,16 @@ def add_voltage_clamp(
def _get_noise_step_rand(self, noisestim_count):
"""Return rng for noise step stimulus."""
if self.rng_settings.mode == "Compatibility":
rng = bluecellulab.neuron.h.Random(self.cell_id.id + noisestim_count)
rng = neuron.h.Random(self.cell_id.id + noisestim_count)
elif self.rng_settings.mode == "UpdatedMCell":
rng = bluecellulab.neuron.h.Random()
rng = neuron.h.Random()
rng.MCellRan4(
noisestim_count * 10000 + 100,
self.rng_settings.base_seed +
self.rng_settings.stimulus_seed +
self.cell_id.id * 1000)
elif self.rng_settings.mode == "Random123":
rng = bluecellulab.neuron.h.Random()
rng = neuron.h.Random()
rng.Random123(
noisestim_count + 100,
self.rng_settings.stimulus_seed + 500,
Expand All @@ -181,11 +181,11 @@ def add_noise_step(self, section,
duration, seed=None, noisestim_count=0):
"""Inject a step current with noise on top."""
if seed is not None:
rand = bluecellulab.neuron.h.Random(seed)
rand = neuron.h.Random(seed)
else:
rand = self._get_noise_step_rand(noisestim_count)

tstim = bluecellulab.neuron.h.TStim(segx, rand, sec=section)
tstim = neuron.h.TStim(segx, rand, sec=section)
tstim.noise(delay, duration, mean, variance)
self.persistent.append(rand)
self.persistent.append(tstim)
Expand Down Expand Up @@ -213,7 +213,7 @@ def add_replay_noise(

def add_replay_hypamp(self, stimulus: Hyperpolarizing):
"""Inject hypamp for the replay."""
tstim = bluecellulab.neuron.h.TStim(0.5, sec=self.soma) # type: ignore
tstim = neuron.h.TStim(0.5, sec=self.soma) # type: ignore
if self.hypamp is None: # type: ignore
raise BluecellulabError("Cell.hypamp must be set for hypamp stimulus")
amp: float = self.hypamp # type: ignore
Expand All @@ -223,7 +223,7 @@ def add_replay_hypamp(self, stimulus: Hyperpolarizing):

def add_replay_relativelinear(self, stimulus):
"""Add a relative linear stimulus."""
tstim = bluecellulab.neuron.h.TStim(0.5, sec=self.soma)
tstim = neuron.h.TStim(0.5, sec=self.soma)
amp = stimulus.percent_start / 100.0 * self.threshold
tstim.pulse(stimulus.delay, stimulus.duration, amp)
self.persistent.append(tstim)
Expand All @@ -238,7 +238,7 @@ def _get_ornstein_uhlenbeck_rand(self, stim_count, seed):
seed3 = self.cell_id.id + 123 if seed is None else seed # GID
logger.debug("Using ornstein_uhlenbeck process seeds %d %d %d" %
(seed1, seed2, seed3))
rng = bluecellulab.neuron.h.Random()
rng = neuron.h.Random()
rng.Random123(seed1, seed2, seed3)
else:
raise BluecellulabError("Shot noise stimulus requires Random123")
Expand All @@ -254,7 +254,7 @@ def _get_shotnoise_step_rand(self, shotnoise_stim_count, seed=None):
seed3 = self.cell_id.id + 123 if seed is None else seed
logger.debug("Using shot noise seeds %d %d %d" %
(seed1, seed2, seed3))
rng = bluecellulab.neuron.h.Random()
rng = neuron.h.Random()
rng.Random123(seed1, seed2, seed3)
else:
raise BluecellulabError("Shot noise stimulus requires Random123")
Expand All @@ -264,7 +264,7 @@ def _get_shotnoise_step_rand(self, shotnoise_stim_count, seed=None):

def inject_current_clamp_signal(self, section, segx, tvec, svec):
"""Inject any signal via current clamp."""
cs = bluecellulab.neuron.h.IClamp(segx, sec=section)
cs = neuron.h.IClamp(segx, sec=section)
cs.dur = tvec[-1]
svec.play(cs._ref_amp, tvec, 1)

Expand All @@ -279,15 +279,15 @@ def inject_dynamic_clamp_signal(self, section, segx, tvec, svec, reversal):
Args:
reversal (float): reversal potential of conductance (mV)
"""
clamp = bluecellulab.neuron.h.SEClamp(segx, sec=section)
clamp = neuron.h.SEClamp(segx, sec=section)
clamp.dur1 = tvec[-1]
clamp.amp1 = reversal
# support delay with initial zero
tvec.insrt(0, 0)
svec.insrt(0, 0)
# replace svec with inverted and clamped signal
# rs is in MOhm, so conductance is in uS (micro Siemens)
svec = bluecellulab.neuron.h.Vector(
svec = neuron.h.Vector(
[1 / x if x > 1E-9 and x < 1E9 else 1E9 for x in svec])
svec.play(clamp._ref_rs, tvec, 1)

Expand Down Expand Up @@ -400,14 +400,14 @@ def inject_current_waveform(self, t_content, i_content, section=None,
"""Inject a custom current to the cell."""
start_time = t_content[0]
stop_time = t_content[-1]
time = bluecellulab.neuron.h.Vector()
currents = bluecellulab.neuron.h.Vector()
time = neuron.h.Vector()
currents = neuron.h.Vector()
time = time.from_python(t_content)
currents = currents.from_python(i_content)

if section is None:
section = self.soma
pulse = bluecellulab.neuron.h.IClamp(segx, sec=section)
pulse = neuron.h.IClamp(segx, sec=section)
self.persistent.append(pulse)
self.persistent.append(time)
self.persistent.append(currents)
Expand Down Expand Up @@ -443,7 +443,7 @@ def add_sin_current(self, amp, start_time, duration, frequency,
"""Add a sinusoidal current to the cell."""
if section is None:
section = self.soma
tstim = bluecellulab.neuron.h.TStim(segx, sec=section)
tstim = neuron.h.TStim(segx, sec=section)
tstim.sin(amp, start_time, duration, frequency)
self.persistent.append(tstim)
return tstim
Expand All @@ -458,7 +458,7 @@ def add_alpha_synapse(
segx=0.5,
) -> HocObjectType:
"""Add an AlphaSynapse NEURON point process stimulus to the cell."""
syn = bluecellulab.neuron.h.AlphaSynapse(segx, sec=section)
syn = neuron.h.AlphaSynapse(segx, sec=section)
syn.onset = onset
syn.tau = tau
syn.gmax = gmax
Expand Down
19 changes: 11 additions & 8 deletions bluecellulab/cell/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from __future__ import annotations

import neuron

import bluecellulab


Expand Down Expand Up @@ -76,26 +78,27 @@ def add_dendrogram(
def init_callbacks(self):
"""Initialize the callback function (if necessary)."""
if not self.delayed_weights.empty():
self.fih_weights = bluecellulab.neuron.h.FInitializeHandler(
self.fih_weights = neuron.h.FInitializeHandler(
1, self.weights_callback)

if self.plot_callback_necessary:
self.fih_plots = bluecellulab.neuron.h.FInitializeHandler(1, self.plot_callback)
self.fih_plots = neuron.h.FInitializeHandler(1, self.plot_callback)

def weights_callback(self):
"""Callback function that updates the delayed weights, when a certain
delay has been reached."""
while not self.delayed_weights.empty() and \
abs(self.delayed_weights.queue[0][0] - bluecellulab.neuron.h.t) < \
bluecellulab.neuron.h.dt:
abs(self.delayed_weights.queue[0][0] - neuron.h.t) < \
neuron.h.dt:
(_, (sid, weight)) = self.delayed_weights.get()
if sid in self.connections:
if self.connections[sid].post_netcon is not None:
self.connections[sid].post_netcon.weight[0] = weight

if not self.delayed_weights.empty():
bluecellulab.neuron.h.cvode.event(self.delayed_weights.queue[0][0],
self.weights_callback)
neuron.h.cvode.event(
self.delayed_weights.queue[0][0], self.weights_callback
)

def plot_callback(self):
"""Update all the windows."""
Expand All @@ -104,5 +107,5 @@ def plot_callback(self):
for cell_dendrogram in self.cell_dendrograms:
cell_dendrogram.redraw()

bluecellulab.neuron.h.cvode.event(
bluecellulab.neuron.h.t + 1, self.plot_callback)
neuron.h.cvode.event(
neuron.h.t + 1, self.plot_callback)
13 changes: 6 additions & 7 deletions bluecellulab/cell/section_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
# limitations under the License.
"""Distance computing functionality between Neuron sections."""

import neuron
import numpy as np

import bluecellulab


class EuclideanSectionDistance:
"""Calculate euclidian distance between positions on two sections.
Expand Down Expand Up @@ -72,18 +71,18 @@ def __call__(
def grindaway(hsection):
"""Grindaway."""
# get the data for the section
n_segments = int(bluecellulab.neuron.h.n3d(sec=hsection))
n_segments = int(neuron.h.n3d(sec=hsection))
n_comps = hsection.nseg

xs = np.zeros(n_segments)
ys = np.zeros(n_segments)
zs = np.zeros(n_segments)
lengths = np.zeros(n_segments)
for index in range(n_segments):
xs[index] = bluecellulab.neuron.h.x3d(index, sec=hsection)
ys[index] = bluecellulab.neuron.h.y3d(index, sec=hsection)
zs[index] = bluecellulab.neuron.h.z3d(index, sec=hsection)
lengths[index] = bluecellulab.neuron.h.arc3d(index, sec=hsection)
xs[index] = neuron.h.x3d(index, sec=hsection)
ys[index] = neuron.h.y3d(index, sec=hsection)
zs[index] = neuron.h.z3d(index, sec=hsection)
lengths[index] = neuron.h.arc3d(index, sec=hsection)

# to use Vector class's .interpolate()
# must first scale the independent variable
Expand Down
Loading

0 comments on commit 9b307e9

Please sign in to comment.