Skip to content

Commit

Permalink
Nicer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Sep 21, 2023
1 parent 138a8ed commit 6c716b5
Show file tree
Hide file tree
Showing 24 changed files with 233 additions and 425 deletions.
36 changes: 9 additions & 27 deletions tests/Transformers/BM25TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
use Rubix\ML\Transformers\Transformer;
use Rubix\ML\Transformers\BM25Transformer;
use PHPUnit\Framework\TestCase;
use Rubix\ML\Exceptions\RuntimeException;

/**
* @group Transformers
* @covers \Rubix\ML\Transformers\BM25Transformer
*/
class BM25TransformerTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Transformers\BM25Transformer
*/
Expand All @@ -31,12 +25,6 @@ class BM25TransformerTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = new Unlabeled([
[1, 3, 0, 0, 1, 0, 0, 0, 1, 2, 0, 2, 0, 0, 0, 4, 1, 0, 1],
[0, 1, 1, 0, 0, 2, 1, 0, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 3, 0, 0, 4, 2, 0, 0, 1, 0, 2, 0, 1, 0, 0],
]);

$this->transformer = new BM25Transformer(1.2, 0.75);
}

Expand All @@ -56,7 +44,13 @@ public function build() : void
*/
public function fitTransform() : void
{
$this->transformer->fit($this->dataset);
$dataset = new Unlabeled([
[1, 3, 0, 0, 1, 0, 0, 0, 1, 2, 0, 2, 0, 0, 0, 4, 1, 0, 1],
[0, 1, 1, 0, 0, 2, 1, 0, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 3, 0, 0, 4, 2, 0, 0, 1, 0, 2, 0, 1, 0, 0],
]);

$this->transformer->fit($dataset);

$this->assertTrue($this->transformer->fitted());

Expand All @@ -66,26 +60,14 @@ public function fitTransform() : void
$this->assertCount(19, $dfs);
$this->assertContainsOnly('int', $dfs);

$this->dataset->apply($this->transformer);
$dataset->apply($this->transformer);

$expected = [
[0.2562582002070131, 0.22742881339794754, 0.0, 0.0, 0.13186359514416618, 0.0, 0.0, 0.0, 0.13186359514416618, 0.19254341937443092, 0.0, 0.19254341937443092, 0.0, 0.0, 0.0, 0.4860031535349766, 0.13186359514416618, 0.0, 0.2562582002070131],
[0.0, 0.17063795450977862, 0.3316106698128093, 0.0, 0.0, 0.23083934808978732, 0.3316106698128093, 0.0, 0.0, 0.0, 0.0, 0.26160416281731713, 0.0, 0.3316106698128093, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.2562582002070131, 0.19254341937443092, 0.22742881339794754, 0.0, 0.0, 0.25008418471976107, 0.19254341937443092, 0.0, 0.0, 0.2562582002070131, 0.0, 0.3741808347986538, 0.0, 0.13186359514416618, 0.0, 0.0],
];

$this->assertEquals($expected, $this->dataset->samples());
}

/**
* @test
*/
public function transformUnfitted() : void
{
$this->expectException(RuntimeException::class);

$samples = $this->dataset->samples();

$this->transformer->transform($samples);
$this->assertEqualsWithDelta($expected, $dataset->samples(), 1e-8);
}
}
29 changes: 13 additions & 16 deletions tests/Transformers/BooleanConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
*/
class BooleanConverterTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Transformers\BooleanConverter
*/
Expand All @@ -28,12 +23,7 @@ class BooleanConverterTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = new Unlabeled([
[true, 'true', '1', 1],
[false, 'false', '0', 0],
]);

$this->transformer = new BooleanConverter('::true::', '::false::');
$this->transformer = new BooleanConverter('!true!', '!false!');
}

/**
Expand All @@ -50,11 +40,18 @@ public function build() : void
*/
public function transform() : void
{
$this->dataset->apply($this->transformer);
$dataset = new Unlabeled([
[true, 'true', '1', 1],
[false, 'false', '0', 0],
]);

$dataset->apply($this->transformer);

$expected = [
['!true!', 'true', '1', 1],
['!false!', 'false', '0', 0],
];

$this->assertEquals([
['::true::', 'true', '1', 1],
['::false::', 'false', '0', 0],
], $this->dataset->samples());
$this->assertEquals($expected, $dataset->samples());
}
}
15 changes: 3 additions & 12 deletions tests/Transformers/GaussianRandomProjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GaussianRandomProjectorTest extends TestCase
*/
protected function setUp() : void
{
$this->generator = new Blob(array_fill(0, 10, 0.0), 3.0);
$this->generator = new Blob(array_fill(0, 20, 0.0), 3.0);

$this->transformer = new GaussianRandomProjector(5);

Expand Down Expand Up @@ -99,26 +99,17 @@ public function minDimensionsProvider() : Generator
*/
public function fitTransform() : void
{
$this->assertCount(10, $this->generator->generate(1)->sample(0));
$dataset = $this->generator->generate(30);

$this->transformer->fit($this->generator->generate(30));
$this->transformer->fit($dataset);

$this->assertTrue($this->transformer->fitted());

$expected = [
-1.5798504291401145,
13.861277276658175,
6.8204901690218,
1.0068840164872395,
-13.878216040342053,
];

$sample = $this->generator->generate(1)
->apply($this->transformer)
->sample(0);

$this->assertCount(5, $sample);
$this->assertEqualsWithDelta($expected, $sample, 1e-8);
}

/**
Expand Down
44 changes: 13 additions & 31 deletions tests/Transformers/HotDeckImputerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Rubix\ML\Transformers\Transformer;
use Rubix\ML\Datasets\Generators\Blob;
use Rubix\ML\Transformers\HotDeckImputer;
use Rubix\ML\Exceptions\RuntimeException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -18,11 +17,6 @@ class HotDeckImputerTest extends TestCase
{
protected const RANDOM_SEED = 0;

/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Datasets\Generators\Blob
*/
Expand All @@ -38,15 +32,6 @@ class HotDeckImputerTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = new Unlabeled([
[30, 0.001],
[NAN, 0.055],
[50, -2.0],
[60, NAN],
[10, 1.0],
[100, 9.0],
]);

$this->generator = new Blob([30.0, 0.0]);

$this->transformer = new HotDeckImputer(2, true, '?');
Expand All @@ -69,25 +54,22 @@ public function build() : void
*/
public function fitTransform() : void
{
$this->transformer->fit($this->dataset);

$this->assertTrue($this->transformer->fitted());

$this->dataset->apply($this->transformer);
$dataset = new Unlabeled([
[30, 0.001],
[NAN, 0.055],
[50, -2.0],
[60, NAN],
[10, 1.0],
[100, 9.0],
]);

$this->assertEquals(30, $this->dataset[1][0]);
$this->assertEquals(-2.0, $this->dataset[3][1]);
}
$this->transformer->fit($dataset);

/**
* @test
*/
public function transformUnfitted() : void
{
$this->expectException(RuntimeException::class);
$this->assertTrue($this->transformer->fitted());

$samples = $this->dataset->samples();
$dataset->apply($this->transformer);

$this->transformer->transform($samples);
$this->assertEquals(30, $dataset[1][0]);
$this->assertEquals(-2.0, $dataset[3][1]);
}
}
17 changes: 6 additions & 11 deletions tests/Transformers/ImageResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
class ImageResizerTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Transformers\ImageResizer
*/
Expand All @@ -29,10 +24,6 @@ class ImageResizerTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = Unlabeled::quick([
[imagecreatefrompng('./tests/test.png'), 'whatever'],
]);

$this->transformer = new ImageResizer(32, 32);
}

Expand All @@ -50,9 +41,13 @@ public function build() : void
*/
public function transform() : void
{
$this->dataset->apply($this->transformer);
$dataset = Unlabeled::quick([
[imagecreatefrompng('./tests/test.png'), 'whatever', 69],
]);

$dataset->apply($this->transformer);

$sample = $this->dataset->sample(0);
$sample = $dataset->sample(0);

$image = $sample[0];

Expand Down
21 changes: 8 additions & 13 deletions tests/Transformers/ImageRotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
class RandomizedImageRotatorTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected \Rubix\ML\Datasets\Unlabeled $dataset;

/**
* @var \Rubix\ML\Transformers\ImageRotator
*/
Expand All @@ -29,10 +24,6 @@ class RandomizedImageRotatorTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = Unlabeled::quick([
[imagecreatefrompng('./tests/test.png'), 'whatever'],
]);

$this->transformer = new ImageRotator(0.0, 1.0);
}

Expand All @@ -50,22 +41,26 @@ public function build() : void
*/
public function transform() : void
{
$dataset = Unlabeled::quick([
[imagecreatefrompng('./tests/test.png'), 'whatever', 69],
]);

$mock = $this->createPartialMock(ImageRotator::class, ['rotationAngle']);

$mock->method('rotationAngle')->will($this->returnValue(-180.0));

$expected = file_get_contents('./tests/test_rotated.png');

$this->dataset->apply($mock);
$dataset->apply($mock);

$sample = $this->dataset->sample(0);
$sample = $dataset->sample(0);

ob_start();

imagepng($sample[0]);

$raw = ob_get_clean();

$expected = file_get_contents('./tests/test_rotated.png');

$this->assertEquals($expected, $raw);
}
}
41 changes: 17 additions & 24 deletions tests/Transformers/ImageVectorizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
class ImageVectorizerTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Transformers\ImageVectorizer
*/
Expand All @@ -31,10 +26,6 @@ class ImageVectorizerTest extends TestCase
*/
protected function setUp() : void
{
$this->dataset = Unlabeled::quick([
[imagecreatefrompng('tests/test.png'), 'something else'],
]);

$this->transformer = new ImageVectorizer(false);
}

Expand All @@ -48,22 +39,24 @@ public function build() : void
$this->assertInstanceOf(Stateful::class, $this->transformer);
}

// Commented out due to GD extension bug.
//
// /**
// * @test
// */
// public function fitTransform() : void
// {
// $this->dataset->apply(new ImageResizer(3, 3));
/**
* @test
*/
public function fitTransform() : void
{
$dataset = Unlabeled::quick([
[imagecreatefrompng('tests/test.png'), 'something else'],
]);

$dataset->apply(new ImageResizer(3, 3));

// $this->dataset->apply($this->transformer);
$dataset->apply($this->transformer);

// $expected = [
// ['something else', 46, 51, 66, 130, 135, 134, 118, 119, 116, 25, 26, 45, 149, 154, 154, 180,
// 183, 170, 39, 39, 54, 77, 80, 89, 141, 140, 132],
// ];
$expected = [
['something else', 46, 51, 66, 130, 135, 134, 118, 119, 116, 25, 26, 45, 149, 154, 154, 180,
183, 170, 39, 39, 54, 77, 80, 89, 141, 140, 132],
];

// $this->assertEquals($expected, $this->dataset->samples());
// }
$this->assertEquals($expected, $dataset->samples());
}
}
Loading

0 comments on commit 6c716b5

Please sign in to comment.