plotting 3D morphology

334 views
Skip to first unread message

Gin Cruz

unread,
Jul 4, 2021, 3:28:42 AM7/4/21
to NetPyNE Q&A forum
Hello,

I am importing morphology from neuromorpho (swc) and would like to see a 3D plot like ones using plotly in a Jupyter notebook (where I can rotate the plot, see the voltage gradients across the morphology in a color code, etc).  I am new to netpyne and I haven't come across an example of how this might be done. I like to work on a Jupyter notebook for my simulations and plots.

Also, is there a way to make a movie of voltage gradients/changes across the morphology of the cell?

Thanks for your help!

-Ginny 

Joe Graham

unread,
Jul 13, 2021, 7:58:07 PM7/13/21
to NetPyNE Q&A forum
Hi Ginny,

Thanks for your question.  You can get an interactive 3D plot of a neuron or neurons in your model by using sim.analysis.plotShape:
http://netpyne.org/netpyne.analysis.network.html#netpyne.analysis.network.plotShape

In order to see the voltage, you would set cvar='voltage'.

Here's an example:

    from netpyne import specs, sim

    netParams = specs.NetParams()

    cellRule = netParams.importCellParams(
        label='swc_cell', 
        fileName='BS0284.swc', 
        cellName='BS0284',
        )

    netParams.renameCellParamsSec('swc_cell', 'soma_0', 'soma')  # rename imported section 'soma_0' to 'soma'

    for secName in cellRule['secs']:
        cellRule['secs'][secName]['mechs']['pas'] = {'g': 0.0000357, 'e': -70}
        cellRule['secs'][secName]['geom']['cm'] = 1
        if secName.startswith('soma'):
            cellRule['secs'][secName]['mechs']['hh'] = {'gnabar': 0.12, 'gkbar': 0.036, 'gl': 0.003, 'el': -70}

    netParams.popParams['swc_pop'] = {'cellType': 'swc_cell', 'numCells': 1}

    cfg = specs.SimConfig()
    cfg.filename = 'plotshape'
    cfg.duration = 10
    cfg.recordTraces = {'V_soma': {'sec': 'soma', 'loc': 0.5, 'var': 'v'}}
    cfg.recordStep = 1

    cfg.analysis['plotTraces'] = {'include': ['all'], 'saveFig': True}   

    sim.createSimulateAnalyze(netParams=netParams, simConfig=cfg)

    sim.analysis.plotShape(cvar='voltage', includePre=[0], saveFig=True)


Unfortunately, we don't have a way to make movies from plotShape right now, but that is a feature that has been requested before, so we will look into adding that capability soon.

Please don't hesitate to ask if you have any more questions.


Cheers,
Joe
plotshape_shape.png

Joe Graham

unread,
Jul 19, 2021, 11:39:37 PM7/19/21
to NetPyNE Q&A forum
We have added the ability to generate movies of voltage to the development branch of NetPyNE (it will get added to the main branch in our next release).  

Please see our latest Jupyter notebook tutorial to see how it's done:
https://github.com/Neurosim-lab/netpyne/blob/development/netpyne/tutorials/voltage_movie_tut.ipynb

Please let us know if you have any problems making a movie, and please share any cool movies you generate! 

Gin Cruz

unread,
Jul 20, 2021, 12:31:36 AM7/20/21
to NetPyNE Q&A forum
Thanks so much, Joe! It might take some time for me to get to the movie part but I think I've got what I need. 

Carol Upchurch

unread,
Oct 11, 2022, 12:18:08 PM10/11/22
to NetPyNE Q&A forum
https://github.com/suny-downstate-medical-center/netpyne/blob/development/netpyne/tutorials/voltage_movie_tut/voltage_movie_tut.ipynb
I'm just reposting this link because the other one gives me a 404. I'm really grateful this forum is here, it really clears up a lot of things. Thanks.

Carol Upchurch

unread,
Oct 12, 2022, 11:54:30 AM10/12/22
to NetPyNE Q&A forum
I ended up not being able to get this script working on my computer as is. Instead of writing multiple frames and gluing them together it just kept overwriting itself. I ended up writing a small separate function to call within  sim.runSimWithIntervalFunc so that I could change the filename of each individual plot. My code ended up looking like: 

def make_mult_frames(simTime):
    sim.analysis.plotShape( includePre = [0],
    includePost= [0],
    cvar       = 'voltage',
    clim       = [-70, -20],
    saveFig    = 'movie'+str(simTime)+".png",
    showFig    = False)
   
sim.runSimWithIntervalFunc(1.0, make_mult_frames,timeRange=[10, 20])

I tried a couple of other things like including that line for saveFig in funcArgs or calling it within sim.analysis.plotShape, but python didn't know what simTime was unless I did it this way. If there's a cleaner way to do this or if my computer is just weird or something please let me know. I'm running it in JupyterLab, I'm using python 3 and upgraded netpyne yesterday. It did work after this change. Thanks again for your help.

Salvador Dura

unread,
Nov 20, 2022, 3:13:00 PM11/20/22
to NetPyNE Q&A forum
thanks Carol, we had made some minor changes to runSimWithIntervalFunc() which maybe messed up this example; but I believe in the last netpyne version (a few days ago) these changes were reverted, so hopefully the original movie code still works. In any case your solutions looks good too. 

Shaina Short

unread,
May 31, 2023, 2:33:21 PM5/31/23
to NetPyNE Q&A forum
Is there an updated version of this tutorial? Mine is hanging up on the very last loop copied below. Many thanks (sorry to be late to the game!)

images = []
filenames = natsort.natsorted([file for file in os.listdir() if 'movie' in file and file.endswith('.png')])
for filename in filenames:
    images.append(imageio.imread(filename))
imageio.mimsave('shape_movie.gif', images)

Eugenio Urdapilleta

unread,
Jul 11, 2023, 3:52:27 PM7/11/23
to NetPyNE Q&A forum
Hi Shaina,
 I dug into your problem with this tutorial, as I also get nothing to watch when running it and it is a really interesting tutorial. To put some context, a couple of years ago, there was a massive update in the plotting/analysis structure within NetPyNE. This tutorial worked perfectly fine with the previous version, but now it has an issue when calling "runSimWithIntervalFunc" with a plot function. I will propose a patch for this issue for the next release. Meanwhile, if you want to work with this tutorial, you may do the following changes in your local NetPyNE: In the function "plotting/plotShape.py"
-- line 173: "if axis is None:"  --> "if axis is None or isinstance(axis,Number):"
-- line 304: "plt.savefig(filename, dpi=dpi" --> "plt.savefig(filename, dpi=plt.rcParams['figure.dpi'])" 

I hope it works,

Eugenio
Reply all
Reply to author
Forward
0 new messages