-
Notifications
You must be signed in to change notification settings - Fork 14
Fix null detections in exports & API. Don't mark images as processed too soon #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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68ef784
fix(ml): create null detection markers only after real saves succeed
mihow b3c64bf
test(ml): RED test for broker-outage leaving null marker
mihow 9fc34da
fix(ml): move null-marker creation to final step in save_results
mihow 8fbd51f
refactor(main): add null-marker abstraction on Detection
mihow a0e6048
refactor(main): sweep NULL_DETECTIONS_FILTER call sites to .valid()/.…
mihow 9dfb913
fix(main): tighten OccurrenceQuerySet.valid to exclude phantoms
mihow c207ae5
feat(main): cleanup_null_only_occurrences management command
mihow 8e4bbf3
fix(main,ml): apply CodeRabbit review feedback
mihow fef1cb6
refactor(main): rename 'orphan' null markers to 'dangling'; test null…
mihow 8c019e4
fix(main): scope cleanup to true #1310 phantoms; drop dead bbox=[] se…
mihow a87133d
docs(ml): explain what existing_detection holds in each get_or_create…
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
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
| """ | ||
| Delete phantom Occurrences and dangling 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 (their only detections are null-marker | ||
| sentinels, or they have none at all), 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 dangling 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) | ||
| # Phantom = an occurrence with NO real (valid) detection backing it: its only detections | ||
| # are null-marker sentinels, or it has none at all. This is the Issue #1310 debris. | ||
| # | ||
| # Deliberately narrower than Occurrence.valid(): valid() ALSO excludes occurrences whose | ||
| # determination is null, but an occurrence that has a real detection and merely a missing | ||
| # determination is a different (partial-write) shape, not #1310 debris. Deleting it would | ||
| # SET_NULL the real detection's occurrence FK (Detection.occurrence is on_delete=SET_NULL), | ||
| # stranding a classified detection on an image that filter_processed_images then skips | ||
| # forever. Those are left for a separate, targeted repair. | ||
| has_valid_detection = Exists(Detection.objects.valid().filter(occurrence_id=OuterRef("pk"))) | ||
| phantom_occs = all_occs.exclude(has_valid_detection) | ||
|
|
||
| has_valid_detection = Detection.objects.valid().filter(source_image_id=OuterRef("source_image_id")) | ||
| dangling_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 = dangling_null_markers.count() | ||
|
|
||
| self.stdout.write(f"Project #{project.pk} ({project.name}):") | ||
| self.stdout.write(f" Phantom occurrences (no real detection backing them): {phantom_count}") | ||
| self.stdout.write(f" Dangling 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(): | ||
| dangling_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} dangling 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.
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.