cii-text-analysis/vendor/rubix/ml/tests/Graph/Trees/BallTreeTest.php
Jéremy Munsch f68d2ac333
premier poc
2026-02-11 08:24:04 +01:00

108 lines
2.6 KiB
PHP

<?php
namespace Rubix\ML\Tests\Graph\Trees;
use Rubix\ML\Graph\Trees\Tree;
use Rubix\ML\Graph\Trees\Spatial;
use Rubix\ML\Graph\Trees\BallTree;
use Rubix\ML\Graph\Trees\BinaryTree;
use Rubix\ML\Datasets\Generators\Blob;
use Rubix\ML\Kernels\Distance\Euclidean;
use Rubix\ML\Datasets\Generators\Agglomerate;
use PHPUnit\Framework\TestCase;
/**
* @group Trees
* @covers \Rubix\ML\Graph\Trees\BallTree
*/
class BallTreeTest extends TestCase
{
protected const DATASET_SIZE = 100;
protected const RANDOM_SEED = 0;
/**
* @var \Rubix\ML\Datasets\Generators\Agglomerate
*/
protected $generator;
/**
* @var \Rubix\ML\Graph\Trees\BallTree
*/
protected $tree;
/**
* @before
*/
protected function setUp() : void
{
$this->generator = new Agglomerate([
'east' => new Blob([5, -2, -2]),
'west' => new Blob([0, 5, -3]),
], [0.5, 0.5]);
$this->tree = new BallTree(20, new Euclidean());
srand(self::RANDOM_SEED);
}
/**
* @test
*/
public function build() : void
{
$this->assertInstanceOf(BallTree::class, $this->tree);
$this->assertInstanceOf(Spatial::class, $this->tree);
$this->assertInstanceOf(BinaryTree::class, $this->tree);
$this->assertInstanceOf(Tree::class, $this->tree);
}
/**
* @test
*/
public function growNeighborsRange() : void
{
$this->tree->grow($this->generator->generate(self::DATASET_SIZE));
$this->assertGreaterThan(2, $this->tree->height());
$sample = $this->generator->generate(1)->sample(0);
[$samples, $labels, $distances] = $this->tree->nearest($sample, 5);
$this->assertCount(5, $samples);
$this->assertCount(5, $labels);
$this->assertCount(5, $distances);
$this->assertCount(1, array_unique($labels));
[$samples, $labels, $distances] = $this->tree->range($sample, 4.3);
$this->assertCount(50, $samples);
$this->assertCount(50, $labels);
$this->assertCount(50, $distances);
$this->assertCount(1, array_unique($labels));
}
/**
* @test
*/
public function growWithSameSamples() : void
{
$generator = new Agglomerate([
'east' => new Blob([5, -2, 10], 0.0),
]);
$dataset = $generator->generate(self::DATASET_SIZE);
$this->tree->grow($dataset);
$this->assertEquals(2, $this->tree->height());
}
protected function assertPreConditions() : void
{
$this->assertEquals(0, $this->tree->height());
}
}