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. We only support 64bit architectures currently if you desperately need 32 bit support please make an issue or modify the source and build it for your use case.

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.get_dims_shape()  # [{'X': (0, 1900), 'Y': (0, 1300), 'Z': (0, 60), 'C': (0, 4), 'S': (0, 40), 'B': (0, 1)}]

czi.dims  # BSCZYX

czi.size  # (1, 40, 4, 60, 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('mosaic_test.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.get_dims_shape()  # [{'X': (0, 924), 'Y': (0, 624), 'Z': (0, 1), 'C': (0, 1), 'T': (0, 1), 'M': (0, 2), 'S': (0, 1)}]

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

If this doesn't work: Please investigate the following (generally windows issues):

  • your OS is 64 bit - we only support 64 bit binaries
  • your python is a 64 bit application (not 32 bit)
  • are your C++ runtime libraries up to date? vc_redist.exe

If you have tried this and are still having trouble please reach out to us and we will endeavor to help.

Documentation

Documentation is available at github.io.

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'

Known Issues

  • with read_mosaic if the scale_factor is not 1.0 Zeiss's libCZI will, on some files, fail to render certain subblocks within the composite mosaic image. It is not currently known if this is an issue with the file or with libCZI.

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-3.0.1-cp39-cp39-win_amd64.whl (460.5 kB view details)

Uploaded CPython 3.9Windows x86-64

aicspylibczi-3.0.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (625.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

aicspylibczi-3.0.1-cp39-cp39-macosx_10_9_x86_64.whl (560.5 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

aicspylibczi-3.0.1-cp38-cp38-win_amd64.whl (462.0 kB view details)

Uploaded CPython 3.8Windows x86-64

aicspylibczi-3.0.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (625.3 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

aicspylibczi-3.0.1-cp38-cp38-macosx_10_9_x86_64.whl (560.5 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

aicspylibczi-3.0.1-cp37-cp37m-win_amd64.whl (461.7 kB view details)

Uploaded CPython 3.7mWindows x86-64

aicspylibczi-3.0.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (628.8 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

aicspylibczi-3.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (559.0 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

aicspylibczi-3.0.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (628.2 kB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

File details

Details for the file aicspylibczi-3.0.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: aicspylibczi-3.0.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 460.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aicspylibczi-3.0.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 42d25bdc67dd9f45c5147a8c5110112656a4f9088ef709b8484d37b6c7f4962f
MD5 0b6f8c00ccc726db2d887eb1d3c37bea
BLAKE2b-256 343be6e8b446a7051befb364e4a8eba06c5241e5d2d26c1c01a75d9e4b3c1c75

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for aicspylibczi-3.0.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b8caead580ce5e288ab90ef8cc1c988203b0f898cfa414860e610d8f9e4f887e
MD5 acba73701e562c8252cc172c8eaebe7a
BLAKE2b-256 29b4964d9fd28503c712ddbaabde551a529a028767436cc6346453f05a4d40d2

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: aicspylibczi-3.0.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 560.5 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aicspylibczi-3.0.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 94fea3a963cc6cccb24ddc8474216ebcc2c4782fe53a217c5eb6cb2f0df8f73f
MD5 dd41e49099ce3ff5af5b03c8bff1e77f
BLAKE2b-256 df5e4358244a74174719a2e31edaae8a118af03f2019ad8c89e805ffa9825730

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: aicspylibczi-3.0.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 462.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9

File hashes

Hashes for aicspylibczi-3.0.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 111ed55cecebd29ebb1e7b67d06471183f6d42bcd0e921cefc30dff308e819a0
MD5 a4606c7b7b1f2139d1fa42ac19d23215
BLAKE2b-256 aecda4cfc6a20bc296bcaf12692bf55c1715d753a2be1d0601dd0f2dfd8790c4

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for aicspylibczi-3.0.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 7736bbf67426d1dacde4dad4cab0e49df4257e4f2cef87589d6a2bd28f89a987
MD5 7514d035a6854327affaa8356c9262a9
BLAKE2b-256 5eb3aa7597253c2d83a914991b0036f3771af9b5ab254916924f5b0a16dadd4c

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: aicspylibczi-3.0.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 560.5 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aicspylibczi-3.0.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 82da822d5408020003da6224b95a5cee6e71779d78ea44f2ce615c787356baac
MD5 efe5bd60ff6067e9578be654df4de7ea
BLAKE2b-256 c7bd85e162adf0f7b90e974c8ade580b376e091601f20dc04f031660ac37ddcb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aicspylibczi-3.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 461.7 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for aicspylibczi-3.0.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 0dea6a088f27059405187bd8cdcbbbb918aa8657c2ebce6946eb1d086a494d01
MD5 2c60f1a20521ffbfc472135887961b44
BLAKE2b-256 6b7d6dfd382b7b71afe1c65992f2c8fd51c60cd4e46e7ee662028db5dc320396

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for aicspylibczi-3.0.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 dcbbaf7519ac8e27bb5d0250b3c526524ec0091bdec9b8d72fe3648b5126bf14
MD5 4196b565a5acd3155a3edb906702d925
BLAKE2b-256 b29988f967a93262d6242d79f887cf99796f1f3a31f69a89f45e01ae599d38ac

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: aicspylibczi-3.0.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 559.0 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aicspylibczi-3.0.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0162ee8e83370edcfb19a6f8df4b1f0113624b50b231ec8bade19134939ed405
MD5 e9bfbec3a5703a182ab57d0c5ec6dd35
BLAKE2b-256 1ae570b892b397fd9abb9e5ababfe3106636cf108d1302c407a02ad7cb8861a3

See more details on using hashes here.

File details

Details for the file aicspylibczi-3.0.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for aicspylibczi-3.0.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c549765906be03f6a14ef590b313acb28b65645daf15fe4b015e33aaaabdb5b7
MD5 9067e2ace964f18a0d350623ff9e3791
BLAKE2b-256 c1041ccabb42970f1bbf4709509e626884be1fe7089b539da0044c0549f87301

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