From 838230abca87dda46141fe781d3746879a7d4641 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Tue, 30 Jan 2024 09:09:00 +0100 Subject: [PATCH 1/8] make them private --- romtools/trial_space/__init__.py | 41 ++++---------------------- romtools/trial_space/utils/__init__.py | 21 +++++++++++++ tests/romtools/test_trial_space.py | 20 ++++++------- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/romtools/trial_space/__init__.py b/romtools/trial_space/__init__.py index 8194c0be..0ff10e31 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** @@ -95,11 +90,7 @@ import abc import numpy as np -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 * +from romtools.trial_space.utils import * class TrialSpace(abc.ABC): ''' @@ -127,7 +118,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. @@ -251,7 +242,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 = utils.tensor_to_matrix(shifted_snapshots) shifted_split_snapshots = splitter(snapshot_matrix) svd_picked = np.linalg.svd if svdFnc is None else svdFnc @@ -327,14 +318,14 @@ 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 = utils.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 = scaler.post_scaling(self.__basis) - self.__basis = tensor_to_matrix(self.__basis) + self.__basis = utils.tensor_to_matrix(self.__basis) self.__basis = orthogonalizer(self.__basis) self.__basis = matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] @@ -356,23 +347,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 diff --git a/romtools/trial_space/utils/__init__.py b/romtools/trial_space/utils/__init__.py index a7ce4159..505f9e0d 100644 --- a/romtools/trial_space/utils/__init__.py +++ b/romtools/trial_space/utils/__init__.py @@ -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 diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 9cd853c2..a4ff1a89 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -4,8 +4,6 @@ import romtools as rt import romtools.trial_space.utils as utils - - #@pytest.mark.mpi_skip #def test_list_snapshots_to_array(): # snapshots = np.random.normal(size=(15,7)) @@ -15,9 +13,9 @@ # 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]) +# 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 @@ -74,7 +72,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)) @@ -91,7 +89,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) @@ -164,7 +162,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) @@ -189,7 +187,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) @@ -214,7 +212,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() @@ -241,7 +239,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 From 798129dcf969dfbd7f743cc372a0ef83aa07b8f5 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Tue, 30 Jan 2024 09:10:15 +0100 Subject: [PATCH 2/8] remove unused --- tests/romtools/test_trial_space.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index a4ff1a89..91e7a86a 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -12,12 +12,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)) From 2ffd69f89bd592390e52bc7bd554ae92fea4a944 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Tue, 30 Jan 2024 09:10:44 +0100 Subject: [PATCH 3/8] remove unused --- tests/romtools/test_trial_space.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 91e7a86a..061f7ed6 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -4,6 +4,8 @@ import romtools as rt import romtools.trial_space.utils as utils + + #@pytest.mark.mpi_skip #def test_list_snapshots_to_array(): # snapshots = np.random.normal(size=(15,7)) From 9e0f36e7e3ce4ffe34bc803841615def4130319e Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Tue, 30 Jan 2024 09:17:51 +0100 Subject: [PATCH 4/8] fix --- romtools/trial_space/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/romtools/trial_space/__init__.py b/romtools/trial_space/__init__.py index 0ff10e31..5e0f1349 100644 --- a/romtools/trial_space/__init__.py +++ b/romtools/trial_space/__init__.py @@ -90,7 +90,13 @@ import abc import numpy as np -from romtools.trial_space.utils import * +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): ''' @@ -242,7 +248,7 @@ def __init__(self, n_var = snapshots.shape[0] shifted_snapshots, self.__shift_vector = shifter(snapshots) - snapshot_matrix = utils.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 @@ -318,14 +324,14 @@ 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 = utils.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 = scaler.post_scaling(self.__basis) - self.__basis = utils.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.__dimension = self.__basis.shape[2] From a7f460315f6a449f53250073e44c5630a2094e50 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Wed, 31 Jan 2024 20:53:29 +0100 Subject: [PATCH 5/8] fix syntax --- romtools/trial_space/__init__.py | 18 +++++++++--------- romtools/trial_space/utils/__init__.py | 4 ++-- tests/romtools/test_trial_space.py | 12 ++++++------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/romtools/trial_space/__init__.py b/romtools/trial_space/__init__.py index 5e0f1349..a84b0581 100644 --- a/romtools/trial_space/__init__.py +++ b/romtools/trial_space/__init__.py @@ -90,7 +90,7 @@ import abc import numpy as np -from romtools.trial_space.utils import tensor_to_matrix, matrix_to_tensor +from romtools.trial_space.utils import tensor_to_matrix_impl, matrix_to_tensor_impl from romtools.trial_space.utils.truncater import * from romtools.trial_space.utils.shifter import * from romtools.trial_space.utils.scaler import * @@ -173,10 +173,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_impl(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_impl(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -248,7 +248,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_impl(shifted_snapshots) shifted_split_snapshots = splitter(snapshot_matrix) svd_picked = np.linalg.svd if svdFnc is None else svdFnc @@ -257,7 +257,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_impl(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -324,16 +324,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_impl(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_impl(n_var, self.__basis) self.__basis = scaler.post_scaling(self.__basis) - self.__basis = tensor_to_matrix(self.__basis) + self.__basis = tensor_to_matrix_impl(self.__basis) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor(n_var, self.__basis) + self.__basis = matrix_to_tensor_impl(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): diff --git a/romtools/trial_space/utils/__init__.py b/romtools/trial_space/utils/__init__.py index 505f9e0d..de376b32 100644 --- a/romtools/trial_space/utils/__init__.py +++ b/romtools/trial_space/utils/__init__.py @@ -54,7 +54,7 @@ from romtools.trial_space.utils.truncater import * from romtools.trial_space.utils.svd_method_of_snapshots import * -def tensor_to_matrix(tensor_input): +def tensor_to_matrix_impl(tensor_input): ''' @private Converts a tensor with shape $[N, M, P]$ to a matrix representation @@ -65,7 +65,7 @@ def tensor_to_matrix(tensor_input): return output_tensor -def matrix_to_tensor(n_var, matrix_input): +def matrix_to_tensor_impl(n_var, matrix_input): ''' @private Inverse operation of `__tensor_to_matrix` diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 061f7ed6..08180ddb 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -68,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 = utils.tensor_to_matrix(basis) + basis = utils.tensor_to_matrix_impl(basis) assert np.allclose(basis.transpose() @ basis, np.eye(12)) @@ -85,7 +85,7 @@ def test_trial_space_from_pod(): my_splitter, my_orthogonalizer) # truth trial space - snapshotMatrix = utils.tensor_to_matrix(snapshots) + snapshotMatrix = utils.tensor_to_matrix_impl(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) @@ -158,7 +158,7 @@ def test_trial_space_from_scaled_pod(): my_splitter, my_orthogonalizer) scaled_snapshots = my_scaler.pre_scaling(snapshots) - snapshotMatrix = utils.tensor_to_matrix(scaled_snapshots) + snapshotMatrix = utils.tensor_to_matrix_impl(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) @@ -183,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 = utils.tensor_to_matrix(scaled_shifted_snapshots) + snapshot_matrix = utils.tensor_to_matrix_impl(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) @@ -208,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 = utils.tensor_to_matrix(scaled_shifted_snapshots) + snapshot_matrix = utils.tensor_to_matrix_impl(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() @@ -235,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 = utils.tensor_to_matrix(scaled_shifted_snapshots) + snapshot_matrix = utils.tensor_to_matrix_impl(scaled_shifted_snapshots) snapshot_matrix = my_splitter(snapshot_matrix) u, s, v = np.linalg.svd(snapshot_matrix, full_matrices=False) ushp = u.shape From 3a5817f6fef7392782d055ce3c586297b6ec369f Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Wed, 31 Jan 2024 21:11:06 +0100 Subject: [PATCH 6/8] fix --- romtools/trial_space/__init__.py | 39 ++++++++++++++++++++------ romtools/trial_space/utils/__init__.py | 21 -------------- tests/romtools/test_trial_space.py | 17 +++++++---- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/romtools/trial_space/__init__.py b/romtools/trial_space/__init__.py index a84b0581..420eb80c 100644 --- a/romtools/trial_space/__init__.py +++ b/romtools/trial_space/__init__.py @@ -90,7 +90,6 @@ import abc import numpy as np -from romtools.trial_space.utils import tensor_to_matrix_impl, matrix_to_tensor_impl from romtools.trial_space.utils.truncater import * from romtools.trial_space.utils.shifter import * from romtools.trial_space.utils.scaler import * @@ -173,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_impl(shifted_snapshots) + snapshot_matrix = _tensor_to_matrix(shifted_snapshots) self.__basis = splitter(snapshot_matrix) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor_impl(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -248,7 +247,7 @@ def __init__(self, n_var = snapshots.shape[0] shifted_snapshots, self.__shift_vector = shifter(snapshots) - snapshot_matrix = tensor_to_matrix_impl(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 @@ -257,7 +256,7 @@ def __init__(self, self.__basis = truncater(lsv, svals) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor_impl(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -324,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_impl(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_impl(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_impl(self.__basis) + self.__basis = _tensor_to_matrix(self.__basis) self.__basis = orthogonalizer(self.__basis) - self.__basis = matrix_to_tensor_impl(n_var, self.__basis) + self.__basis = _matrix_to_tensor(n_var, self.__basis) self.__dimension = self.__basis.shape[2] def get_dimension(self): @@ -353,3 +352,25 @@ def get_basis(self): Concrete implementation of `TrialSpace.get_basis()` ''' return self.__basis + + +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 diff --git a/romtools/trial_space/utils/__init__.py b/romtools/trial_space/utils/__init__.py index de376b32..a7ce4159 100644 --- a/romtools/trial_space/utils/__init__.py +++ b/romtools/trial_space/utils/__init__.py @@ -53,24 +53,3 @@ 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_impl(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_impl(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 diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 08180ddb..1c39fa05 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 local_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(): @@ -68,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 = utils.tensor_to_matrix_impl(basis) + basis = local_tensor_to_matrix(basis) assert np.allclose(basis.transpose() @ basis, np.eye(12)) @@ -85,7 +90,7 @@ def test_trial_space_from_pod(): my_splitter, my_orthogonalizer) # truth trial space - snapshotMatrix = utils.tensor_to_matrix_impl(snapshots) + snapshotMatrix = local_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) @@ -158,7 +163,7 @@ def test_trial_space_from_scaled_pod(): my_splitter, my_orthogonalizer) scaled_snapshots = my_scaler.pre_scaling(snapshots) - snapshotMatrix = utils.tensor_to_matrix_impl(scaled_snapshots) + snapshotMatrix = local_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) @@ -183,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 = utils.tensor_to_matrix_impl(scaled_shifted_snapshots) + snapshot_matrix = local_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) @@ -208,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 = utils.tensor_to_matrix_impl(scaled_shifted_snapshots) + snapshot_matrix = local_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() @@ -235,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 = utils.tensor_to_matrix_impl(scaled_shifted_snapshots) + snapshot_matrix = local_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 From f71cb8cf979e89f1a6829305de15040d4d305540 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Thu, 1 Feb 2024 15:25:26 +0100 Subject: [PATCH 7/8] fix --- tests/romtools/test_trial_space.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 1c39fa05..51cc2035 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -5,7 +5,7 @@ import romtools.trial_space.utils as utils -def local_tensor_to_matrix(tensor_input): +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 @@ -73,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 = local_tensor_to_matrix(basis) + basis = tensor_to_matrix(basis) assert np.allclose(basis.transpose() @ basis, np.eye(12)) @@ -90,7 +90,7 @@ def test_trial_space_from_pod(): my_splitter, my_orthogonalizer) # truth trial space - snapshotMatrix = local_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) @@ -163,7 +163,7 @@ def test_trial_space_from_scaled_pod(): my_splitter, my_orthogonalizer) scaled_snapshots = my_scaler.pre_scaling(snapshots) - snapshotMatrix = local_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) @@ -188,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 = local_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) @@ -213,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 = local_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() @@ -240,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 = local_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 From 6034f80807495006c6085863f5acbf2ea273ef13 Mon Sep 17 00:00:00 2001 From: John Tencer Date: Thu, 1 Feb 2024 08:16:48 -0700 Subject: [PATCH 8/8] add _ --- tests/romtools/test_trial_space.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/romtools/test_trial_space.py b/tests/romtools/test_trial_space.py index 51cc2035..ea26469b 100644 --- a/tests/romtools/test_trial_space.py +++ b/tests/romtools/test_trial_space.py @@ -5,7 +5,7 @@ import romtools.trial_space.utils as utils -def tensor_to_matrix(tensor_input): +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 @@ -73,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)) @@ -90,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) @@ -163,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) @@ -188,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) @@ -213,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() @@ -240,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