Example #1
0
def test_section_types():
    morphology = vasculature.Vasculature(os.path.join(_path, "h5/vasculature1.h5"))
    assert morphology.section(0).type == VasculatureSectionType.vein
    assert morphology.section(1).type == VasculatureSectionType.artery
    assert morphology.section(2).type == VasculatureSectionType.venule
    assert morphology.section(3).type == VasculatureSectionType.arteriole
    assert morphology.section(4).type == VasculatureSectionType.venous_capillary
    assert morphology.section(5).type == VasculatureSectionType.arterial_capillary

    with pytest.raises(RawDataError):
        vasculature.Vasculature(os.path.join(_path, "h5/vasculature-broken-section-type.h5"))
Example #2
0
def test_components_vasculature():
    morphology = vasculature.Vasculature(os.path.join(_path, "h5/vasculature1.h5"))
    assert_array_almost_equal(morphology.section(0).points,
                              np.array([[1265.47399902,  335.42364502, 1869.19274902],
                                        [1266.26647949,  335.57000732, 1869.74914551],
                                        [1267.09082031,  335.68869019, 1870.31469727],
                                        [1267.89404297,  335.78134155, 1870.91418457],
                                        [1268.67077637,  335.85733032, 1871.54992676],
                                        [1269.42773438,  335.92602539, 1872.21008301],
                                        [1270.17431641,  335.99368286, 1872.88195801],
                                        [1270.92016602,  336.06558228, 1873.55395508],
                                        [1271.6739502 ,  336.14227295, 1874.21740723],
                                        [1272.44091797,  336.23706055, 1874.86047363],
                                        [1273.22216797,  336.31613159, 1875.49523926],
                                        [1274.        ,  336.70001221, 1876.        ]]))

    assert_array_almost_equal(morphology.section(0).diameters,
                              np.array([1.96932483, 1.96932483, 1.96932483, 1.96932483, 1.96932483,
                                        1.96932483, 1.96932483, 1.96932483, 1.96932483, 1.96932483,
                                        1.96932483, 2.15068388]))

    assert len(morphology.sections) == 3080
    assert len(morphology.points) == 55807
    assert len(morphology.diameters) == 55807
    assert_array_almost_equal(morphology.diameters[-5:],
                              np.array([0.78039801, 0.78039801, 0.78039801, 2.11725187, 2.11725187]))
    assert len(morphology.section_types) == 3080
    assert len(morphology.section(0).predecessors) == 0
    assert len(morphology.section(0).successors) == 2

    assert morphology.section(0).successors[0].id == 1
    assert morphology.section(0).successors[1].id == 2
Example #3
0
def test_iterators_vasculature():
    morphology = vasculature.Vasculature(os.path.join(_path, "h5/vasculature1.h5"))
    assert_array_equal([sec.id for sec in morphology.sections], range(3080))
    assert len([section.id for section in morphology.iter()]) == 3080
    all_sections = set([sec.id for sec in morphology.sections])
    for sec in morphology.iter():
        all_sections.remove(sec.id)
    assert len(all_sections) == 0
Example #4
0
def test_section_types():
    morphology = vasculature.Vasculature(os.path.join(_path, "h5/vasculature1.h5"))
    assert_equal(morphology.section(0).type, VasculatureSectionType.vein)
    assert_equal(morphology.section(1).type, VasculatureSectionType.artery)
    assert_equal(morphology.section(2).type, VasculatureSectionType.venule)
    assert_equal(morphology.section(3).type, VasculatureSectionType.arteriole)
    assert_equal(morphology.section(4).type, VasculatureSectionType.venous_capillary)
    assert_equal(morphology.section(5).type, VasculatureSectionType.arterial_capillary)

    assert_raises(RawDataError, vasculature.Vasculature, os.path.join(_path, "h5/vasculature-broken-section-type.h5"))
Example #5
0
def test_from_pathlib():
    vasc = vasculature.Vasculature(Path(_path, "h5/vasculature1.h5"))
    assert len(vasc.sections) == 3080
Example #6
0
def test_empty_vasculature():
    with pytest.raises(RawDataError):
        vasculature.Vasculature(os.path.join(_path, "h5/empty_vasculature.h5"))
Example #7
0
    def read_data_from_file(self, center_at_origin=False):
        """Loads the data from the given file in the constructor.

        :param: center_at_origin:
            Centers the morphology at the origin.
        """

        try:

            # Import the required module
            import numpy
            import vmv.utilities
            import vmv.skeleton
            import morphio.vasculature as vasculature
            from morphio import RawDataError, VasculatureSectionType

            # Ignore the console warning and output
            vmv.utilities.disable_std_output()

            # Load the morphology data using MorphIO
            morphology_data = vasculature.Vasculature(self.morphology_file)

            # Get a list of points using the iterator
            points = numpy.vstack(
                [section.points for section in morphology_data.iter()])

            # Get a list of all the points to compute the bounding box quickly
            points_list = [
                Vector((point[0], point[1], point[2])) for point in points
            ]

            # Compute the bounding box of the morphology
            self.bounding_box = vmv.bbox.compute_bounding_box_for_list_of_points(
                points_list)

            # Update the number of samples
            self.number_loaded_vertices = copy.deepcopy(len(points_list))

            # Delete the points list
            points_list.clear()

            # Transform the data of the morphology into a normal structure
            sections_morphio = numpy.vstack([
                self.SectionMorphIO(section.id, section.points,
                                    0.5 * section.diameters,
                                    section.predecessors, section.successors)
                for section in morphology_data.iter()
            ])

            # A dictionary to keep track on the indices of the parents in the array
            index_parent_dictionary = {}
            for i_section, sec in enumerate(sections_morphio):
                index_parent_dictionary[sec[0].id] = i_section

            # Construct a list of sections to be given to the Morphology constructor
            sections_list = list()

            # On-a-per-section basis
            for section_morphio in sections_morphio:

                # Just construct the section
                section = vmv.skeleton.Section(index=section_morphio[0].id)

                # Build the Samples list
                samples = list()
                for i in range(len(section_morphio[0].points)):

                    # A reference to the point
                    point = Vector((section_morphio[0].points[i][0],
                                    section_morphio[0].points[i][1],
                                    section_morphio[0].points[i][2]))

                    # Center the morphology at the origin if required by the user
                    if center_at_origin:
                        point[0] -= self.bounding_box.center[0]
                        point[1] -= self.bounding_box.center[1]
                        point[2] -= self.bounding_box.center[2]

                    # Build the sample
                    sample = vmv.skeleton.Sample(
                        point=point, radius=section_morphio[0].radii[i])

                    # Add it to the samples list
                    samples.append(sample)

                # Link the samples list to the section
                section.samples = samples

                # Append the section to the sections list
                sections_list.append(section)

            # Updating parents and children
            # This is only needed in case we use the ConnectedSectionsBuilder
            for i in range(len(sections_list)):

                # Update the parents IDs
                parents_ids = [
                    j.id for j in sections_morphio[i][0].predecessors
                ]

                # Update the children IDs
                children_ids = [
                    k.id for k in sections_morphio[i][0].successors
                ]

                # Update the parents list
                sections_list[i].parents = [
                    sections_list[index_parent_dictionary[parent_id]]
                    for parent_id in parents_ids
                ]
                # Update the children list
                sections_list[i].children = [
                    sections_list[index_parent_dictionary[children_id]]
                    for children_id in children_ids
                ]
            # Data
            self.sections_list = sections_list

            # Number of strands
            self.number_loaded_strands = len(sections_list)

            # Detect the root sections and update the list
            for section in self.sections_list:
                if section.is_root():
                    self.roots.append(section)

            # Enable std output again
            vmv.utilities.enable_std_output()

        # Raise an exception if we cannot import the h5py module
        except ImportError:

            print('ERROR: Cannot *import h5py* to read the file [%s]' %
                  self.morphology_file)
            exit(0)