From a6410b3aad2cc287539155c6e110d8ce7b0e82ec Mon Sep 17 00:00:00 2001 From: gkourie Date: Thu, 19 Dec 2024 17:06:19 +0100 Subject: [PATCH] Define new attribute for Annotation model to attach multiple labels to the annotation response --- app/Annotation.php | 15 +++++++++++++++ .../Controllers/Api/ImageAnnotationController.php | 3 +-- app/ImageAnnotation.php | 7 +++++++ .../Api/ImageAnnotationControllerTest.php | 3 +-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Annotation.php b/app/Annotation.php index 9b6c7d370..e37b6e1db 100644 --- a/app/Annotation.php +++ b/app/Annotation.php @@ -39,6 +39,11 @@ abstract class Annotation extends Model implements AnnotationContract 'points' => 'array', ]; + /** + * The additional labels suggested by the LabelBOT. + */ + public $labelBOTLabels = []; + /** * Scope a query to only include annotations that are visible for a certain user. * @@ -209,4 +214,14 @@ public function getFile(): VolumeFile { return $this->file; } + + /** + * Get the LabelBOT suggested labels. + * + * @return array + */ + public function getLabelBOTLabelsAttribute(): array + { + return $this->labelBOTLabels; + } } diff --git a/app/Http/Controllers/Api/ImageAnnotationController.php b/app/Http/Controllers/Api/ImageAnnotationController.php index ba527ad1e..c384884d8 100644 --- a/app/Http/Controllers/Api/ImageAnnotationController.php +++ b/app/Http/Controllers/Api/ImageAnnotationController.php @@ -258,9 +258,8 @@ public function store(StoreImageAnnotation $request) $annotation->load('labels.label', 'labels.user'); // Attach the other two labels if they exist. - // TODO: Adjust or create an Eloquent. Save FV? for ($i = 1; $i < count($topNLabels); $i++) { - $annotation->{'label_id_' . ($i + 1)} = $topNLabels[$i]; + $annotation->labelBOTLabels[] = $topNLabels[$i]; } return $annotation; diff --git a/app/ImageAnnotation.php b/app/ImageAnnotation.php index 5f01377f6..e935c2b8b 100644 --- a/app/ImageAnnotation.php +++ b/app/ImageAnnotation.php @@ -19,6 +19,13 @@ class ImageAnnotation extends Annotation 'points' => 'array', ]; + /** + * The attributes that should be included in the JSON response. + * + * @var array + */ + protected $appends = ['labelBOTLabels']; + /** * The image, this annotation belongs to. * diff --git a/tests/php/Http/Controllers/Api/ImageAnnotationControllerTest.php b/tests/php/Http/Controllers/Api/ImageAnnotationControllerTest.php index c24f01972..4ad65d1b2 100644 --- a/tests/php/Http/Controllers/Api/ImageAnnotationControllerTest.php +++ b/tests/php/Http/Controllers/Api/ImageAnnotationControllerTest.php @@ -402,8 +402,7 @@ public function testStoreWithFeatureVectorWithoutHNSW() $response->assertSuccessful(); // The feature vector of differentLabel is more similar to the input feature vector // than feature vector of anotherDifferentLabel, so it is ranked higher. - $response->assertJsonFragment(['label_id_2' => $differentLabel->id]); - $response->assertJsonFragment(['label_id_3' => $anotherDifferentLabel->id]); + $response->assertJsonFragment(['labelBOTLabels' => [$differentLabel->id, $anotherDifferentLabel->id]]); } public function testStoreValidatePoints()