def test_v1(): n = Morphology(os.path.join(H5V1_PATH, 'simple.h5')) assert_equal(len(n.root_sections), 2) assert_equal(n.root_sections[0].type, 3) assert_equal(n.root_sections[1].type, 2) n = Morphology(os.path.join(H5V1_PATH, 'Neuron.h5')) assert_equal(n.version, MorphologyVersion.MORPHOLOGY_VERSION_H5_1) assert_equal(len(n.sections), 84) assert_equal(len(n.soma.points), 3) assert_equal(len(list(n.iter())), 84) assert_equal(len(n.points), 924) section_types = list(s.type for s in n.iter()) assert_equal(len(section_types), 84) real_section_types = list( chain(repeat(SectionType.apical_dendrite, 21), repeat(SectionType.basal_dendrite, 42), repeat(SectionType.axon, 21))) assert_equal(section_types, real_section_types) assert_array_equal( n.points[:7], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.10000000149011612], [0.5529246926307678, -0.7534923553466797, 0.9035181403160095], [1.2052767276763916, -1.3861794471740723, 1.6835479736328125], [1.2670834064483643, -1.3914604187011719, 2.4186644554138184], [1.271288275718689, -2.3130500316619873, 3.192789077758789], [1.605881929397583, -2.6893420219421387, 3.992844343185425]])
def test_v1(): n = Morphology(H5V1_PATH / 'simple.h5') assert len(n.root_sections) == 2 assert n.root_sections[0].type == 3 assert n.root_sections[1].type == 2 assert n.version == ("h5", 1, 1) n = Morphology(H5V1_PATH / 'Neuron.h5') assert n.version == ("h5", 1, 0) assert len(n.sections) == 84 assert len(n.soma.points) == 3 assert len(list(n.iter())) == 84 assert len(n.points) == 924 section_types = list(s.type for s in n.iter()) assert len(section_types) == 84 real_section_types = list( chain(repeat(SectionType.apical_dendrite, 21), repeat(SectionType.basal_dendrite, 42), repeat(SectionType.axon, 21))) assert section_types == real_section_types assert_array_equal( n.points[:7], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.10000000149011612], [0.5529246926307678, -0.7534923553466797, 0.9035181403160095], [1.2052767276763916, -1.3861794471740723, 1.6835479736328125], [1.2670834064483643, -1.3914604187011719, 2.4186644554138184], [1.271288275718689, -2.3130500316619873, 3.192789077758789], [1.605881929397583, -2.6893420219421387, 3.992844343185425]])
def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8): ''' Returns a DiffResult object that is equivalent to True when morphologies differ Additional information about why they differ is stored in DiffResult.info Morphologies with different formats can be compared. Morphologies are considered different if one of the following property differ: - number of root sections - sections type - sections point array - sections diameter array - sections perimeter array - sections number of children The soma are NOT taken into consideration Args: morph1 (str|morphio.Morphology|morphio.mut.Morphology): a morphology morph2 (str|morphio.Morphology|morphio.mut.Morphology): a morphology rtol (float): the relative tolerance used for comparing points (see numpy.allclose help) atol (float): the absolute tolerance used for comparing points (see numpy.allclose help) ''' if not isinstance(morph1, Morphology): morph1 = Morphology(morph1) if not isinstance(morph2, Morphology): morph2 = Morphology(morph2) if len(morph1.root_sections) != len(morph2.root_sections): return DiffResult( True, 'Both morphologies have different a different number of root sections' ) for section1, section2 in zip(morph1.iter(), morph2.iter()): for attrib in ['points', 'diameters', 'perimeters']: val1, val2 = getattr(section1, attrib), getattr(section2, attrib) if val1.shape != val2.shape: return DiffResult( True, 'Attributes Section.{} of:\n' '{}\n{}\nhave different shapes: {} vs {}'.format( attrib, section1, section2, val1.shape, val2.shape)) if not allclose(val1, val2, rtol=rtol, atol=atol): return DiffResult( True, 'Attributes Section.{} of:\n' '{}\n{}\nhave the same shape but different values'.format( attrib, section1, section2)) if section1.type != section2.type: return DiffResult( True, '{} and {} have different section types'.format( section1, section2)) if len(section1.children) != len(section2.children): return DiffResult( True, '{} and {} have different a different number of children'. format(section1, section2)) return DiffResult(False)
def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8): """Returns a DiffResult object that is equivalent to True when morphologies differ. Additional information about why they differ is stored in DiffResult.info Morphologies with different formats can be compared. Morphologies are considered different if one of the following property differ: - number of root sections - sections type - sections point array - sections diameter array - sections perimeter array - sections number of children The soma are NOT taken into consideration Args: morph1 (str|morphio.Morphology|morphio.mut.Morphology): a morphology morph2 (str|morphio.Morphology|morphio.mut.Morphology): a morphology rtol (float): the relative tolerance used for comparing points (see numpy.isclose help) atol (float): the absolute tolerance used for comparing points (see numpy.isclose help) """ if not isinstance(morph1, Morphology): morph1 = Morphology(morph1) if not isinstance(morph2, Morphology): morph2 = Morphology(morph2) if len(morph1.root_sections) != len(morph2.root_sections): return DiffResult(True, 'Both morphologies have a different number of root sections') for section1, section2 in zip(morph1.iter(), morph2.iter()): for attrib in ['points', 'diameters', 'perimeters']: val1, val2 = getattr(section1, attrib), getattr(section2, attrib) if val1.shape != val2.shape: return DiffResult(True, f'Attributes Section.{attrib} of:\n' f'{section1}\n' f'{section2}\n' f'have different shapes: {val1.shape} vs {val2.shape}' ) is_close = np.isclose(val1, val2, rtol=rtol, atol=atol) if not is_close.all(): first_diff_index = np.where(~is_close)[0][0] return DiffResult(True, f'Attributes Section.{attrib} of:\n' f'{section1}\n{section2}\nhave the same shape ' 'but different values\n' f'Vector {attrib} differs at index {first_diff_index}: ' f'{val1[first_diff_index]} != {val2[first_diff_index]}') if section1.type != section2.type: return DiffResult(True, f'{section1} and {section2} have different section types') if len(section1.children) != len(section2.children): return DiffResult(True, f'{section1} and {section2} have a different number of children') return DiffResult(False)
def test_more_iter(): '''This used to fail at commit f74ce1f56de805ebeb27584051bbbb3a65cd1213''' m = Morphology(os.path.join(_path, 'simple.asc')) sections = list(m.iter()) assert_array_equal([s1.id for s1 in sections], [0, 1, 2, 3, 4, 5]) sections = list(m.iter(IterType.breadth_first)) assert_array_equal([s.id for s in sections], [0, 3, 1, 2, 4, 5]) sections = list(m.section(2).iter(IterType.upstream)) assert_array_equal([s.id for s in sections], [2, 0])
def test_read_split_soma(): with tmp_swc_file('''# A simple neuron consisting of a two-branch soma # with a single branch neurite on each branch. # # initial soma point 1 1 1 0 1 4.0 -1 # first neurite 2 3 0 0 2 0.5 1 3 3 0 0 3 0.5 2 4 3 0 0 4 0.5 3 5 3 0 0 5 0.5 4 # soma branch, off initial point 6 1 2 0 0 4.0 1 7 1 3 0 0 4.0 6 # second neurite, off soma branch 8 3 0 0 6 0.5 1 9 3 0 0 7 0.5 8 10 3 0 0 8 0.5 9 11 3 0 0 9 0.5 10 ''') as tmp_file: n = Morphology(tmp_file.name) assert_array_equal(n.soma.points, [[1, 0, 1], [2, 0, 0], [3, 0, 0]]) nt.assert_equal(len(n.root_sections), 2) assert_array_equal(n.root_sections[0].points, [[0, 0, 2], [0, 0, 3], [0, 0, 4], [0, 0, 5]]) assert_array_equal(n.root_sections[1].points, [[0, 0, 6], [0, 0, 7], [0, 0, 8], [0, 0, 9]]) nt.eq_(len(list(n.iter())), 2)
def test_two_point_section(): m = Morphology(os.path.join(_path, 'multiple_point_section.asc'), options=Option.two_points_sections) assert_array_equal( [section.points for section in m.iter()], [[[0., 0., 0.], [10., 50., 0.]], [[10, 50, 0], [0, 1, 2]], [[10, 50, 0], [0, 4, 5]], [[0, 0, 0], [7, 7, 7]]])
def test_no_duplicate(): with captured_output(): with ostream_redirect(stdout=True, stderr=True): m = Morphology(SIMPLE, options=Option.no_duplicates) neurite1 = np.array([[0., 0., 0.], [0, 5, 0], [-5, 5, 0], [6, 5, 0]]) neurite2 = np.array([[0, 0, 0], [0, -4, 0], [6, -4, 0], [-5, -4, 0]]) assert_array_equal(np.vstack([section.points for section in m.iter()]), np.vstack([neurite1, neurite2])) # Combining options NO_DUPLICATES and NRN_ORDER with captured_output(): with ostream_redirect(stdout=True, stderr=True): m = Morphology(SIMPLE, options=Option.no_duplicates | Option.nrn_order) assert_array_equal(np.vstack([section.points for section in m.iter()]), np.vstack([neurite2, neurite1]))
def test_no_duplicate(): with captured_output(): with ostream_redirect(stdout=True, stderr=True): m = Morphology(SIMPLE, options=Option.no_duplicates) neurite1 = [[[0., 0., 0.], [0, 5, 0]], [[-5, 5, 0]], [[6, 5, 0]]] neurite2 = [[[0, 0, 0], [0, -4, 0]], [[6, -4, 0]], [[-5, -4, 0]]] assert_array_equal([section.points.tolist() for section in m.iter()], neurite1 + neurite2) # Combining options NO_DUPLICATES and NRN_ORDER with captured_output(): with ostream_redirect(stdout=True, stderr=True): m = Morphology(SIMPLE, options=Option.no_duplicates | Option.nrn_order) assert_array_equal([section.points.tolist() for section in m.iter()], neurite2 + neurite1)
def test_v2(): n = Morphology(os.path.join(H5V2_PATH, 'Neuron.h5')) assert_equal(n.version, MorphologyVersion.MORPHOLOGY_VERSION_H5_2) # This is a bug in the Neuron.h5 file # First neurite should not have a type: soma assert_equal(n.root_sections[0].type, 1) assert_equal(n.root_sections[1].type, 4) assert_equal(len(list(n.iter())), 85) assert_equal(len(n.points), 926) assert_equal(len(n.sections), 85)
def test_build_read_only(): m = Morphology() m.soma.points = [[-1, -2, -3]] m.soma.diameters = [-4] section = m.append_root_section(PointLevel([[1, 2, 3], [4, 5, 6]], [2, 2], [20, 20]), SectionType.axon) section.append_section(PointLevel([[4, 5, 6], [7, 8, 9]], [2, 3], [20, 30])) section.append_section(PointLevel([[4, 5, 6], [10, 11, 12]], [2, 2], [20, 20])) immutable_morphology = ImmutableMorphology(m) sections = list(immutable_morphology.iter()) assert_equal(len(sections), 3) assert_array_equal(immutable_morphology.soma.points, [[-1, - 2, -3]]) assert_array_equal(immutable_morphology.soma.diameters, [-4]) assert_array_equal(immutable_morphology.section(0).points, [[1, 2, 3], [4, 5, 6]]) assert_array_equal(immutable_morphology.section(0).diameters, [2, 2]) assert_array_equal(immutable_morphology.section(0).perimeters, [20, 20]) assert_equal(len(immutable_morphology.section(0).children), 2) child = immutable_morphology.section(0).children[0] assert_array_equal(child.points, [[4, 5, 6], [7, 8, 9]]) assert_array_equal(child.diameters, [2, 3]) assert_array_equal(child.perimeters, [20, 30]) same_child = immutable_morphology.section(1) assert_array_equal(same_child.points, [[4, 5, 6], [7, 8, 9]]) assert_array_equal(same_child.diameters, [2, 3]) assert_array_equal(same_child.perimeters, [20, 30])
def test_resample_linear_density__astrocyte(): """Run the function and make sure that it doesn't mutate the input cell """ obj = Morphology(DATA_DIR / 'neuron.h5') new_obj = tested.resample_linear_density(obj, linear_density=0.).as_immutable() # densty 0 result to only first and last data points assert len(obj.points) > len(new_obj.points) assert len(obj.diameters) > len(new_obj.diameters) assert len(obj.perimeters) > len(new_obj.perimeters) for s1, s2 in zip(obj.iter(), new_obj.iter()): s2_points = s2.points s2_diameters = s2.diameters assert len(s2_points) == len(s2_diameters) == 2 _assert_allclose_first_last(s1.points, s2_points) _assert_allclose_first_last(s1.diameters, s2_diameters)
def test_iter(): neuron = Morphology(os.path.join(_path, "iterators.asc")) root = neuron.root_sections[0] assert_array_equal([section.id for section in root.iter(IterType.depth_first)], [0, 1, 2, 3, 4, 5, 6]) assert_array_equal([section.id for section in root.iter(IterType.breadth_first)], [0, 1, 4, 2, 3, 5, 6]) assert_array_equal([section.id for section in neuron.iter(IterType.breadth_first)], [0, 7, 1, 4, 8, 9, 2, 3, 5, 6]) for _, cell in CELLS.items(): assert_array_equal([section.id for section in cell.iter()], [0, 1, 2, 3, 4, 5]) assert_array_equal([section.id for section in cell.iter(IterType.depth_first)], [0, 1, 2, 3, 4, 5]) assert_array_equal([section.points for section in cell.root_sections[0].children[0].iter(IterType.upstream)], [[[0., 5., 0.], [-5., 5., 0.]], [[0., 0., 0.], [0., 5., 0.]]])
def test_single_children(): with captured_output() as (_, _): with ostream_redirect(stdout=True, stderr=True): neuron = Morphology( os.path.join(H5V1_PATH, 'two_child_unmerged.h5')) assert_equal(len(list(neuron.iter())), 3)
def test_single_children(): with captured_output() as (_, _): with ostream_redirect(stdout=True, stderr=True): neuron = Morphology(H5V1_PATH / 'two_child_unmerged.h5') assert len(list(neuron.iter())) == 8