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.4-cp314-cp314-win_amd64.whl (11.7 MB view details)

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.15+ x86-64

lunapi-1.6.4-cp313-cp313-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.14+ x86-64

lunapi-1.6.4-cp312-cp312-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.14+ x86-64

lunapi-1.6.4-cp311-cp311-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.14+ x86-64

lunapi-1.6.4-cp310-cp310-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.14+ x86-64

lunapi-1.6.4-cp39-cp39-win_amd64.whl (11.5 MB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

lunapi-1.6.4-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.4-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 11.7 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

Hashes for lunapi-1.6.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 2772a8cf83853dbed6ac93a97389f736a0129bdc1dca015d55c8366d0d3321d2
MD5 53a0c17af3927e1f97aa65dcd7f71a2f
BLAKE2b-256 30cf19da44b13ceef791cba34ee6ce1977aa55648e51b218bab9b6b9f93d1264

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c18c35fe945a0c39f011fc9a72a429b6ff2ee149ad9f41c66a70b27c343d1b5c
MD5 ff76e13956e243ea7a2181d74567b245
BLAKE2b-256 0fea0b6fc907292c8961e018c04fd298a5ff11711c906ab3c9d05f270fe48662

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6124f8c35440a1674054cf53d86ef77ba32df1373f9e385c578f3d778e448d4d
MD5 17e936e1b8985c544910661ce79095eb
BLAKE2b-256 4f856591311d4ce16b747a7ece7470c956c6078811a5c737a95fe6a09538a115

See more details on using hashes here.

File details

Details for the file lunapi-1.6.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 11.5 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

Hashes for lunapi-1.6.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9a5e2138d9f044c209dd79be628325a88f75a41233d2124e6d3222dad229a182
MD5 35521a35342d56e01537e8a4757ade04
BLAKE2b-256 fb7358a1e6acc69edbfebfd96049b223316aa7c9ac94b05ddf214ca0e36366d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 06da741c7d9e2af410aa219b319af758a22cb43376ad338704196c3f02a26377
MD5 d3854b89603b749e74f13052e6984b69
BLAKE2b-256 b825ceca10bf8cfe22fc657cf9082cd8fc7b24d8cb6a9db6c4cad338531321fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 17ec5c3b2363408bf1605e471c1a9ac1c7cff7e3917689b9abfb52b1affb0e17
MD5 bd7735a09a9e887c1a98b301816c9e76
BLAKE2b-256 832a794282a10b10e74b0f675f4d72cd207567aa73354ddab4968855f6a50798

See more details on using hashes here.

File details

Details for the file lunapi-1.6.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 11.5 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

Hashes for lunapi-1.6.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e88f29abaa1d23a0d344e22326db76101338bdadd4406fdf240676da469f6392
MD5 c2ce1eae4a6b161a0e78a9da0c042551
BLAKE2b-256 c6fdc9aa7c1b4e92038b9fdaa04eb0065fcb02655dd41cff27ea1ecf0ea39cd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f2246b22c3ce2a3eedc5ad5df59f8a60de1d20945fd8b7d78318dc8d039cb8af
MD5 47562cb4b090b96ff06b9ab3e784af08
BLAKE2b-256 92b6c595ba1768fd7910ebaf1c8f82474c67c522215a44988be52526c3ed544f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2544dde880266cd9a41322a8fd6e1ad764b1f3f9f926ef39b7f0133354975857
MD5 badc87c1580fd376fead5ad84b65f536
BLAKE2b-256 29132b4e88f366ffaa16cf277ff16ec39d14a64e0a00e617e19b0d043df46981

See more details on using hashes here.

File details

Details for the file lunapi-1.6.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 11.5 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

Hashes for lunapi-1.6.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f2a9cc0ac366e1191e3234443d183b438a66a26a66b9f9fe5362f616d17e8da5
MD5 566368e17e7ca2c10b8314e5a015fa52
BLAKE2b-256 ffba0bc52eec1f07da64fcc47b72f41fa7f54c257eb45231f88a55f4bd38722e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19d6bf11c33cfe7e57a634e78ae06b636755544ed0358655b61e6521a4d6c6b4
MD5 8fc52ab64710b7f911e3e4c6b524b856
BLAKE2b-256 d7837be196e3fe908f7c2d7af94e7f538591e1218720bfd1cf77dd711bdba3f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8b0a3389744d0fa5f2d0d22e6d21a4a266ab26150676cd203e8f20d876d38766
MD5 7b8b9211a4c90f4fc51a79ddd3a8845c
BLAKE2b-256 9ce6dbd8d87e4746790d6b45acb13d6100aba405615925481a1c84922cee460e

See more details on using hashes here.

File details

Details for the file lunapi-1.6.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 11.5 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

Hashes for lunapi-1.6.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0bc0bc1a7429c91de0c2d49baac7e2da2de09e33cbc690bee94346c3dae0b8fe
MD5 deb741d77b6080eff6873e244b61b90d
BLAKE2b-256 85d427a11ebc49706af12d41cb19d671d62b593732205c1fbe1cdccea5bab33a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 99ad4f8e38066c69575fd7f4a60bf5be97676e1c1654d7d10e006feab265b03f
MD5 32fb60cd8b7f099b72d48eba1185ae81
BLAKE2b-256 cda0f07460c26dcdcc88cb52ec4aef0bc976fb4cc6d215b3ac3719d64bb7280d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 28d768d5ea57dc6501dc9fda08c62599b67808249dae94761d899b77a971b6ac
MD5 1b05b454eb35d8bcb1fa76b70ae6bfab
BLAKE2b-256 0477f6be90e6c74d9d2b258eb3fc88bcda34541cbfeb6bad3681b058fb0c00bf

See more details on using hashes here.

File details

Details for the file lunapi-1.6.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 11.5 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

Hashes for lunapi-1.6.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c430ae8ec3c9fee28ecd2f5494326bfa063a2d5346c0bf2d76f8f64dd8fa3e70
MD5 c3cff484ae1e53d1c79c7a111aca6e9e
BLAKE2b-256 2de5a8752dfb74d33ac6bc9cb6ffa092c42df50cf21681a4ac9664a977974c06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b196c69f164201449cbb82193ba7c044728e6e737f81a948b97042214b18c9b6
MD5 63e1df9a1c1a6135352655629881f459
BLAKE2b-256 cf7b2c87b3ff12f752c0059e7fcb0ba5daf48398d73ae62c4adb652b0a57e529

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.4-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 834a939c2e0bd9c95c3c6e625742384c72394d9dff49a9385359ffbd93315b62
MD5 1d98e168d4ef54cb88614268c1d154d3
BLAKE2b-256 3077c68f7ea57093d7bd7bbf52ca1a151518de1c190be9036b4bc889d6384954

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