Skip to content
Merged
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
32 changes: 31 additions & 1 deletion diffpy/snmf/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize_components(number_of_components, number_of_signals, grid_vector):
raise ValueError(f"Number of components = {number_of_components}. Number_of_components must be >= 1.")
components = list()
for component in range(number_of_components):
component = ComponentSignal(grid_vector,number_of_signals,component)
component = ComponentSignal(grid_vector, number_of_signals, component)
components.append(component)
return tuple(components)

Expand Down Expand Up @@ -54,6 +54,36 @@ def lift_data(data_input, lift=1):
return data_input + np.abs(np.min(data_input) * lift)


def construct_stretching_matrix(components, number_of_components, number_of_signals):
"""Constructs the stretching factor matrix

Parameters
----------
components: tuple of ComponentSignal objects
The tuple containing the component signals in ComponentSignal objects.
number_of_signals: int
The number of signals in the data provided by the user.

Returns
-------
2d array
The matrix containing the stretching factors for the component signals for each of the signals in the raw data.
Has dimensions `component_signal` x `number_of_signals`

"""
if (len(components)) == 0:
raise ValueError(f"Number of components = {number_of_components}. Number_of_components must be >= 1.")
number_of_components = len(components)

if number_of_signals <= 0:
raise ValueError(f"Number of signals = {number_of_signals}. Number_of_signals must be >= 1.")

stretching_factor_matrix = np.zeros((number_of_components, number_of_signals))
for i, component in enumerate(components):
stretching_factor_matrix[i] = component.stretching_factors
return stretching_factor_matrix


def initialize_arrays(number_of_components, number_of_moments, signal_length):
"""Generates the initial guesses for the weight, stretching, and component matrices

Expand Down
27 changes: 26 additions & 1 deletion diffpy/snmf/tests/test_subroutines.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
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
update_weights_matrix, initialize_arrays, lift_data, initialize_components, construct_stretching_matrix

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 @@ -161,3 +162,27 @@ def test_initialize_components(tcc):
assert len(actual) == tcc[0]
assert len(actual[0].weights) == tcc[1]
assert (actual[0].grid == np.array(tcc[2])).all()

tcso =[([ComponentSignal([0,.5,1,1.5],20,0)],1,20),
([ComponentSignal([0,.5,1,1.5],20,0)],4,20),
# ([ComponentSignal([0,.5,1,1.5],20,0)],0,20), # Raises an exception
# ([ComponentSignal([0,.5,1,1.5],20,0)],-2,20), # Raises an exception
# ([ComponentSignal([0,.5,1,1.5],20,0)],1,0), # Raises an Exception
# ([ComponentSignal([0,.5,1,1.5],20,0)],1,-3), # Raises an exception
([ComponentSignal([0,.5,1,1.5],20,0),ComponentSignal([0,.5,1,1.5],20,1)],2,20),
([ComponentSignal([0,.5,1,1.5],20,0),ComponentSignal([0,.5,1,21.5],20,1)],2,20),
([ComponentSignal([0,1,1.5],20,0),ComponentSignal([0,.5,1,21.5],20,1)],2,20),
# ([ComponentSignal([0,.5,1,1.5],20,0),ComponentSignal([0,.5,1,1.5],20,1)],1,-3), # Negative signal length. Raises an exception
#([],1,20), # Empty components. Raises an Exception
#([],-1,20), # Empty components with negative number of components. Raises an exception
#([],0,20), # Empty components with zero number of components. Raises an exception
#([],1,0), # Empty components with zero signal length. Raises an exception.
#([],-1,-2), # Empty components with negative number of components and signal length Raises an exception.

]
@pytest.mark.parametrize('tcso',tcso)
def test_construct_stretching_matrix(tcso):
actual = construct_stretching_matrix(tcso[0],tcso[1],tcso[2])
for component in tcso[0]:
np.testing.assert_allclose(actual[component.id,:], component.stretching_factors)
#assert actual[component.id, :] == component.stretching_factors