From 8c510c6347f2b44f6ffd963a1cce97f6a0a053c6 Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Mon, 31 Jul 2023 18:23:32 -0500 Subject: [PATCH 1/2] initial commit --- diffpy/snmf/subroutines.py | 24 ++++++++++++++++++++++++ diffpy/snmf/tests/test_subroutines.py | 20 +++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 17b32918..5712832f 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -1,9 +1,33 @@ import numpy as np from diffpy.snmf.optimizers import get_weights from diffpy.snmf.factorizers import lsqnonneg +from diffpy.snmf.containers import ComponentSignal import numdifftools +def create_components(number_of_components, number_of_signals, grid_vector): + """Creates the ComponentSignal objects + + Parameters + ---------- + number_of_components: int + The number of component signals in the NMF decomposition + number_of_signals: int + grid_vector: 1d array + The grid of the user provided signals. + + Returns + ------- + tuple of ComponentSignal objects + The tuple containing `number_of_components` of initialized ComponentSignal objects. + """ + components = list() + for c in range(number_of_components): + c = ComponentSignal(grid_vector,number_of_signals,c) + components.append(c) + return tuple(components) + + def lift_data(data_input, lift=1): """Lifts values of data_input diff --git a/diffpy/snmf/tests/test_subroutines.py b/diffpy/snmf/tests/test_subroutines.py index 91f6f7d1..84d499c4 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -1,7 +1,7 @@ import pytest import numpy as np from diffpy.snmf.subroutines import objective_function, get_stretched_component, reconstruct_data, get_residual_matrix, \ - update_weights_matrix, initialize_arrays, lift_data + update_weights_matrix, initialize_arrays, lift_data, create_components to = [ ([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), @@ -144,8 +144,26 @@ def test_reconstruct_data(trd): (([[[1.5, 2], [10.5, 1], [0.5, 2]], 1]), ([[2, 2.5], [11, 1.5], [1, 2.5]])), (([[[-10, -10.5], [-12.2, -12.2], [0, 0]], 1]), ([[2.2, 1.7], [0, 0], [12.2, 12.2]])), ] + + @pytest.mark.parametrize('tld', tld) def test_lift_data(tld): actual = lift_data(tld[0][0], tld[0][1]) expected = tld[1] np.testing.assert_allclose(actual, expected) + +tcc = [(2, [0, .5, 1, 1.5], 3, 3), + (3,[0,10,20,30],10,15), + (0,[0],11,30), + (5,[1,1,1,1,1,1],10000,40000), + (3,np.arange(stop=125,step=.05),20,2500), + ] +@pytest.mark.parametrize('tcc', tcc) +def test_create_components(tcc): + actual = create_components(tcc[0], tcc[1], tcc[2], tcc[3]) + assert len(actual) == tcc[0] + for c in actual: + assert len(c.iq) == tcc[3] + assert len(c.weights) == tcc[2] + assert len(c.stretching_factors) == tcc[2] + assert (c.grid == tcc[1]).all() \ No newline at end of file From df192054bea7ce126ab3b185929aadfa50c7effb Mon Sep 17 00:00:00 2001 From: Adeolu Ajayi Date: Tue, 1 Aug 2023 07:47:32 -0500 Subject: [PATCH 2/2] fixed components initializer Fixed tests, refactored name, added raised error. --- diffpy/snmf/subroutines.py | 12 +++++++----- diffpy/snmf/tests/test_subroutines.py | 20 +++++++------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/diffpy/snmf/subroutines.py b/diffpy/snmf/subroutines.py index 5712832f..6b55728e 100644 --- a/diffpy/snmf/subroutines.py +++ b/diffpy/snmf/subroutines.py @@ -5,8 +5,8 @@ import numdifftools -def create_components(number_of_components, number_of_signals, grid_vector): - """Creates the ComponentSignal objects +def initialize_components(number_of_components, number_of_signals, grid_vector): + """Initializes ComponentSignals for each of the components in the decomposition Parameters ---------- @@ -21,10 +21,12 @@ def create_components(number_of_components, number_of_signals, grid_vector): tuple of ComponentSignal objects The tuple containing `number_of_components` of initialized ComponentSignal objects. """ + if number_of_components <= 0: + raise ValueError(f"Number of components = {number_of_components}. Number_of_components must be >= 1.") components = list() - for c in range(number_of_components): - c = ComponentSignal(grid_vector,number_of_signals,c) - components.append(c) + for component in range(number_of_components): + component = ComponentSignal(grid_vector,number_of_signals,component) + components.append(component) return tuple(components) diff --git a/diffpy/snmf/tests/test_subroutines.py b/diffpy/snmf/tests/test_subroutines.py index 84d499c4..f9bad750 100644 --- a/diffpy/snmf/tests/test_subroutines.py +++ b/diffpy/snmf/tests/test_subroutines.py @@ -1,7 +1,7 @@ import pytest import numpy as np from diffpy.snmf.subroutines import objective_function, get_stretched_component, reconstruct_data, get_residual_matrix, \ - update_weights_matrix, initialize_arrays, lift_data, create_components + update_weights_matrix, initialize_arrays, lift_data, initialize_components to = [ ([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), @@ -152,18 +152,12 @@ def test_lift_data(tld): expected = tld[1] np.testing.assert_allclose(actual, expected) -tcc = [(2, [0, .5, 1, 1.5], 3, 3), - (3,[0,10,20,30],10,15), - (0,[0],11,30), - (5,[1,1,1,1,1,1],10000,40000), - (3,np.arange(stop=125,step=.05),20,2500), +tcc = [(2, 3,[0, .5, 1, 1.5]), # Regular usage + #(0, 3,[0, .5, 1, 1.5]), # Zero components raise an exception. Not tested ] @pytest.mark.parametrize('tcc', tcc) -def test_create_components(tcc): - actual = create_components(tcc[0], tcc[1], tcc[2], tcc[3]) +def test_initialize_components(tcc): + actual = initialize_components(tcc[0], tcc[1], tcc[2]) assert len(actual) == tcc[0] - for c in actual: - assert len(c.iq) == tcc[3] - assert len(c.weights) == tcc[2] - assert len(c.stretching_factors) == tcc[2] - assert (c.grid == tcc[1]).all() \ No newline at end of file + assert len(actual[0].weights) == tcc[1] + assert (actual[0].grid == np.array(tcc[2])).all()