Skip to content

circadia-bio/mrpheus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

112 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช‰ mrpheus

Raw physiological signal analysis for biological rhythms research.

r-universe R License: MIT Circadia Lab


๐Ÿ“– What is mrpheus?

mrpheus is the raw signal layer of the Circadia Lab R ecosystem. It ingests and processes multi-modal physiological recordings โ€” polysomnography (EDF/EDF+), MRI-concurrent physiological logs (Philips PMU), and EEG โ€” and extracts features across three biological rhythm domains: cardiac (QRS detection, HRV), respiratory (apnoea detection, respiratory metrics), and neural (sleep spindles, slow oscillations, automatic AASM sleep staging).

Staged hypnograms and derived metrics pass downstream to hypnor and syncR for cross-modal linking.

The name is spelled as Morpheus but carries a silent m, pronounced as Orpheus โ€” carrying both myths at once. Morpheus, god of dreams, gives the package its subject; Orpheus, who descended into the underworld to navigate the unconscious, gives it its spirit.


โœจ Features

  • ๐Ÿ“‚ EDF/EDF+ ingestion โ€” read_edf(), prepare_psg(): channel inventory, epoch segmentation, bad-channel flagging
  • ๐Ÿงน Preprocessing pipeline โ€” preprocess_psg(): channel renaming, DC removal, powerline notch (50/60 Hz + harmonics), per-channel-type bandpass filtering; operates on the full continuous signal before re-epoching
  • ๐Ÿ”Š Signal filters โ€” remove_dc(), detect_powerline(), notch_filter(), bandpass_filter(): exported functions that work on any plain numeric vector
  • ๐Ÿ‘๏ธ EOG artefact correction โ€” correct_eog_regression() (linear regression, no blink detection required), correct_eog_ica() (FastICA, threshold-based component rejection)
  • ๐Ÿšซ Artefact detection โ€” detect_artifacts(): amplitude and muscle contamination flagging
  • ๐Ÿ“Š Spectral analysis โ€” compute_band_power() (ฮด/ฮธ/ฮฑ/ฯƒ/ฮฒ/ฮณ per epoch, YASA-validated Welch PSD), compute_temporal_bandpower() (sliding-window band power across the full night), compute_spectrogram()
  • ๐Ÿ” Sleep event detection โ€” compute_spindles(), compute_slow_oscillations()
  • ๐Ÿ›๏ธ Automatic AASM staging โ€” stage_epochs(): pre-trained LightGBM model ported from YASA (Vallat & Walker, 2021); 77.8 % epoch accuracy on Sleep-EDF Cassette
  • ๐Ÿซ Respiratory metrics โ€” detect_apneas(), compute_ahi(), compute_odi()
  • ๐Ÿ’“ Cardiac rhythm โ€” detect_qrs() (Pan-Tompkins), compute_hr_signal(), compute_hrv_sleep()
  • ๐Ÿฅ Philips PMU ingestion โ€” read_philips_physlog(): wBTU / wired / custom presets, event markers, scan-window alignment
  • ๐Ÿ”— Ecosystem handoffs โ€” export_hypnogram() โ†’ hypnor; derived metrics โ†’ syncR

๐Ÿ—‚๏ธ Project Structure

mrpheus/
โ”œโ”€โ”€ R/
โ”‚   โ”œโ”€โ”€ mrpheus-package.R           # package doc + palette_orpheus
โ”‚   โ”œโ”€โ”€ read_edf.R                  # read_edf(), print.mrpheus_edf
โ”‚   โ”œโ”€โ”€ prepare_psg.R               # prepare_psg(), print.mrpheus_psg
โ”‚   โ”œโ”€โ”€ signal_filters.R            # remove_dc(), detect_powerline(),
โ”‚   โ”‚                               #   notch_filter(), bandpass_filter()
โ”‚   โ”œโ”€โ”€ preprocess_psg.R            # preprocess_psg()
โ”‚   โ”œโ”€โ”€ correct_eog.R               # correct_eog_regression(), correct_eog_ica()
โ”‚   โ”œโ”€โ”€ detect_artifacts.R          # detect_artifacts()
โ”‚   โ”œโ”€โ”€ compute_band_power.R        # compute_band_power()
โ”‚   โ”œโ”€โ”€ compute_temporal_bandpower.R# compute_temporal_bandpower()
โ”‚   โ”œโ”€โ”€ compute_spectrogram.R       # compute_spectrogram()
โ”‚   โ”œโ”€โ”€ compute_spindles.R          # compute_spindles()
โ”‚   โ”œโ”€โ”€ compute_slow_oscillations.R
โ”‚   โ”œโ”€โ”€ compute_hr_signal.R         # compute_hr_signal()
โ”‚   โ”œโ”€โ”€ compute_hrv_sleep.R         # compute_hrv_sleep()
โ”‚   โ”œโ”€โ”€ stage_epochs.R              # stage_epochs()
โ”‚   โ”œโ”€โ”€ staging_features.R          # internal YASA feature extraction
โ”‚   โ”œโ”€โ”€ export_hypnogram.R          # export_hypnogram()
โ”‚   โ”œโ”€โ”€ detect_qrs.R                # detect_qrs(), print.mrpheus_qrs
โ”‚   โ”œโ”€โ”€ detect_apneas.R             # detect_apneas(), compute_ahi(), compute_odi()
โ”‚   โ””โ”€โ”€ read_philips_physlog.R      # read_philips_physlog()
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ staging_features.cpp        # Rcpp hot paths: staging pipeline
โ”‚   โ””โ”€โ”€ event_detection.cpp         # Rcpp hot paths: spindles, slow oscillations
โ”œโ”€โ”€ inst/
โ”‚   โ”œโ”€โ”€ extdata/                    # example_physlog.log
โ”‚   โ”œโ”€โ”€ filters/                    # MNE FIR + resample poly coefficients
โ”‚   โ””โ”€โ”€ models/                     # serialised LightGBM staging model
โ”œโ”€โ”€ data-raw/                       # model extraction, parity validation scripts
โ”œโ”€โ”€ tests/testthat/
โ”œโ”€โ”€ vignettes/
โ”‚   โ”œโ”€โ”€ getting-started.Rmd
โ”‚   โ”œโ”€โ”€ mri-physiology.Rmd
โ”‚   โ”œโ”€โ”€ sleep-staging.Rmd
โ”‚   โ”œโ”€โ”€ eeg-preprocessing.Rmd
โ”‚   โ””โ”€โ”€ spectral-analysis.Rmd
โ”œโ”€โ”€ _pkgdown.yml
โ””โ”€โ”€ DESCRIPTION

๐Ÿš€ Getting Started

Installation

Install from r-universe (recommended โ€” pre-built binaries, no compiler required):

install.packages(
  "mrpheus",
  repos = c("https://circadia-bio.r-universe.dev", "https://cloud.r-project.org")
)

Or install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("circadia-bio/mrpheus")

Prerequisites

The staging model requires LightGBM. If the r-universe binary does not install it automatically:

install.packages("lightgbm")   # CRAN binary

Full PSG pipeline

library(mrpheus)

# 1. Ingest
rec <- read_edf("psg_001.edf")
psg <- prepare_psg(rec)

# 2. Preprocess
psg <- preprocess_psg(
  psg,
  channel_rename = c("C3-A2" = "C3", "C4-A1" = "C4")  # if needed
)

# 3. EOG correction
psg <- correct_eog_regression(psg)   # or correct_eog_ica()

# 4. Stage
stages <- stage_epochs(psg)

# 5. Band power
bp  <- compute_band_power(psg, relative = TRUE)
tbp <- compute_temporal_bandpower(psg, hypno = stages$stage)

# 6. Events
sp  <- compute_spindles(psg)
so  <- compute_slow_oscillations(psg)

# 7. Export to hypnor
hypnogram <- export_hypnogram(stages, start_time = rec$header$startTime)

๐Ÿ“ฆ Dependencies

Package Version Purpose
edfReader >= 1.2.1 EDF/EDF+ file ingestion
gsignal >= 0.3.5 IIR filter design, Welch PSD
pracma >= 2.4.2 Numerical utilities
lightgbm >= 4.0.0 YASA staging model inference
fastICA >= 1.2-0 ICA decomposition for EOG correction
dplyr >= 1.1.0 Tabular manipulation
tibble >= 3.2.0 Tidy output structures
cli >= 3.6.0 Diagnostic messages
rlang >= 1.1.0 Error handling
Rcpp >= 1.0.10 C++ hot-path implementations (feature extraction, rolling statistics, event detection)

๐Ÿ“„ Citation

If you use the automatic sleep staging feature, please also cite the YASA paper:

@article{vallat2021,
  author  = {Vallat, Raphael and Walker, Matthew P},
  title   = {An open-source, high-performance tool for automated sleep staging},
  journal = {eLife},
  volume  = {10},
  pages   = {e70092},
  year    = {2021},
  doi     = {10.7554/eLife.70092}
}

๐Ÿ‘ฅ Authors

Role Name Affiliation
Author, maintainer Lucas Franรงa Northumbria University, Circadia Lab
Author Mario Leocadio-Miguel Circadia Lab

๐Ÿค Related Tools

  • โŒš๏ธ zeitR โ€” wrist actigraphy analysis and circadian metrics
  • ๐Ÿ˜ตโ€๐Ÿ’ซ hypnor โ€” hypnogram handling, sleep architecture metrics, and visualisation
  • ๐Ÿ”„ syncR โ€” unified participant-indexed database (actigraphy + PSG + diary)
  • ๐Ÿงฒ boldR โ€” fMRIPrep BOLD derivatives โ†’ parcellated analysis
  • ๐Ÿงฎ tallieR โ€” sociodemographics and questionnaires
  • ๐Ÿ›Œ slumbR โ€” sleep diary processing
  • ๐ŸŽจ circadia โ€” shared visual identity (palettes, themes)
  • ๐Ÿ”ฌ circadia-bio โ€” the Circadia Lab GitHub organisation

๐Ÿ“„ Licence

Released under the MIT License.

Copyright ยฉ Lucas Franรงa, Mario Leocadio-Miguel, 2025


Staging model attribution: The LightGBM model bundled in inst/models/yasa_staging.txt was originally trained as part of YASA (Vallat & Walker, eLife, 2021; doi:10.7554/eLife.70092) and is redistributed here under YASA's BSD 3-Clause License with attribution.

About

๐Ÿช‰ Raw physiological signal analysis for biological rhythms research.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages