Skip to main content

Fast and simple to use 2D and 3D structure tensor implementation for Python.

Project description

Structure Tensor for Python

Fast and simple to use 2D and 3D structure tensor implementation for Python.

Installation

Install package using pip install structure-tensor or clone the repository.

CUDA Support

For CUDA support install extra (optional) dependancy CuPy. If CUDA is installed on your system, pip install cupy should be enough, but may be slow as CuPy will compile code during install. Alternatively use one of the precompiled packages.

Tiny Examples

The parameters for the structure tensor calculations are $\rho$ (rho) and $\sigma$ (sigma), which are scalar values.

2D and 3D using NumPy

The structure_tensor package support doing either 2D or 3D structure tensor analysis. Eigenvalues (val) are sorted acending.

import numpy as np
from structure_tensor import eig_special_2d, structure_tensor_2d

sigma = 1.5
rho = 5.5

# Load 2D data.
image = np.random.random((128, 128))

S = structure_tensor_3d(image, sigma, rho)
val, vec = eig_special_3d(S)

For volume with shape (x, y, z) the eigenvectors (vec) are returned as zyx.

import numpy as np
from structure_tensor import eig_special_3d, structure_tensor_3d

sigma = 1.5
rho = 5.5

# Load 3D data.
volume = np.random.random((128, 128, 128))

S = structure_tensor_3d(volume, sigma, rho)
val, vec = eig_special_3d(S)

3D using CuPy

CuPy functions are available in the structure_tensor.cp module. They work similar to their NumPy counterparts, except that they return cupy.ndarrays. The functions will automatically handle moving input data if necessary.

import cupy as cp
import numpy as np
from structure_tensor.cp import eig_special_3d, structure_tensor_3d

sigma = 1.5
rho = 5.5

# Load 3D data.
volume = np.random.random((128, 128, 128))

S = structure_tensor_3d(volume, sigma, rho)
val, vec = eig_special_3d(S)

# Convert from cupy to numpy. Moves data from GPU to CPU.
val = cp.asnumpy(val)
vec = cp.asnumpy(vec)

Contributions

Contributions are welcome, just create an issue or a PR.

More information

License

MIT License (see LICENSE file).

Reference

See CuPy reference section.

Project details


Release history Release notifications

Download files

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

Files for structure-tensor, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size structure-tensor-0.1.0.tar.gz (9.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page