test: add initial test of optimizer by john-halloran · Pull Request #167 · diffpy/diffpy.stretched-nmf · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pyproject.toml
2 changes: 2 additions & 0 deletions src/diffpy/snmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# package version
from diffpy.snmf.version import __version__

__all__ = ["__version__", "SNMFOptimizer"]

# silence the pyflakes syntax checker
assert __version__ or True

Expand Down
11 changes: 6 additions & 5 deletions src/diffpy/snmf/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np
from snmf_class import SNMFOptimizer

from diffpy.snmf.snmf_class import SNMFOptimizer

# Example input files (not provided)
init_components_file = np.loadtxt("input/init_components.txt", dtype=float)
source_matrix_file = np.loadtxt("input/source_matrix.txt", dtype=float)
init_stretch_file = np.loadtxt("input/init_stretch.txt", dtype=float)
init_weights_file = np.loadtxt("input/init_weights.txt", dtype=float)
init_components_file = np.loadtxt("inputs/init_components.txt", dtype=float)
source_matrix_file = np.loadtxt("inputs/source_matrix.txt", dtype=float)
init_stretch_file = np.loadtxt("inputs/init_stretch.txt", dtype=float)
init_weights_file = np.loadtxt("inputs/init_weights.txt", dtype=float)

my_model = SNMFOptimizer(
source_matrix=source_matrix_file,
Expand Down
5 changes: 3 additions & 2 deletions src/diffpy/snmf/snmf_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import cvxpy as cp
import numpy as np
from plotter import SNMFPlotter
from scipy.optimize import minimize
from scipy.sparse import coo_matrix, diags

from diffpy.snmf.plotter import SNMFPlotter


class SNMFOptimizer:
"""An implementation of stretched NMF (sNMF), including sparse stretched NMF.
Expand Down Expand Up @@ -625,7 +626,7 @@ def update_stretch(self):
Updates matrix A using constrained optimization (equivalent to fmincon in MATLAB).
"""

# Flatten A for compatibility with the optimizer (since SciPy expects 1D input)
# Flatten A for compatibility with the optimizer (since SciPy expects 1D inputs)
stretch_flat_initial = self.stretch.flatten()

# Define the optimization function
Expand Down
21 changes: 21 additions & 0 deletions tests/inputs/test_snmf_optimizer/README
Loading