Skip to main content

High-performance N-dimensional unit field transformations with Cython-accelerated remapping

Project description

UnitField

License: MIT Python 3.10+ Build Status

UnitField is a high-performance N-dimensional coordinate remapping library built on a Cython kernel with OpenMP parallelism. It maps unit-space coordinates ([0, 1]) through arbitrary displacement fields with configurable interpolation and border handling — purpose-built for image warping, morphing, and nonlinear coordinate transformations.

Why UnitField?

  • Cython-accelerated kernel — 2D and 1D remap loops compiled to C with OpenMP threading. Significantly faster than pure NumPy for large images.
  • Asymmetric per-edge feathering — Control feather blend independently on left, right, top, and bottom borders. Useful for seamless compositing and panorama blending.
  • Per-channel feather masks — Feather only specific channels (e.g., alpha-only) via the feather_dims parameter.
  • Multiple border modes — CLAMP, CONSTANT, REFLECT, WRAP, REFLECT_101, and ARRAY compositing.
  • Multiple interpolation methods — Nearest-neighbor, bilinear, bicubic (Catmull-Rom), Lanczos-3/4.
  • 1-D signal remapping — The same kernel operates on 1-D signals, useful for audio, time-series, and look-up table applications.
  • Endomorphism compositionUnit2DMappedEndomorphism supports composition (f ∘ g) for chaining transformations.

Installation

pip install unitfield

For the fastest installation with a pre-built wheel, ensure you have the cv2 extras (optional — used for comparison benchmarks only):

pip install "unitfield[cv2]"

From Source (Cython)

git clone https://github.com/Grayjou/UnitField.git
cd UnitField
pip install -e ".[dev]"

Requires: Python ≥ 3.10, NumPy ≥ 1.20, Cython ≥ 3.0 (for source builds), a C99 compiler with OpenMP support.

Quick Start

2-D Image Remapping

import numpy as np
from unitfield import (
    BorderConfig, BorderMode, InterpMethod,
    Unit2DMappedEndomorphism, remap_tensor,
)

# Simple identity field
H, W = 256, 256
xs, ys = np.meshgrid(np.linspace(0, 1, W), np.linspace(0, 1, H), indexing="xy")
identity = np.stack([xs, ys], axis=-1)
endo = Unit2DMappedEndomorphism(identity, interp_method=InterpMethod.LINEAR)

# Remap an image with asymmetric feathering
bc = BorderConfig(
    mode=BorderMode.CONSTANT,
    constant_value=0.0,
    feathering_width=0.2,
    feathering_x_overshoot_multiplier=3.0,   # heavy feather on right
    feathering_x_undershoot_multiplier=0.0,   # hard edge on left
    feather_dims=[True, True, True, False],   # RGB feathers, alpha hard
)
result = endo.remap(image, interpolation=1, border_config=bc)

Direct remap with coordinate maps

map_x = np.random.rand(H, W).astype(np.float64)
map_y = np.random.rand(H, W).astype(np.float64)

result = remap_tensor(
    image, map_x, map_y,
    interpolation=1,
    border_config=BorderConfig.constant(0.0, feathering_width=0.1),
)

1-D Signal Remapping

from unitfield import remap_tensor_1d

signal = np.sin(np.linspace(0, 4 * np.pi, 1000))
map_x = np.linspace(0, 1, 800) ** 2  # nonlinear time warp
warped = remap_tensor_1d(signal, map_x, interpolation=1)

Asymmetric Feathering

BorderConfig now exposes four independent feather multipliers — one per edge:

Field Edge Applies when
feathering_x_undershoot_multiplier left u_x < 0.0
feathering_x_overshoot_multiplier right u_x > 1.0
feathering_y_undershoot_multiplier top u_y < 0.0
feathering_y_overshoot_multiplier bottom u_y > 1.0

All default to 1.0. Set to 0.0 for a hard edge, or higher for a softer blend.

API Overview

Module Key exports
unitfield BorderConfig, BorderMode, InterpMethod, remap_tensor, remap_tensor_1d, Unit2DMappedEndomorphism, Unit1DMappedEndomorphism, MappedUnitField
unitfield.core Same + UnitNdimField, UnitMappedEndomorphism, UnitArray, UnitSpaceVector
unitfield.utilities pbm_2d, upbm_2d, flat_1d_pbm — positional basematrix generators

Performance

The kernel is written in Cython with:

  • OpenMP-accelerated inner loops (prange)
  • No Python overhead at runtime (nogil)
  • Bicubic (Catmull-Rom) and Lanczos interpolation with efficient separable sampling
  • Per-edge feather distance computed inline with the border handler

Run benchmarks locally:

pytest tests/ -v -m benchmark

Development

pip install -e ".[dev]"
pytest tests/ -v

License

MIT — see LICENSE.

Citation

@software{unitfield2026,
  author = {GrayJou},
  title = {UnitField: N-dimensional Unit Field Transformations},
  year = {2026},
  url = {https://github.com/Grayjou/UnitField},
}

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

unitfield-0.2.4.tar.gz (548.6 kB view details)

Uploaded Source

Built Distribution

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

unitfield-0.2.4-cp312-cp312-win_amd64.whl (787.4 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file unitfield-0.2.4.tar.gz.

File metadata

  • Download URL: unitfield-0.2.4.tar.gz
  • Upload date:
  • Size: 548.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for unitfield-0.2.4.tar.gz
Algorithm Hash digest
SHA256 de94a34411bd19f673a672f1f35592090107adac7b40f1116d2f20f4b1a8116b
MD5 34fdde70205cf3f814310b2da6c3b5c4
BLAKE2b-256 df50b8665a22d52d1672b4c91a23e1b853fc5f3cbde5ec015e875b60a330e63b

See more details on using hashes here.

File details

Details for the file unitfield-0.2.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: unitfield-0.2.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 787.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for unitfield-0.2.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6aca20a0a861c09093e73ea2046341f9c4a9b5c858b84ee313b9929af459ff0b
MD5 9d4796aef014adcad5bbbcf8e084b942
BLAKE2b-256 becf90e3a1a3d3930f9a443fe8dcf485bb0b95a3e9993b12e83ec353f66cb08d

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