CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-fastremap

High-performance array manipulation functions for remapping, masking, renumbering, and transposing 3D labeled images and point clouds.

Pending
Overview
Eval results
Files

masking.mddocs/

Masking and Filtering

Operations for selectively hiding or preserving specific values in arrays. Essential for image segmentation, data cleaning workflows, and selective data processing where certain labels need to be excluded or isolated.

Capabilities

Label Masking

Zero out designated labels in an array with a specified mask value.

def mask(
    arr: ArrayLike,
    labels: ArrayLike,
    in_place: bool = False,
    value: Union[int, float] = 0
) -> NDArray:
    """
    Mask out designated labels in an array with the given value.
    
    Args:
        arr: Input N-dimensional numpy array
        labels: Iterable list of integers to mask
        in_place: Modify input array to reduce memory consumption
        value: Mask value (default: 0)
    
    Returns:
        Array with specified labels masked out
    """

Usage Example:

import fastremap
import numpy as np

# Sample labeled image
labels = np.array([0, 1, 2, 3, 4, 5, 1, 2, 3])

# Mask specific labels
masked = fastremap.mask(labels, [1, 3, 5])
# Result: [0, 0, 2, 0, 4, 0, 0, 2, 0]

# Mask with custom value
masked = fastremap.mask(labels, [2, 4], value=-1)
# Result: [0, 1, -1, 3, -1, 5, 1, -1, 3]

# In-place masking to save memory
fastremap.mask(labels, [1, 3], in_place=True)

Inverse Masking

Zero out all labels except those in the provided list, effectively preserving only the specified labels.

def mask_except(
    arr: NDArray,
    labels: ArrayLike,
    in_place: bool = False,
    value: Union[int, float] = 0
) -> NDArray:
    """
    Mask out all labels except the provided list.
    
    Args:
        arr: Input N-dimensional numpy array
        labels: Iterable list of integers to preserve
        in_place: Modify input array to reduce memory consumption
        value: Mask value (default: 0)
    
    Returns:
        Array with all labels except specified ones masked out
    """

Usage Example:

import fastremap
import numpy as np

# Sample labeled image
labels = np.array([0, 1, 2, 3, 4, 5, 1, 2, 3])

# Keep only specific labels
preserved = fastremap.mask_except(labels, [1, 3])
# Result: [0, 1, 0, 3, 0, 0, 1, 0, 3]

# Keep labels with custom mask value
preserved = fastremap.mask_except(labels, [2, 4], value=-1)
# Result: [-1, -1, 2, -1, 4, -1, -1, 2, -1]

# In-place operation
fastremap.mask_except(labels, [1, 2], in_place=True)

Types

ArrayLike = Union[np.ndarray, list, tuple]
NDArray = np.ndarray

Install with Tessl CLI

npx tessl i tessl/pypi-fastremap

docs

advanced-remapping.md

core-operations.md

dtype-management.md

index.md

masking.md

memory-layout.md

search-analysis.md

serialization.md

tile.json