Skip to content
John Hoffer edited this page Oct 25, 2017 · 24 revisions

Creating new data formats

Here is an example of a minimal data format:

In bfly/CoreLayer/AccessLayer/QueryLayer/ImageLayer/SimpleSource.py:

import os
import cv2
import numpy as np
from Datasource import Datasource

class SimpleSource(Datasource):
    """ Not implemented
    """
    @staticmethod
    def load_tile(t_query):
        """load a single tile (image)
        Arguments
        -----------
        t_query: :class:`TileQuery`
            With file path and image position
        Returns
        -----------
        np.ndarray
            An image array
        """

        # Get the offset for the tile
        i_z, i_y, i_x = t_query.index_zyx
        file_name = "col={}_row={}.png".format(i_y, i_x)
        file_path = os.path.join(t_query.path, i_z, file_name)
        return cv2.imread(file_path, 0)

    @staticmethod
    def preload_source(t_query):
        """load info from example tile (image)
        Arguments
        -----------
        t_query: :class:`TileQuery`
            Only the file path is needed
        Returns
        --------
        dict
            * data-type -- \
numpy datatype of any given tile
            * block-size -- \
numpy Nx3 array of any given tile shape
            * dimensions -- \
numpy 3x1 array of full volume shape
        """

        # Should count files on filesystem
        N_FILES = np.uint32([3000, 25, 25])
        tile_0 = SimpleSource.load_tile(t_query)
        # Return empty if can't load first tile
        if not tile_0:
            return {}
        # Get properties from example tile
        FILE_SIZE = (1,) + tile_0.shape
        FULL_SIZE = FILE_SIZE * N_FILES
        DATA_TYPE = str(tile_0.dtype)

        # 'block-size', 'dimensions', and 'data-type'
        k_block = t_query.RUNTIME.IMAGE.BLOCK.NAME
        k_size = t_query.OUTPUT.INFO.SIZE.NAME
        k_type = t_query.OUTPUT.INFO.TYPE.NAME
        
        # Combine results with parent method
        common = Datasource.preload_source(t_query)
        return dict(common, **{
            k_block: np.uint32([FILE_SIZE]),
            k_size: np.uint32(FULL_SIZE),
            k_type: DATA_TYPE,
        })

Adding to the package

In bfly/CoreLayer/AccessLayer/QueryLayer/ImageLayer/__init__.py:

from SimpleSource import SimpleSource # Add this
...
__all__ += ['SimpleSource'] # Add this

In bfly/CoreLayer/AccessLayer/QueryLayer/TileQuery.py:

from ImageLayer import SimpleSource  # Add this
...
self.SOURCES = {
    'simple': SimpleSource      # Add this
    ...
}
...

In bfly/CoreLayer/AccessLayer/QueryLayer/UtilityLayer/Keywords.py:

...
self.IMAGE = NamelessStruct(
    SOURCE = NamedStruct('source-type',
        LIST = [
        ...
        'simple',           # Add this
        ],
    ...
    ),
...
)
...

Anything else

If you need extra properties from t_query, check the docs:

Version 2

Version 1

Clone this wiki locally