Converting STPT mosaic datasets from Zarr to TIFF format¶
Load the module that we are going to use. The class is STPTDataset
defined in imaxt_image.datasets
[1]:
from imaxt_image.datasets import STPTDataset
Define here the location (path
) and name (name
) of the dataset. Additionaly set the scale that we want to use: 1, 2, 4, 8, 16 or 32 where 1 is the original resolution, 2 is downsampled 2x2 pixels, etc.
[2]:
path = "/data/meds1_c/storage/processed/stpt/"
name = "20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1"
scale = 8
We read the data as shown below, instantiating the STPTDataset
class wih the location of the dataset and the scale (resolution) that we want to read.
[3]:
ds = STPTDataset(name, path, scale=scale)
ds
[3]:
<xarray.Dataset>
Dimensions: (channel: 4, x: 2340, y: 1820, z: 2)
Coordinates:
* channel (channel) int64 1 2 3 4
type <U6 'mosaic'
* x (x) int64 0 1 2 3 4 5 6 7 ... 2333 2334 2335 2336 2337 2338 2339
* y (y) int64 0 1 2 3 4 5 6 7 ... 1813 1814 1815 1816 1817 1818 1819
* z (z) int64 0 1
Data variables:
S001 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S002 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S003 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S004 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S005 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S006 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S007 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S008 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S009 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S010 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S011 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S012 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S013 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S014 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S015 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S016 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S017 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S018 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S019 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
S020 (z, channel, y, x) float64 dask.array<chunksize=(1, 1, 1040, 1040), meta=np.ndarray>
We can access e.g. the first slide as:
[4]:
ds["S001"]
[4]:
<xarray.DataArray 'S001' (z: 2, channel: 4, y: 1820, x: 2340)>
dask.array<astype, shape=(2, 4, 1820, 2340), dtype=uint16, chunksize=(1, 1, 1040, 1040), chunktype=numpy.ndarray>
Coordinates:
* channel (channel) int64 1 2 3 4
type <U6 'mosaic'
* x (x) int64 0 1 2 3 4 5 6 7 ... 2333 2334 2335 2336 2337 2338 2339
* y (y) int64 0 1 2 3 4 5 6 7 ... 1813 1814 1815 1816 1817 1818 1819
* z (z) int64 0 1
In order to save the slide to a OME.TIFF file use the .to_tiff
method:
[5]:
ds["S001"].to_tiff(name, dir=".")
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S001_l.8.ome.tiff
where the dir
keyword is the directory where the file is to be saved.
In the same way we can define just a region to save:
[6]:
ds["S001"][5000:7000,6000:9000].to_tiff(f"{name}_cutout")
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_cutout_S001_l.8.ome.tiff
Finnally combined with a for
loop the above can be done for all sections or for a range of sections:
[7]:
for section in ds:
section.to_tiff(name, dir=".")
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S001_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S002_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S003_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S004_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S005_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S006_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S007_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S008_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S009_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S010_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S011_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S012_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S013_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S014_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S015_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S016_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S017_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S018_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S019_l.8.ome.tiff
Written ./20200311_balbc_8020_3dIMC_20x15_2x8umOS_set1_S020_l.8.ome.tiff