Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed implementation of linesource method #5

Merged
merged 3 commits into from
Jan 13, 2017

Conversation

espenhgn
Copy link
Collaborator

I found an ugly bug in the linesource implementation, such that somatic currents were not correctly accounted for when computing the potential in proximity to the soma. I fixed now this implementation. The output of the different methods can be compared like this:

#!/usr/bin/env python
'''compare methods for LFPy.RecExtElectrode class'''
import LFPy
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.collections import PolyCollection
import neuron

################################################################################
# Main script, set parameters and create cell, synapse and electrode objects
################################################################################

# clear out old section references in NEURON
neuron.h('forall delete_section()')

cellParameters = {
    'morphology' : 'dipole.hoc',
    'tstartms' : 0.,
    'tstopms' : 10.,
    'timeres_python' : 0.1,
    'timeres_NEURON' : 0.1,
    'pt3d' : True,    
}

cell = LFPy.Cell(**cellParameters)
cell.set_pos(zpos=-15)

synapseParameters = {
    'idx' : 0,
    'e' : 0,
    'syntype' : 'Exp2Syn',
    'tau1' : 0.1,
    'tau2' : 1.,
    'weight' : 0.01,
    'record_current' : True
}
synapse = LFPy.Synapse(cell, **synapseParameters)
synapse.set_spike_times(np.array([2.]))

# compute potentials on a grid in x-z plane at a y-depth of 5 um
dx = 5 # spatial resolution
x = np.arange(-80., 80.+dx, dx)
z = np.arange(-150., 150.+dx, dx)
X, Z = np.meshgrid(x, z)
electrodeParameters = {
    'x' : X.flatten(),
    'y' : np.zeros(X.size)-5,
    'z' : Z.flatten(),
    'sigma' : 0.3,
}

# compute cell response and extracellular potentials
cell.simulate(rec_imem=True, rec_isyn=True)

methods = ['pointsource', 'linesource', 'som_as_point']
electrodes = [LFPy.RecExtElectrode(cell=cell, method=method, **electrodeParameters) for method in methods]
for electrode in electrodes:
    electrode.calc_lfp()


########################
# Figure
########################
fig = plt.figure(figsize=(16, 9))
fig.subplots_adjust(left=0.02, right=0.98)
gs = GridSpec(1, 3, left=0.02, right=0.98)
axes = [fig.add_subplot(gs[0, i], aspect='equal') for i in range(len(methods))]

#morphology - line sources for panels A and B
zips = []
xz = cell.get_idx_polygons()
for x, z in xz:
    zips.append(zip(x, z))
for ax, method, electrode in zip(axes, methods, electrodes):
    polycol = PolyCollection(zips,
                             linewidths=(0.5),
                             edgecolors='k',
                             facecolors='none',
                             zorder=-5)
    ax.add_collection(polycol)

    #morphology mid points
    ax.plot(cell.xmid, cell.zmid, 'o', mec='none', mfc='k',
             markersize=3, zorder=0)
    
    # mark synapse location
    ax.plot(synapse.x, synapse.z, '> ', mec='r', mfc='r', markersize=5, zorder=1)

    t_max = abs(synapse.i) == abs(synapse.i).max()
    vmax = 0.005
    im = ax.contourf(X, Z, electrode.LFP[:, t_max].reshape(X.shape),
                      levels=np.linspace(-vmax, vmax, 51),
                      cmap=plt.get_cmap('PRGn'), zorder=-10,
                      vmin=-vmax,
                      vmax=vmax)
    ax.set_title(method)
    bbox = np.array(ax.get_position())
    cax = fig.add_axes([bbox[0][0]+(bbox[1][0]-bbox[0][0])/4, bbox[0][1]-0.05, (bbox[1][0]-bbox[0][0])/2, 0.015])
    axcb = fig.colorbar(im, cax=cax, orientation='horizontal')
    axcb.outline.set_visible(False)
    axcb.set_label('$\phi$ (mV)')
    axcb.set_ticks(np.arange(-np.round(vmax), np.round(vmax)+0.001, 0.001))

Corresponding morphology file:

/* ----------------------------------------------------
dipole.hoc
-----------------------------------------------------*/
create soma[1]
create dend[1]
soma[0] {
    pt3dadd(0, 0, 0, 10)
    pt3dadd(0, 0, 30, 10)
}
dend[0] {
    pt3dadd(0, 0, 30, 10)
    pt3dadd(0, 0, 60, 10)
}
connect dend[0](0), soma[0](1)

I also added an assertion that the number of x,y,z-coordinate values for electrodes are identical

…x,y,z-coordinate values for electrodes are identical
Copy link
Contributor

@torbjone torbjone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix confirmed. I have a few trivial suggestions for style improvements, but I'll make a separate pull request for that soon.

@espenhgn
Copy link
Collaborator Author

Fine, will merge.

@espenhgn espenhgn merged commit e12fbd2 into LFPy:master Jan 13, 2017
@espenhgn espenhgn deleted the fix_linesource branch January 13, 2017 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants