Convenient package for spatial box counting and fractal analysis across data types with CPU and GPU support
Project description
spacial-boxcounting: Spatial Boxcount Algorithm & Fractal Analysis
An implementation of a spatial boxcount algorithm for fractal analysis, with both CPU and GPU support for accelerated computation.
Abstract
This project implements a spatial boxcount algorithm that characterizes 2D arrays by topological complexity and spatial heterogeneity. With both CPU and GPU support, it enables spatial similarity search, edge detection, and statistical analysis of image datasets.
Key Features
- Spatial Box Counting: Produces 2D maps of box count ratios and lacunarity
- Fractal Dimension Analysis: Multi-scale fractal dimension computation
- Multiple Processing Modes: Spatial maps or single-value results
- CPU & GPU Support: Numba JIT compilation and CuPy acceleration — switch via
backend="cpu"|"gpu" - Batch Processing: Process entire directories of images
- Multiple Input Formats: JPEG, BMP, PNG, and binary files
- Hilbert Curve Mapping: Preserves data locality for binary file analysis
- Cross-Platform: Works on Windows, Linux, and macOS
Installation
Install via pip:
# Basic CPU-only installation
pip install spacial_boxcounting
# With GPU support (NVIDIA CUDA)
pip install spacial_boxcounting[gpu]
# With GPU support (AMD ROCm — experimental)
pip install spacial_boxcounting[gpu-amd]
# Development installation
pip install -e ".[dev]"
Quick Start
Processing from a Numpy Array
import numpy as np
from spacial_boxcounting.api import boxcount_from_array, fractal_dimension_from_array
arr = np.random.randint(0, 256, size=(256, 256)).astype(np.uint8)
# Spatial processing (CPU)
result = boxcount_from_array(arr, mode='spatial')
print('Spatial Result shape:', [r.shape for r in result])
# Single value processing (CPU)
result = boxcount_from_array(arr, mode='single')
print('Box Count & Lacunarity:', result)
# GPU-accelerated — just add backend='gpu'
result_gpu = boxcount_from_array(arr, mode='single', backend='gpu')
print('GPU Result:', result_gpu)
# Fractal dimension (CPU or GPU)
fd_cpu = fractal_dimension_from_array(arr)
fd_gpu = fractal_dimension_from_array(arr, backend='gpu')
print(f'Fractal Dimension: CPU={fd_cpu:.3f}, GPU={fd_gpu:.3f}')
Processing a File
from spacial_boxcounting.api import boxcount_from_file, fractal_dimension
# CPU
result = boxcount_from_file('path/to/image.jpg', mode='spatial')
fd = fractal_dimension('path/to/image.jpg')
# GPU
result = boxcount_from_file('path/to/image.jpg', mode='spatial', backend='gpu')
fd = fractal_dimension('path/to/image.jpg', backend='gpu')
Command-Line Interface
# CPU (default)
spacial-boxcount single --file path/to/image.jpg --mode spatial
# GPU — just add --backend gpu
spacial-boxcount single --file path/to/image.jpg --mode spatial --backend gpu
# Batch processing with GPU
spacial-boxcount batch --folder path/to/images/ --mode single --backend gpu
# With Hilbert curve mapping (for binary files)
spacial-boxcount single --file path/to/data.bin --mode spatial --hilbert
GPU Acceleration
GPU acceleration is available via CuPy. Use the backend="gpu" parameter:
from spacial_boxcounting.api import boxcount_from_array
# All API functions accept backend='cpu' (default) or backend='gpu'
result = boxcount_from_array(arr, mode='single', backend='gpu')
If CuPy is not installed, requesting backend="gpu" raises a clear ImportError:
ImportError: GPU backend requested but CuPy is not installed.
Install with: pip install spacial_boxcounting[gpu]
GPU Backend Detection
from spacial_boxcounting import CUPY_AVAILABLE, GPU_BACKEND
print(f'CuPy available: {CUPY_AVAILABLE}')
print(f'GPU backend: {GPU_BACKEND}') # 'cuda', 'rocm', or None
Performance
Measured on RTX 4060 Ti (batched 4D-tensor implementation):
| Image Size | Boxsize=2 | Boxsize=4 | Boxsize=16 |
|---|---|---|---|
| 128×128 | 9× | 5× | 1× |
| 256×256 | 9× | 18× | 4× |
| 512×512 | 3× | 12× | 16× |
| 1024×1024 | 2.6× | 11× | 35× |
- Small images (< 128×128): CPU may be faster due to GPU transfer overhead
- Large images (> 512×512): GPU provides 2–35× speedup
- AMD users: ROCm support is experimental (
pip install spacial_boxcounting[gpu-amd])
Hilbert Curve Mapping for Binary Data
For binary files, the Hilbert curve mapping preserves data locality when converting 1D data streams to 2D arrays for spatial analysis:
result = boxcount_from_file('data.bin', mode='spatial', hilbert=True)
fd = fractal_dimension('data.bin', hilbert=True)
Batch Processing
Process multiple images with progress tracking:
from spacial_boxcounting.batch import batch_boxcount
results = batch_boxcount('path/to/images/', mode='single')
for filename, result in results.items():
print(f'{filename}: {result}')
License
See LICENSE.txt for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spacial_boxcounting-0.3.1.tar.gz.
File metadata
- Download URL: spacial_boxcounting-0.3.1.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e7990f33cd83922ec3c736f602c3224bf0d11d2f541e037acbd01172ab198e6
|
|
| MD5 |
7e0f81bf8a817b537ee49fe19366804c
|
|
| BLAKE2b-256 |
fa4aa4f6e7afc1fbcb774158d06efa41c90398c5c449285d718319b3f3f328d8
|
File details
Details for the file spacial_boxcounting-0.3.1-py3-none-any.whl.
File metadata
- Download URL: spacial_boxcounting-0.3.1-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11867650bad751d20c98f51d6ac30c5df745091462c19c21c46c9cc438f512f8
|
|
| MD5 |
da80789751a2c4e91fa2a07b876ab597
|
|
| BLAKE2b-256 |
265ba7dd4ca3aeae0e8e1960756a3266bd344c987446b35acf69d65ae4c50276
|