Blame LFPy-2.0.7/LFPy/test/test_cell.py

7d68d07
# -*- coding: utf-8 -*-
7d68d07
7d68d07
"""Copyright (C) 2012 Computational Neuroscience Group, NMBU.
7d68d07
7d68d07
This program is free software: you can redistribute it and/or modify
7d68d07
it under the terms of the GNU General Public License as published by
7d68d07
the Free Software Foundation, either version 3 of the License, or
7d68d07
(at your option) any later version.
7d68d07
7d68d07
This program is distributed in the hope that it will be useful,
7d68d07
but WITHOUT ANY WARRANTY; without even the implied warranty of
7d68d07
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7d68d07
GNU General Public License for more details.
7d68d07
7d68d07
"""
7d68d07
7d68d07
from __future__ import division
7d68d07
import os
7d68d07
import posixpath
7d68d07
import sys
7d68d07
import unittest
7d68d07
import numpy as np
7d68d07
import LFPy
7d68d07
import neuron
7d68d07
import pickle
7d68d07
import random
7d68d07
7d68d07
# for nosetests to run load mechanisms
7d68d07
if "win32" in sys.platform:
7d68d07
    pth = os.path.join(LFPy.__path__[0], 'test', 'nrnmech.dll')
7d68d07
    pth = pth.replace(os.sep, posixpath.sep)
7d68d07
    if not pth in neuron.nrn_dll_loaded:
7d68d07
        neuron.h.nrn_load_dll(pth)
7d68d07
        neuron.nrn_dll_loaded.append(pth)
7d68d07
else:
7d68d07
    neuron.load_mechanisms(os.path.join(LFPy.__path__[0], 'test'))
7d68d07
7d68d07
class testCell(unittest.TestCase):
7d68d07
    """
7d68d07
    test class LFPy.Cell
7d68d07
    """
7d68d07
7d68d07
    def test_cell_tvec_00(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 2**-3,
7d68d07
            'tstart' : 0.,
7d68d07
            'tstop' : 100.,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_01(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 2**-3,
7d68d07
            'tstart' : 0.,
7d68d07
            'tstop' : 10000.,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_02(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.1,
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_03(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.1,
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 10000,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_04(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.2,
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_05(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.2,
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 10000,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_06(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 2**-3,
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 100,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_07(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 2**-3,
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 10000,
7d68d07
        }
7d68d07
7d68d07
        tvec = stickSimulationTesttvec(**stickParams)
7d68d07
        tvec_numpy = np.linspace(0, stickParams['tstop'],
7d68d07
                    int(stickParams['tstop']/stickParams['dt']) + 1)
7d68d07
7d68d07
        np.testing.assert_equal(tvec, tvec_numpy)
7d68d07
7d68d07
    def test_cell_tvec_08(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.1,
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 10000,
7d68d07
        }
7d68d07
7d68d07
        try:
7d68d07
            stickSimulationTesttvec(**stickParams)
7d68d07
        except AssertionError:
7d68d07
            pass
7d68d07
7d68d07
    def test_cell_tvec_09(self):
7d68d07
        stickParams = {
7d68d07
            'dt' : 0.2,
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 10000,
7d68d07
        }
7d68d07
7d68d07
        try:
7d68d07
            stickSimulationTesttvec(**stickParams)
7d68d07
        except AssertionError:
7d68d07
            pass
7d68d07
7d68d07
    def test_cell_set_pos_00(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        np.testing.assert_allclose(cell.somapos, [0, 0, 0])
7d68d07
7d68d07
    def test_cell_set_pos_01(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        np.testing.assert_allclose(cell.somapos, [10., 20., -30.])
7d68d07
7d68d07
    def test_cell_set_pos_02(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                          pt3d=True)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        np.testing.assert_allclose(cell.somapos, [10., 20., -30.])
7d68d07
7d68d07
    def test_cell_set_pos_03(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        np.testing.assert_allclose(cell.somapos, [10., 20., -30.])
7d68d07
7d68d07
    def test_cell_set_pos_04(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                          pt3d=True)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        cell.set_pos(10., 20., -30.)
7d68d07
        np.testing.assert_allclose(cell.somapos, [10., 20., -30.])
7d68d07
7d68d07
7d68d07
    def test_cell_set_pos_05(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        np.testing.assert_allclose(cell.somapos,
7d68d07
                                   [cell.xmid[0], cell.ymid[0], cell.zmid[0]])
7d68d07
7d68d07
7d68d07
    def test_cell_set_pos_06(self):
7d68d07
        '''test LFPy.Cell.set_pos'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         pt3d=True)
7d68d07
        np.testing.assert_allclose(cell.somapos,
7d68d07
                                   [cell.xmid[0], cell.ymid[0], cell.zmid[0]])
7d68d07
7d68d07
7d68d07
    def test_cell_set_rotation_00(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around x-axis
7d68d07
        cell.set_rotation(x=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.ystart, -ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, -ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, -yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, -zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, -zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, -zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_set_rotation_01(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around y-axis
7d68d07
        cell.set_rotation(y=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, -xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, -xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, -xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, -zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, -zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, -zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_set_rotation_02(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        # test rotation 180 deg around z-axis
7d68d07
        cell.set_rotation(z=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, -xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, -xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, -xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, -ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, -ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, -yends, atol=1e-07)
7d68d07
7d68d07
    def test_cell_set_rotation_03(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         pt3d=True)
7d68d07
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around x-axis
7d68d07
        cell.set_rotation(x=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.ystart, -ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, -ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, -yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, -zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, -zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, -zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_set_rotation_04(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         pt3d=True)
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around y-axis
7d68d07
        cell.set_rotation(y=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, -xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, -xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, -xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, -zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, -zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, -zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_set_rotation_05(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         pt3d=True)
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        # test rotation 180 deg around z-axis
7d68d07
        cell.set_rotation(z=np.pi)
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, -xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, -xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, -xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, -ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, -ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, -yends, atol=1e-07)
7d68d07
7d68d07
    def test_cell_set_rotation_06(self):
7d68d07
        '''test LFPy.Cell.set_rotation()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'))
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation: 90 deg around x-axis, 90 deg around y-axis, 90 deg around z-axis
7d68d07
        cell.set_rotation(x=np.pi / 2., y=np.pi, z=np.pi / 4.)
7d68d07
        # revert rotation: -90 deg around x-axis, -90 deg around y-axis, -90 deg around z-axis, rotation_order='zyx'
7d68d07
        cell.set_rotation(x=-np.pi / 2., y=-np.pi, z=-np.pi / 4., rotation_order='zyx')
7d68d07
        # assert that x-, y- and z-coordinates are same as beginning, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, zends, atol=1e-07)
7d68d07
7d68d07
    def test_cell_chiral_morphology_00(self):
7d68d07
        '''test LFPy.Cell.chiral_morphology()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around x-axis
7d68d07
        cell.chiral_morphology(axis='x')
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, -xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, -xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, -xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_chiral_morphology_01(self):
7d68d07
        '''test LFPy.Cell.chiral_morphology()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around y-axis
7d68d07
        cell.chiral_morphology(axis='y')
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, -ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, -ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, -yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, zends, atol=1e-07)
7d68d07
7d68d07
    def test_cell_chiral_morphology_02(self):
7d68d07
        '''test LFPy.Cell.chiral_morphology()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        xstarts = cell.xstart.copy()
7d68d07
        xmids = cell.xmid.copy()
7d68d07
        xends = cell.xend.copy()
7d68d07
        ystarts = cell.ystart.copy()
7d68d07
        ymids = cell.ymid.copy()
7d68d07
        yends = cell.yend.copy()
7d68d07
        zstarts = cell.zstart.copy()
7d68d07
        zmids = cell.zmid.copy()
7d68d07
        zends = cell.zend.copy()
7d68d07
        # test rotation 180 deg around z-axis
7d68d07
        cell.chiral_morphology(axis='z')
7d68d07
        # assert that y- and z-coordinates are inverted, using absolute
7d68d07
        # tolerances
7d68d07
        np.testing.assert_allclose(cell.xstart, xstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xmid, xmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.xend, xends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ystart, ystarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.ymid, ymids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.yend, yends, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zstart, -zstarts, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zmid, -zmids, atol=1e-07)
7d68d07
        np.testing.assert_allclose(cell.zend, -zends, atol=1e-07)
7d68d07
7d68d07
7d68d07
    def test_cell_get_rand_prob_area_norm_00(self):
7d68d07
        '''test LFPy.Cell.get_rand_prob_area_norm()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                 'ball_and_sticks.hoc' ))
7d68d07
        p = cell.get_rand_prob_area_norm()
7d68d07
        self.assertAlmostEqual(p.sum(), 1.)
7d68d07
        self.assertTrue(p.min() >= 0.)
7d68d07
        self.assertTrue(p.max() <= 1.)
7d68d07
7d68d07
7d68d07
    def test_cell_get_rand_prob_area_norm_from_idx(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                 'ball_and_sticks.hoc' ))
7d68d07
        p = cell.get_rand_prob_area_norm_from_idx(idx=cell.get_idx(section='allsec'))
7d68d07
        self.assertListEqual(cell.get_rand_prob_area_norm().tolist(), p.tolist())
7d68d07
7d68d07
7d68d07
    def test_cell_get_rand_prob_area_norm_from_idx_00(self):
7d68d07
        '''test LFPy.Cell.get_rand_prob_area_norm()'''
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        p = cell.get_rand_prob_area_norm_from_idx(idx=np.array([0]))
7d68d07
        np.testing.assert_equal(p, np.array([1.]))
7d68d07
7d68d07
7d68d07
    def test_cell_get_intersegment_vector_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        idx0 = 0
7d68d07
        idx1 = 1
7d68d07
        vector = cell.get_intersegment_vector(idx0=idx0, idx1=idx1)
7d68d07
7d68d07
        self.assertListEqual(vector,
7d68d07
                            [cell.xmid[idx1] - cell.xmid[idx0],
7d68d07
                             cell.ymid[idx1] - cell.ymid[idx0],
7d68d07
                             cell.zmid[idx1] - cell.zmid[idx0]])
7d68d07
7d68d07
7d68d07
    def test_cell_get_intersegment_distance_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        idx0 = 0
7d68d07
        idx1 = 1
7d68d07
        distance = cell.get_intersegment_distance(idx0=idx0, idx1=idx1)
7d68d07
        vector = cell.get_intersegment_vector(idx0=idx0, idx1=idx1)
7d68d07
7d68d07
        self.assertEqual(np.sqrt(np.array(vector)**2).sum(), distance)
7d68d07
7d68d07
7d68d07
    def test_cell_get_idx_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         nsegs_method=None)
7d68d07
        self.assertListEqual(cell.get_idx(section='soma').tolist(), [0])
7d68d07
        self.assertListEqual(cell.get_idx(section='soma[0]').tolist(), [0])
7d68d07
        self.assertListEqual(cell.get_idx(section='dend[0]').tolist(), [1])
7d68d07
        self.assertListEqual(cell.get_idx(section='dend[1]').tolist(), [2])
7d68d07
        self.assertListEqual(cell.get_idx(section='dend[2]').tolist(), [3])
7d68d07
        self.assertListEqual(cell.get_idx(section='dend').tolist(), [1, 2, 3])
7d68d07
        self.assertListEqual(cell.get_idx(section='allsec').tolist(),
7d68d07
                             [0, 1, 2, 3])
7d68d07
        self.assertListEqual(cell.get_idx(section=['soma', 'dend']).tolist(),
7d68d07
                             [0, 1, 2, 3])
7d68d07
        self.assertListEqual(cell.get_idx(section='apic').tolist(), [])
7d68d07
7d68d07
7d68d07
    def test_cell_get_closest_idx_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ),
7d68d07
                         nsegs_method=None)
7d68d07
        self.assertEqual(cell.get_closest_idx(x=0, y=0, z=0),
7d68d07
                             cell.get_idx(section='soma')[0])
7d68d07
7d68d07
        self.assertEqual(cell.get_closest_idx(x=-25, y=0, z=175),
7d68d07
                             cell.get_idx(section='dend[1]')[0])
7d68d07
7d68d07
    def test_cell_get_closest_idx_01(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        x = -41.7
7d68d07
        z = 156.7
7d68d07
        sec_name = "dend"
7d68d07
7d68d07
        idx1 = cell.get_closest_idx(x=x, z=z)
7d68d07
        idx2 = cell.get_closest_idx(x=x, z=z, section=sec_name)
7d68d07
        self.assertEqual(idx1, idx2)
7d68d07
7d68d07
7d68d07
    def test_cell_get_idx_children_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
7d68d07
        np.testing.assert_array_equal(cell.get_idx_children(parent='soma[0]'),
7d68d07
                                      cell.get_idx(section='dend[0]'))
7d68d07
7d68d07
7d68d07
    def test_cell_get_idx_parent_children_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        np.testing.assert_array_equal(cell.get_idx_parent_children(parent='soma[0]'),
7d68d07
                                      cell.get_idx(section=['soma[0]',
7d68d07
                                                            'dend[0]']))
7d68d07
7d68d07
7d68d07
    def test_cell_get_idx_name_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        np.testing.assert_array_equal(cell.get_idx_name(idx=np.array([0])),
7d68d07
                                                np.array([[0, 'soma[0]', 0.5]],
7d68d07
                                                         dtype=object))
7d68d07
7d68d07
7d68d07
    def test_cell_get_rand_idx_area_norm_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        idx = cell.get_rand_idx_area_norm(nidx=1000000)
7d68d07
7d68d07
7d68d07
        # compute histogram and correlate with segment area
7d68d07
        bins = np.arange(cell.totnsegs+1)
7d68d07
        hist, bin_edges = np.histogram(idx, bins=bins)
7d68d07
7d68d07
        # compute Pearson correlation coefficients between area and histogram
7d68d07
        # reporting success if within 4 decimal places
7d68d07
        self.assertAlmostEqual(np.corrcoef(cell.area, hist)[0, 1], 1., places=4)
7d68d07
7d68d07
        # check if min and max is in the range of segment indices
7d68d07
        self.assertEqual(idx.min(), 0)
7d68d07
        self.assertEqual(idx.max(), cell.totnsegs-1)
7d68d07
7d68d07
7d68d07
    def test_cell_set_synapse_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell.set_synapse(idx=0, syntype='ExpSyn', record_curret=False,
7d68d07
                         record_potential=False, weight=1.,
7d68d07
                         **dict(e=10., tau=2.))
7d68d07
7d68d07
        self.assertTrue('ExpSyn' in cell.synlist[0].hname())
7d68d07
        self.assertEqual(len(cell.synlist), 1)
7d68d07
        self.assertEqual(len(cell.netconlist), 1)
7d68d07
        self.assertEqual(len(cell.netstimlist), 1)
7d68d07
        self.assertEqual(cell.synlist[0].e, 10.)
7d68d07
        self.assertEqual(cell.synlist[0].tau, 2.)
7d68d07
        self.assertEqual(cell.netconlist[0].weight[0], 1.)
7d68d07
7d68d07
7d68d07
    def test_cell_set_point_process_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell.set_point_process(idx=0, pptype='IClamp', record_current=False,
7d68d07
                               **dict(delay=1., amp=1.))
7d68d07
        self.assertEqual(cell.stimlist[0].hname(), 'IClamp[0]')
7d68d07
        self.assertEqual(len(cell.stimlist), 1)
7d68d07
        self.assertEqual(cell.stimlist[0].delay, 1.)
7d68d07
        self.assertEqual(cell.stimlist[0].amp, 1.)
7d68d07
7d68d07
7d68d07
    def test_cell_strip_hoc_objects_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell.strip_hoc_objects()
7d68d07
        for attribute in dir(cell):
7d68d07
            self.assertNotEqual(str(type(getattr(cell, attribute))),
7d68d07
                                'hoc.HocObject')
7d68d07
7d68d07
    def test_cell_cellpickler_00(self):
7d68d07
        cell = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                  'ball_and_sticks.hoc' ))
7d68d07
        cell_pickle = cell.cellpickler(filename=None, pickler=pickle.dumps)
7d68d07
        pickled_cell = pickle.loads(cell_pickle)
7d68d07
7d68d07
        for attribute in dir(cell):
7d68d07
            if attribute.startswith('__') or attribute.startswith('_'):
7d68d07
                pass
7d68d07
            else:
7d68d07
                self.assertEqual(type(getattr(cell, attribute)),
7d68d07
                                 type(getattr(pickled_cell, attribute)))
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_00(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in single dend.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend2.connect(dend1(1.), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0], cell.imem[0], rtol=1E-5)
7d68d07
        np.testing.assert_almost_equal(iaxial[1], cell.imem[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[1], cell.imem[1], rtol=1E-5)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_01(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when single dend connected to soma mid.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend = neuron.h.Section(name='dend')
7d68d07
        dend.connect(soma(0.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_almost_equal(-iaxial[1], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_allclose(-iaxial[1], cell.imem[0], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_02(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when single dend connected to soma end.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend = neuron.h.Section(name='dend')
7d68d07
        dend.connect(soma(1.0), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[1], cell.imem[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[1], cell.imem[1], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[0], iaxial[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0], iaxial[1], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_03(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when single dend connected to random soma point.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend = neuron.h.Section(name='dend')
7d68d07
        dend.connect(soma(random.uniform(1e-2, 1.)), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[1], cell.imem[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[1], cell.imem[1], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[0], iaxial[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0], iaxial[1], rtol=1E-4)
7d68d07
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_04(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when two dends connected to soma mid.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend1.connect(soma(0.5), 0)
7d68d07
        dend2.connect(soma(0.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[1]-iaxial[3], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[1]-iaxial[3], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[0], iaxial[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0], iaxial[1], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[2], iaxial[3], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[2], iaxial[3], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_05(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when two dends connected to soma end.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend1.connect(soma(1.), 0)
7d68d07
        dend2.connect(soma(1.), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0]-iaxial[2], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0]-iaxial[2], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[0]+iaxial[2], iaxial[1] + iaxial[3], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0]+iaxial[2], iaxial[1] + iaxial[3], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_06(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when two dends connected to diff soma points.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(soma(.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0]-iaxial[2], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0]-iaxial[2], cell.imem[0], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[0], iaxial[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0], iaxial[1], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(iaxial[2], iaxial[3], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[2], iaxial[3], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_07(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in mid dend when two dends connected to dend.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(dend1(.5), 0)
7d68d07
        dend3.connect(dend1(1.), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(iaxial[0]+iaxial[4], iaxial[1] + iaxial[5], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0]+iaxial[4], iaxial[1] + iaxial[5], rtol=1E-4)
7d68d07
        np.testing.assert_almost_equal(-iaxial[1]+iaxial[2]+iaxial[4], -cell.imem[1], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[1]+iaxial[2]+iaxial[4], -cell.imem[1], rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_08(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when three dends connected to soma.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(soma(.5), 0)
7d68d07
        dend3.connect(soma(.8), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0]-iaxial[2]-iaxial[4], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0]-iaxial[2]-iaxial[4], cell.imem[0], rtol=1E-3)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_09(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in 2-comp model where dend 0 is connected to soma 0.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend1.connect(soma(0.0), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(iaxial[0], -cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_almost_equal(iaxial[0], cell.imem[1], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[0], -cell.imem[0], rtol=1E-3)
7d68d07
        np.testing.assert_allclose(iaxial[0], cell.imem[1], rtol=1E-3)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_10(self):
7d68d07
        '''
7d68d07
        Check that len(iaxial) = (cell.totnsegs - 1)*2
7d68d07
        '''
7d68d07
        soma = neuron.h.Section(name='soma[0]')
7d68d07
        dend1 = neuron.h.Section(name='dend1[0]')
7d68d07
        dend2 = neuron.h.Section(name='dend2[0]')
7d68d07
        dend3 = neuron.h.Section(name='dend3[0]')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(soma(.5), 0)
7d68d07
        dend3.connect(soma(0.8), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        self.assertEqual(iaxial.shape[0], (cell.totnsegs - 1)*2)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_11(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when three dends connected to soma mid.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(0.5), 0)
7d68d07
        dend2.connect(soma(0.5), 0)
7d68d07
        dend3.connect(soma(0.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-iaxial[0]-iaxial[2]-iaxial[4], cell.imem[0], decimal=9)
7d68d07
        np.testing.assert_allclose(-iaxial[0]-iaxial[2]-iaxial[4], cell.imem[0], rtol=1E-3)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_12(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in morph where secs are connected to arc length 0.5.
7d68d07
        '''
7d68d07
        morphology = os.path.join(LFPy.__path__[0], 'test', 'sticks_not_connected_head_to_toe.hoc')
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(iaxial[6]+iaxial[10]+cell.imem[3], iaxial[5], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[6]+iaxial[10]+cell.imem[3], iaxial[5], rtol=1E-5)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_13(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in morph where secs are connected to arc length 0.7.
7d68d07
        '''
7d68d07
        morphology = os.path.join(LFPy.__path__[0], 'test', 'sticks_not_connected_head_to_toe.hoc')
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(iaxial[8]+iaxial[20]+cell.imem[4], iaxial[7], decimal=9)
7d68d07
        np.testing.assert_allclose(iaxial[8]+iaxial[20]+cell.imem[4], iaxial[7], rtol=1E-5)
7d68d07
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_14(self):
7d68d07
        '''
7d68d07
        Check iaxial current mid positions in three-section stick.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend1.connect(soma(1), 0)
7d68d07
        dend2.connect(soma(1), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree(sec=soma)
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        new_x = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
7d68d07
        new_y = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
7d68d07
        new_z = [[-10, 0, 10], [10, 15, 20], [20, 30, 50]]
7d68d07
        new_d = [[20, 20, 20], [10, 10, 10], [5, 5, 5]]
7d68d07
        for j, sec in enumerate(neuron.h.allsec()):
7d68d07
            for n in range(3):
7d68d07
                neuron.h.pt3dchange(n,
7d68d07
                                new_x[j][n],
7d68d07
                                new_y[j][n],
7d68d07
                                new_z[j][n],
7d68d07
                                new_d[j][n])
7d68d07
                neuron.h.define_shape()
7d68d07
        cell._collect_geometry()
7d68d07
        cell2 = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial2, d_list2, pos_list2 = cell2.get_axial_currents_from_vmem()
7d68d07
        mid_current_positions = np.array([[0., 0., 5], [0., 0., 20], [0., 0., 5.], [0., 0., 12.5]])
7d68d07
        np.testing.assert_almost_equal(mid_current_positions, pos_list2, decimal=9)
7d68d07
        np.testing.assert_allclose(mid_current_positions, pos_list2, rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_15(self):
7d68d07
        '''
7d68d07
        Check iaxial current mid positions in ball-n-y.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(dend1(1.), 0)
7d68d07
        dend3.connect(dend1(.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree(sec=soma)
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial1, d_list1, pos_list1 = cell.get_axial_currents_from_vmem()
7d68d07
        new_x = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 5, 10]]
7d68d07
        new_y = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
7d68d07
        new_z = [[-10, 0, 10], [10, 15, 20], [20, 30, 40], [15, 15, 15]]
7d68d07
        new_d = [[20, 20, 20], [10, 10, 10], [5, 5, 5], [2, 2, 2]]
7d68d07
        for j, sec in enumerate(neuron.h.allsec()):
7d68d07
            for n in range(3):
7d68d07
                neuron.h.pt3dchange(n,
7d68d07
                                new_x[j][n],
7d68d07
                                new_y[j][n],
7d68d07
                                new_z[j][n],
7d68d07
                                new_d[j][n])
7d68d07
                neuron.h.define_shape()
7d68d07
        cell._collect_geometry()
7d68d07
        cell2 = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial2, d_list2, pos_list2 = cell2.get_axial_currents_from_vmem()
7d68d07
        mid_current_positions = np.array([[0., 0., 5.], [0., 0., 12.5],
7d68d07
                                          [0., 0., 15.], [2.5, 0., 15.],
7d68d07
                                          [0., 0., 17.5], [0, 0., 25.]])
7d68d07
        np.testing.assert_almost_equal(mid_current_positions, pos_list2, decimal=9)
7d68d07
        np.testing.assert_allclose(mid_current_positions, pos_list2, rtol=1E-4)
7d68d07
7d68d07
    def test_cell_get_axial_currents_from_vmem_16(self):
7d68d07
        '''
7d68d07
        Check Kirchhoff in soma when three dends connected to soma end.
7d68d07
        '''
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(1.0), 0)
7d68d07
        dend2.connect(soma(1.0), 0)
7d68d07
        dend3.connect(soma(1.0), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        iaxial, d_list, pos_list = cell.get_axial_currents_from_vmem()
7d68d07
        np.testing.assert_almost_equal(-cell.imem[0], iaxial[1] + iaxial[3] + iaxial[5], decimal=9)
7d68d07
        np.testing.assert_allclose(-cell.imem[0], iaxial[1] + iaxial[3] + iaxial[5], rtol=1E-3)
7d68d07
7d68d07
    def test_cell_simulate_recorder_00(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
7d68d07
    def test_cell_simulate_recorder_01(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 0.1,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
    def test_cell_simulate_recorder_02(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 0.2,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
    def test_cell_simulate_recorder_03(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 10000,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
7d68d07
    def test_cell_simulate_recorder_04(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 10000,
7d68d07
            'dt' : 0.1,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
    def test_cell_simulate_recorder_05(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 10000,
7d68d07
            'dt' : 0.2,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        stick.simulate(rec_vmem=True, rec_imem=True,
7d68d07
                       rec_current_dipole_moment=True)
7d68d07
        self.assertTrue(stick.tvec.size ==
7d68d07
                        stick.vmem.shape[1] ==
7d68d07
                        stick.imem.shape[1] ==
7d68d07
                        stick.current_dipole_moment.shape[0])
7d68d07
        self.assertTrue(np.all(stick.vmem == stick.v_init))
7d68d07
        self.assertTrue(np.all(stick.imem == 0.))
7d68d07
        self.assertTrue(np.all(stick.current_dipole_moment == 0.))
7d68d07
7d68d07
7d68d07
    def test_cell_simulate_current_dipole_moment_00(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 0.1,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
        for idx in range(31): #31 segments
7d68d07
            if idx != 15: # no net dipole moment because of stick symmetry
7d68d07
                stick = LFPy.Cell(**stickParams)
7d68d07
                synapse = LFPy.StimIntElectrode(stick, idx=idx,
7d68d07
                                       **stimParams)
7d68d07
                stick.simulate(rec_imem=True, rec_current_dipole_moment=True)
7d68d07
                p = np.dot(stick.imem.T, np.c_[stick.xmid, stick.ymid, stick.zmid])
7d68d07
                np.testing.assert_allclose(p, stick.current_dipole_moment)
7d68d07
7d68d07
7d68d07
    def test_cell_simulate_current_dipole_moment_01(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        for idx in range(31): #31 segments
7d68d07
            if idx != 15: # no net dipole moment because of stick symmetry
7d68d07
                stick = LFPy.Cell(**stickParams)
7d68d07
                synapse = LFPy.StimIntElectrode(stick, idx=idx,
7d68d07
                                       **stimParams)
7d68d07
                stick.simulate(rec_imem=True, rec_current_dipole_moment=True)
7d68d07
                p = np.dot(stick.imem.T, np.c_[stick.xmid, stick.ymid, stick.zmid])
7d68d07
                np.testing.assert_allclose(p, stick.current_dipole_moment)
7d68d07
7d68d07
    def test_cell_simulate_current_dipole_moment_02(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'e' : 0,                                # reversal potential
7d68d07
            'syntype' : 'Exp2Syn',                   # synapse type
7d68d07
            'tau1' : 0.1,                              # syn. time constant
7d68d07
            'tau2' : 2.,                              # syn. time constant
7d68d07
            'weight' : 0.01,
7d68d07
        }
7d68d07
7d68d07
        for idx in range(31): #31 segments
7d68d07
            if idx != 15: # no net dipole moment because of stick symmetry
7d68d07
                stick = LFPy.Cell(**stickParams)
7d68d07
                synapse = LFPy.Synapse(stick, idx=idx,
7d68d07
                                       **stimParams)
7d68d07
                synapse.set_spike_times(np.array([10., 20., 30., 40., 50.]))
7d68d07
                stick.simulate(rec_imem=True, rec_current_dipole_moment=True)
7d68d07
                p = np.dot(stick.imem.T, np.c_[stick.xmid, stick.ymid, stick.zmid])
7d68d07
                np.testing.assert_allclose(p, stick.current_dipole_moment)
7d68d07
7d68d07
    def test_cell_tstart_00(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        stick0 = LFPy.Cell(tstart=0, tstop=200, **stickParams)
7d68d07
        synapse0 = LFPy.StimIntElectrode(stick0,
7d68d07
                                         stick0.get_closest_idx(0, 0, 1000),
7d68d07
                                         delay=0, phase=0.,
7d68d07
                                         **stimParams)
7d68d07
        stick0.simulate(rec_imem=True, rec_vmem=True, rec_current_dipole_moment=True)
7d68d07
7d68d07
7d68d07
        stick1 = LFPy.Cell(tstart=-100, tstop=100, **stickParams)
7d68d07
        synapse1 = LFPy.StimIntElectrode(stick1,
7d68d07
                                         stick1.get_closest_idx(0, 0, 1000),
7d68d07
                                         delay=-100, phase=0.,
7d68d07
                                         **stimParams)
7d68d07
        stick1.simulate(rec_imem=True, rec_vmem=True, rec_current_dipole_moment=True)
7d68d07
7d68d07
        inds = stick0.tvec >= 100
7d68d07
        np.testing.assert_allclose(stick0.vmem[:, inds], stick1.vmem)
7d68d07
        np.testing.assert_allclose(stick0.imem[:, inds], stick1.imem)
7d68d07
        np.testing.assert_allclose(stick0.current_dipole_moment[inds, :],
7d68d07
                                   stick1.current_dipole_moment)
7d68d07
7d68d07
7d68d07
    def test_cell_with_recextelectrode_00(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
7d68d07
        }
7d68d07
7d68d07
        electrodeParams = {
7d68d07
            'sigma' : 0.3,
7d68d07
            'x' : np.ones(11) * 100.,
7d68d07
            'y' : np.zeros(11),
7d68d07
            'z' : np.linspace(1000, 0, 11),
7d68d07
            'method' : 'pointsource'
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        synapse = LFPy.StimIntElectrode(stick, stick.get_closest_idx(0, 0, 1000),
7d68d07
                               **stimParams)
7d68d07
        electrode = LFPy.RecExtElectrode(**electrodeParams)
7d68d07
        stick.simulate(electrode, rec_imem=True)
7d68d07
7d68d07
        electrode1 = LFPy.RecExtElectrode(cell=stick, **electrodeParams)
7d68d07
        electrode1.calc_lfp()
7d68d07
7d68d07
        np.testing.assert_allclose(electrode.LFP, electrode1.LFP)
7d68d07
        self.assertTrue(stick.tvec.size == stick.imem.shape[1] ==
7d68d07
                        electrode.LFP.shape[1] == electrode1.LFP.shape[1] ==
7d68d07
                        int(stick.tstop/stick.dt)+1)
7d68d07
7d68d07
7d68d07
    def test_cell_with_recextelectrode_01(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : -100,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 2**-4,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
7d68d07
        }
7d68d07
7d68d07
        electrodeParams = {
7d68d07
            'sigma' : 0.3,
7d68d07
            'x' : np.ones(11) * 100.,
7d68d07
            'y' : np.zeros(11),
7d68d07
            'z' : np.linspace(1000, 0, 11),
7d68d07
            'method' : 'pointsource'
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        synapse = LFPy.StimIntElectrode(stick, stick.get_closest_idx(0, 0, 1000),
7d68d07
                               **stimParams)
7d68d07
        electrode = LFPy.RecExtElectrode(**electrodeParams)
7d68d07
        stick.simulate(electrode, rec_imem=True)
7d68d07
7d68d07
        electrode1 = LFPy.RecExtElectrode(cell=stick, **electrodeParams)
7d68d07
        electrode1.calc_lfp()
7d68d07
7d68d07
        np.testing.assert_allclose(electrode.LFP, electrode1.LFP)
7d68d07
        self.assertTrue(stick.tvec.size == stick.imem.shape[1] ==
7d68d07
                        electrode.LFP.shape[1] == electrode1.LFP.shape[1] ==
7d68d07
                        int(stick.tstop/stick.dt)+1)
7d68d07
7d68d07
    def test_cell_with_recextelectrode_02(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 0.1,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
7d68d07
        }
7d68d07
7d68d07
        electrodeParams = {
7d68d07
            'sigma' : 0.3,
7d68d07
            'x' : np.ones(11) * 100.,
7d68d07
            'y' : np.zeros(11),
7d68d07
            'z' : np.linspace(1000, 0, 11),
7d68d07
            'method' : 'pointsource'
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        synapse = LFPy.StimIntElectrode(stick, stick.get_closest_idx(0, 0, 1000),
7d68d07
                               **stimParams)
7d68d07
        electrode = LFPy.RecExtElectrode(**electrodeParams)
7d68d07
        stick.simulate(electrode, rec_imem=True)
7d68d07
7d68d07
        electrode1 = LFPy.RecExtElectrode(cell=stick, **electrodeParams)
7d68d07
        electrode1.calc_lfp()
7d68d07
7d68d07
        np.testing.assert_allclose(electrode.LFP, electrode1.LFP)
7d68d07
        self.assertTrue(stick.tvec.size == stick.imem.shape[1] ==
7d68d07
                        electrode.LFP.shape[1] == electrode1.LFP.shape[1] ==
7d68d07
                        int(stick.tstop/stick.dt)+1)
7d68d07
7d68d07
    def test_cell_with_recextelectrode_03(self):
7d68d07
        stickParams = {
7d68d07
            'morphology' : os.path.join(LFPy.__path__[0], 'test', 'stick.hoc'),
7d68d07
            'cm' : 1,
7d68d07
            'Ra' : 150,
7d68d07
            'v_init' : -65,
7d68d07
            'passive' : True,
7d68d07
            'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
            'tstart' : 0,
7d68d07
            'tstop' : 100,
7d68d07
            'dt' : 0.2,
7d68d07
            'nsegs_method' : 'lambda_f',
7d68d07
            'lambda_f' : 100,
7d68d07
7d68d07
        }
7d68d07
7d68d07
        electrodeParams = {
7d68d07
            'sigma' : 0.3,
7d68d07
            'x' : np.ones(11) * 100.,
7d68d07
            'y' : np.zeros(11),
7d68d07
            'z' : np.linspace(1000, 0, 11),
7d68d07
            'method' : 'pointsource'
7d68d07
        }
7d68d07
7d68d07
        stimParams = {
7d68d07
            'pptype' : 'SinSyn',
7d68d07
            'delay' : 0.,
7d68d07
            'dur' : 1000.,
7d68d07
            'pkamp' : 1.,
7d68d07
            'freq' : 100.,
7d68d07
            'phase' : 0,
7d68d07
            'bias' : 0.,
7d68d07
            'record_current' : False
7d68d07
        }
7d68d07
7d68d07
        stick = LFPy.Cell(**stickParams)
7d68d07
        synapse = LFPy.StimIntElectrode(stick, stick.get_closest_idx(0, 0, 1000),
7d68d07
                               **stimParams)
7d68d07
        electrode = LFPy.RecExtElectrode(**electrodeParams)
7d68d07
        stick.simulate(electrode, rec_imem=True)
7d68d07
7d68d07
        electrode1 = LFPy.RecExtElectrode(cell=stick, **electrodeParams)
7d68d07
        electrode1.calc_lfp()
7d68d07
7d68d07
        np.testing.assert_allclose(electrode.LFP, electrode1.LFP)
7d68d07
        self.assertTrue(stick.tvec.size == stick.imem.shape[1] ==
7d68d07
                        electrode.LFP.shape[1] == electrode1.LFP.shape[1] ==
7d68d07
                        int(stick.tstop/stick.dt)+1)
7d68d07
7d68d07
    def test_get_multi_current_dipole_moments00(self):
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend1.connect(soma(0.5), 0)
7d68d07
        dend2.connect(dend1(1.0), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        dipoles, dipole_locs = cell.get_multi_current_dipole_moments()
7d68d07
        t_point = -1
7d68d07
        P_from_multi_dipoles = np.sum(dipoles[:,t_point,:],axis=0)
7d68d07
        P = cell.current_dipole_moment[t_point]
7d68d07
        np.testing.assert_almost_equal(P, P_from_multi_dipoles)
7d68d07
        np.testing.assert_allclose(P, P_from_multi_dipoles, rtol=1E-5)
7d68d07
7d68d07
    def test_get_multi_current_dipole_moments01(self):
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend1.connect(soma(1.), 0)
7d68d07
        dend2.connect(soma(1.), 0)
7d68d07
        dend3.connect(soma(1.), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        dipoles, dipole_locs = cell.get_multi_current_dipole_moments()
7d68d07
        t_point = -1
7d68d07
        P_from_multi_dipoles = np.sum(dipoles[:,t_point,:],axis=0)
7d68d07
        P = cell.current_dipole_moment[t_point]
7d68d07
        np.testing.assert_almost_equal(P, P_from_multi_dipoles)
7d68d07
7d68d07
    def test_get_multi_current_dipole_moments02(self):
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend2.connect(dend1(1.), 0)
7d68d07
        dend3.connect(dend2(.5), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        dipoles, dipole_locs = cell.get_multi_current_dipole_moments()
7d68d07
        t_point = -1
7d68d07
        P_from_multi_dipoles = np.sum(dipoles[:,t_point,:],axis=0)
7d68d07
        P = cell.current_dipole_moment[t_point]
7d68d07
        np.testing.assert_almost_equal(P, P_from_multi_dipoles)
7d68d07
7d68d07
    def test_get_multi_current_dipole_moments03(self):
7d68d07
        neuron.h('forall delete_section()')
7d68d07
        soma = neuron.h.Section(name='soma')
7d68d07
        dend1 = neuron.h.Section(name='dend1')
7d68d07
        dend2 = neuron.h.Section(name='dend2')
7d68d07
        dend3 = neuron.h.Section(name='dend3')
7d68d07
        dend4 = neuron.h.Section(name='dend4')
7d68d07
        dend5 = neuron.h.Section(name='dend5')
7d68d07
        dend1.connect(soma(1.), 0)
7d68d07
        dend2.connect(soma(0.), 0)
7d68d07
        dend3.connect(soma(0.), 0)
7d68d07
        dend4.connect(soma(0.), 0)
7d68d07
        dend5.connect(soma(0.432), 0)
7d68d07
        morphology = neuron.h.SectionList()
7d68d07
        morphology.wholetree()
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        dipoles, dipole_locs = cell.get_multi_current_dipole_moments()
7d68d07
        t_point = -1
7d68d07
        P_from_multi_dipoles = np.sum(dipoles[:,t_point,:],axis=0)
7d68d07
        P = cell.current_dipole_moment[t_point]
7d68d07
        np.testing.assert_almost_equal(P, P_from_multi_dipoles)
7d68d07
7d68d07
    def test_get_multi_current_dipole_moments04(self):
7d68d07
        morphology = os.path.join(LFPy.__path__[0], 'test', 'ball_and_sticks.hoc')
7d68d07
        cell = cell_w_synapse_from_sections(morphology)
7d68d07
        dipoles, dipole_locs = cell.get_multi_current_dipole_moments()
7d68d07
        t_point = -1
7d68d07
        P_from_multi_dipoles = np.sum(dipoles[:,t_point,:],axis=0)
7d68d07
        P = cell.current_dipole_moment[t_point]
7d68d07
        np.testing.assert_almost_equal(P, P_from_multi_dipoles)
7d68d07
7d68d07
7d68d07
    def test_cell_distort_geometry_01(self):
7d68d07
        cell0 = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                          'ball_and_sticks.hoc' ))
7d68d07
        factors = [-0.2, 0.1, 0., 0.1, 0.2]
7d68d07
        nus = [-0.5, 0., 0.5]
7d68d07
        for factor in factors:
7d68d07
            for nu in nus:
7d68d07
                for axis in 'xyz':
7d68d07
                    cell1 = LFPy.Cell(morphology=os.path.join(LFPy.__path__[0],
7d68d07
                                                      'test',
7d68d07
                                                      'ball_and_sticks.hoc' ))
7d68d07
                    cell1.distort_geometry(factor=factor, nu=nu, axis=axis)
7d68d07
                    for attr in ['start', 'mid', 'end']:
7d68d07
                        for ax in 'xyz'.replace(axis, ''):
7d68d07
                            np.testing.assert_allclose(getattr(cell0, ax+attr)*(1+factor*nu),
7d68d07
                                                       getattr(cell1, ax+attr))
7d68d07
                        np.testing.assert_allclose(getattr(cell0, axis+attr)*(1-factor),
7d68d07
                                                   getattr(cell1, axis+attr))
7d68d07
7d68d07
    ######## Functions used by tests: ##########################################
7d68d07
def stickSimulationTesttvec(**kwargs):
7d68d07
    stick = LFPy.Cell(morphology = os.path.join(LFPy.__path__[0], 'test',
7d68d07
                                                'stick.hoc'), verbose=False,
7d68d07
                      **kwargs)
7d68d07
    stick.simulate(rec_imem=False)
7d68d07
    return stick.tvec
7d68d07
7d68d07
7d68d07
def cell_w_synapse_from_sections(morphology):
7d68d07
    '''
7d68d07
    Make cell and synapse objects, set spike, simulate and return cell
7d68d07
    '''
7d68d07
    cellParams = {
7d68d07
        'morphology': morphology,
7d68d07
        'cm' : 1,
7d68d07
        'Ra' : 150,
7d68d07
        'v_init' : -65,
7d68d07
        'passive' : True,
7d68d07
        'passive_parameters' : {'g_pas' : 1./30000, 'e_pas' : -65},
7d68d07
        'dt' : 2**-6,
7d68d07
        'tstart' : -50,
7d68d07
        'tstop' : 50,
7d68d07
        'delete_sections' : False
7d68d07
    }
7d68d07
7d68d07
    synapse_parameters = {'e': 0.,
7d68d07
                      'syntype': 'ExpSyn',
7d68d07
                      'tau': 5.,
7d68d07
                      'weight': .001,
7d68d07
                      'record_current': True,
7d68d07
                      'idx': 1}
7d68d07
7d68d07
    cell = LFPy.Cell(**cellParams)
7d68d07
    synapse = LFPy.Synapse(cell, **synapse_parameters)
7d68d07
    synapse.set_spike_times(np.array([1.]))
7d68d07
    cell.simulate(rec_imem=True, rec_vmem=True, rec_current_dipole_moment=True)
7d68d07
    return cell