A Python pipeline for classifying sleep states (Wake / NREM / REM) in freely moving mice from EEG, EMG, accelerometer, and video-tracked velocity, plus the analysis and figure/table-generation code for the accompanying manuscript.
src/ Installable package — the core pipeline (see below)
scripts/ Standalone CLI scripts (validation runs, one-off diagnostics)
notebooks/
figures/ Manuscript figure notebooks, numbered in pipeline order (01_FigAnexa .. 08_Figuras_presentacion)
tables/ Manuscript table notebooks, numbered in dependency order (01_Tabla1 .. 04_TablaS5)
conversion/ Raw-data conversion notebooks, numbered by route (01_rhd_to_h5, 02_mat_h5_conversor)
exploratory/ Scratch / exploratory analysis notebooks (unnumbered, no fixed sequence)
results/ Generated CSVs, figures, and tables (git-tracked, reproducible)
legacy/ Superseded code kept for reference (see legacy/README.md)
docs/ Additional notes (data layout, etc.)
data/ Raw/processed recordings — not tracked, see docs/data_layout.md
| Module | Role |
|---|---|
Sleep.py |
Sleep-state classifier (classify_sleep_states_v7, canonical) |
process_data.py |
EMG/EEG signal preprocessing, spectrogram computation |
Pipeline.py |
Pipeline orchestration and logging |
evaluations.py |
Evaluation/metrics engine (F1, balanced accuracy, LOAO helpers, HDF5 persistence) |
utils.py |
HDF5 I/O, filtering, and other shared utilities |
Videotools.py |
DeepLabCut-derived velocity processing |
PSD.py |
Power spectral density / multitaper band-power calculation |
graphs.py, plot_config.py, Plot_3.py, plot4.py, FiguraS2.py |
Plotting helpers backing the manuscript figures |
Tabla1.py, Tabla2.py, TablaS4.py, TablaS5.py |
Manuscript table builders |
loao_validation.py |
Leave-One-Animal-Out cross-validation |
Arrive.py |
ARRIVE-guideline reporting |
load_intan_rhd_format.py, convert_rhd.py, intanutil/ |
Intan RHD2000 raw-file reading and conversion |
rem_classifier_per_animal.py, rem_classifier_per_animal_oracle.py, rem_error_analysis.py |
Per-animal REM/NREM classifier analysis |
nrem_oracle_all_sessions.py, verify_manuscript_metrics.py |
Manuscript metric verification |
Requires Python 3.9 (the pipeline was developed and tested against the
miogram conda environment on Windows).
pip install -e .This installs every module under src/ (plus intanutil/) as top-level
importable packages/modules — import evaluations, import Sleep, etc. work
from any working directory once installed, matching how every notebook and
script in this repo already imports them (flat, unprefixed names).
For an exact, reproducible dependency snapshot (the versions this pipeline
was last verified against), see requirements-freeze.txt:
pip install -r requirements-freeze.txtpyproject.toml lists the real top-level dependencies with loose version
constraints for portability; pynwb/hdmf/hdmf_zarr/zarr/nwbinspector
are an optional [nwb] extra since only the archived code under
legacy/_archive_py/ uses them.
Every notebook's first cell calls reporoot.chdir_to_repo_root(), which
walks up from the current directory to find pyproject.toml and chdirs
there — so relative paths inside the notebooks (reading from data/,
writing to results/) resolve correctly no matter where Jupyter's working
directory starts. Notebooks under notebooks/conversion/, notebooks/tables/,
and notebooks/figures/ are prefixed 01_, 02_, ... in the order they're
meant to be run — later ones generally depend on outputs (or dataframes) from
earlier ones in the same folder. Run them in that order, top to bottom, to
regenerate the corresponding CSV/PDF outputs in results/.
notebooks/exploratory/ is unnumbered scratch analysis with no fixed
sequence.
Reproducing anything beyond the checked-in results/ outputs requires the
raw/processed session data on disk — see docs/data_layout.md.
Functions and modules confirmed to have zero live callers were moved out of
the active codebase into legacy/ rather than deleted, so the git history
stays intact if they're ever needed for reference. See
legacy/README.md for what's there and, just as
importantly, what was not moved despite misleading "legacy" naming (some
functions are still load-bearing for the manuscript figures).
MIT — see LICENSE.