1<?php
2
3namespace Rubix\ML\Tests\Transformers;
4
5use Rubix\ML\Datasets\Unlabeled;
6use Rubix\ML\Transformers\Stateful;
7use Rubix\ML\Transformers\Transformer;
8use Rubix\ML\Datasets\Generators\Blob;
9use Rubix\ML\Transformers\RandomHotDeckImputer;
10use PHPUnit\Framework\TestCase;
11use Rubix\ML\Exceptions\RuntimeException;
12
13/**
14 * @group Transformers
15 * @covers \Rubix\ML\Transformers\RandomHotDeckImputer
16 */
17class RandomHotDeckImputerTest extends TestCase
18{
19    protected const RANDOM_SEED = 0;
20
21    /**
22     * @var \Rubix\ML\Datasets\Unlabeled
23     */
24    protected $dataset;
25
26    /**
27     * @var \Rubix\ML\Datasets\Generators\Blob
28     */
29    protected $generator;
30
31    /**
32     * @var \Rubix\ML\Transformers\RandomHotDeckImputer
33     */
34    protected $transformer;
35
36    /**
37     * @before
38     */
39    protected function setUp() : void
40    {
41        $this->dataset = new Unlabeled([
42            [30, 0.001],
43            [NAN, 0.055],
44            [50, -2.0],
45            [60, NAN],
46            [10, 1.0],
47            [100, 9.0],
48        ]);
49
50        $this->generator = new Blob([30.0, 0.0]);
51
52        $this->transformer = new RandomHotDeckImputer(2, true, '?');
53
54        srand(self::RANDOM_SEED);
55    }
56
57    /**
58     * @test
59     */
60    public function build() : void
61    {
62        $this->assertInstanceOf(RandomHotDeckImputer::class, $this->transformer);
63        $this->assertInstanceOf(Transformer::class, $this->transformer);
64        $this->assertInstanceOf(Stateful::class, $this->transformer);
65    }
66
67    /**
68     * @test
69     */
70    public function fitTransform() : void
71    {
72        $this->transformer->fit($this->dataset);
73
74        $this->assertTrue($this->transformer->fitted());
75
76        $this->dataset->apply($this->transformer);
77
78        $this->assertEquals(30, $this->dataset[1][0]);
79        $this->assertEquals(0.001, $this->dataset[3][1]);
80    }
81
82    /**
83     * @test
84     */
85    public function transformUnfitted() : void
86    {
87        $this->expectException(RuntimeException::class);
88
89        $samples = $this->dataset->samples();
90
91        $this->transformer->transform($samples);
92    }
93}
94