Skip to main content

A python module and a python extension for Zeiss (CZI/ZISRAW) microscopy files.

Project description

aicspylibczi

C++ Build & Test Python Build & Test codecov License: BSD3/GPLv3

Python module to expose libCZI functionality for reading (subset of) Zeiss CZI files and meta-data.

Usage

The first example show how to work with a standard CZI file (Single or Multe-Scene). The second example shows how to work with a Mosaic CZI file.

Example 1: Read in a czi and select a portion of the image to display

import numpy as np
from aicspylibczi import CziFile
from pathlib import Path
import matplotlib.pyplot as plt

pth = Path('20190610_S02-02.czi')
czi = CziFile(pth)

# Get the shape of the data, the coordinate pairs are (start index, size)
dimensions = czi.dims_shape()  # {'Z': (0, 70), 'C': (0, 2), 'T': (0, 146), 'S': (0, 12), 'B': (0, 1)}

czi.dims  #  BSTCZYX

czi.size  #  (1, 12, 146, 2, 70, 1300, 1900) 

# Load the image slice I want from the file
img, shp = czi.read_image(S=13, Z=16)

# shp = [('B', 1), ('S', 1), ('C', 4), ('Z', 1), ('Y', 1300), ('X', 1900)]  # List[(Dimension, size), ...] 
# img.shape = (1, 1, 4, 1, 1300, 1900)   # numpy.ndarray

# define helper functions
def norm_by(x, min_, max_):
    norms = np.percentile(x, [min_, max_])
    i2 = np.clip((x - norms[0])/(norms[1]-norms[0]), 0, 1)
    return i2

def recolor(im): # transform from rgb to cyan-magenta-yellow
    im_shape = np.array(im.shape)
    color_transform = np.array([[1, 1, 0], [0, 1, 1], [1, 0, 1]]).T
    im_reshape = im.reshape([np.prod(im_shape[0:2]), im_shape[2]]).T
    im_recolored = np.matmul(color_transform.T, im_reshape).T
    im_shape[2] = 3
    im = im_recolored.reshape(im_shape)
    return im

#normalize, combine into RGB and transform to CMY
c1 = (norm_by(img[0, 0, 0, 0, 0:750, 250:1000], 50, 99.8)*255).astype(np.uint8)
c2 = (norm_by(img[0, 0, 1, 0, 0:750, 250:1000], 50, 99.8)*255).astype(np.uint8)
c3 = (norm_by(img[0, 0, 2, 0, 0:750, 250:1000], 0, 100)*255).astype(np.uint8)
rgb = np.stack((c1, c2, c3), axis=2)
cmy = np.clip(recolor(rgb), 0, 255)

# plot using matplotlib¶
plt.figure(figsize=(10,10))
plt.imshow(cmy)
plt.axis('off')

Cardio Image

Example 2: Read in a mosaic file

import numpy as np
import aicspylibczi
import pathlib
from PIL import Image


mosaic_file = pathlib.Path('20190618_CL001_HB01_Rescan_002.czi')
czi = aicspylibczi.CziFile(mosaic_file)

# Get the shape of the data
dimensions = czi.dims   #  'STCZMYX'

czi.size  # (1, 1, 1, 1, 2, 624, 924)

czi.dims_shape()  # {'C': (0, 0), 'M': (0, 1), 'S': (0, 0), 'T': (0, 0), 'X': (0, 923), 'Y': (0, 623), 'Z': (0, 0)}

czi.is_mosaic()  # True 
 # Mosaic files ignore the S dimension and use an internal mIndex to reconstruct, the scale factor allows one to generate a manageable image
mosaic_data = czi.read_mosaic(C=0, scale_factor=1) 

mosaic_data.shape  #  (1, 1, 624, 1756)
 # the C channel has been specified S & M are used internally for position so this is (T, Z, Y, X)

normed_mosaic_data = norm_by(mosaic_data[0, 0, :, :], 5, 98)*255
img = Image.fromarray(normed_mosaic_data.astype(np.uint8))

Mosaic Image

Installation

The preferred installation method is with pip install. This will install the aicspylibczi python module and extension binaries (hosted on PyPI):

pip install aicspylibczi

Documentation

Documentation is available at github.io and readthedocs.

Build

Use these steps to build and install aicspylibczi locally:

  • Clone the repository including submodules (--recurse-submodules).
  • Requirements:
    • libCZI requires a c++11 compatible compiler. Built & Tested with clang.
    • Development requirements are those required for libCZI: libpng, zlib
    • Install the package:
      pip install .
      pip install -e .[dev] # for development (-e means editable so changes take effect when made)
      pip install .[all] # for everything including jupyter notebook to work with the Example_Usage above
      
    • libCZI is automatically built as a submodule and linked statically into aicspylibczi.
  • Note: If you get the message directly below on windows you need to set PYTHONHOME to be the folder the python.exe you are compiling against lives in.
EXEC : Fatal Python error : initfsencoding: unable to load the file system codec ... 
ModuleNotFoundError: No module named 'encodings'

History

aicspylibczi was originally a fork of pylibczi that was developed by Paul Watkins and focused on mSEM data. In attempting to extend the work to we transitioned to pybind11, implemented c++ and python tests, added continuous integration via github actions, and added the functionality to read individual subblocks and stacks of subblocks as a numpy.ndarray. Metadata reading, including specific subblock metadata reading has also been added.

We intend for this work to be merged back into the original project once we have the new work integrated with the original work.

Licenses & Acknowledgements

The code directly in this repo implemented in C++ and Python is covered by BSD-3 license by the Allen Institute for Cell Science at the Allen Institute.

This project however was created from a fork of pylibczi as explained above in the history section and Paul Watkins is a developer on our repo as well. Pylibczi, from the Center of Advanced European Studies And Research and the core dependency libCZI, are covered by the GPLv3 license.

The GPLv3 license is a consequence of libCZI which imposes GPLv3. If you wish to use libCZI or this derivative in a commercial product you may need to talk to Zeiss and CAESAR. A discussion about GPLv3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

aicspylibczi-2.3.2-cp37-cp37m-win_amd64.whl (432.4 kB view details)

Uploaded CPython 3.7mWindows x86-64

aicspylibczi-2.3.2-cp37-cp37m-manylinux2010_x86_64.whl (585.7 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

aicspylibczi-2.3.2-cp37-cp37m-macosx_10_13_x86_64.whl (539.4 kB view details)

Uploaded CPython 3.7mmacOS 10.13+ x86-64

File details

Details for the file aicspylibczi-2.3.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: aicspylibczi-2.3.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 432.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for aicspylibczi-2.3.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 92f66ded6f1ae7a185baa25ec066611d75d887263caac4884680cd291677c96a
MD5 f059accd44a2ceca4713b74ad074a204
BLAKE2b-256 fd1cd93a57ce6332eed377b80b3dd22b776b51d2ac2b70ca8aace1d6e202e445

See more details on using hashes here.

File details

Details for the file aicspylibczi-2.3.2-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: aicspylibczi-2.3.2-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 585.7 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for aicspylibczi-2.3.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 04822be797f9a4eb29183c5ab04bdd689682ca8a245a940c049ad0516b607c70
MD5 8ff8b0ac394f26d27036ee4a1aa6a5c2
BLAKE2b-256 c6f58b37b5ba94547089c51bacbd907e60895cde30f5035ea26b47ba29e7b446

See more details on using hashes here.

File details

Details for the file aicspylibczi-2.3.2-cp37-cp37m-macosx_10_13_x86_64.whl.

File metadata

  • Download URL: aicspylibczi-2.3.2-cp37-cp37m-macosx_10_13_x86_64.whl
  • Upload date:
  • Size: 539.4 kB
  • Tags: CPython 3.7m, macOS 10.13+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for aicspylibczi-2.3.2-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 abb13e0f91fc0669114f55e5ee9bcc582d75bf9169bf6e07db06b059613099b9
MD5 5bd2129bc2244daa3840a9992562d306
BLAKE2b-256 57dfa2cc043546be07a172d49e202183b5b6d2d2492de332f703efbae50a9598

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page