Metadata-Version: 2.4
Name: opensoundscape
Version: 0.13.0
Summary: Open source, scalable acoustic data analysis for ecology and conservation
License: MIT
License-File: LICENSE
Author: Sam Lapp
Author-email: sammlapp@gmail.com
Requires-Python: >=3.10, !=2.7.*, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, !=3.6.*, !=3.7.*, !=3.8.*, !=3.9.*, !=3.14.*
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Provides-Extra: all
Provides-Extra: grad-cam
Provides-Extra: hoplite
Provides-Extra: onnx
Provides-Extra: tensorflow
Provides-Extra: viz
Requires-Dist: Deprecated (>=1.2.13,<2.0.0)
Requires-Dist: Jinja2 (>=3.1.6)
Requires-Dist: aiohttp (>=3.13.4)
Requires-Dist: aru-metadata-parser (>=1.0.0)
Requires-Dist: certifi (>=2024.7.4)
Requires-Dist: docopt (>=0.6.2)
Requires-Dist: filelock (>=3.20.3)
Requires-Dist: fonttools (>=4.61.0)
Requires-Dist: gitpython (>=3.1.47)
Requires-Dist: grad-cam (>=1.5.0) ; extra == "grad-cam" or extra == "all"
Requires-Dist: h11 (>=0.16.0)
Requires-Dist: ipykernel (>=5.2.0)
Requires-Dist: ipython (>=7.34.0)
Requires-Dist: ipywidgets (>=8.1.8) ; extra == "viz" or extra == "all"
Requires-Dist: joblib (>=1.2.0)
Requires-Dist: jupyter-server (>=2.11.2)
Requires-Dist: jupyterlab (>=2.1.4)
Requires-Dist: keras (>=3.12.0) ; extra == "tensorflow"
Requires-Dist: librosa (>=0.11.0)
Requires-Dist: lightning (>=2.2.5)
Requires-Dist: llvmlite (>=0.44.0)
Requires-Dist: lxml (>=6.1.0)
Requires-Dist: matplotlib (>=3.9.4)
Requires-Dist: matplotlib-inline (>=0.1.7)
Requires-Dist: nbconvert (>=7.17.1)
Requires-Dist: noisereduce (>=3.0.2)
Requires-Dist: numba (>=0.57.0)
Requires-Dist: numpy (>2.0)
Requires-Dist: onnx (>=1.19.1) ; extra == "onnx" or extra == "all"
Requires-Dist: onnxruntime (>=1.23.2) ; extra == "onnx" or extra == "all"
Requires-Dist: onnxscript (>=0.5.4) ; extra == "onnx" or extra == "all"
Requires-Dist: pandas (>=1.3)
Requires-Dist: perch-hoplite (>=1.0.1) ; extra == "hoplite" or extra == "all"
Requires-Dist: pillow (>=12.2.0)
Requires-Dist: plotly (>=6.6.0) ; extra == "viz" or extra == "all"
Requires-Dist: protobuf (>=5.29.6)
Requires-Dist: pywavelets (>=1.2.0)
Requires-Dist: requests (>=2.33.0)
Requires-Dist: schema (>=0.7.2)
Requires-Dist: scikit-image (>=0.17.2)
Requires-Dist: scikit-learn (>=0.24.2)
Requires-Dist: scipy (>=1.15)
Requires-Dist: sentry-sdk (>=2.8.0)
Requires-Dist: setuptools (>=78.1.1)
Requires-Dist: soundfile (>=0.13.1)
Requires-Dist: tensorflow (==2.20.0) ; extra == "tensorflow" or extra == "all"
Requires-Dist: tensorflow-hub (>=0.14.0) ; extra == "tensorflow" or extra == "all"
Requires-Dist: torch (>=2.8.0)
Requires-Dist: torchaudio (>=2.8.0)
Requires-Dist: torchvision (>=0.15.1)
Requires-Dist: tornado (>=6.5.5)
Requires-Dist: tqdm (>=4.67.3)
Requires-Dist: urllib3 (>=2.6.3)
Requires-Dist: wandb (>=0.17.0)
Requires-Dist: werkzeug (>=3.1.5)
Project-URL: Repository, https://github.com/jkitzes/opensoundscape
Description-Content-Type: text/markdown

# OpenSoundscape

[![CI](https://github.com/kitzeslab/opensoundscape/actions/workflows/poetry.yml/badge.svg?branch=master)](https://github.com/kitzeslab/opensoundscape/actions/workflows/poetry.yml)
[![Documentation Status](https://readthedocs.org/projects/opensoundscape/badge/?version=latest)](http://opensoundscape.org/en/latest/?badge=latest)

OpenSoundscape (OPSO) is free and open source Python utility library analyzing bioacoustic data. 

OpenSoundscape includes utilities which can be strung together to create data analysis pipelines, including functions to:

* load and manipulate audio files
* create and manipulate spectrograms
* train deep learning models to recognize sounds
* run pre-trained CNNs to detect vocalizations
* tune pre-trained CNNs to custom classification tasks
* detect periodic vocalizations with RIBBIT
* load and manipulate Raven annotations
* estimate the location of sound sources from synchronized recordings


OpenSoundscape's documentation can be found on [OpenSoundscape.org](https://opensoundscape.org).

## Show me the code!

For examples of how to use OpenSoundscape, see the [Quick Start Guide](#quick-start-guide) below.

For full API documentation and tutorials on how to use OpenSoundscape to work with audio and spectrograms, train machine learning models, apply trained machine learning models to acoustic data, and detect periodic vocalizations using RIBBIT, see the [documentation](http://opensoundscape.org).


## Contact & Citation

OpenSoundcape is developed and maintained by the [Kitzes Lab](http://www.kitzeslab.org/) at the University of Pittsburgh. It is currently in active development. If you find a bug, please [submit an issue](https://github.com/kitzeslab/opensoundscape/issues) on the GitHub repository. If you have another question about OpenSoundscape, please use the (OpenSoundscape Discussions board)[https://github.com/kitzeslab/opensoundscape/discussions] or email Sam Lapp (`sam.lapp at pitt.edu`)


Suggested citation:

    Lapp, Sam; Rhinehart, Tessa; Freeland-Haynes, Louis; 
    Khilnani, Jatin; Syunkova, Alexandra; Kitzes, Justin. 
    “OpenSoundscape: An Open-Source Bioacoustics Analysis Package for Python.” 
    Methods in Ecology and Evolution 2023. https://doi.org/10.1111/2041-210X.14196.


## Quick Start Guide

A guide to the most commonly used features of OpenSoundscape.


### Installation

Details about installation are available on the OpenSoundscape documentation at [OpenSoundscape.org](https://opensoundscape.org). FAQs:

#### How do I install OpenSoundscape?

* Most users should install OpenSoundscape via pip, preferably within a virtual environment: `pip install opensoundscape==0.13.0`. 
* To use OpenSoundscape in Jupyter Notebooks (e.g. for tutorials), follow the installation instructions for your operating system, then follow the "Jupyter" instructions.
* Contributors and advanced users can also use Poetry to install OpenSoundscape using the "Contributor" instructions

#### Will OpenSoundscape work on my machine?

* OpenSoundscape can be installed on Windows, Mac, and Linux machines.
* For Windows users, we strongly recommend using WSL2 which facilitates happy coding
* We support Python 3.10, 3.11, 3.12, and 3.13 (but current github runners only test on Python 3.13)
* Most computer cluster users should follow the Linux installation instructions
* For older Macs (Intel chip), use this workaround since newer PyTorch versions are not found by pip (replace `NAME` with the desired name of your enviornment):

```
conda create -n NAME python=3.11
conda activate NAME
conda install pytorch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 -c conda-forge
pip install opensoundscape==0.13.0
```

### Use Audio and Spectrogram classes to inspect audio data
```python
from opensoundscape import Audio, Spectrogram

#load an audio file and trim out a 5 second clip
my_audio = Audio.from_file("/path/to/audio.wav")
clip_5s = my_audio.trim(0,5)

#create a spectrogram and plot it
my_spec = Spectrogram.from_audio(clip_5s)
my_spec.plot()
```

### Load audio starting at a real-world timestamp
```python
from datetime import datetime; import pytz

start_time = pytz.timezone('UTC').localize(datetime(2020,4,4,10,25))
audio_length = 5 #seconds  
path = '/path/to/audiomoth_file.WAV' #an AudioMoth recording

Audio.from_file(path, start_timestamp=start_time,duration=audio_length)
```

### Load and use a model from the Bioacoustics Model Zoo
The [Bioacoustics Model Zoo](https://github.com/kitzeslab/bioacoustics-model-zoo) hosts models in a repository that can be installed as a package and are compatible with OpenSoundscape. To install, use
`pip install --upgrade bioacoustics-model-zoo`

To install additional dependencies for specific models, use patterns like 

`pip install --upgrade bioacoustics-model-zoo[hawkears]`

Load up a model and apply it to your own audio right away:

```python
import bioacoustics_model_zoo as bmz

#list available models
print(bmz.utils.list_models())

#generate class predictions and embedding vectors with HawkEars...
hawkears = bmz.HawkEars()
scores = hawkears.predict(files)
embeddings = hawkears.embed(files)

#...or BirdNET...
# (you'll need ai-edge-litert in your environment, run `pip install bioacoustics-model-zoo[birdnet]`)
birdnet = bmz.BirdNET()
scores = birdnet.predict(files)
embeddings = birdnet.embed(files)

# or Perch2
# `pip install bioacoustics-model-zoo[perch]` will install tensorflow and tensorflow-hub
#...or BirdNET...
# (you'll need ai-edge-litert in your environment, run `pip install bioacoustics-model-zoo[birdnet]`)
perch2 = bmz.Perch2()
scores = perch2.predict(files)
embeddings = perch2.embed(files)
```

See the tutorial notebooks for examples of training and fine-tuning models from the model zoo with your own annotations. 

### Load a pre-trained CNN from a local file, and make predictions on long audio files
```python
from opensoundscape import load_model

#get list of audio files
files = glob('./dir/*.WAV')

#generate predictions with a model
model = load_model('/path/to/saved.model')
scores = model.predict(files)

#scores is a dataframe with MultiIndex: file, start_time, end_time
#containing inference scores for each class and each audio window
```

### Train a CNN using audio files and Raven annotations 
```python
from sklearn.model_selection import train_test_split
from opensoundscape import BoxedAnnotations, CNN

# assume we have a list of raven annotation files and corresponding audio files
# load the annotations into OpenSoundscape
all_annotations = BoxedAnnotations.from_raven_files(raven_file_paths,audio_file_paths)

# pick classes to train the model on. These should occur in the annotated data
class_list = ['IBWO','BLJA']

# create labels for fixed-duration (2 second) clips 
labels = all_annotations.clip_labels(
  clip_duration=2,
  clip_overlap=0,
  min_label_overlap=0.25,
  class_subset=class_list
)

# split the labels into training and validation sets
train_df, validation_df = train_test_split(labels, test_size=0.3)

# create a CNN and train on the labeled data
model = CNN(architecture='resnet18', sample_duration=2, classes=class_list, sample_rate=32000)

# train the model to recognize the classes of interest in audio data
model.train(train_df, validation_df, steps=500, num_workers=8, batch_size=256)
```

### Train a custom classifier on BirdNET or Perch embeddings

Make sure you've installed the model zoo in your Python environment:

`pip install bioacoustics-model-zoo==0.12.0`

```python
import bioacoustics_model_zoo as bmz

# load a model from the model zoo
model = bmz.BirdNET() #or bmz.Perch()

# define classes for your custom classifier
model.change_classes(train_df.columns)

# fit the trainable PyTorch classifier on your labels
model.train(train_df,val_df,num_augmentation_variants=4,batch_size=64)

# run inference using your custom classifier on audio data
model.predict(audio_files)

# save and load customized models
model.save(save_path)
reloaded_model = bmz.BirdNET.load(save_path)
```

