-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 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 81bffbf
Batch process annotattions and prevent duplicates
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
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
| """ | ||
| 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__) | ||
|
|
||
| COPY_COMMENT = "Copy of batbot pulse annotation" | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.argument("username") | ||
| @click.option("--batch-size", default=5000) | ||
| def copy_batbot_pulse_annotations(username: str, batch_size: int = 5000): | ||
| 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 = ( | ||
| Annotations.objects.filter(model=BATBOT_ANNOTATION_MODEL) | ||
| .order_by("recording_id") | ||
| .iterator(chunk_size=batch_size) | ||
| ) | ||
|
|
||
| total_count = 0 | ||
|
|
||
| copies = [] | ||
| current_recording_id = None | ||
| skip_recording = False | ||
| for original in batbot_pulse_annotations: | ||
| if original.recording_id != current_recording_id: | ||
| current_recording_id = original.recording_id | ||
| # This check attempts to prevent multiple runs of this command | ||
| # from creating redundant copies for the given user. This is not | ||
| # perfect, since it relies on the editable "comments" field, and | ||
| # lazily checks for the existence of any annotation for the given | ||
| # recording that was created by a run of this command. There are 2 | ||
| # existing concerns that are unlikely to cause trouble in the short | ||
| # term, but should be noted: | ||
| # | ||
| # 1. "comments" are editable. If a user edits all the comments | ||
| # for their copied annotations for a given recording, a re-run of | ||
| # this command will duplicate the annotations again. | ||
| # | ||
| # 2. If a run of this command is interrupted and a strict subset of | ||
| # annotations for a given recording has been saved, then whichever annotations | ||
| # for that recording have not been copied WILL NOT be copied by a subsequent run. | ||
| copied_already = Annotations.objects.filter( | ||
| owner=new_owner, comments=COPY_COMMENT, recording_id=current_recording_id | ||
| ).exists() | ||
| skip_recording = copied_already | ||
| if skip_recording: | ||
| continue | ||
| else: | ||
| # Create a new annotation object and add it to copies | ||
| copies.append( | ||
| Annotations( | ||
| recording_id=original.recording_id, | ||
| 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_COMMENT, | ||
| model="", | ||
| confidence=original.confidence, | ||
| ) | ||
| ) | ||
| if len(copies) >= batch_size: | ||
| logger.info("Saving %d copied annotations...", len(copies)) | ||
| total_count += len(copies) | ||
| Annotations.objects.bulk_create(copies) | ||
| copies = [] | ||
|
|
||
| if len(copies) > 0: | ||
| logger.info("Saving %d copied annotations...", len(copies)) | ||
| total_count += len(copies) | ||
| Annotations.objects.bulk_create(copies) | ||
|
|
||
| logger.info("Done. Copied %d annotations.", total_count) |
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
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.