Skip to main content

Python bindings for event camera utilities

Project description

evlib logo

evlib: Event Camera Data Processing Library

PyPI Version Python Versions Documentation Python Rust License

An event camera processing library with Rust backend and Python bindings, designed for scalable data processing with real-world event camera datasets.

Core Features

  • Universal Format Support: Load data from H5, AEDAT, EVT2/3, AER, and text formats
  • Automatic Format Detection: No need to specify format types manually
  • Polars DataFrame Integration: High-performance DataFrame operations with up to 360M events/s filtering
  • Event Filtering: Comprehensive filtering with temporal, spatial, and polarity options
  • Event Representations: Stacked histograms, voxel grids, and mixed density stacks
  • Neural Network Models: E2VID model loading and inference
  • Real-time Data Processing: Handle large datasets (550MB+ files) efficiently
  • Polarity Encoding: Automatic conversion between 0/1 and -1/1 polarities
  • Rust Performance: Memory-safe, high-performance backend with Python bindings

In Development: Advanced neural network processing (hopefully with Rust backend, maybe Candle) Real-time visualization (Only simulated working at the moment — see wasm-evlib)

Note: The Rust backend currently focuses on data loading and processing, with Python modules providing advanced features like filtering and representations.


Quick Start

Basic Usage

import evlib

# Load events from any supported format (automatic detection)
df = evlib.load_events("data/slider_depth/events.txt").collect(engine='streaming')

# Or load as LazyFrame for memory-efficient processing
lf = evlib.load_events("data/slider_depth/events.txt")

# Basic event information
print(f"Loaded {len(df)} events")
print(f"Resolution: {df['x'].max()} x {df['y'].max()}")
print(f"Duration: {df['timestamp'].max() - df['timestamp'].min()}")

# Convert to NumPy arrays for compatibility
x_coords = df['x'].to_numpy()
y_coords = df['y'].to_numpy()
timestamps = df['timestamp'].to_numpy()
polarities = df['polarity'].to_numpy()

Advanced Filtering

import evlib
import evlib.filtering as evf

# High-level preprocessing pipeline
processed = evf.preprocess_events(
    "data/slider_depth/events.txt",
    t_start=0.1, t_end=0.5,
    roi=(100, 500, 100, 400),
    polarity=1,
    remove_hot_pixels=True,
    remove_noise=True,
    hot_pixel_threshold=99.9,
    refractory_period_us=1000
)

# Individual filters (work with LazyFrames)
events = evlib.load_events("data/slider_depth/events.txt")
time_filtered = evf.filter_by_time(events, t_start=0.1, t_end=0.5)
spatial_filtered = evf.filter_by_roi(time_filtered, x_min=100, x_max=500, y_min=100, y_max=400)
clean_events = evf.filter_hot_pixels(spatial_filtered, threshold_percentile=99.9)
denoised = evf.filter_noise(clean_events, method="refractory", refractory_period_us=1000)

Event Representations

import evlib
import evlib.representations as evr

# TODO: Representation functions have Polars compatibility issues
# Create stacked histogram (replaces RVT preprocessing)
# events = evlib.load_events("data/slider_depth/events.txt")
# events_df = events.collect()
# hist = evr.create_stacked_histogram_py(
#     events_df,
#     _height=480, _width=640,
#     nbins=10, window_duration_ms=50.0,
#     _count_cutoff=10
# )

# Create mixed density stack representation
# density = evr.create_mixed_density_stack_py(
#     events_df,
#     _height=480, _width=640,
#     nbins=10, window_duration_ms=50.0
# )

# TODO: High-level preprocessing for neural networks
# data = evr.preprocess_for_detection(
#     "data/slider_depth/events.txt",
#     representation="stacked_histogram",
#     height=480, width=640,
#     nbins=10, window_duration_ms=50
# )

# TODO: Performance benchmarking against RVT
# results = evr.benchmark_vs_rvt("data/slider_depth/events.txt", height=480, width=640)

Installation

Basic Installation

pip install evlib

# For Polars DataFrame support (recommended)
pip install evlib[polars]

Development Installation

# Clone the repository
git clone https://github.com/tallamjr/evlib.git
cd evlib

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install in development mode with all features
pip install -e ".[dev,polars]"

# Build the Rust extensions
maturin develop

System Dependencies

# Ubuntu/Debian
sudo apt install libhdf5-dev pkg-config

# macOS
brew install hdf5 pkg-config

Performance-Optimized Installation

For optimal performance, ensure you have the recommended system configuration:

System Requirements:

  • RAM: 8GB+ recommended for files >100M events
  • Python: 3.10+ (3.12 recommended for best performance)
  • Polars: Latest version for advanced DataFrame operations

Installation for Performance:

# Install with Polars support (recommended)
pip install "evlib[polars]"

# For development with all performance features
pip install "evlib[dev,polars]"

# Verify installation with benchmark
python -c "import evlib; print('evlib installed successfully')"
python benchmark_memory.py  # Test memory efficiency

Optional Performance Dependencies:

# For advanced memory monitoring
pip install psutil

# For parallel processing (already included in dev)
pip install multiprocessing-logging

Polars DataFrame Integration

evlib provides comprehensive Polars DataFrame support for high-performance event data processing:

Key Benefits

  • Performance: 1.9M+ events/s loading speed, 360M+ events/s filtering speed
  • Memory Efficiency: ~23 bytes/event (5x better than typical 110 bytes/event)
  • Expressive Queries: SQL-like operations for complex data analysis
  • Lazy Evaluation: Query optimization for better performance
  • Ecosystem Integration: Seamless integration with data science tools

API Overview

Loading Data

import evlib

# Load as LazyFrame (recommended)
events = evlib.load_events("data/slider_depth/events.txt")
df = events.collect()  # Collect to DataFrame when needed

# Automatic format detection and optimization
events = evlib.load_events("data/slider_depth/events.txt")  # EVT2 format automatically detected
print(f"Format: {evlib.formats.detect_format('data/slider_depth/events.txt')}")
print(f"Description: {evlib.formats.get_format_description('EVT2')}")

Advanced Features

import evlib
import polars as pl

# Chain operations with LazyFrames for optimal performance
events = evlib.load_events("data/slider_depth/events.txt")
result = events.filter(pl.col("polarity") == 1).with_columns([
    pl.col("timestamp").dt.total_microseconds().alias("time_us"),
    (pl.col("x") + pl.col("y")).alias("diagonal_pos")
]).collect()

# Memory-efficient temporal analysis
time_stats = events.with_columns([
    pl.col("timestamp").dt.total_microseconds().alias("time_us")
]).group_by([
    (pl.col("time_us") // 1_000_000).alias("time_second")  # Group by second
]).agg([
    pl.len().alias("event_count"),
    pl.col("polarity").mean().alias("avg_polarity")
]).collect()

# Combine with filtering module for complex operations
import evlib.filtering as evf
filtered = evf.filter_by_time(events, t_start=0.1, t_end=0.5)
analysis = filtered.with_columns([
    pl.col("timestamp").dt.total_microseconds().alias("time_us")
]).collect()

Utility Functions

import evlib
import polars as pl
import evlib.filtering as evf

# Built-in format detection
format_info = evlib.formats.detect_format("data/slider_depth/events.txt")
print(f"Detected format: {format_info}")

# Spatial filtering using dedicated filtering functions (preferred)
events = evlib.load_events("data/slider_depth/events.txt")
spatial_filtered = evf.filter_by_roi(events, x_min=100, x_max=200, y_min=50, y_max=150)

# Or using direct Polars operations
manual_filtered = events.filter(
    (pl.col("x") >= 100) & (pl.col("x") <= 200) &
    (pl.col("y") >= 50) & (pl.col("y") <= 150)
)

# Temporal analysis with Polars operations
rates = events.with_columns([
    pl.col("timestamp").dt.total_microseconds().alias("time_us")
]).group_by([
    (pl.col("time_us") // 10_000).alias("time_10ms")  # Group by 10ms
]).agg([
    pl.len().alias("event_rate"),
    pl.col("polarity").mean().alias("avg_polarity")
]).collect()

# TODO: Save functions have type compatibility issues
# Save processed data
# processed = evf.preprocess_events("data/slider_depth/events.txt", t_start=0.1, t_end=0.5)
# processed_df = processed.collect()
# x, y, t_us, p = processed_df.select(["x", "y", "timestamp", "polarity"]).to_numpy().T
# Convert microseconds to seconds for save function
# t = t_us.astype('float64') / 1_000_000
# evlib.formats.save_events_to_hdf5(x, y, t, p, "output.h5")

Performance Benchmarks

Performance Benchmarks

Benchmark Results:

  • Loading Speed: 1.9M+ events/second average across formats
  • Filter Speed: 360M+ events/second for complex filtering operations
  • Memory Efficiency: ~23 bytes/event
  • Format Performance: RAW binary (2.6M events/s) > HDF5 (2.5M events/s) > Text (0.6M events/s)

Benchmarking and Monitoring

Run performance benchmarks to verify optimizations:

# Verify README performance claims and generate plots
python benches/benchmark_performance_readme.py

# Memory efficiency benchmark
python benches/benchmark_memory.py

# Test with your own data
python -c "
import evlib
import time
start = time.time()
events = evlib.load_events('data/slider_depth/events.txt')
df = events.collect()
print(f'Loaded {len(df):,} events in {time.time()-start:.2f}s')
print(f'Format: {evlib.detect_format(\"data/slider_depth/events.txt\")}')
print(f'Memory per event: {df.estimated_size() / len(df):.1f} bytes')
"

Performance Examples

Optimal Loading for Different File Sizes

import evlib
import evlib.filtering as evf
import polars as pl

# Small files (<5M events) - Direct loading
events_small = evlib.load_events("data/slider_depth/events.txt")
df_small = events_small.collect()

# Large files (>5M events) - Automatic streaming
events_large = evlib.load_events("data/slider_depth/events.txt")
# Same API, automatically uses streaming for memory efficiency

# Memory-efficient filtering on large datasets using filtering module
filtered = evf.filter_by_time(events_large, t_start=1.0, t_end=2.0)
positive_events = evf.filter_by_polarity(filtered, polarity=1)

# Or using direct Polars operations
manual_filtered = events_large.filter(
    (pl.col("timestamp").dt.total_microseconds() / 1_000_000 > 1.0) &
    (pl.col("polarity") == 1)
).collect()

Memory Monitoring

import evlib
import psutil
import os

def monitor_memory():
    process = psutil.Process(os.getpid())
    return process.memory_info().rss / 1024 / 1024  # MB

# Monitor memory usage during loading
initial_mem = monitor_memory()
events = evlib.load_events("data/slider_depth/events.txt")
df = events.collect()
peak_mem = monitor_memory()

print(f"Memory used: {peak_mem - initial_mem:.1f} MB")
print(f"Memory per event: {(peak_mem - initial_mem) * 1024 * 1024 / len(df):.1f} bytes")
print(f"Polars DataFrame size: {df.estimated_size() / 1024 / 1024:.1f} MB")

Troubleshooting Large Files

Memory Constraints

  • Automatic Streaming: Files >5M events use streaming by default
  • LazyFrame Operations: Memory-efficient processing without full materialization
  • Memory Monitoring: Use benchmark_memory.py to track usage
  • System Requirements: Recommend 8GB+ RAM for files >100M events

Performance Tuning

  • Optimal Chunk Size: System automatically calculates based on available memory
  • LazyFrame Operations: Use .lazy() for complex filtering chains
  • Memory-Efficient Formats: RAW binary formats provide best performance, followed by HDF5
  • Progress Reporting: Large files show progress during loading

Common Issues and Solutions

Issue: Out of memory errors

import evlib
import evlib.filtering as evf

# Solution: Use filtering before collecting (streaming activates automatically)
events = evlib.load_events("data/slider_depth/events.txt")
# Streaming activates automatically for files >5M events

# Apply filtering before collecting to reduce memory usage
filtered = evf.filter_by_time(events, t_start=0.1, t_end=0.5)
df = filtered.collect()  # Only collect when needed

# Or stream to disk using Polars
filtered.sink_parquet("filtered_events.parquet")

Issue: Slow loading performance

import evlib
import evlib.filtering as evf
import polars as pl

# Solution: Use LazyFrame for complex operations and filtering module
events = evlib.load_events("data/slider_depth/events.txt")

# Use filtering module for optimized operations
result = evf.filter_by_roi(events, x_min=0, x_max=640, y_min=0, y_max=480)
df = result.collect()

# Or chain Polars operations
result = events.filter(pl.col("polarity") == 1).select(["x", "y", "timestamp"]).collect()

Issue: Memory usage higher than expected

import evlib

# Solution: Monitor and verify optimization
events = evlib.load_events("data/slider_depth/events.txt")
df = events.collect()
print(f"Memory efficiency: {df.estimated_size() / len(df)} bytes/event")
print(f"DataFrame schema: {df.schema}")
print(f"Number of events: {len(df):,}")

# Check format detection
format_info = evlib.formats.detect_format("data/slider_depth/events.txt")
print(f"Format: {format_info}")

Available Python Modules

evlib provides several Python modules for different aspects of event processing:

Core Modules

  • evlib.formats: Direct Rust access for format loading and detection
  • evlib.filtering: High-performance event filtering with Polars
  • evlib.representations: Event representations (stacked histograms, voxel grids)
  • evlib.models: Neural network model loading and inference (Under construction)

Module Overview

import evlib
import evlib.filtering as evf
import evlib.representations as evr

# Core event loading (returns Polars LazyFrame)
events = evlib.load_events("data/slider_depth/events.txt")

# Format detection and description
format_info = evlib.formats.detect_format("data/slider_depth/events.txt")
description = evlib.formats.get_format_description("HDF5")

# Advanced filtering
filtered = evf.preprocess_events("data/slider_depth/events.txt", t_start=0.1, t_end=0.5)
time_filtered = evf.filter_by_time(events, t_start=0.1, t_end=0.5)

# TODO: Event representations have Polars compatibility issues
# Event representations (need to load data first)
# events_df = events.collect()
# hist = evr.create_stacked_histogram_py(events_df, _height=480, _width=640, nbins=10)
# voxel = evr.create_voxel_grid_py(events_df, _height=480, _width=640, nbins=5)

# Neural network models (limited functionality)
from evlib.models import ModelConfig  # If available

# TODO: Data saving functions have type compatibility issues
# Data saving (need to get arrays first)
# df = events.collect()
# x, y, t_us, p = df.select(["x", "y", "timestamp", "polarity"]).to_numpy().T
# Convert microseconds to seconds for save functions
# t = t_us.astype('float64') / 1_000_000
# evlib.formats.save_events_to_hdf5(x, y, t, p, "output.h5")
# evlib.formats.save_events_to_text(x, y, t, p, "output.txt")

Examples

Run examples:

# Test all notebooks
pytest --nbmake examples/

# Run specific examples
python examples/simple_example.py
python examples/filtering_demo.py
python examples/stacked_histogram_demo.py

Development

Testing

Core Testing

# Run all tests (Python and Rust)
pytest
cargo test

# Test specific modules
pytest tests/test_filtering.py
pytest tests/test_representations.py
pytest tests/test_evlib_exact_match.py

# Test notebooks (including examples)
pytest --nbmake examples/

# Test with coverage
pytest --cov=evlib

Documentation Testing

All code examples in the documentation are automatically tested to ensure they work correctly:

# Test all documentation examples
pytest --markdown-docs docs/

# Test specific documentation file
pytest --markdown-docs docs/getting-started/quickstart.md

# Use the convenient test script
python scripts/test_docs.py --list    # List testable files
python scripts/test_docs.py --report  # Generate report

# Test specific documentation section
pytest --markdown-docs docs/user-guide/
pytest --markdown-docs docs/getting-started/

Code Quality

# Format code
black python/ tests/ examples/
cargo fmt

# Run linting
ruff check python/ tests/
cargo clippy

# Check types
mypy python/evlib/

Building

Requirements

  • Rust: Stable toolchain (see rust-toolchain.toml)
  • Python: ≥3.10 (3.12 recommended)
  • Maturin: For building Python extensions
# Development build
maturin develop --features python # python required to register python modules

# Build with features
maturin develop --features polars
maturin develop --features pytorch

# Release build
maturin build --release

Community & Support

xkcd{ width=100% }

  • GitHub: tallamjr/evlib
  • Issues: Report bugs and request features
  • Discussions: Community Q&A and ideas

License

MIT License - see LICENSE.md for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

evlib-0.4.3-cp312-cp312-manylinux_2_39_x86_64.whl (23.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

evlib-0.4.3-cp312-cp312-macosx_11_0_arm64.whl (13.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

evlib-0.4.3-cp311-cp311-manylinux_2_39_x86_64.whl (23.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

evlib-0.4.3-cp311-cp311-macosx_11_0_arm64.whl (13.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

evlib-0.4.3-cp310-cp310-manylinux_2_39_x86_64.whl (23.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

evlib-0.4.3-cp310-cp310-macosx_11_0_arm64.whl (13.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file evlib-0.4.3-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 cd9ac436274b68b17820e8149fcffef9fbf4dfb40d265da0f13bb847196810ff
MD5 8db12d7cc7e6a0ec3f453348ca24fb34
BLAKE2b-256 e742a72bbda8dd0534a562596daa71a4a810090fead8225a81ae103145923c00

See more details on using hashes here.

File details

Details for the file evlib-0.4.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 df722d1a11fa9d044726f3301a2e832ce011e763d923ae2f086e61e602ec34cc
MD5 8b3427470ac2aaae6d08f577546b0737
BLAKE2b-256 5fdb8fd8cfc1c33b3a84b11758b4df67a737b4f1f61fb89210a7e034ded3649a

See more details on using hashes here.

File details

Details for the file evlib-0.4.3-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8f0d08d0547a1dfa7be504a828e42177bad6087f1dc385b2cbda2d5e745beb92
MD5 327b79c6b56398928d25baad71e8ae27
BLAKE2b-256 4ec53d93d231616ee3847048370e9dc7b9736d4ba76d3e21a72a351ab9c85257

See more details on using hashes here.

File details

Details for the file evlib-0.4.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f628312c4c7710997696ee2dec3f1e3a05f9751ce709b00f858b9895bcc8cbf9
MD5 f702e5462d7ded99a9fd15dffd6e26ba
BLAKE2b-256 e6767a99187228c1348a73ad8a4f39177b609cf7c3de80382a3246ef310abf49

See more details on using hashes here.

File details

Details for the file evlib-0.4.3-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d99099cd022f1f6d95cba394daaeb07dc2a2f047632f78f10a3c8d750699cd55
MD5 2980d870b83eed2c3d94f3863ed6c0c5
BLAKE2b-256 43723c5ead0fea1f6ee5771eb94a0be8889ccaaa066df7f1909e1eb7c593f699

See more details on using hashes here.

File details

Details for the file evlib-0.4.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for evlib-0.4.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c4017d32a9cba6767f0c9ee5b22c8cd47b4845f68eb1b0751f1d5446bb74fbf3
MD5 7b9ca467665640b121176c7d28f02dc0
BLAKE2b-256 3cb4c7fd17a8e7185647c2f91268dcff38a64fc28562f58311674cda267b819d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page