Skip to main content

TiffSlide-Xarray

A simple integration library between tiffslide and xarray.

Installation

Install from pypi:

pip install tiffslide-xarray

Usage

This library hooks into xarray's extension system as a backend engine. So it can be used even without importing.

from xarray import open_dataset

slide_level0 = open_dataset("input.svs")

The library automatically recoginizes "tiff" and "svs" files. If required, the "engine" keyword can force usage:

slide_level0 = open_dataset("input.another_extension", engine="tiffslide")

Tifflside uses the fsspec and tifffiles packages to open files. Options to these libraries can be passed using the "storage_options" and "tifffile_options" keyword arguments.

slide_level0 = open_dataset("s3://input.svs", storage_options={"s3": ... })

By default, the level 0 of the the file is read. Other levels can be read by using the "level" keyword.

slide_level1 = open_dataset("input.svs", level=1)

Negative levels are allowed to allow indexing from end of the level array.

slide_level_last = open_dataset("input.svs", level=-1)

Opening All Levels

To open all the levels in the slide, use the "open_all" to return a datatree of the slide.

from tiffslide_xarray import open_all_levels

slide = open_all_levels("input.svs")

The returned datatree places level0 at the root group, and places subsequent levels at the f"level{n}" group.

Data and MetaData Model

The data for each slide is accessible at "image,"

slide_level0.image
slide_level0["image"]

Coordinates for the x, y (and z if it exists) dimensions are added, in units of "px" of the level 0 slide. This makes the cordinates between different levels directly comparable. The library assumes there are three channels, in the order of (r, g, b).

>>> slide_level0.x
[0, 1, 2...]
>>> slide_level0.y
[0, 1, 2...]

All the metadata from the slide is stored in the dataset attributes. The source file name is added to the metadata of both the 'image' array and the dataset. If found in the metadata, the microns per pixel (mpp) is stored in the "mpp" attributes of the 'x' and 'y' coordinates.

Lazy Loading

Slides are lazy loaded which makes the initial open very quick, and loading of small regions is quick (but not cached). Loading of large regions can be slow. To manage this, be sure to call "load" on datasets to bring them into memory if they will be accessed multiple times.

For example, this code will execute two costly reads:

roi = slide_level1.sel(x=slice(10000, 40000), x=slice(5000, 20000))  # select a large ROI

roi2 = 2.0 * roi   # first read
roi2 = 3.0 * roi   # second read

Calling "load" on "roi" or "slide_level1" solves this problem.

roi = slide_level1.sel(x=slice(10000, 40000), x=slice(5000, 20000))  # select a large ROI

roi = roi.load() # load the ROI into memory for subsequent processing.
roi2 = 2.0 * roi   # no read
roi2 = 3.0 * roi   # no read

the WSI accessor (experimental)

This package extends xarray with a new accessor, called "wsi," which can be enabled with this import.

import tiffslide_xarray.accessor

Eventually, this import will be made automatically, once this api has fully stabilized. The functions of this accessor can be accessed with the wsi attribute of any xarray dataset or dataarray.

Coordinate Units in Microns or Pixels

The accessor includes a very lightweight units functionality, based on three methods:

  • "set_mpp" sets the microns-per-pixel (stored in the "mpp" attribute) in all coordinates and data_vars with "px" or "um" units.
  • "um" optionally sets the microns-per-pixel, but also changes the units of all relevant coordinates/data_vars to "um."
  • "px" optionally sets the microns-per-pixel, but also changes the units of all relevant coordinates/data_vars to "px."

All three methods take two arguments:

  • "default," which enables setting the mpp to this argument if it does not already exist.
  • "override," which sets the mpp to this argument, regardless of whether it is already set.

Using these methods, it is easy ensure a slide has a mpp set, and convert back and forth between px and um coordinates.

slide = slide.wsi.um(0.5) # convert coordinates to microns, using a default mpp of 0.5
slide = slide.wsi.px(override=0.5) # convert cordinates to pixels using mpp of 0.5 (regardless of the metadata in the slide).

Regularly-Spaced Grids

There are several tools for regularly spaced, increasing grids. These are grids that are characterized in each dimension by,

  • origin, the point at which the 0,0 pixel lies.
  • spacing, the gap between each pixel, which is a constant number.
  • size, the number of points in the grid along each number.

This follows closely the semantics of the ITK tooklit. These numbers can be computed for coordinate that is a regularly spaced-grid, as a dictionary:

slide.wsi.grids
slide.wsi.origin
slide.wsi.spacing
slide.wsi.size

It is also possible to compute,

  • shift, defined as origin % spacing
  • min, the minimum coordinate
  • max, the maximum coordinate
  • slice, a slice with start=min and stop=max.

The slice can be used with sel to clip objects by the bounding box of another objection. For example, if we have a region of interest stored in an xarray object (roi),

slide_roi = slide.sel(**roi.wsi.slice)

Converting to PIL

Converting an xarray to a PIL image,

img = slide.image.wsi.pil

Keep in mind that displaying the PIL image in jupyter will be very slow for large images.

Better Defaults for Plot

Data-arrays can plot themsleves as an image with better functionality, and defaults suitable for medical images. In contrast with the default imshow of xarray,

  1. The aspect ratio is fixed.
  2. "yincreasing" defaults to False
slide.image.wsi.show()

Requesting Feedback

This project currently in alpha to obtain feedback on the API. Please submit issues or API feature/modification requests to: https://github.com/swamidasslab/tiffslide-xarray.

Metadata

Release files for tiffslide-xarray 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tiffslide-xarray 0.3.0
File Size Uploaded
tiffslide_xarray-0.3.0.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tiffslide-xarray 0.3.0
File Interpreter ABI Platform
tiffslide_xarray-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.9 kB

Release files / tiffslide_xarray-0.3.0.tar.gz

Download URL tiffslide_xarray-0.3.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
06ba84be7a00503fcd26435d4c7c9700a1a66adeae54c555c300fcdc7a799ca8
BLAKE2b-256 checksum
How to use checksums
1d42149ab699399794ba09fc0f25bb1ae5d47976bc6531f3b84d8529283e9dc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.14

Release files / tiffslide_xarray-0.3.0-py3-none-any.whl

Download URL tiffslide_xarray-0.3.0-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba7bbff45e1c8a793055234dcfb0e098af66ab6284443e27425d3ffbfa8439ff
BLAKE2b-256 checksum
How to use checksums
e2544da54ecd4e6d82696735522298788ed742f319e0e14a8e2cdde8b40af77c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.14

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page