forked from diffpy/diffpy.stretched-nmf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_snmf_optimizer.py
More file actions
157 lines (145 loc) · 4.34 KB
/
Copy pathtest_snmf_optimizer.py
File metadata and controls
157 lines (145 loc) · 4.34 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
151
152
153
154
155
156
157
import numpy as np
import pytest
from scipy.sparse import csr_matrix
from diffpy.stretched_nmf.snmf_class import SNMFOptimizer
def test_fit_recovers_rank_one_factors():
expected_components = np.array(
[
[0.20],
[0.75],
[1.20],
[0.80],
[0.30],
]
)
expected_weights = np.array(
[
[0.20, 0.60, 1.00, 0.40],
]
)
source = expected_components @ expected_weights
model = SNMFOptimizer(
n_components=1,
show_plots=False,
random_state=1,
min_iter=0,
max_iter=2,
rho=0.0,
eta=0.0,
)
model.fit(source_matrix=source)
assert np.isfinite(model.objective_function_)
assert np.allclose(
model.components_, expected_components, rtol=0.2, atol=0.1
)
assert np.allclose(model.weights_, expected_weights, rtol=0.2, atol=0.1)
@pytest.mark.parametrize(
"inputs, expected",
# inputs tuple:
# (components, residuals, stretch, rho, eta, spline smoothness operator)
[
# Case 0: No smoothness or sparsity penalty, reduces to NMF objective
# residual Frobenius norm^2 = 3^2 + 4^2 = 25 -> 0.5 * 25 = 12.5
(
(
np.array([[0.0, 0.0], [3.0, 4.0]]),
np.array([[0.0, 0.0], [3.0, 4.0]]),
np.ones((2, 2)),
0.0,
0.0,
np.zeros((2, 2)),
),
12.5,
),
# Case 1: rho = 0, sparsity penalty only
# sqrt components sum = 1 + 2 + 3 + 4 = 10 -> eta * 10 = 5
# residual term remains 12.5 -> total = 17.5
(
(
np.array([[1.0, 4.0], [9.0, 16.0]]),
np.array([[3.0, 4.0], [0.0, 0.0]]),
np.ones((2, 2)),
0.0,
0.5,
np.zeros((2, 2)),
),
17.5,
),
# Case 2: eta = 0, smoothness penalty only
# residual = 12.5, smoothing = 0.5 * 1 * 1 = 0.5 -> total = 13.0
(
(
np.array([[1.0, 2.0], [3.0, 4.0]]),
np.array([[3.0, 4.0], [0.0, 0.0]]),
np.array([[1.0, 2.0]]),
1.0,
0.0,
np.array([[1.0, -1.0]]),
),
13.0,
),
# Case 3: penalty for smoothness and sparsity
# residual = 2.5, sparsity = 1.5, smoothing = 9 -> total = 13.0
(
(
np.array([[1.0, 4.0]]),
np.array([[1.0, 2.0]]),
np.array([[1.0, 4.0]]),
2.0,
0.5,
np.array([[3.0, 0.0]]),
),
13.0,
),
],
)
def test_compute_objective_function(inputs, expected):
components, residuals, stretch, rho, eta, operator = inputs
result = SNMFOptimizer._compute_objective_function(
components=components,
residuals=residuals,
stretch=stretch,
rho=rho,
eta=eta,
spline_smooth_operator=operator,
)
assert np.isclose(result, expected)
def test_regularize_function_hessian_has_expected_structure():
model = SNMFOptimizer(n_components=2, rho=0.5)
model.n_components_ = 2
model.n_signals_ = 3
model._spline_smooth_operator = csr_matrix(
[[1.0, -1.0, 0.0], [0.0, 1.0, -1.0]]
)
residuals = np.array([[2.0, -1.0, 4.0], [1.0, 3.0, -2.0]])
d_stretch_comps = np.array(
[
[1.0, 0.0, 1.0, 2.0, 0.0, 1.0],
[0.0, 1.0, 1.0, 0.0, 3.0, -1.0],
]
)
dd_stretch_comps = np.array(
[
[0.5, 1.0, 0.0, 1.0, 0.0, -0.5],
[1.0, 0.0, 0.25, 0.0, 1.0, 0.5],
]
)
model._stretch_residual_and_derivatives = lambda stretch: (
residuals,
d_stretch_comps,
dd_stretch_comps,
)
hessian = model._regularize_function_hessian(np.ones((2, 3)))
expected = np.array(
[
[3.5, -0.5, 0.0, 2.0, 0.0, 0.0],
[-0.5, 1.0, -0.5, 0.0, 3.0, 0.0],
[0.0, -0.5, 2.0, 0.0, 0.0, 0.0],
[2.0, 0.0, 0.0, 6.5, -0.5, 0.0],
[0.0, 3.0, 0.0, -0.5, 13.0, -0.5],
[0.0, 0.0, 0.0, 0.0, -0.5, -0.5],
]
)
assert hessian.shape == (6, 6)
assert np.allclose(hessian, hessian.T)
assert np.allclose(hessian, expected)