Skip to content

Commit

Permalink
Implement pruning of training proposal feature vectors
Browse files Browse the repository at this point in the history
Resolves #131
  • Loading branch information
mzur committed Oct 13, 2023
1 parent 774c764 commit 9b5335c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Listeners/PruneTrainingProposalFeatureVectors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Biigle\Modules\Maia\Listeners;

use Biigle\Modules\Maia\Events\MaiaJobContinued;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
use Illuminate\Contracts\Queue\ShouldQueue;

class PruneTrainingProposalFeatureVectors implements ShouldQueue
{
/**
* Handle the event.
*
* @param MaiaJobContinued $event
* @return void
*/
public function handle(MaiaJobContinued $event)
{
$event->job
->trainingProposals()
->unselected()
->select('id')
->chunkById(1000, function ($chunk) {
TrainingProposalFeatureVector::whereIn('id', $chunk->pluck('id'))
->delete();
});
}
}
2 changes: 2 additions & 0 deletions src/MaiaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Biigle\Modules\Maia\Listeners\DispatchMaiaJob;
use Biigle\Modules\Maia\Listeners\DispatchObjectDetectionRequest;
use Biigle\Modules\Maia\Listeners\PrepareDeleteAnnotationPatches;
use Biigle\Modules\Maia\Listeners\PruneTrainingProposalFeatureVectors;
use Biigle\Modules\Maia\Listeners\PruneTrainingProposalPatches;
use Biigle\Services\Modules;
use Event;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function boot(Modules $modules, Router $router)
Event::listen(MaiaJobCreated::class, DispatchMaiaJob::class);
Event::listen(MaiaJobContinued::class, DispatchObjectDetectionRequest::class);
Event::listen(MaiaJobContinued::class, PruneTrainingProposalPatches::class);
Event::listen(MaiaJobContinued::class, PruneTrainingProposalFeatureVectors::class);
Event::listen(MaiaJobDeleting::class, PrepareDeleteAnnotationPatches::class);
Event::listen(MaiaJobDeleting::class, DeleteAnnotationFeatureVectors::class);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Listeners/PruneTrainingProposalFeatureVectorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Biigle\Tests\Modules\Maia\Listeners;

use Biigle\Modules\Maia\Events\MaiaJobContinued;
use Biigle\Modules\Maia\Listeners\PruneTrainingProposalFeatureVectors;
use Biigle\Tests\Modules\Maia\TrainingProposalTest;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
use TestCase;

class PruneTrainingProposalFeatureVectorsTest extends TestCase
{
public function testHandle()
{
$a = TrainingProposalTest::create(['selected' => true]);
$af = TrainingProposalFeatureVector::factory()->create([
'id' => $a->id,
'job_id' => $a->job_id,
]);

$a2 = TrainingProposalTest::create([
'job_id' => $a->job_id,
'selected' => false,
]);
$af2 = TrainingProposalFeatureVector::factory()->create([
'id' => $a2->id,
'job_id' => $a2->job_id,
]);


$listener = new PruneTrainingProposalFeatureVectors;
$listener->handle(new MaiaJobContinued($a->job));
$this->assertNotNull($af->fresh());
$this->assertNull($af2->fresh());
}
}

0 comments on commit 9b5335c

Please sign in to comment.