-
Notifications
You must be signed in to change notification settings - Fork 14
fix(ml): create null detection markers only after real saves succeed #1312
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
mihow
wants to merge
9
commits into
main
Choose a base branch
from
fix/premptive-processed-marker
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
9 commits
Select commit
Hold shift + click to select a range
4e33f96
fix(ml): create null detection markers only after real saves succeed
mihow 356d495
docs(planning): PR #1312 null-marker followup plan + session notes
mihow 2b782d3
test(ml): RED test for broker-outage leaving null marker
mihow 08103da
fix(ml): move null-marker creation to final step in save_results
mihow 1c108bd
refactor(main): add null-marker abstraction on Detection
mihow 2554e62
refactor(main): sweep NULL_DETECTIONS_FILTER call sites to .valid()/.…
mihow 8d7e060
fix(main): tighten OccurrenceQuerySet.valid to exclude phantoms
mihow e5f004a
feat(main): cleanup_null_only_occurrences management command
mihow dda794f
fix(main,ml): apply CodeRabbit review feedback
mihow 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
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
86 changes: 86 additions & 0 deletions
86
ami/main/management/commands/cleanup_null_only_occurrences.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,86 @@ | ||
| """ | ||
| Delete phantom Occurrences and orphan null-marker Detections left by the Issue #1310 | ||
| field bug, on a per-project basis. | ||
|
|
||
| The bug created two categories of rows that should never have been persisted: | ||
| - Occurrence rows with no real detections (or with determination=NULL), surfaced as | ||
| ghost rows in the API. | ||
| - Detection rows that mark a SourceImage as "processed" while no real detections | ||
| exist for it — these prevent filter_processed_images from re-yielding the image | ||
| on the next ML run. | ||
|
|
||
| After cleanup, the source images become eligible for re-processing. | ||
|
|
||
| Dry-run by default. Pass --commit to delete. | ||
| """ | ||
|
|
||
| from django.core.management.base import BaseCommand, CommandError | ||
| from django.db import transaction | ||
| from django.db.models import Exists, OuterRef | ||
|
|
||
| from ami.main.models import Detection, Occurrence, Project | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Delete phantom Occurrences and orphan null-marker Detections (Issue #1310)." | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--project", | ||
| type=int, | ||
| required=True, | ||
| help="Project ID to clean up.", | ||
| ) | ||
| parser.add_argument( | ||
| "--commit", | ||
| action="store_true", | ||
| help="Actually delete. Defaults to dry-run.", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| project_id: int = options["project"] | ||
| commit: bool = options["commit"] | ||
|
|
||
| try: | ||
| project = Project.objects.get(pk=project_id) | ||
| except Project.DoesNotExist as err: | ||
| raise CommandError(f"Project {project_id} does not exist") from err | ||
|
|
||
| all_occs = Occurrence.objects.filter(project=project) | ||
| valid_occs = all_occs.valid() | ||
| phantom_occs = all_occs.exclude(pk__in=valid_occs.values("pk")) | ||
|
|
||
| has_valid_detection = Detection.objects.valid().filter(source_image_id=OuterRef("source_image_id")) | ||
| orphan_null_markers = ( | ||
| Detection.objects.filter(source_image__project=project) | ||
| .null_markers() | ||
| .annotate(_has_valid=Exists(has_valid_detection)) | ||
| .filter(_has_valid=False) | ||
| ) | ||
|
|
||
| phantom_count = phantom_occs.count() | ||
| null_count = orphan_null_markers.count() | ||
|
|
||
| self.stdout.write(f"Project #{project.pk} ({project.name}):") | ||
| self.stdout.write(f" Phantom occurrences (no valid detection or null determination): {phantom_count}") | ||
| self.stdout.write(f" Orphan null-marker detections on images with no real detections: {null_count}") | ||
|
|
||
| if phantom_count == 0 and null_count == 0: | ||
| self.stdout.write(self.style.SUCCESS("Nothing to clean up.")) | ||
| return | ||
|
|
||
| if not commit: | ||
| self.stdout.write(self.style.WARNING("Dry run — pass --commit to delete.")) | ||
| return | ||
|
|
||
| with transaction.atomic(): | ||
| orphan_null_markers.delete() | ||
| phantom_occs.delete() | ||
|
|
||
| # Report the pre-calculated counts of the rows we targeted directly. The tuple from | ||
| # .delete() also counts cascade-deleted related rows (e.g. classifications under a | ||
| # phantom occurrence's detections), which would inflate the numbers and confuse the | ||
| # operator about what the command actually targeted. | ||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Deleted {phantom_count} phantom occurrences and {null_count} orphan null markers.") | ||
| ) |
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.
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.