Completed
Push — master ( fe60a9...f3d068 )
by Wouter
04:03
created

test_one_hot()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
dl 0
loc 19
ccs 7
cts 7
cp 1
crap 5
rs 8.5454
c 1
b 0
f 0
1 1
import numpy as np
2 1
import numpy.random as rnd
3 1
import pytest
4 1
from libtlda.util import is_pos_def, one_hot, regularize_matrix
5
6
7 1
def test_is_pos_def():
8
    """Check if function returns boolean positive-definiteness."""
9
    # Positive-definite matrix
10 1
    A = np.array([[1, 0], [0, 1]])
11
12
    # Not positive-definite matrix
13 1
    B = np.array([[-1, 0], [0, 1]])
14
15
    # Assert correct positive-definiteness
16 1
    assert is_pos_def(A)
17 1
    assert not is_pos_def(B)
18
19
20 1
def test_one_hot():
21
    """Check if one_hot returns correct label matrices."""
22
    # Generate label vector
23 1
    y = np.hstack((np.ones((10,))*0,
24
                   np.ones((10,))*1,
25
                   np.ones((10,))*2))
26
27
    # Map to matrix
28 1
    Y, labels = one_hot(y)
29
30
    # Check for only 0's and 1's
31 1
    assert len(np.setdiff1d(np.unique(Y), [0, 1])) == 0
32
33
    # Check for correct labels
34 1
    assert np.all(labels == np.unique(y))
35
36
    # Check correct shape of matrix
37 1
    assert Y.shape[0] == y.shape[0]
38 1
    assert Y.shape[1] == len(labels)
39
40
41 1
def test_regularize_matrix():
42
    """Test whether function regularizes matrix correctly."""
43
    # Generate test matrix
44 1
    A = rnd.randn(3)
45
46
    # Check for inappropriate input argument
47 1
    with pytest.raises(ValueError):
48
        regularize_matrix(A, a=-1.0)
49