[![Tests](https://github.com/schlegelp/xform/actions/workflows/test-package.yml/badge.svg)](https://github.com/schlegelp/xform/actions/workflows/test-package.yml)
# 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](https://github.com/schlegelp/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](https://www.nitrc.org/docman/?group_id=212) warp transforms
- [Elastix](https://github.com/SuperElastix/elastix) warp transforms
- [H5 deformation fields](https://github.com/saalfeldlab/template-building/wiki/Hdf5-Deformation-fields)
- Landmark-based thin-plate spline transforms (powered by [morphops](https://github.com/vaipatel/morphops))
- Landmark-based least-moving square transforms (powered by [molesq](https://github.com/clbarnes/molesq))
- Affine transformations
### Install
```
$ pip3 install xform
```
Additional dependencies:
To use CMTK transforms, you need to have [CMTK](https://www.nitrc.org/docman/?group_id=212)
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`:
- `AffineTransform` for affine transforms using a
[affine matrix](https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations)
- `CMTKtransform` for CMTK transforms
- `ElastixTransform` for Elastix transforms
- `TPStransform` or `MovingLeastSquaresTransform` for landmark-based transforms
- `H5transform` for deformation-field transforms using Hdf5 files
([specs](https://github.com/saalfeldlab/template-building/wiki/Hdf5-Deformation-fields))
A quick example that uses an affine transform to scale coordinates by a factor
of 2:
```Python
>>> 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:
```Python
>>> 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:
```Python
>>> 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
Raw data
{
"_id": null,
"home_page": "http://xform.readthedocs.io",
"name": "xform",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "Transform CMTK affine landmarks thin-plate spline",
"author": "Philipp Schlegel",
"author_email": "pms70@cam.ac.uk",
"download_url": "https://files.pythonhosted.org/packages/24/c8/10a3014e140ea36294728a0d0c10c2642ee9f0800bc6721234d3d770af67/xform-0.1.0.tar.gz",
"platform": null,
"description": "[![Tests](https://github.com/schlegelp/xform/actions/workflows/test-package.yml/badge.svg)](https://github.com/schlegelp/xform/actions/workflows/test-package.yml)\n\n# xform [WIP]\n`xform` is a library to transform spatial data from one space to another and\nprovides a common interface to combine different types of transforms.\n\nIt was originally written for [navis](https://github.com/schlegelp/navis)\nto transform neurons from one brain template space to another and then\nsplit off into a separate general-purpose package.\n\n### Features\n- various supported transforms (see below)\n- chaining of transforms\n- a template registry that tracks available transforms and plots paths to\n get from a given source to the desired target template space\n\n### Supported transforms\n\n- [CMTK](https://www.nitrc.org/docman/?group_id=212) warp transforms\n- [Elastix](https://github.com/SuperElastix/elastix) warp transforms\n- [H5 deformation fields](https://github.com/saalfeldlab/template-building/wiki/Hdf5-Deformation-fields)\n- Landmark-based thin-plate spline transforms (powered by [morphops](https://github.com/vaipatel/morphops))\n- Landmark-based least-moving square transforms (powered by [molesq](https://github.com/clbarnes/molesq))\n- Affine transformations\n\n\n### Install\n\n```\n$ pip3 install xform\n```\n\nAdditional dependencies:\n\nTo use CMTK transforms, you need to have [CMTK](https://www.nitrc.org/docman/?group_id=212)\ninstalled and its binaries (specifically `streamxform`) in a path where `xform`\ncan find them (e.g. `/usr/local/bin`).\n\n\n### Usage\n\n#### Single transforms\n\nAt the most basic level you can use individual transform from `xform.transforms`:\n\n- `AffineTransform` for affine transforms using a\n [affine matrix](https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations)\n- `CMTKtransform` for CMTK transforms\n- `ElastixTransform` for Elastix transforms\n- `TPStransform` or `MovingLeastSquaresTransform` for landmark-based transforms\n- `H5transform` for deformation-field transforms using Hdf5 files\n ([specs](https://github.com/saalfeldlab/template-building/wiki/Hdf5-Deformation-fields))\n\nA quick example that uses an affine transform to scale coordinates by a factor\nof 2:\n\n```Python\n>>> import xform\n>>> import numpy as np\n>>> # Generate the affine matrix\n>>> m = np.diag([2, 2, 2, 2])\n>>> # Create the transform\n>>> tr = xform.AffineTransform(m)\n>>> # Some 3D points to transform\n>>> points = np.array([[1,1,1], [2,2,2], [3,3,3]])\n>>> # Apply\n>>> xf = tr.xform(points)\n>>> xf\narray([[2., 2., 2.],\n [4., 4., 4.],\n [6., 6., 6.]])\n>>> # Transforms are invertible!\n>>> (-tr).xform(xf)\narray([[1., 1., 1.],\n [2., 2., 2.],\n [3., 3., 3.]])\n```\n\n#### Transform sequences\n\nIf you find yourself in a situation where you need to chain some transforms,\nyou can use `xform.transforms.TransformSequence` to combine transforms.\n\nFor example, let's say we have a CMTK transform that requires spatial data to\nbe in microns but our data is in nanometers:\n\n```Python\n>>> from xform import CMTKtransform, AffineTransform, TransformSequence\n>>> import numpy as np\n>>> # Initialize CMTK transform\n>>> cmtk = CMTKtransform('~/transform/target_source.list')\n>>> # Create an affine transform to go from microns to nanometers\n>>> aff = AffineTransform(np.diag([1e3, 1e3, 1e3, 1e3]))\n>>> # Create a transform sequence\n>>> tr = TransformSequence([-aff, cmtk])\n>>> # Apply transform\n>>> points = np.array([[1,1,1], [2,2,2], [3,3,3]])\n>>> xf = tr.xform(points)\n```\n\n#### Bridging graphs\n\nWhen working with many interconnected transforms (e.g. A->B, B->C, B->D, etc.),\nyou can register the individual transforms and let `xform` plot the shortest\npath to get from a given source to a given target for you:\n\n```Python\n>>> import xform\n>>> from xform import CMTKtransform, AffineTransform, TransformRegistry\n>>> import numpy as np\n>>> # Create a transform registry\n>>> registry = TransformRegistry()\n>>> # Generate a couple transforms\n>>> # Note that we now provide source and target labels\n>>> tr1 = AffineTransform(np.diag([1e3, 1e3, 1e3, 1e3]),\n... source_space='A', target_space='B')\n>>> cmtk = CMTKtransform('~/transform/C_B.list',\n... source_space='B', target_space='C')\n>>> # Register the transforms\n>>> registry.register_transform([tr1, cmtk])\n>>> # Now you ask the registry for the required transforms to move between spaces\n>>> path, trans_seq = registry.shortest_bridging_seq(source='A', target='C')\n>>> path\narray(['A', 'B', 'C'], dtype='<U1')\n>>> trans_seq\nTransformSequence with 2 transform(s)\n```\n\n#### Custom transforms\n\nTODO\n",
"bugtrack_url": null,
"license": "GNU GPL V3",
"summary": "Library for transforming data",
"version": "0.1.0",
"project_urls": {
"Documentation": "http://xform.readthedocs.io",
"Homepage": "http://xform.readthedocs.io",
"Source": "https://github.com/schlegelp/xform"
},
"split_keywords": [
"transform",
"cmtk",
"affine",
"landmarks",
"thin-plate",
"spline"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "65aeff4a6f53e3a46856bee410aec5a5a8047dcf9b665915dc79db79334a91a4",
"md5": "2e6593f464aa708d6e8bac026277c77d",
"sha256": "91fa8963fdd0173da18e5537a788112fff7a8330e2799c0e98ead79c64450c11"
},
"downloads": -1,
"filename": "xform-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e6593f464aa708d6e8bac026277c77d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 50301,
"upload_time": "2024-02-10T11:01:14",
"upload_time_iso_8601": "2024-02-10T11:01:14.070789Z",
"url": "https://files.pythonhosted.org/packages/65/ae/ff4a6f53e3a46856bee410aec5a5a8047dcf9b665915dc79db79334a91a4/xform-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "24c810a3014e140ea36294728a0d0c10c2642ee9f0800bc6721234d3d770af67",
"md5": "3f79b4ee67cbdfbf281bcf947b4b40c5",
"sha256": "6d2281b6565e5b2172e7c871269bd84c705e6bf9e0db05ed945e17e882ea717d"
},
"downloads": -1,
"filename": "xform-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "3f79b4ee67cbdfbf281bcf947b4b40c5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 41086,
"upload_time": "2024-02-10T11:01:15",
"upload_time_iso_8601": "2024-02-10T11:01:15.776084Z",
"url": "https://files.pythonhosted.org/packages/24/c8/10a3014e140ea36294728a0d0c10c2642ee9f0800bc6721234d3d770af67/xform-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-10 11:01:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "schlegelp",
"github_project": "xform",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "xform"
}