Reading and writing

The format of a file is worked out from the file when it is read, and from its extension when it is written.

Whole files

These functions handle one file per call and wait for it. They suit a single file. To go through many images, use the batches below, as the performance guide explains.

vitrio.query_descriptor(key, readers=None)

Get the shape and data type of what an image file holds.

The extents are those of the whole file, slowest axis first, so a stack reports the axis it stacks along before the shape of one of its images. get_core_extents leaves that axis out, which is the shape a batch destination carries beside its leading extent:

descriptor = query_descriptor(path)
destination = np.empty(
        (len(locations), *get_core_extents(descriptor)),
        dtype=np.float32
)
Parameters:
  • key – The file to ask about, as its key in readers.

  • readers – Where the file becomes a reader. Defaults to a provider that opens the file at the path key is.

Returns:

The descriptor of the file.

Return type:

ImageDescriptor

vitrio.read(location, readers=None, data_type=None, out=None)

Read an image file, or one image or volume of a stack.

Each call opens the file and waits for the read, which suits a single file. A program that goes through many images reads them in batches instead, with read_batch_async or read_patches_async, or with vitrio.data to go through a whole dataset. One call per image is many times slower than one call per batch.

Parameters:
  • location – The file to read, as its key in readers, or an ImageLocation naming one image or volume of it. A string is a key and only a key: read “3@stack.mrc” by handing it to ImageLocation.from_string first.

  • readers – Where the file becomes a reader. Defaults to a provider that opens the file at the path the key is.

  • data_type – The type to read the values as. Defaults to the one the file holds. Cannot be given with out, whose own type is the one the values are read as.

  • out – The array to read into. Its shape has to be that of what location names. Defaults to a new Array.

Returns:

out when it is given. Otherwise an Array over memory of its own, which numpy and whatever takes DLPack use without a copy.

Raises:

ValueError – If data_type and out are both given.

vitrio.write_single(array, path, formats=None, data_type=None, transfers=None, metadata=None)

Write an array to an image file as one image or volume.

Returns once the file has been flushed to the storage. A program that writes many files, or one file a batch at a time, uses write_batch_async or write_patches_async over a saver instead, so that it does not wait for each file.

Parameters:
  • array – The values to write.

  • path – The file to write them to.

  • formats – The formats to choose from. Defaults to the bundled ones.

  • data_type – The type to store the values as. Defaults to the one the array already carries.

  • transfers – The threads that carry out the writes of the file. Defaults to a pool shared by the process.

  • metadata – What the file states beyond its shape and data type, as a mapping of labels to values such as read_metadata returns. Its format writes what of it it can carry and leaves out the rest. Defaults to nothing.

Raises:
  • TypeError – If a label of metadata is no str, or if a value of it is not an int, a float or a str. A bool is none of them.

  • ValueError – If a label of metadata has no prefix and is no standard field, or if a standard field is given a value of another type.

vitrio.write_stack(array, path, formats=None, data_type=None, transfers=None, metadata=None)

Write an array to an image file as a stack of images or volumes.

The leading extent of the array is the axis the file stacks along and the rest are the shape of one image or volume.

Returns once the file has been flushed to the storage. To write a stack a batch at a time, or many stacks at once, use write_batch_async over a saver instead.

Parameters:
  • array – The values to write.

  • path – The file to write them to.

  • formats – The formats to choose from. Defaults to the bundled ones.

  • data_type – The type to store the values as. Defaults to the one the array already carries.

  • transfers – The threads that carry out the writes of the file. Defaults to a pool shared by the process.

  • metadata – What the file states beyond its shape and data type, as a mapping of labels to values such as read_metadata returns. Its format writes what of it it can carry and leaves out the rest. Defaults to nothing.

Raises:
  • TypeError – If a label of metadata is no str, or if a value of it is not an int, a float or a str. A bool is none of them.

  • ValueError – If a label of metadata has no prefix and is no standard field, or if a standard field is given a value of another type.

vitrio.write(array, path, descriptor, formats=None, transfers=None, metadata=None)

Write an array to an image file as a descriptor states.

The core rank of the descriptor is what makes the file a stack and not a single image or volume, and its data type is the one the file stores.

Returns once the file has been flushed to the storage, as write_single does.

Parameters:
  • array – The values to write. Its shape has to be the extents of descriptor.

  • path – The file to write them to.

  • descriptor – What the file holds.

  • formats – The formats to choose from. Defaults to the bundled ones.

  • transfers – The threads that carry out the writes of the file. Defaults to a pool shared by the process.

  • metadata – What the file states beyond its shape and data type, as a mapping of labels to values such as read_metadata returns. Its format writes what of it it can carry and leaves out the rest. Defaults to nothing.

Raises:
  • TypeError – If a label of metadata is no str, or if a value of it is not an int, a float or a str. A bool is none of them.

  • ValueError – If a label of metadata has no prefix and is no standard field, or if a standard field is given a value of another type.

Metadata

What a file states beyond its shape and data type, such as its pixel size, is read apart from its values, and given when a file is written.

vitrio.read_metadata(path, formats=None)

Read what an image file states beyond its shape and data type.

The result is a dict of values under labels. Nothing has to be in it: what the file does not state is absent, and so is what its format does not read. So ask for a label with get:

pixel_size = vio.read_metadata(path).get('pixel_size_x')

A label with no dot in it is a standard field, which means the same whatever the format of the file:

  • pixel_size_x, pixel_size_y and pixel_size_z, each a float: the distance between two consecutive samples along that axis, in ångströms. Of the axes of one image or volume, x is the last, y the one before it and z the one before that, so an image has no z, whether or not its file stacks several.

Any other label has a prefix that says whose it is, such as the format that defines it:

  • mrc.labels, a str: the labels of an MRC file, one a line, in which software records what it did to the file.

  • tiff.image_description, tiff.software and tiff.date_time, each a str: what a TIFF file says it shows, the software it says made it, and when, as YYYY:MM:DD HH:MM:SS.

A text is read as UTF-8, and what of it is not becomes the replacement character.

The file is not opened for reading its values, and the GIL is released while it is read.

Parameters:
  • path – The file to read.

  • formats – The formats to choose from. Defaults to the bundled ones.

Returns:

An int, a float or a str under each label, sorted by label. It is yours to change, and what the writes take as metadata.

Return type:

dict

Batches and patches

A batch is read or written on the threads of a loader or a saver, and its images may come from any number of files. These functions return at once, so a program starts the next batch before it works on the current one. This is the fastest way to read and write many images. vitrio.loader() and vitrio.saver() make the loader and the saver.

vitrio.read_batch_async(loader: vitrio._binding.ImageLoader, destination: ndarray[device='cpu'], locations: collections.abc.Sequence[vitrio._binding.ImageLocation]) → vitrio._binding.Completion

Read one image or volume per location into a batch.

Returns before the reads are done. Slot i of destination receives the image locations[i] names, or the whole file when it names no index in a stack. A batch may not mix the two. The images may come from any number of files, and the reads run on the loader’s executor.

Parameters:
  • loader – The ImageLoader the reads are dispatched to.

  • destination – Where the images land, as any host array, written where it is. Its leading extent is the batch size and the rest are the shape of one image. Its data type is the one the values are read as.

  • locations – A list of ImageLocation, one for each slot, in order.

Returns:

Ready once every image is in or has failed. It keeps destination alive, and destroying it waits for the reads. Its get raises the first failure, such as an index its stack does not hold.

Return type:

Completion

Raises:

ValueError – If the leading extent of destination is not the number of locations, or if the locations mix those that name an index with those that name none.

vitrio.read_patches_async(loader, destination, location, centres)

Crop a batch of equally sized patches out of one image.

Returns before the reads are done. Each slot of destination receives the patch around one centre, which lands at index extent // 2 within it. A patch reaching past the edge of the image is read as far as the image goes, and the rest of its slot keeps what destination held beforehand.

Parameters:
  • loader – Where the reads are dispatched.

  • destination – Where the patches land. Its leading extent is the batch size and the rest are the shape of one patch.

  • location – The image every patch is cropped from.

  • centres – The centre of each patch: an IndexTable, an array of integers with one row per centre, or any sequence of sequences. Each centre has the rank of one patch.

Returns:

Ready once every patch has been read or has failed. It keeps destination alive, and destroying it waits for the reads.

Return type:

Completion

vitrio.write_batch_async(saver: vitrio._binding.ImageSaver, source: ndarray[device='cpu', writable=False], locations: collections.abc.Sequence[vitrio._binding.ImageLocation]) → vitrio._binding.Completion

Write one image or volume per location out of a batch.

Returns before the writes are done. Slot i of source goes where locations[i] names. The files have to be declared with their final shape beforehand, on the provider the saver writes through, so a stack can be written a batch at a time, and one batch may span several stacks.

Parameters:
  • saver – The ImageSaver the writes are dispatched to.

  • source – The values, as any host array, read from where it is. Its leading extent is the batch size and the rest are the shape of one image.

  • locations – A list of ImageLocation, one for each slot, in order.

Returns:

Ready once every image is written or has failed. It keeps source alive, and destroying it waits for the writes. Its get raises the first failure.

Return type:

Completion

Raises:

ValueError – If the leading extent of source is not the number of locations, or if the locations mix those that name an index with those that name none.

vitrio.write_patches_async(saver, source, location, centres)

Write a batch of equally sized patches into one image.

The mirror of read_patches_async: each slot of source is written where a read around its centre would have read it from. The part of a patch past the edge of the image is not written. Patches of one call must not overlap, and what no patch covers keeps what the format leaves there. Returns before the writes are done.

The file has to be declared with its final shape beforehand, on the provider saver writes through, so an image or volume can be written a batch of patches at a time: the tiles of a sliding window, for one.

Parameters:
  • saver – Where the writes are dispatched.

  • source – The patches. Its leading extent is the batch size and the rest are the shape of one patch.

  • location – The image every patch is written into.

  • centres – The centre of each patch, as read_patches_async takes them.

Returns:

Ready once every patch has been written or has failed. It keeps source alive, and destroying it waits for the writes.

Return type:

Completion

class vitrio.Completion

What an asynchronous read or write returns to wait on.

It is ready once all the work behind it has run, successfully or not. Call get to wait for it and to learn whether anything failed. The completion keeps the arrays the work reads or writes alive, and destroying one that is not ready waits for it.

get(self) → None

Block until the work is done, then raise the first failure.

The GIL is released meanwhile.

Raises:

Exception – Whatever the first piece of work to fail raised.

property is_ready

Whether the work is done, successfully or not.

wait(self) → None

Block until the work is done.

It does not raise whatever failed. The GIL is released meanwhile.