Exemplo n.º 1
0
    def launch(self,
               uploaded,
               surface_type,
               zero_based_triangles=False,
               should_center=False):
        """
        Execute import operations: unpack ZIP and build Surface object as result.

        :param uploaded: an archive containing the Surface data to be imported
        :param surface_type: a string from the following\: \
                            "Skin Air", "Skull Skin", "Brain Skull", "Cortical Surface", "EEG Cap", "Face"

        :returns: a subclass of `Surface` DataType
        :raises LaunchException: when
                * `uploaded` is missing
                * `surface_type` is invalid
        :raises RuntimeError: when triangles contain an invalid vertex index
        """
        if uploaded is None:
            raise LaunchException(
                "Please select ZIP file which contains data to import")

        self.logger.debug("Start to import surface: '%s' from file: %s" %
                          (surface_type, uploaded))
        try:
            zip_surface = ZipSurfaceParser(uploaded)
        except IOError:
            exception_str = "Did not find the specified ZIP at %s" % uploaded
            raise LaunchException(exception_str)

        # Detect and instantiate correct surface type
        self.logger.debug("Create surface instance")
        surface = self._make_surface(surface_type)
        surface.storage_path = self.storage_path
        surface.zero_based_triangles = zero_based_triangles
        if should_center:
            vertices = center_vertices(zip_surface.vertices)
        else:
            vertices = zip_surface.vertices
        surface.vertices = vertices
        if len(zip_surface.normals) != 0:
            surface.vertex_normals = zip_surface.normals
        if zero_based_triangles:
            surface.triangles = zip_surface.triangles
        else:
            surface.triangles = zip_surface.triangles - 1

        if zip_surface.bi_hemispheric:
            self.logger.info("Hemispheres detected")

        surface.hemisphere_mask = zip_surface.hemisphere_mask
        surface.triangle_normals = None

        # Now check if the triangles of the surface are valid
        triangles_min_vertex = numpy.amin(surface.triangles)
        if triangles_min_vertex < 0:
            if triangles_min_vertex == -1 and not zero_based_triangles:
                raise LaunchException(
                    "Triangles contain a negative vertex index. Maybe you have a ZERO based surface."
                )
            else:
                raise LaunchException(
                    "Your triangles contain a negative vertex index: %d" %
                    triangles_min_vertex)

        no_of_vertices = len(surface.vertices)
        triangles_max_vertex = numpy.amax(surface.triangles)
        if triangles_max_vertex >= no_of_vertices:
            if triangles_max_vertex == no_of_vertices and zero_based_triangles:
                raise LaunchException(
                    "Your triangles contain an invalid vertex index: %d. "
                    "Maybe your surface is NOT ZERO Based." %
                    triangles_max_vertex)
            else:
                raise LaunchException(
                    "Your triangles contain an invalid vertex index: %d." %
                    triangles_max_vertex)

        validation_result = surface.validate()

        if validation_result.warnings:
            self.add_operation_additional_info(validation_result.summary())

        self.logger.debug("Surface ready to be stored")
        return surface
Exemplo n.º 2
0
    def launch(self, view_model):
        # type: (ZIPSurfaceImporterModel) -> [SurfaceIndex]
        """
        Execute import operations: unpack ZIP and build Surface object as result
        :raises LaunchException: when
                * `uploaded` is missing
                * `surface_type` is invalid
        :raises RuntimeError: when triangles contain an invalid vertex index
        """
        if view_model.uploaded is None:
            raise LaunchException(
                "Please select ZIP file which contains data to import")

        self.logger.debug("Start to import surface: '%s' from file: %s" %
                          (view_model.surface_type, view_model.uploaded))
        try:
            zip_surface = ZipSurfaceParser(view_model.uploaded)
        except IOError:
            exception_str = "Did not find the specified ZIP at %s" % view_model.uploaded
            raise LaunchException(exception_str)

        # Detect and instantiate correct surface type
        self.logger.debug("Create surface instance")
        surface = self._make_surface(view_model.surface_type)
        surface.zero_based_triangles = view_model.zero_based_triangles
        if view_model.should_center:
            vertices = center_vertices(zip_surface.vertices)
        else:
            vertices = zip_surface.vertices
        surface.vertices = vertices
        if len(zip_surface.normals) != 0:
            surface.vertex_normals = zip_surface.normals
        if view_model.zero_based_triangles:
            surface.triangles = zip_surface.triangles
        else:
            surface.triangles = zip_surface.triangles - 1

        if zip_surface.bi_hemispheric:
            self.logger.info("Hemispheres detected")

        surface.hemisphere_mask = zip_surface.hemisphere_mask
        surface.compute_triangle_normals()

        # Now check if the triangles of the surface are valid
        triangles_min_vertex = numpy.amin(surface.triangles)
        if triangles_min_vertex < 0:
            if triangles_min_vertex == -1 and not view_model.zero_based_triangles:
                raise LaunchException(
                    "Triangles contain a negative vertex index. Maybe you have a ZERO based surface."
                )
            else:
                raise LaunchException(
                    "Your triangles contain a negative vertex index: %d" %
                    triangles_min_vertex)

        no_of_vertices = len(surface.vertices)
        triangles_max_vertex = numpy.amax(surface.triangles)
        if triangles_max_vertex >= no_of_vertices:
            if triangles_max_vertex == no_of_vertices and view_model.zero_based_triangles:
                raise LaunchException(
                    "Your triangles contain an invalid vertex index: %d. "
                    "Maybe your surface is NOT ZERO Based." %
                    triangles_max_vertex)
            else:
                raise LaunchException(
                    "Your triangles contain an invalid vertex index: %d." %
                    triangles_max_vertex)

        validation_result = surface.validate()

        if validation_result.warnings:
            self.add_operation_additional_info(validation_result.summary())

        surface.configure()
        self.logger.debug("Surface ready to be stored")

        surf_idx = h5.store_complete(surface, self.storage_path)
        self.generic_attributes.user_tag_1 = surface.surface_type
        return surf_idx