-
Notifications
You must be signed in to change notification settings - Fork 14
feat(tracking): revive occurrence tracking as a post-processing task #1272
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
claude/revive-tracking-feature-OyMO3
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 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29707e8
feat(tracking): revive detection tracking as a post-processing task
mihow 45c2f96
fix(tracking): address PR #1272 review feedback
mihow 1e98ea2
feat(tracking): v1 hardening — fresh-event guard, merge-into-first, a…
mihow 6393a71
fix(occurrence): refresh determination_score when taxon unchanged
mihow abcc433
feat(tracking): accept event_ids in tracking task config
mihow 8951d98
feat(tracking): admin form + template for tracking action
mihow 10e2f0f
feat(tracking): EventAdmin action with intermediate confirmation page
mihow a34abf3
refactor(tracking): collection action uses same form + drop collectio…
mihow ded1189
test(tracking): scope-resolution + admin action coverage
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
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,14 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("main", "0083_dedupe_taxalist_names"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunSQL( | ||
| sql="CREATE EXTENSION IF NOT EXISTS vector;", | ||
| reverse_sql="DROP EXTENSION IF EXISTS vector;", | ||
| ), | ||
| ] | ||
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,20 @@ | ||
| from django.db import migrations | ||
| import pgvector.django.vector | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("main", "0084_add_pgvector_extension"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="classification", | ||
| name="features_2048", | ||
| field=pgvector.django.vector.VectorField( | ||
| dimensions=2048, | ||
| null=True, | ||
| help_text="Feature embedding from the model backbone", | ||
| ), | ||
| ), | ||
| ] |
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,23 @@ | ||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("main", "0085_classification_features_2048"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="detection", | ||
| name="next_detection", | ||
| field=models.OneToOneField( | ||
| blank=True, | ||
| help_text="The detection that follows this one in the tracking sequence.", | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="previous_detection", | ||
| to="main.detection", | ||
| ), | ||
| ), | ||
| ] |
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
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
Empty file.
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,99 @@ | ||
| import logging | ||
| from collections import defaultdict | ||
|
|
||
| import numpy as np | ||
| from django.test import TestCase | ||
| from django.utils import timezone | ||
|
|
||
| from ami.main.models import Classification, Detection, Occurrence | ||
| from ami.ml.models import Algorithm | ||
| from ami.ml.post_processing.tracking_task import DEFAULT_TRACKING_PARAMS, assign_occurrences_by_tracking_images | ||
| from ami.tests.fixtures.main import create_captures, create_occurrences, create_taxa, setup_test_project | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TestTracking(TestCase): | ||
| def setUp(self) -> None: | ||
| self.project, self.deployment = setup_test_project(reuse=False) | ||
| # 1 night, 5 captures spaced 1 minute apart so they group into one event. | ||
| create_captures(deployment=self.deployment, num_nights=1, images_per_night=5, interval_minutes=1) | ||
| create_taxa(self.project) | ||
| create_occurrences(deployment=self.deployment, num=6) | ||
|
|
||
| self.event = self.project.events.first() | ||
| assert self.event is not None | ||
| self.source_images = list(self.event.captures.order_by("timestamp")) | ||
|
|
||
| # Source images need dimensions for the cost function. | ||
| for img in self.source_images: | ||
| if not img.width or not img.height: | ||
| img.width = 4096 | ||
| img.height = 2160 | ||
| img.save(update_fields=["width", "height"]) | ||
|
|
||
| self.algorithm = self._assign_mock_features_to_occurrence_detections(self.event) | ||
|
|
||
| # Capture ground-truth groupings so we can compare after re-tracking. | ||
| self.ground_truth_groups = defaultdict(set) | ||
| for occ in Occurrence.objects.filter(event=self.event): | ||
| for det_id in Detection.objects.filter(occurrence=occ).values_list("id", flat=True): | ||
| self.ground_truth_groups[occ.pk].add(det_id) | ||
|
|
||
| Detection.objects.filter(source_image__event=self.event).update(next_detection=None) | ||
|
|
||
| def _assign_mock_features_to_occurrence_detections( | ||
| self, event, algorithm_name: str = "MockTrackingAlgorithm" | ||
| ) -> Algorithm: | ||
| algorithm, _ = Algorithm.objects.get_or_create(name=algorithm_name, key="mock-tracking-algo") | ||
| rng = np.random.default_rng(seed=42) | ||
|
|
||
| for occurrence in event.occurrences.all(): | ||
| base_vector = rng.random(2048) | ||
| for det in occurrence.detections.all(): | ||
| noisy = base_vector + rng.normal(0, 0.001, size=2048) | ||
| Classification.objects.update_or_create( | ||
| detection=det, | ||
| algorithm=algorithm, | ||
| defaults={ | ||
| "timestamp": timezone.now(), | ||
| "features_2048": noisy.tolist(), | ||
| "terminal": True, | ||
| "score": 1.0, | ||
| }, | ||
| ) | ||
| return algorithm | ||
|
|
||
| def test_tracking_reproduces_occurrence_groups(self): | ||
| # Wipe existing chain links and occurrences so tracking has to rebuild them. | ||
| for det in Detection.objects.filter(source_image__event=self.event): | ||
| det.occurrence = None | ||
| det.next_detection = None | ||
| det.save() | ||
| Occurrence.objects.filter(event=self.event).delete() | ||
|
|
||
| assign_occurrences_by_tracking_images( | ||
| event=self.event, | ||
| logger=logger, | ||
| algorithm=self.algorithm, | ||
| params=DEFAULT_TRACKING_PARAMS, | ||
| ) | ||
|
|
||
| new_groups = { | ||
| occ.pk: set(Detection.objects.filter(occurrence=occ).values_list("id", flat=True)) | ||
| for occ in Occurrence.objects.filter(event=self.event) | ||
| } | ||
|
|
||
| self.assertEqual( | ||
| len(new_groups), | ||
| len(self.ground_truth_groups), | ||
| f"Expected {len(self.ground_truth_groups)} groups, got {len(new_groups)}", | ||
| ) | ||
|
|
||
| gt_values = list(self.ground_truth_groups.values()) | ||
| for new_set in new_groups.values(): | ||
| self.assertIn( | ||
| new_set, | ||
| gt_values, | ||
| f"Reconstructed group {new_set} does not match any ground-truth group", | ||
| ) |
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.