Metadata-Version: 2.4
Name: sleepscienceviewer
Version: 0.3.0
Summary: A Python-native EDF and XML annotation viewer for sleep science
Author-email: "Dennis A. Dean, II" <dennis.a.dean@gmail.com>
License-Expression: AGPL-3.0-or-later
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: asttokens>=3.0.0
Requires-Dist: attrs>=25.3.0
Requires-Dist: backcall>=0.2.0
Requires-Dist: beautifulsoup4>=4.13.0
Requires-Dist: bleach>=6.2.0
Requires-Dist: certifi>=2025.8.0
Requires-Dist: cffi>=2.0.0
Requires-Dist: charset-normalizer>=3.4.0
Requires-Dist: colorcet>=3.1.0
Requires-Dist: contourpy>=1.3.0
Requires-Dist: cryptography>=46.0.0
Requires-Dist: cycler>=0.12.0
Requires-Dist: decorator>=5.2.0
Requires-Dist: defusedxml>=0.7.0
Requires-Dist: docopt>=0.6.0
Requires-Dist: et_xmlfile>=2.0.0
Requires-Dist: executing>=2.2.0
Requires-Dist: fastjsonschema>=2.21.0
Requires-Dist: fonttools>=4.59.0
Requires-Dist: gatspy>=0.3
Requires-Dist: idna>=3.10
Requires-Dist: ipython>=9.5.0
Requires-Dist: ipython_pygments_lexers>=1.1.0
Requires-Dist: jaraco.classes>=3.4.0
Requires-Dist: jaraco.context>=6.0.0
Requires-Dist: jaraco.functools>=4.3.0
Requires-Dist: jedi>=0.19.2
Requires-Dist: jeepney>=0.9.0
Requires-Dist: joblib>=1.5.0
Requires-Dist: jsonschema>=4.25.0
Requires-Dist: jsonschema-specifications>=2025.9.0
Requires-Dist: keyring>=25.6.0
Requires-Dist: kiwisolver>=1.4.0
Requires-Dist: lxml>=6.0.0
Requires-Dist: MarkupSafe>=3.0.0
Requires-Dist: matplotlib<4.0.0,>=3.10.0
Requires-Dist: matplotlib-inline>=0.1.0
Requires-Dist: mdurl>=0.1.0
Requires-Dist: more-itertools>=10.8.0
Requires-Dist: mpmath>=1.3.0
Requires-Dist: nbclient>=0.10.0
Requires-Dist: nbconvert>=7.16.0
Requires-Dist: nbformat>=5.10.0
Requires-Dist: nh3>=0.3.0
Requires-Dist: numpy<3.0.0,>=2.3.0
Requires-Dist: openpyxl>=3.1.0
Requires-Dist: packaging>=25.0
Requires-Dist: pandas<3.0.0,>=2.3.0
Requires-Dist: pandas-stubs>=2.3.0
Requires-Dist: pandocfilters>=1.5.0
Requires-Dist: parso>=0.8.0
Requires-Dist: pathlib2>=2.3.7.post1
Requires-Dist: pexpect>=4.9.0
Requires-Dist: pickleshare>=0.7.0
Requires-Dist: pillow>=11.3.0
Requires-Dist: platformdirs>=4.4.0
Requires-Dist: prompt_toolkit>=3.0.0
Requires-Dist: ptyprocess>=0.7.0
Requires-Dist: pure_eval>=0.2.0
Requires-Dist: py-ecg-detectors>=1.3.0
Requires-Dist: pycparser>=2.23
Requires-Dist: Pygments>=2.19.2
Requires-Dist: pyparsing>=3.2.4
Requires-Dist: pyproject_hooks>=1.2.0
Requires-Dist: PyQt6_sip>=13.10.2
Requires-Dist: pyrsdameraulevenshtein>=1.1.0
Requires-Dist: PySide6<7.0.0,>=6.9.0
Requires-Dist: PySide6_Addons<7.0.0,>=6.9.0
Requires-Dist: PySide6_Essentials<7.0.0,>=6.9.0
Requires-Dist: python-dateutil>=2.9.0.post0
Requires-Dist: pytz>=2025.2
Requires-Dist: PyWavelets>=1.9.0
Requires-Dist: pyzmq>=27.1.0
Requires-Dist: referencing>=0.36.2
Requires-Dist: requests<3.0.0,>=2.32.0
Requires-Dist: requests-toolbelt>=1.0.0
Requires-Dist: rfc3986>=2.0.0
Requires-Dist: rich>=14.1.0
Requires-Dist: rpds-py>=0.27.1
Requires-Dist: scipy<2.0.0,>=1.16.2
Requires-Dist: SecretStorage>=3.4.0
Requires-Dist: shiboken6>=6.9.2
Requires-Dist: six>=1.17.0
Requires-Dist: soupsieve>=2.8
Requires-Dist: stack-data>=0.6.3
Requires-Dist: sympy>=1.14.0
Requires-Dist: tinycss2>=1.4.0
Requires-Dist: tornado>=6.5.2
Requires-Dist: traitlets>=5.14.3
Requires-Dist: types-pytz>=2025.2.0
Requires-Dist: typing_extensions>=4.15.0
Requires-Dist: tzdata>=2025.2
Requires-Dist: urllib3>=2.5.0
Requires-Dist: wcwidth>=0.2.0
Requires-Dist: webencodings>=0.5.0

# Sleep Science Viewer

A Python-native EDF file and XML annotation viewer.

## Description

SleepScienceViewer is a Python-native application for visualizing and analyzing sleep data stored in EDF (European Data Format) and corresponding annotation files (XML). Designed with sleep science workflows in mind, the tool enables efficient review of signals and sleep stages through a responsive and customizable GUI.

SleepScienceViewer uses a class-based architecture to [represent EDF](Media/Docs/EDF_File_Class.md) and [annotation files](Media/Docs/Annotation_XML_Class.md). These classes can also be used independently to access specific information from the files, supporting flexible review and analysis within notebooks or other Python programs.

![SleepScienceViewer](Media/SleepScienceViewer.png)
*Figure 1. Sleep Science Viewer interface with signals, hypnogram, spectrogram, and annotations.*

## Key Features

* **EDF & Annotation Support**

  * Load EDF files with associated XML annotation files
  * Visualize up to 10 simultaneous signals
  * View and interact with a full hypnogram display (see Figure 1)

* **Annotation Interaction**

  * Filter listed annotations by type
  * Hypnogram-aligned annotation plot with [automatically assigned colors](Media/annotation_legend.png)
  * Annotation combo box directly linked to annotation plot and list for synchronized selection (see Figure 1)

* **Custom Display Options**

  * Change epoch duration for signal navigation
  * Toggle visibility of hypnogram, spectrogram, and annotation plots (see Figure 3 for signal-only mode)
  * Switch hypnogram rendering between line trace and background-colored stage rectangles
  * Generate multi-taper spectrograms for selected signals; if not feasible (e.g., low sampling frequency), display a compact heatmap instead
  * Customize signal colors via color picker; choices persist across pan/zoom events
  * Legends automatically update with user-defined signal colors

* **Report Generation & Export Tools**

  * Generate [EDF summary reports](Media/edf_summary.png)
  * Export individual [signals to folder](Media/signal_export.png)(s) for downstream use
  * Export annotation data including:

    * A [full annotation listing](Media/sleep_event_export.png)
    * [Sleep stage timeline](Media/sleep_stages.png)
    * [Summary reports](Media/sleep_event_summary.png) for review and documentation

* **Interface**

  * Show/hide hypnogram, spectrogram, and annotation panels from the main menu (see Figures 1 and 3)

* **Navigation**

  * Double-click on hypnogram, spectrogram, or annotation plots to move to the selected epoch
  * Double-click on annotation list entries to jump to annotation start times

![Signal Viewer Interface](Media/signal_viewer_beta.png)
*Figure 2. Signal Viewer interface displaying a single channel with epochs and overlays.*

* **Signal Viewer**

  * Signals

    * View a single signal as a raster plot with 15 epochs displayed vertically (see Figure 2)
    * Sleep stages shown as background rectangles behind the signal trace
    * X-axis moved to the bottom of the plot for improved readability
  * Interface/Plotting

    * Toggle hypnogram, spectrogram, and annotation overlays similar to the main viewer
    * Signal-only mode available (see Figure 4)
  * Processing & Analysis

    * Compute spectrograms (or default to heatmap if sampling frequency is insufficient)
    * Apply common Band Pass and Notch filters on demand
  * Considering (future work)

    * Displaying annotations directly on signal plots
    * Support for marking epochs in-progress

<p align="center">    
<img src="Media/SleepScienceViewer_signals_only.png" width="600" /><br>
*Figure 3. Sleep Science Viewer with hypnogram, spectrogram, and annotations hidden (signal-only mode).*
</p>

<p align="center">    
<img src="Media/signal_viewer_beta_signals_only.png" width="600" /><br>
*Figure 4. Signal Viewer in signal-only mode with hypnogram, spectrogram, and annotations hidden.*
</p>

## Workarounds

Double-click navigation is implemented but may be unstable due to limitations in how matplotlib interacts with PySide6. Framework code is in place, but full functionality is not guaranteed. Known workarounds:

* Avoid frequent switching between the Sleep Science Viewer and Signal Viewer windows.
* Redrawing figures within a window or reloading data often restores expected behavior.

## Getting Started

The Sleep Science Viewer requires an EDF and Annotation file. We used files downloaded from the [National Sleep Research Resource](https://sleepdata.org/) tutorial to develop the interface.

We recommend using a virtual environment when running the Sleep Science Viewer.

## Intended Use

Ideal for researchers, clinicians, and developers working in sleep research, human performance, or bio-signal analysis. The interface and tools are designed to streamline review and reporting workflows for sleep study data.

## Dependencies

This application was developed in Python 3.12, with the GUI built using PySide6. Please refer to [requirements.txt](requirements.txt) for a complete list of required dependencies.

## Download Test Data

We tested the SleepScienceViewer with data from [here](https://zzz.bwh.harvard.edu/luna/tut/tut1/).

## Installing

**1. Install pipx**

```
python -m pip install --user pipx
python -m pipx ensurepath
```

**2. Download install package**

```
git clone https://github.com/DennisDean/SleepScienceViewer4.git
cd SleepScienceViewer4/dist
```

**3. Install SleepScience Viewer**

```
pipx install sleepscienceviewer-0.1.0-py3-none-any.whl
```

## Running the Application

To launch the application:

```
SleepScienceViewer
```

## Help

Help documentation will be added as questions are received and common usage scenarios emerge.
For questions or feedback, feel free to reach out to the author listed below.

## Authors

**Dennis A. Dean, II, PhD**
[dennis.a.dean@gmail.com](mailto:dennis.a.dean@gmail.com)

## Version History

* v0.1.1

  * Added display toggles for hypnogram, spectrogram, and annotation panels
  * Hypnogram background color rendering for sleep stages
  * Annotation synchronization between combo box, plot, and list
  * Spectrogram-to-heatmap fallback for low sampling rates
  * Signal color customization with legends
  * Added Signal Viewer with raster view of single-channel data

* v0.1

  * First functioning release

## License

This project is licensed under the **GNU Affero General Public License v3.0 License**.
See the [LICENSE.md](LICENSE.md) file for details.

## Acknowledgments

This project builds on work originally developed during my time at **Brigham and Women's Hospital**, where a similar class structure was used in earlier internal tools.

The original **MATLAB version** of this tool was shaped by valuable community feedback received following its public release on **MATLAB Central**.

It also benefited from code developed at **Case Western Reserve University**.

Special thanks to the authors of the [multitaper_spectrogram_python.py](https://github.com/preraulab/multitaper_toolbox/blob/master/python/multitaper_spectrogram_python.py) module, which was refactored for this application to support multi-taper spectrogram visualization. More information on the multi-taper method can be found on the [Prerau Lab website](https://prerau.bwh.harvard.edu/multitaper/).
