Repository: bazingagin/npc_gzip
File: tests/test_knn_classifier.py (Python)

Code before:

        assert isinstance(compressed_input, np.ndarray)
        assert isinstance(compressed_combined, np.ndarray)
        assert compressed_input.shape == compressed_combined.shape

    def test__calculate_distance(self) -> None:
        compressed_input, compressed_combined = self.model._compress_sample(
            self.sample_input
        )
        distance = self.model._calculate_distance(compressed_input, compressed_combined)

        assert isinstance(distance, np.ndarray)
        assert distance.shape == compressed_input.shape == compressed_combined.shape

    def test_predict(self) -> None:
        top_k = 1
        (distance, labels, similar_samples) = self.model.predict(
            self.sample_input, top_k
        )

        assert distance.shape == (
            len([self.sample_input]),
            self.model.training_inputs.shape[0],
        )
        assert labels.shape == (len([self.sample_input]),)
        assert similar_samples.shape == (top_k, len([self.sample_input]))

        test_set_size = random.randint(1, 50)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = 2
        (distance, labels, similar_samples) = self.model.predict(test_set, top_k)

        assert distance.shape == (test_set_size, self.model.training_inputs.shape[0])
        assert labels.shape == (test_set_size,)
        assert similar_samples.shape == (test_set_size, top_k)

    def test_negative_top_k(self) -> None:
        test_set_size = random.randint(1, 50)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = -1

        with pytest.raises(AssertionError):
            self.model.predict(test_set, top_k)

    def test_top_k_bigger_than_test_set(self) -> None:
        test_set_size = random.randint(1, 10)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = test_set_size + 1
        with pytest.raises(AssertionError):
            self.model.predict(test_set, top_k)


Review comment (suggestion):
This may be a separate issue, and it's not new with this PR, but `len([self.sample_input])` is always 1, because it is taking the length of the 1-element list `[self.sample_input]`. So the assertions here are equivalent to:

- `assert distance.shape == (1, self.model.training_inputs.shape[0])`
- `assert labels.shape == (1,)` – previously `assert labels.shape == (top_k, 1)`
- `assert similar_samples.shape == (top_k, 1)`

This may be intentional, but I'm not sure. @zbloss

Code after:

        assert isinstance(compressed_input, np.ndarray)
        assert isinstance(compressed_combined, np.ndarray)
        assert compressed_input.shape == compressed_combined.shape

    def test__calculate_distance(self) -> None:
        compressed_input, compressed_combined = self.model._compress_sample(
            self.sample_input
        )
        distance = self.model._calculate_distance(compressed_input, compressed_combined)

        assert isinstance(distance, np.ndarray)
        assert distance.shape == compressed_input.shape == compressed_combined.shape

    def test_predict(self) -> None:
        top_k = 1
        (distance, labels, similar_samples) = self.model.predict(
            self.sample_input, top_k
        )

        assert distance.shape == (
            1,
            self.model.training_inputs.shape[0],
        )
        assert labels.shape == (1,)
        assert similar_samples.shape == (top_k, 1)

        test_set_size = random.randint(1, 50)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = 2
        (distance, labels, similar_samples) = self.model.predict(test_set, top_k)

        assert distance.shape == (test_set_size, self.model.training_inputs.shape[0])
        assert labels.shape == (test_set_size,)
        assert similar_samples.shape == (test_set_size, top_k)

    def test_negative_top_k(self) -> None:
        test_set_size = random.randint(1, 50)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = -1

        with pytest.raises(AssertionError):
            self.model.predict(test_set, top_k)

    def test_top_k_bigger_than_test_set(self) -> None:
        test_set_size = random.randint(1, 10)
        test_set = [self.sample_input for _ in range(test_set_size)]
        top_k = test_set_size + 1
        with pytest.raises(AssertionError):
            self.model.predict(test_set, top_k)