A Python package for decoding non-local neural activity from electrophysiological data using Bayesian inference with Hidden Markov Models (HMMs). This package specializes in detecting spatial replay events and decoding position from neural spike data.
The non_local_detector package provides state-of-the-art algorithms for:
- Replay Detection: Identify when the brain is replaying past experiences during rest periods
- Position Decoding: Decode spatial position from neural activity in real-time
- Multi-Environment Analysis: Handle complex experimental paradigms with multiple spatial contexts
- GPU Acceleration: Leverage JAX for high-performance computing on GPUs
- π¬ Multiple Likelihood Models: GLM, KDE, and GMM-based approaches for both sorted and clusterless spike data
- β‘ JAX-Powered: GPU-accelerated computations for large-scale analyses
- π― Scikit-learn Compatible: Familiar fit/predict interface for easy integration
- π Rich Visualizations: Built-in plotting functions for results analysis
- π§ͺ Extensive Testing: Comprehensive test suite and validation notebooks
pip install non_local_detectorFor CUDA-enabled GPU acceleration:
pip install non_local_detector[gpu]Or using conda/mamba:
mamba install jaxlib=*=*cuda* jax cuda-nvcc -c conda-forge -c nvidia
mamba install non_local_detector -c edenoFor development and contributing:
git clone https://github.com/LorenFrankLab/non_local_detector.git
cd non_local_detector
mamba env create -f environment.yml # or environment_gpu.yml for GPU
conda activate non_local_detector
pip install -e .[dev]import numpy as np
from non_local_detector import NonLocalClusterlessDetector
# Initialize detector
detector = NonLocalClusterlessDetector(
environments=[environment], # Your spatial environment
observation_model="clusterless_kde",
transition_type="random_walk"
)
# Fit the model
detector.fit(
position=training_position,
spike_times=training_spike_times, # List of arrays, one per electrode
spike_waveform_features=training_waveform_features, # List of arrays, one per electrode
time=training_time
)
# Detect replay events
results = detector.predict(
spike_times=test_spike_times,
spike_waveform_features=test_waveform_features,
time=test_time
)
# Analyze results
state_probability = results.acausal_state_probabilities
decoded_position = results.acausal_posterior.unstack("state_bins").sum("position")from non_local_detector import NonLocalSortedSpikesDetector
# For traditional spike-sorted data
detector = NonLocalSortedSpikesDetector(
environments=[environment],
observation_model="sorted_spikes_kde"
)
detector.fit(
position=position,
spike_times=spike_times, # List of arrays, one per neuron
time=time
)-
Likelihood Models (
non_local_detector.likelihoods)sorted_spikes_glm: GLM-based likelihood for sorted spike datasorted_spikes_kde: KDE-based likelihood for sorted spike dataclusterless_kde: KDE-based likelihood for clusterless dataclusterless_gmm: GMM-based likelihood for clusterless data
-
State Transition Models
- Continuous:
RandomWalk,EmpiricalMovement,Identity,Uniform - Discrete: Stationary/non-stationary diagonal and custom transitions
- Continuous:
-
Decoder Models (
non_local_detector.models)NonLocalDetector: Main replay detection modelsContFragClassifier: Fragmented decoding for interrupted sequencesClusterlessDecoder/SortedSpikesDecoder: Basic position decodingMultiEnvironmentClassifier: Multi-environment decoding
This project maintains high code quality standards:
- Linting:
ruff check src/(zero issues required) - Formatting:
ruff format src/andblack src/ - Type Checking:
mypy src/non_local_detector/(gradual adoption) - Testing:
pytestwith coverage reporting
-
Setup Development Environment
mamba env create -f environment.yml conda activate non_local_detector pip install -e .[dev]
-
Run Quality Checks
# Full quality check (required before committing) ruff check src/ && ruff format --check src/ && black --check src/ && pytest # Auto-fix linting issues ruff check --fix src/ ruff format src/
-
Testing
# Run all tests pytest # With coverage pytest --cov=non_local_detector --cov-report=html # Single test file pytest src/non_local_detector/tests/test_version_import.py
-
Building
# Build package python -m build # Check distribution twine check dist/*
The project uses GitHub Actions for continuous integration:
- Quality Gate: All PRs must pass linting, formatting, and type checking
- Multi-Version Testing: Tests run on Python 3.10, 3.11, and 3.12
- Package Building: Automatic wheel and source distribution building
- Coverage Reporting: Integration with Codecov
- API Reference: [Coming Soon]
- Tutorials: See
notebooks/directory for example analyses - Contributing Guide: See development section above
- Issue Tracker: GitHub Issues
We welcome contributions! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Run quality checks locally (see Development Workflow)
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
All contributions must:
- Pass all quality checks (
ruff,black,mypy) - Include tests for new functionality
- Follow the existing code style and patterns
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
- Author: Eric Denovellis
- Institution: Loren Frank Lab, UCSF
- Funding: [Add funding sources if applicable]
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- track_linearization: Spatial trajectory processing
- dynamax: JAX-based probabilistic state space models