{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BatDetect2 - Demo\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/macaodha/batdetect2/blob/master/batdetect2_notebook.ipynb)\n",
    "\n",
    "This notebook gives an example of how to run the BatDetect2 code. You can either run it locally on your own computer or on Google Colab. \n",
    "\n",
    "It first specifies some arguments, loads the model, selects the audio file of interest, runs the model, and finally plots the results. \n",
    "\n",
    "Click [here](https://colab.research.google.com/github/macaodha/batdetect2/blob/master/batdetect2_notebook.ipynb) to run it on on Google Colab. Make sure to enable GPU usage on Colab for this Notebook to make it run faster. On Colab you will also have to uncomment some lines - see below for details. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load BatDetect and configure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the following lines are only needed in Colab.\n",
    "# clone the code and change directory\n",
    "#!git clone https://github.com/macaodha/batdetect2\n",
    "#%cd batdetect2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import batdetect2 api and plotting functions\n",
    "from batdetect2 import api, plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure the run\n",
    "config = api.get_config(\n",
    "    detection_threshold=0.3,\n",
    "    time_expansion_factor=1,\n",
    "    max_duration=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select the audio file and run the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# choose an audio file\n",
    "audio_file = \"example_data/audio/20170701_213954-MYOMYS-LR_0_0.5.wav\"\n",
    "\n",
    "# the following lines are only needed in Colab\n",
    "# alternatively you can upload your own file\n",
    "# from google.colab import files\n",
    "# uploaded = files.upload()\n",
    "# audio_file = list(uploaded.keys())[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the model\n",
    "results = api.process_file(audio_file, config=config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Print the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Results for 20170701_213954-MYOMYS-LR_0_0.5.wav\n",
      "15 calls detected\n",
      "\n",
      "time\tprob\tlfreq\tspecies_name\n",
      "0.0225\t0.55\t33203\tMyotis mystacinus\n",
      "0.0525\t0.679\t28046\tMyotis mystacinus\n",
      "0.1155\t0.488\t33203\tMyotis mystacinus\n",
      "0.1385\t0.46\t48671\tPipistrellus pipistrellus\n",
      "0.1445\t0.656\t27187\tMyotis mystacinus\n",
      "0.2025\t0.549\t34062\tMyotis mystacinus\n",
      "0.2195\t0.503\t48671\tPipistrellus pipistrellus\n",
      "0.2315\t0.672\t27187\tMyotis mystacinus\n",
      "0.2995\t0.65\t48671\tPipistrellus pipistrellus\n",
      "0.3245\t0.687\t27187\tMyotis mystacinus\n",
      "0.3705\t0.547\t34062\tMyotis mystacinus\n",
      "0.4125\t0.664\t28906\tMyotis mystacinus\n",
      "0.4365\t0.544\t36640\tMyotis mystacinus\n",
      "0.4745\t0.73\t48671\tPipistrellus pipistrellus\n",
      "0.4895\t0.404\t36640\tMyotis mystacinus\n"
     ]
    }
   ],
   "source": [
    "# print summary info for the individual detections\n",
    "api.print_summary(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualise the detections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# read the audio file\n",
    "audio = api.load_audio(audio_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate spectrogram\n",
    "spec = api.generate_spectrogram(audio, config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compute the detections\n",
    "detections, _ = api.process_spectrogram(spec, config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 415324 characters]",
      "text/plain": [
       "<Figure size 1500x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot detections\n",
    "ax = plot.spectrogram_with_detections(spec, detections, config=config, figsize=(15, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use extracted features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the process_spectrogram function also returns a feature vector for each detection\n",
    "detections, features = api.process_spectrogram(spec, config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We will use seaborn to plot the PCA projection of the features\n",
    "# Make sure it is installed by running the following line\n",
    "# %pip install seaborn\n",
    "\n",
    "import seaborn as sns\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot: >"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 33012 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X, Y = PCA(n_components=2).fit_transform(features).T\n",
    "sns.scatterplot(\n",
    "    x=X,\n",
    "    y=Y,\n",
    "    style=[d[\"class\"] for d in detections],\n",
    "    hue=[d[\"det_prob\"] for d in detections],\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sci",
   "language": "python",
   "name": "sci"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
