Skip to content

Commit 6a520b4

Browse files
author
John Halloran
committed
Updated X and Y optimizers, now produces formatted but partially incorrect output
1 parent 8ce263f commit 6a520b4

File tree

3 files changed

+622
-47
lines changed

3 files changed

+622
-47
lines changed

src/diffpy/snmf/main.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,17 @@
11
import numpy as np
2-
import pandas as pd
3-
import snmf
2+
import snmf_class
43

5-
# Define fixed feature matrix (X) with distinct, structured features
6-
X = np.array(
7-
[
8-
[10, 0, 0], # First component dominates first feature
9-
[0, 8, 0], # Second component dominates second feature
10-
[0, 0, 6], # Third component dominates third feature
11-
[4, 4, 0], # Mixed contribution to the fourth feature
12-
[3, 2, 5], # Mixed contribution to the fifth feature
13-
],
14-
dtype=float,
15-
)
16-
17-
# Define fixed coefficient matrix (Y) representing weights
18-
Y = np.array(
19-
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]],
20-
dtype=float,
21-
)
22-
23-
# Compute the resulting data matrix M
24-
MM = np.dot(X, Y)
25-
26-
# Normalize matrices X, Y, and M to be between 0 and 1
27-
X_norm = (X - X.min()) / (X.max() - X.min())
28-
Y_norm = (Y - Y.min()) / (Y.max() - Y.min())
29-
MM_norm = (MM - MM.min()) / (MM.max() - MM.min())
30-
31-
# Generate an initial guess Y0 with slightly perturbed values
32-
Y0 = np.array(
33-
[
34-
[1.5, 1.8, 2.9, 3.6, 4.8, 5.7, 7.1, 8.2, 9.4, 10.3],
35-
[2.2, 4.1, 5.9, 8.1, 9.8, 11.9, 14.2, 16.5, 18.1, 19.7],
36-
[2.7, 5.5, 8.8, 11.5, 14.6, 17.8, 20.5, 23.9, 26.3, 29.2],
37-
],
38-
dtype=float,
39-
)
40-
41-
# Normalize Y0 as well
42-
Y0_norm = (Y0 - Y0.min()) / (Y0.max() - Y0.min())
4+
X0 = np.loadtxt("inputs/my_X0_4.txt", dtype=float)
5+
MM = np.loadtxt("inputs/my_MM_4.txt", dtype=float)
6+
A0 = np.loadtxt("inputs/my_A0_4.txt", dtype=float)
7+
Y0 = np.loadtxt("inputs/my_W0_4.txt", dtype=float)
8+
N, M = MM.shape
439

4410
# Convert to DataFrames for display
45-
df_X = pd.DataFrame(X, columns=[f"Comp_{i+1}" for i in range(X.shape[1])])
46-
df_Y = pd.DataFrame(Y, columns=[f"Sample_{i+1}" for i in range(Y.shape[1])])
47-
df_MM = pd.DataFrame(MM, columns=[f"Sample_{i+1}" for i in range(MM.shape[1])])
48-
df_Y0 = pd.DataFrame(Y0, columns=[f"Sample_{i+1}" for i in range(Y0.shape[1])])
11+
# df_X = pd.DataFrame(X, columns=[f"Comp_{i+1}" for i in range(X.shape[1])])
12+
# df_Y = pd.DataFrame(Y, columns=[f"Sample_{i+1}" for i in range(Y.shape[1])])
13+
# df_MM = pd.DataFrame(MM, columns=[f"Sample_{i+1}" for i in range(MM.shape[1])])
14+
# df_Y0 = pd.DataFrame(Y0, columns=[f"Sample_{i+1}" for i in range(Y0.shape[1])])
4915

5016
# Print the matrices
5117
"""
@@ -55,8 +21,11 @@
5521
print("Initial Guess (Y0):\n", df_Y0, "\n")
5622
"""
5723

58-
my_model = snmf.SNMFOptimizer(MM_norm, Y0_norm)
24+
my_model = snmf_class.SNMFOptimizer(MM=MM, Y0=Y0, X0=X0, A=A0)
5925
print(f"My final guess for X: {my_model.X}")
6026
print(f"My final guess for Y: {my_model.Y}")
61-
print(f"Compare to true X: {X_norm}")
62-
print(f"Compare to true Y: {Y_norm}")
27+
# print(f"Compare to true X: {X_norm}")
28+
# print(f"Compare to true Y: {Y_norm}")
29+
np.savetxt("my_new_X.txt", my_model.X, fmt="%.6g", delimiter=" ")
30+
np.savetxt("my_new_Y.txt", my_model.Y, fmt="%.6g", delimiter=" ")
31+
np.savetxt("my_new_A.txt", my_model.A, fmt="%.6g", delimiter=" ")

src/diffpy/snmf/old_main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
import pandas as pd
3+
import snmf
4+
5+
# Define fixed feature matrix (X) with distinct, structured features
6+
X = np.array(
7+
[
8+
[10, 0, 0], # First component dominates first feature
9+
[0, 8, 0], # Second component dominates second feature
10+
[0, 0, 6], # Third component dominates third feature
11+
[4, 4, 0], # Mixed contribution to the fourth feature
12+
[3, 2, 5], # Mixed contribution to the fifth feature
13+
],
14+
dtype=float,
15+
)
16+
17+
# Define fixed coefficient matrix (Y) representing weights
18+
Y = np.array(
19+
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]],
20+
dtype=float,
21+
)
22+
23+
# Compute the resulting data matrix M
24+
MM = np.dot(X, Y)
25+
26+
# Normalize matrices X, Y, and M to be between 0 and 1
27+
X_norm = (X - X.min()) / (X.max() - X.min())
28+
Y_norm = (Y - Y.min()) / (Y.max() - Y.min())
29+
MM_norm = (MM - MM.min()) / (MM.max() - MM.min())
30+
31+
# Generate an initial guess Y0 with slightly perturbed values
32+
Y0 = np.array(
33+
[
34+
[1.5, 1.8, 2.9, 3.6, 4.8, 5.7, 7.1, 8.2, 9.4, 10.3],
35+
[2.2, 4.1, 5.9, 8.1, 9.8, 11.9, 14.2, 16.5, 18.1, 19.7],
36+
[2.7, 5.5, 8.8, 11.5, 14.6, 17.8, 20.5, 23.9, 26.3, 29.2],
37+
],
38+
dtype=float,
39+
)
40+
41+
# Normalize Y0 as well
42+
Y0_norm = (Y0 - Y0.min()) / (Y0.max() - Y0.min())
43+
44+
# Convert to DataFrames for display
45+
df_X = pd.DataFrame(X, columns=[f"Comp_{i+1}" for i in range(X.shape[1])])
46+
df_Y = pd.DataFrame(Y, columns=[f"Sample_{i+1}" for i in range(Y.shape[1])])
47+
df_MM = pd.DataFrame(MM, columns=[f"Sample_{i+1}" for i in range(MM.shape[1])])
48+
df_Y0 = pd.DataFrame(Y0, columns=[f"Sample_{i+1}" for i in range(Y0.shape[1])])
49+
50+
# Print the matrices
51+
"""
52+
print("Feature Matrix (X):\n", df_X, "\n")
53+
print("Coefficient Matrix (Y):\n", df_Y, "\n")
54+
print("Data Matrix (MM):\n", df_MM, "\n")
55+
print("Initial Guess (Y0):\n", df_Y0, "\n")
56+
"""
57+
58+
my_model = snmf.SNMFOptimizer(MM_norm, Y0_norm)
59+
print(f"My final guess for X: {my_model.X}")
60+
print(f"My final guess for Y: {my_model.Y}")
61+
print(f"Compare to true X: {X_norm}")
62+
print(f"Compare to true Y: {Y_norm}")

0 commit comments

Comments
 (0)