Skip to content

Commit

Permalink
Reconcile master and mut_morphio branch a tad more (#873)
Browse files Browse the repository at this point in the history
This commit does not touch the functionality.
It simply format the files (mainly test files) so that
these changes are no longer needed when using mut_morphio.
  • Loading branch information
Benoit Coste authored Mar 5, 2021
1 parent c9a222d commit 935a0d5
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 268 deletions.
2 changes: 1 addition & 1 deletion neurom/apps/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def generate_annotation(result, settings):

points = [p for _, _points in result.info for p in _points]

annotations = (" ({0} {1} {2} 0.50) ; MUK_ANNOTATION".format(
annotations = (" ({:10.2f} {:10.2f} {:10.2f} 0.50) ; MUK_ANNOTATION".format(
p[COLS.X], p[COLS.Y], p[COLS.Z]) for p in points)
footer = ") ; MUK_ANNOTATION\n"

Expand Down
12 changes: 6 additions & 6 deletions neurom/apps/tests/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def test_generate_annotation():
(circle ; MUK_ANNOTATION
(Color blue) ; MUK_ANNOTATION
(Name "dangling") ; MUK_ANNOTATION
(1 2 3 0.50) ; MUK_ANNOTATION
(4 5 6 0.50) ; MUK_ANNOTATION
(7 8 9 0.50) ; MUK_ANNOTATION
(10 11 12 0.50) ; MUK_ANNOTATION
( 1.00 2.00 3.00 0.50) ; MUK_ANNOTATION
( 4.00 5.00 6.00 0.50) ; MUK_ANNOTATION
( 7.00 8.00 9.00 0.50) ; MUK_ANNOTATION
( 10.00 11.00 12.00 0.50) ; MUK_ANNOTATION
) ; MUK_ANNOTATION
"""

Expand All @@ -35,8 +35,8 @@ def test_annotate():
(Circle1 ; MUK_ANNOTATION
(Color Blue) ; MUK_ANNOTATION
(Name "narrow start") ; MUK_ANNOTATION
(0.0 0.0 0.0 0.50) ; MUK_ANNOTATION
(0.0 0.0 0.0 0.50) ; MUK_ANNOTATION
( 0.00 0.00 0.00 0.50) ; MUK_ANNOTATION
( 0.00 0.00 0.00 0.50) ; MUK_ANNOTATION
) ; MUK_ANNOTATION
"""

Expand Down
17 changes: 7 additions & 10 deletions neurom/apps/tests/test_morph_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,18 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
from pathlib import Path
import warnings
from pathlib import Path

import neurom as nm
import numpy as np
from nose.tools import (assert_almost_equal, assert_equal,
assert_greater_equal, assert_raises, ok_)
import pandas as pd
from pandas.testing import assert_frame_equal

import neurom as nm
from neurom.apps import morph_stats as ms
from neurom.exceptions import ConfigError
from neurom.features import NEURITEFEATURES, NEURONFEATURES
from nose.tools import assert_almost_equal, assert_equal, assert_greater_equal, assert_raises, ok_
from numpy.testing import assert_array_equal

from pandas.testing import assert_frame_equal

DATA_PATH = Path(__file__).parent.parent.parent.parent / 'test_data'
SWC_PATH = DATA_PATH / 'swc'
Expand Down Expand Up @@ -139,7 +136,7 @@ def test_eval_stats_applies_numpy_function():

for m in modes:
assert_equal(ms.eval_stats(ref_array, m),
getattr(np, m)(ref_array))
getattr(np, m)(ref_array))


def test_extract_stats_single_neuron():
Expand Down Expand Up @@ -207,7 +204,7 @@ def test_extract_dataframe():
expected = pd.DataFrame(
columns=expected_columns,
data=[['Neuron', 207.87975221, 418.43241644, 214.37304578, 840.68521442],
['simple', 15., 16., 0., 31., ]])
['simple', 15., 16., 0., 31., ]])
assert_frame_equal(actual, expected)


Expand All @@ -226,7 +223,6 @@ def test_extract_dataframe_multiproc():
assert_frame_equal(actual, expected)



def test_get_header():
fake_results = {'fake_name0': REF_OUT,
'fake_name1': REF_OUT,
Expand Down Expand Up @@ -277,6 +273,7 @@ def test_sanitize_config():
new_config = ms.sanitize_config(full_config)
assert_equal(3, len(new_config)) # neurite, neurite_type & neuron


def test_multidimensional_features():
'''Features should be split into sub-features when they
are multidimensional.
Expand Down
7 changes: 3 additions & 4 deletions neurom/check/structural_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Module with consistency/validity checks for raw data blocks."""
"""Module with consistency/validity checks for raw data blocks."""
import numpy as np
from neurom.check import CheckResult
from neurom.core.dataformat import COLS
from neurom.core.dataformat import POINT_TYPE
from neurom.core import make_soma
from neurom.fst._core import make_neurites
from neurom.core.dataformat import COLS, POINT_TYPE
from neurom.exceptions import SomaError
from neurom.fst._core import make_neurites


def has_sequential_ids(data_wrapper):
Expand Down
43 changes: 21 additions & 22 deletions neurom/check/tests/test_neuron_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,31 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pathlib import Path
from copy import deepcopy
from io import StringIO

from nose import tools as nt
from nose.tools import assert_equal
from numpy.testing import assert_array_equal
from pathlib import Path

from neurom import check, load_neuron
from neurom.check import neuron_checks as nrn_chk
from neurom.core.dataformat import COLS
from neurom.core.types import dendrite_filter
from nose import tools as nt
from nose.tools import assert_equal
from numpy.testing import assert_array_equal

DATA_PATH = Path(__file__).parent.parent.parent.parent / 'test_data'
SWC_PATH = Path(DATA_PATH, 'swc')
ASC_PATH = Path(DATA_PATH, 'neurolucida')
H5V1_PATH = Path(DATA_PATH, 'h5/v1')
SWC_PATH = DATA_PATH / 'swc'
ASC_PATH = DATA_PATH / 'neurolucida'
H5V1_PATH = DATA_PATH / 'h5/v1'


def _load_neuron(name):
if name.endswith('.swc'):
path = Path(SWC_PATH, name)
path = SWC_PATH / name
elif name.endswith('.h5'):
path = Path(H5V1_PATH, name)
path = H5V1_PATH / name
else:
path = Path(ASC_PATH, name)
path = ASC_PATH / name
return name, load_neuron(path)


Expand Down Expand Up @@ -222,15 +221,13 @@ def test_nonzero_segment_lengths_bad_data():
'Single_axon.swc',
]

bad_ids = [[(2, 0), (23, 0), (44, 0), (65, 0)],
[(2, 0)],
[(2, 0)],
[(2, 0)],
[(2, 0)]]
morphio_offset = 0
bad_ids = [[2, 23, 44, 65], [2], [2], [2], [2]]

for i, nrn in enumerate(_pick(files)):
ids = nrn_chk.has_all_nonzero_segment_lengths(nrn)
nt.assert_equal(ids.info, bad_ids[i])
nt.assert_equal(ids.info,
[(id + morphio_offset, 0) for id in bad_ids[i]])


def test_nonzero_segment_lengths_threshold():
Expand All @@ -241,8 +238,11 @@ def test_nonzero_segment_lengths_threshold():
nt.assert_equal(len(ids.info), 0)

ids = nrn_chk.has_all_nonzero_segment_lengths(nrn, threshold=0.25)
nt.assert_equal(ids.info, [(2, 0), (23, 0), (38, 9), (44, 0),
(54, 7), (62, 2), (65, 0), (72, 4), (78, 6)])

bad_ids = [(2, 0), (23, 0), (38, 9), (44, 0), (54, 7), (62, 2), (65, 0), (72, 4), (78, 6)]
morphio_offset = 0
nt.assert_equal(ids.info,
[(id + morphio_offset, val) for id, val in bad_ids])


def test_nonzero_section_lengths_good_data():
Expand Down Expand Up @@ -280,13 +280,13 @@ def test_nonzero_section_lengths_threshold():

def test_has_nonzero_soma_radius():

nrn = load_neuron(Path(SWC_PATH, 'Neuron.swc'))
nrn = load_neuron(SWC_PATH / 'Neuron.swc')
nt.assert_true(nrn_chk.has_nonzero_soma_radius(nrn))


def test_has_nonzero_soma_radius_bad_data():

nrn = load_neuron(Path(SWC_PATH, 'Single_basal.swc'))
nrn = load_neuron(SWC_PATH / 'Single_basal.swc')
nt.assert_false(nrn_chk.has_nonzero_soma_radius(nrn).status)


Expand Down Expand Up @@ -420,7 +420,6 @@ def test_has_no_dangling_branch():
nt.ok_(res.status)



def test__bool__():
c = check.CheckResult(status=True)
nt.ok_(c.__nonzero__())
Expand Down
2 changes: 1 addition & 1 deletion neurom/core/_soma.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def points(self):
def volume(self):
"""Gets soma volume assuming it is a sphere."""
warnings.warn('Approximating soma volume by a sphere. {}'.format(self))
return 4.0 / 3 * math.pi * self.radius ** 3
return 4. / 3 * math.pi * self.radius ** 3


class SomaSinglePoint(Soma):
Expand Down
48 changes: 38 additions & 10 deletions neurom/core/tests/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pathlib import Path
from io import StringIO

from nose import tools as nt
from numpy.testing import assert_array_equal
from pathlib import Path

import neurom as nm
from neurom import COLS, core, load_neuron
from neurom.core import NeuriteIter, Tree
from nose import tools as nt
from numpy.testing import assert_array_equal

DATA_PATH = Path(__file__).parent.parent.parent.parent / 'test_data'

Expand All @@ -47,10 +46,14 @@
]
TOT_NEURITES = sum(len(N.neurites) for N in NEURONS)

SIMPLE = load_neuron(Path(DATA_PATH, 'swc/simple.swc'))
REVERSED_NEURITES = load_neuron(Path(DATA_PATH, 'swc/ordering/reversed_NRN_neurite_order.swc'))

POP = core.Population(NEURONS, name='foo')

# To make transition to morphio easier
MORPHIO_OFFSET = 0


def assert_sequence_equal(a, b):
nt.eq_(tuple(a), tuple(b))
Expand Down Expand Up @@ -107,35 +110,60 @@ def test_iter_sections_filter():
assert_sequence_equal(a, b)

def test_iter_sections_inrnorder():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(neurite_order=NeuriteIter.NRN)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(neurite_order=NeuriteIter.NRN)],
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 2, 3, 4])

def test_iter_sections_ipreorder():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(Tree.ipreorder)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(Tree.ipreorder)],
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 2, 3, 4])


def test_iter_sections_ipostorder():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(Tree.ipostorder)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(Tree.ipostorder)],
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 85, 83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 85, 83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 2, 3, 4])


def test_iter_sections_ibifurcation():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(Tree.ibifurcation_point)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(Tree.ibifurcation_point)],
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83],)


def test_iter_sections_iforking():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(Tree.iforking_point)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(Tree.iforking_point)],
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83])


def test_iter_sections_ileaf():
assert_sequence_equal([s.id for n in POP.neurites for s in n.iter_sections(Tree.ileaf)],
assert_sequence_equal([s.id + MORPHIO_OFFSET for n in POP.neurites for s in n.iter_sections(Tree.ileaf)],
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 85, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 85, 2, 3, 4])


def test_iter_section_nrn():
ref = list(core.iter_sections(SIMPLE))
nt.eq_(len(ref), 6)

ref = list(core.iter_sections(SIMPLE, neurite_filter=lambda n: n.type == nm.AXON))
nt.eq_(len(ref), 3)

ref = list(core.iter_sections(SIMPLE, neurite_filter=lambda n: n.type == nm.BASAL_DENDRITE))
nt.eq_(len(ref), 3)

ref = list(core.iter_sections(SIMPLE, neurite_filter=lambda n: n.type == nm.APICAL_DENDRITE))
nt.eq_(len(ref), 0)


def test_iter_segments_nrn():
ref = list(core.iter_segments(SIMPLE))
nt.eq_(len(ref), 6)

ref = list(core.iter_segments(SIMPLE, neurite_filter=lambda n: n.type == nm.AXON))
nt.eq_(len(ref), 3)

ref = list(core.iter_segments(SIMPLE, neurite_filter=lambda n: n.type == nm.BASAL_DENDRITE))
nt.eq_(len(ref), 3)

ref = list(core.iter_segments(SIMPLE, neurite_filter=lambda n: n.type == nm.APICAL_DENDRITE))
nt.eq_(len(ref), 0)

ref = list(core.iter_segments(NRN1))
nt.eq_(len(ref), 840)
Expand Down
7 changes: 3 additions & 4 deletions neurom/core/tests/test_neuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
SWC_PATH = Path(__file__).parent.parent.parent.parent / 'test_data/swc/'


def test_simple():
nrn1 = nm.load_neuron(str(Path(SWC_PATH, 'simple.swc')))

def test_load_neuron_pathlib():
nrn1 = nm.load_neuron(Path(SWC_PATH, 'simple.swc'))

Expand Down Expand Up @@ -82,10 +85,6 @@ def check_cloned_neuron(nrn1, nrn2):
nrn2.soma.radius = 10.
nt.ok_(nrn1.soma.radius != nrn2.soma.radius)

nrn2._data.data_block[0, :] = np.zeros_like(nrn2._data.data_block[0, :])
nt.ok_(not np.allclose(nrn1._data.data_block[0, :],
nrn2._data.data_block[0, :]))


def test_str():
n = nm.load_neuron(Path(SWC_PATH, 'simple.swc'))
Expand Down
16 changes: 8 additions & 8 deletions neurom/core/tests/test_soma.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from io import StringIO
import math
import warnings
from io import StringIO

from nose import tools as nt
from neurom.core import _soma
from neurom.exceptions import SomaError, RawDataError
from neurom import load_neuron
import numpy as np
from neurom import load_neuron
from neurom.core import _soma
from neurom.exceptions import RawDataError, SomaError
from nose import tools as nt
from numpy.testing import assert_array_equal
import math


def test_Soma_SinglePoint():
Expand Down Expand Up @@ -82,7 +82,7 @@ def check_SomaC(stream):
sm = load_neuron(StringIO(stream), reader='asc').soma
nt.ok_('SomaSimpleContour' in str(sm))
nt.ok_(isinstance(sm, _soma.SomaSimpleContour))
np.testing.assert_allclose(sm.center, (0., 0., 0.), atol=1e-16)
np.testing.assert_almost_equal(sm.center, [0., 0., 0.])
nt.assert_almost_equal(sm.radius, 1.0)


Expand Down Expand Up @@ -186,7 +186,7 @@ def test_Soma_Cylinders():

nt.ok_('SomaNeuromorphoThreePointCylinders' in str(s))
nt.eq_(list(s.center), [0., 0., 0.])
nt.assert_almost_equal(s.area, 794.76706126368811)
nt.assert_almost_equal(s.area, 794.76706126368811, places=4)

s = load_neuron(StringIO(u"""
1 1 0 0 0 0 -1
Expand Down
Loading

0 comments on commit 935a0d5

Please sign in to comment.