Skip to content

Commit

Permalink
Fix ordering of inserted annotation labels from metadata import
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Sep 30, 2024
1 parent d05d5ff commit 7554d48
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Jobs/ImportVolumeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ protected function insertAnnotationChunk(
->take(count($annotations))
->pluck('id')
->reverse()
->values()
->toArray();

foreach ($ids as $index => $id) {
Expand Down
56 changes: 56 additions & 0 deletions tests/php/Jobs/ImportVolumeMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,60 @@ public function testHandleChunkAnnotations()

$this->assertEquals(2, $image->annotations()->count());
}

/*
* This tests if the annotations and their labels match after the import. The check is
* important because annotations and labels are inserted in two different DB
* statements and the ordering is important. The ordering was mixed up at some point
* so the annotations did not get the correct labels.
*/
public function testHandleMultipleOrdering()
{
$image = Image::factory()->create();
$dbUser = DbUser::factory()->create();
$dbLabel = DbLabel::factory()->create();
$dbLabel2 = DbLabel::factory()->create();

$metadata = new VolumeMetadata;
$file = new ImageMetadata($image->filename);
$metadata->addFile($file);
$label = new Label(123, 'my label');
$user = new User(321, 'joe user');
$lau = new LabelAndUser($label, $user);
$file->addFileLabel($lau);
$annotation = new ImageAnnotation(
shape: Shape::point(),
points: [10, 10],
labels: [$lau],
);
$file->addAnnotation($annotation);

$label2 = new Label(321, 'my other label');
$lau2 = new LabelAndUser($label2, $user);
$annotation2 = new ImageAnnotation(
shape: Shape::point(),
points: [10, 10],
labels: [$lau2],
);
$file->addAnnotation($annotation2);

Cache::store('array')->put('metadata-pending-metadata-mymeta.csv', $metadata);

$pv = PendingVolume::factory()->create([
'media_type_id' => MediaType::imageId(),
'metadata_file_path' => 'mymeta.csv',
'import_file_labels' => true,
'import_annotations' => true,
'user_map' => [321 => $dbUser->id],
'label_map' => [123 => $dbLabel->id, 321 => $dbLabel2->id],
'volume_id' => $image->volume_id,
]);

(new ImportVolumeMetadata($pv))->handle();

$annotations = $image->annotations()->orderBy('id', 'asc')->get();

$this->assertSame($dbLabel->id, $annotations[0]->labels[0]->label_id);
$this->assertSame($dbLabel2->id, $annotations[1]->labels[0]->label_id);
}
}

0 comments on commit 7554d48

Please sign in to comment.