Skip to content

function reconstruct_signal_gra #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions diffpy/snmf/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ def reconstruct_signal(components, signal_idx):
reconstruction += stretched_and_weighted
return reconstruction

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
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.
"""
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):
"""Generates the initial guesses for the weight, stretching, and component matrices
Expand Down
18 changes: 16 additions & 2 deletions diffpy/snmf/tests/test_subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)