Show Diameter

Anything that doesn't fit elsewhere.
Post Reply
Ziling Wang
Posts: 4
Joined: Mon Mar 25, 2024 2:45 am

Show Diameter

Post by Ziling Wang »

The code below is plotting the membrane potential of the entire neuron via PlotShape, but I want to show the diameter of the neuron. When I set ps.show(1) as suggested by the document, it doesn't successfully show the diameter of the neuron.
Is there any solution to make plotshape in neuron python display the radius of the neuron?

Code: Select all

 v_vals = [seg.v for sec in h.allsec() for seg in sec]
    print(min(v_vals),max(v_vals))
    if setv:
        minv= vmin
        maxv = vmax
    else:
        minv= min(v_vals)
        maxv= max(v_vals)

            
    ps = h.PlotShape(False)  # False tells h.PlotShape not to use NEURON's gui
    ps.variable('v')
    ps.scale(minv, maxv)
    ax = ps.plot(pyplot, cmap=cmap)
    ps.show(1)

    # 设置坐标轴范围
    ax.set_xlim([-700, 0])  # 替换 xmin 和 xmax 为所需的 x 范围
    ax.set_ylim([500, 1400])  # 替换 ymin 和 ymax 为所需的 y 范围
    ax.set_zlim([-1000, 00])  # 替换 zmin 和 zmax 为所需的 z 范围
    # 关闭网格
    ax.grid(False)
    # 关闭坐标轴
    ax.axis('off')
    # 保存为 EPS 格式
    if savefig:
        plt.savefig(figname+'.eps', format='eps')
    
    
    
    # second figure
    ps = h.PlotShape(True)
    ps.variable("v")
    ps.scale(minv, maxv)
    fig = ps.plot(plotly, cmap=cmap)


    # 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=minv,
            cmax=maxv,
            colorbar=dict(
                title=colorbarlabel,
                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=True)
    fig.update_yaxes(showticklabels=False, showgrid=True)
    fig.update_layout(
        plot_bgcolor='rgba(0,0,0,0)'
    )

    fig.show()
    if savefig:
        # 保存动图为 HTML 文件
        plotly.offline.plot(fig, filename=figname+'.html', auto_open=False)
    

    # 创建自定义的颜色映射
    cmap = cmap
    colormap = cm.ScalarMappable(cmap=cmap, 
            norm=mcolors.Normalize(vmin=minv, vmax=maxv))

    # 创建一个新的图形和轴
    fig, ax = plt.subplots(figsize=(6, 1))

    # 绘制颜色条
    cbar = plt.colorbar(colormap, cax=ax, orientation='horizontal')
    cbar.set_label(colorbarlabel)
    if ticklabels:
        cbar.set_ticks(np.arange(tickmin, tickmax, dt))
        cbar.set_ticklabels([f'{i}' for i in np.arange(tickmin, tickmax, dt)])

    # 设置图的边界
    plt.tight_layout()

    if savefig:
        # 保存为 EPS 格式
        plt.savefig(figname+'colorbar'+'.eps', format='eps')

    # 显示图形(可选)
    plt.show()
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: Show Diameter

Post by hines »

Code: Select all

ps.show(1)
At least for the classical InterViews nrn gui, the diameter is shown when the arg to show is 0. (as specified in https://nrn.readthedocs.io/en/8.2.3/pyt ... Shape.show )

Often times, the neurites ar so slender compared to length, it is not easy to see the diameter (though the short thick soma looks fine). In that case one can use https://nrn.readthedocs.io/en/8.2.3/pyt ... .len_scale
Ziling Wang
Posts: 4
Joined: Mon Mar 25, 2024 2:45 am

Re: Show Diameter

Post by Ziling Wang »

Sorry, I set up ps.show(0), it still doesn't show diameter, even for soma. Following are the code and corresponding figure.
code:
v_vals = [seg.v for sec in h.allsec() for seg in sec]
print(min(v_vals),max(v_vals))
if setv:
minv= vmin
maxv = vmax
else:
minv= min(v_vals)
maxv= max(v_vals)


ps = h.PlotShape(False) # False tells h.PlotShape not to use NEURON's gui
ps.variable('v')
ps.scale(minv, maxv)
ax = ps.plot(pyplot, cmap=cmap)
ps.show(0)

# 设置坐标轴范围
ax.set_xlim([-700, 0]) # 替换 xmin 和 xmax 为所需的 x 范围
ax.set_ylim([500, 1400]) # 替换 ymin 和 ymax 为所需的 y 范围
ax.set_zlim([-1000, 00]) # 替换 zmin 和 zmax 为所需的 z 范围
# 关闭网格
ax.grid(False)
# 关闭坐标轴
ax.axis('off')
# 保存为 EPS 格式
if savefig:
plt.savefig(figname+'.eps', format='eps')



# second figure
ps = h.PlotShape(True)
ps.variable("v")
ps.scale(minv, maxv)
fig = ps.plot(plotly, cmap=cmap)
ps.show(0)

figure: https://resonant-frame-a38.notion.site/ ... 8362?pvs=4
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: Show Diameter

Post by hines »

After looking around a bit in the github history for nrn, my first impression is that show diameter is supposed to work with plotly. I.e.
https://github.com/neuronsimulator/nrn/pull/2404 at the end of June 2023 with the same mode call as works for InterViews GUI PlotShape.

After some experimenting, I found that

Code: Select all

def p():
  from neuron import h
  from matplotlib import pyplot
  ps = h.PlotShape(False)
  ps.show(0)
  ps.plot(pyplot)
  pyplot.show()
does show the diameters.

Apparently, at least at the moment, for plotly, the ps.show(0) must be prior to the ps.plot(pyplot). That differs from the more dynamic behavior of the InterViews GUI. The documenation should be updated to mention this difference.
Ziling Wang
Posts: 4
Joined: Mon Mar 25, 2024 2:45 am

Re: Show Diameter

Post by Ziling Wang »

I tested the following code, but it didn't show diameter of the soma. And I would also like to ask how to adjust a better view in Shapeplot so that the neuron is right in the middle.

# set morphology
soma = h.Section(name='soma')
soma.nseg = 1
soma.diam = 100
soma.L = 10.
from neuron import h
from matplotlib import pyplot
ps = h.PlotShape(False)
ps.show(0)
ps.plot(pyplot)
pyplot.show()
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: Show Diameter

Post by hines »

Your example code above worked for me (after prefixing "from neuron import h").
The show diameter feature for pyplot is only available in version 9. For mac, linux you can

Code: Select all

pip install NEURON-nightly
For windows the latest master build is (see the first master label at https://github.com/neuronsimulator/nrn/ ... indows.yml and click on
the title: Remove check for deprecated header (#2813) , and then download the nrn-nightly-AMD64.exe artifact, unzip the downloaded file to get the exe)
Ziling Wang
Posts: 4
Joined: Mon Mar 25, 2024 2:45 am

Re: Show Diameter

Post by Ziling Wang »

It worked, thank you very much!
Post Reply