Skip to main content

Image pyramid generation specialized for connectomics data types and procedures.

Project description

Build Status PyPI version

tinybrain

Image pyramid generation specialized for connectomics data types and procedures. If your brain wasn't tiny before, it will be now.

import tinybrain 

img = load_3d_em_stack()

# 2x2 and 2x2x2 downsamples are on a fast path.
# e.g. (2,2), (2,2,1), (2,2,1,1), (2,2,2), (2,2,2,1)
img_pyramid = tinybrain.downsample_with_averaging(img, factor=(2,2,1), num_mips=5, sparse=False)

labels = load_3d_labels()
label_pyramid = tinybrain.downsample_segmentation(labels, factor=(2,2,1), num_mips=5, sparse=False))

Installation

pip install numpy
pip install tinybrain

Motivation

Image hierarchy generation in connectomics uses a few different techniques for visualizing data, but predominantly we create image pyramids of uint8 grayscale images using 2x2 average pooling and of uint8 to uint64 segmentation labels using 2x2 mode pooling. When images become very large and people wish to visualze upper mip levels using three axes at once, it becomes desirable to perform 2x2x2 downsamples to maintain isotropy.

It's possible to compute both of these using numpy, however as multiple packages found it useful to copy the downsample functions, it makes sense to formalize these functions into a seperate library located on PyPI.

Given the disparate circumstances that they will be used in, these functions should work fast as possible with low memory usage and avoid numerical issues such as integer truncation while generating multiple mip levels.

Considerations: downsample_with_averaging

It's advisable to generate multiple mip levels at once rather than recursively computing new images as for integer type images, this leads to integer truncation issues. In the common case of 2x2x1 downsampling, a recursively computed image would lose 0.75 brightness per a mip level. Therefore, take advantage of the num_mips argument which strikes a balance that limits integer truncation loss to once every 4 mip levels. This compromise allows for the use of integer arithmatic and no more memory usage than 2x the input image including the output downsamples. If you seek to eliminate the loss beyond 4 mip levels, try promoting the type before downsampling. 2x2x2x1 downsamples truncate every 8 mip levels.

A C++ high performance path is triggered for 2x2x1x1 and 2x2x2x1 downsample factors on uint8, uint16, float32, and float64 data types in Fortran order. Other factors, data types, and orderings are computed using a numpy pathway that is much slower and more memory intensive.

We also include a sparse mode for downsampling 2x2x2 patches, which prevents "ghosting" where one z-slice overlaps a black region on the next slice and becomes semi-transparent after downsampling. We deal with this by neglecting the background pixels from the averaging operation.

Example Benchmark

On a 1024x1024x100 uint8 image I ran the following code. PIL and OpenCV are actually much faster than this benchmark shows because most of the time is spent writing to the numpy array. tinybrain has a large advantage working on 3D and 4D arrays. Of course, this is a very simple benchmark and it may be possible to tune each of these approaches. On single slices, Pillow was faster than tinybrain.

img = np.load("image.npy")

s = time.time()
downsample_with_averaging(img, (2,2,1))
print("Original ", time.time() - s)

s = time.time()
out = tinybrain.downsample_with_averaging(img, (2,2,1))
print("tinybrain ", time.time() - s)

s = time.time()
out = np.zeros(shape=(512,512,100))
for z in range(img.shape[2]):
  out[:,:,z] = cv2.resize(img[:,:,z], dsize=(512, 512) )
print("OpenCV ", time.time() - s)

s = time.time()
out = np.zeros(shape=(512,512,100))
for z in range(img.shape[2]):
  pilimg = Image.fromarray(img[:,:,z])
  out[:,:,z] = pilimg.resize( (512, 512) )
print("Pillow ", time.time() - s)

# Method     Run Time             Rel. Perf.
# Original   1820 ms +/- 3.73 ms    1.0x
# tinybrain    67 ms +/- 0.40 ms   27.2x 
# OpenCV      469 ms +/- 1.12 ms    3.9x
# Pillow      937 ms +/- 7.63 ms    1.9x

Considerations: downsample_segmentation

The downsample_segmentation function performs mode pooling operations provided the downsample factor is a power of two, including in three dimensions. If the factor is a non-power of two, striding is used. The mode pooling, which is usually what you want, is computed recursively. Mode pooling is superior to striding, but the recursive calculation can introduce defects at mip levels higher than 1. This may be improved in the future.

The way the calculation is actually done uses an ensemble of several different methods. For (2,2,1,1) and (2,2,2,1) downsamples, a Cython fast, low memory path is selected. (2,2,1,1) implements countless if. (2,2,2,1) uses a combination of counting and "instant" majority detection. For (4,4,1) or other 2D powers of two, the countless 2d algorithm is used. For (4,4,4), (8,8,8) etc, the dynamic countless 3d algorithm is used. For 2D powers of two, stippled countless 2d is used if the sparse flag is enabled. For all other configurations, striding is used.

Countless 2d paths are also fast, but use slightly more memory and time. Countless 3D is okay for (2,2,2) and (4,4,4) but will use time and memory exponential in the product of dimensions. This state of affairs could be improved by implementing a counting based algorithm in Cython/C++ for arbitrary factors that doesn't compute recursively. The countless algorithms were developed before I knew how to write Cython and package libraries. However, C++ implementations of countless are much faster than counting for computing the first 2x2x1 mip level. In particular, an AVX2 SIMD implementation can saturate memory bandwidth.

Documentation for the countless algorithm family is located here: https://github.com/william-silversmith/countless

Project details


Download files

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

Source Distribution

tinybrain-1.1.0.tar.gz (296.5 kB view details)

Uploaded Source

Built Distributions

tinybrain-1.1.0-cp38-cp38-win_amd64.whl (487.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

tinybrain-1.1.0-cp38-cp38-win32.whl (452.9 kB view details)

Uploaded CPython 3.8 Windows x86

tinybrain-1.1.0-cp38-cp38-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8

tinybrain-1.1.0-cp38-cp38-macosx_10_9_x86_64.whl (575.3 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

tinybrain-1.1.0-cp37-cp37m-win_amd64.whl (473.9 kB view details)

Uploaded CPython 3.7m Windows x86-64

tinybrain-1.1.0-cp37-cp37m-win32.whl (446.9 kB view details)

Uploaded CPython 3.7m Windows x86

tinybrain-1.1.0-cp37-cp37m-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.7m

tinybrain-1.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (566.8 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

tinybrain-1.1.0-cp36-cp36m-win_amd64.whl (473.7 kB view details)

Uploaded CPython 3.6m Windows x86-64

tinybrain-1.1.0-cp36-cp36m-win32.whl (446.9 kB view details)

Uploaded CPython 3.6m Windows x86

tinybrain-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.6m

tinybrain-1.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (566.0 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

tinybrain-1.1.0-cp35-cp35m-manylinux1_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.5m

tinybrain-1.1.0-cp27-cp27m-manylinux1_x86_64.whl (1.3 MB view details)

Uploaded CPython 2.7m

tinybrain-1.1.0-cp27-cp27m-macosx_10_15_x86_64.whl (553.2 kB view details)

Uploaded CPython 2.7m macOS 10.15+ x86-64

File details

Details for the file tinybrain-1.1.0.tar.gz.

File metadata

  • Download URL: tinybrain-1.1.0.tar.gz
  • Upload date:
  • Size: 296.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4c3b561db376ac523635554b594bfcd86f01b46d1b07057e5cf288ad6ef64f35
MD5 a39ebe6e4a67225b31796da7ac072890
BLAKE2b-256 c1aff084d1a01514d55d78172b38abeccc8d35502b03c5f58d4b23d0e3026cfc

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 487.1 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c6099b4c8cca3cd340058d6db30fc2e047129bd8eb1323375bdb0fc4b10775a6
MD5 0b038bf7c36febb64add272efef51ac2
BLAKE2b-256 2a77efe681f8b5ed9354ba773391699d8e417b91e34b26e091fcd20107ed22a3

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp38-cp38-win32.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 452.9 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 8784822b1f5bb880a5e12b414af079dd9062f2a045f897db4bde406ac6537bad
MD5 15a2bd558617bb437f918e2825477753
BLAKE2b-256 207ad1d762cb190e91cc36ab755c0f51b644436ae3afb016349d83d41e93c570

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b501f4fe5f364da81febc0f69d951c1b80c77304a504c0845ca9ddf1743e1b88
MD5 da47da6c1b1d9496487356ed25f6e44b
BLAKE2b-256 3da93b96c6bac4e5fbeef6b90d928bd4319529b9f8bc4d6f2f47e438274b1d72

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 575.3 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 464f5b9ab43492d338a6804565fc9cf6a0b264e23a7d0a9c8086f94e3e8799fa
MD5 b3d6b69351f23bf790270eacb115666b
BLAKE2b-256 6f4021b92f1f177ae4fa9a64f67f5ae6fcad0f9f30ba9e32ce63edc0028a1b2b

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 473.9 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 6c766bf5ba5c0b0ba58179e230015b0d6483df579ea7a7bbcfc911a3f6c7372a
MD5 5d483cf8a51b60a306bdd7098c923f91
BLAKE2b-256 7981a3357240c37f33d4fbcd43739ae309479740867e089c5a2256cc5cdd6c5b

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp37-cp37m-win32.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 446.9 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 24bdb4caae1686def3bc10fde0c3d373de803072c997a1756abc1baf418dd3e2
MD5 0ab57ef0f5d354b22c78ddc721d20f7c
BLAKE2b-256 23a50623c48d36a915cb3305f199d926772d5a60910b8e65e3f4e757c348e621

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 11d4cd3d1404f937657c6a01b16c27d2e8bbc87f708293299722f91be47278c4
MD5 fad65f9a34e5b27f3ff3bc22a6bff173
BLAKE2b-256 2872ad813ffa5bc1c11930f8f3ddfe5e9162d3a6e447cef3de48e342bf099963

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 566.8 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 994e9a76c6b07bb3ab0089cdf73b588840a79514aebc3173d8349f63715f5756
MD5 5b8e531a12b8b23f6c80581f46413255
BLAKE2b-256 12c99e8af2468858980759de028d9c744b9fe3fc47682f7695b297206ea1926c

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 473.7 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 8169c72644936cd6a648e833ae21249f9248b6c6afb90d6b74a02ddcb2beef70
MD5 dec67063aebb980d6a371b3891fb7320
BLAKE2b-256 a96f7d5a5006600f08fc17e369cdfc61d9d7307cb6ff9857f8405237a8325983

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp36-cp36m-win32.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 446.9 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 bacae9941ef2634763919ee31a0b8ddb1784d08086837b0749372353b6d571fa
MD5 a779adc9c8d884acf21d4be82013d486
BLAKE2b-256 8f8597131a1c1fbea776188a15460b62cb4aa34b76fed2ac4f3b45608a14d061

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7d4abe7b1a8f52ff0a0c15991583443e119b17436b9e0deb4a7fa7376a2aefa1
MD5 12c1a52905805dd66243154d5ddce9ab
BLAKE2b-256 67877eca88028c42a8c0e54f4307b2d315f540994ac62b28023d7523513aa32b

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 566.0 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 12ac73dfee6ae6843c09c9625b85d36166ef522c29c102bbf44e8772ab8c394d
MD5 9112121889ae00b447287781f7689114
BLAKE2b-256 c9cb21455d646b9b748fb7f7659f24de2fc1cd06a08114d9ffd3142d063f58b7

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a6b2c1d228bc653855600d6370f0b26303d91ce14f97b0bdfcbd9304241aac8b
MD5 6299311c3c4a78a9ace10840068284ce
BLAKE2b-256 0164ae5f011f393b335573c275afcccaef2b7210a05a6d6e57fcf421a1e1aebb

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e2a331523afa24526f93c9e48465e630211f7d150c24025ac9568548e488a6a2
MD5 0cea347d1d6b7297bd22ec7c2259be4a
BLAKE2b-256 f8099c4129fa3af82056215d917a77afdbf383fd497effe3dd0848a5fa658a22

See more details on using hashes here.

File details

Details for the file tinybrain-1.1.0-cp27-cp27m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: tinybrain-1.1.0-cp27-cp27m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 553.2 kB
  • Tags: CPython 2.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for tinybrain-1.1.0-cp27-cp27m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d8c60db45829af5134a19837a521ba2d7987fd4ad132269ac00e75bbd6838ca6
MD5 39c4cc5cdfd2aa2fcf80143e8d2e4324
BLAKE2b-256 fb56a8eded390edf5ef9a11916fcb25f70e7c46acbb57c943d392a6c9fbe6da3

See more details on using hashes here.

Supported by

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