src/napari_tissuemaps_interface/napari_tissuemaps_interface.py
def tissuemaps_interface(url, token, experiment_id, channel_layer_id):
    # pylint: disable=R0914
    """
    Function which reads a channel layer from the TissueMAPs API.
    Given NAPARI_OCTREE==1, it returns a multi-scale (pyramidal) image as a delayed Dask
    array. Otherwise, it returns a high-resolution image as numpy array.
    Note: The later will download the full image into memory!

    :param url: The base url of the TissueMAPs server
    :param token: The authentication token obtained by the TissueMAPs server
    :param experiment_id: The TissueMAPs experiment id
    :param channel_layer_id: The TissueMAPs channel layer id

    :return: A tiled multi-scale image if NAPARI_OCTREE==1,
             otherwise a numpy array with the high-resolution image
    """
    class LazyTiledTMArray(LazyArray):
        # pylint: disable=R0903
        """
        A numpy-like array which lazily loads tiles from a TissueMAPS server.
        """
        def __init__(self, shape, dtype, tile_size, zoom):
            super().__init__(shape, dtype, tile_size)
            self.zoom = zoom

        @dask.delayed
        def read_tile(self, y_tile, x_tile):
            '''
            Reads a tile from a TissuMAPS server
            :param y_tile: the y coordinate of the tile
            :param x_tile: the x coordinate of the tile
            :return: numpy array with the the cooresponding tile updated
            '''
            api_url = (
                'api/experiments/' + str(experiment_id) + '/channel_layers/' +
                str(channel_layer_id) + '/tiles'
            )
            tiles_resp = http_get(url, api_url, token, x=x_tile, y=y_tile, z=self.zoom)
            img = PIL.Image.open(BytesIO(tiles_resp.content))
            data = np.zeros((self.tile_size, self.tile_size))
            data[:img.size[1], :img.size[0]] = np.asarray(img)
            return data

    channel_layers = get_data(
        url,
        'api/experiments/' + str(experiment_id) + '/channel_layers',
        token
    )

    channel_layer = next(
        item for item in channel_layers if item["id"] == channel_layer_id
    )

    image_data = {}
    image_data['image_height'] = channel_layer['image_size']['height']
    image_data['image_width'] = channel_layer['image_size']['width']
    image_data['max_zoom'] = channel_layer['max_zoom']

    pyramid = []
    for zoom in reversed(range(1, image_data['max_zoom'] + 1)):
        if image_data['image_width'] < TILE_SIZE:
            break

        array = LazyTiledTMArray(
            shape=(image_data['image_height'], image_data['image_width']),
            dtype=np.uint8,
            tile_size=TILE_SIZE,
            zoom=zoom
        )

        if 'NAPARI_OCTREE' not in os.environ or os.environ['NAPARI_OCTREE'] != '1':
            # given we don't have a spatial index (e.g. an octree), we can
            # directly return the highest resolution.
            # NOTE: this will download the full image into memory!
            array = np.asarray(array)
            return array

        pyramid.append(array)
        image_data['image_height'] //= 2
        image_data['image_width'] //= 2

    return pyramid

    Function which reads a channel layer from the TissueMAPs API.
    Given NAPARI_OCTREE==1, it returns a multi-scale (pyramidal) image as a delayed Dask
    array. Otherwise, it returns a high-resolution image as numpy array.
    Note: The later will download the full image into memory!

    :param url: The base url of the TissueMAPs server
    :param token: The authentication token obtained by the TissueMAPs server
    :param experiment_id: The TissueMAPs experiment id
    :param channel_layer_id: The TissueMAPs channel layer id

    :return: A tiled multi-scale image if NAPARI_OCTREE==1,
             otherwise a numpy array with the high-resolution image
    
Function which reads a channel layer from the TissueMAPs API.
Given NAPARI_OCTREE==1, it returns a multi-scale (pyramidal) image as a delayed Dask
array. Otherwise, it returns a high-resolution image as numpy array.
Note: The later will download the full image into memory!
Function which reads a channel layer from the TissueMAPs API . Given NAPARI_OCTREE == 1 it returns a multi - scale ( pyramidal ) image as a delayed Dask array . Otherwise it returns a high - resolution image as numpy array . Note : The later will download the full image into memory!
def tissuemaps_interface(url, token, experiment_id, channel_layer_id):
    class LazyTiledTMArray(LazyArray):
        def __init__(self, shape, dtype, tile_size, zoom):
            super().__init__(shape, dtype, tile_size)
            self.zoom = zoom
        @dask.delayed
        def read_tile(self, y_tile, x_tile):
            api_url = (
                'api/experiments/' + str(experiment_id) + '/channel_layers/' +
                str(channel_layer_id) + '/tiles'
            )
            tiles_resp = http_get(url, api_url, token, x=x_tile, y=y_tile, z=self.zoom)
            img = PIL.Image.open(BytesIO(tiles_resp.content))
            data = np.zeros((self.tile_size, self.tile_size))
            data[:img.size[1], :img.size[0]] = np.asarray(img)
            return data
    channel_layers = get_data(
        url,
        'api/experiments/' + str(experiment_id) + '/channel_layers',
        token
    )
    channel_layer = next(
        item for item in channel_layers if item["id"] == channel_layer_id
    )
    image_data = {}
    image_data['image_height'] = channel_layer['image_size']['height']
    image_data['image_width'] = channel_layer['image_size']['width']
    image_data['max_zoom'] = channel_layer['max_zoom']
    pyramid = []
    for zoom in reversed(range(1, image_data['max_zoom'] + 1)):
        if image_data['image_width'] < TILE_SIZE:
            break
        array = LazyTiledTMArray(
            shape=(image_data['image_height'], image_data['image_width']),
            dtype=np.uint8,
            tile_size=TILE_SIZE,
            zoom=zoom
        )
        if 'NAPARI_OCTREE' not in os.environ or os.environ['NAPARI_OCTREE'] != '1':
            array = np.asarray(array)
            return array
        pyramid.append(array)
        image_data['image_height'] //= 2
        image_data['image_width'] //= 2
    return pyramid
def tissuemaps_interface ( url , token , experiment_id , channel_layer_id ) : class LazyTiledTMArray ( LazyArray ) : """
        A numpy-like array which lazily loads tiles from a TissueMAPS server.
        """ def __init__ ( self , shape , dtype , tile_size , zoom ) : super ( ) . __init__ ( shape , dtype , tile_size ) self . zoom = zoom @ dask . delayed def read_tile ( self , y_tile , x_tile ) : '''
            Reads a tile from a TissuMAPS server
            :param y_tile: the y coordinate of the tile
            :param x_tile: the x coordinate of the tile
            :return: numpy array with the the cooresponding tile updated
            ''' api_url = ( 'api/experiments/' + str ( experiment_id ) + '/channel_layers/' + str ( channel_layer_id ) + '/tiles' ) tiles_resp = http_get ( url , api_url , token , x = x_tile , y = y_tile , z = self . zoom ) img = PIL . Image . open ( BytesIO ( tiles_resp . content ) ) data = np . zeros ( ( self . tile_size , self . tile_size ) ) data [ : img . size [ 1 ] , : img . size [ 0 ] ] = np . asarray ( img ) return data channel_layers = get_data ( url , 'api/experiments/' + str ( experiment_id ) + '/channel_layers' , token ) channel_layer = next ( item for item in channel_layers if item [ "id" ] == channel_layer_id ) image_data = { } image_data [ 'image_height' ] = channel_layer [ 'image_size' ] [ 'height' ] image_data [ 'image_width' ] = channel_layer [ 'image_size' ] [ 'width' ] image_data [ 'max_zoom' ] = channel_layer [ 'max_zoom' ] pyramid = [ ] for zoom in reversed ( range ( 1 , image_data [ 'max_zoom' ] + 1 ) ) : if image_data [ 'image_width' ] < TILE_SIZE : break array = LazyTiledTMArray ( shape = ( image_data [ 'image_height' ] , image_data [ 'image_width' ] ) , dtype = np . uint8 , tile_size = TILE_SIZE , zoom = zoom ) if 'NAPARI_OCTREE' not in os . environ or os . environ [ 'NAPARI_OCTREE' ] != '1' : array = np . asarray ( array ) return array pyramid . append ( array ) image_data [ 'image_height' ] //= 2 image_data [ 'image_width' ] //= 2 return pyramid
Function which reads a channel layer from the TissueMAPs API.
Function which reads a channel layer from the TissueMAPs API .
# pylint: disable=R0914 """
    Function which reads a channel layer from the TissueMAPs API.
    Given NAPARI_OCTREE==1, it returns a multi-scale (pyramidal) image as a delayed Dask
    array. Otherwise, it returns a high-resolution image as numpy array.
    Note: The later will download the full image into memory!

    :param url: The base url of the TissueMAPs server
    :param token: The authentication token obtained by the TissueMAPs server
    :param experiment_id: The TissueMAPs experiment id
    :param channel_layer_id: The TissueMAPs channel layer id

    :return: A tiled multi-scale image if NAPARI_OCTREE==1,
             otherwise a numpy array with the high-resolution image
    """ # pylint: disable=R0903 """
        A numpy-like array which lazily loads tiles from a TissueMAPS server.
        """ '''
            Reads a tile from a TissuMAPS server
            :param y_tile: the y coordinate of the tile
            :param x_tile: the x coordinate of the tile
            :return: numpy array with the the cooresponding tile updated
            ''' # given we don't have a spatial index (e.g. an octree), we can # directly return the highest resolution. # NOTE: this will download the full image into memory!
A tiled multi-scale image if NAPARI_OCTREE==1,
otherwise a numpy array with the high-resolution image
A tiled multi - scale image if NAPARI_OCTREE == 1 otherwise a numpy array with the high - resolution image
url
The base url of the TissueMAPs server
The base url of the TissueMAPs server
token
The authentication token obtained by the TissueMAPs server
The authentication token obtained by the TissueMAPs server
experiment_id
The TissueMAPs experiment id
The TissueMAPs experiment id
channel_layer_id
The TissueMAPs channel layer id
The TissueMAPs channel layer id