fastremap Library

1.18.0 · active · verified Fri Apr 17

fastremap is a highly optimized C++ library with a Python wrapper for high-performance image and point cloud processing. It provides extremely fast functions for remapping, masking, renumbering, finding unique elements, and in-place transposition of 3D labeled images. The current version is 1.18.0, and it maintains an active release cadence with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of `fastremap` for remapping labels in a NumPy array. It shows both non-in-place (`remap`) and in-place (`remap_inplace`) operations, as well as the `renumber` function for making labels contiguous and obtaining the mapping.

import numpy as np
import fastremap

# Example 1: Remap an array using a dictionary
arr1 = np.array([1, 2, 1, 3, 0], dtype=np.uint32)
mapping = {1: 10, 2: 20, 3: 30}
remapped_arr = fastremap.remap(arr1, mapping)
print(f"Original array: {arr1}")
print(f"Remapped array: {remapped_arr}")
# Expected: Remapped array: [10 20 10 30  0]

# Example 2: In-place remapping
arr2 = np.array([10, 20, 10, 30, 0], dtype=np.uint32)
reverse_mapping = {10: 1, 20: 2, 30: 3}
fastremap.remap_inplace(arr2, reverse_mapping)
print(f"Array after in-place remapping: {arr2}")
# Expected: Array after in-place remapping: [1 2 1 3 0]

# Example 3: Renumber contiguous labels
arr3 = np.array([0, 5, 2, 0, 5, 10], dtype=np.uint32)
new_array, old_to_new_map = fastremap.renumber(arr3)
print(f"Original array for renumber: {arr3}")
print(f"Renumbered array: {new_array}")
print(f"Old to new map: {old_to_new_map}")
# Expected: Renumbered array: [0 2 1 0 2 3], Old to new map: {0: 0, 5: 2, 2: 1, 10: 3}

view raw JSON →