|
2 | 2 |
|
3 | 3 | This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
4 | 4 |
|
| 5 | +## Overview |
| 6 | + |
| 7 | +`spectral_connectivity` is a Python package for computing multitaper spectral estimates and frequency-domain brain connectivity measures. It provides tools for functional and directed connectivity analysis of electrophysiological data, with optional GPU acceleration via CuPy. |
| 8 | + |
| 9 | +## Core Architecture |
| 10 | + |
| 11 | +The package follows a modular design with three main components: |
| 12 | + |
| 13 | +1. **Transforms** (`spectral_connectivity/transforms.py`): Implements the `Multitaper` class for computing multitaper Fourier transforms |
| 14 | +2. **Connectivity** (`spectral_connectivity/connectivity.py`): The `Connectivity` class computes various connectivity measures from spectral estimates |
| 15 | +3. **Wrapper** (`spectral_connectivity/wrapper.py`): Provides `multitaper_connectivity()` function as a high-level interface |
| 16 | + |
| 17 | +### Key Design Patterns |
| 18 | + |
| 19 | +- **GPU/CPU Abstraction**: Uses `xp` namespace (numpy or cupy) controlled by `SPECTRAL_CONNECTIVITY_ENABLE_GPU` environment variable |
| 20 | +- **Caching**: Frequently computed quantities like cross-spectral matrices are cached for performance |
| 21 | +- **Expectation Framework**: Uses `EXPECTATION` dictionary to handle averaging over different dimensions (time, trials, tapers) |
| 22 | + |
| 23 | +### Main Classes |
| 24 | + |
| 25 | +- `Multitaper`: Handles multitaper spectral estimation |
| 26 | +- `Connectivity`: Computes connectivity measures from spectral data |
| 27 | +- Both classes support method chaining and lazy evaluation patterns |
| 28 | + |
5 | 29 | ## Development Commands |
6 | 30 |
|
7 | 31 | ### Environment Setup |
8 | 32 | ```bash |
9 | | -# Create conda environment and install dependencies |
| 33 | +# Create conda environment |
10 | 34 | conda env create -f environment.yml |
11 | 35 | conda activate spectral_connectivity |
12 | 36 | pip install -e . |
13 | 37 | ``` |
14 | 38 |
|
15 | 39 | ### Testing |
16 | 40 | ```bash |
17 | | -# Run all tests (requires nitime to be installed) |
18 | | -pytest |
| 41 | +# Run all tests with coverage |
| 42 | +pytest --cov=spectral_connectivity tests/ --cov-report=lcov:coverage.lcov -v |
19 | 43 |
|
20 | 44 | # Run specific test file |
21 | | -pytest tests/test_connectivity.py |
| 45 | +pytest tests/test_connectivity.py -v |
22 | 46 |
|
23 | | -# Run tests with coverage |
24 | | -pytest --cov=spectral_connectivity |
| 47 | +# Run single test |
| 48 | +pytest tests/test_connectivity.py::TestConnectivity::test_coherence -v |
25 | 49 | ``` |
26 | 50 |
|
27 | 51 | ### Code Quality |
28 | 52 | ```bash |
29 | | -# Format code with black |
30 | | -black spectral_connectivity/ tests/ |
| 53 | +# Format code |
| 54 | +black . |
31 | 55 |
|
32 | | -# Lint with ruff (replaces flake8, isort, pydocstyle) |
33 | | -ruff check spectral_connectivity/ tests/ |
| 56 | +# Lint code |
| 57 | +flake8 |
34 | 58 |
|
35 | | -# Auto-fix with ruff |
36 | | -ruff check --fix spectral_connectivity/ tests/ |
37 | | - |
38 | | -# Type checking with mypy |
| 59 | +# Type checking |
39 | 60 | mypy spectral_connectivity/ |
40 | | -``` |
41 | 61 |
|
42 | | -### Documentation |
43 | | -```bash |
44 | | -# Build documentation (from docs/ directory) |
45 | | -cd docs/ |
46 | | -make html |
| 62 | +# Documentation style |
| 63 | +pydocstyle spectral_connectivity/ |
47 | 64 | ``` |
48 | 65 |
|
49 | | -### Build and Release |
| 66 | +### Building and Release |
50 | 67 | ```bash |
51 | 68 | # Build package |
52 | | -python -m build |
53 | | - |
54 | | -# Check distribution before uploading |
55 | | -twine check dist/* |
56 | | - |
57 | | -# Upload to PyPI (requires twine and proper credentials) |
58 | | -twine upload dist/* |
| 69 | +hatch build |
59 | 70 |
|
60 | | -# Or use the automated release workflow by creating a version tag |
61 | | -git tag v1.2.0 |
62 | | -git push origin v1.2.0 |
63 | | -# This triggers the release.yml workflow which builds, tests, and publishes automatically |
| 71 | +# Clean build artifacts |
| 72 | +rm -rf build/ dist/ *.egg-info/ |
64 | 73 | ``` |
65 | 74 |
|
66 | | -## Code Architecture |
67 | | - |
68 | | -### Core Components |
69 | | - |
70 | | -The package follows a modular architecture with three main components: |
71 | | - |
72 | | -1. **transforms.py**: `Multitaper` class - Handles time-to-frequency domain transformation using multitaper methods |
73 | | -2. **connectivity.py**: `Connectivity` class - Computes various connectivity measures from spectral data |
74 | | -3. **wrapper.py**: High-level convenience functions that combine transforms and connectivity analysis |
75 | | - |
76 | | -### Key Classes |
77 | | - |
78 | | -- **`Multitaper`**: Primary class for spectral analysis using Slepian tapers |
79 | | - - Transforms time series to frequency domain |
80 | | - - Supports windowing, overlapping, and multiple trials |
81 | | - - Caches computations for efficiency |
82 | | - |
83 | | -- **`Connectivity`**: Main connectivity analysis class |
84 | | - - Takes Multitaper output or raw Fourier coefficients |
85 | | - - Implements 15+ connectivity measures (coherence, Granger causality, phase measures) |
86 | | - - Handles both functional and directed connectivity |
| 75 | +## Important Configuration |
87 | 76 |
|
88 | 77 | ### GPU Support |
| 78 | +Set environment variable `SPECTRAL_CONNECTIVITY_ENABLE_GPU=true` to enable GPU acceleration via CuPy. |
89 | 79 |
|
90 | | -The package supports GPU acceleration via CuPy: |
91 | | -- Set `SPECTRAL_CONNECTIVITY_ENABLE_GPU=true` environment variable |
92 | | -- Requires CuPy installation (`pip install cupy` or `conda install cupy`) |
93 | | -- GPU/CPU switching is handled automatically at import time in both `transforms.py` and `connectivity.py` |
94 | | -- Uses `xp` alias throughout codebase for numpy/cupy compatibility |
95 | | - |
96 | | -### Data Flow |
| 80 | +### Dependencies |
| 81 | +- Core: numpy, scipy, xarray, matplotlib |
| 82 | +- Dev tools: pytest, black, flake8, mypy, pydocstyle |
| 83 | +- Optional GPU: cupy-cuda12x |
97 | 84 |
|
98 | | -1. **Input**: Time series data (n_time, n_trials, n_signals) |
99 | | -2. **Transform**: `Multitaper` → Fourier coefficients + metadata |
100 | | -3. **Analysis**: `Connectivity.from_multitaper()` → Various connectivity measures |
101 | | -4. **Output**: Arrays or xarray DataArrays with proper labeling |
| 85 | +## Testing Strategy |
102 | 86 |
|
103 | | -### Caching Strategy |
| 87 | +- Unit tests in `tests/` directory mirror the source structure |
| 88 | +- CI runs tests on Python 3.9+ and Ubuntu |
| 89 | +- Coverage reporting via Coveralls |
| 90 | +- Notebook integration tests execute tutorial examples |
| 91 | +- Tests include both CPU and GPU code paths when available |
104 | 92 |
|
105 | | -The package implements intelligent caching: |
106 | | -- Cross-spectral matrices are cached in `Connectivity` class |
107 | | -- Minimum phase decompositions are cached for Granger causality measures |
108 | | -- This allows fast computation of multiple connectivity measures from the same spectral data |
| 93 | +## File Structure |
109 | 94 |
|
110 | | -### Testing Dependencies |
111 | | - |
112 | | -Tests require additional dependencies not included in core package: |
113 | | -- `nitime`: For validating DPSS window implementations |
114 | | -- Install via: `pip install nitime` or use dev dependencies: `pip install -e .[dev]` |
115 | | - |
116 | | -### Python Version Requirements |
117 | | - |
118 | | -This package requires Python 3.10 or later. The package is tested on: |
119 | | -- Python 3.10 |
120 | | -- Python 3.11 |
121 | | -- Python 3.12 |
122 | | -- Python 3.13 |
123 | | - |
124 | | -### Code Quality Tools |
125 | | - |
126 | | -The project uses modern Python tooling: |
127 | | -- **Black**: Code formatting (88 character line length) |
128 | | -- **Ruff**: Fast Python linter (replaces flake8, isort, pydocstyle, and more) |
129 | | -- **MyPy**: Static type checking with numpy plugin |
130 | | - |
131 | | -All tools are configured in [pyproject.toml](pyproject.toml). |
| 95 | +``` |
| 96 | +spectral_connectivity/ |
| 97 | +├── __init__.py # Main API exports |
| 98 | +├── connectivity.py # Connectivity measures |
| 99 | +├── transforms.py # Multitaper transforms |
| 100 | +├── wrapper.py # High-level interface |
| 101 | +├── minimum_phase_decomposition.py |
| 102 | +├── statistics.py # Statistical utilities |
| 103 | +└── simulate.py # Data simulation utilities |
| 104 | +``` |
0 commit comments