importing .asc (Neurolucida ASCII) files with Python

Managing anatomically complex model cells with the CellBuilder. Importing morphometric data with NEURON's Import3D tool or Robert Cannon's CVAPP. Where to find detailed morphometric data.
Post Reply
hjunji21
Posts: 2
Joined: Fri Apr 20, 2018 7:30 pm

importing .asc (Neurolucida ASCII) files with Python

Post by hjunji21 » Tue Jun 05, 2018 5:29 pm

Hello all,

As NEURON user, I am very beginner. Thus, I have been facing several problems to deal with the use.

In my work, I need to simulate the membrane dynamics considering in a sample of 91 *.asc morphologies. Therefore, I have to import such *.asc morphologies to NEURON and set the "experiment" on each of them. However, I have not been able to import such *.asc morphologies. If I use the NEURON GUI, I have no problems to import the *.asc morphologies. In addition, I have been checking the morphologies using this NEURON GUI and there is nothing wrong with them.

I adapted my code from ModelDBhttps://senselab.med.yale.edu/ModelDB/s ... .py#tabs-2 (I tried to make an adaptation, since this example from ModelDB refers to *.swc morphology). My code is ('0409_H50_01.asc' is the morphology and it is in the same folder of the code):

Code: Select all

from neuron import h,gui
from matplotlib import pyplot as plt
import numpy as np
from numpy import random as rand
import datetime as dame
import sys
import os.path

arquivo = '0409_H50_01.asc'

################~Importing morphology~########################################

#The load_swc function at the following link inserts an SWC morphology 
#into a Python class. Just change Import3d_SWC_read to Import3d_Neurolucida3 
#to work with ASC files instead.

def load(arquivo, cell, use_axon=True, xshift=0, yshift=0, zshift=0):
    """load an SWC from filename and instantiate inside cell"""

    name_form = {1: 'soma[%d]', 2: 'axon[%d]', 3: 'dend[%d]', 4: 'apic[%d]'}

    # a helper library, included with NEURON
    h.load_file('stdlib.hoc')
    h.load_file('import3d.hoc')

    # load the data. Use Import3d_SWC_read for swc, Import3d_Neurolucida3 for
    # Neurolucida V3, Import3d_MorphML for MorphML (level 1 of NeuroML), or
    # Import3d_Eutectic_read for Eutectic.
    morph = h.Import3d_Neurolucida3()
    morph.input(arquivo)

    # easiest to instantiate by passing the loaded morphology to the Import3d_GUI
    # tool; with a second argument of 0, it won't display the GUI, but it will allow
    # use of the GUI's features
    i3d = h.Import3d_GUI(morph, 0)

    # get a list of the swc section objects
    asc_secs = i3d.asc.sections
    asc_secs = [asc_secs.object(i) for i in xrange(int(asc_secs.count()))]

    # initialize the lists of sections
    cell.soma, cell.apic, cell.dend, cell.axon = [], [], [], []
    sec_list = {1: cell.soma, 2: cell.axon, 3: cell.dend, 4: cell.apic}

    # name and create the sections
    real_secs = {}
    for asc_sec in asc_secs:
        cell_part = int(asc_sec.type)

        # skip everything else if it's an axon and we're not supposed to
        # use it... or if is_subsidiary
        if (not(use_axon) and cell_part == 2) or asc_sec.is_subsidiary:
            continue
        
        # figure out the name of the new section
        if cell_part not in name_form:
            raise Exception('unsupported point type')
        name = name_form[cell_part] % len(sec_list[cell_part])

        # create the section
        sec = h.Section(cell=cell, name=name)
        
        # connect to parent, if any
        if asc_sec.parentsec is not None:
            sec.connect(real_secs[asc_sec.parentsec.hname()](asc_sec.parentx))

        # define shape
        if asc_sec.first == 1:
            h.pt3dstyle(1, asc_sec.raw.getval(0, 0), asc_sec.raw.getval(1, 0),
                        asc_sec.raw.getval(2, 0), sec=sec)

        j = asc_sec.first
        xx, yy, zz = [asc_sec.raw.getrow(i).c(j) for i in xrange(3)]
        dd = asc_sec.d.c(j)
        if asc_sec.iscontour_:
            # never happens in SWC files, but can happen in other formats supported
            # by NEURON's Import3D GUI
            raise Exception('Unsupported section style: contour')

        if dd.size() == 1:
            # single point soma; treat as sphere
            x, y, z, d = [dim.x[0] for dim in [xx, yy, zz, dd]]
            for xprime in [x - d / 2., x, x + d / 2.]:
                h.pt3dadd(xprime + xshift, y + yshift, z + zshift, d, sec=sec)
        else:
            for x, y, z, d in zip(xx, yy, zz, dd):
                h.pt3dadd(x + xshift, y + yshift, z + zshift, d, sec=sec)

        # store the section in the appropriate list in the cell and lookup table               
        sec_list[cell_part].append(sec)    
        real_secs[asc_sec.hname()] = sec

    cell.all = cell.soma + cell.apic + cell.dend + cell.axon

def main(filename=arquivo):
    """demo test program"""
    class Cell:
        def __str__(self):
            return 'neuron'

    cell = Cell()
    load(arquivo, cell)
    return cell

if __name__ == '__main__':
    cell = main()

###############~Visualize imported neuron~####################################
shape_window = h.PlotShape()
shape_window.exec_menu('Show Diam')

input("Press <enter> to continue")
However, when I run it, I have the following error message:

Code: Select all

NEURON -- VERSION 7.5 master (6b4c19f) 2017-09-25
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2016
See http://neuron.yale.edu/neuron/credits

NEURON: unable to open font "*helvetica-medium-r-normal*--14*", using "fixed"
could not open 0409_H50_01.asc
NEURON: 0409_H50_01.asc :file is not open
 near line 0
 ^
        File[0].eof()
      Import3d_Neurolucida3[0].rdfile("0409_H50_01.asc")
    Import3d_Neurolucida3[0].input("0409_H50_01.asc")
oc_restore_code tobj_count=1 should be 0
Traceback (most recent call last):
  File "load_teste_from_github.py", line 110, in <module>
    cell = main()
  File "load_teste_from_github.py", line 106, in main
    load(arquivo, cell)
  File "load_teste_from_github.py", line 34, in load
    morph.input(arquivo)
RuntimeError: hoc error
I really can not understand what is the problem of my code. Please, if there is someone who has any suggestion, I will be very thankful.

In advance, thank you very much for attention.

Regards,

Helio

ted
Site Admin
Posts: 5623
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: importing .asc (Neurolucida ASCII) files with Python

Post by ted » Thu Jun 07, 2018 6:10 pm

Example provided by Robert McDougal:
Suppose you had a Neurolucida ASCII file called C011098A-P4.asc

Code: Select all

from neuron import h, gui
h.load_file('import3d.hoc')

class MyCell:
    def __init__(self):
        morph_reader = h.Import3d_Neurolucida3()
        morph_reader.input('C011098A-P4.asc')
        i3d = h.Import3d_GUI(morph_reader, 0)
        i3d.instantiate(self)

m = MyCell()

print('%d apic sections' % len(m.apic))
sp = h.PlotShape()
sp.show(0)  # show diameters
It is dangerous to do this unless you are absolutely certain that the data files are free of errors that would make them useless for modeling. This is why I strongly recommend against automating the importation of morphometric data, unless you know for sure that those data are good. Avoid wasting a lot of time and having your manuscript rejected. Read about the kinds of errors that can affect morphometric data:
Caveats about anatomically detailed model cells viewtopic.php?f=28&t=3862
Soma stacks and the Import3d tool viewtopic.php?f=28&t=3833
Then use the GUI's Import3d tool, which reports errors that it encounters and makes it easy to review both the data and the 3d shape that it specifies.

Post Reply