Git Product home page Git Product logo

xform's Introduction

Tests

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

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:

  • AffineTransform for affine transforms using a affine matrix
  • CMTKtransform for CMTK transforms
  • ElastixTransform for Elastix transforms
  • TPStransform or MovingLeastSquaresTransform for landmark-based transforms
  • H5transform for 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

xform's People

Contributors

pnewstein avatar schlegelp avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pnewstein

xform's Issues

Discussion : Apply transform to a 3D image

Hello,
I was looking for python libraries to:

  • load CMTK transform obtained by other tools;
  • apply this CMTK transform to the 3D image that was used to compute this transform (registration on a template+atlas 3D image)
  • inverse the CMTK transform
  • apply the inverse CMTK transform to the ATLAS

I found yours libraries navis and xform and installed them.
However, I understand that this is actually not adapted to my 3D images but to points lists.
Do you have an idea if there are other python libraries I couls use for that, and else, how much work it be to adapt your tools to my task?

I thank you in advance for your reply,

Best regards

Empty PyPI listing

The PyPI listing for this project appears to be empty.

pip install xform
python -c "import xform"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'xform'

Installing from github works fine.

Quo vadis

Hi @clbarnes

I finished a first working version including some simple tests. Compared to the navis version:

  1. Moved a bunch of functions/modules around
  2. Renamed xform_brain -> xform_space and mirror_brain -> mirror_space
  3. Slimmed down the TemplateRegistry to bare minimum
  4. Already added source_space and target_space to all transforms because it made sense while working on the registry anyway

Re 2: for now, I dropped the distinction between bridging and mirror transforms and mirror/mirror_space are currently not working. In navis, mirroring requires a registered TemplateBrain (which is used to generate a Affine transform to flip the data) and an optional warping transform. Here, I was considering simply treating mirrors as a space of their own - e.g. you would have space1 and space1_mirr, and the transform from one to the other would minimally be just an Affine transform but could also be a TransformSequence. For mirror_space this would then be something along the lines of:

def mirror_space(x, template):
   # Check for mirror transform
   tr = registry.shortest_bridging_seq(source=template, target=f'{template}_mirr')
  
  return tr.xform(x)

I'm not entirely sold on this because (a) this is a rather opaque to the user and (b) won't work with just any hashable object. May need to go back to the registry tracking mirror vs bridging transforms.

Do you have thoughts on how to proceed from here? I'm primarily interested in preserving functionality required for navis but not at all married to how it's implemented so, from my end, I'd say have at it.

Wishlist: "shape-based" transform

Basically, what I want is a Transform that accepts two sets of points (e.g. from two neurons) and then finds an affine (i.e. non-warping) transform that minimises the distance between those points. This affine transform could be restricted to e.g. only use translation and rotation but not scaling.

One use case would be to align two neurons of the same cell type but from different segments.

I'm sure I've read about/seen something like this but I can't think of the proper term. @clbarnes: any idea of what I'm talking about?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.