Ejemplo n.º 1
0
def test_store_load_complete_region_mapping(tmph5factory, connectivity_factory, surface_factory, region_mapping_factory):
    connectivity = connectivity_factory(2)
    surface = surface_factory(5)
    region_mapping = region_mapping_factory(surface, connectivity)

    with ConnectivityH5(tmph5factory('Connectivity_{}.h5'.format(connectivity.gid))) as conn_h5:
        conn_h5.store(connectivity)
        conn_stored = Connectivity()
        conn_h5.load_into(conn_stored)

    with SurfaceH5(tmph5factory('Surface_{}.h5'.format(surface.gid))) as surf_h5:
        surf_h5.store(surface)
        surf_stored = Surface()
        surf_h5.load_into(surf_stored)

    with RegionMappingH5(tmph5factory('RegionMapping_{}.h5'.format(region_mapping.gid))) as rm_h5:
        rm_h5.store(region_mapping)
        rm_stored = RegionMapping()
        rm_h5.load_into(rm_stored)

    # load_into will not load dependent datatypes. connectivity and surface are undefined
    with pytest.raises(TraitAttributeError):
        rm_stored.connectivity
    with pytest.raises(TraitAttributeError):
        rm_stored.surface

    rm_stored.connectivity = conn_stored
    rm_stored.surface = surf_stored
    assert rm_stored.connectivity is not None
    assert rm_stored.surface is not None
Ejemplo n.º 2
0
    def _store_related_region_mappings(self, original_conn_gid, new_connectivity_ht):
        result = []

        linked_region_mappings = dao.get_generic_entity(RegionMappingIndex, original_conn_gid, 'fk_connectivity_gid')
        for mapping in linked_region_mappings:
            original_rm = h5.load_from_index(mapping)
            surface = self.load_traited_by_gid(mapping.fk_surface_gid)

            new_rm = RegionMapping()
            new_rm.connectivity = new_connectivity_ht
            new_rm.surface = surface
            new_rm.array_data = original_rm.array_data

            result_rm_index = self.store_complete(new_rm)
            result.append(result_rm_index)

        return result
Ejemplo n.º 3
0
    def _store_related_region_mappings(self, original_conn_gid,
                                       new_connectivity_ht):
        result = []

        linked_region_mappings = dao.get_generic_entity(
            RegionMappingIndex, original_conn_gid, 'connectivity_gid')
        for mapping in linked_region_mappings:
            original_rm = h5.load_from_index(mapping)
            surface_idx = dao.get_generic_entity(SurfaceIndex,
                                                 mapping.surface_gid, 'gid')[0]
            surface = h5.load_from_index(surface_idx)

            new_rm = RegionMapping()
            new_rm.connectivity = new_connectivity_ht
            new_rm.surface = surface
            new_rm.array_data = original_rm.array_data

            result_rm_index = h5.store_complete(new_rm, self.storage_path)
            result.append(result_rm_index)

        return result
Ejemplo n.º 4
0
    def launch(self, mapping_file, surface, connectivity):
        """
        Creates region mapping from uploaded data.

        :param mapping_file: an archive containing data for mapping surface to connectivity

        :raises LaunchException: when
                    * a parameter is None or missing
                    * archive has more than one file
                    * uploaded files are empty
                    * number of vertices in imported file is different to the number of surface vertices
                    * imported file has negative values
                    * imported file has regions which are not in connectivity
        """
        if mapping_file is None:
            raise LaunchException(
                "Please select mappings file which contains data to import")
        if surface is None:
            raise LaunchException(
                "No surface selected. Please initiate upload again and select a brain surface."
            )
        if connectivity is None:
            raise LaunchException(
                "No connectivity selected. Please initiate upload again and select one."
            )

        self.logger.debug("Reading mappings from uploaded file")

        if zipfile.is_zipfile(mapping_file):
            tmp_folder = tempfile.mkdtemp(
                prefix='region_mapping_zip_',
                dir=TvbProfile.current.TVB_TEMP_FOLDER)
            try:
                files = FilesHelper().unpack_zip(mapping_file, tmp_folder)
                if len(files) > 1:
                    raise LaunchException(
                        "Please upload a ZIP file containing only one file.")
                array_data = self.read_list_data(files[0], dtype=numpy.int32)
            finally:
                if os.path.exists(tmp_folder):
                    shutil.rmtree(tmp_folder)
        else:
            array_data = self.read_list_data(mapping_file, dtype=numpy.int32)

        # Now we do some checks before building final RegionMapping
        if array_data is None or len(array_data) == 0:
            raise LaunchException(
                "Uploaded file does not contains any data. Please initiate upload with another file."
            )

        # Check if we have a mapping for each surface vertex.
        if len(array_data) != surface.number_of_vertices:
            msg = "Imported file contains a different number of values than the number of surface vertices. " \
                  "Imported: %d values while surface has: %d vertices." % (len(array_data), surface.number_of_vertices)
            raise LaunchException(msg)

        # Now check if the values from imported file correspond to connectivity regions
        if array_data.min() < 0:
            raise LaunchException(
                "Imported file contains negative values. Please fix problem and re-import file"
            )

        if array_data.max() >= connectivity.number_of_regions:
            msg = "Imported file contains invalid regions. Found region: %d while selected connectivity has: %d " \
                  "regions defined (0 based)." % (array_data.max(), connectivity.number_of_regions)
            raise LaunchException(msg)

        self.logger.debug("Creating RegionMapping instance")
        region_mapping_inst = RegionMapping()
        region_mapping_inst.storage_path = self.storage_path
        region_mapping_inst.surface = surface
        region_mapping_inst.connectivity = connectivity

        if array_data is not None:
            region_mapping_inst.array_data = array_data

        return [region_mapping_inst]
    def launch(self, mapping_file, surface, connectivity):
        """
        Creates region mapping from uploaded data.

        :param mapping_file: an archive containing data for mapping surface to connectivity

        :raises LaunchException: when
                    * a parameter is None or missing
                    * archive has more than one file
                    * uploaded files are empty
                    * number of vertices in imported file is different to the number of surface vertices
                    * imported file has negative values
                    * imported file has regions which are not in connectivity
        """
        if mapping_file is None:
            raise LaunchException("Please select mappings file which contains data to import")
        if surface is None:
            raise LaunchException("No surface selected. Please initiate upload again and select a brain surface.")
        if connectivity is None:
            raise LaunchException("No connectivity selected. Please initiate upload again and select one.")
            
        self.logger.debug("Reading mappings from uploaded file")

        if zipfile.is_zipfile(mapping_file):
            tmp_folder = tempfile.mkdtemp(prefix='region_mapping_zip_', dir=TvbProfile.current.TVB_TEMP_FOLDER)
            try:
                files = FilesHelper().unpack_zip(mapping_file, tmp_folder)
                if len(files) > 1:
                    raise LaunchException("Please upload a ZIP file containing only one file.")
                array_data = self.read_list_data(files[0], dtype=numpy.int32)
            finally:
                if os.path.exists(tmp_folder):
                    shutil.rmtree(tmp_folder)
        else:
            array_data = self.read_list_data(mapping_file, dtype=numpy.int32)
        
        # Now we do some checks before building final RegionMapping
        if array_data is None or len(array_data) == 0:
            raise LaunchException("Uploaded file does not contains any data. Please initiate upload with another file.")
        
        # Check if we have a mapping for each surface vertex.
        if len(array_data) != surface.number_of_vertices:
            msg = "Imported file contains a different number of values than the number of surface vertices. " \
                  "Imported: %d values while surface has: %d vertices." % (len(array_data), surface.number_of_vertices)
            raise LaunchException(msg)     
        
        # Now check if the values from imported file correspond to connectivity regions
        if array_data.min() < 0:
            raise LaunchException("Imported file contains negative values. Please fix problem and re-import file")
        
        if array_data.max() >= connectivity.number_of_regions:
            msg = "Imported file contains invalid regions. Found region: %d while selected connectivity has: %d " \
                  "regions defined (0 based)." % (array_data.max(), connectivity.number_of_regions)
            raise LaunchException(msg)
        
        self.logger.debug("Creating RegionMapping instance")
        region_mapping_inst = RegionMapping()
        region_mapping_inst.storage_path = self.storage_path
        region_mapping_inst.set_operation_id(self.operation_id)
        region_mapping_inst.surface = surface
        region_mapping_inst.connectivity = connectivity
        
        if array_data is not None:
            region_mapping_inst.array_data = array_data
        
        return [region_mapping_inst]