Skip to content

Conversation

@carlosgjs
Copy link
Collaborator

@carlosgjs carlosgjs commented Nov 5, 2025

Summary

This pull request adds a new Django management command to make it easier to test the Pipeline.save_results() method using data from a JSON file. The command loads pipeline results from a JSON file, validates them, creates a test job, and then calls save_results() to store the results in the database.

List of Changes

  • Added test_save_results management command in ami/ml/management/commands/test_save_results.py to load results from a JSON file, validate them using the PipelineResultsResponse schema, create a test job, and call Pipeline.save_results() for manual testing and debugging.

Screenshots

docker compose run --rm django python manage.py test_save_results 11 data/celery/debug/pipeline_results_1761696555.json 1

[+] Creating 5/5
 ✔ Container ami_local_redis        Running                                                                                                                        0.0s 
 ✔ Container antenna-ml_backend-1   Running                                                                                                                        0.0s 
 ✔ Container antenna-postgres-1     Running                                                                                                                        0.0s 
 ✔ Container antenna-minio-1        Running                                                                                                                        0.0s 
 ✔ Container antenna-minio-proxy-1  Running                                                                                                                        0.0s 
[+] Running 1/1
 ✔ Container antenna-minio-init-1  Started                                                                                                                         0.1s 
PostgreSQL is available
Using pipeline: Moth / Non-Moth Classifier (ID: 11)
Loading results from: data/celery/debug/pipeline_results_1761696555.json
✓ Parsed results: 1 source images, 9984 detections
Created test job: 26
Calling Pipeline.save_results()...
INFO 2025-11-11 14:13:30,327 models 1 281473805675840 Adding JobLogHandler to logger for job 26
INFO 2025-11-11 14:13:30,327 models 1 281473805675840 Saving results from pipeline moth_binary
INFO 2025-11-11 14:13:30,880 models 1 281473805675840 Algorithms registered for pipeline: 
fasterrcnn_for_ami_moth_traps_2023, moth_nonmoth_classifier
INFO 2025-11-11 14:13:46,192 models 1 281473805675840 Created 0 new detections, updated 9984 existing detections, for 4 source image(s)
INFO 2025-11-11 14:14:15,331 models 1 281473805675840 Created 0 new classifications, updated 9984 existing classifications for 9984 detections.
INFO 2025-11-11 14:14:15,334 models 1 281473805675840 Determining occurrences for 4096 detections for source image 21300
INFO 2025-11-11 14:14:16,208 models 1 281473805675840 Created 0 new occurrences
INFO 2025-11-11 14:14:16,210 models 1 281473805675840 Updated 0 detections with occurrences
INFO 2025-11-11 14:14:28,892 models 1 281473805675840 Updated the determination of 0 occurrences, left 4096 unchanged
INFO 2025-11-11 14:14:28,901 models 1 281473805675840 Determining occurrences for 1728 detections for source image 26259
INFO 2025-11-11 14:14:29,281 models 1 281473805675840 Created 0 new occurrences
INFO 2025-11-11 14:14:29,283 models 1 281473805675840 Updated 0 detections with occurrences
INFO 2025-11-11 14:14:34,624 models 1 281473805675840 Updated the determination of 0 occurrences, left 1728 unchanged
INFO 2025-11-11 14:14:34,631 models 1 281473805675840 Determining occurrences for 2432 detections for source image 38867
INFO 2025-11-11 14:14:35,171 models 1 281473805675840 Created 0 new occurrences
INFO 2025-11-11 14:14:35,173 models 1 281473805675840 Updated 0 detections with occurrences
INFO 2025-11-11 14:14:42,611 models 1 281473805675840 Updated the determination of 0 occurrences, left 2432 unchanged
INFO 2025-11-11 14:14:42,618 models 1 281473805675840 Determining occurrences for 1728 detections for source image 36181
INFO 2025-11-11 14:14:42,993 models 1 281473805675840 Created 0 new occurrences
INFO 2025-11-11 14:14:42,995 models 1 281473805675840 Updated 0 detections with occurrences
INFO 2025-11-11 14:14:48,340 models 1 281473805675840 Updated the determination of 0 occurrences, left 1728 unchanged
INFO 2025-11-11 14:14:48,348 pipeline 1 281473805675840 Updating calculated fields for 1 source images
INFO 2025-11-11 14:14:48,367 models 1 281473805675840 Creating detection images in sub-task 3642519d-7d94-4271-ac59-47a077d63d16
INFO 2025-11-11 14:14:48,368 models 1 281473805675840 Updating pre-calculated fields for 0 events
INFO 2025-11-11 14:14:48,385 models 1 281473805675840 Saved results from pipeline #11 "Moth / Non-Moth Classifier" (moth_binary) v1 in 78.06 seconds
✓ save_results() completed successfully

Done! Results saved for job 26. Check the database for Detection and Classification **records.**

Checklist

  • I have tested these changes appropriately.
  • I have added and/or modified relevant tests.
  • I updated relevant documentation or comments.
  • I have verified that this PR follows the project's coding standards.
  • Any dependent changes have already been merged to main.

Summary by CodeRabbit

  • Tests
    • Added a management command to validate and test pipeline results processing using JSON test data. It loads a target pipeline and project context, validates input, runs the pipeline results save flow, and reports detailed success or error output for troubleshooting.

@netlify
Copy link

netlify bot commented Nov 5, 2025

Deploy Preview for antenna-preview canceled.

Name Link
🔨 Latest commit aa9cd61
🔍 Latest deploy log https://app.netlify.com/projects/antenna-preview/deploys/691395c7d5ff3f0008cbc928

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 5, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds a new Django management command test_save_results.py that loads a Pipeline, reads and validates a JSON file against PipelineResultsResponse, creates a minimal test Job for a Project, calls Pipeline.save_results(..., job_id=...), and prints success or error messages.

Changes

Cohort / File(s) Change Summary
New Management Command
ami/ml/management/commands/test_save_results.py
Introduces Command (subclass of django.core.management.base.BaseCommand) to: parse pipeline_id, json_file, project_id; load the Pipeline; read and validate JSON via PipelineResultsResponse; create a minimal test Job linked to the specified Project; invoke Pipeline.save_results(results, job_id=...); and emit detailed stdout/stderr messages.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI as management command
    participant DB as Database
    participant PipelineSvc as Pipeline model
    participant Validator as PipelineResultsResponse

    User->>CLI: run test_save_results(pipeline_id, json_file, project_id)
    CLI->>DB: load Pipeline(pipeline_id)
    DB-->>CLI: Pipeline instance
    CLI->>CLI: read json_file -> results
    CLI->>Validator: validate(results)
    alt valid
        Validator-->>CLI: validated results
        CLI->>DB: create minimal Job(project_id)
        DB-->>CLI: Job created (job_id)
        CLI->>PipelineSvc: Pipeline.save_results(results, job_id)
        PipelineSvc-->>DB: persist results
        DB-->>PipelineSvc: success
        PipelineSvc-->>CLI: success
        CLI->>User: print success
    else invalid
        Validator-->>CLI: validation errors
        CLI->>User: print validation errors
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify argument parsing and helpful error messages for pipeline_id, json_file, and project_id.
  • Check JSON read/parse error handling and schema validation against PipelineResultsResponse.
  • Inspect creation of the minimal Job and its Project association.
  • Confirm correct call to Pipeline.save_results(...), handling of returned errors/exceptions, and completeness of stdout logging.

Poem

🐰 I hopped in with a JSON sack,
To test a pipeline's gentle track,
A tiny job I set with care,
Saved results and danced in air,
Debug carrots scattered everywhere. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a command to test Pipeline.save_results() functionality, which matches the core purpose of the changeset.
Description check ✅ Passed The description includes Summary, List of Changes, and Screenshots sections with a complete checklist, though How to Test and Deployment Notes sections are missing.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0968d94 and aa9cd61.

📒 Files selected for processing (1)
  • ami/ml/management/commands/test_save_results.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
ami/ml/management/commands/test_save_results.py (1)
ami/jobs/models.py (1)
  • MLJob (316-513)
🪛 Ruff (0.14.4)
ami/ml/management/commands/test_save_results.py

42-42: Unused method argument: args

(ARG002)


51-51: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


51-51: Avoid specifying long messages outside the exception class

(TRY003)


58-58: Avoid specifying long messages outside the exception class

(TRY003)


67-67: Do not catch blind exception: Exception

(BLE001)


68-68: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


68-68: Avoid specifying long messages outside the exception class

(TRY003)


80-80: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


80-80: Avoid specifying long messages outside the exception class

(TRY003)


96-96: Do not catch blind exception: Exception

(BLE001)


97-97: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


97-97: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test
🔇 Additional comments (3)
ami/ml/management/commands/test_save_results.py (3)

1-9: LGTM - Docstring updated correctly.

The docstring now accurately reflects the command interface with the correct positional arguments and no mention of the non-existent --job-id option.


77-80: LGTM - Project error handling added.

The Project.DoesNotExist exception is now properly handled with a user-friendly error message, consistent with the Pipeline lookup pattern.


55-104: LGTM - Test command logic is sound.

The JSON loading, validation, job creation, and save_results invocation are all implemented correctly. The broad exception catches at lines 67 and 96 (flagged by static analysis) are acceptable for a diagnostic command where you want to capture and report any validation or runtime errors to the user.

The minimal job creation is appropriate since this command only tests save_results() rather than running a full ML job pipeline.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds a new Django management command test_save_results to facilitate manual testing and debugging of the Pipeline.save_results() method. The command loads pipeline results from a JSON file, validates them against the PipelineResultsResponse schema, creates a test job, and executes save_results() to store the data in the database.

Key Changes

  • New management command that accepts pipeline ID, JSON file path, and project ID as arguments
  • Validates input data using Pydantic schemas before processing
  • Creates a test job associated with the specified project and pipeline

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 58af519 and 0968d94.

📒 Files selected for processing (1)
  • ami/ml/management/commands/test_save_results.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
ami/ml/management/commands/test_save_results.py (1)
ami/jobs/models.py (1)
  • MLJob (316-513)
🪛 Ruff (0.14.4)
ami/ml/management/commands/test_save_results.py

43-43: Unused method argument: args

(ARG002)


52-52: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


52-52: Avoid specifying long messages outside the exception class

(TRY003)


59-59: Avoid specifying long messages outside the exception class

(TRY003)


68-68: Do not catch blind exception: Exception

(BLE001)


69-69: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


69-69: Avoid specifying long messages outside the exception class

(TRY003)


94-94: Do not catch blind exception: Exception

(BLE001)


95-95: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


95-95: Avoid specifying long messages outside the exception class

(TRY003)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Agent
🔇 Additional comments (3)
ami/ml/management/commands/test_save_results.py (3)

12-21: LGTM!

Imports are well-organized and all appear necessary for the command's functionality.


26-41: LGTM!

Arguments are properly defined with appropriate types and helpful descriptions.


80-87: LGTM!

Test job creation is properly configured with all necessary fields.

Copy link
Collaborator

@mihow mihow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @carlosgjs! There is stalled PR in draft (#916) that adds a comprehensive method for importing pipeline results from JSON files, for data that is processed externally. When that PR is resumed, we can probably merge the two implementations!

@mihow mihow merged commit 6caa37b into RolnickLab:main Nov 13, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants