-
Notifications
You must be signed in to change notification settings - Fork 3
Batbot pulse annotations #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BryonLewis
wants to merge
11
commits into
main
Choose a base branch
from
batbot-pulse-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
16cd953
add configuration setting for creating pulse annotations
BryonLewis 1427a73
migration for pulses
BryonLewis 1855a56
extract pulses and add to recording data
BryonLewis 7b4f186
recenter on select, format precision for times
BryonLewis 1898a22
Fix get_or_create calls
naglepuff bf8a3ec
Allow any users to edit "batbot" pulse annots
naglepuff f597851
Use constant value for "batbot"
naglepuff 7de587a
Format
naglepuff 699e9b3
Regenerate migration file
naglepuff 5188dc1
Prevent non-owners from seeing batbot annotations
naglepuff 221df9f
Add management command for copying batbot annots
naglepuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
bats_ai/core/management/commands/copy_batbot_pulse_annotations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| Management command to duplicate the batbot model's pulse annotations for a user. | ||
|
|
||
| Useful for generating training data to improve Batbot's ability to detect pulses. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from django.contrib.auth.models import User | ||
| from django.core.management.base import CommandError | ||
| import djclick as click | ||
|
|
||
| from bats_ai.core.models import Annotations | ||
| from bats_ai.core.utils.batbot_annotations import BATBOT_ANNOTATION_MODEL | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.argument("username") | ||
| def copy_batbot_pulse_annotations(username: str): | ||
| logger.info("Finding user with username %s...", username) | ||
| new_owner = User.objects.filter(username=username).first() | ||
| if not new_owner: | ||
| raise CommandError(f"No user found with username {username}") | ||
|
|
||
| logger.info("Finding all pulse annotations created by batbot...") | ||
| batbot_pulse_annotations = list( | ||
| Annotations.objects.filter(model=BATBOT_ANNOTATION_MODEL).prefetch_related("species") | ||
| ) | ||
|
|
||
| copies = [ | ||
| Annotations( | ||
| recording=original.recording, | ||
| owner=new_owner, | ||
| start_time=original.start_time, | ||
| end_time=original.end_time, | ||
| low_freq=original.low_freq, | ||
| high_freq=original.high_freq, | ||
| type=original.type, | ||
| comments="Copy of batbot pulse annotation", | ||
| model="", | ||
| confidence=original.confidence, | ||
| ) | ||
| for original in batbot_pulse_annotations | ||
| ] | ||
|
|
||
| logger.info("Saving %d new annotations...", len(copies)) | ||
| created = Annotations.objects.bulk_create(copies) | ||
|
|
||
| logger.info("Copying species info...") | ||
| for original, copy in zip(batbot_pulse_annotations, created, strict=True): | ||
| copy.species.set(original.species.all()) | ||
|
|
||
| logger.info("Done") |
19 changes: 19 additions & 0 deletions
19
bats_ai/core/migrations/0042_configuration_create_pulse_annotations_from_batbot.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Generated by Django 6.0.7 on 2026-08-10 18:29 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("core", "0041_rename_grts_cell_nabatrecording_sample_frame_id_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="configuration", | ||
| name="create_pulse_annotations_from_batbot", | ||
| field=models.BooleanField(default=False), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from bats_ai.core.models import Annotations, Configuration | ||
|
|
||
| if TYPE_CHECKING: | ||
| from bats_ai.core.models import Recording | ||
| from bats_ai.core.utils.batbot_metadata import BatBotMetadataCurve | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| BATBOT_ANNOTATION_MODEL = "batbot" | ||
|
|
||
|
|
||
| def _segment_bounds( | ||
| segment: BatBotMetadataCurve, | ||
| ) -> tuple[float, float, float, float] | None: | ||
| curve = segment.get("curve_hz_ms") or [] | ||
| if not curve: | ||
| return None | ||
|
|
||
| times = [pt[1] for pt in curve] | ||
| freqs = [pt[0] for pt in curve] | ||
| return min(times), max(times), min(freqs), max(freqs) | ||
|
|
||
|
|
||
| def create_pulse_annotations_from_batbot_segments( | ||
| recording: Recording, | ||
| segments: list[BatBotMetadataCurve], | ||
| ) -> int: | ||
| """Create pulse annotations from BatBot segments when enabled in Configuration.""" | ||
| config = Configuration.objects.first() | ||
| if not config or not config.create_pulse_annotations_from_batbot: | ||
| return 0 | ||
|
|
||
| Annotations.objects.filter( | ||
| recording=recording, | ||
| model=BATBOT_ANNOTATION_MODEL, | ||
| ).delete() | ||
|
|
||
| created = 0 | ||
| for segment in segments: | ||
| bounds = _segment_bounds(segment) | ||
| if bounds is None: | ||
| segment_index = segment.get("segment_index") | ||
| logger.warning( | ||
| "Skipping BatBot pulse annotation for recording=%s segment_index=%s: no bbox", | ||
| recording.pk, | ||
| segment_index, | ||
| ) | ||
| continue | ||
|
|
||
| t_start, t_end, f_lo, f_hi = bounds | ||
| Annotations.objects.create( | ||
| recording=recording, | ||
| owner=recording.owner, | ||
| start_time=t_start, | ||
| end_time=t_end, | ||
| low_freq=f_lo, | ||
| high_freq=f_hi, | ||
| type="pulse", | ||
| model=BATBOT_ANNOTATION_MODEL, | ||
| comments="", | ||
| ) | ||
| created += 1 | ||
|
|
||
| return created | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.