{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "32653374-08ba-4390-8830-4d6c9413b6fe", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:20.527941Z", "iopub.status.busy": "2024-11-18T21:26:20.527163Z", "iopub.status.idle": "2024-11-18T21:26:20.553961Z", "shell.execute_reply": "2024-11-18T21:26:20.551578Z", "shell.execute_reply.started": "2024-11-18T21:26:20.527868Z" } }, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "id": "c8930546-1827-496d-98a8-264f4c97a2cc", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:20.555450Z", "iopub.status.busy": "2024-11-18T21:26:20.555137Z", "iopub.status.idle": "2024-11-18T21:26:26.663946Z", "shell.execute_reply": "2024-11-18T21:26:26.662988Z", "shell.execute_reply.started": "2024-11-18T21:26:20.555420Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/santiago/Software/bat_detectors/batdetect2/.venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import json\n", "from pathlib import Path\n", "from typing import List\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pytest\n", "from batdetect2.compat.data import load_annotation_project\n", "from batdetect2.compat.params import get_training_preprocessing_config\n", "from batdetect2.train.preprocess import generate_train_example" ] }, { "cell_type": "code", "execution_count": 3, "id": "5af8d5fd-58a8-4034-b92d-0514cd987eaf", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:26.665630Z", "iopub.status.busy": "2024-11-18T21:26:26.664900Z", "iopub.status.idle": "2024-11-18T21:26:26.735198Z", "shell.execute_reply": "2024-11-18T21:26:26.734346Z", "shell.execute_reply.started": "2024-11-18T21:26:26.665601Z" } }, "outputs": [], "source": [ "regression_dir = Path(\"../tests/data/regression/\")\n", "example_audio_dir = Path(\"../example_data/audio/\")\n", "example_audio_files = list(example_audio_dir.glob(\"*.wav\"))\n", "example_anns_dir = Path(\"../example_data/anns/\")\n", "\n", "audio_file = example_audio_files[0]\n", "example_file = regression_dir / f\"{audio_file.name}.npz\"\n", "\n", "dataset = np.load(example_file)\n", "\n", "spec = dataset[\"spec\"][0]\n", "detection_mask = dataset[\"detection_mask\"][0]\n", "size_mask = dataset[\"size_mask\"]\n", "class_mask = dataset[\"class_mask\"]\n", "\n", "project = load_annotation_project(\n", " example_anns_dir,\n", " audio_dir=example_audio_dir,\n", ")\n", "\n", "clip_annotation = next(\n", " ann for ann in project.clip_annotations if ann.clip.recording.path == audio_file\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "id": "b7b069a3-995f-4e73-8886-d1ce2202f5a5", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:26.736224Z", "iopub.status.busy": "2024-11-18T21:26:26.735953Z", "iopub.status.idle": "2024-11-18T21:26:26.767802Z", "shell.execute_reply": "2024-11-18T21:26:26.766909Z", "shell.execute_reply.started": "2024-11-18T21:26:26.736200Z" } }, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clip_annotation.clip.duration" ] }, { "cell_type": "code", "execution_count": 5, "id": "62c23365-8660-4ffb-b987-9131d6af6718", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:26.769444Z", "iopub.status.busy": "2024-11-18T21:26:26.768777Z", "iopub.status.idle": "2024-11-18T21:26:26.806659Z", "shell.execute_reply": "2024-11-18T21:26:26.805641Z", "shell.execute_reply.started": "2024-11-18T21:26:26.769415Z" } }, "outputs": [], "source": [ "old_parameters = json.loads((regression_dir / \"params.json\").read_text())\n", "config = get_training_preprocessing_config(old_parameters)" ] }, { "cell_type": "code", "execution_count": 6, "id": "2f244732-fa11-4042-8043-c8f9268a55a3", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:26.810985Z", "iopub.status.busy": "2024-11-18T21:26:26.810570Z", "iopub.status.idle": "2024-11-18T21:26:26.846914Z", "shell.execute_reply": "2024-11-18T21:26:26.845654Z", "shell.execute_reply.started": "2024-11-18T21:26:26.810945Z" } }, "outputs": [ { "data": { "text/plain": [ "SpecSizeConfig(height=256, resize_factor=0.5, divide_factor=32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.preprocessing.spectrogram.size" ] }, { "cell_type": "code", "execution_count": 7, "id": "641433fb-3d19-44df-8902-b0e0208b2013", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:26.849480Z", "iopub.status.busy": "2024-11-18T21:26:26.848511Z", "iopub.status.idle": "2024-11-18T21:26:27.390760Z", "shell.execute_reply": "2024-11-18T21:26:27.389986Z", "shell.execute_reply.started": "2024-11-18T21:26:26.849438Z" } }, "outputs": [], "source": [ "new_dataset = generate_train_example(clip_annotation, config)\n", "new_spec = new_dataset[\"spectrogram\"].values\n", "new_detection_mask = new_dataset[\"detection\"].values\n", "new_size_mask = new_dataset[\"size\"].values\n", "new_class_mask = new_dataset[\"class\"].values" ] }, { "cell_type": "code", "execution_count": 8, "id": "502d25c1-0924-4a9f-8676-5a68cbd6c3af", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:27.391967Z", "iopub.status.busy": "2024-11-18T21:26:27.391628Z", "iopub.status.idle": "2024-11-18T21:26:27.426737Z", "shell.execute_reply": "2024-11-18T21:26:27.426148Z", "shell.execute_reply.started": "2024-11-18T21:26:27.391935Z" } }, "outputs": [ { "data": { "text/plain": [ "(128, 512)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_spec.shape" ] }, { "cell_type": "code", "execution_count": 9, "id": "185f8b4e-bfcb-447f-9097-845fbd69ba6f", "metadata": { "execution": { "iopub.execute_input": "2024-11-18T21:26:27.428183Z", "iopub.status.busy": "2024-11-18T21:26:27.427611Z", "iopub.status.idle": "2024-11-18T21:26:27.468561Z", "shell.execute_reply": "2024-11-18T21:26:27.467552Z", "shell.execute_reply.started": "2024-11-18T21:26:27.428145Z" } }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'category' (category: 17)> Size: 2kB\n", "array(['Barbastellus barbastellus', 'Eptesicus serotinus', 'Myotis alcathoe',\n", " 'Myotis bechsteinii', 'Myotis brandtii', 'Myotis daubentonii',\n", " 'Myotis mystacinus', 'Myotis nattereri', 'Nyctalus leisleri',\n", " 'Nyctalus noctula', 'Pipistrellus nathusii',\n", " 'Pipistrellus pipistrellus', 'Pipistrellus pygmaeus',\n", " 'Plecotus auritus', 'Plecotus austriacus', 'Rhinolophus ferrumequinum',\n", " 'Rhinolophus hipposideros'], dtype='<U25')\n", "Coordinates:\n", " channel int64 8B 0\n", " * category (category) <U25 2kB 'Barbastellus barbastellus' ... 'Rhinolophu...