-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial metadata extraction * add models to recording * client side guano metadata * add fron-end tool for details * linting * batch uploading guano metadata * client linting * support for additional time formats
- Loading branch information
1 parent
7388e11
commit bc2f2d0
Showing
18 changed files
with
826 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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
51 changes: 51 additions & 0 deletions
51
bats_ai/core/migrations/0010_recording_computed_species_recording_detector_and_more.py
This file contains 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,51 @@ | ||
# Generated by Django 4.1.13 on 2024-04-03 13:07 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('core', '0009_annotations_type'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='recording', | ||
name='computed_species', | ||
field=models.ManyToManyField( | ||
related_name='recording_computed_species', to='core.species' | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='detector', | ||
field=models.TextField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='official_species', | ||
field=models.ManyToManyField( | ||
related_name='recording_official_species', to='core.species' | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='site_name', | ||
field=models.TextField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='software', | ||
field=models.TextField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='species_list', | ||
field=models.TextField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='recording', | ||
name='unusual_occurrences', | ||
field=models.TextField(blank=True, null=True), | ||
), | ||
] |
This file contains 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 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 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,88 @@ | ||
from datetime import datetime | ||
import logging | ||
|
||
from django.http import HttpRequest, JsonResponse | ||
from guano import GuanoFile | ||
from ninja import File, Schema | ||
from ninja.files import UploadedFile | ||
from ninja.pagination import RouterPaginated | ||
|
||
router = RouterPaginated() | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GuanoMetadataSchema(Schema): | ||
nabat_grid_cell_grts_id: str | None = None | ||
nabat_latitude: float | None = None | ||
nabat_longitude: float | None = None | ||
nabat_site_name: str | None = None | ||
nabat_activation_start_time: datetime | None = None | ||
nabat_activation_end_time: datetime | None = None | ||
nabat_software_type: str | None = None | ||
nabat_species_list: list[str] | None = None | ||
nabat_comments: str | None = None | ||
nabat_detector_type: str | None = None | ||
nabat_unusual_occurrences: str | None = None | ||
|
||
|
||
router = RouterPaginated() | ||
|
||
|
||
@router.post('/') | ||
def default_data( | ||
request: HttpRequest, | ||
audio_file: File[UploadedFile], | ||
): | ||
try: | ||
# Read GUANO metadata from the file name provided | ||
gfile = GuanoFile(audio_file.file.name) | ||
|
||
# Extract required NABat fields | ||
nabat_fields = { | ||
'nabat_grid_cell_grts_id': gfile.get('NABat|Grid Cell GRTS ID', None), | ||
'nabat_latitude': (gfile.get('NABat|Latitude', None)), | ||
'nabat_longitude': (gfile.get('NABat|Longitude', None)), | ||
'nabat_site_name': gfile.get('NABat|Site Name', None), | ||
} | ||
|
||
# Extract additional fields with conditionals | ||
additional_fields = { | ||
'nabat_activation_start_time': parse_datetime( | ||
gfile.get('NABat|Activation start time', None) | ||
) | ||
if 'NABat|Activation start time' in gfile | ||
else None, | ||
'nabat_activation_end_time': parse_datetime( | ||
gfile.get('NABat|Activation end time', None) | ||
) | ||
if 'NABat|Activation end time' in gfile | ||
else None, | ||
'nabat_software_type': gfile.get('NABat|Software type', None), | ||
'nabat_species_list': gfile.get('NABat|Species List', '').split(','), | ||
'nabat_comments': gfile.get('NABat|Comments', None), | ||
'nabat_detector_type': gfile.get('NABat|Detector type', None), | ||
'nabat_unusual_occurrences': gfile.get('NABat|Unusual occurrences', ''), | ||
} | ||
|
||
# Combine all extracted fields | ||
metadata = {**nabat_fields, **additional_fields} | ||
|
||
return JsonResponse(metadata, safe=False) | ||
|
||
except Exception as e: | ||
return JsonResponse({'error': str(e)}, status=500) | ||
|
||
|
||
def parse_datetime(datetime_str): | ||
if datetime_str: | ||
try: | ||
# Try parsing using the custom format | ||
return datetime.strptime(datetime_str, '%Y%m%dT%H%M%S') | ||
except ValueError: | ||
try: | ||
# Try parsing using ISO format | ||
return datetime.fromisoformat(datetime_str) | ||
except ValueError: | ||
# If both formats fail, return None or handle the error accordingly | ||
return None | ||
return None |
This file contains 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 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.