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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.15+ x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.14+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.14+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.14+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.14+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

lunapi-1.6.0-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.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d2d351bfe2caa4006af24e834df2d2e273195abd31d918453f0b0183291c425b
MD5 832fff6b984fcad167f19ad61659fbcf
BLAKE2b-256 364515697acc6ba050b5b15120bc7fba581845a3421a52cbdadc298f51162024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7973ef0b15cacfe8fa3cc4175a52b240b9b006c173fc46daf35da503f374ca19
MD5 00abb2593569bb45ee2859e20184ee40
BLAKE2b-256 f9d213629b20b6527794bafbe26fa4b0cb4d40e1b8b6296113fb37995ac938d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 40d73c91f0645cc239e1bac4ee7a7ac01cd15d851c0e5efe55200d4ddd9182ae
MD5 81142077da2d79bfe011766b5706f86d
BLAKE2b-256 d032f198bedb6038ae2f5c0f2193c708e50ae7c3a7441ebb889699e82fdef7f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a18c1e9581881cbef30671cf6cd2f4bbfa971312ce83997f9e07f5db5fff2991
MD5 64fffc0a5ec4a299b27b7e0c18732764
BLAKE2b-256 791dd7be9c6e3537240d915dada8866f34e37f50f9293167b97a3463ad983587

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 49e9caac7abeacac2d1aa3752081d2f0c692bddfa819aafdc69bef6e4ec3620e
MD5 a884a3627d3bd4706567e9673ebce603
BLAKE2b-256 7d3db5d94ee18e747dce3755d6c2978ce5f3889d4d2b32428b7c511370aee4d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 e308ea38e1a8e242941c3455d5c9a4107db8fc6d7a17ab999ed9b2008795250e
MD5 725990ab6f13dcc97adacd32deb49ad0
BLAKE2b-256 7a71be534abbd62a06c8c5efcd9de0b389e359176b15096ea8c3d60dae0abe94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 53e66bee590d04779be2c068fb6d18cc955cf7019508dd6f04d77c97322163e8
MD5 3bd4e9ade7e82225e96e012565b46452
BLAKE2b-256 6b599eaeacbe05f4efabc880ef92aceede71ff6c6e111f798e44be7b5d4397dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 974453219170436d1f6f7164bf244e4ce26a93ff3466dcbe9a0509c7a65800e2
MD5 498198b6caa64b9d23b3649a0d3f90a4
BLAKE2b-256 07ce6714d3e4f22342bf30a20fb0ae3450e792a5941862c022183c0bf521781e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 773a99da5201498c6da1f504861e7caca07633170f2261a0b328686909d304e6
MD5 6dc6cd08e04e1a09e1d4ebf738585bcb
BLAKE2b-256 ae71b6d25bcbaef8eb77af62d094d71b514b4ae5c5bff3d06d7e17c64cbe2e6d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 878a84b567347975864c0e520c2e47a4be48fa978290ea5eb4adc9d5ed5ee7dd
MD5 5f5d0cd1d76808754a01f8f4fed65bb4
BLAKE2b-256 b3561f0f263506481488c78b05dc97c7e40c3813952b108a5881750b3935582c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 57075933779089f0090bb8353466ceb4c490a5dd20ff04861ad51281a917e1ab
MD5 ab0f75dd3666c8e3f5b26b5669b9614e
BLAKE2b-256 54bbebc2defef958e0951d6acd46551380c0dfa948e36bcccb098a4b287b1a07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 904f29c7e2190e26aebbb33dd686562362dbf6c2a21fc70259b3d413c83fdfec
MD5 8be3940e793505dc78250cdc29ae92ca
BLAKE2b-256 0efeeaaca0e19608494719611c03ae02d0bbd0d2887cb157ff290f9024d65078

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e9084b28353626dbb5ee7a23e2a2ba4f81c8ccc6825f8199b7aa58d796412632
MD5 2002e7fd042a5c0edbab33ff4f1a9ee7
BLAKE2b-256 4bb0834a4eef582d31e4d13872e0b8dcf81222f06d3e00aedea7750e155fa545

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5fc700424ac6ffd9b6f15e478ca34ed086ee41fb9df708243ac4b74f671807e7
MD5 f960d96e790f43b483495487bb29c0dc
BLAKE2b-256 5948ad9c12e35db21c32e50ea7b011bd8e7a8eb6c7496d48ad29c061f88fb249

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 dd0fed48cad5e672314e4a25444f09784ea0feda4d3c2c732d8643c4eebe7c01
MD5 a8f5ef2bd920dd45c89af24efc7dc4e9
BLAKE2b-256 7a72841f0306784dfa045be8146acdb1c0e4bb711e9d8fc94f8e2716ffabdd30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunapi-1.6.0-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.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dcbaab46c1d430bc218426a4f601b832d9995eecbb2795ab3ec3178d5aa57864
MD5 41484ffa89e6ca5f5751b35784a7a3b5
BLAKE2b-256 8c6d9d57cc3f155a7142f6ebdd709f7e0c266d7ddb87837c61b63f26e73b3974

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 392e751b66c08e7fb77deba5eca4bab9f53de8625737577fd0dd27b259ea7b0c
MD5 ed61f4570436a83ae5a1e78cbdcd2289
BLAKE2b-256 babdd9f88f921876072c9f970d6bb7fd313b60cc3268ad025a3aa6585477ee9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lunapi-1.6.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 afc6d09d4a5d5d211fdf19f5fe2737f07a1fef65cf010ab2896ab874c671e668
MD5 0efc30d3463da718b379cb41f2ac5d77
BLAKE2b-256 295c49a2d7fce5b38bb687ca520328dda494c842cf035e3b8e8bee9da503bbae

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