ftools provides fast local neighborhood filters fmedian and fsigma that automatically handle both 2D and 3D arrays, implemented as C extensions with optimized sorting networks for small window sizes up to 27 elements.
fmedian: Filtered median computation - automatically works with 2D and 3D arraysfsigma: Local population standard deviation - automatically works with 2D and 3D arrays- Optional center pixel/voxel exclusion for better outlier detection (default: include center)
- Robust NaN handling
- Correct edge handling (edge pixels/voxels use smaller neighborhoods)
- Full test coverage with unit, edge case, and integration tests
- Python 3.8+
- NumPy >= 1.20
- C compiler toolchain (gcc, clang, or MSVC)
pip install .If you want to build the C extensions without installing:
python setup.py build_ext --inplaceThe repository includes a pre-commit hook in hooks/pre-commit that automatically increments the patch version number and appends the branch name on each commit.
To enable it, create a symlink:
ln -sf ../../hooks/pre-commit .git/hooks/pre-commitThis will automatically update the version in setup.py (e.g., 3.2.1-main → 3.2.2-main).
import numpy as np
from ftools import fmedian, fsigma
# Works with both 2D and 3D data automatically!
xsize, ysize, zsize = 3, 3, 3 # Example window sizes
# Generate random input data
data_2d = np.random.normal(0.0, 1.0, (100, 200)).astype(np.float64)
data_3d = np.random.normal(0.0, 1.0, (100, 200, 100)).astype(np.float64)
# 2D example
median_filtered_2d = fmedian(data_2d, (xsize, ysize), exclude_center=1)
sigma_map_2d = fsigma(data_2d, (xsize, ysize), exclude_center=1)
# 3D example
median_filtered_3d = fmedian(data_3d, (xsize, ysize, zsize))
sigma_map_3d = fsigma(data_3d, (xsize, ysize, zsize))input_data: Input NumPy array (2D or 3D, will be converted to float64)window_size: tuple with window sizes. Must be odd positive integers.exclude_center: Optional, if 1, exclude center pixel/voxel from filter calculation; if 0, include it (default: 0)
- NumPy array of same shape as input, containing filtered values (float64)
See the examples directory.
Comprehensive tests covering unit tests, edge cases, parameter validation, and integration scenarios. Run tests using pytest:
pytestpytest --cov=ftools --cov-report=htmltree --gitignoreThe project follows standard Python conventions:
- PEP 8 for Python code
- Type hints where applicable
- Comprehensive docstrings
MIT License
Contributions are welcome! Please ensure:
- All tests pass (
pytest) - New features include tests
- Code follows project style conventions
- Documentation is updated as needed
- Both functions convert input to
float64for computation - C implementations provide significant speedup over pure Python/NumPy equivalents
- Edge handling uses appropriate boundary conditions
- Blazingly fast sorting networks adapted from Sorting Networks
- Many thanks to Claude!