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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.15+ x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.14+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.14+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.14+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.14+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

lunapi-1.6.3-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.3-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8bc5be6325bd2618812f9abba625849b4b7139ccfa09f4372e1ebdeccb76d6c3
MD5 af4e148aecfb095d100e44551cf6a256
BLAKE2b-256 df6c54aa4c3d7fc2aa29302d1bbbfca64fe55b8cc6319151cf27cbd14439b1f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7fa279f3ba12f274f70549a58867a16e204cb1d7e974d364e7a467f68a1d4666
MD5 dd55d0accf1e0574e5f13184ee366678
BLAKE2b-256 54c42c51a080926b5661c3eb70731b033bffc44d134828b6c7f172451a87e940

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 fffd4fdf992dabf95c23ffb53cb0addae9a6aadc26a9084115b43d8fd377a228
MD5 208511f9ad6e65ed8f8176ac3ffc3142
BLAKE2b-256 479003e514a9e680cda2824c046120eeeda731c61a50719368a6c5973553597f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 157e60bed6f311b1154aa144f5f6f99410866b7c22caee5fb0767fe1e94dda74
MD5 e2a6221877d122d2eeb9cb8e02e39fe7
BLAKE2b-256 76124cc66c94591ea0f5c63567433762e66a85d81010734d9bf1b2dafcdb1e3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 31416f853a5f556ecd5a2fbfef10eb63575e78140fcf772fda5716c1c0c31921
MD5 ef0300e2375eb31ecbd626003384badc
BLAKE2b-256 9ada78832d5e2d2293a0cfe02b5014a85fb27528d141a6b5ef5a294fe52b3a16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c340ba92f0da6fb12776cb3f2166d83be4902260d5af7b0d5f3fe2537a1d2588
MD5 ad4d8fa33215a8ef1c9beb25a45e8ce3
BLAKE2b-256 5f68758e678b298c7cf720cd3b2968cb99347768f9c7d63b88f96fd58911a3e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f1f6257daeda000cd7f20365b83aa93bd7de20404f6574885af9e86e2dd7f924
MD5 723153e87492066928a734fdd4663d4e
BLAKE2b-256 53e21584218318bf6ec465a9a7c5f956910648a471ea1c1756ece9b21950ee55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ad2c8d73fb081a34ef959f2594600c13c7e1544bb5706521072677b7770b5137
MD5 0cb98614a3c48d2372557609dcecf162
BLAKE2b-256 70c55040908a5f22e222a94664f9874d3c3519fd83c2bd1fa950350c0878a828

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0794041f87ebca1222d823894f087b5d79a238c758d1d4ce949b9f651463a146
MD5 4288c5b72e9ef943c3ec15f88ee2a2b8
BLAKE2b-256 c5f01f6b3bac237dab3dea2cb1846ad20585991a3e2cdb3a6a40ac3301ab6af6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e539c86f19dba08beb46eecc93c25281ee4e147e3d66c017d2c40fc309b7a52b
MD5 0173a9ef3404617a2f08b5cad5e09c7f
BLAKE2b-256 4deb437ecd108162230d49c23fe80400100f912f70d6d9877fa41028f312acaf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dce7dfb9a14937801544f986285b854024541cca16c6e22aadb4b6cf7e4a0e53
MD5 f8572148c7d461b0b1f781eb0f9f8635
BLAKE2b-256 3e74391f6829495ef293681cba7bb92bd2d2589cc77a65ee536188ac45b928b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8c4f8b386fc5a85f8f8f1d4668115263588b69bb836517dc68943303ed634a2a
MD5 63166421d8b1825561924d7767bf7968
BLAKE2b-256 df660ac24bf87d81259e97d87a586a09040a8cdcb0ed7b146b9a05c8b0fc8bd3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 acae2a1a4edc801fb07084a0eb967a6ba6e52d5641114367998cbf6c5506f625
MD5 4c34ec21a41ca2de70eea6ee8d4ce994
BLAKE2b-256 5ad9f4b42f935efafa2f4b571641f7fd5da63af27f704951404847619e2071af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37ad6bd7e4b9112c0957e9c72e7bb7710b8474859e2a1ae296b6897d125195a4
MD5 d3702a9953066dc19b9538b1f024b730
BLAKE2b-256 b6481ed29a6396131b42808748d761eccacf5103ef498a2f0c89bd9a09a49ec7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 eabb07147d78678494bd522b319746a79f5baf02a7b14cf9b7cc9da13be2ef15
MD5 5bdaca548ca00856888cd2950006cc4c
BLAKE2b-256 69ccebc014d4094c7d62da29b3c4b5c618cc0db2a201294834a91cce127a76e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.3-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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 22dfd5e5d536b2f278dcafe449ec3bd495c91dfa9231e5d97c5663706c6f2a17
MD5 6943a4681a4a57a3a0d5233572aa9c31
BLAKE2b-256 a3d04580c085dc304f651dd805e421cb020775efa0a3986b7f02f4f28797f86f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc31c53bd6dce63f72605c45aaffc18272436960bf0de5c57b8501265c1a2b95
MD5 af3a49681f3031f2fc22304c535ade53
BLAKE2b-256 e1c6f9ede436c65acaece4dbc6eabc1a3b9605ab5406b858868b6d5fbb460913

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.3-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9f8f3e615a2ed3e0b873e3820e90201ab2f659894903ba2652c013f38ec17d7a
MD5 7a7e9fd18a22e4235f1769e366fc9ab5
BLAKE2b-256 ee4aced0d5dc8a2864a28ee9a6a6285f94003ecc5e082f152990cf70816efd50

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