8000 GitHub - neuro-ml/imops: Efficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines · GitHub
[go: up one dir, main page]

Skip to content
neuro-mlPublic

About

Efficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines

Topics

Resources

Stars

24 stars

Watchers

4 watching

Forks

Repository files navigation

codecov pypi License PyPI - Downloads

Imops

Efficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines.

Install

pip install imops  # default install with Cython backend

Wheels pick the kernels for the CPU at import: x86-64-v3 (AVX2) where the CPU has it, x86-64-v2 elsewhere. To build for your own CPU only, use -march=native:

IMOPS_NATIVE=1 pip install --no-binary imops imops

How fast is it?

Time comparisons (ms) for Intel(R) Xeon(R) Silver 4114 CPU @ 2.20GHz using 8 threads. All inputs are C-contiguous NumPy arrays. For morphology functions bool dtype is used and float64 for all others.

function / backend Scipy() Cython()
zoom(..., order=0) 2072 1114
zoom(..., order=1) 6527 596
interp1d 780 149
radon 59711 5982
inverse_radon 52928 8254
binary_dilation 2207 310
binary_erosion 2296 326
binary_closing 4158 544
binary_opening 4410 567
center_of_mass 2237 64

We use airspeed velocity to benchmark our code. For detailed results visit benchmark page.

Features

Fast Radon transform

from imops import radon, inverse_radon

Fast 0/1-order zoom

from imops import zoom, zoom_to_shape

# fast zoom with optional fallback to scipy's implementation
y = zoom(x, 2, axis=[0, 1])
# a handy function to zoom the array to a given shape 
# without the need to compute the scale factor
z = zoom_to_shape(x, (4, 120, 67))

Works faster only for ndim<=4, dtype=float32 or float64 (and bool-int16-32-64-uint8-16-32 if order == 0), output=None, order=0 or 1, mode='constant', grid_mode=False

Fast 0/1-order rotate

from imops import rotate

# rotate every plane parallel to axes 1 and 2 by 30 degrees
y = rotate(x, 30, axes=(1, 2))
# a bool mask answers what `rotate(mask.astype('float32')) >= 0.5` answers, without building that array
m = rotate(mask, 30, axes=(1, 2))

Works faster only for order=0 or 1, dtype=bool-float16-float32-uint8-uint16-int16-int32, mode='constant'. Unlike scipy.ndimage.rotate it also accepts float16, copies the input only if it is not C-contiguous or is float16, and always returns a C-contiguous array.

Fast 1d linear interpolation

from imops import interp1d  # same as `scipy.interpolate.interp1d`

Works faster only for ndim<=3, dtype=float32 or float64, order=1

Fast 2d linear interpolation

import numpy as np
from imops.interp2d import Linear2DInterpolator
n, m = 1024, 2
points = np.random.randint(low=0, high=1024, size=(n, m))
points = np.unique(points, axis=0)
x_points = points[: n // 2]
values = np.random.uniform(low=0.0, high=1.0, size=(len(x_points),))
interp_points = points[n // 2:]
num_threads = -1 # will be equal to num of CPU cores
# You can optionally pass your own triangulation as an np.array of shape [num_triangles, 3], element at (i, j) position is an index of a point from x_points
interpolator = Linear2DInterpolator(x_points, values, num_threads=num_threads, triangles=None)
# Also you can pass values to __call__ and rewrite the ones that were passed to __init__
interp_values = interpolator(interp_points, values + 1.0, fill_value=0.0)

Fast binary morphology

from imops import binary_dilation, binary_erosion, binary_opening, binary_closing

These functions mimic scikit-image counterparts

Padding

from imops import pad, pad_to_shape

y = pad(x, 10, axis=[0, 1])
# `ratio` controls how much padding is applied to left side:
# 0 - pad from right
# 1 - pad from left
# 0.5 - distribute the padding equally
z = pad_to_shape(x, (4, 120, 67), ratio=0.25)

Cropping

from imops import crop_to_shape

# `ratio` controls the position of the crop
# 0 - crop from right
# 1 - crop from left
# 0.5 - crop from the middle
z = crop_to_shape(x, (4, 120, 67), ratio=0.25)

Labeling

from imops import label

# same components as `skimage.measure.label`, possibly numbered in another order
labeled, num_components = label(x, background=1, return_num=True)

Backends

For all heavy image routines except label you can specify which backend to use. Backend can be specified by a string or by an instance of Backend class. The latter allows you to customize some backend options:

from imops import Cython, Scipy, zoom

y = zoom(x, 2, backend='Cython')
y = zoom(x, 2, backend=Cython())  # same as previous
y = zoom(x, 2, backend=Scipy())  # use scipy original implementation

Also backend can be specified globally or locally:

from imops import imops_backend, set_backend, zoom

set_backend('Scipy')  # sets Scipy as default backend
with imops_backend('Cython'):  # sets Cython backend via context manager
    zoom(x, 2)

Available backends:

< 6975 td align="center">✓
function / backend Scipy Cython
zoom ✓ ✓
interp1d ✓ ✓
radon ✗ ✓
inverse_radon ✗ ✓
binary_dilation ✓
binary_erosion ✓ ✓
binary_closing ✓ ✓
binary_opening ✓ ✓
center_of_mass ✓ ✓

Acknowledgements

Some parts of our code for radon/inverse radon transform as well as the code for linear interpolation are inspired by the implementations from scikit-image and scipy. Also we used fastremap, edt and cc3d out of the box.

Building from source

Building needs GCC 11+ or clang 12+, which know -march=x86-64-v2 and -march=x86-64-v3, or MSVC. IMOPS_NATIVE=1 has no effect with MSVC, which has no -march=native.

About

Efficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines

Topics

Resources

Stars

24 stars

Watchers

4 watching

Forks

Releases

Used by

Contributors

Languages

0