File size: 1,418 Bytes
173a895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest

import numpy as np

from src.code_challenge import _aligned_proba_frame


class TestProbabilityAlignment(unittest.TestCase):
    def test_rejects_encoded_classes_that_do_not_match_decoded_labels(self):
        frame = _aligned_proba_frame(
            np.array([[0.7, 0.2, 0.1], [0.1, 0.6, 0.3]]),
            classes=[0, 1, 2],
            labels=["AA", "Bio", "FA"],
            n_rows=2,
        )

        self.assertIsNone(frame)

    def test_aligns_decoded_classes_to_label_order(self):
        frame = _aligned_proba_frame(
            np.array([[0.2, 0.7, 0.1], [0.6, 0.1, 0.3]]),
            classes=["Bio", "AA", "FA"],
            labels=["AA", "Bio", "FA"],
            n_rows=2,
        )

        self.assertIsNotNone(frame)
        self.assertEqual(list(frame.columns), ["AA", "Bio", "FA"])
        self.assertEqual(frame["AA"].tolist(), [0.7, 0.1])
        self.assertEqual(frame["Bio"].tolist(), [0.2, 0.6])

    def test_accepts_positional_probabilities_when_classes_are_missing(self):
        frame = _aligned_proba_frame(
            np.array([[0.2, 0.7, 0.1]]),
            classes=None,
            labels=["AA", "Bio", "FA"],
            n_rows=1,
        )

        self.assertIsNotNone(frame)
        self.assertEqual(list(frame.columns), ["AA", "Bio", "FA"])
        self.assertEqual(frame.iloc[0].tolist(), [0.2, 0.7, 0.1])


if __name__ == "__main__":
    unittest.main()