Skip to main content

Python interface to the Luna toolset for sleep signal analysis

Project description

lunapi

Python interface to the Luna C/C++ sleep-signal analysis engine.

lunapi exposes the full Luna command set to Python, enabling scripted and notebook-based workflows for polysomnographic (PSG) data — using the same analytical core as the Luna command-line tool and the Lunascope desktop application.


Statement of Need

Large-scale sleep research increasingly requires processing thousands of PSG recordings programmatically, while maintaining the ability to inspect individual records interactively. Existing Python EEG libraries are general-purpose frameworks that lack dedicated sleep-analysis primitives. lunapi provides direct Python access to Luna's domain-specific command set — covering spectral analysis, sleep staging, annotation handling, artifact detection, connectivity, and more — without requiring users to leave the Python/notebook environment or learn the Luna command-line interface.


Features

  • Full Luna command set — run any Luna command (PSD, SPINDLES, SOAP, POPS, …) directly from Python and receive structured results as DataFrames
  • Project-level workflows — load a sample list and execute commands across an entire cohort in a few lines
  • Per-recording access — attach EDF and annotation files, extract raw signals and annotation events, insert or modify signals in memory
  • Structured output — results returned as pandas DataFrames keyed by command and stratum
  • SQLite result stores — write output to .db files that can be queried as virtual TSV-like tables
  • Sleep staging — SOAP and POPS automated staging models accessible directly from Python
  • Visualization helpers — hypnogram, PSD, spectrogram, and topographic map plotting for notebooks
  • NSRR Moonbeam — download and attach recordings directly from the National Sleep Research Resource
  • GPA association modeling — linear association analysis on cohort-level result matrices
  • Docker containers — pre-built images with Luna, lunapi, and all dependencies

Installation

lunapi is distributed as a binary wheel via PyPI (no compiler required):

pip install lunapi

Supported platforms: macOS (Intel & Apple Silicon), Linux (x86_64), Windows 10/11 (x86_64), Python 3.9–3.14.

Using a virtual environment is recommended:

python3 -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install lunapi

Worked Example

Single-recording workflow

import lunapi as lp

# Create a project and attach a recording
p = lp.proj(verbose=False)
rec = p.inst("my_subject")
rec.attach_edf("/path/to/recording.edf")
rec.attach_annot("/path/to/recording.annot")

# List available channels
print(rec.chs())

# Run a Luna command and retrieve results as a DataFrame
rec.eval("PSD sig=EEG dB spectrum")
df = rec.table("PSD", "CH_F")
print(df.head())

Cohort workflow

import lunapi as lp

p = lp.proj(verbose=False)
p.sample_list("study.lst")        # three-column file: ID, EDF path, annot path

# Run PSD across all recordings (returns dict of DataFrames)
results = p.proc("PSD sig=EEG dB spectrum")
df = results["PSD"]["CH_F"]
print(df.groupby("CH")["PSD"].mean())

Automated sleep staging

rec.eval("RUN-POPS sig=C3 ref=M2 path=~/dropbox/pops")   # point to downloaded POPS models
stages = rec.table("RUN_POPS", "E")   # per-epoch predictions
print(stages["PRED"].value_counts())

Full documentation, command reference, and interactive notebooks are at zzz.nyspi.org/luna/.


Testing

pip install -e ".[dev]"
pytest

This runs 59 tests covering the core workflow (load instance, run commands, retrieve tables, extract signals, annotation handling, sample-list workflow), visualization helpers, GPA utilities, and package smoke tests. No real EDF files are required — a synthetic in-memory recording is generated by the test fixtures.


Documentation

Resource URL
Luna ecosystem https://zzz.nyspi.org/luna/
lunapi notebooks https://github.com/remnrem/luna-api/tree/main/notebooks
Lunascope desktop app https://github.com/Lorcan7274/lunascope
NSRR https://sleepdata.org

Contributing

See CONTRIBUTING.md for guidelines on bug reports, feature requests, and pull requests.

Questions / Support

Open an issue on GitHub or write to luna.remnrem@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

lunapi-1.6.1-cp314-cp314-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

lunapi-1.6.1-cp314-cp314-macosx_10_15_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

lunapi-1.6.1-cp313-cp313-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

lunapi-1.6.1-cp313-cp313-macosx_10_14_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.13macOS 10.14+ x86-64

lunapi-1.6.1-cp312-cp312-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

lunapi-1.6.1-cp312-cp312-macosx_10_14_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.12macOS 10.14+ x86-64

lunapi-1.6.1-cp311-cp311-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

lunapi-1.6.1-cp311-cp311-macosx_10_14_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.11macOS 10.14+ x86-64

lunapi-1.6.1-cp310-cp310-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

lunapi-1.6.1-cp310-cp310-macosx_10_14_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

lunapi-1.6.1-cp39-cp39-macosx_11_0_arm64.whl (8.5 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

lunapi-1.6.1-cp39-cp39-macosx_10_14_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

File details

Details for the file lunapi-1.6.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 af96ad1718921b5f23de33a3b5258ab7e54a5457006519e62f655c512c1e81c9
MD5 b5873d3f6f90a269826485632809309b
BLAKE2b-256 ac30907e5861ea02d1ee6518271cd5a339c78489c85ebc26679a59f89cf3f0f6

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 1d20597293b436e6e3c6fb40b2b073ab26f81f2147d0315bf6772781217d4547
MD5 205822e886b4a0714aae8d967045dd4b
BLAKE2b-256 ad86463e2418f369de3a42767c41c4965d47ec419b5e1148b6232ac97191daa7

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 821c76c855a02482f03a47c901e4dbfc27da2327759422d82c7e33a90bbfb955
MD5 486de188f88c1dcb9fb43c89f9f7509c
BLAKE2b-256 3fc42057f352e5d6b609eae3ca71836e52ada44de8163acc4739f9bb518d9850

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp313-cp313-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0a04b90db0d7be61cb48048b49e776bb0744d1fa5b8ffbe1147735d565671880
MD5 6df0d529c2c0848f02a5ec369aef4fb3
BLAKE2b-256 cb58aaea083fdd32b79bbe07e999d9d08846eb2e9d1738008da77676960262ba

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2f0e3d79ce6f5f7e4b5d79c7219a935d7e3f177e6c332ae1b02fe5797a705c1
MD5 08bb134ea57eff776230a26cf7886e8f
BLAKE2b-256 99d6b42a38bb3f9244554a80fd137d443301f13eef32c5365bb6f14793294030

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp312-cp312-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2d4c3c7c99643d0b31cce36810c8a2bd3e150c162f6440c30145d4e9d376efcf
MD5 d650d61029c816707648d07cc28bd0c2
BLAKE2b-256 5029e627877fab5829b10e07cc567756b3597a6cf9c3834da2dd0bc79de92f41

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 80ad4e8320606ddbc50d9a88eb51fb6c5133e681cac59f9969e0b1fa2ea7b221
MD5 c74b50532b57138730c950341826e706
BLAKE2b-256 290238af92353840d2024a0162b79470d6bc3a859f388a205947db646239dada

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp311-cp311-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c0a00dd642245c4a8c4eb3d81c5e6ba471df9a5b4d383fcefa0a6b93533f32f9
MD5 62b6a8dd704c02bbaaf17180c115781d
BLAKE2b-256 5fc6288ab72ab4f48c719278e6dc3ed12351a3bdaf31e3ddee94d65a8b151d1c

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c8084e5b76174c616ce0b009b3eedeb6dd00db55abf16ffe031829fe13dab661
MD5 87adc33b28bb6be11c20ff748dfe6219
BLAKE2b-256 f77ff544c6efa10ee494d7734fe050f255cde4e3af552901886f9d10c1cbda06

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b95440641a603eb5431ccc0f44a2fed3153111420b1ca1dd9c9f8831bb4be173
MD5 f551008a86baba0e337f0c3ae8b713c4
BLAKE2b-256 53b7e06d889027468c1c6ef172488ae21ca782bc2060b2ad04e2ddbec7ddcfa7

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ac17a2a1ba74c6e0bd9222131a1b4093d9c1b6bd27c463bacf2eca4abc783c1c
MD5 e838f07c8df1768c6a71fd076aa66568
BLAKE2b-256 4f51c37cefbbcd607b8610547d7e1ae27f06bf391e16d0be8c7eb80c3e978595

See more details on using hashes here.

File details

Details for the file lunapi-1.6.1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for lunapi-1.6.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6c959564fc2e84887b511d0c714d32996b02badac34f74940475ce298ec7d49d
MD5 997338f3062a24944c4fcee97ac380a6
BLAKE2b-256 f65d1bf2db40daa8579d7521f7c9a59322e47619ac00891e8ec5c3105ef1fbfc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page