Page 1 of 1

Voltage gradient and color scale in anatomically detailed model in Python

Posted: Thu Aug 24, 2023 7:12 am
by bril27
Hello,
I have the following code which reads a mitral cell SWC morphology, runs a simulation with injected current, and then plots the morphology with the results of the simulation based on data from NEURON's plotshape mechanism.

Code: Select all

import plotly
from neuron import h, gui
from neuron.units import mV, ms
h.load_file('stdrun.hoc')
h.load_file('import3d.hoc')
cell = h.Import3d_SWC_read()
path_to_file = "mitral-cell-08-SDB20130216pair2_0.CNG.swc"
cell.input(path_to_file)
i3d = h.Import3d_GUI(cell, False)
i3d.instantiate(None)
h.hh.insert(h.allsec())                 # inserts hh mech into all of the sections
for sec in h.allsec():                  # set the number of segments for consistent spatial discretization and computational accuracy
  sec.nseg = 1 + 2 * int(sec.L / 40)
ic = h.IClamp(h.soma[0](0.5))
ic.amp = 10
ic.delay = 1
ic.dur = 1
h.finitialize(-65 * mV)
h.continuerun(2 * ms)
ps = h.PlotShape(False)
ps.variable('v')
ps.scale(-80, 30)
ps.plot(plotly).show()
My question is: Is there a straightforward way of plotting a colormap or color scale (that reflects and labels the voltage gradients in the different segments) with the neuron's 3D morphology in plotly? The current code does not plot a color scale legend.

Re: Voltage gradient and color scale in anatomically detailed model in Python

Posted: Sun Sep 03, 2023 4:36 pm
by ramcdougal
The trick here is that calling ps.plot(plotly) returns a go.Figure. And then anything you can do with regular plotly graphs, you can do with that... I don't particularly like the below solution, but one way is to create a hidden 2D graph that has a colorbar:

A full, working example is at:
https://colab.research.google.com/drive ... sp=sharing

The relevant code is:

Code: Select all

ps = h.PlotShape(False)
ps.variable("v")
fig=ps.plot(plotly, cmap=cm.cool)

v_vals = [seg.v for sec in h.allsec() for seg in sec]
# Create a custom colormap using Matplotlib (cool colormap)
cmap = cm.cool

# Create a colormap function
colormap = cm.ScalarMappable(cmap=cmap, norm=mcolors.Normalize(vmin=0, vmax=1)).to_rgba

# Map the normalized values to a Plotly colorscale as strings
plotly_colorscale = [[v, f'rgb{tuple(int(255 * c) for c in colormap(v)[:3])}'] for v in np.linspace(0, 1, cmap.N)]

# Create a separate scatter plot for the colorbar
colorbar_trace = go.Scatter(
    x=[0],
    y=[0],
    mode='markers',
    marker=dict(
        colorscale=plotly_colorscale,
        cmin=min(v_vals),
        cmax=max(v_vals),
        colorbar=dict(
            title='v (mV)',
            thickness=20  # Adjust the thickness of the colorbar
        ),
        showscale=True
    )
)

# Add the colorbar trace to the figure
fig.add_trace(colorbar_trace)
fig.update_xaxes(showticklabels=False, showgrid=False)
fig.update_yaxes(showticklabels=False, showgrid=False)
fig.update_layout(
    plot_bgcolor='rgba(0,0,0,0)'
)
fig.show()
Disclaimer: I had help from both GPT-3.5 and GPT-4 figuring this out.