Last active
November 8, 2021 16:07
-
-
Save Helveg/079a7e717e5b1e48e7b8c6b3598e47d5 to your computer and use it in GitHub Desktop.
Arbor morphology to NEURON object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NrnCell: | |
def __init__(self): | |
self.all = [] | |
def _dims(segments): | |
# Get the (prox - dist) ^ 2 along a certain axis | |
d = lambda seg, axis: (getattr(seg.prox, axis) - getattr(seg.dist, axis)) ** 2 | |
# Sum and sqrt the distances along the x, y, z axes for eucl. dist | |
eucl = [sum(d(s, axis) for axis in ("x", "y", "z")) ** (1/2) for s in segments] | |
# Average the frustrum prox and dist radius for equivalent segment cilinder radii | |
radii = [(s.prox.radius + s.dist.radius) / 2 for s in segments] | |
total_length = sum(eucl) | |
# Average of the segment cilinder radii, weighted by segment length | |
r = sum(r * seg_len for r, seg_len in zip(radii, eucl)) / total_length / len(radii) | |
# Return `L` and `diam` | |
return total_length, r * 2 | |
def make_section(segments): | |
import neuron | |
sec = neuron.h.Section() | |
sec.L, sec.diam = _dims(segments) | |
return sec | |
def make_cell(morphology): | |
cell = NrnCell() | |
num = morphology.num_branches | |
cell.all = [ | |
make_section(morphology.branch_segments(i)) | |
for i in range(num) | |
] | |
for i, sec in enumerate(cell.all): | |
p = morphology.branch_parent(i) | |
# If branch has no parent, this beaut is returned | |
# from the `branch_parent` function | |
if p != 4294967295: | |
sec.connect(cell.all[p]) | |
return cell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment