Skip to content

Commit

Permalink
Define new attribute for Annotation model to attach multiple labels t…
Browse files Browse the repository at this point in the history
…o the annotation response
  • Loading branch information
gkourie committed Dec 19, 2024
1 parent f441d15 commit a6410b3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
15 changes: 15 additions & 0 deletions app/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -209,4 +214,14 @@ public function getFile(): VolumeFile
{
return $this->file;
}

/**
* Get the LabelBOT suggested labels.
*
* @return array<int>
*/
public function getLabelBOTLabelsAttribute(): array
{
return $this->labelBOTLabels;
}
}
3 changes: 1 addition & 2 deletions app/Http/Controllers/Api/ImageAnnotationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions app/ImageAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class ImageAnnotation extends Annotation
'points' => 'array',
];

/**
* The attributes that should be included in the JSON response.
*
* @var array<int, string>
*/
protected $appends = ['labelBOTLabels'];

/**
* The image, this annotation belongs to.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a6410b3

Please sign in to comment.