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
pandasDataFrames keyed by command and stratum - SQLite result stores — write output to
.dbfiles 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lunapi-1.6.2.post1-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 10.5 MB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7e8a3b9eaf230450fd4136a03f25563383a8c9d6c9aec7337456322b0396b6b
|
|
| MD5 |
28ef5f2196c32a1d917a3ae445acf452
|
|
| BLAKE2b-256 |
b8b5503c2c5fd14a9dc13cac943e2942691c476a7102b730fa61169edf87e3ec
|
File details
Details for the file lunapi-1.6.2.post1-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e270f2e27bdb412584b62beed4371205d74cd1a51583137b9b3319be4a7b539
|
|
| MD5 |
8271621056de1145f65048ce97ab9282
|
|
| BLAKE2b-256 |
252fdba559ddd98f1b39c25b0faf41e9d8b526eb7cd91efed57aa92b3b308cdb
|
File details
Details for the file lunapi-1.6.2.post1-cp314-cp314-macosx_10_15_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp314-cp314-macosx_10_15_x86_64.whl
- Upload date:
- Size: 9.5 MB
- Tags: CPython 3.14, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a886df72b191d4e5eab3d557adde29b14a5006691cc8207f377c7ba532cbfe74
|
|
| MD5 |
f147ca6f1c01418ddf897a4419e19cc7
|
|
| BLAKE2b-256 |
36ac4d8c116d551b2ec0e7e5aa7b2a0b3a6b41e481e24d3f372884847c94837f
|
File details
Details for the file lunapi-1.6.2.post1-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7741b653254203af49f9d727bec11871edaf7d864aee531206d9ea34ef6cf8c
|
|
| MD5 |
af4f5ac0f79f6d46b789e4bd10b4ae0f
|
|
| BLAKE2b-256 |
8e95eb13659768a8801e775d12cd9dedc1f658898e57d5b50e387b004147af75
|
File details
Details for the file lunapi-1.6.2.post1-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
757a8f66440c20f23c4cee95697f5fa846a6f14878160e4ee19882fa3cde8320
|
|
| MD5 |
c751f5cb8307a6c412a2a8a6e9ef65da
|
|
| BLAKE2b-256 |
be05e6d8dfffd60b7f77d7f6b83dffb489890b7684d5e24699a56d5c2d84a4b3
|
File details
Details for the file lunapi-1.6.2.post1-cp313-cp313-macosx_10_14_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp313-cp313-macosx_10_14_x86_64.whl
- Upload date:
- Size: 9.5 MB
- Tags: CPython 3.13, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eda6aaeeb57ae34b4fc5f74ee6f3bd33c86452ec1af1a4e94c4fa43d39b02dab
|
|
| MD5 |
58abf2540084cdfbe5d7f2aeeee2a298
|
|
| BLAKE2b-256 |
35478973206d7166e7960c9122f38d440565e6d1953c6e8935948ec21c592339
|
File details
Details for the file lunapi-1.6.2.post1-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e97acc81e6f556601b7b25566f8e25c12222a5216b964bbfbfea02356cf92ff
|
|
| MD5 |
d7ed4a72a0119cfa1dab53851660bd5a
|
|
| BLAKE2b-256 |
b5d8df87b74f1439bf324ad0159130408ad07d629d0c862d56826a2814b70dd5
|
File details
Details for the file lunapi-1.6.2.post1-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b7bb14cc8d89f0a81eb7ac6c4e2afce757c92445ae189f9c185ca16685d7777
|
|
| MD5 |
72c410d44d0659cb872b353a1b165e47
|
|
| BLAKE2b-256 |
8fae02390de279f02cb2948649fae642ecb21f006a7c33e518b5cf38269f2e06
|
File details
Details for the file lunapi-1.6.2.post1-cp312-cp312-macosx_10_14_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp312-cp312-macosx_10_14_x86_64.whl
- Upload date:
- Size: 9.5 MB
- Tags: CPython 3.12, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4361fcdae04bac943be5763606a2d157d137b4416c062be8d736e591b1960b4f
|
|
| MD5 |
a350ad5acb8e641059e50c07b9d01f35
|
|
| BLAKE2b-256 |
67bc6b1c9b3be8894a4de40640094bbd867dd0334d212411bf7ea4ee822a57e1
|
File details
Details for the file lunapi-1.6.2.post1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0faa5d0bb0ac6e27cdf546cf0aca08b1a99464fa78fdbd2591b5e0e0ce88998
|
|
| MD5 |
721ea53dfab3701f1b13e58bd3f45364
|
|
| BLAKE2b-256 |
40df6f269d1676c8d5be76e1dd5fe1ef559c5d5b6529086990bb44c1c8a5c39a
|
File details
Details for the file lunapi-1.6.2.post1-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db349830579b546658d54c6a7c9b0080ca0fc9e681e53006b28cce75f0db0df3
|
|
| MD5 |
d5b434c37366e10d583a4464ca21c2ee
|
|
| BLAKE2b-256 |
33214bd65c6a6ecc98230a74f002eef1c38de54779ca68332dfc252b8385656c
|
File details
Details for the file lunapi-1.6.2.post1-cp311-cp311-macosx_10_14_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp311-cp311-macosx_10_14_x86_64.whl
- Upload date:
- Size: 9.5 MB
- Tags: CPython 3.11, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc2dcf97c592afda9246ef41fe9f71c09791b4e476324a6f990650f9dd68f30d
|
|
| MD5 |
759d87626baea42f4920715d2a62e919
|
|
| BLAKE2b-256 |
f2974d4227edfa005842fc692c52979a4b3f6d6f12761e226c2cd858b5a5f3d0
|
File details
Details for the file lunapi-1.6.2.post1-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e06b72eee8b75afff54c1d997fbec2b76c40afbf6a5799af335ac8f651825bba
|
|
| MD5 |
8c3f6f483e27e345693af3bb02856bce
|
|
| BLAKE2b-256 |
ccc45925adc3857873aca30c4db7f044d1d85ee7096bfa7e4f15f00f7091c843
|
File details
Details for the file lunapi-1.6.2.post1-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a8eeb1f3e75b7fe5a49f16832faf6f14fe2f45f0727cdd452c7b2f336359950
|
|
| MD5 |
fc465066013cfe31ff44607d60ba7e32
|
|
| BLAKE2b-256 |
d4e76bb9f7aa63b6eb0422a1b6cacf3251298dc286518d99c9a584a20a011a77
|
File details
Details for the file lunapi-1.6.2.post1-cp310-cp310-macosx_10_14_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp310-cp310-macosx_10_14_x86_64.whl
- Upload date:
- Size: 9.4 MB
- Tags: CPython 3.10, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bf5bbf40c41306a605e6cd23a911d4c00bacc7ba04e8f24e8c98ab385a7aabd
|
|
| MD5 |
55b4a1ee1ec05558d0c104b9254cc822
|
|
| BLAKE2b-256 |
a84987622a557d6152d93a55a4928e59de70b44280693ee86f2f985493d9eac0
|
File details
Details for the file lunapi-1.6.2.post1-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98ee0ff4b49caa43042964f96b6e4b7a9491a4dfbed9c9a274a73db9524a827b
|
|
| MD5 |
443155a8782addb2fe32b8de54c2f84d
|
|
| BLAKE2b-256 |
08208b6f8953735630520076f40a4667216287d2d5d3f0cf151c74aaf0e66c3d
|
File details
Details for the file lunapi-1.6.2.post1-cp39-cp39-macosx_11_0_arm64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.5 MB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43755f63e56d8ed274efd735dfc1cce9a0f6dff963d3af54c5ee769c36376b81
|
|
| MD5 |
e319babdb2f894c756f9f2f2b2e0aa7c
|
|
| BLAKE2b-256 |
1b58ef4c17b380f91c6f4a941849d8ba449bd9cfea591f38e41b863622d93925
|
File details
Details for the file lunapi-1.6.2.post1-cp39-cp39-macosx_10_14_x86_64.whl.
File metadata
- Download URL: lunapi-1.6.2.post1-cp39-cp39-macosx_10_14_x86_64.whl
- Upload date:
- Size: 9.5 MB
- Tags: CPython 3.9, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b30888e386251b5c721011f0261d414a196fe0d960251487ed91905390515459
|
|
| MD5 |
34ba335ed1c13f4f1a7b73f739b0943f
|
|
| BLAKE2b-256 |
59c5866fd5f602b140a77a874ef95ecc90122f737e9be1af11eee0ca5cc23310
|