Skip to content

Commit 1bee9c6

Browse files
author
John Halloran
committed
refactor: use rho and eta in fit
1 parent d02649a commit 1bee9c6

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/diffpy/snmf/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
init_stretch=init_stretch_file,
1616
show_plots=True,
1717
)
18-
my_model.fit()
18+
my_model.fit(rho=1e12, eta=610)
1919

2020
print("Done")
2121
np.savetxt("my_norm_components.txt", my_model.components_, fmt="%.6g", delimiter=" ")

src/diffpy/snmf/snmf_class.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@ def __init__(
6969
init_weights=None,
7070
init_components=None,
7171
init_stretch=None,
72-
rho=0,
73-
eta=0,
7472
max_iter=500,
7573
min_iter=20,
7674
tol=5e-7,
7775
n_components=None,
7876
random_state=None,
7977
show_plots=False,
8078
):
81-
"""Initialize an instance of SNMF and run the optimization.
79+
"""Initialize an instance of sNMF.
8280
8381
Parameters
8482
----------
@@ -95,14 +93,6 @@ def __init__(
9593
0, 1e-3, size=(self.n_components, self.n_signals)
9694
The initial guesses for the stretching factor for each component, at each
9795
condition (for each signal). Shape is (number_of_components, number_of_signals).
98-
rho : float Optional Default = 0
99-
The stretching factor that influences the decomposition. Zero corresponds to no
100-
stretching present. Relatively insensitive and typically adjusted in powers of 10.
101-
eta : int Optional Default = 0
102-
The sparsity factor that influences the decomposition. Should be set to zero for
103-
non-sparse data such as PDF. Can be used to improve results for sparse data such
104-
as XRD, but due to instability, should be used only after first selecting the
105-
best value for rho. Suggested adjustment is by powers of 2.
10696
max_iter : int Optional Default = 500
10797
The maximum number of times to update each of A, X, and Y before stopping
10898
the optimization.
@@ -121,8 +111,6 @@ def __init__(
121111
"""
122112

123113
self.source_matrix = source_matrix
124-
self.rho = rho
125-
self.eta = eta
126114
self.tol = tol
127115
self.max_iter = max_iter
128116
self.min_iter = min_iter
@@ -172,7 +160,23 @@ def __init__(
172160
[1, -2, 1], offsets=[0, 1, 2], shape=(self.n_signals - 2, self.n_signals)
173161
)
174162

175-
def fit(self):
163+
def fit(self, rho=0, eta=0):
164+
"""Run the sNMF optimization with the given parameters, using the setup from __init__.
165+
166+
Parameters
167+
----------
168+
rho : float Optional Default = 0
169+
The stretching factor that influences the decomposition. Zero corresponds to no
170+
stretching present. Relatively insensitive and typically adjusted in powers of 10.
171+
eta : int Optional Default = 0
172+
The sparsity factor that influences the decomposition. Should be set to zero for
173+
non-sparse data such as PDF. Can be used to improve results for sparse data such
174+
as XRD, but due to instability, should be used only after first selecting the
175+
best value for rho. Suggested adjustment is by powers of 2.
176+
"""
177+
178+
self.rho = rho
179+
self.eta = eta
176180

177181
# Set up residual matrix, objective function, and history
178182
self.residuals = self.get_residual_matrix()

tests/test_snmf_optimizer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ def test_final_objective_below_threshold(inputs):
3636
init_components=inputs["components"],
3737
init_stretch=inputs["stretch"],
3838
show_plots=False,
39-
rho=1e12,
40-
eta=610,
4139
random_state=1,
4240
min_iter=5,
4341
max_iter=5,
4442
)
45-
model.fit()
43+
model.fit(rho=1e12, eta=610)
4644

4745
# Basic sanity check and the actual assertion
4846
assert np.isfinite(model.objective_function)

0 commit comments

Comments
 (0)