From 6c8d7f0737d1904c0bbccb8575999281834e2e10 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Sun, 13 Aug 2023 11:40:14 -0500 Subject: [PATCH 1/3] initial commit created docstring and test function --- diffpy/snmf/subroutines.py | 21 ++++++++++++++++++++- diffpy/snmf/tests/test_subroutines.py | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index cdaa1035..8cc384f9 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -171,7 +171,7 @@ def update_weights(components, data_input, method=None): for i, component in enumerate(components): stretched_components[:, i] = component.apply_stretch(signal)[0] if method == 'align': - weights = lsqnonneg(stretched_components, data_input[:,signal]) + weights = lsqnonneg(stretched_components, data_input[:, signal]) else: weights = get_weights(stretched_components.T @ stretched_components, -stretched_components.T @ data_input[:, signal], 0, 1) @@ -179,6 +179,25 @@ def update_weights(components, data_input, method=None): return weight_matrix +def reconstruct_signal(components, signal_idx): + """Reconstructs a specific signal from its weighted and stretched components + + Parameters + ---------- + components: tuple of ComponentSignal objects + The tuple containing the ComponentSignal objects + signal_idx: int + The index of the specific signal to be reconstructed + + Returns + ------- + 1d array like + The reconstruction of a signal from calculated weights, stretching factors, and iq values. + + """ + pass + + def initialize_arrays(number_of_components, number_of_moments, signal_length): """Generates the initial guesses for the weight, stretching, and component matrices diff --git a/diffpy/snmf/tests/test_subroutines.py b/diffpy/snmf/tests/test_subroutines.py index 06e11f30..3ab7c0c3 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -3,7 +3,7 @@ from diffpy.snmf.containers import ComponentSignal from diffpy.snmf.subroutines import objective_function, get_stretched_component, reconstruct_data, get_residual_matrix, \ update_weights_matrix, initialize_arrays, lift_data, initialize_components, construct_stretching_matrix, \ - construct_component_matrix, construct_weight_matrix, update_weights + construct_component_matrix, construct_weight_matrix, update_weights, reconstruct_signal to = [ ([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), @@ -252,3 +252,7 @@ def test_construct_weight_matrix(tcwm): def test_update_weights(tuw): actual = update_weights(tuw[0], tuw[1], tuw[2]) assert np.shape(actual) == (len(tuw[0]), len(tuw[0][0].weights)) +trs = [] +@pytest.mark.parametrize('trs',trs) +def test_reconstruct_signal(trs): + assert False From 3a8a617f7465ada2d804227d61ea2027b3ea1353 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Sun, 13 Aug 2023 12:04:44 -0500 Subject: [PATCH 2/3] added function contents --- diffpy/snmf/subroutines.py | 10 ++++++++-- diffpy/snmf/tests/test_subroutines.py | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 8cc384f9..85884201 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -187,7 +187,7 @@ def reconstruct_signal(components, signal_idx): components: tuple of ComponentSignal objects The tuple containing the ComponentSignal objects signal_idx: int - The index of the specific signal to be reconstructed + The index of the specific signal in the input data to be reconstructed Returns ------- @@ -195,7 +195,13 @@ def reconstruct_signal(components, signal_idx): The reconstruction of a signal from calculated weights, stretching factors, and iq values. """ - pass + signal_length = len(components[0].grid) + reconstruction = np.zeros(signal_length) + for component in components: + stretched = component.apply_stretch(signal_idx)[0] + stretched_and_weighted = component.apply_weight(signal_idx, stretched) + reconstruction += stretched_and_weighted + return reconstruction def initialize_arrays(number_of_components, number_of_moments, signal_length): diff --git a/diffpy/snmf/tests/test_subroutines.py b/diffpy/snmf/tests/test_subroutines.py index 3ab7c0c3..aa55ddea 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -252,7 +252,17 @@ def test_construct_weight_matrix(tcwm): def test_update_weights(tuw): actual = update_weights(tuw[0], tuw[1], tuw[2]) assert np.shape(actual) == (len(tuw[0]), len(tuw[0][0].weights)) -trs = [] + +trs = [([ComponentSignal([0, .25, .5, .75, 1], 2, 0), ComponentSignal([0, .25, .5, .75, 1], 2, 1), + ComponentSignal([0, .25, .5, .75, 1], 2, 2)], 1), + ([ComponentSignal([0, .25, .5, .75, 1], 2, 0), ComponentSignal([0, .25, .5, .75, 1], 2, 1), + ComponentSignal([0, .25, .5, .75, 1], 2, 2)], 0), + ([ComponentSignal([0, .25, .5, .75, 1], 3, 0), ComponentSignal([0, .25, .5, .75, 1], 3, 1), + ComponentSignal([0, .25, .5, .75, 1], 3, 2)], 2), + # ([ComponentSignal([0, .25, .5, .75, 1], 2, 0), ComponentSignal([0, .25, .5, .75, 1], 2, 1), + # ComponentSignal([0, .25, .5, .75, 1], 2, 2)], -1), +] @pytest.mark.parametrize('trs',trs) def test_reconstruct_signal(trs): - assert False + actual = reconstruct_signal(trs[0], trs[1]) + assert len(actual) == len(trs[0][0].grid) From be81e675b232084e479b543594900f044233eef9 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Sun, 13 Aug 2023 12:09:33 -0500 Subject: [PATCH 3/3] added extended docstring description --- diffpy/snmf/subroutines.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 85884201..58990b7b 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -180,7 +180,10 @@ def update_weights(components, data_input, method=None): def reconstruct_signal(components, signal_idx): - """Reconstructs a specific signal from its weighted and stretched components + """Reconstructs a specific signal from its weighted and stretched components. + + Calculates the linear combination of stretched components where each term is the stretched component multiplied + by its weight factor. Parameters ----------