Skip to main content

ND coordinate transformations

Project description

transformnd

Code style: black GitHub Test Status Docs Status

A library providing an API for coordinate transformations, as well as some common transforms. The goal is to allow downstream applications which require such transformations (e.g. image registration) to be generic over anything inheriting from transformnd.Transform.

The base classes and utilities are very lightweight with few dependencies, for use as an API; additional transforms and features use extras.

Heavily inspired by/ cribbed directly from Philipp Schlegel's work in navis; co-developed with xform as a red team prototype.

N coordinates in D dimensions are given as a numpy array of shape (N, D).

Transform subclasses which are restricted to certain dimensionalities can specify this in their ndim class variable. Instances of Transform subclasses can further restrict their ndim. Use self._validate_coords(coords) in __call__ to ensure the coordinates are of valid type and dimensions.

Additionally, transformnd provides an interface for transforming types other than NxD numpy arrays, and implements these adapters for a few common types.

See the tutorial here. It is a marimo notebook. Open it with uv run --group tutorial marimo edit examples/tutorial.py.

Implemented transforms

  • Identity (transformnd.transforms.Identity)
  • Translation (transformnd.transforms.Translate)
  • Scale (transformnd.transforms.Scale)
  • Reflection (transformnd.transforms.Reflect)
  • Affine (transformnd.transforms.Affine)
    • Can be composed efficiently with @ operator; the right hand operand is effectively applied first
  • MapAxis (transformnd.transforms.MapAxis): permute coordinate axes
  • ByDimension (transformnd.transforms.ByDimension): apply transformations to subsets of coordinate axes
  • Moving Least Squares, affine (transformnd.transforms.moving_least_squares.MovingLeastSquares)
    • uses movingleastsquares extra
  • Thin Plate Splines (transformnd.transforms.thinplate.ThinPlateSplines)
    • uses thinplatesplines extra

Arbitrary transforms can be composed into a TransformSequence with transform1 | transform2. A graph of transforms between defined spaces can be traversed using the TransformGraph.

Implemented adapters

  • Numpy arrays of shape (..., D, ...) (transformnd.adapters.ReshapeAdapter)
  • meshio.Mesh (transformnd.adapters.meshio.MeshAdapter)
  • pandas.DataFrame (transformnd.adapters.pandas.PandasAdapter)
    • Takes a subset of columns as a coordinate array
  • polars.DataFrame (transformnd.adapters.polars.PolarsAdapter)
    • Similar to the pandas adapter
    • Currently, only scalar columns are supported (e.g. not a single struct column with fields x, y, z)
  • Geometries from shapely (transformnd.adapters.shapely.GeometryAdapter)
  • Objects composed of transformable attributes (transformnd.adapters.AttrAdapter).

Additional transforms and adapters

Contributions of additional transforms and adapters are welcome! Even if they're only thin wrappers around an external library, the downstream ecosystem benefits from a consistent API.

Such external transformation libraries should be specified as "extras", and be contained in a submodule so that they are not immediately imported with transformnd. Dependencies for new adapters do not need to be included in transformnd's dependencies, but should be specified in the requirements.txt for tests.

Alternatively, consider adopting transformnd's base classes in your own library, and have your transformation instantly compatible for downstream users.

Methods which MUST be implemented:

  • __init__: should validate parameters and set self.ndim if the parameters constrain the dimensionality
  • apply: should call _validate_coords method early to check that the given coordinates are the correct shape

Methods which SHOULD be implemented if applicable:

  • to_device: if any of the transformation's parameters need to be placed on a specific device (e.g. affine matrices on the GPU)
  • is_identity: if you can cheaply check whether your transformation is an identity transformation. The base class implementation returns False.
  • into_affine: if your transformation can be represented as an affine matrix. The base class implementation returns None.
  • invert: if your transformation can be inverted (default None if not)
    • This automatically implements __invert__ (the ~my_transform operator), which raises NotImplemented if invert would return None.

Contributing

  • Use uv for environment and dependency management.
    • uv sync to set up the environment.
  • Use prek for running pre-commit hooks.
    • prek install-hooks && prek run --all-files to get started.
  • Use just for common development tasks (format, lint, test, generate docs, run benchmarks).
    • just to list commands.
  • Docs are generated with pdoc (use just doc) and hosted on ReadTheDocs

Thanks

Thanks to contributors

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

transformnd-0.1.0.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transformnd-0.1.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file transformnd-0.1.0.tar.gz.

File metadata

  • Download URL: transformnd-0.1.0.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for transformnd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a5021a80d9cbe6ea71b310c970b9f4d12e20f746014b4b95726156eebb62edb
MD5 d25ee417270b3a4c83fd547f72581466
BLAKE2b-256 420a5d0141cb0e4540fcab60b214e783e74937853614a88e159a1d8814026e0e

See more details on using hashes here.

File details

Details for the file transformnd-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: transformnd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for transformnd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 255d9069a177dad5c919747ef7edb22f08a93852561956cc8cfa4ca5790f63b8
MD5 355923648240b61efec5cf38bce30769
BLAKE2b-256 332ebacaea2d94f1302d9392fac370b41b74fc6e3f7d1be8ae525047a826e670

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page