SWC files

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

SWC files

Post by Kolorek » Mon Feb 02, 2015 5:26 am

Hello,
I want to ask is there possibility to open swc files from Neuromorpho.org in python and build from this cells a small network ?
Thank you for help

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Mon Feb 02, 2015 4:48 pm

Yes.

Here's a function to load and instantiate a single cell:

Code: Select all

def instantiate_swc(filename):
    """load an swc file and instantiate it"""
    
    # load the NEURON library (just in case h is defined otherwise elsewhere)
    from neuron import h
    
    # a helper library, included with NEURON
    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. (There is also an 
    # Import3d_Neurolucida_read for old Neurolucida files, but I've never seen one
    # in practice; try Import3d_Neurolucida3 first.)
    cell = h.Import3d_SWC_read()
    cell.input(filename)

    # 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(cell, 0)
    i3d.instantiate(None)
If you want to make multiple copies, then instead of instantiating the cell (the last line of the function), pass it to a cell builder.

If you're only looking to create one particular network, you can design something with the GUI tools, instantiate, save session, and then just h.load_file your session to recreate the network.

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

Re: SWC files

Post by ted » Mon Feb 02, 2015 9:15 pm

That's potentially very useful code, but it should be used with great caution. Keep in mind that
--There is no guarantee that any morphometric data file will be suitable for any use other than that which motivated the authors from whose lab it came.
--Much, if not most, morphometric data were acquired by students who worked long hours while being paid minimum wage, and whose careers didn't depend on the quality of that data.

The files in the NeuroMorpho.org archive have been cleaned up so that most if not all of them should be importable by NEURON, either interactively with the Import3d tool or by executing code like that in Robert's post, but the fact that a given file imports without complaint does not guarantee the quality of the data or its suitability for computational modeling. Indeed, morphometric data are subject to many limitations and artifacts that need to be considered--see Quality issues with morphometric data in the Hot tips area of the NEURON Forum.

At the very least, before using any such data, the modeler should

1. Read the paper written by the anatomist(s) who collected the data. What staining and imaging techniques were used (some are more likely than others to produce incomplete "fills", or to lead to under or overestimation of the diameter of fine branches)? What controls did the authors use to reject cells that were incompletely filled? Were the authors concerned about accurate diameter measurements, or did they only want to capture the branched architecture and the lengths of individual neurites? What was the optical resolution of the imaging method?

2. Examine the data. This can be done by parsing the original file, or by exploring the pt3d data after it has been imported into NEURON. What is the finest diameter measurement, and what is the diameter quantization (smallest difference between different diameter measurements)? Do the diameter measurements reveal "favorite numbers" (bias for "nice" diameter values)?

3. Pay attention to the comments and error messages that are generated by when the cell is imported by the Import3d tool. Some surprising errors may turn up; many of these can be easily resolved, but others may make you decide to avoid using that particular morphology. I don't know if Robert's code will generate useful messages if such errors occur.

4. Examine the shape of the reconstructed cell, e.g. in a shape plot. Does it look like a representative member of its cell class? Did the authors use "bogus neurites" as fiducial marks, e.g. to indicate the location of a cell body layer or the boundaries of different cortical layers? In the shape plot, rotate the cell 90 degrees around the y (or x) axis to see if there are abrupt jumps in the z direction. Such jumps result from drift and backlash in the microscope's z axis position control, and can make a morphology file useless for modeling purposes.

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Wed Feb 04, 2015 12:43 pm

I use this function but i have an error:

Traceback (most recent call last):
File "C:\Users\Właściciel\Desktop\komórki\hh.py", line 17, in <module>
kom = instantiate_swc('51-2b.CNG.swc')
File "C:\Users\Właściciel\Desktop\komórki\hh.py", line 11, in instantiate_swc
cell = h.import3d_swc_read()
AttributeError: 'hoc.HocObject' object has no attribute 'import3d_swc_read'

I check that I have all the files (I use pyNEURON in python), i try to make small and big letters but that didn't work
what else can I do ?
ramcdougal wrote:Yes.

Here's a function to load and instantiate a single cell:

Code: Select all

def instantiate_swc(filename):
    """load an swc file and instantiate it"""
    
    # load the NEURON library (just in case h is defined otherwise elsewhere)
    from neuron import h
    
    # a helper library, included with NEURON
    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. (There is also an 
    # Import3d_Neurolucida_read for old Neurolucida files, but I've never seen one
    # in practice; try Import3d_Neurolucida3 first.)
    cell = h.Import3d_SWC_read()
    cell.input(filename)

    # 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(cell, 0)
    i3d.instantiate(None)
If you want to make multiple copies, then instead of instantiating the cell (the last line of the function), pass it to a cell builder.

If you're only looking to create one particular network, you can design something with the GUI tools, instantiate, save session, and then just h.load_file your session to recreate the network.

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Wed Feb 04, 2015 1:02 pm

My sincere apologies. I copied the function out of another file I wrote and didn't test it on its own; it requires the GUI tools to be loaded. I've modified the import accordingly. Please try this version:

Code: Select all

def instantiate_swc(filename):
    """load an swc file and instantiate it"""
    
    # load the NEURON library (just in case h is defined otherwise elsewhere)
    from neuron import h, gui
    
    # a helper library, included with NEURON
    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. (There is also an 
    # Import3d_Neurolucida_read for old Neurolucida files, but I've never seen one
    # in practice; try Import3d_Neurolucida3 first.)
    cell = h.Import3d_SWC_read()
    cell.input(filename)

    # 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(cell, 0)
    i3d.instantiate(None)
If that doesn't work, can you tell us: (1) the version of NEURON, (2) the platform (Windows/Mac/Linux), and (3) the output of running the following in Python:

Code: Select all

from neuron import h, gui
h.load_file('import3d.hoc')
print ', '.join([f for f in dir(h) if 'import3d' in f.lower()])
It should display something like:

Code: Select all

Import3d_Eutectic_read, Import3d_GUI, Import3d_LexToken, Import3d_MorphML, Import3d_Neurolucida3, Import3d_Neurolucida_read, Import3d_SWC_read, Import3d_Section, makeimport3dtool

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Wed Feb 04, 2015 2:53 pm

I use your this version and i have error :(

Traceback (most recent call last):
File "C:\Users\Właściciel\Desktop\komórki\hh.py", line 40, in <module>
kom = instantiate_swc("j7_L4stellate.CNG.swc")
File "C:\Users\Właściciel\Desktop\komórki\hh.py", line 23, in instantiate_swc
cell = h.Import3d_SWC_read()
AttributeError: 'hoc.HocObject' object has no attribute 'Import3d_SWC_read'

(1) the version of NEURON: I use pyNEURON version 7.2.536.16 and python version Python 2.7.3
(2) the platform (Windows/Mac/Linux): I work on Windows 7 N profesionall 32 bit-version
(3) the output of running the following in Python:

from neuron import h, gui
h.load_file('import3d.hoc')
print ', '.join([f for f in dir(h) if 'import3d' in f.lower()])


I have only this:
Found NEURON at C:\Python27\neuronhome
and there a blank line (it looks like python write something in white bu t even if a note this line there is nothing)
ramcdougal wrote:My sincere apologies. I copied the function out of another file I wrote and didn't test it on its own; it requires the GUI tools to be loaded. I've modified the import accordingly. Please try this version:

Code: Select all

def instantiate_swc(filename):
    """load an swc file and instantiate it"""
    
    # load the NEURON library (just in case h is defined otherwise elsewhere)
    from neuron import h, gui
    
    # a helper library, included with NEURON
    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. (There is also an 
    # Import3d_Neurolucida_read for old Neurolucida files, but I've never seen one
    # in practice; try Import3d_Neurolucida3 first.)
    cell = h.Import3d_SWC_read()
    cell.input(filename)

    # 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(cell, 0)
    i3d.instantiate(None)
If that doesn't work, can you tell us: (1) the version of NEURON, (2) the platform (Windows/Mac/Linux), and (3) the output of running the following in Python:

Code: Select all

from neuron import h, gui
h.load_file('import3d.hoc')
print ', '.join([f for f in dir(h) if 'import3d' in f.lower()])
It should display something like:

Code: Select all

Import3d_Eutectic_read, Import3d_GUI, Import3d_LexToken, Import3d_MorphML, Import3d_Neurolucida3, Import3d_Neurolucida_read, Import3d_SWC_read, Import3d_Section, makeimport3dtool

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Wed Feb 04, 2015 11:44 pm

If you're using PyNEURON, you have to manually set a NEURONHOME environment variable in order for it to find the HOC libraries. The need for this extra step is unfortunate, but it's a simple process (see, for example, here -- updated 20191029, link broken so removed but this is no longer necessary in current versions of NEURON).

The correct location for this is in principle system-dependent, but for a default Python 2.7 installation, it should be: C:\python27\neuronhome

With that environment variable set, if you launch a new terminal, run python, and enter the three lines from my last post, you should get a non-empty result (and all the rest should work).

(I tested this with Windows 7, 32-bit Python 2.7.9, PyNEURON, and nothing else installed.)
Last edited by ramcdougal on Tue Oct 29, 2019 9:45 am, edited 1 time in total.

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Thu Feb 05, 2015 3:13 pm

I do what you write and now everything works :)

when I use your code

from neuron import h, gui
h.load_file('import3d.hoc')
print ', '.join([f for f in dir(h) if 'import3d' in f.lower()])

I have the results

Import3d_Eutectic_read, Import3d_GUI, Import3d_LexToken, Import3d_MorphML, Import3d_Neurolucida3, Import3d_Neurolucida_read, Import3d_SWC_read, Import3d_Section, makeimport3dtool

Thank you for your help :)
ramcdougal wrote:If you're using PyNEURON, you have to manually set a NEURONHOME environment variable in order for it to find the HOC libraries. The need for this extra step is unfortunate, but it's a simple process (see, for example, here.

The correct location for this is in principle system-dependent, but for a default Python 2.7 installation, it should be: C:\python27\neuronhome

With that environment variable set, if you launch a new terminal, run python, and enter the three lines from my last post, you should get a non-empty result (and all the rest should work).

(I tested this with Windows 7, 32-bit Python 2.7.9, PyNEURON, and nothing else installed.)

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Fri Feb 06, 2015 12:48 pm

Could you explain what do you mean in this sentence : If you want to make multiple copies, then instead of instantiating the cell (the last line of the function), pass it to a cell builder.

(I mean how to pass the cell to CellBuilder) ?
ramcdougal wrote:Yes.

Here's a function to load and instantiate a single cell:

Code: Select all

def instantiate_swc(filename):
    """load an swc file and instantiate it"""
    
    # load the NEURON library (just in case h is defined otherwise elsewhere)
    from neuron import h
    
    # a helper library, included with NEURON
    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. (There is also an 
    # Import3d_Neurolucida_read for old Neurolucida files, but I've never seen one
    # in practice; try Import3d_Neurolucida3 first.)
    cell = h.Import3d_SWC_read()
    cell.input(filename)

    # 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(cell, 0)
    i3d.instantiate(None)
If you want to make multiple copies, then instead of instantiating the cell (the last line of the function), pass it to a cell builder.

If you're only looking to create one particular network, you can design something with the GUI tools, instantiate, save session, and then just h.load_file your session to recreate the network.

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Fri Feb 06, 2015 11:12 pm

Stepping back a bit, if the goal is to use Python to create a network of some fixed pool of cell types, it's probably easiest and most efficient to use NEURON's GUI tools to convert swc files into cell templates (this is a manual process, but you only have to do it once per cell type) rather than have NEURON convert each file at each run.

To transform an swc file into a neuron template file:
  • Tools - Miscellaneous - Import 3D
  • "choose a file" button, then select a file and click "Read"
  • export - CellBuilder
  • use the Biophysics tab to place mechanisms (ion channels, etc...) throughout the cell
  • Use the management tab to export by selecting "Cell type", optionally entering a "Classname" and then selecting "Save hoc code in file"
If, by doing the above, you created a file "myneuron.hoc" with a cell type called "MyCell", you can load the template via:

Code: Select all

from neuron import h
h.load_file('myneuron.hoc')
You can create multiple copies by invoking the constructor repeatedly:

Code: Select all

cell1 = h.MyCell()
cell2 = h.MyCell()
To translate one of the cells, use .position, e.g.

Code: Select all

cell2.position(100, 0, 0)
The same basic procedure can be used programmatically, but again, you may be better off letting the GUI help you with this part of the initial setup. To transfer to a cell builder use i3d.cbexport() instead of i3d.instantiate(). If you have a CellBuilder cb, the management tab options are in cb.manage, and the main function for exporting as a HOC template is cb.manage.save_class.

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Sat Feb 07, 2015 7:56 am

Thank you for explain it :).
Last edited by Kolorek on Sun Feb 08, 2015 10:02 am, edited 1 time in total.

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Tue Feb 10, 2015 2:54 pm

I do like you write and now I have swc files as hoc files and i read in python everything is alright till I want ot connect the cells (I want to connect cells, put some current and have a graph of voltage in time and have graph with the concentration of ions inside and outside the cells )

Code: Select all

from neuron import h
h.load_file('neu.hoc')


cell1 = h.pira('cell1')
cell2 = h.pira('cell2')
cell3 = h.pira('cell3')
cell4 = h.pira('cell4')
cell5 = h.pira('cell5')
cell6 = h.pira('cell6')
cell7 = h.pira('cell7')
cell8 = h.pira('cell8')

cell1.position(1,2,0)
cell2.position(1,1,0)
cell3.position(2,2,0)
cell4.position(2,1,0)
cell5.position(3,2,0)
cell6.position(3,1,0)
cell7.position(4,2,0)
cell8.position(4,1,0)
If I write dir(cell1) i have this results:

['Section', '__call__', '__class__', '__delattr__', '__delitem__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'all', 'allsec', 'apic', 'apical', 'basal', 'baseattr', 'basic_shape', 'biophys', 'biophys_inhomo', 'cas', 'connect2target', 'dend', 'geom', 'geom_nseg', 'hname', 'hocobjptr', 'init', 'next', 'position', 'ref', 'setpointer', 'soma', 'somatic', 'subsets', 'synlist', 'topol', 'x', 'y', 'z']

I don't have for example Exp2Sync, how to connect cells ( I try connect2target but i have an error like this:

Traceback (most recent call last):
File "C:\Users\Właściciel\Desktop\komórki\hh.py", line 40, in <module>
cell1.connect2target(cell2)
RuntimeError: hoc error

Please help now I really don't know what to do :(
ramcdougal wrote:Stepping back a bit, if the goal is to use Python to create a network of some fixed pool of cell types, it's probably easiest and most efficient to use NEURON's GUI tools to convert swc files into cell templates (this is a manual process, but you only have to do it once per cell type) rather than have NEURON convert each file at each run.

To transform an swc file into a neuron template file:
  • Tools - Miscellaneous - Import 3D
  • "choose a file" button, then select a file and click "Read"
  • export - CellBuilder
  • use the Biophysics tab to place mechanisms (ion channels, etc...) throughout the cell
  • Use the management tab to export by selecting "Cell type", optionally entering a "Classname" and then selecting "Save hoc code in file"
If, by doing the above, you created a file "myneuron.hoc" with a cell type called "MyCell", you can load the template via:

Code: Select all

from neuron import h
h.load_file('myneuron.hoc')
You can create multiple copies by invoking the constructor repeatedly:

Code: Select all

cell1 = h.MyCell()
cell2 = h.MyCell()
To translate one of the cells, use .position, e.g.

Code: Select all

cell2.position(100, 0, 0)
The same basic procedure can be used programmatically, but again, you may be better off letting the GUI help you with this part of the initial setup. To transfer to a cell builder use i3d.cbexport() instead of i3d.instantiate(). If you have a CellBuilder cb, the management tab options are in cb.manage, and the main function for exporting as a HOC template is cb.manage.save_class.

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Wed Feb 11, 2015 3:13 pm

These cells have morphology; it doesn't suffice to say connect to cell2, because that doesn't say where on cell2 you want to connect.

The basic procedure is to create a mechanism for the post-synaptic side of the connection (e.g. an Exp2Syn) at a given location on the post-synaptic cell and then make the connection either with connect2target or NetCon.

Code: Select all

from neuron import h
h.load_file('neu.hoc')

cell1 = h.pira()
cell2 = h.pira()

# we'll make a connection from 1 to 2's dend[0](0.5)
# the current is on the post syn side, so we'll define an Exp2Syn and put it there
post_syn = h.Exp2Syn(0.5, sec=cell2.dend[0])

# now for the connection; connect2target uses the soma's membrane potential as a trigger
connection = cell1.connect2target(post_syn)
The last line above is equivalent to the more generalizable statement:

Code: Select all

connection = h.NetCon(cell1.soma(1)._ref_v, post_syn)
See the documentation for NetCon to see how to customize the location of the presynaptic synapse, the synaptic delay, threshold for triggering, and synaptic weight.

The default weight in both cases is 0. To adjust it to 1, use

Code: Select all

connection.weight[0] = 1

Kolorek
Posts: 20
Joined: Wed Jan 28, 2015 3:48 pm

Re: SWC files

Post by Kolorek » Thu Feb 12, 2015 4:46 pm

Thank you :)

I have some other questions

1) how can i put some current
(something like this)

Code: Select all

ic = h.IClamp(cell1(1.0))
ic.deley = 1
ic.dur = 1
ic.amp = 100
2) I saw that there is something like neuron rxd but when i want to import (from neuron import rxd) it says that there is no rxd -I ask about it because like i wrote i want to have graph with the concentration of ions inside and outside the cells

One more time thank you :)

ramcdougal
Posts: 160
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: SWC files

Post by ramcdougal » Thu Feb 12, 2015 7:03 pm

With an IClamp, you have the same situation as with an Exp2Syn: there are many places on cell1 that could receive the current (the various dendrites, the soma, the axon), and you have to be specific. The syntax is similar to that for the synapse; to put a current clamp at the center (0.5) of cell1's soma, use:

Code: Select all

ic = h.IClamp(0.5, sec=cell1.soma)
ic.delay = 1
ic.dur = 1
ic.amp = 100
The reaction-diffusion (rxd) module was introduced in NEURON 7.3; you're running 7.2. If you don't have an extensive Python setup that you need to preserve, on Windows I'd recommend using the cygwin version; the NEURON download page is here; the direct link for the 32-bit cygwin version for Windows is: http://www.neuron.yale.edu/ftp/neuron/v ... -setup.exe. The module was described in McDougal et al 2013.

The rxd module will allow you to keep track of concentrations, and I encourage you to try it. That said, you can handle changes to concentrations with NMODL mechanisms instead. See for, example,
this file from Canavier 1999. (That file also includes sodium diffusion at a default rate of 0.6 mu^2/ms; remove the LONGITUDINAL_DIFFUSION line to disable sodium diffusion.)

Post Reply