-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from aajayi-21/objective_function
function objective_function
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import numpy as np | ||
|
||
|
||
# import scipy.interpolate | ||
|
||
|
||
def objective_function(residual_matrix, stretching_factor_matrix, smoothness, smoothness_term, component_matrix, | ||
sparsity): | ||
"""Defines the objective function of the algorithm and returns its value. | ||
Calculates the value of '(||residual_matrix||_F) ** 2 + smoothness * (||smoothness_term * | ||
stretching_factor_matrix.T||)**2 + sparsity * sum(component_matrix ** .5)' and returns its value. | ||
Parameters | ||
---------- | ||
residual_matrix: 2d array like | ||
The matrix where each column is the difference between an experimental PDF/XRD pattern and a calculated PDF/XRD | ||
pattern at each grid point. Has dimensions R x M where R is the length of each pattern and M is the amount of | ||
patterns. | ||
stretching_factor_matrix: 2d array like | ||
The matrix containing the stretching factors of the calculated component signal. Has dimensions K x M where K is | ||
the amount of components and M is the number of experimental PDF/XRD patterns. | ||
smoothness: float | ||
The coefficient of the smoothness term which determines the intensity of the smoothness term and its behavior. | ||
It is not very sensitive and is usually adjusted by multiplying it by ten. | ||
smoothness_term: 2d array like | ||
The regularization term that ensures that smooth changes in the component stretching signals are favored. | ||
Has dimensions (M-2) x M where M is the amount of experimentally obtained PDF/XRD patterns, the moment amount. | ||
component_matrix: 2d array like | ||
The matrix containing the calculated component signals of the experimental PDF/XRD patterns. Has dimensions R x K | ||
where R is the signal length and K is the number of component signals. | ||
sparsity: float | ||
The parameter determining the intensity of the sparsity regularization term which enables the algorithm to | ||
exploit the sparse nature of XRD data. It is usually adjusted by doubling. | ||
Returns | ||
------- | ||
float | ||
The value of the objective function. | ||
""" | ||
residual_matrix = np.asarray(residual_matrix) | ||
stretching_factor_matrix = np.asarray(stretching_factor_matrix) | ||
component_matrix = np.asarray(component_matrix) | ||
return .5 * np.linalg.norm(residual_matrix, 'fro') ** 2 + .5 * smoothness * np.linalg.norm( | ||
smoothness_term @ stretching_factor_matrix.T, 'fro') ** 2 + sparsity * np.sum(np.sqrt(component_matrix)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
from diffpy.snmf.subroutines import objective_function | ||
|
||
to = [ | ||
([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 1], 2.574e14), | ||
([[[11, 2], [31, 4]], [[5, 63], [7, 18]], .001, [[21, 2], [3, 4]], [[11, 22], [3, 40]], 1], 650.4576), | ||
([[[1, 2], [3, 4]], [[5, 6], [7, 8]], 1e11, [[1, 2], [3, 4]], [[1, 2], [3, 4]], 0], 2.574e14), | ||
|
||
] | ||
|
||
|
||
@pytest.mark.parametrize("to", to) | ||
def test_objective_function(to): | ||
actual = objective_function(to[0][0], to[0][1], to[0][2], to[0][3], to[0][4], to[0][5]) | ||
expected = to[1] | ||
assert actual == pytest.approx(expected) |