The QueryLayer

You can import from ./ or with bfly installed.

from bfly import QueryLayer

Or, in scripts in some directories:

# if './bfly/' in sys.path
from CoreLayer.AccessLayer import QueryLayer
# if './bfly/CoreLayer/' in sys.path
from AccessLayer import QueryLayer
# if './bfly/CoreLayer/AccessLayer' in sys.path
import QueryLayer

You can from QueryLayer import both ImageLayer and UtilityLayer.

# if './bfly/CoreLayer/AccessLayer' in sys.path
from QueryLayer import ImageLayer, UtilityLayer

QueryLayer

You can import QueryLayer from bfly, CoreLayer, and AccessLayer.

QueryLayer classes

class QueryLayer.DataQuery(*args, **keywords)[source]

Describe INPUT.METHODS.IMAGE_LIST requests

Parameters:
  • args (list) – unused
  • keywords (dict) –
    • OUTPUT.INFO.PATH (str) –
      the path to the given Datasource
    • INPUT.POSITION.Z (int) –
      the scaled resolution z start of the image request
    • INPUT.POSITION.Y (int) –
      the scaled resolution y start of the image request
    • INPUT.POSITION.X (int) –
      the scaled resolution x start of the image request
    • INPUT.POSITION.DEPTH (int) –
      the requested scaled depth of the image request
    • INPUT.POSITION.HEIGHT (int) –
      the requested scaled height of the image request
    • INPUT.POSITION.WIDTH (int) –
      the requested scaled width of the image request
    • INPUT.RESOLUTION.XY (int) –
      the number of halvings along the X and Y axes
    • INPUT.RESOLUTION.FORMAT (str) –
      the requested output image file format
    • INPUT.RESOLUTION.VIEW (str) –
      the requested coloring method for images
_basic_mime = 'image/{}'

default mime type for HTTP response

all_in_some(t_index)[source]

The tile region inside the scaled target volume

Parameters:t_index (numpy.ndarray) – The 3x1 tile offset of a tile from the origin
Returns:The 2x3 offset of target_bounds() from the origin of tile_origin repetitions of blocksize() such that either row gives an upper or lower corner of the total target volume within the requested t_index tile.
Return type:numpy.ndarray
some_in_all(t_origin, t_shape)[source]

The target subvolume needed by a given tile

Parameters:
  • t_origin (numpy.ndarray) – The 3x1 origin of the tile in the scaled target image
  • t_shape (numpy.ndarray) – The 3x1 shape of the tile in the scaled target image
Returns:

The 2x3 joining of t_origin and t_shape offset from target_origin() and clipped by target_shape() such that either row gives the upper or lower corner of the tile in the full scaled target volume.

Return type:

numpy.ndarray

blocksize

get the size of a Datasource tile

Returns:The 3x1 block value for this resolution
Return type:numpy.ndarray
blocksizes

get the sizes of each Datasource tile

Returns:The Nx3 block values from RUNTIME.IMAGE
Return type:numpy.ndarray
dtype

return actual numpy dtype object

Returns:from the type value from OUTPUT.INFO
Return type:numpy.dtype
key

return the key for the database

Returns:the path value from OUTPUT.INFO
Return type:str
scales

convert a number of halvings to a scale array

Returns:A 3x1 array of the target/source voxel ratios
Return type:numpy.ndarray
source_off

Get the data offset at full resolution

Returns:[z_off, y_off, x_off]
Return type:numpy.ndarray
source_origin

Full source offset of the requested origin

Returns:The 3x1 target_origin() multiplied by the voxel scales from scales().
Return type:numpy.ndarray
source_shape

Full source size of the requested volume

Returns:The 3x1 target_shape() multiplied by the voxel scales from scales().
Return type:numpy.ndarray
target_bounds

Scaled bounding box of the requested volume

Returns:The 2x3 joining of target_origin() and target_shape() transposed such that each row gives one of two bounding corners.
Return type:numpy.ndarray
target_off

Get the data offset at target resolution

Returns:[z_off, y_off, x_off]
Return type:numpy.ndarray
target_origin

Scaled target offset of the requested origin

Returns:The 3x1 block of Z, Y, and X in INPUT.POSITION
Return type:numpy.ndarray
target_shape

Scaled target size of the requested volume

Returns:The 3x1 block of DEPTH, HEIGHT, WIDTH values from INPUT.POSITION
Return type:numpy.ndarray
tile_bounds

Bounding box given by counting whole tiles

Returns:The 2x3 joining of the lowermost tile and the uppermost tile transposed such that a row gives the upper or lower limit of tiles.
Return type:numpy.ndarray
tile_shape

The ranges of tiles needed for the request

Returns:The 3x1 difference of tile_bounds() to give three numbers that count the number of tiles needed in all three directions.
Return type:numpy.ndarray
class QueryLayer.InfoQuery(*args, **keywords)[source]

Describe INPUT.METHODS.INFO_LIST requests

Parameters:
  • args (list) – unused
  • keywords (dict) –
    • OUTPUT.INFO.NAMES (list) –
      all answers to group and feature queries
    • OUTPUT.INFO.PATH (str) –
      the path to the given Datasource
    • OUTPUT.INFO.SIZE (numpy.ndarray) –
      3x1 for full volume shape
    • OUTPUT.INFO.CHANNEL (str) –
      The name of the most specific image group
    • OUTPUT.INFO.FORMAT (str) –
      The requested format for HTTP output
_basic_mime = 'text/plain'
_form = [{'indent': 4, 'cls': <class 'QueryLayer.InfoQuery.NumpyEncoder'>}, {'default_flow_style': False}]

Options for all _write

_write

list – All write functions for output formats

dump_dataset(all_channels)[source]

format result() with write

Parameters:all_channels (list) – Contains keywords for each channel
Returns:The result() formatted as a string
Return type:str
set_channel(channel)[source]

Change the query to a given channel

channel: dict
self.OUTPUT.INFO.CHANNEL: str self.OUTPUT.INFO.PATH: str
websocket_edit(msg={})[source]

Change internal state based on websocket message

Parameters:msg (dict) – The commands controlling the message
Returns:The values changed in the internal state
Return type:dict
websocket_result(action)[source]

Results to a websocket query

Parameters:action (str) – The type of result to send to client
Returns:The result to send to client
Return type:dict
channels

return the list of channels

Returns:All channel dictionaries
Return type:list
dump

format result() with write

Returns:The result() formatted as a string
Return type:str
get_format

get the index of the output format

Returns:index for _form or _write
Return type:int
result

get the answer to the info query

Returns:
  • dict – For the method INPUT.METHODS.META.NAME:
    • OUTPUT.INFO.PATH.NAME
      (str) OUTPUT.INFO.PATH.VALUE
    • OUTPUT.INFO.TYPE.NAME
      (str) OUTPUT.INFO.TYPE.VALUE
    • OUTPUT.INFO.SIZE.NAME
      (dict) OUTPUT.INFO.SIZE.VALUE
    • OUTPUT.INFO.CHANNEL.NAME
      (str) OUTPUT.INFO.CHANNEL.VALUE
  • list – For all methods INPUT.METHODS.GROUP_LIST: For the method INPUT.METHODS.FEATURE.NAME:
    • all (str) in OUTPUT.INFO.NAMES.VALUE
class QueryLayer.TileQuery(*args, **keywords)[source]

Describe requsts for tiles from a Datasource

Parameters:
  • args (list) –
    • (DataQuery) gives the full volume request
    • (numpy.ndarray) 3x1 tile offset of the given tile
    • (numpy.ndarray) 2x3 subregion within given tile
  • keywords (dict) – unused
_basic_mime = 'text/plain'
source_list

list – from RUNTIME .IMAGE.SOURCE.LIST

SOURCES

dict

  • source_list[0]: HDF5
  • source_list[1]: BossGrid
get_source(name)[source]

Gets a Datasource from SOURCES

Parameters:name (str) – The key to a Datasource subclass as listed in SOURCES
Returns:A Datasource subclass given by name
Return type:Datasource
all_scales

A scale array from RUNTIME.TILE

Returns:A 3x1 array of the target/source voxel ratios
Return type:numpy.ndarray
index_zyx

The tile offsets needed to get the tile

Returns:3x1 tile offset of the given tile
Return type:numpy.ndarray
key

return the key for the database

Returns:the path value from path() joined with the tile_values from index_zyx() and all_scales()
Return type:str
my_source

return the source that loads this tile

Returns:the source value from RUNTIME.IMAGE as an actual Datasource that can load a TileQuery.
Return type:Datasource
pixels_kji

The pixels bounds needed from the tile

Returns:The 2x3 subregion within the given tile
Return type:numpy.ndarray
preload_source

Get a valid_source() with a measured size

Returns:Empty if no valid_source()
  • RUNTIME .CACHE.META.NAME
    (int) – The byte size of this dictionary
  • RUNTIME .IMAGE.SOURCE.NAME
    (str) – The Datasource subclass
  • RUNTIME .IMAGE.BLOCK.NAME
    (numpy.ndarray) – 3x1 for any give tile shape
  • OUTPUT .INFO.TYPE.NAME
    (str) – numpy dtype of any given tile
  • OUTPUT .INFO.SIZE.NAME
    (numpy.ndarray) – 3x1 for full volume shape
Return type:dict
source_bounds

The bounds of query in full coordinates

Returns:2x3 full source pixel bounds in tile
Return type:numpy.ndarray
source_tile_bounds

The full image bounds of the tile

Returns:2x3 full image pixel bounds
Return type:numpy.ndarray
target_bounds

The bounds of query in target coordinates

Returns:
  • numpy.ndarray
  • 2x3 target pixel boundaries
target_origin

The origin of query in target coordinates

Returns:3x1 target pixel lower bound
Return type:numpy.ndarray
target_tile_bounds

The scaled target bounds of the tile

Returns:2x3 scaled target pixel bounds
Return type:numpy.ndarray
tile

Load the requested tile from the source

Returns:The full image loaded by my_source()
Return type:numpy.ndarray
valid_source

Call preload_source for all source_list

Returns:Empty if no Datasource.preload_source() can find this filname to give a valid volume.
  • RUNTIME .IMAGE.SOURCE.NAME
    (str) – The Datasource subclass
  • RUNTIME .IMAGE.BLOCK.NAME
    (numpy.ndarray) – 3x1 for any give tile shape
  • OUTPUT .INFO.TYPE.NAME
    (str) – numpy dtype of any given tile
  • OUTPUT .INFO.SIZE.NAME
    (numpy.ndarray) – 3x1 for full volume shape
Return type:dict

Query base class

class QueryLayer.Query(*args, **keywords)[source]

Describe content of AccessLayer requests

Parameters:
  • args (list) – See TileQuery for use of positional arguments
  • keywords (dict) – Each derived class may use any keyword arguments given keys each match a NAME of a NamedStruct`in :class:`INPUT, RUNTIME, or OUTPUT.
_basic_mime = 'text/plain'

default mime type for HTTP response

INPUT

INPUT

RUNTIME

RUNTIME

OUTPUT

OUTPUT

keywords

dict – Taken from keywords argument

check_list(whitelist, value, term)[source]

Checks that a value is in a given list

Parameters:
  • whitelist (list-like) – The list of desired values
  • value (anything) – The value should be in the whitelist
  • term (str) – The name of the attribute to test
check_vector(value, term='list', dims=3)[source]

Checks that a vector has three dimensions

Parameters:
  • term (str) – The name of the attribute to test
  • value (list-like) – The value should be a list or array
  • dims (int) – The desired number of dimensions
set_key(struct, key, empty='')[source]

Copy the key from keywords to the structure

Parameters:
  • struct (NamelessStruct) – Where to store a value from keywords
  • key (str) – The key to transfer from keywords
  • empty (anything) – The value if keywords has no key``and the ``key has no default in the struct
update_keys(keywords)[source]

Update stored keywords

Parameters:keywords (dict) – Updates stored keywords
update_source(keywords)[source]

Set all attribute values to match keywords

Parameters:keywords (dict) –
  • RUNTIME .IMAGE.SOURCE.NAME
    (str) – The subclass of Datasource
  • RUNTIME .IMAGE.BLOCK.NAME
    (numpy.ndarray) – 3x1 for any given tile shape
  • OUTPUT .INFO.TYPE.NAME
    (str) – numpy dtype of any given tile
  • OUTPUT .INFO.SIZE.NAME
    (numpy.ndarray) – 3x1 for full volume shape

Keyword arguments only for HDF5

  • RUNTIME .IMAGE.SOURCE.HDF5.OUTER.NAME
    (str) – The direct filename to an hdf5 file
  • RUNTIME .IMAGE.SOURCE.HDF5.INNER.NAME
    (str) – The dataset in the file with image data

Keyword arguments if editable * RUNTIME .IMAGE.MERGE.NAME

lil_matrix – The matrix of merged ids
  • RUNTIME .IMAGE.MERGE.NAME
    lil_matrix – The matrix of split regions
edit_path

return the path to edit the volume

Returns:the path under the EDIT_PATH root
Return type:str
is_data

Checks whether the method requests images

Returns:
Return type:bool
is_dataset

Checks if requesting dataset info

Returns:
Return type:bool
is_group

Checks whether the method requests groups

Returns:
Return type:bool
is_websocket

Checks whether websocket method

Returns:
Return type:bool
key

return the key for the database

Returns:the path value from OUTPUT.INFO
Return type:str
mime_type

Gets the mime type for the file_type

The mime type is from INPUT.IMAGE if is_data(), or INPUT.INFO otherwise.

Returns:The mime type from mimetypes.types_map
Return type:str
path

return the path to the whole volume

Returns:the path value from OUTPUT.INFO
Return type:str

ImageLayer

You can import ImageLayer from bfly, CoreLayer, AccessLayer, and QueryLayer.

ImageLayer classes

class ImageLayer.HDF5[source]

Loads images from hdf5 files

_meta_files = ['.json']

All extensions of files pointing to h5

_read

list – All load functions for _meta_files

static get_details(h5_info, file_dict)[source]

Get all needed h5 file info from a pointer file

Parameters:file_dict (dict) – Contains keys for INNER, OUTER, and OFF values
Returns:All INNER, OUTER, OFF values in a flat list
Return type:list
static load_file(h5_file)[source]

Load the needed volume from a single h5 File

Parameters:t_query (TileQuery) – With file path and image position
static load_info(t_query)[source]

Gets the h5 volume filename and datapath

If the t_query path has an extension in the _meta_files and the file contains RUNTIME.IMAGE.SOURCE.HDF5.OUTER.NAME and RUNTIME.IMAGE.SOURCE.HDF5.INNER.NAME keys, then the values of those keys are returned. If any of those statements is not true, then the original t_query path is returned along with the default dataset given by RUNTIME.IMAGE.SOURCE.HDF5.INNER.VALUE.

Parameters:t_query (TileQuery) – Only the file path is needed
Returns:
  • The direct filename to an hdf5 file
  • The datset in the file with image data
Return type:list
static load_tile(t_query)[source]

load a single tile (image)

Gets the image path from the TileQuery.RUNTIME. IMAGE attribute.

Gets the position of the image with the whole volume from TileQuery.all_scales(), TileQuery.tile_origin(), and TileQuery.blocksize().

Parameters:t_query (TileQuery) – With file path and image position
Returns:An image array that may be as large as an entire full resolution slice of the whole hdf5 volume. Based on the value of TileQuery.all_scales(), this array will likely be downsampled by to a small fraction of the full tile resolution.
Return type:np.ndarray
static preload_source(t_query)[source]

load info from example tile (image)

Calls valid_path() to get filename and inner dataset path for the full h5 image volume.

Then gets three needed values from the given path from the TileQuery t_query

Parameters:t_query (TileQuery) – Only the file path is needed
Returns:Will be empty if valid_path() findsthis filname to not give a valid h5 volume.
  • RUNTIME .IMAGE.BLOCK.NAME
    (numpy.ndarray) – 3x1 for any give tile shape
  • OUTPUT .INFO.TYPE.NAME
    (str) – numpy dtype of any given tile
  • OUTPUT .INFO.SIZE.NAME
    (numpy.ndarray) – 3x1 for full volume shape
Return type:dict
static valid_path(t_query)[source]

Check if filename can access h5 data

The filename can be a path to a json file that lists an h5 file and dataset path, or the filename can be a direct path to an h5 file. In either case the ‘outer’ file path directly to the h5 file and the ‘inner’ dataset path will be returned.

Parameters:t_query (TileQuery) – Only the file path is needed
Returns:Empty if not a valid h5 volume
  • RUNTIME .IMAGE.SOURCE.HDF5.OUTER.NAME
    (str) – The direct filename to an hdf5 file
  • RUNTIME .IMAGE.SOURCE.HDF5.INNER.NAME
    (str) – The datset in the file with image data
Return type:dict

Datasource base class

class ImageLayer.Datasource[source]

Loads images from files on the server

static load_tile(t_query)[source]

load a single tile (image)

Parameters:t_query (TileQuery) – With file path and image position
static preload_source(t_query)[source]

load info from example tile (image)

Parameters:t_query (TileQuery) – Only the file path is needed
Returns:
  • RUNTIME .IMAGE.BLOCK.NAME
    (numpy.ndarray) – 3x1 for any given tile shape
  • OUTPUT .INFO.TYPE.NAME
    (str) – numpy dtype of any given tile
  • OUTPUT .INFO.SIZE.NAME
    (numpy.ndarray) – 3x1 for full volume shape
  • OUTPUT .IMAGE.MERGE.NAME
    (lil_matrix) – A matrix of merged ids
  • OUTPUT .IMAGE.SPLIT.NAME
    (lil_matrix) – A matrix of plit regions
  • OUTPUT .IMAGE.ERROR.NAME
    dict – All error messages
Return type:dict