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()

# factors (2,2), (2,2,1), and (2,2,1,1) are on a fast path
img_pyramid = tinybrain.downsample_with_averaging(img, factor=(2,2,1), num_mips=5)

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

Installation

pip install numpy
pip install tinybrain

Motivation

Image heirarchy 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.

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.

A C++ high performance path is triggered for 2x2x1x1 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.

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) downsamples, a Cython fast, low memory path is selected that implements countless if. For (4,4,1) or other 2D powers of two, the countless 2d algorithm is used. For (2,2,2), (4,4,4), 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 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.0.0.tar.gz (288.0 kB view details)

Uploaded Source

Built Distributions

tinybrain-1.0.0-cp38-cp38-win_amd64.whl (497.6 kB view details)

Uploaded CPython 3.8Windows x86-64

tinybrain-1.0.0-cp38-cp38-win32.whl (447.2 kB view details)

Uploaded CPython 3.8Windows x86

tinybrain-1.0.0-cp38-cp38-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.8

tinybrain-1.0.0-cp38-cp38-macosx_10_9_x86_64.whl (573.7 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

tinybrain-1.0.0-cp37-cp37m-win_amd64.whl (487.7 kB view details)

Uploaded CPython 3.7mWindows x86-64

tinybrain-1.0.0-cp37-cp37m-win32.whl (442.8 kB view details)

Uploaded CPython 3.7mWindows x86

tinybrain-1.0.0-cp37-cp37m-manylinux1_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7m

tinybrain-1.0.0-cp37-cp37m-macosx_10_9_x86_64.whl (565.6 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

tinybrain-1.0.0-cp36-cp36m-win_amd64.whl (487.5 kB view details)

Uploaded CPython 3.6mWindows x86-64

tinybrain-1.0.0-cp36-cp36m-win32.whl (442.7 kB view details)

Uploaded CPython 3.6mWindows x86

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

Uploaded CPython 3.6m

tinybrain-1.0.0-cp36-cp36m-macosx_10_9_x86_64.whl (565.0 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

tinybrain-1.0.0-cp35-cp35m-manylinux1_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.5m

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

Uploaded CPython 2.7m

tinybrain-1.0.0-cp27-cp27m-macosx_10_15_x86_64.whl (551.6 kB view details)

Uploaded CPython 2.7mmacOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: tinybrain-1.0.0.tar.gz
  • Upload date:
  • Size: 288.0 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.0.0.tar.gz
Algorithm Hash digest
SHA256 276f299b0fc97c8c60384b42908538ea94fae6737d60ecc2b828103f47aa015e
MD5 29100c7f293837c0ba07eb37b17be9c1
BLAKE2b-256 529a96790ecfb48e581e68efe34c684aa67dff3aa87551f71a5a465a370d62f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 497.6 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.0.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d8c712d26fe489ed90181331dd7aef3828c545dfc5ea59af3c4ffd81d4b7fb3d
MD5 0cb14e5ead321b01249dcf9af07eef08
BLAKE2b-256 29b7db6dd092027eef4c679ba2df3158403f4ab9364360a0d33be6212270a1c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 447.2 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.0.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 12e50d09df6a52bde9e7bf0cda51b0338024eb31ca59babeac4641ba2d94b1de
MD5 7cc0fbc3aaf8caba8fd20cbf5be846a7
BLAKE2b-256 3dc5a09d0d76237002c9ac2244a0e5b612117e14fefe4672ec0d97a3cd0cc2ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 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.0.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b94a51087fd36a5061ef78f2bcb2d9888a32042c3dce2858c7c90098589fa128
MD5 11443eda22bbf6a6aae1ab7c8567d31c
BLAKE2b-256 e0b6b875b6c2d773da079d8a666c068fb4e5b7135ab1cef67f0ec9279f8edc9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 573.7 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.0.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 519518e5686d15e66622173ca5de409b78454b61420708ebb657f941255f869f
MD5 3ff37791040b33738c7493e1da0b13fb
BLAKE2b-256 a048805b827d705465a700c0f785e951d4b38a8e960ec29921c232b3052bb4c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 487.7 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.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ba791d18d5881d4d2e3c2378676cce4520ff25bd907ac7a029f87859e14e94f6
MD5 a82a086bc0c62f7a0749f5ea7301fef5
BLAKE2b-256 bc0690b99251867f0ff34b3dd2ad3919194e0cb268a9cf71869d11345001e928

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 442.8 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.0.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 8969f7585a44d8f014f712cf6f09d7e17553d7b3db40f7f114a1a5a755751ca2
MD5 c7cc8c010d206591534636eff4a20d30
BLAKE2b-256 3290d6326e1da8faffdb449bf5c93cbc71209d0c6cadc432dfe78fedab2e29ab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.4 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.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 172b4cd5cbb962f6de03e636a4d7325043a58271e97498da6c8ac280108a75c6
MD5 ae34373606c2b6b289e75d1a21c1bde2
BLAKE2b-256 3ef0f9334535fde1b6b7c07a41e3e7b448c4fa919f3a21392cbc062a42895141

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 565.6 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.0.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3223d7d18199446b46afc55ea8a30d1fef11061fe396cc39fc862c67955507be
MD5 b1d916402dbfa9775138a25889fc2a0b
BLAKE2b-256 26bd7ac6fb43b37cfb527e7dd9aa0fb0853bb2a9552bf4a7ff3a1f126060ae09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 487.5 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.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 a81c823335149629595109d962b07ad6bc07f00f28470e1262241d2996e94530
MD5 878d5b901aee65930ab5e4f19f1b2bb7
BLAKE2b-256 a493fe4f579b1b024acfa5d6e40627ac9d517638105643eceed44c120ecfed43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 442.7 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.0.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 e6be8b5c989e0e23a01cb9802269ec9624e86003d49317919ccfb28dab115357
MD5 aa633574ad56647376095e651daf59fb
BLAKE2b-256 5310dfffdeaf224eb848b6ec963b51060e57353b1dab10ab83fc43b129df02c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.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.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dcb0d6d262eb30e28f2dacabe7b7b90ed7849015b23f90c43dbc3bf3ce2ca437
MD5 504af4a5680ad7f4d1bc0bbbbd2eb6ea
BLAKE2b-256 2223447b2e8d14b7af544921189c3332814d69432deee5352ca4c0896db2ae42

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 565.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.0.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3958397fb7dc52035761ecacf366232195910c24e9ab6d488b5bb7f6f9345bdc
MD5 9a4a9d8219547619c4f7d847d59351a7
BLAKE2b-256 0525e61f9f9c5911b8e254f76ac25bf9ac94a843ee2085e2179bbae49f194da7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.3 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.0.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2094a2e680d41c7f053738096337ce27cafb92061f9adc39714752f1c17ca1fa
MD5 de4a2428fa187a0defae6a8ac5ecf3a8
BLAKE2b-256 bf6c126851f12b512e116ee69689820ca01aa5e12138e93227e0955a9156074c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.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.0.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 815e8d4c3ef5ba837a8cdcbe2c6a59eb367cd34ad85987d133ffda3923aba7d9
MD5 15521fa12fcb36007b2bce548a87bf36
BLAKE2b-256 09a80bcbbcfe767d3e0d52646722d2a0774b884bc5105f946aefb584a0fad404

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinybrain-1.0.0-cp27-cp27m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 551.6 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.0.0-cp27-cp27m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a60b40a5b3d8096878987fad7fb79b7c72f14bfad2f3252ba58d1946d65652f6
MD5 d64ec46cd89dfc3f80a6cbf7d92bc44a
BLAKE2b-256 62fe1564f8be2ba15de6714a713e427810d2c9ac0fcb372570e790037f0bbf37

See more details on using hashes here.

Supported by

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