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

dtype-management.mddocs/

Data Type Management

Functions for manipulating and optimizing numpy data types to balance memory usage and value range requirements. These utilities help optimize memory consumption by selecting appropriate data types for your data's actual range.

Capabilities

Data Type Fitting

Find the smallest data type of the same kind that can accommodate a specific value.

def fit_dtype(
    dtype: DTypeLike,
    value: Union[int, float, np.integer, np.unsignedinteger, np.complexfloating, np.floating],
    exotics: bool = False
) -> DTypeLike:
    """
    Find smallest dtype of same kind that can fit the given value.
    
    Args:
        dtype: Input data type
        value: Value to fit
        exotics: Allow exotic dtypes
    
    Returns:
        The fitted dtype
    """

Usage Example:

import fastremap
import numpy as np

# Find dtype to fit a specific value
fitted_dtype = fastremap.fit_dtype(np.int32, 500)
# Result: np.int16 (since 500 fits in int16)

fitted_dtype = fastremap.fit_dtype(np.uint8, 300)
# Result: np.uint16 (since 300 doesn't fit in uint8)

Data Type Narrowing

Reduce a data type to the next smaller size of the same type.

def narrow_dtype(
    dtype: DTypeLike,
    exotics: bool = False
) -> DTypeLike:
    """
    Narrow the given dtype to the next smaller size of the same type.
    
    Args:
        dtype: Input data type
        exotics: Include exotic dtypes
    
    Returns:
        The narrowed dtype
    """

Usage Example:

import fastremap
import numpy as np

# Narrow integer types
narrowed = fastremap.narrow_dtype(np.uint32)
# Result: np.uint16

narrowed = fastremap.narrow_dtype(np.int64)
# Result: np.int32

# Narrow float types
narrowed = fastremap.narrow_dtype(np.float64)
# Result: np.float32

Data Type Widening

Expand a data type to the next larger size of the same type.

def widen_dtype(
    dtype: DTypeLike,
    exotics: bool = False
) -> DTypeLike:
    """
    Widen the given dtype to the next larger size of the same type.
    
    Args:
        dtype: Input data type
        exotics: Include exotic dtypes
    
    Returns:
        The widened dtype
    """

Usage Example:

import fastremap
import numpy as np

# Widen integer types
widened = fastremap.widen_dtype(np.uint16)
# Result: np.uint32

widened = fastremap.widen_dtype(np.int8)
# Result: np.int16

# Widen float types
widened = fastremap.widen_dtype(np.float32)
# Result: np.float64

Types

DTypeLike = Union[np.dtype, type, str]

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