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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

lunapi-1.6.2-cp314-cp314-macosx_10_15_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

lunapi-1.6.2-cp313-cp313-win_amd64.whl (10.3 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

lunapi-1.6.2-cp313-cp313-macosx_10_14_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.13macOS 10.14+ x86-64

lunapi-1.6.2-cp312-cp312-win_amd64.whl (10.3 MB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

lunapi-1.6.2-cp312-cp312-macosx_10_14_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.12macOS 10.14+ x86-64

lunapi-1.6.2-cp311-cp311-win_amd64.whl (10.3 MB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

lunapi-1.6.2-cp311-cp311-macosx_10_14_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.11macOS 10.14+ x86-64

lunapi-1.6.2-cp310-cp310-win_amd64.whl (10.3 MB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.14+ x86-64

lunapi-1.6.2-cp39-cp39-win_amd64.whl (10.3 MB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

lunapi-1.6.2-cp39-cp39-macosx_10_14_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

File details

Details for the file lunapi-1.6.2-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7580507ceee82bfc3e1c1996cc96ac1990f052fd2bcbf5a1b44824547a56dd8f
MD5 ea787ccc4339b578949725ffecd90389
BLAKE2b-256 b8746347562360a878c7216957a1c9bc6b5106e1f189a16f83fe9763ed0e0b0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ca0e1fc9e1913b8756ea057fcc2184d8950178e08a60dc12876c8fd4b587a4d7
MD5 8ee4ce5df9fac235b92f7694ccb67c74
BLAKE2b-256 76e2c32d1cc180ada208c335066e8355ec69d77da56909e8a810feececfa2e0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 cb98598682606a3425fd77746cdf0450d5c6b3f22179e949b0ed420d90e07317
MD5 5b41a722768cf68dda0f789b4435cedd
BLAKE2b-256 3493c3c2d14a9c7517a58cf48410294d2bec60e03e32a2783e4769b376a2d0b9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6ef2b09c3b4197784fdaa13b4863bfa7b83bd54049188e3da07d47caf53846fa
MD5 5eac88d15f48982086595cfd22c51dee
BLAKE2b-256 a01757cac4fc764fb352d7bb1ab2b6e26ee0cd33f2b8ab4e549c3cd758851f6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b691ea01b27de18c81045ad93557611b180d8d7a4e5d807038ebd89f3e52238c
MD5 3db3b39eba68bf916ac65b7a9dfaf7c6
BLAKE2b-256 3abc76a5b074972e6e93f92244b40b6a1e61a2131fb9a29eab3be0ae0da0ab25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4f7119adf3c8e3a4cbf0c32f19e4542b2ff59f4bad5221856f524b81422486a3
MD5 0f888830271dbdfa0ea4bb00e31d81fd
BLAKE2b-256 8feb261d0f1345024163c24459ae54e5bf31b65f9c3cd53277136f20be11d045

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4c88fd562f099e0c9e8cdcced1c495adb31f625a7faa8907601a6525b984fb9d
MD5 16f3a3982188887f02d364ae32b5b861
BLAKE2b-256 67fe5417c86bb80b8613ecb07a11fc628ca66e9577da0d886ca0f818ed7735a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9fcd1d2d5de1a9856acae27170a9530a271b6021a5e2dc37dd635e15d30c1836
MD5 f1005a5277da6899ffdca59a742ccf83
BLAKE2b-256 b1ce840107309d2f102fd69d04f091556b8935b4e74d2cf8edd46fd75ab9b60e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 beeee312dd76b69d2480c2b098cf8e720b499244c3aa9022fcdaf618d97e8b3f
MD5 fa7ec87b6301906f89ea12bf02740d06
BLAKE2b-256 1237ffccbce9de744f6231d58b5043091ad00baf907b9bfeee068526253f4028

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 acb8e81a55e1cf417467ad339f1f6789cee6f43eadb67191aafd90fc9940a0c0
MD5 21c43d0273efd22509e352e6dd6d882b
BLAKE2b-256 c3ae7c4269d0684d08f8b3d41fa1b69f7fefe4f7c02a4ae28e9722967cfef346

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc17c664460eb161dacc7c687479ac227fad78fc8e30a0e8b4624f50f135d687
MD5 aefebeb3fcd08bc7877faee986b469c2
BLAKE2b-256 e003660207627ed07ba625e277f9b50cdb404a90a30d90faa1665e746f3f520b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7269a629ef9016f6170c23a11497fe8ab29c86825e27eef66d1c9f06de38e515
MD5 6556e4a660514f222f9bfd7bdcf1966f
BLAKE2b-256 c014f2a50447b878aedb5273a5bd6907ebf215b256e2efb1e2765a15029bbe70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 40cb197e52109145d83ca57781372a534e9b736937fd05e13e8e313a78cd54d2
MD5 c9d250e6da8f15c881232cca16037809
BLAKE2b-256 e33138413ecea151175c6c94864513f4fdfecf890444db4cb38ff209d463992c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d124b52674a12b108a7a35bcc16b89b68572e21829afdced6b1706d9d612ef5c
MD5 5b431f31ffd3016781d6de00af1e6af2
BLAKE2b-256 7c5df11e50c0f6d10ada9ab4bd62caff7f0cc15260938099cf1153fbf4cb88fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 003bc97e91840254cb9963b427b785487a643f253c5546c66eef1f0003610f0e
MD5 313e1173abb94233db1aa597ad61d64a
BLAKE2b-256 612f912399e274402210d2f90b0391b17cbb21d01800af28c84d06edfa915a91

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.2-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

Hashes for lunapi-1.6.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 221c2e18158c9c8fa5c3f5fe624eb50dfd38b332ae6f3be4dc22a903dfaa7b08
MD5 391753e99842a95b11ccd26099f2028b
BLAKE2b-256 105e9ab0eac14052ad0e2d0df043dba6b69c9801a86c8f759628097206cb79c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 96ce31e1f6bd2db7cf2cabfadf01bcc71ab4b3cba1166ad2131c94749cbd9608
MD5 0cf1024c0cc96f8150e7693c47f0b448
BLAKE2b-256 4ba0fd7e6e2ee16abcb0caf7e77233c91b0b3f705786da5843c432df7c40e0c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.2-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 f2e2fb06710fa5122211705fb9753455e7feef57e21d510a510010669a820ad5
MD5 988636a6a7988bb7f84b11d871d96c92
BLAKE2b-256 6a9dafe87fa08a699ef601f43dc32c37987d7973b84cdc586cbfa12c75758bf8

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