a b/tests/edgepy/test_nbinomDeviance.py
1
import unittest
2
3
import numpy as np
4
5
from inmoose.edgepy import nbinomDeviance
6
from inmoose.utils import rnbinom
7
8
9
class test_nbinomDeviance(unittest.TestCase):
10
    def setUp(self):
11
        y = np.array(rnbinom(80, size=5, mu=20, seed=42)).reshape((20, 4))
12
        self.y = np.vstack(([0, 0, 0, 0], [0, 0, 2, 2], y))
13
14
    def test_nbinomDeviance(self):
15
        dev_ref = np.array(
16
            [
17
                7.999999,
18
                5.545177,
19
                321.818901,
20
                184.761910,
21
                355.515748,
22
                340.390858,
23
                414.784086,
24
                461.324010,
25
                235.746631,
26
                354.718673,
27
                410.643778,
28
                265.824737,
29
                422.344282,
30
                412.896876,
31
                392.316278,
32
                599.686176,
33
                242.088733,
34
                501.330767,
35
                608.676351,
36
                706.581453,
37
                430.474599,
38
                310.504051,
39
            ]
40
        )
41
        dev = nbinomDeviance(self.y, np.ones(self.y.shape))
42
        self.assertTrue(np.allclose(dev, dev_ref, atol=0, rtol=10 - 9))
43
44
        dev_ref = np.array([8.454617])
45
        dev = nbinomDeviance(np.array([1, 2, 3, 4]), np.ones(4), dispersion=np.zeros(4))
46
        self.assertTrue(np.allclose(dev, dev_ref, atol=0, rtol=10 - 9))
47
48
        with self.assertRaisesRegex(
49
            ValueError, expected_regex="y is a matrix but mean is not"
50
        ):
51
            nbinomDeviance(self.y, np.ones((22,)))
52
53
        with self.assertRaisesRegex(
54
            ValueError, expected_regex="mean is a matrix but y is not"
55
        ):
56
            nbinomDeviance(np.array([1, 2, 3, 4]), np.ones((4, 3)))
57
58
        with self.assertRaisesRegex(
59
            ValueError, expected_regex="length of mean differs from that of y"
60
        ):
61
            nbinomDeviance(np.array([1, 2, 3, 4]), np.ones(3))