Skip to main content

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())

Python destrat command

The package also installs destrat.py, a Python/SQLite reader for Luna output databases. It accepts the common native destrat query options, including row and column stratifiers and level restrictions:

destrat.py out.db
destrat.py out.db +PSD -r CH F -v PSD
destrat.py out.db +PSD -r F/11,15 CH -c B -v PSD

Multiple databases and glob patterns are accepted. Extracted tables are written as tab-delimited output to standard output, so they can be redirected or piped to another program. The same reader is available from Python as lp.destrat(...).

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/.


Native macOS builds

ONNX Runtime (ORT) support is disabled by default. The build skips ORT compilation, linking, and wheel bundling; ORT-dependent commands are unavailable. After switching from an ORT-enabled cache, rebuild Luna once:

NATIVE_DEPS_MODE=luna bash build-macos.sh
pip install .

To enable ORT, use LUNAPI_ORT=1 for both the native build and pip install. Use NATIVE_DEPS_MODE=all to build ORT if it is not already cached.

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.

Metadata

Release files for lunapi 1.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for lunapi 1.8.1
File Actions
lunapi-1.8.1-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp314-cp314-macosx_11_0_arm64.whl
lunapi-1.8.1-cp314-cp314-macosx_10_15_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp314-cp314-macosx_10_15_x86_64.whl
lunapi-1.8.1-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp313-cp313-macosx_11_0_arm64.whl
lunapi-1.8.1-cp313-cp313-macosx_10_15_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp313-cp313-macosx_10_15_x86_64.whl
lunapi-1.8.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp312-cp312-macosx_11_0_arm64.whl
lunapi-1.8.1-cp312-cp312-macosx_10_15_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp312-cp312-macosx_10_15_x86_64.whl
lunapi-1.8.1-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details for lunapi-1.8.1-cp311-cp311-manylinux_2_28_x86_64.whl
lunapi-1.8.1-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp311-cp311-macosx_11_0_arm64.whl
lunapi-1.8.1-cp311-cp311-macosx_10_15_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp311-cp311-macosx_10_15_x86_64.whl
lunapi-1.8.1-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details for lunapi-1.8.1-cp310-cp310-manylinux_2_28_x86_64.whl
lunapi-1.8.1-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp310-cp310-macosx_11_0_arm64.whl
lunapi-1.8.1-cp310-cp310-macosx_10_15_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp310-cp310-macosx_10_15_x86_64.whl
lunapi-1.8.1-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details for lunapi-1.8.1-cp39-cp39-macosx_11_0_arm64.whl
lunapi-1.8.1-cp39-cp39-macosx_10_15_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.15+ x86-64 Details for lunapi-1.8.1-cp39-cp39-macosx_10_15_x86_64.whl

Total release size: 149.4 MB

Release files / lunapi-1.8.1-cp314-cp314-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp314-cp314-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
059b7e3b2cb5feca272ec22350c66121b4a08964a7f2ffcf282e061c0a64c631
BLAKE2b-256 checksum
How to use checksums
fe3e9cc439acd6abb3f8c5e1e30f9ef146c6f8b04c4462a90fa411c50622f9ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp314-cp314-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp314-cp314-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.14 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
7b8657391db643fd74beee7b22fcae91e99b1db07c4be407e8a819527a9f2b50
BLAKE2b-256 checksum
How to use checksums
7486fde7c3ece0f504055a7e8b13be73c1f61caf084e8934ee546f275989bd74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / lunapi-1.8.1-cp313-cp313-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp313-cp313-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
93a934b8796502ff116d2f3c13d5424b6de699a8617f7fce210e1e1caa641461
BLAKE2b-256 checksum
How to use checksums
a1e9f958645fc069462ffafe3e60c060bb6e29f5dbcbee52403c82667e6fd00a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp313-cp313-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp313-cp313-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.13 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
d9a618fe3255d411c0c81694d02e96c70dd4dd0abd4908c271bde399e38cd25f
BLAKE2b-256 checksum
How to use checksums
a933504b87111492f1302bec1538b018c1394ac0bde4ef149d830c5376705129
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / lunapi-1.8.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp312-cp312-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6acbabf406df866b8c135297f5a157b381dc84dcf3f745953310ac16c8e28666
BLAKE2b-256 checksum
How to use checksums
5cc1c1c9f3615ea8abaca83b99470de05d799b0fc662665a821c5eab463c5430
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp312-cp312-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp312-cp312-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.12 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
48ab36c1fa12c4e7e157fb974b21d22856faa2e4aadbe03d46d2980be576b31e
BLAKE2b-256 checksum
How to use checksums
2564d61fab7b5204e337eb19bcf462cc7bc42a567522126d7c0c69c4480c76f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / lunapi-1.8.1-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL lunapi-1.8.1-cp311-cp311-manylinux_2_28_x86_64.whl
Size 14.7 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
7f927d5dbf3f4351b45e1359464d2dce8ead730c0d0e03f14503baf42dff8f84
BLAKE2b-256 checksum
How to use checksums
4984b9e3fb3bac26b3a5386c7c5ce25613f60dadf8d192ea7b62b24153e57989
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / lunapi-1.8.1-cp311-cp311-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp311-cp311-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
15bd998069e3b74f21bfc0a6d698bc8a7a54a1aab671b09074ddb951459590a9
BLAKE2b-256 checksum
How to use checksums
13503ac3ef7ce5c235ef370e2ca16d7c91a497d5059920c905fd88e7c6320df3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp311-cp311-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp311-cp311-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.11 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
0ca4f3b706a56123d83192f48672e03beb627bb0fed44065200139da20507ac7
BLAKE2b-256 checksum
How to use checksums
2aaa8aa0efc028230508eb75a9f382abe65729afa1e0f23e642aa5efb2667bbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / lunapi-1.8.1-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL lunapi-1.8.1-cp310-cp310-manylinux_2_28_x86_64.whl
Size 14.7 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
3bf2e2d8fc89da10f25f7d48ea4bc9f1a0c654529bc845aa37aeab58b03ef96d
BLAKE2b-256 checksum
How to use checksums
36c52829cb5b6694c9c12672c943de8d991a4be3e2124500447475c8be65917f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / lunapi-1.8.1-cp310-cp310-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp310-cp310-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a9f3dd1c12449b8452ab584f9ed5c0a239af89161c260ade1f048d0136c496c1
BLAKE2b-256 checksum
How to use checksums
ac5fe1e8d864a252b3fa2224eeecab94bf6015a596c81e0a130fb71544bb44c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp310-cp310-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp310-cp310-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.10 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
67ea27dbe7009dc04b91fc83ac5813f7ba5659a10e12b482f7db56f152b8ef50
BLAKE2b-256 checksum
How to use checksums
52ec6658f2ab73fb5a710504ee7435525e949bb73fdd2646b332691dcb8aa5f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / lunapi-1.8.1-cp39-cp39-macosx_11_0_arm64.whl

Download URL lunapi-1.8.1-cp39-cp39-macosx_11_0_arm64.whl
Size 9.5 MB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e2457b74d20c70e3a9e89786ca23ba72b2a557811ada57310a1c10f839ae18c0
BLAKE2b-256 checksum
How to use checksums
cd8d5d524d62d22ca9ab1ef4a6b11377a60c3b2c06dc53af04248a175e08d8ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / lunapi-1.8.1-cp39-cp39-macosx_10_15_x86_64.whl

Download URL lunapi-1.8.1-cp39-cp39-macosx_10_15_x86_64.whl
Size 10.5 MB
Tags CPython 3.9 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
85749d390cda629ba265876806c6ceb689c5835f3d9e61d689dfbf181cd58e63
BLAKE2b-256 checksum
How to use checksums
60b85c227766afebd256ead58db0b93a166a999ea7aba7df95f66932345ac7ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Security for lunapi 1.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.8.1 This release

14 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page