From 62df21f55c697cb74b851188d60318a97ef7c905 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Tue, 10 Oct 2023 21:35:51 -0400 Subject: [PATCH 1/2] Created reconstruct_signal_hess function and test function. --- diffpy/snmf/subroutines.py | 18 ++++++++++++++++++ diffpy/snmf/tests/test_subroutines.py | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index c0052bbb..95948793 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -206,7 +206,25 @@ def reconstruct_signal(components, signal_idx): reconstruction += stretched_and_weighted return reconstruction +def reconstruct_signal_hess(components, signal_idx): + """Reconstruct a specific signal's hessian (second derivative) from its weighted and stretched components. + Calculates the linear combination of stretched components' hessians where each term is a stretched component's + hessian mulitplied by its weight factor. + + Parameters + ---------- + components: tuple of ComponentSignal objects + The tuple containing the ComponentSignal objects + signal_idx: int + The index of the specific signal in the input data to be reconstructed. + + Returns + ------- + 1d array like + The reconstruction of a signal's hessian from calculated weights, stretching factors, and iq values + """ + return 0 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 1a93971d..f0eff3c4 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, reconstruct_signal + construct_component_matrix, construct_weight_matrix, update_weights, reconstruct_signal, reconstruct_signal_hess to = [ ([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), @@ -251,3 +251,8 @@ 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) + +trsh = [] +@pytest.mark.parametrize('trsh', trsh) +def test_reconstruct_signal_hessian(): + assert False From 60eb42a48fba4ea41d563c58d6b861d8e95259b2 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Tue, 10 Oct 2023 21:59:04 -0400 Subject: [PATCH 2/2] Added function implementation and tests --- diffpy/snmf/subroutines.py | 8 +++++++- diffpy/snmf/tests/test_subroutines.py | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 95948793..46d394c6 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -224,7 +224,13 @@ def reconstruct_signal_hess(components, signal_idx): 1d array like The reconstruction of a signal's hessian from calculated weights, stretching factors, and iq values """ - return 0 + signal_length = len(components[0].grid) + reconstruction = np.zeros(signal_length) + for component in components: + stretched = component.apply_stretch(signal_idx)[2] + 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): """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 f0eff3c4..8f677283 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -252,7 +252,16 @@ def test_reconstruct_signal(trs): actual = reconstruct_signal(trs[0], trs[1]) assert len(actual) == len(trs[0][0].grid) -trsh = [] +trsh = [([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('trsh', trsh) -def test_reconstruct_signal_hessian(): - assert False +def test_reconstruct_signal_hess(trsh): + actual = reconstruct_signal(trsh[0], trsh[1]) + assert len(actual) == len(trsh[0][0].grid)