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

Set pos 3d fixed #4

Merged
merged 12 commits into from
Jan 11, 2017
Merged

Set pos 3d fixed #4

merged 12 commits into from
Jan 11, 2017

Conversation

alejoe91
Copy link
Contributor

Cell.set_pos() fixed:

  • 3d segments are moved differentially wrt to current soma location
  • start and end of segments are moved anyways (deleted if-else)

@espenhgn
Copy link
Collaborator

@alejoe91 I cannot reproduce the problem you're seeing. I set up these test files:

#!/usr/bin/env python
'''test Cell pt3d arg'''
import LFPy
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
import neuron

neuron.h('forall delete_section()')

cellParameters = {
    'morphology' : 'example_morphology.hoc',
    'pt3d' : True,    
}
cell = LFPy.Cell(**cellParameters)

xzpositions = [0, 0, 10, 20]

########################
# Figure
########################
fig, axes = plt.subplots(1, len(xzpositions), figsize=(16, 9), sharex=True, sharey=True)
for i, (ax, xzpos) in enumerate(zip(axes, xzpositions)):
    if i != 0: # is soma position in (0,0,0) after creating the cell?
        cell.set_pos(xpos=xzpos, ypos=0, zpos=xzpos)

    print cell.somapos

    zips = []
    xz = cell.get_idx_polygons()
    for x, z in xz:
        zips.append(zip(x, z))
    polycol = PolyCollection(zips,
                             linewidths=(0.5),
                             edgecolors='k',
                             facecolors='none',
                             zorder=0)
    ax.add_collection(polycol)

    if cell.pt3d:
        for x, z in cell.get_pt3d_polygons():
            zips.append(zip(x, z))
        polycol = PolyCollection(zips,
                                 linewidths=(0.5),
                                 edgecolors='r',
                                 facecolors='gray',
                                 zorder=-1)
        ax.add_collection(polycol)

    #morphology mid points
    ax.plot(cell.xmid, cell.zmid, 'o', mec='none', mfc='k',
             markersize=3, zorder=0)
    
plt.show()

and

/* ----------------------------------------------------
example_morphology.hoc

This hoc file creates a neuron of the following shape:

            \       
             \     
              \   /
               \ /
                V
                |
                |
                |
                O
                
Note the conventions:
 - soma needs to be a list (soma[0], not soma),
 - use soma for the soma compartment,
 - use a name starting with dend for the dendrites.
-----------------------------------------------------*/

create soma[1]
create dend[3]

soma[0] {
    pt3dadd(0, 0, 0, 5)
    pt3dadd(0, 0, 15, 25)
    pt3dadd(0, 0, 30, 5)
}

dend[0] {
    pt3dadd(0, 0, 30, 5)
    pt3dadd(0, 0, 150, 2)
}

dend[1] {
    pt3dadd(0, 0, 150, 2)
    pt3dadd(-50, 20, 200, 1)
}

dend[2] {
    pt3dadd(0, 0, 150, 2)
    pt3dadd(30, 0, 160, 1)
}

connect dend[0](0), soma[0](1)
connect dend[1](0), dend[0](1)
connect dend[2](0), dend[0](1)

With the current master branch the soma positions are correct ([ 0. 0. 0.], [ 0. 0. 0.], [ 10. 0. 10.], [ 30. 0. 30.]) for the different x,z-offsets, as well as pt3d and segment coordinate info:
test_pt3d_fix_master.pdf

Notice on the first iteration the cell.set_pos function is not used, but the cell should default to (0,0,0) anyway.

However on your pull request the cell's soma's center position is no longer in (0,0,0) and pt3d info and segment coordinate info do not overlap as they should:
test_pt3d_fix_alesioe91.pdf

@alejoe91
Copy link
Contributor Author

Hi,

I was wrong in deleting the if-else, because the start, end, and midpoints are updated in _update_pt3d (which calls _collect_geometry()). So I was basically moving them twice.

I think that the other fix (passing diffx, diffy and diffz to _set_pt3d_pos()) is correct, though. In fact, if you set_pos(50,0,0) for three times in a row:

  • the first time the cell is correctly moved to (50,0,0)
  • second time to (100,0,0)
  • third time to (150,0,0)
    and that's because in _set_pt3d_pos():

def _set_pt3d_pos(self):
for i in range(len(self.x3d)):
self.x3d[i] += self.somapos[0]
self.y3d[i] += self.somapos[1]
self.z3d[i] += self.somapos[2]
self._update_pt3d()

so if the soma position hasn't changed, the 3dpoints are moved anyways. Do you agree?

I just committed a corrected version (commit 'reinserted if-else, kept diffx, diffy, diffz')

@espenhgn
Copy link
Collaborator

@alejoe91 I was mistaken above: the last position was (30,0,30) on the master branch when it should have been (20,0,20). Sorry about that.

However, I reran now with your code, and the cell position is not correctly instantiated to (0, 0, 0) when pt3d==True. For the simplified geometry above the result is (0,0,-15), however if pt3d==False the result is (0,0,0). The issue with non-overlapping geometry appears fixed though.

@alejoe91
Copy link
Contributor Author

@espenhgn
The first time the cell position is set so that the end of the soma is at (0,0,0) and I don't know why actually. It might depend on the .hoc file.

Anyway, try the new version (after the first time the soma and the segments should always in the correct position) and let me know :)

@espenhgn
Copy link
Collaborator

There is a call to self.set_pos() immediately after calling self._collect_geometry() in cell.py line 210. An ugly fix would be to call self.set_pos() twice but I'm sure there are better ways.

@alejoe91
Copy link
Contributor Author

I saw that, that's why I was quite surprised to see that the first time it doesn't set the position correctly.
I'm sure there are better ways as well, but for now we could keep it like this.
If you need to move your neuron it works, otherwise the soma ends at (0,0,0), which is not such a displacement.

@espenhgn
Copy link
Collaborator

For these morphology files the root of the morphology (the 1st data point for the first section, typ. the soma) may be some arbitrary location in 3D space. For consistency between different cell models in LFPy we made a decision that the "default" cell soma position should be having it's mid point at (0,0,0), not one of it's end points. That should also be the case if the soma.nseg > 1.

@alejoe91
Copy link
Contributor Author

Ok, then I will try to dig deeper and see where it gets displaced.

@alejoe91
Copy link
Contributor Author

alejoe91 commented Jan 11, 2017

@espenhgn
I think I found it!
in collect_geometry first you compute the start, end, mid points, then the somapos, so the first time the somapos is not (0,0,0). In set_pos(), then since the displacement is computed relative to the soma, diffx, diffy, and diffz are not 'correct'.
Easy fix: before the initial set_pos():

self.somapos=[0,0,0]

What do you think about it?

@espenhgn
Copy link
Collaborator

I think that should work for now. Thanks for your help!

@espenhgn espenhgn merged commit f382c25 into LFPy:master Jan 11, 2017
@espenhgn espenhgn mentioned this pull request Jan 11, 2017
torbjone pushed a commit to torbjone/LFPy that referenced this pull request Jan 13, 2017
Added celsius parameter to cell class, and updated example2.py
@alejoe91 alejoe91 deleted the set_pos_3d branch January 20, 2017 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants