Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make them private #117

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
31 changes: 4 additions & 27 deletions romtools/trial_space/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,22 @@

- `TrialSpaceFromScaledPOD`: POD trial space computed via scaled SVD.

which derive from the abstract class `TrialSpace`. Additionally, we provide two helpers free-functions:

- `tensor_to_matrix`: converts a tensor with shape $[N, M, P]$ to a matrix
representation in which the first two dimension are collapsed $[N M, P]$

- `matrix_to_tensor`: inverse operation of `tensor_to_matrix`
which derive from the abstract class `TrialSpace`.

---
##**API**
'''

import abc
import numpy as np
from romtools.trial_space.utils import tensor_to_matrix, matrix_to_tensor
from romtools.trial_space.utils.truncater import *
from romtools.trial_space.utils.shifter import *
from romtools.trial_space.utils.scaler import *
from romtools.trial_space.utils.splitter import *
from romtools.trial_space.utils.orthogonalizer import *


class TrialSpace(abc.ABC):
'''
Abstract base class for trial space implementations.
Expand All @@ -127,7 +124,7 @@ def get_shift_vector(self):
Retrieves the shift vector of the trial space.

Returns:
`np.ndarray`: The shift vector in tensorm form.
`np.ndarray`: The shift vector in tensor form.

Concrete subclasses should implement this method to return the shift
vector specific to their trial space implementation.
Expand Down Expand Up @@ -356,23 +353,3 @@ def get_basis(self):
Concrete implementation of `TrialSpace.get_basis()`
'''
return self.__basis


def tensor_to_matrix(tensor_input):
'''
Converts a tensor with shape $[N, M, P]$ to a matrix representation
in which the first two dimension are collapsed $[N M, P]$.
'''
output_tensor = tensor_input.reshape(tensor_input.shape[0]*tensor_input.shape[1],
tensor_input.shape[2])
return output_tensor


def matrix_to_tensor(n_var, matrix_input):
'''
Inverse operation of `tensor_to_matrix`
'''
d1 = int(matrix_input.shape[0] / n_var)
d2 = matrix_input.shape[1]
output_matrix = matrix_input.reshape(n_var, d1, d2)
return output_matrix
21 changes: 21 additions & 0 deletions romtools/trial_space/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,24 @@
from romtools.trial_space.utils.splitter import *
from romtools.trial_space.utils.truncater import *
from romtools.trial_space.utils.svd_method_of_snapshots import *

def tensor_to_matrix(tensor_input):
'''
@private
Converts a tensor with shape $[N, M, P]$ to a matrix representation
in which the first two dimension are collapsed $[N M, P]$.
'''
output_tensor = tensor_input.reshape(tensor_input.shape[0]*tensor_input.shape[1],
tensor_input.shape[2])
return output_tensor


def matrix_to_tensor(n_var, matrix_input):
'''
@private
Inverse operation of `__tensor_to_matrix`
'''
d1 = int(matrix_input.shape[0] / n_var)
d2 = matrix_input.shape[1]
output_matrix = matrix_input.reshape(n_var, d1, d2)
return output_matrix
18 changes: 6 additions & 12 deletions tests/romtools/test_trial_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
# assert matrix.shape[0] == 15
# assert matrix.shape[1] == 7


def tensor_to_matrix(tensor_input):
return tensor_input.reshape(tensor_input.shape[0]*tensor_input.shape[1],
tensor_input.shape[2])


@pytest.mark.mpi_skip
def test_dictionary_trial_space():
snapshots = np.random.normal(size=(3, 8, 6))
Expand Down Expand Up @@ -74,7 +68,7 @@ def test_dictionary_trial_space():
np.mean(snapshots, axis=2))
assert np.allclose(my_trial_space.get_dimension(), 12)
basis = my_trial_space.get_basis()
basis = tensor_to_matrix(basis)
basis = utils.tensor_to_matrix(basis)
assert np.allclose(basis.transpose() @ basis, np.eye(12))


Expand All @@ -91,7 +85,7 @@ def test_trial_space_from_pod():
my_splitter,
my_orthogonalizer)
# truth trial space
snapshotMatrix = tensor_to_matrix(snapshots)
snapshotMatrix = utils.tensor_to_matrix(snapshots)
u, s, v = np.linalg.svd(snapshotMatrix, full_matrices=False)
basis_tensor = my_trial_space.get_basis()
assert np.allclose(u.reshape(basis_tensor.shape), basis_tensor)
Expand Down Expand Up @@ -164,7 +158,7 @@ def test_trial_space_from_scaled_pod():
my_splitter,
my_orthogonalizer)
scaled_snapshots = my_scaler.pre_scaling(snapshots)
snapshotMatrix = tensor_to_matrix(scaled_snapshots)
snapshotMatrix = utils.tensor_to_matrix(scaled_snapshots)
u, s, v = np.linalg.svd(snapshotMatrix, full_matrices=False)
basis_tensor = my_trial_space.get_basis()
u = u.reshape(basis_tensor.shape)
Expand All @@ -189,7 +183,7 @@ def test_trial_space_from_scaled_pod():
shifted_snapshots, shift_vector = my_shifter(snapshots)
my_scaler = utils.VariableScaler('max_abs')
scaled_shifted_snapshots = my_scaler.pre_scaling(shifted_snapshots)
snapshot_matrix = tensor_to_matrix(scaled_shifted_snapshots)
snapshot_matrix = utils.tensor_to_matrix(scaled_shifted_snapshots)
u, s, v = np.linalg.svd(snapshot_matrix, full_matrices=False)
basis_tensor = my_trial_space.get_basis()
u = u.reshape(basis_tensor.shape)
Expand All @@ -214,7 +208,7 @@ def test_trial_space_from_scaled_pod():
shifted_snapshots, _ = my_shifter(snapshots)
my_scaler = utils.VariableScaler('max_abs')
scaled_shifted_snapshots = my_scaler.pre_scaling(shifted_snapshots)
snapshot_matrix = tensor_to_matrix(scaled_shifted_snapshots)
snapshot_matrix = utils.tensor_to_matrix(scaled_shifted_snapshots)
snapshot_matrix = my_splitter(snapshot_matrix)
u, s, v = np.linalg.svd(snapshot_matrix, full_matrices=False)
basis_tensor = my_trial_space.get_basis()
Expand All @@ -241,7 +235,7 @@ def test_trial_space_from_scaled_pod():
shifted_snapshots, shift_vector = my_shifter(snapshots)
my_scaler = utils.VariableScaler('max_abs')
scaled_shifted_snapshots = my_scaler.pre_scaling(shifted_snapshots)
snapshot_matrix = tensor_to_matrix(scaled_shifted_snapshots)
snapshot_matrix = utils.tensor_to_matrix(scaled_shifted_snapshots)
snapshot_matrix = my_splitter(snapshot_matrix)
u, s, v = np.linalg.svd(snapshot_matrix, full_matrices=False)
ushp = u.shape
Expand Down
Loading