Raster plots in Python

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

Moderator: hines

Post Reply
BBAmp
Posts: 12
Joined: Fri Mar 26, 2010 2:36 pm

Raster plots in Python

Post by BBAmp »

Hello,

Is is possible to create a raster plot of a network of cells using pylab the same way I can record and create a graph of the membrane potentials of cells?

I've tried following the syntax used in the NEURON documentation to the best of my ability:

Code: Select all

tobj = h.NetCon(h.PM[0].soma(.5)._ref_v, h.nil)
tobj.threshold = -65
tobj.record(vectemp)
PM[0] is a neuron created in NEURON.

I don't get any error messages but if I try to plot either tobj or the vecotor 'vectemp' I get a blank graph in pylab.

Any help is appreciated,
-Youngmin
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Raster plots in Python

Post by ted »

The answer is in the documentation of pylab.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Raster plots in Python

Post by ted »

Looking at your question again, I think the first thing to find out is whether vectemp actually captured any spike times, and if not, why not.

I don't know if pylab has a raster plot built in, but if it has a function that plots a mark like "|" at a specified location on a graph, it should be easy to make your own raster plot.
BBAmp
Posts: 12
Joined: Fri Mar 26, 2010 2:36 pm

Re: Raster plots in Python

Post by BBAmp »

Hello,

Thank you for your replies. I took a similar approach to what you suggested and figured out what was wrong.

My project currently involves taking a simulation coded entirely in NEURON and graph all output in python. It turns out I was pointing the netcon object to h.PM.soma(.5)._ref_v (where PM is a list of neurons created in NEURON) which didn't record anything because the (python) netcon object was point to a (NEURON) cell. That's why the vector was empty. It would have recorded something had the neuron been created in python.

If anyone is trying to graph a simulation coded entirely in NEURON, make sure you make the netcon object, record the events to vectors entirely in NEURON, THEN call the vectors from Python. To make a raster plot of the data using pylab, simply plot a scatter plot of the vectors. You can also change the plot markers to squares, circles, triangles, etc.

If your neurons have been initialized in python, then the syntax in my first post should work just fine.

My PI Dr. Thomas has been in contact with Tom McTavish who will soon be putting up a neat raster plot package specifically for NEURON/Python on a repository. Using his package you will be able to plot vertical bars to mark spike times which pylab cannot do.

I will soon be writing a more detailed documentation of the steps I took to create raster plots in python (in general and not just specific to my problem here). I will post the documentation here once it's written.
BBAmp
Posts: 12
Joined: Fri Mar 26, 2010 2:36 pm

Re: Raster plots in Python

Post by BBAmp »

This documentation is for plotting raster plots ONLY. Please refer to the online documentation of pylab, NEURON, and Python to learn more about plotting NEURON/Python data.


1. Python Raster Plots for NEURON/Python Simulations
This section assumes all your cells and parameters have been initialized in Python.

2. Python Raster Plots for NEURON Simulations
This section assumes all your cells and parameters have been initialized in NEURON.


1. Python Raster Plots for NEURON/Python Simulations

Code: Select all

import neuron, matplotlib.pylab as mp

h = neuron.h
Section = h.Section

list_of_cells = [] #this list will contain all the cells

for i in range(50): #assuming you want 50 cells in the population.
	list_of_cells.append(Section()) #append the list with NEURON sections (equivalent to creating a cell)


# here you will have to specify cell parameters, network connectivity, import mechanisms, etc.


#create vectors that will record data for the raster plot:
tvec = h.Vector() #time
idvec = h.Vector() #cell number


recording_netcon = h.NetCon(list_of_cells[i].soma(.5)._ref_v, h.nil, sec=list_of_cells[i].soma(.5))
recording_netcon.threshold = -10 #set threshold to a value of your choice
recording_netcon.record(tvec, idvec, i)

h.load_file('stdrun.hoc')
h.tstop = 100 #simulation time in ms
h.run() #run the simulation

#once the simulation is finished, plot using matplotlib:

#you may add titles, axis labels, legends, etc. as well.  The how-to is in the pylab documentation
mp.figure(1) #to change the size of the plot use: mp.figure(1, figsize=(20,20))
mp.scatter(tvec, idvec, c='b', marker='+') #you can change the color and marker according to the pylab documentation
mp.savefig('screenshots/raster_plot.png') #this will save the plot - comment out if this is not needed

mp.draw() 
mp.show() #this allows you to view the plot - comment out if this not needed
mp.close()


2. Python Raster Plots for NEURON Simulations



Suppose your cells have been appended to a list called 'list_of_cells' and the cells have been appropriately initialized with the desired properties. You must then record the spike trains entirely within NEURON using the following syntax:

Code: Select all

tvec = new Vector()
idvec = new Vector()

proc preprasterplot2() {
     for i=0, list_of_cells.count()-1 {
	 list_of_cells.object(i).soma netcon = new NetCon(&v(0.5), nil, -10, 1, 0)    
	 netcon.record(tvec, idvec, i+1)      
     } 
Lets assume this code is saved to a file called 'neuron.hoc'

Create a python file in the same directory as 'neuron.hoc' with the following code:

Code: Select all

import neuron, matplotlib.pylab as mp

h = neuron.h
Section = h.Section

h.load_file('neuron.hoc')

h.load_file('stdrun.hoc')
h.tstop = 100 #time of simulation in ms
h.run() #run the simulation

#now the plotting code:
mp.figure(1) # to increase the size of the graph use: mp.figure(1, figsize=(20,20))
mp.scatter(h.tvec, h.idvec, c='b', marker='+')

mp.savefig('screenshots/raster_plot.png') #saves the plot – comment if this is not needed
mp.draw()
mp.show() #displays the plot immediately after the simulation is over – comment if this is not needed
mp.close()
wwlytton
Posts: 66
Joined: Wed May 18, 2005 10:37 pm
Contact:

Unable to get pylab.savefig() to work with nrniv -python

Post by wwlytton »

savefig for a pylab.scatter() or line plot works from native python but does not seem to work from the python interpreter when using 'nrniv -python'

import pylab
pylab.scatter([5,3,4],[1,7,8])
pylab.savefig("ff.png") # generates 'libpng error: zlib error' under nrniv
pylab.savefig("ff.svg") # works
pylab.savefig("ff.pdf") # no error but legal pdf file is blank
pylab.savefig("ff.ps") # works
pylab.savefig("ff.eps") # works
pylab.show() # works

the following are the formats that claim to be supported:
# Supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz

Saving to .png works when running python interpreter directly (and then 'import neuron')
Post Reply