forked from diffpy/diffpy.stretched-nmf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnmf_class.py
More file actions
1173 lines (1033 loc) · 42.4 KB
/
snmf_class.py
File metadata and controls
1173 lines (1033 loc) · 42.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
import cvxpy as cp
import numpy as np
from scipy.optimize import minimize
from scipy.sparse import coo_matrix, diags
from diffpy.stretched_nmf.plotter import SNMFPlotter
class SNMFOptimizer:
"""An implementation of stretched NMF, including sparse stretched
NMF.
Instantiate the estimator with hyperparameters, then call ``fit`` to
optimize model factors. Trailing underscores indicate that an attribute
was determined during the fit process.
For more information on sNMF, please reference:
Gu, R., Rakita, Y., Lan, L. et al.
Stretched non-negative matrix factorization.
npj Comput Mater 10, 193 (2024) https://doi.org/10.1038/s41524-024-01377-5
Attributes
----------
stretch_ : numpy.ndarray
The best guess (or while running, the current guess) for the stretching
factor matrix.
components_ : numpy.ndarray
The best guess (or while running, the current guess) for the matrix of
component intensities.
weights_ : numpy.ndarray
The best guess (or while running, the current guess) for the matrix of
component weights.
rho : float
The stretching factor that influences the decomposition. Zero
corresponds to no stretching present. Relatively insensitive and
typically adjusted in powers of 10.
eta : float
The sparsity factor that influences the decomposition. Should be set
to zero for non-sparse data such as PDF. Can be used to improve
results for sparse data such as XRD, but due to instability, should
be used only after first selecting the best value for rho. Suggested
adjustment is by powers of 2.
max_iter : int
The maximum number of times to update each of stretch, components,
and weights before stopping the optimization.
min_iter : int
The minimum number of times to update each of stretch, components,
and weights before terminating the optimization due to low/no
improvement.
tol : float
The convergence threshold. This is the minimum fractional improvement
in the objective function to allow without terminating the
optimization.
n_components : int
The referred number of components when ``init_weights`` is not
provided to ``fit``.
random_state : int
The seed for the initial guesses at the matrices (stretch, components,
and weights) created by the decomposition.
n_components_ : int
The learned number of components from initialization.
signal_length_ : int
The number of rows in the fitted source matrix.
n_signals_ : int
The number of columns in the fitted source matrix.
objective_function_ : float
Current objective value from the most recent update.
objective_difference_ : float
The change in the objective function value since the last update. A
positive value means that the result improved.
n_iter_ : int
The number of outer iterations completed in ``fit``.
"""
def __init__(
self,
n_components=None,
max_iter=500,
min_iter=20,
tol=5e-7,
rho=0,
eta=0,
random_state=None,
show_plots=False,
verbose=False,
):
"""Initialize an instance of sNMF with estimator
hyperparameters.
Parameters
----------
n_components : int, optional
The number of components to extract when ``init_weights`` is not
provided to ``fit``.
max_iter : int
The maximum number of times to update each of A, X, and Y before
stopping the optimization. Optional.
min_iter : int
The minimum number of outer-loop iterations before convergence
checks can stop optimization. Optional.
tol : float
The convergence threshold. This is the minimum fractional
improvement in the objective function to allow without terminating
the optimization. Note that a minimum of 20 updates are run before
this parameter is checked. Optional.
rho : float
The stretching regularization hyperparameter. Zero corresponds to
no stretching.
eta : float
The sparsity regularization hyperparameter. Turn off for non-sparse
data such as PDF.
random_state : int
The seed for the initial guesses at the matrices (A, X, and Y)
created by the decomposition. Optional.
show_plots : bool
Enables plotting at each step of the decomposition. Optional.
"""
if n_components is not None and n_components < 1:
raise ValueError("n_components must be a positive integer.")
self.n_components = n_components
self.max_iter = max_iter
self.min_iter = min_iter
self.tol = tol
self.rho = rho
self.eta = eta
self.random_state = random_state
self.show_plots = show_plots
self.verbose = verbose
self._rng = np.random.default_rng(self.random_state)
self._plotter = SNMFPlotter() if self.show_plots else None
def _initialize_factors(
self,
source_matrix,
init_weights=None,
init_components=None,
init_stretch=None,
):
self._rng = np.random.default_rng(self.random_state)
self.signal_length_, self.n_signals_ = source_matrix.shape
if init_weights is None and self.n_components is None:
raise ValueError(
"n_components must be provided when init_weights is not set."
)
if init_weights is None:
n_components = self.n_components
weights = self._rng.beta(
a=2.0,
b=2.0,
size=(n_components, self.n_signals_),
)
else:
weights = np.asarray(init_weights, dtype=float)
n_components = weights.shape[0]
if (
self.n_components is not None
and self.n_components != n_components
):
raise ValueError(
"init_weights has a different number of components than "
"n_components."
)
if init_stretch is None:
stretch = np.ones(
(n_components, self.n_signals_)
) + self._rng.normal(
0,
1e-3,
size=(n_components, self.n_signals_),
)
else:
stretch = np.asarray(init_stretch, dtype=float)
if init_components is None:
components = self._rng.random((self.signal_length_, n_components))
else:
components = np.asarray(init_components, dtype=float)
expected_weights_shape = (n_components, self.n_signals_)
expected_stretch_shape = (n_components, self.n_signals_)
expected_components_shape = (self.signal_length_, n_components)
if weights.shape != expected_weights_shape:
raise ValueError(
"init_weights must have shape "
f"{expected_weights_shape}, got {weights.shape}."
)
if stretch.shape != expected_stretch_shape:
raise ValueError(
"init_stretch must have shape "
f"{expected_stretch_shape}, got {stretch.shape}."
)
if components.shape != expected_components_shape:
raise ValueError(
"init_components must have shape "
f"{expected_components_shape}, got {components.shape}."
)
self.n_components_ = n_components
self.weights_ = np.maximum(0, weights)
self.stretch_ = stretch
self.components_ = np.maximum(0, components)
self._init_components = self.components_.copy()
self._init_weights = self.weights_.copy()
self._init_stretch = self.stretch_.copy()
# Second-order spline: Tridiagonal (-2 on diags, 1 on sub/superdiags)
self._spline_smooth_operator = 0.25 * diags(
[1, -2, 1],
offsets=[0, 1, 2],
shape=(self.n_signals_ - 2, self.n_signals_),
dtype=float,
)
def fit(
self,
source_matrix,
init_weights=None,
init_components=None,
init_stretch=None,
reset=True,
):
"""Run the sNMF optimization on ``source_matrix``.
Parameters
----------
source_matrix : ndarray of shape (signal_length, n_signals)
The source data matrix to decompose.
init_weights : ndarray, optional
The initial weights matrix of shape
``(n_components, n_signals)``.
init_components : ndarray, optional
Optional initial components matrix of shape
``(signal_length, n_components)``.
init_stretch : ndarray, optional
The initial stretch matrix of shape
``(n_components, n_signals)``.
reset : bool
Whether to reinitialize model factors before fitting. If ``False``,
the previous factor matrices are reused.
"""
source_matrix = np.asarray(source_matrix, dtype=float)
if source_matrix.ndim != 2:
raise ValueError("source_matrix must be a 2D array.")
self.converged_ = False
self._source_matrix = source_matrix
if reset:
self._initialize_factors(
source_matrix=source_matrix,
init_weights=init_weights,
init_components=init_components,
init_stretch=init_stretch,
)
else:
if any(
v is not None
for v in (init_weights, init_components, init_stretch)
):
raise ValueError(
"init_weights, init_components, and init_stretch can only "
"be provided when reset=True."
)
if not all(
hasattr(self, name)
for name in (
"components_",
"weights_",
"stretch_",
"n_components_",
"signal_length_",
"n_signals_",
"_spline_smooth_operator",
)
):
raise ValueError(
"Cannot warm-start before initialization. Call fit with "
"reset=True first."
)
expected_shape = (self.signal_length_, self.n_signals_)
if source_matrix.shape != expected_shape:
raise ValueError(
"Warm-start requires source_matrix to keep the same shape "
f"{expected_shape}, got {source_matrix.shape}."
)
# Set stretch matrix to 1 if no stretching present
if self.rho == 0:
self.stretch_ = np.ones_like(self.stretch_)
# Set up residual matrix, objective function, and history
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.best_objective_ = self.objective_function_
self.best_matrices_ = [
self.components_.copy(),
self.weights_.copy(),
self.stretch_.copy(),
]
self.objective_difference_ = None
self.objective_log = [
{
"step": "start",
"iteration": 0,
"objective": self.objective_function_,
"timestamp": time.time(),
}
]
# Set up tracking variables for _update_components()
self._prev_components = None
self._grad_components = np.zeros_like(self.components_)
self._prev_grad_components = np.zeros_like(self.components_)
self.n_iter_ = 0
regularization_term = (
0.5
* self.rho
* np.linalg.norm(
self._spline_smooth_operator @ self.stretch_.T, "fro"
)
** 2
)
sparsity_term = self.eta * np.sum(
np.sqrt(self.components_)
) # Square root penalty
base_obj = (
self.objective_function_ - regularization_term - sparsity_term
)
if self.verbose:
print(
f"\n--- Start ---"
f"\nTotal Objective : {self.objective_function_:.5e}"
f"\nBase Obj (No Reg) : {base_obj:.5e}"
)
# Main optimization loop
for outiter in range(self.max_iter):
self._outer_iter = outiter
self._outer_loop()
self.n_iter_ = outiter + 1
# Print diagnostics
regularization_term = (
0.5
* self.rho
* np.linalg.norm(
self._spline_smooth_operator @ self.stretch_.T, "fro"
)
** 2
)
sparsity_term = self.eta * np.sum(
np.sqrt(self.components_)
) # Square root penalty
base_obj = (
self.objective_function_ - regularization_term - sparsity_term
)
convergence_threshold = self.objective_function_ * self.tol
# Convergence check: Stop if diffun is small
# and at least min_iter iterations have passed
if self.verbose:
print(
f"\n--- Iteration {self._outer_iter} ---"
f"\nTotal Objective : {self.objective_function_:.5e}"
f"\nBase Obj (No Reg) : {base_obj:.5e}"
"\nConvergence Check : Δ "
f"({self.objective_difference_:.2e})"
f" < Threshold ({convergence_threshold:.2e})\n"
)
if (
self.objective_difference_ < convergence_threshold
and outiter >= self.min_iter
):
self.converged_ = True
break
self._normalize_results()
self.reconstruction_err_ = np.linalg.norm(self.residuals_, "fro")
return self
def _normalize_results(self):
if self.verbose:
print("\nNormalizing results after convergence...")
# Select our best results for normalization
self.components_ = self.best_matrices_[0]
self.weights_ = self.best_matrices_[1]
self.stretch_ = self.best_matrices_[2]
# Normalize weights/stretch first
weights_row_max = np.max(self.weights_, axis=1, keepdims=True)
self.weights_ = self.weights_ / weights_row_max
stretch_row_max = np.max(self.stretch_, axis=1, keepdims=True)
self.stretch_ = self.stretch_ / stretch_row_max
# re-running with component updates only vs normalized weights/stretch
self._grad_components = np.zeros_like(
self.components_
) # Gradient of X (zeros for now)
self._prev_grad_components = np.zeros_like(
self.components_
) # Previous gradient of X (zeros for now)
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.objective_difference_ = None
self._objective_history = [self.objective_function_]
self._outer_iter = 0
self._inner_iter = 0
for outiter in range(self.max_iter):
self._outer_iter = outiter
if outiter == 1:
self._inner_iter = (
1 # So step size can adapt without an inner loop
)
self._update_components()
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.objective_log.append(
{
"step": "c_norm",
"iteration": outiter,
"objective": self.objective_function_,
"timestamp": time.time(),
}
)
self.objective_difference_ = (
self.objective_log[-2]["objective"]
- self.objective_log[-1]["objective"]
)
if self._plotter is not None:
self._plotter.update(
components=self.components_,
weights=self.weights_,
stretch=self.stretch_,
update_tag="normalize components",
)
convergence_threshold = self.objective_function_ * self.tol
if self.verbose:
print(
f"\n--- Iteration {outiter} after normalization---"
f"\nTotal Objective : {self.objective_function_:.5e}"
"\nConvergence Check : Δ "
f"({self.objective_difference_:.2e})"
f" < Threshold ({convergence_threshold:.2e})\n"
)
if (
self.objective_difference_ < convergence_threshold
and outiter >= 7
):
break
def _outer_loop(self):
if self.verbose:
print("Updating components and weights...")
for inner_iter in range(4):
self._inner_iter = inner_iter
self._prev_grad_components = self._grad_components.copy()
self._update_components()
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.objective_log.append(
{
"step": "c",
"iteration": self._outer_iter,
"objective": self.objective_function_,
"timestamp": time.time(),
}
)
self.objective_difference_ = (
self.objective_log[-2]["objective"]
- self.objective_log[-1]["objective"]
)
if self.objective_function_ < self.best_objective_:
self.best_objective_ = self.objective_function_
self.best_matrices_ = [
self.components_.copy(),
self.weights_.copy(),
self.stretch_.copy(),
]
if self._plotter is not None:
self._plotter.update(
components=self.components_,
weights=self.weights_,
stretch=self.stretch_,
update_tag="components",
)
self._update_weights()
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.objective_log.append(
{
"step": "w",
"iteration": self._outer_iter,
"objective": self.objective_function_,
"timestamp": time.time(),
}
)
self.objective_difference_ = (
self.objective_log[-2]["objective"]
- self.objective_log[-1]["objective"]
)
if self.objective_function_ < self.best_objective_:
self.best_objective_ = self.objective_function_
self.best_matrices_ = [
self.components_.copy(),
self.weights_.copy(),
self.stretch_.copy(),
]
if self._plotter is not None:
self._plotter.update(
components=self.components_,
weights=self.weights_,
stretch=self.stretch_,
update_tag="weights",
)
self.objective_difference_ = (
self.objective_log[-2]["objective"]
- self.objective_log[-1]["objective"]
)
if (
self.objective_log[-3]["objective"] - self.objective_function_
< self.objective_difference_ * 1e-3
):
break
# Skip updating stretch if no stretching factor
if not self.rho == 0:
self._update_stretch()
self.residuals_ = self._get_residual_matrix()
self.objective_function_ = self._get_objective_function()
self.objective_log.append(
{
"step": "s",
"iteration": self._outer_iter,
"objective": self.objective_function_,
"timestamp": time.time(),
}
)
self.objective_difference_ = (
self.objective_log[-2]["objective"]
- self.objective_log[-1]["objective"]
)
if self.objective_function_ < self.best_objective_:
self.best_objective_ = self.objective_function_
self.best_matrices_ = [
self.components_.copy(),
self.weights_.copy(),
self.stretch_.copy(),
]
if self._plotter is not None:
self._plotter.update(
components=self.components_,
weights=self.weights_,
stretch=self.stretch_,
update_tag="stretch",
)
def _get_residual_matrix(
self, components=None, weights=None, stretch=None
):
"""Return the residuals (difference) between the source matrix
and its reconstruction.
Parameters
----------
components : (signal_len, n_components) array, optional
weights : (n_components, n_signals) array, optional
stretch : (n_components, n_signals) array, optional
Returns
-------
residuals : (signal_len, n_signals) array
"""
if components is None:
components = self.components_
if weights is None:
weights = self.weights_
if stretch is None:
stretch = self.stretch_
reconstructed_matrix = _reconstruct_matrix(
components, weights, stretch
)
residuals = reconstructed_matrix - self._source_matrix
return residuals
def _get_objective_function(self, residuals=None, stretch=None):
"""Return the objective value, passing stored attributes or
overrides to _compute_objective_function().
Parameters
----------
residuals : ndarray, optional
Residual matrix to use instead of self.residuals_.
stretch : ndarray, optional
Stretch matrix to use instead of self.stretch_.
Returns
-------
float
Current objective function value.
"""
return SNMFOptimizer._compute_objective_function(
components=self.components_,
residuals=self.residuals_ if residuals is None else residuals,
stretch=self.stretch_ if stretch is None else stretch,
rho=self.rho,
eta=self.eta,
spline_smooth_operator=self._spline_smooth_operator,
)
def _compute_stretched_components(
self, components=None, weights=None, stretch=None
):
"""Interpolates each component along its sample axis according
to per-(component, signal) stretch factors, then applies
per-(component, signal) weights. Also computes the first and
second derivatives with respect to stretch. Left and right,
respectively, refer to the sample prior to and subsequent to the
interpolated sample's position.
Inputs
------
components : array, shape (signal_len, n_components)
Each column is a component with signal_len samples.
weights : array, shape (n_components, n_signals)
Per-(component, signal) weights.
stretch : array, shape (n_components, n_signals)
Per-(component, signal) stretch factors.
Outputs
-------
stretched_components : array, shape (signal_len, n_comps * n_sigs)
Interpolated and weighted components.
d_stretched_components : array, shape (signal_len, n_comps * n_sigs)
First derivatives with respect to stretch.
dd_stretched_components : array, shape (signal_len, n_comps * n_sigs)
Second derivatives with respect to stretch.
"""
# --- Defaults ---
if components is None:
components = self.components_
if weights is None:
weights = self.weights_
if stretch is None:
stretch = self.stretch_
# Dimensions
signal_len = components.shape[0] # number of samples
n_components = components.shape[1] # number of components
n_signals = weights.shape[1] # number of signals
# Guard stretches
eps = 1e-8
stretch = np.clip(stretch, eps, None)
stretch_inv = 1.0 / stretch
# Apply stretching to the original sample indices,
# represented as a "time-stretch"
t = (
np.arange(signal_len, dtype=float)[:, None, None]
* stretch_inv[None, :, :]
)
# has shape (signal_len, n_components, n_signals)
# For each stretched coordinate, find its prior integer (original)
# index and their difference
i0 = np.floor(t).astype(np.int64) # prior original index
alpha = t - i0.astype(float) # fractional distance between left/right
# Clip indices to range (0, signal_len - 1) to maintain original size
max_idx = signal_len - 1
i0 = np.clip(i0, 0, max_idx)
i1 = np.clip(i0 + 1, 0, max_idx)
# Gather sample values
comps_3d = components[
:, :, None
] # expand components by a dim for broadcasting across n_signals
c0 = np.take_along_axis(comps_3d, i0, axis=0) # left sample values
c1 = np.take_along_axis(comps_3d, i1, axis=0) # right sample values
# Linear interpolation to determine stretched sample values
interp = c0 * (1.0 - alpha) + c1 * alpha
interp_weighted = interp * weights[None, :, :]
# Derivatives
di = -t * stretch_inv[None, :, :] # first-derivative coefficient
ddi = (
-di * stretch_inv[None, :, :] * 2.0
) # second-derivative coefficient
d_unweighted = c0 * (-di) + c1 * di
dd_unweighted = c0 * (-ddi) + c1 * ddi
d_weighted = d_unweighted * weights[None, :, :]
dd_weighted = dd_unweighted * weights[None, :, :]
# Flatten back to expected shape (signal_len, n_components * n_signals)
return (
interp_weighted.reshape(signal_len, n_components * n_signals),
d_weighted.reshape(signal_len, n_components * n_signals),
dd_weighted.reshape(signal_len, n_components * n_signals),
)
def _apply_transformation_matrix(
self, stretch=None, weights=None, residuals=None
):
"""Computes the transformation matrix `stretch_transformed` for
residuals, using scaling matrix `stretch` and weight
coefficients `weights`."""
if stretch is None:
stretch = self.stretch_
if weights is None:
weights = self.weights_
if residuals is None:
residuals = self.residuals_
# Compute scaling matrix
stretch_tiled = np.tile(
stretch.reshape(1, self.n_signals_ * self.n_components_, order="F")
** -1,
(self.signal_length_, 1),
)
# Compute indices
indices = np.arange(self.signal_length_)[:, None] * stretch_tiled
# Weighting coefficients
weights_tiled = np.tile(
weights.reshape(
1, self.n_signals_ * self.n_components_, order="F"
),
(self.signal_length_, 1),
)
# Compute floor indices
floor_indices = np.floor(indices).astype(int)
floor_indices_1 = np.minimum(floor_indices + 1, self.signal_length_)
floor_indices_2 = np.minimum(floor_indices_1 + 1, self.signal_length_)
# Compute fractional part
fractional_indices = indices - floor_indices
# Expand row indices
repm = np.tile(
np.arange(self.n_components_),
(self.signal_length_, self.n_signals_),
)
# Compute transformations
kron = np.kron(residuals, np.ones((1, self.n_components_)))
fractional_kron = kron * fractional_indices
fractional_weights = (fractional_indices - 1) * weights_tiled
# Construct sparse matrices
x2 = coo_matrix(
(
(-kron * fractional_weights).flatten(),
(floor_indices_1.flatten() - 1, repm.flatten()),
),
shape=(self.signal_length_ + 1, self.n_components_),
).tocsc()
x3 = coo_matrix(
(
(fractional_kron * weights_tiled).flatten(),
(floor_indices_2.flatten() - 1, repm.flatten()),
),
shape=(self.signal_length_ + 1, self.n_components_),
).tocsc()
# Combine the last row into previous, then remove the last row
x2[self.signal_length_ - 1, :] += x2[self.signal_length_, :]
x3[self.signal_length_ - 1, :] += x3[self.signal_length_, :]
x2 = x2[:-1, :]
x3 = x3[:-1, :]
stretch_transformed = x2 + x3
return stretch_transformed
def _solve_quadratic_program(self, t, m):
"""
Solves the quadratic program for updating y in stretched NMF:
min J(y) = 0.5 * y^T q y + d^T y
subject to: 0 ≤ y ≤ 1
Parameters:
- t: (N, k) ndarray
- source_matrix_col: (N,) column of source_matrix for the
corresponding m
Returns:
- y: (k,) optimal solution
"""
source_matrix_col = self._source_matrix[:, m]
# Compute q and d
q = t.T @ t # Gram matrix (k x k)
d = -t.T @ source_matrix_col # Linear term (k,)
k = q.shape[0] # Number of variables
# Regularize q to ensure positive semi-definiteness
reg_factor = 1e-8 * np.linalg.norm(
q, ord="fro"
) # Adaptive regularization, original was fixed
q += np.eye(k) * reg_factor
# Define optimization variable
y = cp.Variable(k)
# Define quadratic objective
objective = cp.Minimize(0.5 * cp.quad_form(y, q) + d.T @ y)
# Define constraints (0 ≤ y ≤ 1)
constraints = [y >= 0, y <= 1]
# Solve using a QP solver
prob = cp.Problem(objective, constraints)
prob.solve(
solver=cp.OSQP,
verbose=False,
polish=False, # TODO keep? removes polish message
# solver_verbose=False
)
# Get the solution
return np.maximum(
y.value, 0
) # Ensure non-negative values in case of solver tolerance issues
def _update_components(self):
"""Updates `components` using gradient-based optimization with
adaptive step size."""
# Compute stretched components using the interpolation function
stretched_components, _, _ = (
self._compute_stretched_components()
) # Discard the derivatives
# Compute reshaped_stretched_components and component_residuals
intermediate_reshaped = stretched_components.flatten(
order="F"
).reshape(
(self.signal_length_ * self.n_signals_, self.n_components_),
order="F",
)
reshaped_stretched_components = intermediate_reshaped.sum(
axis=1
).reshape((self.signal_length_, self.n_signals_), order="F")
component_residuals = (
reshaped_stretched_components - self._source_matrix
)
# Compute gradient
self._grad_components = self._apply_transformation_matrix(
residuals=component_residuals
).toarray() # toarray equivalent of full, make non-sparse
# Compute initial step size `initial_step_size`
initial_step_size = np.linalg.eigvalsh(
self.weights_.T @ self.weights_
).max() * np.max([self.stretch_.max(), 1 / self.stretch_.min()])
# Compute adaptive step size `step_size`
if self._outer_iter == 0 and self._inner_iter == 0:
step_size = initial_step_size
else:
num = np.sum(
(self._grad_components - self._prev_grad_components)
* (self.components_ - self._prev_components)
) # Element-wise multiplication
denom = (
np.linalg.norm(self.components_ - self._prev_components, "fro")
** 2
) # Frobenius norm squared
step_size = num / denom if denom > 0 else initial_step_size
if step_size <= 0:
step_size = initial_step_size
# Store our old X before updating because it is used in step selection
self._prev_components = self.components_.copy()
while True: # iterate updating components
components_step = (
self._prev_components - self._grad_components / step_size
)
# Solve x^3 + p*x + q = 0 for the largest real root
self.components_ = np.square(
_cubic_largest_real_root(
-components_step, self.eta / (2 * step_size)
)
)
# Mask values that should be set to zero
mask = (
self.components_**2 * step_size / 2
- step_size * self.components_ * components_step
+ self.eta * np.sqrt(self.components_)
< 0
)
self.components_ = mask * self.components_
objective_improvement = self.objective_log[-1][
"objective"
] - self._get_objective_function(
residuals=self._get_residual_matrix()
)
# Check if objective function improves
if objective_improvement > 0:
break
# If not, increase step_size (step size)
step_size *= 2
if np.isinf(step_size):
break
def _update_weights(self):
"""Updates weights by building the stretched component matrix
`stretched_comps` with np.interp and solving a quadratic program
for each signal."""
sample_indices = np.arange(self.signal_length_)
for signal in range(self.n_signals_):
# Stretch factors for this signal across components:
this_stretch = self.stretch_[:, signal]
# Build stretched_comps[:, k] by interpolating component at frac.
# pos. index / this_stretch[comp]
stretched_comps = np.empty(
(self.signal_length_, self.n_components_),
dtype=self.components_.dtype,
)
for comp in range(self.n_components_):
pos = sample_indices / this_stretch[comp]
stretched_comps[:, comp] = np.interp(
pos,
sample_indices,
self.components_[:, comp],
left=self.components_[0, comp],
right=self.components_[-1, comp],
)
# Solve quadratic problem for a given signal and update its weight
new_weight = self._solve_quadratic_program(
t=stretched_comps, m=signal
)
self.weights_[:, signal] = new_weight
def _regularize_function(self, stretch=None):
if stretch is None:
stretch = self.stretch_
stretched_components, d_stretch_comps, dd_stretch_comps = (
self._compute_stretched_components(stretch=stretch)
)
intermediate = stretched_components.flatten(order="F").reshape(
(self.signal_length_ * self.n_signals_, self.n_components_),
order="F",
)
residuals = (
intermediate.sum(axis=1).reshape(
(self.signal_length_, self.n_signals_), order="F"
)
- self._source_matrix
)
fun = self._get_objective_function(residuals, stretch)
tiled_res = np.tile(residuals, (1, self.n_components_))
grad_flat = np.sum(d_stretch_comps * tiled_res, axis=0)
gra = grad_flat.reshape(
(self.n_signals_, self.n_components_), order="F"
).T
gra += (
self.rho
* stretch
@ (self._spline_smooth_operator.T @ self._spline_smooth_operator)
)
# Hessian would go here
return fun, gra
def _update_stretch(self):
"""Updates stretching matrix using constrained optimization