The images are taken from the paper "SARS-CoV-2 productively Infects Human Gut Enterocytes" published May 2020 in Science: https://doi.org/10.1126/science.abc1669
The electron micrograph images can be viewed online in the Image Data Resource. Both images are over 13 gigapixels each!
The cell below will install dependencies if you choose to run the notebook in Google Colab.
%pip install zarr fsspec>=0.3.3 aiohttp
import dask.array as da
from IPython.display import display, Image
from matplotlib import pyplot as plt
import requests
import zarr
The Zarr data is stored separately from the IDR, on an S3 object store
# imageid = 9822151
imageid = 9822152
endpoint = 'https://uk1s3.embassy.ebi.ac.uk'
imagepath = f'idr/zarr/v0.1/{imageid}.zarr'
The original image is over 25GB but with the help of Dask it is easy to lazily load just the required regions of the image.
Images are stored as 5D arrays: multi-channel (C
) 3D (X Y Z
) timelapse (T
) images. The order of the array dimensions are (T, C, Z, Y, X)
. Since these images are so big downsampled versions ("multi-resolutions") have also been calculated.
The list of resolutions is stored in a JSON file called .zattrs
. Dask automatically creates a nice summary of each resolution in Jupyter. These resolutions will always be stored in order from the largest image (most detailed resolution) to the smallest. Note that although the name of each resolution may be informative this is not always the case so you should not rely on it.
For the official specification of Zarr images see https://github.com/ome/ngff
def da_from_image(resolution):
return da.from_zarr(f'{endpoint}/{imagepath}/{resolution}')
zattrs = requests.get(f'{endpoint}/{imagepath}/.zattrs').json()
resolutions = [d['path'] for d in zattrs['multiscales'][0]['datasets']]
darrays = {}
for resolution in resolutions:
print(f'Resolution: "{resolution}"')
a = da_from_image(resolution)
darrays[resolution] = a
display(a)
Resolution: "0"
|
Resolution: "1"
|
Resolution: "2"
|
Resolution: "3"
|
Resolution: "4"
|
Resolution: "5"
|
Resolution: "6"
|
Resolution: "7"
|
Resolution: "8"
|
Resolution: "9"
|
Resolution: "10"
|
Display the smallest resolution image. In this image T
, C
, Z
are all singleton dimensions
r = resolutions[-1]
a0 = darrays[r][0, 0, 0, :, :]
plt.figure(figsize=(10, 10))
plt.imshow(a0, cmap='gray')
plt.title(r);
As you can see it very low resolution, so let's look at the 3rd smallest resolution instead.
r = resolutions[-3]
a0 = darrays[r][0, 0, 0, :, :]
plt.figure(figsize=(10, 10))
plt.imshow(a0, cmap='gray')
plt.title(r);
Now "zoom in" to the centre of image by showing the same number of pixels but at the next resolution up
dy = int(a0.shape[0] / 2)
dx = int(a0.shape[1] / 2)
for r in reversed(resolutions[:-3]):
a = darrays[r][0, 0, 0, :, :]
# Change the following line to zoom into a different location
cy, cx = int(a.shape[0] / 2), int(a.shape[1] / 2)
region = a[cy-dy:cy+dy, cx-dx:cx+dx]
plt.figure(figsize=(10, 10))
plt.imshow(a[cy-dy:cy+dy, cx-dx:cx+dx], cmap='gray')
plt.title(r)
plt.show()
Copyright (C) 2020-2022 University of Dundee. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.