Beispiel #1
0
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"))
Beispiel #2
0
def test_write_perimeter():
    morpho = Morphology()
    morpho.soma.points = [[0, 0, 0]]
    morpho.soma.diameters = [2]

    dendrite = morpho.append_root_section(
        PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2], [5, 6]),
        SectionType.basal_dendrite)

    dendrite.append_section(PointLevel([[0, 5, 0], [-5, 5, 0]], [2, 3],
                                       [6, 7]))

    dendrite.append_section(PointLevel([[0, 5, 0], [6, 5, 0]], [2, 3], [6, 8]))

    with setup_tempdir('test_write_perimeter') as tmp_folder:
        h5_out = os.path.join(tmp_folder, "test_write.h5")
        morpho.write(h5_out)

        assert_array_equal(
            ImmutMorphology(h5_out).perimeters, [5., 6., 6., 7., 6., 8.])

        # Cannot right a morph with perimeter data to ASC and SWC
        for ext in ['swc', 'asc']:
            out_path = os.path.join(tmp_folder, "test_write_perimeter." + ext)
            assert_raises(WriterError, morpho.write, out_path)
Beispiel #3
0
def test_write_merge_only_child_asc_h5():
    '''The root section has only one child

    When writing, children should *not* be merged with their parent section.

    Note: See `test_write_merge_only_child_swc` for the SWC case.
    '''

    morpho = Morphology()
    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]))
    with setup_tempdir('test_write_merge_only_child') as tmp_folder:
        for extension in ['asc', 'h5']:
            with captured_output() as (_, err):
                with ostream_redirect(stdout=True, stderr=True):
                    filename = Path(tmp_folder, 'test.{}'.format(extension))
                    morpho.write(filename)

            read = Morphology(filename)

            root = read.root_sections[0]
            assert_array_equal(root.points,
                               [[0, 0, 0],
                                [0, 5, 0]])
            assert len(root.children) == 1
Beispiel #4
0
def test_write_no_soma():
    morpho = Morphology()
    dendrite = morpho.append_root_section(
                                     PointLevel([[0, 0, 0],
                                                 [0, 5, 0]],
                                                [2, 2]),
                                     SectionType.basal_dendrite)
    dendrite = morpho.append_root_section(
                                     PointLevel([[0, 1, 0],
                                                 [0, 7, 0]],
                                                [2, 2]),
                                     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]])
Beispiel #5
0
def test_endoplasmic_reticulum():
    neuron = Morphology(Path(_path, "simple.asc"))
    reticulum = neuron.endoplasmic_reticulum
    assert_equal(reticulum.section_indices, [])
    assert_equal(reticulum.volumes, [])
    assert_equal(reticulum.surface_areas, [])
    assert_equal(reticulum.filament_counts, [])

    reticulum.section_indices = [1, 1]
    reticulum.volumes = [2, 2]
    reticulum.surface_areas = [3, 3]
    reticulum.filament_counts = [4, 4]

    assert_equal(reticulum.section_indices, [1, 1])
    assert_equal(reticulum.volumes, [2, 2])
    assert_equal(reticulum.surface_areas, [3, 3])
    assert_equal(reticulum.filament_counts, [4, 4])

    with setup_tempdir('test-endoplasmic-reticulum') as folder:
        path = Path(folder, 'with-reticulum.h5')
        neuron.write(path)

        neuron = Morphology(path)
    reticulum = neuron.endoplasmic_reticulum
    assert_equal(reticulum.section_indices, [1, 1])
    assert_equal(reticulum.volumes, [2, 2])
    assert_equal(reticulum.surface_areas, [3, 3])
    assert_equal(reticulum.filament_counts, [4, 4])
Beispiel #6
0
def test_duplicate_different_diameter():
    '''Test that starting a child section with a different diamete
    work as expected'''
    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])
Beispiel #7
0
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))
Beispiel #8
0
def test_write_soma_basic():
    morpho = Morphology()
    morpho.soma.points = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    morpho.soma.diameters = [2, 3, 3]

    with setup_tempdir('test_write_soma_basic') as tmp_folder:
        morpho.write(os.path.join(tmp_folder, "test_write.swc"))
        morpho.write(Path(tmp_folder, "test_write_pathlib.swc"))
        assert_equal(len(os.listdir(tmp_folder)), 2)
Beispiel #9
0
def test_mitochondria():
    morpho = Morphology()
    morpho.soma.points = [[0, 0, 0], [1, 1, 1]]
    morpho.soma.diameters = [1, 1]

    neuronal_section_ids = [0, 0]
    relative_pathlengths = np.array([0.5, 0.6], dtype=np.float32)
    diameters = [10, 20]
    mito_id = morpho.mitochondria.append_root_section(
        MitochondriaPointLevel(neuronal_section_ids, relative_pathlengths,
                               diameters))

    mito_id.append_section(
        MitochondriaPointLevel([0, 0, 0, 0], [0.6, 0.7, 0.8, 0.9],
                               [20, 30, 40, 50]))
    with setup_tempdir('test_mitochondria') as tmp_folder:
        morpho.write(os.path.join(tmp_folder, "test.h5"))

        with captured_output() as (_, err):
            with ostream_redirect(stdout=True, stderr=True):
                morpho.write(os.path.join(tmp_folder, "test.swc"))
                assert_string_equal(
                    err.getvalue(),
                    "Warning: this cell has mitochondria, they cannot be saved in "
                    " ASC or SWC format. Please use H5 if you want to save them."
                )

        with captured_output() as (_, err):
            with ostream_redirect(stdout=True, stderr=True):
                morpho.write(os.path.join(tmp_folder, "test.asc"))
                assert_string_equal(
                    err.getvalue(),
                    "Warning: this cell has mitochondria, they cannot be saved in "
                    " ASC or SWC format. Please use H5 if you want to save them."
                )

        mito = ImmutMorphology(os.path.join(tmp_folder,
                                            'test.h5')).mitochondria
        assert_array_equal(mito.root_sections[0].diameters, diameters)
        assert_array_equal(mito.root_sections[0].neurite_section_ids,
                           neuronal_section_ids)
        assert_array_equal(mito.root_sections[0].relative_path_lengths,
                           relative_pathlengths)

        assert_equal(len(mito.root_sections), 1)

        mito = Morphology(os.path.join(tmp_folder, 'test.h5')).mitochondria
        assert_equal(len(mito.root_sections), 1)
        assert_equal(mito.root_sections[0].neurite_section_ids,
                     neuronal_section_ids)
        assert_array_equal(mito.section(0).diameters, diameters)

        assert_array_equal(
            mito.section(0).neurite_section_ids, neuronal_section_ids)
Beispiel #10
0
def test_convert():
    set_maximum_warnings(0)
    with setup_tempdir('test-convert-file') as tmp_dir:
        runner = CliRunner()
        filename = Path(DATA, 'simple.asc')
        output_name = Path(tmp_dir, 'simple.h5')
        result = runner.invoke(cli, ['convert', 'file', str(filename), str(output_name)])
        assert_equal(result.exit_code, 0, result.exception)
        ok_(output_name.exists())


    with setup_tempdir('test-convert-folder') as tmp_dir:
        runner = CliRunner()
        result = runner.invoke(cli, ['convert', 'folder', '-ext', 'swc',
                                     str(DATA / 'input-convert'), tmp_dir])
        assert_equal(result.exit_code, 0, result.exc_info)

        n_converted_files = len(list(Path(tmp_dir).rglob('**/*.swc')))

        assert_equal(n_converted_files, 2)
def test_convert_swc_contour_to_sphere():
    with setup_tempdir('test_convert_swc_contour_to_sphere') as tmp_dir:
        # needs to have a complex contour soma
        simple = os.path.join(DATA, 'tkb061126a4_ch0_cc2_h_zk_60x_1.asc')
        outname = os.path.join(tmp_dir, 'test.swc')
        convert(simple, outname, single_point_soma=True)

        m = morphio.Morphology(outname)
        assert_equal(1, len(m.soma.points))
        assert_equal(1, len(m.soma.diameters))

        #value dumped from NEURON: h.area(0.5, sec=icell.soma[0])
        np.testing.assert_approx_equal(m.soma.surface, 476.0504050847511)
Beispiel #12
0
def test_convert_sanitize():

    with setup_tempdir('test_convert_sanitize') as tmp_dir:
        # needs to have a complex contour soma
        simple = os.path.join(DATA, 'single_child.asc')
        outname = os.path.join(tmp_dir, 'single_child.swc')
        with assert_raises(MorphToolException) as obj:
            convert(simple, outname, single_point_soma=True)
        assert_equal('Use `sanitize` option for converting', str(obj.exception))

        convert(simple, outname, single_point_soma=True, sanitize=True)
        m = morphio.Morphology(outname)
        assert_equal(len(m.sections), 1)
Beispiel #13
0
def test_write_merge_only_child():
    '''The root section has only one child
    The child should be merged with its parent section
    Special care must be given for the potential duplicate point
                             o
                            /
                           / son 1
      root       child    /
    o--------o----------o<
                          \
                           \  son 2
                            \
                             o
    '''
    morpho = Morphology()
    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,
                               [[0, 6, 0], [0, 7, 0]])

            assert_array_equal(root.children[1].points,
                               [[0, 6, 0], [4, 5, 6]])
Beispiel #14
0
def test_convert():
    with setup_tempdir('test-convert') as tmp_dir:
        for in_ext, out_ext in it.product(['asc', 'h5', 'swc'], repeat=2):
            # A simple morphology
            inname = os.path.join(DATA, 'simple.' + in_ext)
            outname = os.path.join(tmp_dir, 'test.' + out_ext)
            convert(inname, outname)
            ok_(not diff(inname, outname))

            # A more complex one
            inname = os.path.join(DATA, 'tkb061126a4_ch0_cc2_h_zk_60x_1.' + in_ext)
            outname = os.path.join(tmp_dir, 'test.' + out_ext)
            convert(inname, outname)
            diff_result = diff(inname, outname, rtol=1e-5, atol=1e-5)
            ok_(not bool(diff_result), diff_result.info)
Beispiel #15
0
def test_convert_recenter():
    with setup_tempdir('test-convert_recenter') as tmp_dir:
        simple = os.path.join(DATA, 'simple.swc')
        outname = os.path.join(tmp_dir, 'test.swc')
        convert(simple, outname, recenter=True)
        ok_(not diff(simple, outname))  #simple.swc is already centered

        mut = morphio.Morphology(simple).as_mutable()
        mut.soma.points = [[1, 1, 1], ]
        inname = os.path.join(tmp_dir, 'moved.swc')
        mut.write(inname)

        convert(inname, outname, recenter=True)
        simple = morphio.Morphology(simple)
        centered_morph = morphio.Morphology(outname)
        ok_(np.all((simple.points - centered_morph.points) == 1))
Beispiel #16
0
def feature_extractor(param_file,
                      paths_to_images_and_masks,
                      path_to_results,
                      verbose=0,
                      n_jobs=None,
                      drop_missing=False,
                      variance_thresh=0.0):
    """Extract features from images using the PyRadimoics package.

    Preliminary results are stored for re-entering the process in case of
    abortion.

    Args:
        param_file (str):
        samples (list):

    Kwargs:
        verbose (int): The level of verbosity during extraction.
        n_jobs (int): The number of CPUs to distribute the extraction process.
            Defaults to available - 1.

    Returns:
        (dict): The extracted image features.

    """

    global TMP_FEATURE_DIR, TIMEOUT

    _check_is_file(param_file)

    # Setup temporary directory to store preliminary results.
    path_tempdir = utils.setup_tempdir(TMP_FEATURE_DIR)

    # Set number of available CPUs.
    if n_jobs is None:
        n_jobs = cpu_count() - 1 if cpu_count() > 1 else cpu_count()

    if verbose > 0:
        print('Initiated feature extraction.')

    # Extract features.
    results = Parallel(n_jobs=n_jobs, verbose=verbose, timeout=TIMEOUT)(
        delayed(_extract_features)(param_file=param_file,
                                   case=path_to_images_and_masks,
                                   path_tempdir=path_tempdir,
                                   verbose=verbose)
        for path_to_images_and_masks in paths_to_images_and_masks)
Beispiel #17
0
def test_write_basic():
    morpho = Morphology()
    morpho.soma.points = [[0, 0, 0]]
    morpho.soma.diameters = [2]

    dendrite = morpho.append_root_section(PointLevel([[0, 0, 0], [0, 5, 0]], [2, 2]),
                                          SectionType.basal_dendrite)

    dendrite.append_section(PointLevel([[0, 5, 0], [-5, 5, 0]], [2, 3]))
    dendrite.append_section(PointLevel([[0, 5, 0], [6, 5, 0]], [2, 3]))

    axon = morpho.append_root_section(
                                 PointLevel([[0, 0, 0],
                                             [0, -4, 0]],
                                            [2, 2]),
                                 SectionType.axon)

    axon.append_section(PointLevel([[0, -4, 0],
                                      [6, -4, 0]],
                                     [2, 4]))

    axon = axon.append_section(PointLevel([[0, -4, 0],
                                             [-5, -4, 0]],
                                            [2, 4]))

    with setup_tempdir('test_write_basic') as tmp_folder:
        morpho.write(Path(tmp_folder, "test_write.asc"))
        morpho.write(Path(tmp_folder, "test_write.swc"))
        h5_out = Path(tmp_folder, "test_write.h5")
        morpho.write(h5_out)

        expected = [[0., 0., 0.], [0., 5., 0.], [0., 5., 0.], [-5., 5., 0.],
                    [0., 5., 0.], [6., 5., 0.], [0., 0., 0.], [0., -4., 0.],
                    [0., -4., 0.], [6., -4., 0.], [0., -4., 0.], [-5., -4., 0.]]
        assert_array_equal(ImmutMorphology(Path(tmp_folder, "test_write.asc")).points, expected)
        assert_array_equal(ImmutMorphology(Path(tmp_folder, "test_write.swc")).points, expected)
        h5_morph = ImmutMorphology(Path(tmp_folder, "test_write.h5"))
        assert_array_equal(h5_morph.points, expected)
        assert h5_morph.version == ('h5', 1, 2)

        import h5py
        with h5py.File(h5_out, 'r') as h5_file:
            assert '/perimeters' not in h5_file.keys()
Beispiel #18
0
 def test_roundtrip(self):
     with setup_tempdir() as tmp_dir:
         output = Path(tmp_dir, "output.csv")
         self.test_obj_sorted.to_csv(str(output))
         new = self.ids_cls.from_csv(str(output))
         assert self.test_obj_sorted == new
Beispiel #19
0
 def test_roundtrip(self):
     with setup_tempdir() as tmp_dir:
         output = Path(tmp_dir, "output.csv")
         self.test_obj_sorted.to_csv(str(output))
         new = test_module.CircuitNodeIds.from_csv(str(output))
         assert self.test_obj_sorted == new