neuroglancer_scripts.accessor module

Low-level file access to Neuroglancer pre-computed datasets.

The central component here is the Accessor base class. Use get_accessor_for_url() for instantiating a concrete accessor object.

class neuroglancer_scripts.accessor.Accessor[source]

Bases: object

Access a Neuroglancer pre-computed pyramid.

An accessor encapsulates access to the files that form a Neuroglancer pre-computed dataset. It works with sequences of bytes, without interpreting the file contents.

You can inherit from this class in order to implement a new accessor (see FileAccessor, HttpAccessor).

can_read = False

This accessor is able to read data.

can_write = False

This accessor is able to write data.

fetch_chunk(key, chunk_coords)[source]

Fetch a chunk from the pyramid as a bytes buffer.

Parameters:
  • key (str) – the scale’s key

  • chunk_coords (tuple) – tuple of the chunk coordinates (xmin, xmax, ymin, ymax, zmin, zmax)

Returns:

the data for the chunk (uncompressed for gzip)

Return type:

bytes

Raises:
fetch_file(relative_path)[source]

Fetch a file relative to the precomputed pyramid’s base directory.

Parameters:

relative_path (str) – path to the file relative to the base directory of the pyramid

Returns:

contents of the fetched file

Return type:

bytes

Raises:
file_exists(relative_path)[source]

Test existence of a file relative to the base directory.

Parameters:

relative_path (str) – path to the file relative to the base directory of the pyramid

Returns:

True if the file exists

Return type:

bool

Raises:
store_chunk(buf, key, chunk_coords, mime_type='application/octet-stream', overwrite=False)[source]

Store a chunk in the pyramid from a bytes buffer.

Parameters:
  • key (str) – the scale’s key

  • chunk_coords (tuple) – tuple of the chunk coordinates (xmin, xmax, ymin, ymax, zmin, zmax)

  • mime_type (str) – MIME type of the chunk data

Raises:
store_file(relative_path, buf, mime_type='application/octet-stream', overwrite=False)[source]

Store a file relative to the precomputed pyramid’s base directory.

Parameters:
  • relative_path (str) – path to the file relative to the base directory of the pyramid

  • buf (bytes) – the contents of the file to be stored

  • mime_type (str) – MIME type of the file

  • overwrite (bool) – whether to allow overwriting an existing file

Raises:
exception neuroglancer_scripts.accessor.DataAccessError[source]

Bases: Exception

Exception indicating an error with access to a data resource.

exception neuroglancer_scripts.accessor.URLError[source]

Bases: Exception

Exception indicating an invalid or unsupported URL.

neuroglancer_scripts.accessor.add_argparse_options(parser, write_chunks=True, write_files=True)[source]

Add command-line options for file access.

Parameters:
  • parser (argparse.ArgumentParser) – an argument parser

  • write_chunks (bool) – whether to add options for chunk writing

  • write_files (bool) – whether to add options for file writing

The accesor options can be obtained from command-line arguments with add_argparse_options() and passed to get_accessor_for_url():

import argparse
parser = argparse.ArgumentParser()
add_argparse_options(parser)
args = parser.parse_args()
get_accessor_for_url(url, vars(args))
neuroglancer_scripts.accessor.convert_file_url_to_pathname(url)[source]

Convert a local file:// URL or plain pathname to a pathname.

Parameters:

url (str) – the URL or pathname to convert into a local pathname

Returns:

a local pathname suitable for open()

Return type:

str

Raises:

URLError – if the provided string looks like a URL but is not a valid local file:/// URL

neuroglancer_scripts.accessor.get_accessor_for_url(url, accessor_options={})[source]

Create an accessor object from a URL or pathname.

Parameters:
  • url (str) – URL or plain local pathname to the pyramid

  • accessor_options (dict) – options passed to the accessor as kwargs.

Returns:

an instance of a sub-class of Accessor

Return type:

Accessor