-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconftest.py
163 lines (132 loc) · 4.85 KB
/
conftest.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Pyttb pytest configuration."""
# Copyright 2024 National Technology & Engineering Solutions of Sandia,
# LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the
# U.S. Government retains certain rights in this software.
import numpy
import numpy as np
# content of conftest.py
import pytest
import pyttb
import pyttb as ttb
@pytest.fixture(autouse=True)
def add_packages(doctest_namespace): # noqa: D103
doctest_namespace["np"] = numpy
doctest_namespace["ttb"] = pyttb
@pytest.fixture(params=[{"order": "F"}, {"order": "C"}])
def memory_layout(request):
"""Test C and F memory layouts."""
return request.param
def pytest_addoption(parser): # noqa: D103
parser.addoption(
"--packaging",
action="store_true",
dest="packaging",
default=False,
help="enable slow packaging tests",
)
def pytest_configure(config): # noqa: D103
if not config.option.packaging:
config.option.markexpr = "not packaging"
@pytest.fixture()
def sample_tensor_2way(): # noqa: D103
data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
shape = (2, 3)
params = {"data": data, "shape": shape}
tensorInstance = ttb.tensor(data, shape)
return params, tensorInstance
@pytest.fixture()
def sample_tensor_3way(): # noqa: D103
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0])
shape = (2, 3, 2)
params = {"data": np.reshape(data, np.array(shape), order="F"), "shape": shape}
tensorInstance = ttb.tensor(data, shape)
return params, tensorInstance
@pytest.fixture()
def sample_ndarray_1way(): # noqa: D103
shape = (16,)
ndarrayInstance = np.reshape(np.arange(1, 17), shape, order="F")
params = {"data": ndarrayInstance, "shape": shape}
return params, ndarrayInstance
@pytest.fixture()
def sample_ndarray_2way(): # noqa: D103
shape = (4, 4)
ndarrayInstance = np.reshape(np.arange(1, 17), shape, order="F")
params = {"data": ndarrayInstance, "shape": shape}
return params, ndarrayInstance
@pytest.fixture()
def sample_ndarray_4way(): # noqa: D103
shape = (2, 2, 2, 2)
ndarrayInstance = np.reshape(np.arange(1, 17), shape, order="F")
params = {"data": ndarrayInstance, "shape": shape}
return params, ndarrayInstance
@pytest.fixture()
def sample_tenmat_4way(): # noqa: D103
shape = (4, 4)
data = np.reshape(np.arange(1, 17), shape, order="F")
tshape = (2, 2, 2, 2)
rdims = np.array([0, 1])
cdims = np.array([2, 3])
tenmatInstance = ttb.tenmat()
tenmatInstance.tshape = tshape
tenmatInstance.rindices = rdims.copy()
tenmatInstance.cindices = cdims.copy()
tenmatInstance.data = data.copy()
params = {
"data": data,
"rdims": rdims,
"cdims": cdims,
"tshape": tshape,
"shape": shape,
}
return params, tenmatInstance
@pytest.fixture()
def sample_tensor_4way(): # noqa: D103
data = np.arange(1, 17)
shape = (2, 2, 2, 2)
params = {"data": np.reshape(data, np.array(shape), order="F"), "shape": shape}
tensorInstance = ttb.tensor(data, shape)
return params, tensorInstance
@pytest.fixture()
def sample_ktensor_2way(): # noqa: D103
weights = np.array([1.0, 2.0])
fm0 = np.array([[1.0, 2.0], [3.0, 4.0]])
fm1 = np.array([[5.0, 6.0], [7.0, 8.0]])
factor_matrices = [fm0, fm1]
data = {"weights": weights, "factor_matrices": factor_matrices}
ktensorInstance = ttb.ktensor(factor_matrices, weights)
return data, ktensorInstance
@pytest.fixture()
def sample_ktensor_3way(): # noqa: D103
rank = 2
shape = (2, 3, 4)
vector = np.arange(1, rank * sum(shape) + 1).astype(float)
weights = 2 * np.ones(rank).astype(float)
vector_with_weights = np.concatenate((weights, vector), axis=0)
# vector_with_weights = vector_with_weights.reshape((len(vector_with_weights), 1))
# ground truth
fm0 = np.array([[1.0, 3.0], [2.0, 4.0]])
fm1 = np.array([[5.0, 8.0], [6.0, 9.0], [7.0, 10.0]])
fm2 = np.array([[11.0, 15.0], [12.0, 16.0], [13.0, 17.0], [14.0, 18.0]])
factor_matrices = [fm0, fm1, fm2]
data = {
"weights": weights,
"factor_matrices": factor_matrices,
"vector": vector,
"vector_with_weights": vector_with_weights,
"shape": shape,
}
ktensorInstance = ttb.ktensor(factor_matrices, weights)
return data, ktensorInstance
@pytest.fixture()
def sample_ktensor_symmetric(): # noqa: D103
weights = np.array([1.0, 1.0])
fm0 = np.array(
[[2.340431417384394, 4.951967353890655], [4.596069112758807, 8.012451489774961]]
)
fm1 = np.array(
[[2.340431417384394, 4.951967353890655], [4.596069112758807, 8.012451489774961]]
)
factor_matrices = [fm0, fm1]
data = {"weights": weights, "factor_matrices": factor_matrices}
ktensorInstance = ttb.ktensor(factor_matrices, weights)
return data, ktensorInstance