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_2d(image, sigma, rho)
val, vec = eig_special_2d(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


Download files

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

Source Distribution

structure-tensor-0.2.0.tar.gz (12.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page