Imops
Efficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines
Install
pip install imops # default install with Cython backend
pip install imops[numba] # additionally install Numba backend
Functions
imops.crop.crop_to_shape(x, shape, axis=None, ratio=0.5)
Crop x
to match shape
along axis
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array |
required |
shape |
AxesLike
|
final shape |
required |
axis |
AxesLike
|
axis along which |
None
|
ratio |
AxesParams
|
float or sequence of floats describing what proportion of cropping to apply on the left sides of cropping axes. Remaining ratio of cropping will be applied on the right sides |
0.5
|
Returns:
Name | Type | Description |
---|---|---|
cropped |
ndarray
|
cropped array |
Examples:
x # array of shape [2, 3, 4]
cropped = crop_to_shape(x, [1, 2, 3], ratio=0) # crop to shape [1, 2, 3] from the right
cropped = crop_to_shape(x, 2, axis=1, ratio=1) # crop to shape [2, 2, 4] from the left
cropped = crop_to_shape(x, [3, 4, 5]) # fail due to bigger resulting shape
Source code in imops/crop.py
imops.crop.crop_to_box(x, box, axis=None, padding_values=None, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Crop x
according to box
along axis
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array |
required |
box |
ndarray
|
array of shape (2, x.ndim or len(axis) if axis is passed) describing crop boundaries |
required |
axis |
AxesLike
|
axis along which |
None
|
padding_values |
AxesParams
|
values to pad with if box exceeds the input's limits |
None
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
cropped |
ndarray
|
cropped array |
Examples:
x # array of shape [2, 3, 4]
cropped = crop_to_box(x, np.array([[0, 0, 0], [1, 1, 1]])) # crop to shape [1, 1, 1]
cropped = crop_to_box(x, np.array([[0, 0, 0], [5, 5, 5]])) # fail, box exceeds the input's limits
cropped = crop_to_box(x, np.array([[0], [5]]), axis=0, padding_values=0) # pad with 0-s to shape [5, 3, 4]
Source code in imops/crop.py
imops.pad.pad(x, padding, axis=None, padding_values=0, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Pad x
according to padding
along the axis
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array to pad |
required |
padding |
Union[AxesLike, Sequence[Sequence[int]]]
|
if 2D array [[start_1, stop_1], ..., [start_n, stop_n]] - specifies individual padding
for each axis from |
required |
axis |
AxesLike
|
axis along which |
None
|
padding_values |
Union[AxesParams, Callable]
|
values to pad with, must be broadcastable to the resulting array.
If Callable (e.g. |
0
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
padded |
ndarray
|
padded array |
Examples:
padded = pad(x, 2) # pad 2 zeros on each side of each axes
padded = pad(x, [1, 1], axis=(-1, -2)) # pad 1 zero on each side of last 2 axes
Source code in imops/pad.py
imops.pad.pad_to_shape(x, shape, axis=None, padding_values=0, ratio=0.5, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Pad x
to match shape
along the axis
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array to pad |
required |
shape |
AxesLike
|
final shape |
required |
axis |
AxesLike
|
axis along which |
None
|
padding_values |
Union[AxesParams, Callable]
|
values to pad with, must be broadcastable to the resulting array.
If Callable (e.g. |
0
|
ratio |
AxesParams
|
float or sequence of floats describing what proportion of padding to apply on the left sides of padding axes. Remaining ratio of padding will be applied on the right sides |
0.5
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
padded |
ndarray
|
padded array |
Examples:
padded = pad_to_shape(x, [4, 5, 6]) # pad 3d array
padded = pad_to_shape(x, [4, 5], axis=[0, 1], ratio=0) # pad first 2 axes on the right
Source code in imops/pad.py
imops.pad.pad_to_divisible(x, divisor, axis=None, padding_values=0, ratio=0.5, remainder=0, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Pad x
to be divisible by divisor
along the axis
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array to pad |
required |
divisor |
AxesLike
|
float or sequence of floats an incoming array shape will be divisible by |
required |
axis |
AxesLike
|
axis along which the array will be padded. If None - the last |
None
|
padding_values |
Union[AxesParams, Callable]
|
values to pad with. If Callable (e.g. |
0
|
ratio |
AxesParams
|
float or sequence of floats describing what proportion of padding to apply on the left sides of padding axes. Remaining ratio of padding will be applied on the right sides |
0.5
|
remainder |
AxesLike
|
|
0
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
padded |
ndarray
|
padded array |
Examples:
x # array of shape [2, 3, 4]
padded = pad_to_divisible(x, 6) # pad to shape [6, 6, 6]
padded = pad_to_divisible(x, [4, 3], axis=[0, 1], ratio=1) # pad first 2 axes on the left, shape - [4, 3, 4]
padded = pad_to_divisible(x, 3, remainder=1) # pad to shape [4, 4, 4]
Source code in imops/pad.py
imops.pad.restore_crop(x, box, shape, padding_values=0, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Pad x
to match shape
. The left padding is taken equal to box
's start.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array to pad |
required |
box |
ndarray
|
array of shape (2, x.ndim) describing crop boundaries |
required |
shape |
AxesLike
|
shape to restore crop to |
required |
padding_values |
Union[AxesParams, Callable]
|
values to pad with. If Callable (e.g. |
0
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
padded |
ndarray
|
padded array |
Examples:
x # array of shape [2, 3, 4]
padded = restore_crop(x, np.array([[0, 0, 0], [2, 3, 4]]), [4, 4, 4]) # pad to shape [4, 4, 4]
padded = restore_crop(x, np.array([[0, 0, 0], [1, 1, 1]]), [4, 4, 4]) # fail, box is inconsistent with an array
padded = restore_crop(x, np.array([[1, 2, 3], [3, 5, 7]]), [3, 5, 7]) # pad to shape [3, 5, 7]
Source code in imops/pad.py
imops.zoom.zoom(x, scale_factor, axis=None, order=1, fill_value=0, num_threads=-1, backend=None)
Rescale x
according to scale_factor
along the axis
.
Uses a fast parallelizable implementation for fp32-fp64 and bool-int16-32-64-uint8-16-32 if order == 0 inputs, ndim <= 4 and order = 0 or 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array |
required |
scale_factor |
AxesParams
|
float or sequence of floats describing how to scale along axes |
required |
axis |
AxesLike
|
axis along which array will be scaled |
None
|
order |
int
|
order of interpolation |
1
|
fill_value |
Union[float, Callable]
|
value to fill past edges. If Callable (e.g. |
0
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
zoomed |
ndarray
|
zoomed array |
Examples:
zoomed = zoom(x, 2, axis=[0, 1]) # 3d array
zoomed = zoom(x, [1, 2, 3]) # different scales along each axes
zoomed = zoom(x.astype(int)) # will fall back to scipy's implementation because of int dtype
Source code in imops/zoom.py
imops.zoom.zoom_to_shape(x, shape, axis=None, order=1, fill_value=0, num_threads=-1, backend=None)
Rescale x
to match shape
along the axis
.
Uses a fast parallelizable implementation for fp32-fp64 and bool-int16-32-64-uint8-16-32 if order == 0 inputs, ndim <= 4 and order = 0 or 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
n-dimensional array |
required |
shape |
AxesLike
|
float or sequence of floats describing desired lengths along axes |
required |
axis |
AxesLike
|
axis along which array will be scaled |
None
|
order |
int
|
order of interpolation |
1
|
fill_value |
Union[float, Callable]
|
value to fill past edges. If Callable (e.g. |
0
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
zoomed |
ndarray
|
zoomed array |
Examples:
zoomed = zoom_to_shape(x, [3, 4, 5]) # 3d array
zoomed = zoom_to_shape(x, [6, 7], axis=[1, 2]) # zoom to shape along specified axes
zoomed = zoom_to_shape(x.astype(int)) # will fall back to scipy's implementation because of int dtype
Source code in imops/zoom.py
imops.interp1d.interp1d
Faster parallelizable version of scipy.interpolate.interp1d
for fp32 / fp64 inputs.
Works faster only for ndim <= 3. Shares interface with scipy.interpolate.interp1d
except for num_threads
and
backend
arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
1-dimensional array of real values (aka coordinates) |
required |
y |
ndarray
|
n-dimensional array of real values. The length of y along the interpolation axis must be equal to the x length |
required |
kind |
Union[int, str]
|
specifies the kind of interpolation as a string or as an integer specifying the order of interpolation to use.
Only kind=1 and 'linear |
'linear'
|
axis |
int
|
specifies the axis of y along which to interpolate. Interpolation defaults to the last axis of y |
-1
|
copy |
bool
|
if True, the class makes internal copies of x and y. If False, references to x and y are used |
True
|
bounds_error |
bool
|
if True, a ValueError is raised any time interpolation is attempted on a value outside of the range of x where extrapolation is necessary. If False, out of bounds values are assigned fill_value. By default, an error is raised unless fill_value='extrapolate' |
None
|
fill_value |
Union[float, str]
|
if a float, this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. If 'extrapolate', values for points outside of the data range will be extrapolated |
nan
|
assume_sorted |
bool
|
if False, values of x can be in any order and they are sorted first. If True, x has to be an array of monotonically increasing values |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Methods:
Name | Description |
---|---|
__call__ |
|
Examples:
import numpy as np
from imops.interp1d import interp1d
x = np.arange(0, 10)
y = np.exp(-x/3.0)
f = interp1d(x, y)
xnew = np.arange(0, 9, 0.1)
ynew = f(xnew) # use interpolation function returned by `interp1d`
Source code in imops/interp1d.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
__call__(x_new)
Evaluate the interpolant
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_new |
ndarray
|
1d array points to evaluate the interpolant at. |
required |
Returns:
Name | Type | Description |
---|---|---|
y_new |
ndarray
|
interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x |
Source code in imops/interp1d.py
imops.interp2d.Linear2DInterpolator
Bases: Linear2DInterpolatorCpp
2D Delaunay triangulation and parallel linear interpolation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
ndarray
|
2-D array of data point coordinates |
required |
values |
ndarray
|
1-D array of fp32/fp64 values |
None
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
1
|
triangels |
optional precomputed triangulation in the form of array or arrays of points indices |
required |
Methods:
Name | Description |
---|---|
__call__ |
|
Examples:
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
interpolator = Linear2DInterpolator(x_points, values, num_threads)
# 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)
Source code in imops/interp2d.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
__call__(points, values=None, fill_value=0.0)
Evaluate the interpolant
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
ndarray
|
2-D array of data point coordinates to interpolate at |
required |
values |
ndarray
|
1-D array of fp32/fp64 values to use at initial points |
None
|
fill_value |
float
|
value to fill past edges |
0.0
|
Returns:
Name | Type | Description |
---|---|---|
new_values |
ndarray
|
interpolated values at given points |
Source code in imops/interp2d.py
imops.morphology.binary_dilation(image, footprint=None, output=None, boxed=False, num_threads=-1, backend=None)
Fast parallelizable binary morphological dilation of an image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
input image |
required |
footprint |
ndarray
|
the neighborhood expressed as a n-D array of 1's and 0's. If None, use a cross-shaped footprint (connectivity=1) |
None
|
output |
ndarray
|
array of the same shape as input, into which the output is placed (must be C-contiguous). By default, a new array is created |
None
|
boxed |
bool
|
if True, dilation is performed on cropped image which may speed up computation depedning on how localized True pixels are. This may induce differences with Scikit-Image implementation at border pixels if footprint is exotic (has even shape or center pixel is False) |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
dilated |
ndarray
|
the result of morphological dilation |
Examples:
Source code in imops/morphology.py
imops.morphology.binary_erosion(image, footprint=None, output=None, boxed=False, num_threads=-1, backend=None)
Fast parallelizable binary morphological erosion of an image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
input image |
required |
footprint |
ndarray
|
the neighborhood expressed as a n-D array of 1's and 0's. If None, use a cross-shaped footprint (connectivity=1) |
None
|
output |
ndarray
|
array of the same shape as input, into which the output is placed (must be C-contiguous). By default, a new array is created |
None
|
boxed |
bool
|
if True, erosion is performed on cropped image which may speed up computation depedning on how localized True pixels are. This may induce differences with Scikit-Image implementation at border pixels if footprint is exotic (has even shape or center pixel is False) |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
eroded |
ndarray
|
the result of morphological erosion |
Examples:
Source code in imops/morphology.py
imops.morphology.binary_opening(image, footprint=None, output=None, boxed=False, num_threads=-1, backend=None)
Fast parallelizable binary morphological opening of an image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
input image |
required |
footprint |
ndarray
|
the neighborhood expressed as a n-D array of 1's and 0's. If None, use a cross-shaped footprint (connectivity=1) |
None
|
output |
ndarray
|
array of the same shape as input, into which the output is placed (must be C-contiguous). By default, a new array is created |
None
|
boxed |
bool
|
if True, opening is performed on cropped image which may speed up computation depedning on how localized True pixels are. This may induce differences with Scikit-Image implementation at border pixels if footprint is exotic (has even shape or center pixel is False) |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
opened |
ndarray
|
the result of morphological opening |
Examples:
Source code in imops/morphology.py
imops.morphology.binary_closing(image, footprint=None, output=None, boxed=False, num_threads=-1, backend=None)
Fast parallelizable binary morphological closing of an image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
input image |
required |
footprint |
ndarray
|
the neighborhood expressed as a n-D array of 1's and 0's. If None, use a cross-shaped footprint (connectivity=1) |
None
|
output |
ndarray
|
array of the same shape as input, into which the output is placed (must be C-contiguous). By default, a new array is created |
None
|
boxed |
bool
|
if True, closing is performed on cropped image which may speed up computation depedning on how localized True pixels are. This may induce differences with Scikit-Image implementation at border pixels if footprint is exotic (has even shape or center pixel is False) |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
closed |
ndarray
|
the result of morphological closing |
Examples:
Source code in imops/morphology.py
imops.morphology.distance_transform_edt(image, sampling=None, return_distances=True, return_indices=False, num_threads=-1, backend=None)
Fast parallelizable Euclidean distance transform for <= 3D inputs
This function calculates the distance transform of the image
, by
replacing each foreground (non-zero) element, with its
shortest distance to the background (any zero-valued element).
In addition to the distance transform, the feature transform can be calculated. In this case the index of the closest background element to each foreground element is returned in a separate array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
array_like
|
input data to transform. Can be any type but will be converted into binary: 1 wherever input equates to True, 0 elsewhere |
required |
sampling |
tuple of `image.ndim` floats
|
spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied |
None
|
return_distances |
bool
|
whether to calculate the distance transform. Default is True |
True
|
return_indices |
bool
|
whether to calculate the feature transform. Default is False |
False
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed cpu count + num_threads + 1 threads will be used |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
distances |
float32 ndarray, optional
|
the calculated distance transform. Returned only when
|
indices |
int32 ndarray, optional
|
the calculated feature transform. It has an input-shaped array for each
dimension of the input. See example below.
Returned only when |
Notes
The Euclidean distance transform gives values of the Euclidean distance::
n
y_i = sqrt(sum (x[i]-b[i])**2) i
where b[i] is the background point (value 0) with the smallest Euclidean distance to input points x[i], and n is the number of dimensions.
Examples:
import numpy as np a = np.array(([0,1,1,1,1], [0,0,1,1,1], [0,1,1,1,1], [0,1,1,1,0], [0,1,1,0,0])) distance_transform_edt(a) array([[ 0. , 1. , 1.4142, 2.2361, 3. ], [ 0. , 0. , 1. , 2. , 2. ], [ 0. , 1. , 1.4142, 1.4142, 1. ], [ 0. , 1. , 1.4142, 1. , 0. ], [ 0. , 1. , 1. , 0. , 0. ]])
With a sampling of 2 units along x, 1 along y:
distance_transform_edt(a, sampling=[2, 1]) array([[ 0. , 1. , 2. , 2.8284, 3.6056], [ 0. , 0. , 1. , 2. , 3. ], [ 0. , 1. , 2. , 2.2361, 2. ], [ 0. , 1. , 2. , 1. , 0. ], [ 0. , 1. , 1. , 0. , 0. ]])
Asking for indices as well:
edt, inds = distance_transform_edt(a, return_indices=True) inds array([[[0, 0, 1, 1, 3], [1, 1, 1, 1, 3], [2, 2, 1, 3, 3], [3, 3, 4, 4, 3], [4, 4, 4, 4, 4]], [[0, 0, 1, 1, 4], [0, 1, 1, 1, 4], [0, 0, 1, 4, 4], [0, 0, 3, 3, 4], [0, 0, 3, 3, 4]]])
Source code in imops/morphology.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
|
imops.measure.label(label_image, background=None, connectivity=None, return_num=False, return_labels=False, return_sizes=False, dtype=None)
Fast version of skimage.measure.label
which optionally returns number of connected components, labels and sizes.
If 2 or more outputs are requested NamedTuple
is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label_image |
ndarray
|
image to label |
required |
background |
int
|
consider all pixels with this value as background pixels, and label them as 0. By default, 0-valued pixels are considered as background pixels |
None
|
connectivity |
int
|
maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. If None, a full connectivity of input.ndim is used |
None
|
return_num |
bool
|
whether to return the number of connected components |
False
|
return_labels |
bool
|
whether to return assigned labels |
False
|
return_sizes |
bool
|
whether to return sizes of connected components (excluding background) |
False
|
dtype |
type
|
if specified, must be one of np.uint16, np.uint32 or np.uint64. If not specified, it will be automatically determined. Most of the time, you should leave this off so that the smallest safe dtype will be used. However, in some applications you can save an up-conversion in the next operation by outputting the appropriately sized type instead. Has no effect for python3.6 |
None
|
Returns:
Name | Type | Description |
---|---|---|
labeled_image |
ndarray
|
array of np.uint16, np.uint32 or np.uint64 numbers depending on the number of connected components and
|
num_components |
int
|
number of connected components excluding background. Returned if |
labels |
ndarray
|
components labels. Returned if |
sizes |
ndarray
|
components sizes. Returned if |
Examples:
labeled = label(x)
labeled, num_components, sizes = label(x, return_num=True, return_sizes=True)
out = label(x, return_labels=True, return_sizes=True)
out.labeled_image, out.labels, out.sizes # output fields can be accessed this way
Source code in imops/measure.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
imops.measure.center_of_mass(array, labels=None, index=None, num_threads=-1, backend=None)
Calculate the center of mass of the values.
Works faster for ndim <= 3
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ndarray
|
data from which to calculate center-of-mass. The masses can either be positive or negative |
required |
labels |
ndarray
|
labels for objects in input, as generated by |
None
|
index |
Union[int, Sequence[int]]
|
labels for which to calculate centers-of-mass. If specified, |
None
|
num_threads |
int
|
the number of threads to use for computation. Default = the cpu count. If negative value passed
cpu count + num_threads + 1 threads will be used. If |
-1
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
center_of_mass |
tuple, or list of tuples
|
coordinates of centers-of-mass |
Examples:
Source code in imops/measure.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
imops.numeric.pointwise_add(nums, summand, output=None, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Perform pointwise addition between array and array or scalar.
Uses a fast parallelizable implementation for fp16-32-64 and int16-32-64 inputs and ndim <= 4.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nums |
ndarray
|
n-dimensional array |
required |
summand |
Union[array, int, float]
|
array of the same shape or scalar |
required |
output |
ndarray
|
array of the same shape as input, into which the output is placed. By default, a new array is created |
None
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
sum |
ndarray
|
result of summation |
Examples:
sum = pointwise_add(x, 1, x) # inplace addition
sum = pointwise_add(x, 1, backend='Scipy') # just `np.add`
sum = pointwise_add(x.astype('float32'), x.astype('float16')) # will fail because of different dtypes
Source code in imops/numeric.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
imops.numeric.fill_(nums, value, num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Fill the array with a scalar value.
Uses a fast parallelizable implementation for fp16-32-64 and int16-32-64 inputs and ndim <= 4.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nums |
ndarray
|
n-dimensional array |
required |
value |
Union[number, int, float]
|
scalar |
required |
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Examples:
fill_(x, 1)
fill_(np.empty((2, 3, 4)), 42)
fill_(x.astype('uint16'), 3) # will fail because of unsupported uint16 dtype
Source code in imops/numeric.py
imops.numeric.full(shape, fill_value, dtype=None, order='C', num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Return a new array of given shape and dtype, filled with fill_value
.
Uses a fast parallelizable implementation for fp16-32-64 and int16-32-64 inputs and ndim <= 4.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape |
Union[int, Sequence[int]]
|
desired shape |
required |
fill_value |
Union[number, int, float]
|
scalar to fill array with |
required |
dtype |
Union[type, str]
|
desired dtype to which |
None
|
order |
str
|
whether to store multidimensional data in C or F contiguous order in memory |
'C'
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Examples:
x = full((2, 3, 4), 1.0) # same as `np.ones((2, 3, 4))`
x = full((2, 3, 4), 1.5, dtype=int) # same as np.ones((2, 3, 4), dtype=int)
x = full((2, 3, 4), 1, dtype='uint16') # will fail because of unsupported uint16 dtype
Source code in imops/numeric.py
imops.numeric.copy(nums, output=None, order='K', num_threads=_NUMERIC_DEFAULT_NUM_THREADS, backend=None)
Return copy of the given array.
Uses a fast parallelizable implementation for fp16-32-64 and int16-32-64 inputs and ndim <= 4.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nums |
ndarray
|
n-dimensional array |
required |
output |
ndarray
|
array of the same shape and dtype as input, into which the copy is placed. By default, a new array is created |
None
|
order |
str
|
controls the memory layout of the copy. |
'K'
|
num_threads |
int
|
the number of threads to use for computation. Default = 4. If negative value passed cpu count + num_threads + 1 threads will be used |
_NUMERIC_DEFAULT_NUM_THREADS
|
backend |
BackendLike
|
which backend to use. |
None
|
Returns:
Name | Type | Description |
---|---|---|
copy |
ndarray
|
copy of array |
Examples:
copied = copy(x)
copied = copy(x, backend='Scipy') # same as `np.copy`
copy(x, output=y) # copied into `y`
Source code in imops/numeric.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
|
imops.radon.radon(image, axes=None, theta=180, return_fill=False, num_threads=-1, backend=None)
Fast implementation of Radon transform. Adapted from scikit-image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
an n-dimensional array with at least 2 axes |
required |
axes |
Tuple[int, int]
|
the axes in the |
None
|
theta |
Union[int, Sequence[float]]
|
the angles for which the Radon transform will be computed. If it is an integer - the angles will
be evenly distributed between 0 and 180, |
180
|
return_fill |
bool
|
whether to return the value that fills the image outside the circle working area |
False
|
num_threads |
int
|
the number of threads to be used for parallel computation. By default - equals to the number of cpu cores |
-1
|
backend |
BackendLike
|
the execution backend. Currently only "Cython" is avaliable |
None
|
Returns:
Name | Type | Description |
---|---|---|
sinogram |
ndarray
|
the result of the Radon transform |
fill_value |
float
|
the value that fills the image outside the circle working area. Returned only if |
Examples:
sinogram = radon(image) # 2d image
sinogram, fill_value = radon(image, return_fill=True) # 2d image with fill value
sinogram = radon(image, axes=(-2, -1)) # nd image
Source code in imops/radon.py
imops.radon.inverse_radon(sinogram, axes=None, theta=None, fill_value=0, a=0, b=1, num_threads=-1, backend=None)
Fast implementation of inverse Radon transform. Adapted from scikit-image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sinogram |
ndarray
|
an n-dimensional array with at least 2 axes |
required |
axes |
Tuple[int, int]
|
the axes in the |
None
|
theta |
Union[int, Sequence[float]]
|
the angles for which the inverse Radon transform will be computed. If it is an integer - the angles will
be evenly distributed between 0 and 180, |
None
|
fill_value |
float
|
the value that fills the image outside the circle working area. Can be returned by |
0
|
a |
float
|
the first parameter of the sharpen filter |
0
|
b |
float
|
the second parameter of the sharpen filter |
1
|
num_threads |
int
|
the number of threads to be used for parallel computation. By default - equals to the number of cpu cores |
-1
|
backend |
BackendLike
|
the execution backend. Currently only "Cython" is avaliable |
None
|
Returns:
Name | Type | Description |
---|---|---|
image |
ndarray
|
the result of the inverse Radon transform |
Examples:
image = inverse_radon(sinogram) # 2d image
image = inverse_radon(sinogram, fill_value=-1000) # 2d image with fill value
image = inverse_radon(sinogram, axes=(-2, -1)) # nd image
Source code in imops/radon.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
imops.utils.isin(element, test_elements, num_threads=1)
Calculates element in test_elements
, broadcasting over element
only.
Returns a boolean array of the same shape as element
that is True where
an element of element
is in test_elements
and False otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
ndarray
|
n-dimensional array |
required |
test_elements |
ndarray
|
1-d array of the values against which to test each value of element |
required |
num_threads |
int
|
the number of threads to use for computation. Default = 1. If negative value passed cpu count + num_threads + 1 threads will be used |
1
|
Returns:
Name | Type | Description |
---|---|---|
isin |
(ndarray, bool)
|
has the same shape as |
Examples:
element = 2*np.arange(4).reshape((2, 2)) test_elements = [1, 2, 4, 8] mask = isin(element, test_elements)