-
Notifications
You must be signed in to change notification settings - Fork 1
Bulk Import Management Command #271
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
5 commits
Select commit
Hold shift + click to select a range
d6476e1
bulk import managment command
BryonLewis f7cd98d
Update bats_ai/core/utils/guano_utils.py
BryonLewis f201084
Update bats_ai/core/management/commands/importRecordings.py
BryonLewis 9cf42e9
Update bats_ai/core/management/commands/importRecordings.py
BryonLewis 9b7791e
adding comments and linting
BryonLewis 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
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,206 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from django.contrib.auth.models import User | ||
| from django.contrib.gis.geos import Point | ||
| from django.core.files import File | ||
| from django.core.management.base import BaseCommand | ||
| from django.utils import timezone | ||
|
|
||
| from bats_ai.core.models import Recording | ||
| from bats_ai.core.utils.guano_utils import extract_guano_metadata | ||
| from bats_ai.tasks.tasks import recording_compute_spectrogram | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = 'Import WAV files from a directory, extract GUANO metadata, and create recordings' | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| 'directory', | ||
| type=str, | ||
| help='Directory path containing WAV files to import', | ||
| ) | ||
| parser.add_argument( | ||
| '--owner', | ||
| type=str, | ||
| help='Username of the owner for the recordings (defaults to first superuser)', | ||
| ) | ||
| parser.add_argument( | ||
| '--public', | ||
| action='store_true', | ||
| help='Make imported recordings public', | ||
| ) | ||
| parser.add_argument( | ||
| '--limit', | ||
| type=int, | ||
| help='Limit the number of WAV files to import (useful for testing)', | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| import matplotlib | ||
|
|
||
| matplotlib.use('Agg') | ||
|
|
||
| directory_path = Path(options['directory']) | ||
| owner_username = options.get('owner') | ||
| is_public = options.get('public', False) | ||
| limit = options.get('limit') | ||
|
|
||
| # Validate directory | ||
| if not directory_path.exists(): | ||
| self.stdout.write(self.style.ERROR(f'Directory does not exist: {directory_path}')) | ||
| return | ||
|
|
||
| if not directory_path.is_dir(): | ||
| self.stdout.write(self.style.ERROR(f'Path is not a directory: {directory_path}')) | ||
| return | ||
|
|
||
| # Get or find owner | ||
| if owner_username: | ||
| try: | ||
| owner = User.objects.get(username=owner_username) | ||
| except User.DoesNotExist: | ||
| self.stdout.write(self.style.ERROR(f'User not found: {owner_username}')) | ||
| return | ||
| else: | ||
| # Default to first superuser | ||
| owner = User.objects.filter(is_superuser=True).first() | ||
| if not owner: | ||
| self.stdout.write( | ||
| self.style.ERROR( | ||
| 'No superuser found. Please specify --owner or create a superuser.' | ||
| ) | ||
| ) | ||
| return | ||
| self.stdout.write(self.style.WARNING(f'Using default owner: {owner.username}')) | ||
|
|
||
| # Find all WAV files | ||
| wav_files = list(directory_path.rglob('*.wav', case_sensitive=False)) | ||
|
|
||
| if not wav_files: | ||
| self.stdout.write( | ||
| self.style.WARNING(f'No WAV files found in directory: {directory_path}') | ||
| ) | ||
| return | ||
|
|
||
| # Apply limit if specified | ||
| total_files = len(wav_files) | ||
| if limit and limit > 0: | ||
| wav_files = wav_files[:limit] | ||
| self.stdout.write( | ||
| self.style.SUCCESS( | ||
| f'Found {total_files} WAV file(s), importing first {len(wav_files)}' | ||
| ) | ||
| ) | ||
| else: | ||
| self.stdout.write(self.style.SUCCESS(f'Found {len(wav_files)} WAV file(s) to import')) | ||
|
|
||
| # Process each file | ||
| successful = 0 | ||
| failed = 0 | ||
|
|
||
| for idx, wav_file in enumerate(wav_files, 1): | ||
| self.stdout.write(f'\n[{idx}/{len(wav_files)}] Processing: {wav_file.name}') | ||
|
|
||
| try: | ||
| # Extract GUANO metadata | ||
| self.stdout.write(' Extracting GUANO metadata...') | ||
| metadata = extract_guano_metadata(wav_file, check_filename=True) | ||
|
|
||
| # Extract date and time from metadata or file modification time | ||
| recorded_date = None | ||
| recorded_time = None | ||
|
|
||
| if metadata.get('nabat_activation_start_time'): | ||
| dt = metadata['nabat_activation_start_time'] | ||
| recorded_date = dt.date() | ||
| recorded_time = dt.time() | ||
| else: | ||
| # Use file modification time as fallback | ||
| mtime = timezone.datetime.fromtimestamp( | ||
| wav_file.stat().st_mtime, tz=timezone.get_current_timezone() | ||
| ) | ||
| recorded_date = mtime.date() | ||
| recorded_time = mtime.time() | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| ' No activation start time in metadata, using file modification time' | ||
| ) | ||
| ) | ||
|
|
||
| # Create Point from latitude/longitude if available | ||
| point = None | ||
| if metadata.get('nabat_latitude') and metadata.get('nabat_longitude'): | ||
| point = Point(metadata['nabat_longitude'], metadata['nabat_latitude']) | ||
|
|
||
| # Get grid cell ID | ||
| grts_cell_id = None | ||
| if metadata.get('nabat_grid_cell_grts_id'): | ||
| try: | ||
| grts_cell_id = int(metadata['nabat_grid_cell_grts_id']) | ||
| except (ValueError, TypeError): | ||
| pass | ||
|
|
||
| # Convert species list to string if present | ||
| species_list_str = None | ||
| if metadata.get('nabat_species_list'): | ||
| species_list_str = ','.join(metadata['nabat_species_list']) | ||
|
|
||
| # Create recording | ||
| self.stdout.write(' Creating recording...') | ||
| with open(wav_file, 'rb') as f: | ||
| recording = Recording( | ||
| name=wav_file.name, | ||
| owner=owner, | ||
| audio_file=File(f, name=wav_file.name), | ||
| recorded_date=recorded_date, | ||
| recorded_time=recorded_time, | ||
| equipment=None, # Not in GUANO metadata | ||
| grts_cell_id=grts_cell_id, | ||
| recording_location=point, | ||
| public=is_public, | ||
| comments=metadata.get('nabat_comments'), | ||
| detector=metadata.get('nabat_detector_type'), | ||
| software=metadata.get('nabat_software_type'), | ||
| site_name=metadata.get('nabat_site_name'), | ||
| species_list=species_list_str, | ||
| unusual_occurrences=metadata.get('nabat_unusual_occurrences'), | ||
| ) | ||
| recording.save() | ||
|
|
||
| self.stdout.write(self.style.SUCCESS(f' Created recording ID: {recording.pk}')) | ||
|
|
||
| # Generate spectrogram synchronously | ||
| self.stdout.write(' Generating spectrogram...') | ||
| try: | ||
| result = recording_compute_spectrogram(recording.pk) | ||
| self.stdout.write( | ||
| self.style.SUCCESS( | ||
| f' Spectrogram generated (ID: {result.get("spectrogram_id")})' | ||
| ) | ||
| ) | ||
| except Exception as e: | ||
| self.stdout.write( | ||
| self.style.ERROR(f' Failed to generate spectrogram: {str(e)}') | ||
| ) | ||
| logger.exception('Error generating spectrogram', exc_info=e) | ||
| raise e | ||
|
|
||
| successful += 1 | ||
| self.stdout.write(self.style.SUCCESS(f' ✓ Successfully imported: {wav_file.name}')) | ||
|
|
||
| except Exception as e: | ||
| failed += 1 | ||
| self.stdout.write( | ||
| self.style.ERROR(f' ✗ Failed to import {wav_file.name}: {str(e)}') | ||
| ) | ||
| logger.exception('Error importing file', exc_info=e) | ||
|
|
||
| # Summary | ||
| self.stdout.write('\n' + '=' * 60) | ||
| self.stdout.write( | ||
| self.style.SUCCESS(f'Import complete: {successful} successful, {failed} failed') | ||
| ) | ||
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to re-raise so the outer try/except can properly increment
failed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update to add it in.