Skip to content

Commit a32e11c

Browse files
authored
Merge pull request #1 from lina-usc/dev
Merge Dev with main
2 parents 235fc66 + f6cb455 commit a32e11c

29 files changed

+1093
-1288
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/__pycache__

README.md

Lines changed: 205 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,207 @@
1-
# contourusv
2-
This repository contains the source code for ContourUSV detector. The code can be tested with the open source USVSEG dataset.
1+
# ContourUSV: Ultrasonic Vocalization Detection Pipeline
32

4-
# Installation
3+
![Python Version](https://img.shields.io/badge/python-%3E%3D3.9-blue.svg)
4+
![License](https://img.shields.io/badge/license-MIT-green.svg)
55

6-
Python 3.9 or above required.
6+
ContourUSV is an automated pipeline for detecting ultrasonic vocalizations (USVs) in audio recordings. The system uses spectrogram analysis combined with advanced image processing techniques to identify and classify 22kHz and 50kHz USVs.
7+
8+
<!-- ## Features
9+
10+
- Audio preprocessing with bandpass filtering and normalization
11+
- Spectrogram generation with customizable parameters
12+
- Advanced image processing for noise reduction:
13+
- Median filtering
14+
- Otsu's thresholding
15+
- Contrast Limited Adaptive Histogram Equalization (CLAHE)
16+
- Morphological operations
17+
- Contour-based USV detection
18+
- Annotation generation from multiple file formats (HTML, Excel, CSV)
19+
- Comprehensive evaluation metrics:
20+
- Precision, Recall, F1 Score, Specificity
21+
- Carbon emissions tracking via CodeCarbon
22+
- Parallel processing support -->
23+
24+
## Installation
25+
26+
1. **Clone the repository:**
27+
```bash
28+
git clone https://github.com/yourusername/contourusv.git
29+
cd contourusv
30+
```
31+
32+
2. **Create and activate virtual environment:**
33+
```bash
34+
python -m venv venv
35+
source venv/bin/activate # Linux/MacOS
36+
venv\Scripts\activate # Windows
37+
```
38+
39+
3. **Install dependencies:**
40+
```bash
41+
pip install -e .
42+
```
43+
44+
## Data Directory Structure
45+
46+
Organize your input data using the following structure:
47+
48+
```
49+
root_path/ # Passed via --root_path argument
50+
├── EXPERIMENT_NAME/ # Passed via --experiment argument
51+
│ └── TRIAL_NAME/ # Passed via --trial argument
52+
│ ├── *.wav # Audio recordings
53+
│ ├── *.WAV # (Alternative capitalization)
54+
│ ├── *.html # HTML annotations
55+
│ ├── *.xlsx # Excel annotations
56+
│ └── *.csv # CSV annotations
57+
```
58+
59+
**Example Concrete Structure:**
60+
```
61+
/Users/username/data/
62+
└── PTSD16/
63+
└── ACQ/
64+
├── rat12_day1.wav
65+
├── rat12_day1.html
66+
├── rat13_day1.WAV
67+
└── rat13_day1.html
68+
```
69+
70+
**File Requirements:**
71+
- Audio files: Must have `.wav` or `.WAV` extension
72+
- Annotation files: Must match audio filenames and reside in same directory
73+
- Supported annotation formats:
74+
- HTML
75+
- Excel
76+
- CSV
77+
78+
## Output Structure
79+
80+
```
81+
output/
82+
├── EXPERIMENT_NAME/
83+
│ ├── TRIAL_NAME/
84+
│ │ ├── contour_detections/
85+
│ │ │ └── *.csv (detection annotations)
86+
│ │ ├── evaluation_results/
87+
│ │ │ └── Evaluation_*.csv (performance metrics)
88+
│ │ └── spectrograms/
89+
│ │ └── *.png (annotated spectrograms)
90+
│ └── ground_truth_annotations/
91+
│ └── *.csv (processed ground truth)
92+
```
93+
94+
## Usage
95+
96+
In the `src` directory execute the following command to run the detection pipeline.
97+
98+
### Basic Command
99+
```bash
100+
python main.py \
101+
--root_path /path/to/your/data \
102+
--experiment EXPERIMENT_NAME \
103+
--trial TRIAL_NAME \
104+
--file_ext ANNOTATION_FILE_EXT
105+
```
106+
107+
### Example Command
108+
```bash
109+
python main.py \
110+
--root_path /Users/username/data \
111+
--experiment PTSD16 \
112+
--trial ACQ \
113+
--file_ext .html
114+
```
115+
116+
### Required Arguments
117+
| Argument | Description | Example |
118+
|---------------|-------------------------------------------|------------------|
119+
| `--root_path` | Root directory containing experiment data | `/data/studies` |
120+
| `--experiment`| Name of the experiment | `PTSD16` |
121+
| `--trial` | Name of the trial/condition | `ACQ` |
122+
| `--file_ext` | Annotation file extension (`.html`, `.xlsx`, `.csv`) | `.html` |
123+
124+
### Optional Parameters
125+
| Parameter | Default | Description |
126+
|----------------|---------|------------------------------------------|
127+
| `--overlap` | 3 | Overlap duration between windows (seconds) |
128+
| `--winlen` | 10 | Window length for processing (seconds) |
129+
| `--freq_min` | 15 | Minimum frequency for detection (kHz) |
130+
| `--freq_max` | 115 | Maximum frequency for detection (kHz) |
131+
| `--wsize` | 2500 | Window size for processing |
132+
| `--th_perc` | 95 | Percentile threshold for noise reduction |
133+
134+
<!-- ## Pipeline Architecture
135+
136+
1. **Preprocessing**
137+
- Audio normalization and filtering
138+
- Spectrogram generation
139+
- Noise reduction using:
140+
- Median filtering
141+
- Otsu's thresholding
142+
- CLAHE contrast enhancement
143+
144+
2. **Detection**
145+
- Contour detection using OpenCV
146+
- USV classification (22kHz vs 50kHz)
147+
- Bounding box annotation
148+
- Temporal and spectral feature extraction
149+
150+
3. **Annotation Generation**
151+
- Supports multiple input formats:
152+
- HTML
153+
- Excel
154+
- CSV
155+
156+
4. **Evaluation**
157+
- Precision/Recall calculations
158+
- F1 Score and Specificity metrics
159+
- Carbon emissions tracking
160+
- Energy consumption monitoring
161+
162+
## Evaluation Metrics
163+
164+
The pipeline calculates four key performance metrics:
165+
- **Precision**: Ratio of correct USV detections to total detections
166+
- **Recall**: Ratio of detected USVs to total actual USVs
167+
- **F1 Score**: Harmonic mean of precision and recall
168+
- **Specificity**: Ability to identify true negative segments
169+
170+
Example output:
171+
```
172+
Mean Precision: 0.92 ± 0.05
173+
Mean Recall: 0.88 ± 0.07
174+
Mean F1 Score: 0.90 ± 0.04
175+
Mean Specificity: 0.95 ± 0.03
176+
```
177+
178+
## Environmental Impact Tracking
179+
180+
The pipeline integrates with CodeCarbon to monitor:
181+
- CO₂ emissions (kg)
182+
- Energy consumption (kWh)
183+
- Computational efficiency
184+
185+
Sample output:
186+
```
187+
ContourUSV_Execution_Time_(s) = 452.783
188+
ContourUSV_Carbon_Emissions_(kgCO2) = 0.127
189+
ContourUSV_Total_Energy_Consumed_(kWh) = 0.342
190+
``` -->
191+
192+
## Contributing
193+
194+
Contributions are welcome! Please follow these steps:
195+
1. Fork the repository
196+
2. Create your feature branch (`git checkout -b feature/your-feature`)
197+
3. Commit your changes (`git commit -am 'Add some feature'`)
198+
4. Push to the branch (`git push origin feature/your-feature`)
199+
5. Open a Pull Request
200+
201+
## License
202+
203+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
204+
205+
## Contact
206+
207+
Sabah Anis - [[email protected]](mailto:[email protected])

data/USVSEG/rat_distressed/contour_detections/20181026_142935_384K_1Ch_a55_r.csv

Lines changed: 0 additions & 45 deletions
This file was deleted.

data/USVSEG/rat_distressed/contour_detections/20181026_145634_384K_1Ch_a57_r.csv

Lines changed: 0 additions & 46 deletions
This file was deleted.

data/USVSEG/rat_distressed/contour_detections/20181026_150742_384K_1Ch_a58_r.csv

Lines changed: 0 additions & 31 deletions
This file was deleted.

data/USVSEG/rat_distressed/contour_detections/20181026_153035_384K_1Ch_a60_r.csv

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)