How to use the morphio.ostream_redirect function in morphio

To help you get started, we’ve selected a few morphio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BlueBrain / MorphIO / tests / test_6_writers.py View on Github external
PointLevel([[0, 0, 0],
                                                 [0, 5, 0]],
                                                [2, 2],
                                                [5, 6]),
                                     SectionType.basal_dendrite)
    dendrite = morpho.append_root_section(
                                     PointLevel([[0, 1, 0],
                                                 [0, 7, 0]],
                                                [2, 2],
                                                [5, 6]),
                                     SectionType.basal_dendrite)

    with setup_tempdir('test_write_no_soma') as tmp_folder:
        for ext in ['asc', 'h5', 'swc']:
            with captured_output() as (_, err):
                with ostream_redirect(stdout=True, stderr=True):
                    outfile = os.path.join(tmp_folder, 'tmp.' + ext)
                    morpho.write(outfile)
                    assert_equal(err.getvalue().strip(),
                                 'Warning: writing file without a soma')

                    read = Morphology(outfile)

            assert_equal(len(read.soma.points), 0)
            assert_equal(len(read.root_sections), 2)
            assert_array_equal(read.root_sections[0].points, [[0, 0, 0], [0, 5, 0]])
            assert_array_equal(read.root_sections[1].points, [[0, 1, 0], [0, 7, 0]])
github BlueBrain / MorphIO / tests / test_6_writers.py View on Github external
def test_single_point_root_section():
    m = Morphology()
    points = []
    diameters = []

    # Too hide the warning: appending empty section
    with captured_output():
        with ostream_redirect(stdout=True, stderr=True):
            m.append_root_section(PointLevel(points, diameters), SectionType(2))

            with setup_tempdir('test_single_point_root_section', no_cleanup=True) as tmp_folder:
                assert_raises(SectionBuilderError, m.write, os.path.join(tmp_folder, "h5/empty_vasculature.h5"))

    m = Morphology()
    points = [[1., 1., 1.]]
    diameters = [2.]
    m.append_root_section(PointLevel(points, diameters), SectionType(2))

    with setup_tempdir('test_single_point_root_section', no_cleanup=True) as tmp_folder:
        assert_raises(SectionBuilderError, m.write, os.path.join(tmp_folder, "h5/empty_vasculature.h5"))
github BlueBrain / MorphIO / tests / test_1_swc.py View on Github external
def test_disconnected_neurite():
    with captured_output() as (_, err):
        with ostream_redirect(stdout=True, stderr=True):
            n = Morphology(os.path.join(_path, 'disconnected_neurite.swc'))
            assert_equal(
                _path + '''/disconnected_neurite.swc:10:warning
Warning: found a disconnected neurite.
Neurites are not supposed to have parentId: -1
(although this is normal if this neuron has no soma)''',
                strip_color_codes(err.getvalue().strip()))
github BlueBrain / MorphIO / tests / test_2_neurolucida.py View on Github external
(3 -10 0 2)
                      (
                        (3 -10 0 2)  ; merged with parent section
                        (0 -10 0 2)  ; merged with parent section
                        (-3 -10 0 2) ; merged with parent section
                        (
                          (-5 -5 5 5)
                          |
                          (-6 -6 6 6)
                        )
                       )
                      )
                 ''') as tmp_file:

        with captured_output() as (_, err):
            with ostream_redirect(stdout=True, stderr=True):
                n = Morphology(tmp_file.name)
                assert_substring('is the only child of section: 0 starting at:',
                                 err.getvalue().strip())
                assert_substring('It will be merged with the parent section',
                                 err.getvalue().strip())

        nt.assert_equal(len(n.soma.points), 0)
        nt.assert_equal(len(n.soma.points), 0)
        assert_equal(len(n.root_sections), 1)
        assert_array_equal(n.root_sections[0].points,
                           np.array([[3, -4, 0],
                                     [3, -6, 0],
                                     [3, -8, 0],
                                     [3, -10, 0],
                                     [0, -10, 0],
                                     [-3, -10, 0]],
github BlueBrain / MorphIO / tests / test_1_swc.py View on Github external
assert_equal(Morphology(tmp_file.name).soma_type,
                     SomaType.SOMA_CYLINDERS)

    # 3 points soma can be of type SOMA_CYLINDERS or SOMA_NEUROMORPHO_THREE_POINT_CYLINDERS
    # depending on the point layout

    # SOMA_NEUROMORPHO_THREE_POINT_CYLINDERS are characterized by
    # one soma point with 2 children
    with tmp_swc_file('''1 1 0  0 0 3.0 -1
    2 1 0 -3 0 3.0  1
    3 1 0  3 0 3.0  1 # PID is 1''') as tmp_file:
        assert_equal(Morphology(tmp_file.name).soma_type,
                     SomaType.SOMA_NEUROMORPHO_THREE_POINT_CYLINDERS)

    with captured_output() as (_, err):
        with ostream_redirect(stdout=True, stderr=True):

            with tmp_swc_file('''1 1 0  0 0 3.0 -1
                                 2 1 1 -3 0 3.0  1
                                 3 1 0  0 0 3.0  1 # PID is 1''') as tmp_file:
                assert_equal(Morphology(tmp_file.name).soma_type,
                             SomaType.SOMA_NEUROMORPHO_THREE_POINT_CYLINDERS)
                assert_string_equal(
                    '''{}:0:warning
                       Warning: the soma does not conform the three point soma spec
                       The only valid neuro-morpho soma is:
                       1 1 x   y   z r -1
                       2 1 x (y-r) z r  1
                       3 1 x (y+r) z r  1

                       Got:
                       1 1 0 0 0 3 -1
github BlueBrain / MorphIO / tests / test_6_writers.py View on Github external
morpho = Morphology()
    morpho.soma.points = [[0, 0, 0], [1, 1, 1]]
    morpho.soma.diameters = [1, 1]

    section = morpho.append_root_section(PointLevel([[2, 2, 2], [3, 3, 3]],
                                                       [4, 4]),
                                         SectionType.axon,)

    section.append_section(PointLevel([[3, 3, 3], [4, 4, 4]], [10, 12]))
    section.append_section(PointLevel([[3, 3, 3], [5, 5, 5]], [11, 12]))


    with setup_tempdir('test_write_duplicate_different_diameter', no_cleanup=True) as tmp_folder:
        for ext in ['asc', 'h5', 'swc']:
            with captured_output() as (_, err):
                with ostream_redirect(stdout=True, stderr=True):
                    outfile = os.path.join(tmp_folder, 'tmp.' + ext)
                    morpho.write(outfile)

                    read = Morphology(outfile)

            assert_equal(len(read.root_sections[0].children), 2)
            child1, child2 = read.root_sections[0].children
            assert_array_equal(child1.points, [[3, 3, 3], [4, 4, 4]])
            assert_array_equal(child2.points, [[3, 3, 3], [5, 5, 5]])
            assert_array_equal(child1.diameters, [10, 12])
            assert_array_equal(child2.diameters, [11, 12])
github BlueBrain / MorphIO / tests / test_6_writers.py View on Github external
def test_write_empty_file():
    '''Check that empty morphology are not written to disk'''
    with captured_output() as (_, _):
        with ostream_redirect(stdout=True, stderr=True):
            with setup_tempdir('test_write_empty_file', no_cleanup=True) as tmp_folder:
                for ext in ['asc', 'swc', 'h5']:
                    outname = os.path.join(tmp_folder, 'empty.' + ext)
                    Morphology().write(outname)
                    ok_(not os.path.exists(outname))
github BlueBrain / MorphIO / tests / test_5_mut.py View on Github external
def test_append_no_duplicate():
    m = Morphology()

    section = m.append_root_section(PointLevel([[1, 2, 3], [4, 5, 6]],
                                               [2, 2],
                                               [20, 20]),
                                    SectionType.axon)
    assert_equal(section.id, 0)

    with captured_output() as (_, err):
        with ostream_redirect(stdout=True, stderr=True):

            section.append_section(
                             PointLevel([[400, 5, 6], [7, 8, 9]],
                                        [2, 3],
                                        [20, 30]))
            assert_equal(err.getvalue().strip(),
                         'Warning: while appending section: 1 to parent: 0\n'
                         'The section first point should be parent section last point: \n'
github BlueBrain / MorphIO / tests / test_7_modifiers.py View on Github external
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):
github BlueBrain / MorphIO / tests / test_6_writers.py View on Github external
morpho.soma.points = [[0, 0, 0]]
    morpho.soma.diameters = [2]

    root = morpho.append_root_section(
                                 PointLevel([[0, 0, 0],
                                             [0, 5, 0]],
                                            [2, 2]),
                                 SectionType.basal_dendrite)
    child = root.append_section(PointLevel([[0, 5, 0], [0, 6, 0]], [2, 3]))
    son1 = child.append_section(PointLevel([[0, 6, 0], [0, 7, 0]], [2, 3]))
    son2 = child.append_section(PointLevel([[0, 6, 0], [4, 5, 6]], [3, 3]))

    with setup_tempdir('test_write_merge_only_child') as tmp_folder:
        for extension in ['swc', 'asc', 'h5']:
            with captured_output() as (_, err):
                with ostream_redirect(stdout=True, stderr=True):
                    filename = os.path.join(tmp_folder, 'test.{}'.format(extension))
                    morpho.write(filename)

                    assert_equal(err.getvalue().strip(),
                                 'Warning: section 1 is the only child of section: 0\nIt will be merged with the parent section')


            read = Morphology(filename)
            root = read.root_sections[0]
            assert_array_equal(root.points,
                               [[0, 0, 0],
                                [0, 5, 0],
                                [0, 6, 0]])
            assert_equal(len(root.children), 2)

            assert_array_equal(root.children[0].points,