-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_build_core.py
More file actions
150 lines (84 loc) · 3.98 KB
/
test_build_core.py
File metadata and controls
150 lines (84 loc) · 3.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
from scikit_tt.tensor_train import build_core, build_core_vector
import unittest as ut
from unittest import TestCase
from numpy.random import randint, rand
np.random.seed(1234)
class TestBuildCore(TestCase):
def input_data(self):
"""
Initialize test data, i.e. list of lists of matrices.
"""
self.length = randint(3, 6) # Left rank of core
self.n_matrices = randint(3, 6) # Right rank of core
self.matrix_order = (randint(3, 6), randint(3, 6)) # Order of matrices
class TestMatrixList(TestBuildCore):
def random_indices(self):
"""
Generate random indices for changing elements of input at random positions.
"""
rand_outer = randint(0, self.length - 1)
rand_inner = randint(0, self.n_matrices - 1)
return rand_outer, rand_inner
def build_matrix_list(self):
"""
Generate random input for build_core using randomly chosen matrices.
"""
self.input_data()
self.matrix_list = [[np.zeros(self.matrix_order)] * self.n_matrices] * self.length
for i in range(self.length):
self.matrix_list[i] = [ rand(self.matrix_order[0], self.matrix_order[1]) for _ in range(self.n_matrices) ]
def test_shape(self):
"""
Try to pass a matrix list with a 3-dimensional array instead of matrix.
"""
self.build_matrix_list()
core = build_core(self.matrix_list)
self.assertEqual(core.shape, (self.length, self.matrix_order[0], self.matrix_order[1], self.n_matrices))
with self.assertRaises(ValueError):
matrix_list = self.matrix_list
rand_outer, rand_inner = self.random_indices()
matrix_list[rand_outer][rand_inner] = rand(3, 3, 3) # Fail when element in inner list is not matrix
build_core(matrix_list)
def test_valid_type(self):
self.build_matrix_list()
rand_outer, rand_inner = self.random_indices()
self.matrix_list[rand_outer][rand_inner] = 0 # Add zero at a random position in matrix list.
core = build_core(self.matrix_list)
self.assertEqual(True, np.all(core[rand_outer, :, :, rand_inner] == np.zeros(self.matrix_order)))
with self.assertRaises(TypeError):
self.build_matrix_list()
self.matrix_list[rand_outer][rand_inner] = "random_string" # Fail when passing object of type that is neither string nor integer 0.
build_core(self.matrix_list)
def test_valid_matrix_shape(self):
with self.assertRaises(ValueError):
self.build_matrix_list()
rand_outer, rand_inner = self.random_indices()
self.matrix_list[rand_outer][rand_inner] = rand(2, 2)
build_core(self.matrix_list)
def test_matrix_number(self):
"""
Ensure the same number of lists of matrices in overall list.
"""
self.build_matrix_list()
rand_outer, _ = self.random_indices()
self.matrix_list[rand_outer] = [ rand(self.matrix_order[0], self.matrix_order[1]) for _ in range(2)]
with self.assertRaises(IndexError):
build_core(self.matrix_list)
return
class TestMatrixListSimple(TestBuildCore):
def build_matrix_list_simple(self):
"""
Generate random input for second case of build_core (i.e. for build_core_vector).
"""
self.input_data()
self.matrix_list_simple = [ np.zeros(self.matrix_order) ] * self.length
for i in range(self.length):
self.matrix_list_simple[i] = rand(self.matrix_order[0], self.matrix_order[1])
def test_build_core_vector(self):
self.build_matrix_list_simple()
build_core_vector(self.matrix_list_simple)
core = build_core(self.matrix_list_simple)
assert(core.shape == (self.length, self.matrix_order[0], self.matrix_order[1], 1))
if __name__ == '__main__':
ut.main()