-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from meyer-lab/op_for_4d
OP for 4D tensor
- Loading branch information
Showing
9 changed files
with
146 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import autograd.numpy as anp | ||
import numpy as np | ||
import pymanopt | ||
from pymanopt import Problem | ||
from pymanopt.manifolds import Stiefel | ||
from pymanopt.optimizers import ConjugateGradient | ||
|
||
|
||
def project_data(tensor: np.ndarray, proj_matrix: np.ndarray) -> np.ndarray: | ||
""" | ||
Projects a 3D tensor of C x C x LR with a projection matrix of C x CES | ||
along both C dimensions to form a resulting tensor of CES x CES x LR. | ||
""" | ||
return np.einsum("ab,cd,acg->bdg", proj_matrix, proj_matrix, tensor) | ||
|
||
|
||
def solve_projections( | ||
X_list: list, | ||
full_tensor: np.ndarray, | ||
) -> list[np.ndarray]: | ||
""" | ||
Takes a list of 3D tensors of C x C x LR, a means matrix, factors of | ||
A: obs x rank | ||
B: CES x rank | ||
C: CES x rank | ||
D: LR x rank | ||
and solves for the projection matrices for each tensor as well as | ||
reconstruct the data based on the projection matrices. | ||
""" | ||
projections: list[np.ndarray] = [] | ||
|
||
for i, mat in enumerate(X_list): | ||
manifold = Stiefel(mat.shape[0], full_tensor.shape[1]) | ||
a_mat = anp.asarray(mat) | ||
a_lhs = anp.asarray(full_tensor[i, :, :, :]) | ||
|
||
@pymanopt.function.autograd(manifold) | ||
def objective_function(proj): | ||
a_mat_recon = anp.einsum("ba,dc,acg->bdg", proj, proj, a_lhs) | ||
return anp.sum(anp.square(a_mat - a_mat_recon)) | ||
|
||
problem = Problem(manifold=manifold, cost=objective_function) | ||
|
||
# Solve the problem | ||
solver = ConjugateGradient( | ||
verbosity=1, min_gradient_norm=1e-9, min_step_size=1e-12 | ||
) | ||
proj = solver.run(problem).point | ||
|
||
U, _, Vt = np.linalg.svd(proj, full_matrices=False) | ||
|
||
proj = U @ Vt | ||
|
||
projections.append(proj) | ||
|
||
return projections |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import numpy as np | ||
|
||
from ..cc_pf2 import project_data, solve_projections | ||
|
||
|
||
def test_project_data(): | ||
""" | ||
Tests that the dimensions are correct and that the method is able to run without errors. | ||
""" | ||
|
||
# Define dimensions | ||
cells = 20 | ||
LR = 10 | ||
rank = 5 | ||
|
||
# Generate random X_list | ||
X_mat = np.random.rand(cells, cells, LR) | ||
|
||
# Projection matrix | ||
proj_matrix = np.linalg.qr(np.random.rand(cells, rank))[0] | ||
|
||
# Call the project_data method | ||
print(proj_matrix.shape) | ||
projected_X = project_data(X_mat, proj_matrix) | ||
|
||
assert projected_X.shape == (rank, rank, LR) | ||
|
||
|
||
def test_project_data_output_proj_matrix(): | ||
""" | ||
Tests that the project data method is actually able to solve for the correct optimal projection matrix. | ||
Asserts that the projection matrices solved are the same. | ||
""" | ||
# Define dimensions | ||
num_tensors = 3 | ||
cells = 20 | ||
variables = 10 | ||
obs = 20 | ||
rank = 5 | ||
# Generate a random projected tensor | ||
projected_X = np.random.rand(obs, rank, rank, variables) | ||
|
||
# Generate a random set of projection matrices | ||
projections = [ | ||
np.linalg.qr(np.random.rand(cells, rank))[0] for _ in range(num_tensors) | ||
] | ||
|
||
# Recreate the original tensor using the projection matrices and projected tensor | ||
recreated_tensors = [] | ||
for i in range(num_tensors): | ||
Q = projections[i] | ||
A = projected_X[i, :, :, :] | ||
B = project_data(A, Q.T) | ||
recreated_tensors.append(B) | ||
|
||
# Call the project_data method using the recreated tensors to get the projected_X that gets solved by our method | ||
projections_recreated = solve_projections( | ||
recreated_tensors, | ||
projected_X, | ||
) | ||
|
||
# Assert that the projections are the same | ||
for i in range(num_tensors): | ||
sign_correct = np.sign(projections[i][0, 0] * projections_recreated[i][0, 0]) | ||
np.testing.assert_allclose( | ||
projections[i], projections_recreated[i] * sign_correct, atol=1e-9 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters