-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtests.py
More file actions
1544 lines (1301 loc) · 66.6 KB
/
tests.py
File metadata and controls
1544 lines (1301 loc) · 66.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import concurrent.futures
import datetime
import pathlib
import unittest
import uuid
from django.test import TestCase
from rest_framework.test import APIRequestFactory, APITestCase
from ami.base.serializers import reverse_with_params
from ami.main.models import (
Classification,
Deployment,
Detection,
Event,
Project,
SourceImage,
SourceImageCollection,
Taxon,
group_images_into_events,
)
from ami.ml.models import Algorithm, Pipeline, ProcessingService
from ami.ml.models.pipeline import collect_images, get_or_create_algorithm_and_category_map, save_results
from ami.ml.post_processing.small_size_filter import SmallSizeFilterTask
from ami.ml.schemas import (
AlgorithmConfigResponse,
AlgorithmReference,
BoundingBox,
ClassificationResponse,
DetectionResponse,
PipelineResultsResponse,
SourceImageResponse,
)
from ami.tests.fixtures.main import (
create_captures_from_files,
create_processing_service,
create_taxa,
setup_test_project,
)
from ami.tests.fixtures.ml import ALGORITHM_CHOICES
from ami.users.models import User
class TestProcessingServiceAPI(APITestCase):
"""
Test the Processing Services API endpoints.
"""
def setUp(self):
self.project = Project.objects.create(name="Processing Service Test Project")
self.user = User.objects.create_user( # type: ignore
email="testuser@insectai.org",
is_staff=True,
)
self.factory = APIRequestFactory()
def _create_processing_service(self, name: str, endpoint_url: str):
processing_services_create_url = reverse_with_params(
"api:processingservice-list", params={"project_id": self.project.pk}
)
self.client.force_authenticate(user=self.user)
processing_service_data = {
"name": name,
"endpoint_url": endpoint_url,
}
resp = self.client.post(processing_services_create_url, processing_service_data)
self.client.force_authenticate(user=None)
self.assertEqual(resp.status_code, 201)
return resp.json()["instance"]
def _delete_processing_service(self, processing_service_id: int):
processing_services_delete_url = reverse_with_params(
"api:processing-service-detail", kwargs={"pk": processing_service_id}
)
self.client.force_authenticate(user=self.user)
resp = self.client.delete(processing_services_delete_url)
self.client.force_authenticate(user=None)
self.assertEqual(resp.status_code, 204)
return resp
def _register_pipelines(self, processing_service_id):
processing_services_register_pipelines_url = reverse_with_params(
"api:processingservice-register-pipelines", args=[processing_service_id]
)
self.client.force_authenticate(user=self.user)
resp = self.client.post(processing_services_register_pipelines_url)
data = resp.json()
self.assertEqual(data["success"], True)
return data
def test_create_processing_service(self):
self._create_processing_service(
name="Processing Service Test",
endpoint_url="http://processing_service:2000",
)
def test_project_was_added(self):
response = self._create_processing_service(
name="Processing Service Test",
endpoint_url="http://processing_service:2000",
)
processing_service_id = response["id"]
processing_service = ProcessingService.objects.get(pk=processing_service_id)
self.assertIn(self.project, processing_service.projects.all())
def test_processing_service_pipeline_registration(self):
# register a processing service
response = self._create_processing_service(
name="Processing Service Test",
endpoint_url="http://processing_service:2000",
)
processing_service_id = response["id"]
# sync the processing service to create/add the associate pipelines
response = self._register_pipelines(processing_service_id)
processing_service = ProcessingService.objects.get(pk=processing_service_id)
pipelines_queryset = processing_service.pipelines.all()
self.assertEqual(pipelines_queryset.count(), len(response["pipelines"]))
def test_create_processing_service_without_endpoint_url(self):
"""Test creating a ProcessingService without endpoint_url (pull mode)"""
processing_services_create_url = reverse_with_params(
"api:processingservice-list", params={"project_id": self.project.pk}
)
self.client.force_authenticate(user=self.user)
processing_service_data = {
"name": "Pull Mode Service",
"description": "Service without endpoint",
}
resp = self.client.post(processing_services_create_url, processing_service_data)
self.client.force_authenticate(user=None)
self.assertEqual(resp.status_code, 201)
data = resp.json()
# Check that endpoint_url is null
self.assertIsNone(data["instance"]["endpoint_url"])
# Check that status indicates service is not yet live (no heartbeat received)
self.assertFalse(data["status"]["request_successful"])
self.assertFalse(data["status"]["server_live"])
self.assertIsNone(data["status"]["endpoint_url"])
def test_get_status_with_null_endpoint_url(self):
"""Test get_status method when endpoint_url is None"""
service = ProcessingService.objects.create(name="Pull Mode Service", endpoint_url=None)
service.projects.add(self.project)
status = service.get_status()
self.assertFalse(status.request_successful)
self.assertFalse(status.server_live) # No heartbeat received yet = not live
self.assertIsNone(status.endpoint_url)
self.assertEqual(status.pipelines_online, [])
def test_get_pipeline_configs_with_null_endpoint_url(self):
"""Test get_pipeline_configs method when endpoint_url is None"""
service = ProcessingService.objects.create(name="Pull Mode Service", endpoint_url=None)
configs = service.get_pipeline_configs()
self.assertEqual(configs, [])
class TestProcessingServiceLastSeen(TestCase):
"""Test the last_seen, last_seen_live, and last_seen_latency fields."""
def setUp(self):
self.project = Project.objects.create(name="Last Seen Test Project")
def test_mark_seen_sets_fields(self):
"""Test that mark_seen() sets last_seen and last_seen_live."""
service = ProcessingService.objects.create(name="Async Worker", endpoint_url=None)
service.projects.add(self.project)
self.assertIsNone(service.last_seen)
self.assertIsNone(service.last_seen_live)
service.mark_seen(live=True)
service.refresh_from_db()
self.assertIsNotNone(service.last_seen)
self.assertTrue(service.last_seen_live)
def test_mark_seen_offline(self):
"""Test that mark_seen(live=False) sets last_seen_live to False."""
service = ProcessingService.objects.create(name="Async Worker Offline", endpoint_url=None)
service.mark_seen(live=False)
service.refresh_from_db()
self.assertIsNotNone(service.last_seen)
self.assertFalse(service.last_seen_live)
def test_get_status_updates_last_seen_for_sync_service(self):
"""Test that get_status() updates last_seen fields for sync services (even if endpoint is unreachable)."""
service = ProcessingService.objects.create(name="Sync Service", endpoint_url="http://nonexistent-host:9999")
service.projects.add(self.project)
# get_status should update the fields even for unreachable endpoints
service.get_status(timeout=1)
service.refresh_from_db()
self.assertIsNotNone(service.last_seen)
self.assertFalse(service.last_seen_live) # unreachable = not live
self.assertIsNotNone(service.last_seen_latency)
def test_model_has_last_seen_fields(self):
"""Test that ProcessingService model has last_seen fields and not last_checked."""
service = ProcessingService.objects.create(name="Field Test Service", endpoint_url=None)
service.mark_seen(live=True)
service.refresh_from_db()
# Verify new fields exist
self.assertTrue(hasattr(service, "last_seen"))
self.assertTrue(hasattr(service, "last_seen_live"))
self.assertTrue(hasattr(service, "last_seen_latency"))
# Verify old fields don't exist
self.assertFalse(hasattr(service, "last_checked"))
self.assertFalse(hasattr(service, "last_checked_live"))
self.assertFalse(hasattr(service, "last_checked_latency"))
class TestProjectPipelineRegistrationUpdatesLastSeen(APITestCase):
"""Test that async pipeline registration updates last_seen on the processing service."""
def setUp(self):
from ami.users.roles import ProjectManager, create_roles_for_project
self.user = User.objects.create_user(email="lastseen@example.com") # type: ignore
self.project = Project.objects.create(name="Last Seen Project", owner=self.user, create_defaults=False)
create_roles_for_project(self.project)
ProjectManager.assign_user(self.user, self.project)
def test_pipeline_registration_marks_service_as_seen(self):
"""Test that POSTing to the pipeline registration endpoint marks the service as last_seen_live."""
url = f"/api/v2/projects/{self.project.pk}/pipelines/"
payload = {
"processing_service_name": "AsyncTestWorker",
"pipelines": [],
}
self.client.force_authenticate(user=self.user)
response = self.client.post(url, payload, format="json")
self.assertEqual(response.status_code, 201)
service = ProcessingService.objects.get(name="AsyncTestWorker")
self.assertIsNotNone(service.last_seen)
self.assertTrue(service.last_seen_live)
def test_repeated_registration_updates_last_seen(self):
"""Test that re-registering updates the last_seen timestamp."""
url = f"/api/v2/projects/{self.project.pk}/pipelines/"
payload = {
"processing_service_name": "AsyncTestWorkerRepeat",
"pipelines": [],
}
self.client.force_authenticate(user=self.user)
# First registration
self.client.post(url, payload, format="json")
service = ProcessingService.objects.get(name="AsyncTestWorkerRepeat")
first_seen = service.last_seen
# Second registration
self.client.post(url, payload, format="json")
service.refresh_from_db()
second_seen = service.last_seen
self.assertIsNotNone(first_seen)
self.assertIsNotNone(second_seen)
self.assertGreaterEqual(second_seen, first_seen)
class TestPipelineWithProcessingService(TestCase):
def test_run_pipeline_with_errors_from_processing_service(self):
"""
Run a real pipeline and verify that if an error occurs for one image, the error is logged in job.logs.stderr.
"""
from ami.jobs.models import Job
# Setup test project, images, and job
project, deployment = setup_test_project()
captures = create_captures_from_files(deployment, skip_existing=False)
test_images = [image for image, frame in captures]
processing_service_instance = create_processing_service(project)
pipeline = processing_service_instance.pipelines.all().get(slug="constant")
job = Job.objects.create(project=project, name="Test Job Real Pipeline Error Handling", pipeline=pipeline)
# Simulate an error by passing an invalid image (e.g., missing file or corrupt)
# Here, we manually set the path of one image to a non-existent file
error_image = test_images[0]
error_image.path = "/tmp/nonexistent_image.jpg"
error_image.save()
images = [error_image] + test_images[1:2] # Only two images for brevity
# Run the pipeline and catch any error
try:
pipeline.process_images(images, job_id=job.pk, project_id=project.pk)
except Exception:
pass # Expected if the backend raises
job.refresh_from_db()
stderr_logs = job.logs.stderr
# Check that an error message mentioning the failed image is present
assert any(
"Failed to process" in log for log in stderr_logs
), f"Expected error message in job.logs.stderr, got: {stderr_logs}"
def setUp(self):
self.project, self.deployment = setup_test_project()
self.captures = create_captures_from_files(self.deployment, skip_existing=False)
self.test_images = [image for image, frame in self.captures]
self.processing_service_instance = create_processing_service(self.project)
self.processing_service = self.processing_service_instance
assert self.processing_service_instance.pipelines.exists()
self.pipeline = self.processing_service_instance.pipelines.all().get(slug="constant")
def test_run_pipeline(self):
# Send images to Processing Service to process and return detections
assert self.pipeline
pipeline_response = self.pipeline.process_images(self.test_images, job_id=None, project_id=self.project.pk)
assert pipeline_response.detections
def test_created_category_maps(self):
# Send images to ML backend to process and return detections
assert self.pipeline
pipeline_response = self.pipeline.process_images(self.test_images, project_id=self.project.pk)
save_results(pipeline_response, return_created=True)
source_images = SourceImage.objects.filter(pk__in=[image.id for image in pipeline_response.source_images])
detections = Detection.objects.filter(source_image__in=source_images).select_related(
"detection_algorithm",
"detection_algorithm__category_map",
)
assert detections.count() > 0
for detection in detections:
# No detection algorithm should have category map at this time (but this may change!)
assert detection.detection_algorithm
assert detection.detection_algorithm.category_map is None
# Ensure that all classification algorithms have a category map
classification_taxa = set()
for classification in detection.classifications.all().select_related(
"algorithm",
"algorithm__category_map",
):
assert classification.algorithm is not None
assert classification.category_map is not None
assert classification.algorithm.category_map == classification.category_map
_, top_score = list(classification.predictions(sort=True))[0]
assert top_score == classification.score
top_taxon, top_taxon_score = list(classification.predictions_with_taxa(sort=True))[0]
assert top_taxon == classification.taxon
assert top_taxon_score == classification.score
classification_taxa.add(top_taxon)
# Check the occurrence determination taxon
assert detection.occurrence
assert detection.occurrence.determination in classification_taxa
def test_missing_category_map(self):
# Test that an exception is raised if a classification algorithm is missing a category map
from ami.ml.exceptions import PipelineNotConfigured
# Get the response from the /info endpoint
pipeline_configs = self.processing_service.get_pipeline_configs()
# Assert that there is a least one classification algorithm with a category map
self.assertTrue(
any(
algo.task_type in Algorithm.classification_task_types and algo.category_map is not None
for pipeline in pipeline_configs
for algo in pipeline.algorithms
),
"Expected pipeline to have at least one classification algorithm with a category map",
)
# Remove the category map from one of the classification algorithms
for pipeline_config in pipeline_configs:
for algorithm in pipeline_config.algorithms:
if algorithm.task_type in Algorithm.classification_task_types and algorithm.category_map is not None:
algorithm.category_map = None
# Change the key to ensure it's treated as a new algorithm
algorithm.key = "missing-category-map-classifier"
algorithm.name = "Classifier with Missing Category Map"
break
with self.assertRaises(
PipelineNotConfigured,
msg="Expected an exception to be raised if a classification algorithm is missing a category map",
):
self.processing_service.create_pipelines(pipeline_configs=pipeline_configs)
def test_alignment_of_predictions_and_category_map(self):
# Ensure that the scores and labels are aligned
pipeline = self.processing_service_instance.pipelines.all().get(slug="random-detection-random-species")
pipeline_response = pipeline.process_images(self.test_images, project_id=self.project.pk)
results = save_results(pipeline_response, return_created=True)
assert results is not None, "Expected results to be returned in a PipelineSaveResults object"
assert results.classifications, "Expected classifications to be returned in the results"
for classification in results.classifications:
assert classification.scores
taxa_with_scores = list(classification.predictions_with_taxa(sort=True))
assert taxa_with_scores
assert classification.score == taxa_with_scores[0][1]
assert classification.taxon == taxa_with_scores[0][0]
def test_top_n_alignment(self):
# Ensure that the top_n parameter works
pipeline = self.processing_service_instance.pipelines.all().get(slug="random-detection-random-species")
pipeline_response = pipeline.process_images(self.test_images, project_id=self.project.pk)
results = save_results(pipeline_response, return_created=True)
assert results is not None, "Expecected results to be returned in a PipelineSaveResults object"
assert results.classifications, "Expected classifications to be returned in the results"
for classification in results.classifications:
top_n = classification.top_n(n=3)
assert classification.score == top_n[0]["score"]
assert classification.taxon == top_n[0]["taxon"]
def test_pipeline_reprocessing(self):
"""
Test that reprocessing the same images with differet pipelines does not create duplicate
detections. The 2 pipelines used are a random detection + random species classifier, and a
constant species classifier.
"""
if not self.project.feature_flags.reprocess_existing_detections:
self.project.feature_flags.reprocess_existing_detections = True
self.project.save()
# Process the images once
pipeline_one = self.processing_service_instance.pipelines.all().get(slug="random-detection-random-species")
num_classifiers_pipeline_one = pipeline_one.algorithms.filter(task_type="classification").count()
pipeline_response = pipeline_one.process_images(self.test_images, project_id=self.project.pk)
results = save_results(pipeline_response, return_created=True)
assert results is not None, "Expected results to be returned in a PipelineSaveResults object"
assert results.detections, "Expected detections to be returned in the results"
num_initial_detections = len(results.detections)
# This particular pipeline produces 2 classifications per detection
for det in results.detections:
num_classifications = det.classifications.count()
self.assertEqual(
num_classifications,
num_classifiers_pipeline_one,
f"Expected {num_classifiers_pipeline_one} classifications per detection "
"(random species and random binary classifier).",
)
source_images = SourceImage.objects.filter(pk__in=[image.id for image in pipeline_response.source_images])
detections = Detection.objects.filter(source_image__in=source_images).select_related(
"detection_algorithm",
"detection_algorithm__category_map",
)
initial_detection_ids = sorted([det.pk for det in detections])
assert detections.count() > 0
# Reprocess the same images using a different pipeline
pipeline_two = self.processing_service_instance.pipelines.all().get(slug="constant")
num_classifiers_pipeline_two = pipeline_two.algorithms.filter(task_type="classification").count()
pipeline_response = pipeline_two.process_images(self.test_images, project_id=self.project.pk)
reprocessed_results = save_results(pipeline_response, return_created=True)
assert reprocessed_results is not None, "Expected results to be returned in a PipelineSaveResults object"
assert reprocessed_results.detections, "Expected detections to be returned in the results"
num_reprocessed_detections = len(reprocessed_results.detections)
self.assertEqual(
num_reprocessed_detections,
num_initial_detections,
"Expected the same number of detections after reprocessing with a different pipeline.",
)
source_images = SourceImage.objects.filter(pk__in=[image.id for image in pipeline_response.source_images])
detections = Detection.objects.filter(source_image__in=source_images).select_related(
"detection_algorithm",
"detection_algorithm__category_map",
)
# Check detections were re-processed, and not re-created
reprocessed_detection_ids = sorted([det.pk for det in detections])
assert initial_detection_ids == reprocessed_detection_ids, (
"Expected the same detections to be returned after reprocessing with a different pipeline, "
f"but found {initial_detection_ids} != {reprocessed_detection_ids}"
)
# The constant pipeline produces 1 classification per detection (added to the existing classifications)
for detection in detections:
self.assertEqual(
detection.classifications.count(),
num_classifiers_pipeline_one + num_classifiers_pipeline_two,
f"Expected {num_classifiers_pipeline_one + num_classifiers_pipeline_two} "
"classifications per detection (2 random classifiers + constant classifier).",
)
class TestPipeline(TestCase):
def setUp(self):
self.project = Project.objects.create(name="Test Project")
# Create test images and collection
self.test_images = [
SourceImage.objects.create(path="test1-20240101000000.jpg"),
SourceImage.objects.create(path="test2-20240101001000.jpg"),
]
self.image_collection = SourceImageCollection.objects.create(
name="Test Collection",
project=self.project,
)
self.image_collection.images.set(self.test_images)
# Create test pipeline and algorithms
self.pipeline = Pipeline.objects.create(
name="Test Pipeline (Random)",
)
self.pipeline_two = Pipeline.objects.create(
name="Test Pipeline (Constant)",
)
self.algorithms = {
key: get_or_create_algorithm_and_category_map(val) for key, val in ALGORITHM_CHOICES.items()
}
self.pipeline.algorithms.set(
[
self.algorithms["random-detector"],
self.algorithms["random-binary-classifier"],
self.algorithms["random-species-classifier"],
]
)
self.pipeline_two.algorithms.set(
[
self.algorithms["random-detector"],
self.algorithms["random-binary-classifier"],
self.algorithms["constant-species-classifier"],
]
)
def test_create_pipeline(self):
assert self.pipeline.slug.startswith("test-pipeline")
self.assertEqual(self.pipeline.algorithms.count(), 3)
self.assertEqual(self.pipeline_two.algorithms.count(), 3)
for algorithm in self.pipeline.algorithms.all():
assert isinstance(algorithm, Algorithm)
self.assertIn(algorithm.key, [algo.key for algo in ALGORITHM_CHOICES.values()])
def test_collect_images(self):
images = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
assert len(images) == 2
def fake_pipeline_results(
self,
source_images: list[SourceImage],
pipeline: Pipeline,
alt_species_classifier: AlgorithmConfigResponse | None = None,
):
# @TODO use the pipeline passed in to get the algorithms
source_image_results = [SourceImageResponse(id=image.pk, url=image.path) for image in source_images]
detector = ALGORITHM_CHOICES["random-detector"]
binary_classifier = ALGORITHM_CHOICES["random-binary-classifier"]
assert binary_classifier.category_map
if alt_species_classifier is None:
species_classifier = ALGORITHM_CHOICES["random-species-classifier"]
else:
species_classifier = alt_species_classifier
assert species_classifier.category_map
detection_results = [
DetectionResponse(
source_image_id=image.pk,
bbox=BoundingBox(x1=0.0, y1=0.0, x2=1.0, y2=1.0),
inference_time=0.4,
algorithm=AlgorithmReference(
name=detector.name,
key=detector.key,
),
timestamp=datetime.datetime.now(),
classifications=[
ClassificationResponse(
classification=binary_classifier.category_map.labels[0],
labels=binary_classifier.category_map.labels,
scores=[0.9213],
algorithm=AlgorithmReference(
name=binary_classifier.name,
key=binary_classifier.key,
),
timestamp=datetime.datetime.now(),
terminal=False,
),
ClassificationResponse(
classification=species_classifier.category_map.labels[0],
labels=species_classifier.category_map.labels,
scores=[0.64333],
algorithm=AlgorithmReference(
name=species_classifier.name,
key=species_classifier.key,
),
timestamp=datetime.datetime.now(),
terminal=True,
),
],
)
for image in self.test_images
]
fake_results = PipelineResultsResponse(
pipeline=pipeline.slug,
algorithms={
detector.key: detector,
binary_classifier.key: binary_classifier,
species_classifier.key: species_classifier,
},
total_time=0.01,
source_images=source_image_results,
detections=detection_results,
)
return fake_results
def test_save_results(self):
results = self.fake_pipeline_results(self.test_images, self.pipeline)
save_results(results)
for image in self.test_images:
image.save()
self.assertEqual(image.detections_count, 1)
# @TODO test the cached counts for detections, etc are updated on Events, Deployments, etc.
def test_skip_existing_when_all_matching(self):
"""
When processing images, skip images that have already been processed by the same set of algorithms.
(must be the same detection algorithm and all classification algorithms)
"""
images = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
total_images = len(images)
self.assertEqual(total_images, self.image_collection.images.count())
created = save_results(self.fake_pipeline_results(images, self.pipeline), return_created=True)
assert created, "Expected created objects to be returned in a PipelineSaveResults object"
# Collect all detection algorithms used on the detections
detections = created.detections
detection_algos_used = {
detection.detection_algorithm.name for detection in detections if detection.detection_algorithm
}
# detection_algos_used = set(Detection.objects.all().values_list("detection_algorithm__name", flat=True))
# Assert it was only one algorithm, and it was the one we used
self.assertEqual(
detection_algos_used, {self.algorithms["random-detector"].name}, "Wrong detection algorithm used."
)
# Collect all classification algorithms used on the classifications
classifications = created.classifications
classification_algos_used = {
classification.algorithm.name for classification in classifications if classification.algorithm
}
# classification_algos_used = set(Classification.objects.all().values_list("algorithm__name", flat=True))
# Assert it was only one algorithm, and it was the one we used
self.assertEqual(
classification_algos_used,
{self.algorithms["random-species-classifier"].name, self.algorithms["random-binary-classifier"].name},
"Wrong classification algorithms used.",
)
images_again = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
remaining_images_to_process = len(images_again)
self.assertEqual(remaining_images_to_process, 0)
def test_skip_existing_with_new_detector(self):
images = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
total_images = len(images)
self.assertEqual(total_images, self.image_collection.images.count())
pipeline_response = self.fake_pipeline_results(images, self.pipeline)
save_results(pipeline_response)
# Find the fist algo used where task_type is classification
classifiers = [algo for algo in pipeline_response.algorithms.values() if algo.task_type == "classification"]
last_classifier = Algorithm.objects.get(key=classifiers[-1].key)
self.pipeline.algorithms.set(
[
Algorithm.objects.create(name="NEW Object Detector 2.0"),
last_classifier,
]
)
images_again = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
remaining_images_to_process = len(images_again)
self.assertEqual(remaining_images_to_process, total_images)
@unittest.skip("Not implemented yet")
def test_skip_existing_with_new_classifier(self):
"""
@TODO add support for skipping the detection model if only the classifier has changed.
"""
images = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
total_images = len(images)
self.assertEqual(total_images, self.image_collection.images.count())
pipeline_response = self.fake_pipeline_results(images, self.pipeline)
# Find the fist algo used where task_type is detection
first_detector_in_response = next(
algo for algo in pipeline_response.algorithms.values() if algo.task_type == "detection"
)
first_detector = Algorithm.objects.get(key=first_detector_in_response.key)
save_results(pipeline_response)
self.pipeline.algorithms.set(
[
first_detector,
Algorithm.objects.create(name="NEW Classifier 2.0"),
]
)
images_again = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
remaining_images_to_process = len(images_again)
self.assertEqual(remaining_images_to_process, total_images)
@unittest.skip("Not implemented yet")
def test_skip_existing_per_batch_during_processing(self):
# Send the same batch to two simultaneous processing pipelines
# @TODO this needs to test the `process_images()` function with a real pipeline
# @TODO enable test when a pipeline is added to the CI environment in PR #576
pass
def test_unknown_algorithm_returned_by_processing_service(self):
"""
Test that unknown algorithms returned by the processing service are handled correctly.
Previously we allowed unknown algorithms to be returned by the pipeline,
now all algorithms must be registered first from the processing service's /info
endpoint.
"""
fake_results = self.fake_pipeline_results(self.test_images, self.pipeline)
new_detector = AlgorithmConfigResponse(
name="Unknown Detector 5.1b-mobile", key="unknown-detector", task_type="detection"
)
new_classifier = AlgorithmConfigResponse(
name="Unknown Classifier 3.0b-mega", key="unknown-classifier", task_type="classification"
)
fake_results.algorithms[new_detector.key] = new_detector
fake_results.algorithms[new_classifier.key] = new_classifier
for detection in fake_results.detections:
detection.algorithm = AlgorithmReference(name=new_detector.name, key=new_detector.key)
for classification in detection.classifications:
classification.algorithm = AlgorithmReference(name=new_classifier.name, key=new_classifier.key)
current_total_algorithm_count = Algorithm.objects.count()
# Ensure an exception is raised that a new algorithm was not
# pre-registered from the /info endpoint
from ami.ml.exceptions import PipelineNotConfigured
with self.assertRaises(PipelineNotConfigured):
save_results(fake_results)
# Ensure no new algorithms were added to the database
new_algorithm_count = Algorithm.objects.count()
self.assertEqual(new_algorithm_count, current_total_algorithm_count)
# Ensure new algorithms were also added to the pipeline
def test_yes_reprocess_if_new_terminal_algorithm_same_intermediate(self):
"""
Test two pipelines with the same detector and same moth/non-moth classifier, but a new species classifier.
The first pipeline should process the images and save the results.
The second pipeline should reprocess the images.
"""
images = list(collect_images(collection=self.image_collection, pipeline=self.pipeline))
assert len(images), "No images to process"
detector = Algorithm.objects.get(key="random-detector")
binary_classifier = Algorithm.objects.get(key="random-binary-classifier")
old_species_classifier = Algorithm.objects.get(key="random-species-classifier")
Detection.objects.all().delete()
results = save_results(self.fake_pipeline_results(images, self.pipeline), return_created=True)
assert results is not None, "Expecected results to be returned in a PipelineSaveResults object"
for raw_detection in results.detections:
self.assertEqual(raw_detection.detection_algorithm, detector)
# Ensure all results have the binary classifier and the old species classifier
for saved_detection in Detection.objects.all():
self.assertEqual(saved_detection.detection_algorithm, detector)
# Assert that the binary classifier was used
self.assertTrue(
saved_detection.classifications.filter(algorithm=binary_classifier).exists(),
"Binary classifier not used in first run",
)
# Assert that the old species classifier was used
self.assertTrue(
saved_detection.classifications.filter(algorithm=old_species_classifier).exists(),
"Old species classifier not used in first run",
)
# Get another species classifier
new_species_classifier_key = "constant-species-classifier"
new_species_classifier = Algorithm.objects.get(key=new_species_classifier_key)
# new_species_classifier_response = ALGORITHM_CHOICES[new_species_classifier_key]
# Create a new pipeline with the same detector and the new species classifier
new_pipeline = Pipeline.objects.create(
name="New Pipeline",
)
new_pipeline.algorithms.set(
[
detector,
binary_classifier,
new_species_classifier,
]
)
# Process the images with the new pipeline
images_again = list(collect_images(collection=self.image_collection, pipeline=new_pipeline))
remaining_images_to_process = len(images_again)
self.assertEqual(remaining_images_to_process, len(images), "Images not re-processed with new pipeline")
def test_project_pipeline_config(self):
"""
Test the default_config for a pipeline, as well as the project pipeline config.
Ensure the project pipeline parameters override the pipeline defaults.
"""
from ami.ml.models import ProjectPipelineConfig
from ami.ml.schemas import PipelineRequestConfigParameters
# Add config to the pipeline & project
self.pipeline.default_config = PipelineRequestConfigParameters({"test_param": "test_value"})
self.pipeline.save()
self.project_pipeline_config = ProjectPipelineConfig.objects.create(
project=self.project,
pipeline=self.pipeline,
config={"test_param": "project_value"},
)
self.project_pipeline_config.save()
# Check the final config
default_config = self.pipeline.get_config()
self.assertEqual(default_config["test_param"], "test_value")
final_config = self.pipeline.get_config(self.project.pk)
self.assertEqual(final_config["test_param"], "project_value")
def test_image_with_null_detection(self):
"""
Test saving results for a pipeline that returns null detections for some images.
"""
image = self.test_images[0]
results = self.fake_pipeline_results([image], self.pipeline)
# Manually change the results for a single image to a list of empty detections
results.detections = []
save_results(results)
image.save()
self.assertEqual(image.get_detections_count(), 0) # detections_count should exclude null detections
total_num_detections = image.detections.distinct().count()
self.assertEqual(total_num_detections, 1)
was_processed = image.get_was_processed()
self.assertEqual(was_processed, True)
# Also test filtering by algorithm
was_processed = image.get_was_processed(algorithm_key="random-detector")
self.assertEqual(was_processed, True)
def test_filter_processed_images_skips_null_only_image(self):
"""
An image with only null detections (processed, nothing found) should be
skipped by filter_processed_images — it doesn't need reprocessing.
"""
from ami.ml.models.pipeline import filter_processed_images
image = self.test_images[0]
detector = self.algorithms["random-detector"]
# Simulate a previous run that found nothing: create a null detection
Detection.objects.create(
source_image=image,
detection_algorithm=detector,
bbox=None,
)
result = list(filter_processed_images([image], self.pipeline))
self.assertEqual(result, [], "Image with only null detections should be skipped")
def test_filter_processed_images_yields_image_with_null_and_real_unclassified(self):
"""
An image with BOTH a null detection AND a real detection lacking classifications
should NOT be skipped — the real detection still needs to be classified.
"""
from ami.ml.models.pipeline import filter_processed_images
image = self.test_images[0]
detector = self.algorithms["random-detector"]
# Null detection from a prior empty run
Detection.objects.create(
source_image=image,
detection_algorithm=detector,
bbox=None,
)
# Real detection with no classification yet
Detection.objects.create(
source_image=image,
detection_algorithm=detector,
bbox=[0.1, 0.2, 0.3, 0.4],
)
result = list(filter_processed_images([image], self.pipeline))
self.assertEqual(result, [image], "Image with real unclassified detections should be yielded")
def test_filter_processed_images_skips_null_and_fully_classified(self):
"""
An image with a null detection AND a real detection that is fully classified
by all pipeline algorithms should be skipped — it's fully processed.
"""
from ami.ml.models.pipeline import filter_processed_images
image = self.test_images[0]
detector = self.algorithms["random-detector"]
binary_classifier = self.algorithms["random-binary-classifier"]
species_classifier = self.algorithms["random-species-classifier"]
# Null detection from a prior empty run
Detection.objects.create(
source_image=image,
detection_algorithm=detector,
bbox=None,
)
# Real detection with classifications from all pipeline algorithms
real_det = Detection.objects.create(
source_image=image,
detection_algorithm=detector,
bbox=[0.1, 0.2, 0.3, 0.4],
)
taxon = Taxon.objects.create(name="Test Species Filtered")
Classification.objects.create(
detection=real_det,
taxon=taxon,
algorithm=binary_classifier,
score=0.9,
timestamp=datetime.datetime.now(),
)
Classification.objects.create(
detection=real_det,
taxon=taxon,
algorithm=species_classifier,
score=0.8,
timestamp=datetime.datetime.now(),
)
result = list(filter_processed_images([image], self.pipeline))
self.assertEqual(result, [], "Fully classified image with null detection should be skipped")
def test_null_detections_are_algorithm_specific(self):
"""
Null detections from different pipelines/algorithms should not be shared.
Each algorithm's null detection is tracked separately so that
get_was_processed(algorithm_key=...) returns the correct per-algorithm status.
"""
from ami.ml.models.pipeline import save_results
image = self.test_images[0]
# Pipeline 1 processes image, finds nothing
results_1 = self.fake_pipeline_results([image], self.pipeline)
results_1.detections = []
save_results(results_1)
# Create a second pipeline with a DIFFERENT detector algorithm
detector_2, _ = Algorithm.objects.get_or_create(
key="constant-detector",
defaults={"name": "Constant Detector", "task_type": "detection"},
)
pipeline_2 = Pipeline.objects.create(name="Test Pipeline 2 Null Detect")
pipeline_2.algorithms.set([detector_2])
# Pipeline 2 processes the same image, also finds nothing
results_2 = self.fake_pipeline_results([image], pipeline_2)
results_2.detections = []
save_results(results_2)
# Both algorithms should independently mark the image as processed
detector_1_key = self.algorithms["random-detector"].key
self.assertTrue(image.get_was_processed(algorithm_key=detector_1_key))
self.assertTrue(
image.get_was_processed(algorithm_key="constant-detector"),
"Pipeline 2's null detection should be created separately",
)
# Each pipeline must have its own null detection in the DB
null_detections = image.detections.filter(bbox__isnull=True)