예제 #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
예제 #2
0
    def deserialize_simulator(simulator_gid, storage_path):
        simulator_in_path = h5.path_for(storage_path, SimulatorH5,
                                        simulator_gid)
        simulator_in = Simulator()

        with SimulatorH5(simulator_in_path) as simulator_in_h5:
            simulator_in_h5.load_into(simulator_in)
            connectivity_gid = simulator_in_h5.connectivity.load()
            stimulus_gid = simulator_in_h5.stimulus.load()
            simulation_state_gid = simulator_in_h5.simulation_state.load()

        conn_index = dao.get_datatype_by_gid(connectivity_gid.hex)
        conn = h5.load_from_index(conn_index)

        simulator_in.connectivity = conn

        if simulator_in.surface:
            cortex_path = h5.path_for(storage_path, CortexH5,
                                      simulator_in.surface.gid)
            with CortexH5(cortex_path) as cortex_h5:
                local_conn_gid = cortex_h5.local_connectivity.load()
                region_mapping_gid = cortex_h5.region_mapping_data.load()

            region_mapping_index = dao.get_datatype_by_gid(
                region_mapping_gid.hex)
            region_mapping_path = h5.path_for_stored_index(
                region_mapping_index)
            region_mapping = RegionMapping()
            with RegionMappingH5(region_mapping_path) as region_mapping_h5:
                region_mapping_h5.load_into(region_mapping)
                region_mapping.gid = region_mapping_h5.gid.load()
                surf_gid = region_mapping_h5.surface.load()

            surf_index = dao.get_datatype_by_gid(surf_gid.hex)
            surf_h5 = h5.h5_file_for_index(surf_index)
            surf = CorticalSurface()
            surf_h5.load_into(surf)
            surf_h5.close()
            region_mapping.surface = surf
            simulator_in.surface.region_mapping_data = region_mapping

            if local_conn_gid:
                local_conn_index = dao.get_datatype_by_gid(local_conn_gid.hex)
                local_conn = h5.load_from_index(local_conn_index)
                simulator_in.surface.local_connectivity = local_conn

        if stimulus_gid:
            stimulus_index = dao.get_datatype_by_gid(stimulus_gid.hex)
            stimulus = h5.load_from_index(stimulus_index)
            simulator_in.stimulus = stimulus

        return simulator_in, simulation_state_gid
예제 #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, '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
예제 #4
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
예제 #5
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]