Skip to content

Commit

Permalink
Fix bugs in diameter models
Browse files Browse the repository at this point in the history
Change-Id: Iab4454c9e6963bb3969171fd1116aa125b578c18
  • Loading branch information
lidakanari committed Feb 22, 2019
1 parent 20464b3 commit 2819a5e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 71 deletions.
19 changes: 12 additions & 7 deletions tests/test_extract_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ def test_trunk_distr():
assert_equal(trunkAP, target_trunkAPIC)

def test_diameter_extract():
assert_equal(extract_input.from_diameter.model(NEU),
{'basal': {'Rall_ratio': 0.6666666666666666,
'siblings_ratio': 1.0,
'taper': [0.24000000000000005, 0.1],
'term': [2.0, 2.0],
'trunk': [3.9],
'trunk_taper': [0.3000000000000001]}})
res = extract_input.from_diameter.model(NEU)
assert_equal(set(res.keys()), {'basal'})
expected = {'Rall_ratio': 1.5,
'siblings_ratio': 1.0,
'taper': [0.24, 0.1],
'term': [2.0, 2.0],
'trunk': [3.9],
'trunk_taper': [0.30]}

assert_equal(res['basal'].keys(), expected.keys())
for key in expected.keys():
assert_array_almost_equal(res['basal'][key], expected[key])


class NeuromJSON(json.JSONEncoder):
Expand Down
84 changes: 25 additions & 59 deletions tns/extract_input/from_diameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from neurom.morphmath import segment_length, segment_radius

from tns.morphio_utils import NEUROM_TYPE_TO_STR
from tns.utils import _check

default_model = {'Rall_ratio': 2. / 3.,
default_model = {'Rall_ratio': 3. / 2.,
'siblings_ratio': 1.}


Expand All @@ -26,7 +27,9 @@ def section_mean_taper(s):

def terminal_diam(tree):
"""Returns the model for the terminations"""
term_diam = [2. * t.points[-1, 3] for t in Tree.ileaf(next(tree.iter_sections()))]
mean_diam = np.mean(tree.points[:, 3])
term_diam = [2. * t.points[-1, 3] for t in Tree.ileaf(next(tree.iter_sections()))
if t.points[-1, 3] < 1.2 * mean_diam]

return term_diam

Expand All @@ -46,71 +49,34 @@ def section_trunk_taper(tree):
return section_mean_taper(next(tree.iter_sections()))


def model(neuron):
"""Measures the statistical properties of a neuron's
diameters and outputs a diameter_model"""

def model(input_object):
"""Measures the statistical properties of an input_object's
diameters and outputs a diameter_model
Input can be a population of neurons, or a single neuron.
"""
values = {}

for neurite_type in set(tree.type for tree in neuron.neurites):
taper = [section_taper(tree) for tree in iter_neurites(neuron,
filt=is_type(neurite_type))]
trunk_taper = np.array([section_trunk_taper(tree)
for tree in iter_neurites(neuron,
filt=is_type(neurite_type))])
for neurite_type in set(tree.type for tree in input_object.neurites):

neurites = list(iter_neurites(input_object, filt=is_type(neurite_type)))

taper = [section_taper(tree) for tree in neurites]
trunk_taper = np.array([section_trunk_taper(tree) for tree in neurites])
taper_c = np.array(list(chain(*taper)))
# Keep only positive, non-zero taper rates
taper_c = taper_c[np.where(taper_c > 0)[0]]
trunk_taper = trunk_taper[np.where(trunk_taper > 0.0)[0]]

term_diam = [terminal_diam(tree)
for tree in iter_neurites(neuron, filt=is_type(neurite_type))]
trunk_diam = [2. * np.max(get('segment_radii', tree))
for tree in iter_neurites(neuron, filt=is_type(neurite_type))]
taper_c = taper_c[np.where(taper_c > 0.00001)[0]]
trunk_taper = trunk_taper[np.where(trunk_taper >= 0.0)[0]]
term_diam = [terminal_diam(tree) for tree in neurites]
trunk_diam = [2. * np.max(get('segment_radii', tree)) for tree in neurites]

key = NEUROM_TYPE_TO_STR[neurite_type]

values[key] = {"taper": taper_c,
"term": [c for c in chain(*term_diam)],
"trunk": trunk_diam,
"trunk_taper": trunk_taper}
"term": [c for c in chain(*term_diam)],
"trunk": trunk_diam,
"trunk_taper": trunk_taper}

_check(values[key])
values[key].update(default_model)

return values


def get_taper(neurons, neurite_type):
'''get taper'''
return [section_taper(tree) for tree in iter_neurites(neurons, filt=is_type(neurite_type))]


def get_term_diam(neurons, neurite_type):
'''get term'''
return [terminal_diam(tree)
for tree in iter_neurites(neurons, filt=is_type(neurite_type))]


def population_model(neurons):
"""Measures the statistical properties of a neuron's
diameters and outputs a diameter_model"""

values = {}

types_to_process = {tree.type.value: tree.type for tree in neurons.neurites}

for typee in types_to_process:

neurite_type = types_to_process[typee]

values[typee - 1] = {
"taper": [c for c in chain(*get_taper(neurons, neurite_type))],
"term": [c for c in chain(*get_term_diam(neurons, neurite_type))],
"trunk": [
2. * np.max(get('segment_radii', tree))
for tree in iter_neurites(neurons, filt=is_type(neurite_type))
]
}

values[typee - 1].update(default_model)

return values
5 changes: 1 addition & 4 deletions tns/generate/soma.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from tns.morphmath.utils import norm

from tns.utils import TNSError

L = logging.getLogger()

Expand Down Expand Up @@ -103,9 +103,6 @@ def interpolate(self, points3D, interpolation=10):
"""
from scipy.spatial import ConvexHull # pylint: disable=no-name-in-module

class TNSError(Exception):
'''Raises TNS error'''

interpolation = np.max([3, interpolation]) # soma must have at least 3 points

if len(points3D) >= interpolation:
Expand Down
13 changes: 13 additions & 0 deletions tns/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""TNS utils used by multiple tools"""


class TNSError(Exception):
'''Raises TNS error'''


def _check(data):
"""Checks if data in dictionary are empty.
"""
for key, val in data.items():
if len(val) == 0:
raise TNSError('Empty distrib for diameter key: {}'.format(key))
2 changes: 1 addition & 1 deletion tns/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
""" tns version """
VERSION = "1.0.6"
VERSION = "1.0.7.dev0"

0 comments on commit 2819a5e

Please sign in to comment.