Metadata-Version: 2.1
Name: misleep
Version: 0.3.2
Summary: MiSleep: Mice sleep EEG/EMG visualization, scoring, and analysis
Author-email: Xueqiang Wang <swang9194@gmail.com>
Maintainer-email: Xueqiang Wang <swang9194@gmail.com>
License: BSD 3-Clause License
        
        Copyright (c) 2023, Xueqiang Wang
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Project-URL: Homepage, https://github.com/BryanWang0702/MiSleep
Project-URL: Repository, https://github.com/BryanWang0702/MiSleep
Project-URL: Documentation, https://bryanwang.cn/MiSleep/
Project-URL: Bug Tracker, https://github.com/BryanWang0702/MiSleep/issues
Keywords: sleep,EEG,EMG,mice,neuroscience,scoring,hypnogram,spectrogram,signal-processing,pyside6
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.20
Requires-Dist: scipy>=1.8
Requires-Dist: matplotlib>=3.5
Requires-Dist: pandas>=1.4
Requires-Dist: pyedflib>=0.1.30
Requires-Dist: mat73>=0.60
Requires-Dist: openpyxl>=3.0
Requires-Dist: joblib>=1.1
Requires-Dist: scikit-learn>=1.0
Requires-Dist: PySide6<6.6,>=6.4; python_version < "3.9"
Requires-Dist: PySide6>=6.6; python_version >= "3.9"
Provides-Extra: analysis
Requires-Dist: lightgbm>=3.3; extra == "analysis"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Provides-Extra: full
Requires-Dist: lightgbm>=3.3; extra == "full"
Provides-Extra: gui
Provides-Extra: transformer
Requires-Dist: torch>=2.0; extra == "transformer"

# MiSleep

**MiSleep** is an open-source Python toolbox for **mice sleep EEG/EMG**
visualization, scoring, and analysis.

![logo](src/misleep/gui/resources/entire_logo.png)

The name *MiSleep* comes from "**Mi**ce **Sleep**" and sounds like
"**my sleep**".

| | |
|---|---|
| License | BSD 3-Clause |
| Python | 3.9 – 3.14 |
| GUI toolkit | PySide6 (Qt6) — Windows, macOS, Linux |
| Documentation | [https://bryanwang.cn/MiSleep/](https://bryanwang.cn/MiSleep/) |

---

## Features

- **Cross-platform desktop GUI** built on **PySide6** (Qt6). Works on
  Windows, macOS and Linux without any platform-specific code.
- **Flexible data input** — load MATLAB, EDF/EDF+, BDF, NumPy NPY/NPZ,
  CSV and TSV recordings; JSON/CSV/TSV annotation imports are also built in.
- **Full scoring workflow** — per-second sleep state scoring (NREM / REM /
  Wake / Init), single time-point markers, start-end events, hypnogram,
  spectrogram and per-state spectral analysis.
- **Event detection** — slow-wave activity (SWA) and sleep spindle
  detection with state-specific thresholds.
- **Automatic sleep staging** — a LightGBM classifier (trained on
  time- and frequency-domain features) and an optional PyTorch
  causal-transformer model.
- **Analysis export** — per-hour and 12 h light/dark phase sleep
  statistics exported to Excel.
- **Clean, modular architecture** — data, I/O, preprocessing, analysis,
  visualization and GUI are separate modules with a stable public API and
  plugin-friendly extension points (see the
  [developer guide](docs/developer_guide.md)).

## Installation

```bash
# Base install - includes everything (data, I/O, preprocessing, analysis,
# visualization AND the PySide6 GUI)
pip install misleep

# With the LightGBM auto-staging model as well
pip install "misleep[full]"

# For development / contributing
git clone https://github.com/BryanWang0702/MiSleep.git
cd misleepv3
pip install -e ".[analysis,dev]"
```

> **Note on PyTorch:** the causal-transformer auto-staging model requires
> `torch`, which is not available on every platform. Install it
> separately with `pip install "misleep[transformer]"`.

## Quick start

### Launch the GUI

```bash
python -m misleep
# or, after installation:
misleep
```

### Open files directly (command line)

```bash
misleep data.mat                # open a recording
misleep data.mat anno.txt       # open a recording + its annotation
misleep --data data.edf --anno anno.txt
python -m misleep data.mat      # same via the module
```

### Open files by double-clicking (Windows)

Register MiSleep as the handler for your recording files:

```bash
python tools/install_file_associations.py
```

After that, **double-clicking a `.mat` or `.edf` file opens it in MiSleep**
(no console window). Annotation `.txt` files get an **"Open with MiSleep"**
right-click menu item without changing their default handler. The previous
`.mat`/`.edf` handlers are backed up automatically and restored with:

```bash
python tools/install_file_associations.py --uninstall
```

On macOS/Linux, run the same script for instructions (or open files from
the command line as shown above).

### Use the library

```python
import misleep as ms

# Load a recording
midata = ms.load_signal("data.npz")     # MAT, EDF/BDF, NPY/NPZ, CSV or TSV

# Inspect it
print(midata.channels, midata.sf, midata.duration)

# Preprocess
midata.filter(chans=["EEG"], btype="bandpass", low=0.5, high=30)
nrem, rem, wake, init = ms.crop_state_data(midata, mianno)

# Analyze
freq, psd = ms.spectrum(midata.signals[0], midata.sf[0])
swa = ms.SWA_detection(midata.signals[0], midata.sf[0], df=True)

# Plot
fig, ax = ms.plot_hypno(mianno.sleep_state)
```

See [docs/getting_started.md](docs/getting_started.md) and the
[examples](examples/) folder for more.

## Project layout

```
misleepv3/
├── pyproject.toml            # modern packaging (PEP 621)
├── src/misleep/
│   ├── data/                 # data model: MiData, MiAnnotation
│   ├── io/                   # input/output: MAT, EDF, annotations (+ plugin registry)
│   ├── preprocessing/        # filtering, artifact rejection, spectral analysis
│   ├── analysis/             # detection, feature extraction, auto staging
│   ├── viz/                  # matplotlib plotting (signals, spectra, hypnograms)
│   ├── gui/                  # PySide6 desktop application
│   ├── utils/                # shared helpers
│   ├── config.py             # INI configuration handling
│   └── logger.py             # logging setup
├── tests/                    # pytest suite
├── docs/                     # user + developer documentation
├── examples/                 # runnable examples
└── tools/                    # UI/resource compilation scripts
```

## Documentation

- [Getting started](docs/getting_started.md) — installation and first steps
- [User guide](docs/user_guide.md) — the GUI in detail
- [Data formats](docs/data_formats.md) — how data and annotations are stored
- [Configuration](docs/config_file.md) — the `config.ini` reference
- [API reference](docs/api_reference.md) — full API documentation
- [Developer guide](docs/developer_guide.md) — architecture and how to extend MiSleep
- [Changelog](CHANGELOG.md)

## Citing MiSleep

If you use this software in your research, please cite it as:

> Xueqiang Wang. (2024). BryanWang0702/MiSleep. Zenodo.
> https://doi.org/10.5281/zenodo.14511905

## License

BSD 3-Clause. See [LICENSE](LICENSE).
