From fc0c4ff61d15f4899193eb8f82cf9f97d1bde0f5 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Mon, 25 Sep 2023 12:23:41 -0400 Subject: [PATCH 1/2] initial commit, added docstring --- diffpy/snmf/subroutines.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 58990b7..065ce07 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -205,6 +205,25 @@ def reconstruct_signal(components, signal_idx): stretched_and_weighted = component.apply_weight(signal_idx, stretched) reconstruction += stretched_and_weighted return reconstruction +def reconstructed_signal_gra(components, signal_idx): + """Reconstruts a specific signal's gradient from its weighted and stretched components. + + Calculates the linear combination of stretched components' gradients where each is the strethced component's + gradient multiplied by its weight factor. + + Parameters + ---------- + components: tuple of Componentsignal objects + The tuple containing the ComponentSignal objects + signal_idx: int + The index of the specified signal in the input data to be reconstructed + + Returns + ------- + 1d array like + The reconstruction of a signal from calculated weights, stretching factors, and iq values. + """ + def initialize_arrays(number_of_components, number_of_moments, signal_length): From f54169535e17bb0832dce714cbb9accb582ea4e5 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Sat, 30 Sep 2023 20:36:16 -0400 Subject: [PATCH 2/2] Added reconstruct_signal_gra and tests --- diffpy/snmf/subroutines.py | 11 +++++++++-- diffpy/snmf/tests/test_subroutines.py | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 065ce07..d44a53c 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -205,7 +205,8 @@ def reconstruct_signal(components, signal_idx): stretched_and_weighted = component.apply_weight(signal_idx, stretched) reconstruction += stretched_and_weighted return reconstruction -def reconstructed_signal_gra(components, signal_idx): + +def reconstruct_signal_gra(components, signal_idx): """Reconstruts a specific signal's gradient from its weighted and stretched components. Calculates the linear combination of stretched components' gradients where each is the strethced component's @@ -223,7 +224,13 @@ def reconstructed_signal_gra(components, signal_idx): 1d array like The reconstruction of a signal from calculated weights, stretching factors, and iq values. """ - + signal_length = len(components[0].grid) + reconstruction = np.zeros(signal_length) + for component in components: + stretched = component.apply_stretch(signal_idx)[1] + 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 aa55dde..1de1ad3 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -2,8 +2,8 @@ import numpy as np 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, reconstruct_signal + update_weights_matrix, lift_data, initialize_components, construct_stretching_matrix, \ + construct_component_matrix, construct_weight_matrix, update_weights, reconstruct_signal, reconstruct_signal_gra to = [ ([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), @@ -266,3 +266,17 @@ def test_update_weights(tuw): def test_reconstruct_signal(trs): actual = reconstruct_signal(trs[0], trs[1]) assert len(actual) == len(trs[0][0].grid) + +trsg = 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('trsg', trsg) +def test_reconstruct_signal_gra(trsg): + actual = reconstruct_signal_gra(trsg[0], trsg[1]) + assert len(actual) == len(trsg[0][0].grid)