File size: 3,349 Bytes
3e8740a
 
 
 
 
 
8353697
3e8740a
 
8353697
 
8452914
 
 
3e8740a
 
2fd14d8
 
 
 
 
97e6f07
2fd14d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97e6f07
 
2fd14d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8353697
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
title: NeuroNap
emoji: πŸ§ πŸ’€
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.49.1
app_file: main.py
pinned: false
python_version: 3.12.2
license: mit
thumbnail: >-
  https://cdn-uploads.huggingface.co/production/uploads/651643e699e8060f1e8abafb/p-2ydiwoc01OvDnIz6qaO.png
short_description: EEG-based sleep analysis tool
---

# 🧠 NeuroNap

**NeuroNap** is an EEG-based sleep analysis tool that processes EEG CSV files to analyze sleep stages, generate hypnograms, visualize frequency spectra, and provide LLM-driven insights using **Gemini**.  
Built with **Python** and **Gradio**, it uses **YASA** for sleep stage classification and **HMM** for clustering and offers an interactive interface for researchers and clinicians studying sleep patterns.

---

## πŸš€ Features

- **EEG Processing:**  
  Processes raw EEG CSV files using **bandpass** and **notch filters** to remove noise and artifacts, ensuring clean, reliable signal data.

- **Sleep Stage Classification:**  
  Utilizes **YASA** (Yet Another Sleep Analysis) to automatically classify sleep stages β€” **W, N1, N2, N3, and REM** β€” and compute essential sleep metrics such as **Total Sleep Time (TST)**, **Sleep Efficiency (SE)**, and **Wake After Sleep Onset (WASO)**.

- **Clustering:**  
  Applies **Hidden Markov Models (HMM)** to cluster EEG-derived features, revealing latent patterns and transitions across sleep stages.

- **Visualizations:**  
  Generates detailed plots for **EEG signals**, **hypnograms**, **frequency spectra**, and **band-specific waveforms**, allowing for intuitive data interpretation.

- **Sleep Statistics:**  
  Displays comprehensive sleep metrics (e.g., **Total Sleep Time**, **Sleep Efficiency**) with expandable tables showing additional parameters like **Time in Bed (TIB)**, **Sleep Period Time (SPT)**, and **latencies**.

- **LLM Insights:**  
  Integrates **Gemini** via **LangChain** for natural language interaction, using a **FAISS vector store** for context-aware sleep data insights.

- **Exportable Outputs:**  
  Enables easy download of results in **CSV** format (clean EEG, extracted features, clusters) and **PDF** format (visual reports and plots).
 

---

## βš™οΈ Installation

```bash
# Clone the repository
git clone https://github.com/yourusername/NeuroNap.git
cd NeuroNap

# Create a virtual environment
python -m venv venv

# Activate the environment
source venv/bin/activate   # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt
```

πŸ”‘ **API Setup**  
Set your Gemini API key in a `.env` file:  
```bash
echo "GEMINI_API_KEY=your_api_key" > .env
```

🧩 **Knowledge Embeddings**

Before running the app, generate embeddings from your knowledge PDFs:

```bash
python rag.py
```
Place relevant research papers or notes in the knowledge/ folder before running the above command.

πŸ–₯️ **Usage**

Launch the Gradio interface:
```bash
python main.py
```
Then:

1. **Upload** an EEG CSV file (e.g., `EEGDATA.csv`).  
2. **View** sleep stage visualizations, HMM clusters, and frequency spectra.  
3. **Interact** with the **"Chat with NeuroNap"** feature to get context-aware insights.  
4. **Download** processed data and reports in **CSV** or **PDF** format.  

## πŸ“¦ Requirements

- **Python 3.9+**  
- See `requirements.txt` for the full dependency list