xform [WIP]
xform is a library to transform spatial data from one space to another and
provides a common interface to combine different types of transforms.
It was originally written for navis to transform neurons from one brain template space to another and then split off into a separate general-purpose package.
Features
- various supported transforms (see below)
- chaining of transforms
- a template registry that tracks available transforms and plots paths to get from a given source to the desired target template space
Supported transforms
- CMTK warp transforms
- Elastix warp transforms
- H5 deformation fields
- Landmark-based thin-plate spline transforms (powered by morphops)
- Landmark-based least-moving square transforms (powered by molesq)
- Affine transformations
Install
$ pip3 install xform
Additional dependencies:
To use CMTK transforms, you need to have CMTK
installed and its binaries (specifically streamxform) in a path where xform
can find them (e.g. /usr/local/bin).
Usage
Single transforms
At the most basic level you can use individual transform from xform.transforms:
AffineTransformfor affine transforms using a affine matrixCMTKtransformfor CMTK transformsElastixTransformfor Elastix transformsTPStransformorMovingLeastSquaresTransformfor landmark-based transformsH5transformfor deformation-field transforms using Hdf5 files (specs)
A quick example that uses an affine transform to scale coordinates by a factor of 2:
>>> import xform
>>> import numpy as np
>>> # Generate the affine matrix
>>> m = np.diag([2, 2, 2, 2])
>>> # Create the transform
>>> tr = xform.AffineTransform(m)
>>> # Some 3D points to transform
>>> points = np.array([[1,1,1], [2,2,2], [3,3,3]])
>>> # Apply
>>> xf = tr.xform(points)
>>> xf
array([[2., 2., 2.],
[4., 4., 4.],
[6., 6., 6.]])
>>> # Transforms are invertible!
>>> (-tr).xform(xf)
array([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]])
Transform sequences
If you find yourself in a situation where you need to chain some transforms,
you can use xform.transforms.TransformSequence to combine transforms.
For example, let's say we have a CMTK transform that requires spatial data to be in microns but our data is in nanometers:
>>> from xform import CMTKtransform, AffineTransform, TransformSequence
>>> import numpy as np
>>> # Initialize CMTK transform
>>> cmtk = CMTKtransform('~/transform/target_source.list')
>>> # Create an affine transform to go from microns to nanometers
>>> aff = AffineTransform(np.diag([1e3, 1e3, 1e3, 1e3]))
>>> # Create a transform sequence
>>> tr = TransformSequence([-aff, cmtk])
>>> # Apply transform
>>> points = np.array([[1,1,1], [2,2,2], [3,3,3]])
>>> xf = tr.xform(points)
Bridging graphs
When working with many interconnected transforms (e.g. A->B, B->C, B->D, etc.),
you can register the individual transforms and let xform plot the shortest
path to get from a given source to a given target for you:
>>> import xform
>>> from xform import CMTKtransform, AffineTransform, TransformRegistry
>>> import numpy as np
>>> # Create a transform registry
>>> registry = TransformRegistry()
>>> # Generate a couple transforms
>>> # Note that we now provide source and target labels
>>> tr1 = AffineTransform(np.diag([1e3, 1e3, 1e3, 1e3]),
... source_space='A', target_space='B')
>>> cmtk = CMTKtransform('~/transform/C_B.list',
... source_space='B', target_space='C')
>>> # Register the transforms
>>> registry.register_transform([tr1, cmtk])
>>> # Now you ask the registry for the required transforms to move between spaces
>>> path, trans_seq = registry.shortest_bridging_seq(source='A', target='C')
>>> path
array(['A', 'B', 'C'], dtype='<U1')
>>> trans_seq
TransformSequence with 2 transform(s)
Custom transforms
TODO
Release files for xform 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| xform-0.1.0.tar.gz | 41.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xform-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 91.4 kB
Release files / xform-0.1.0.tar.gz
| Download URL | xform-0.1.0.tar.gz |
|---|---|
| Size | 41.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d2281b6565e5b2172e7c871269bd84c705e6bf9e0db05ed945e17e882ea717d
|
|
BLAKE2b-256 checksum How to use checksums |
24c810a3014e140ea36294728a0d0c10c2642ee9f0800bc6721234d3d770af67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / xform-0.1.0-py3-none-any.whl
| Download URL | xform-0.1.0-py3-none-any.whl |
|---|---|
| Size | 50.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
91fa8963fdd0173da18e5537a788112fff7a8330e2799c0e98ead79c64450c11
|
|
BLAKE2b-256 checksum How to use checksums |
65aeff4a6f53e3a46856bee410aec5a5a8047dcf9b665915dc79db79334a91a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|