Skip to content

Commit 3256040

Browse files
authored
Merge pull request #50 from aajayi-21/reconstruct_data
function reconsturct_data
2 parents 3ec3c4d + 7d19a57 commit 3256040

File tree

2 files changed

+23
-89
lines changed

2 files changed

+23
-89
lines changed

Diff for: diffpy/snmf/subroutines.py

+13-64
Original file line numberDiff line numberDiff line change
@@ -449,76 +449,25 @@ def get_residual_matrix(component_matrix, weights_matrix, stretching_matrix, dat
449449
return residual_matrx
450450

451451

452-
def reconstruct_data(stretching_factor_matrix, component_matrix, weight_matrix, component_amount,
453-
moment_amount, signal_length):
454-
"""Reconstructs the experimental data from the component signals, stretching factors, and weights.
452+
def reconstruct_data(components):
453+
"""Reconstructs the `input_data` matrix
455454
456-
Calculates the stretched and weighted components at each moment.
455+
Reconstructs the `input_data` matrix from calculated component signals, weights, and stretching factors.
457456
458457
Parameters
459458
----------
460-
stretching_factor_matrix: 2d array like
461-
The matrix containing the stretching factors of the component signals. Has dimensions K x M where K is the number
462-
of components and M is the number of moments.
463-
464-
component_matrix: 2d array like
465-
The matrix containing the unstretched component signals. Has dimensions N x K where N is the length of the signals
466-
and K is the number of components.
467-
468-
weight_matrix: 2d array like
469-
The matrix containing the weights of the stretched component signals at each moment in time. Has dimensions
470-
K x M where K is the number of components and M is the number of moments.
471-
472-
component_amount: int
473-
The number of component signals the user would like to obtain from the experimental data.
474-
475-
moment_amount: int
476-
The number of patterns in the experimental data. Represents the number of moments in time in the data series.
477-
478-
signal_length: int
479-
The length of the signals in the experimental data.
459+
components: tuple of ComponentSignal objects
460+
The tuple containing the component signals.
480461
481462
Returns
482463
-------
483-
tuple of 2d array of floats
484-
The stretched and weighted component signals at each moment. Has dimensions N x (M * K) where N is the length of
485-
the signals, M is the number of moments, and K is the number of components. The resulting matrix has M blocks
486-
stacked horizontally where each block is the weighted and stretched components at each moment. Also contains
487-
the gradient and hessian matrices of the reconstructed data.
464+
2d array
465+
The 2d array containing the reconstruction of input_data.
488466
489467
"""
490-
stretching_factor_matrix = np.asarray(stretching_factor_matrix)
491-
component_matrix = np.asarray(component_matrix)
492-
weight_matrix = np.asarray(weight_matrix)
493-
stretched_component_series = []
494-
stretched_component_series_gra = []
495-
stretched_component_series_hess = []
496-
for moment in range(moment_amount):
497-
for component in range(component_amount):
498-
stretched_component = get_stretched_component(stretching_factor_matrix[component, moment],
499-
component_matrix[:, component], signal_length)
500-
stretched_component_series.append(stretched_component[0])
501-
stretched_component_series_gra.append(stretched_component[1])
502-
stretched_component_series_hess.append(stretched_component[2])
503-
stretched_component_series = np.column_stack(stretched_component_series)
504-
stretched_component_series_gra = np.column_stack(stretched_component_series_gra)
505-
stretched_component_series_hess = np.column_stack(stretched_component_series_hess)
506-
507-
reconstructed_data = []
508-
reconstructed_data_gra = []
509-
reconstructed_data_hess = []
510-
moment = 0
511-
for s_component in range(0, moment_amount * component_amount, component_amount):
512-
block = stretched_component_series[:, s_component:s_component + component_amount]
513-
block_gra = stretched_component_series_gra[:, s_component:s_component + component_amount]
514-
block_hess = stretched_component_series_hess[:, s_component:s_component + component_amount]
515-
for component in range(component_amount):
516-
block[:, component] = block[:, component] * weight_matrix[component, moment]
517-
block_gra[:, component] = block_gra[:, component] * weight_matrix[component, moment]
518-
block_hess[:, component] = block_hess[:, component] * weight_matrix[component, moment]
519-
reconstructed_data.append(block)
520-
reconstructed_data_gra.append(block_gra)
521-
reconstructed_data_hess.append(block_hess)
522-
moment += 1
523-
return np.column_stack(reconstructed_data), np.column_stack(reconstructed_data_gra), np.column_stack(
524-
reconstructed_data_hess)
468+
signal_length = len(components[0].iq)
469+
number_of_signals = len(components[0].weights)
470+
data_reconstruction = np.zeros((signal_length, number_of_signals))
471+
for signal in range(number_of_signals):
472+
data_reconstruction[:, signal] = reconstruct_signal(components, signal)
473+
return data_reconstruction

Diff for: diffpy/snmf/tests/test_subroutines.py

+10-25
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,19 @@ def test_get_residual_matrix(tgrm):
108108

109109

110110
trd = [
111-
([np.array([[.5], [.5]]), np.array([[1, 2], [3, 4]]), np.array([[.25], [.75]]), 2, 1, 2],
112-
(np.array([[.25, 1.5], [0, 0]]), np.array([[0, 0], [0, 0]]), np.array([[0, 0], [0, 0]]))),
113-
([[[.9], [.7]], [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], [[.25], [.75]], 2, 1, 5],
114-
([[.25, 1.5], [.8056, 3.6429], [1.3611, 5.7857], [1.9167, 0], [0, 0]],
115-
[[0, 0], [-.61728, -3.06122], [-1.234567, -6.12244], [-1.8518, 0], [0, 0]],
116-
[[0, 0], [1.3717, 8.746355], [2.74348, 17.49271], [4.11523, 0], [0, 0]])),
117-
([[[.5, .7], [.5, .8]], [[1.1, 2.2], [3.3, 4.4]], [[.25, .5], [.75, .5]], 2, 2, 2],
118-
([[.275, 1.65, .55, 1.1], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]])),
119-
([[[.9, .9], [.9, .9], [.95, .95], [.95, .95]], [[-11, -.3, 0, -.2], [-14, -1, 0, -.75]],
120-
[[.5, .5], [.2, 0], [.3, .5], [0, 0]], 4, 2, 2],
121-
([[-5.5, -0.06, 0, 0, -5.5, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
122-
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]])),
123-
([[[.7, .8, .9], [.9, .9, .95], [1, .8, .9]], [[0, 0, 0], [.3, .4, .5], [1, 1.2, 1.3]],
124-
[[.33, .5, .6], [0, .25, .3], [.66, .25, .1]], 3, 3, 3],
125-
([[0, 0, 0, 0, 0, 0, 0, 0, 0], [.1980, 0, .33, .2375, .1222, .1750, .2267, .1326, .0589],
126-
[0, 0, .8580, 0, 0, 0, 0, 0, 0]],
127-
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [-.47142, 0, -.429, -.546875, -.246913, -.3125, -.51851, -.26593, -.098765],
128-
[0, 0, -0.24175643, 0, 0, 0, 0, 0, 0]],
129-
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [1.3469, 0, 1.96711566, 1.3671875, .548696, .78125, 1.15226, .55984, .2194787],
130-
[0, 0, 0.27796989, 0, 0, 0, 0, 0, 0]]))
111+
([ComponentSignal([0, .25, .5, .75, 1], 2, 0), ComponentSignal([0, .25, .5, .75, 1], 2, 1),
112+
ComponentSignal([0, .25, .5, .75, 1], 2, 2)]),
113+
([ComponentSignal([0, .25, .5, .75, 1], 2, 0)]),
114+
([ComponentSignal([0, .25, .5, .75, 1], 2, 0), ComponentSignal([0, .25, .5, .75, 1], 2, 1),
115+
ComponentSignal([0, .25, .5, .75, 1], 2, 2), ComponentSignal([0, .25, .5, .75, 1], 2, 3),
116+
ComponentSignal([0, .25, .5, .75, 1], 2, 4)]),
117+
#([]) # Exception expected
131118
]
132-
133-
134119
@pytest.mark.parametrize('trd', trd)
135120
def test_reconstruct_data(trd):
136-
actual = reconstruct_data(trd[0][0], trd[0][1], trd[0][2], trd[0][3], trd[0][4], trd[0][5])
137-
expected = trd[1]
138-
np.testing.assert_allclose(actual, expected, rtol=1e-03)
121+
actual = reconstruct_data(trd)
122+
assert actual.shape == (len(trd[0].iq),len(trd[0].weights))
123+
print(actual)
139124

140125

141126
tld = [(([[[1, -1, 1], [0, 0, 0], [2, 10, -3]], 1]), ([[4, 2, 4], [3, 3, 3], [5, 13, 0]])),

0 commit comments

Comments
 (0)