diff --git a/romtools/trial_space/__init__.py b/romtools/trial_space/__init__.py index 8194c0be..420eb80c 100644 --- a/romtools/trial_space/__init__.py +++ b/romtools/trial_space/__init__.py @@ -82,12 +82,7 @@ - `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** @@ -101,6 +96,7 @@ 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. @@ -127,7 +123,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. @@ -176,10 +172,10 @@ def __init__(self, snapshots, shifter, splitter, orthogonalizer): # compute basis n_var = snapshots.shape[0] shifted_snapshots, self.__shift_vector = shifter(snapshots) - snapshot_matrix = tensor_to_matrix(shifted_snapshots) + snapshot_matrix = _tensor_to_matrix(shifted_snapshots) self.__basis = splitter(snapshot_matrix) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -251,7 +247,7 @@ def __init__(self, n_var = snapshots.shape[0] shifted_snapshots, self.__shift_vector = shifter(snapshots) - snapshot_matrix = tensor_to_matrix(shifted_snapshots) + snapshot_matrix = _tensor_to_matrix(shifted_snapshots) shifted_split_snapshots = splitter(snapshot_matrix) svd_picked = np.linalg.svd if svdFnc is None else svdFnc @@ -260,7 +256,7 @@ def __init__(self, self.__basis = truncater(lsv, svals) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -327,16 +323,16 @@ def __init__(self, snapshots, n_var = snapshots.shape[0] shifted_snapshots, self.__shift_vector = shifter(snapshots) scaled_shifted_snapshots = scaler.pre_scaling(shifted_snapshots) - snapshot_matrix = tensor_to_matrix(scaled_shifted_snapshots) + snapshot_matrix = _tensor_to_matrix(scaled_shifted_snapshots) snapshot_matrix = splitter(snapshot_matrix) lsv, svals, _ = np.linalg.svd(snapshot_matrix, full_matrices=False) self.__basis = truncater(lsv, svals) - self.__basis = matrix_to_tensor(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__basis = scaler.post_scaling(self.__basis) - self.__basis = tensor_to_matrix(self.__basis) + self.__basis = _tensor_to_matrix(self.__basis) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -358,8 +354,9 @@ def get_basis(self): return self.__basis -def tensor_to_matrix(tensor_input): +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]$. ''' @@ -368,9 +365,10 @@ def tensor_to_matrix(tensor_input): return output_tensor -def matrix_to_tensor(n_var, matrix_input): +def _matrix_to_tensor(n_var, matrix_input): ''' - Inverse operation of `tensor_to_matrix` + @private + Inverse operation of `__tensor_to_matrix` ''' d1 = int(matrix_input.shape[0] / n_var) d2 = matrix_input.shape[1] diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 9cd853c2..ea26469b 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -5,6 +5,11 @@ import romtools.trial_space.utils as utils +def _tensor_to_matrix(tensor_input): + output_tensor = tensor_input.reshape(tensor_input.shape[0]*tensor_input.shape[1], + tensor_input.shape[2]) + return output_tensor + #@pytest.mark.mpi_skip #def test_list_snapshots_to_array(): @@ -14,12 +19,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)) @@ -74,7 +73,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 = _tensor_to_matrix(basis) assert np.allclose(basis.transpose() @ basis, np.eye(12)) @@ -91,7 +90,7 @@ def test_trial_space_from_pod(): my_splitter, my_orthogonalizer) # truth trial space - snapshotMatrix = tensor_to_matrix(snapshots) + snapshotMatrix = _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) @@ -164,7 +163,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 = _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) @@ -189,7 +188,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 = _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) @@ -214,7 +213,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 = _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() @@ -241,7 +240,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 = _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