This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
track_linearization is a Python package for mapping 2D trajectories to 1D using Hidden Markov Models (HMMs). The core functionality involves converting animal movement tracks on 2D environments (like mazes) into linear representations.
src/track_linearization/: Main package sourcecore.py: Core algorithms for position linearization, track segment projection, and HMM-based analysisutils.py: Utility functions for graph construction, plotting, and visualization__init__.py: Package initialization with main API exportstests/: Test suite with comprehensive coverage
notebooks/: Jupyter notebooks for examples and analysistrack_linearization_tutorial.ipynb: Comprehensive pedagogical tutorial (recommended starting point)test_linearization.ipynb: Original test examples
build/: Build artifacts (generated)htmlcov/: Test coverage reports (generated)
The package follows a two-module design:
-
Core Module (
core.py): Contains the main algorithmsget_linearized_position(): Main API function for 2D→1D conversion- Track segment projection and distance calculations
- HMM-based position estimation using scipy sparse matrices
- Edge mapping: Merge or relabel track segments for behavioral analysis
- Optional numba acceleration when available
-
Utils Module (
utils.py): Graph construction and visualizationmake_track_graph(): Creates NetworkX graphs from node positions and edges- Plotting functions for track visualization and animations
infer_edge_layout(): Automatically determine edge order and spacing (renamed fromget_auto_linear_edge_order_spacing)- Edge ordering and spacing utilities
The package uses NetworkX graphs to represent tracks, where nodes have pos attributes (spatial coordinates) and edges have distance and edge_id attributes.
The edge_map parameter enables merging track segments into unified linear coordinate systems:
How it works:
- Calculate linear positions using original edge_order (preserves accurate 2D→edge projection)
- Build merged coordinate system by identifying which edges map to same target
- Adjust linear positions: calculate offset within original edge, add to merged edge's start position
- Apply edge_map to output segment IDs with proper dtype handling (supports strings)
Key behavior: Positions N units from the start of any merged edge have the same linear position, even if they're at different 2D locations. This enables behavioral analyses that treat different spatial paths as equivalent (e.g., T-maze left/right arms).
# Run all tests with coverage
python -m pytest src/track_linearization/tests/ -v
# Run specific test file
python -m pytest src/track_linearization/tests/test_core.py -v
# Run with coverage report
python -m pytest src/track_linearization/tests/ -v --cov=track_linearization --cov-report=html --cov-report=term-missing
# Run single test
python -m pytest src/track_linearization/tests/test_core.py::test_specific_function -v# Install dev dependencies first
pip install -e .[dev]
# Linting with ruff
python -m ruff check src/track_linearization/
# Auto-fix ruff issues
python -m ruff check src/track_linearization/ --fix
# Type checking with mypy
python -m mypy src/track_linearization/
# Code formatting with black
python -m black src/track_linearization/
# Format check only (no changes)
python -m black --check src/track_linearization/# Install pre-commit hooks
pre-commit install
# Run hooks manually on all files
pre-commit run --all-files
# Run specific hook
pre-commit run black --all-files# Build package
python -m build
# Install in development mode
pip install -e .
# Install with optional dependencies
pip install -e .[opt] # numba, ipympl for performance/visualization- Update CHANGELOG.md with changes
- Create and push a version tag:
git tag v2.4.0 git push origin v2.4.0
- GitHub Actions automatically:
- Runs tests across Python 3.10-3.13
- Builds wheel and sdist
- Publishes to PyPI via Trusted Publishing
- Creates GitHub Release
See .github/PYPI_TRUSTED_PUBLISHING.md for detailed setup instructions.
- Core (with minimum versions):
- numpy >= 1.24
- scipy >= 1.10
- matplotlib >= 3.7
- pandas >= 2.0
- dask[array] >= 2023.5.0
- networkx >= 3.2.1
- Optional performance: numba (for acceleration)
- Optional visualization: ipympl (for interactive plots)
- Development: black, pytest, pytest-cov, ruff, mypy, pandas-stubs, pre-commit
Tests are configured in pyproject.toml with:
- Test path:
src/track_linearization/tests - Coverage reporting enabled by default
- Doctest modules included
- Python 3.10+ support
- The package detects numba availability at runtime and uses acceleration when possible
- Track graphs must have nodes with
posattributes (spatial coordinates) - Edge attributes include
distance(Euclidean) andedge_idfor identification - Version is managed via hatch-vcs from git tags