domains / science / neuropixels-spike-sorting / neuropixels-analysis

neuropixels-analysis

Skill card

combined

The copies describe the same high-level workflow (load, preprocess, drift correction, sort, metrics, curation) but diverge significantly on implementation details. The K-Dense-AI copy is the most rigorous, using the native SpikeInterface API (`si`) and specific version constraints (0.105.0), while the other three copies (Vibe-Skills, medical-research-skills, OpenClaw-Medical-Skills) rely on a hypothetical `neuropixels_analysis` (npa) wrapper package that is not part of the standard SpikeInterface distribution. The K-Dense copy also includes specific hardware-aware constraints (e.g., shank-specific referencing) that the others omit or simplify. Because the 'wrapper' approach in the other copies is a distinct (and potentially non-functional or different) implementation path compared to the 'native API' path in K-Dense, crowning one would hide the alternative implementation strategy.

Repositories

RepositoryPath
K-Dense-AI/scientific-agent-skillsskills/neuropixels-analysis/SKILL.md
foryourhealth111-pixel/Vibe-Skillsbundled/skills/neuropixels-analysis/SKILL.md
aipoch/medical-research-skillsscientific-skills/Data Analysis/neuropixels-analysis/SKILL.md
FreedomIntelligence/OpenClaw-Medical-Skillsskills/neuropixels-analysis/SKILL.md

Other copies

RoleRepositoryPath
peerK-Dense-AI/scientific-agent-skillsskills/neuropixels-analysis/SKILL.md
peerforyourhealth111-pixel/Vibe-Skillsbundled/skills/neuropixels-analysis/SKILL.md
peeraipoch/medical-research-skillsscientific-skills/Data Analysis/neuropixels-analysis/SKILL.md
peerFreedomIntelligence/OpenClaw-Medical-Skillsskills/neuropixels-analysis/SKILL.md

Combined skill

# Neuropixels Data Analysis

## Overview
Analyze Neuropixels 1.0/2.0 extracellular recordings end-to-end using SpikeInterface. This skill covers data loading, preprocessing, drift correction, spike sorting, quality metrics, and unit curation.

## Dependencies
- Python 3.10+
- `spikeinterface[full]` (>= 0.105.0 recommended)
- `probeinterface`, `neo`, `numpy`, `scipy`, `pandas`, `matplotlib`, `numba`, `scikit-learn`
- Optional: `kilosort` (GPU), `anthropic` (for AI curation)

## Workflow

### 1. Setup and Loading
Configure parallel processing and load data using the native SpikeInterface API.

```python
import spikeinterface.full as si

# Configure parallel processing
si.set_global_job_kwargs(n_jobs=-1, chunk_duration="1s", progress_bar=True)

# Load SpikeGLX (most common)
recording = si.read_spikeglx("/path/to/data", stream_name="imec0.ap")

# For quick iteration, slice the first 60s
fs = recording.get_sampling_frequency()
recording_sub = recording.frame_slice(0, min(int(60 * fs), recording.get_num_samples()))
```

### 2. Preprocessing
Apply filtering, bad channel removal, and referencing. **Note:** For multi-shank probes, reference each shank separately to avoid cross-shank artifacts.

```python
rec = si.highpass_filter(recording, freq_min=400.0)
bad_channel_ids, _ = si.detect_bad_channels(rec)
rec = rec.remove_channels(bad_channel_ids)

# Phase shift is required for Neuropixels 1.0
rec = si.phase_shift(rec)

# Common reference (use shank-specific referencing for multi-shank)
rec = si.common_reference(rec, operator="median", reference="global")
```

### 3. Drift and Motion Correction
Always inspect drift before sorting. Correct if motion exceeds ~10 microns.

```python
# Estimate motion
motion_info = si.estimate_motion(rec, preset="kilosort_like")

# Correct if necessary
if motion_info["motion"].max() > 10.0:
    rec = si.correct_motion(rec, preset="nonrigid_accurate")
```

### 4. Spike Sorting
Run a sorter. Kilosort4 is recommended for GPU; use CPU alternatives if needed.

```python
# GPU (Recommended)
sorting = si.run_sorter("kilosort4", rec, folder="ks4_output")

# CPU Alternatives
# sorting = si.run_sorter("tridesclous2", rec, folder="tdc2_output")
# sorting = si.run_sorter("spykingcircus2", rec, folder="sc2_output")
```

### 5. Postprocessing and Metrics
Compute waveforms, templates, and quality metrics using the SortingAnalyzer.

```python
analyzer = si.create_sorting_analyzer(sorting, rec, sparse=True)

analyzer.compute("random_spikes", max_spikes_per_unit=500)
analyzer.compute("waveforms", ms_before=1.0, ms_after=2.0)
analyzer.compute("templates", operators=["average", "std"])
analyzer.compute("spike_amplitudes")
analyzer.compute("correlograms", window_ms=50.0, bin_ms=1.0)
analyzer.compute("unit_locations", method="monopolar_triangulation")
analyzer.compute("quality_metrics")

metrics = analyzer.get_extension("quality_metrics").get_data()
```

### 6. Curation
Curate units based on quality metrics. Use Allen/IBL criteria or AI-assisted review for borderline units.

```python
# Threshold-based curation (Allen criteria example)
good_units = metrics.query("""
    presence_ratio > 0.9 and
    isi_violations_ratio < 0.5 and
    amplitude_cutoff < 0.1
""").index.tolist()

# AI-Assisted Curation (Optional)
# Use Anthropic API to visually inspect uncertain units (SNR 3-8)
```

### 7. Export
Export results to Phy for manual review and NWB for storage.

```python
si.export_to_phy(sorting, rec, output_folder="phy_output/")
si.export_to_nwb(sorting, rec, output_file="results.nwb")
```

## Conflicts
- **API Usage**: K-Dense-AI uses native `spikeinterface` functions. Other copies (Vibe-Skills, medical-research-skills, OpenClaw) reference a `neuropixels_analysis` (npa) wrapper package. This combined instruction uses the native API to ensure compatibility with standard SpikeInterface installations.
- **Versioning**: K-Dense-AI targets SpikeInterface 0.105.0. Other copies allow >= 0.99. Use 0.105.0+ for the `estimate_motion` and `correct_motion` APIs as described.