-
Notifications
You must be signed in to change notification settings - Fork 21
add d_to_tth, tth_to_d #199
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
| **Added:** | ||
|
|
||
| * functions to convert between d and tth | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from diffpy.utils.transforms import d_to_q, q_to_d, q_to_tth, tth_to_q | ||
| from diffpy.utils.transforms import d_to_q, d_to_tth, q_to_d, q_to_tth, tth_to_d, tth_to_q | ||
|
|
||
| params_q_to_tth = [ | ||
| # UC1: Empty q values, no wavelength, return empty arrays | ||
|
|
@@ -31,7 +31,7 @@ def test_q_to_tth(inputs, expected): | |
| [4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2])], | ||
| [ | ||
| ValueError, | ||
| "The supplied q-array and wavelength will result in an impossible two-theta. " | ||
| "The supplied input array and wavelength will result in an impossible two-theta. " | ||
| "Please check these values and re-instantiate the DiffractionObject with correct values.", | ||
| ], | ||
| ), | ||
|
|
@@ -40,7 +40,7 @@ def test_q_to_tth(inputs, expected): | |
| [100, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])], | ||
| [ | ||
| ValueError, | ||
| "The supplied q-array and wavelength will result in an impossible two-theta. " | ||
| "The supplied input array and wavelength will result in an impossible two-theta. " | ||
| "Please check these values and re-instantiate the DiffractionObject with correct values.", | ||
| ], | ||
| ), | ||
|
|
@@ -103,8 +103,8 @@ def test_tth_to_q_bad(inputs, expected): | |
| ([np.array([])], np.array([])), | ||
| # UC2: User specified valid q values | ||
| ( | ||
| [np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0])], | ||
| np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]), | ||
| [np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])], | ||
| np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small edit so that we have an increasing qarray |
||
| ), | ||
| ] | ||
|
|
||
|
|
@@ -120,8 +120,8 @@ def test_q_to_d(inputs, expected): | |
| ([np.array([])], np.array([])), | ||
| # UC2: User specified valid d values | ||
| ( | ||
| [np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])], | ||
| np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]), | ||
| [np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0])], | ||
| np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]), | ||
| ), | ||
| ] | ||
|
|
||
|
|
@@ -130,3 +130,99 @@ def test_q_to_d(inputs, expected): | |
| def test_d_to_q(inputs, expected): | ||
| actual = d_to_q(inputs[0]) | ||
| assert np.allclose(actual, expected) | ||
|
|
||
|
|
||
| params_tth_to_d = [ | ||
| # UC0: User specified empty tth values (without wavelength) | ||
| ([None, np.array([])], np.array([])), | ||
| # UC1: User specified empty tth values (with wavelength) | ||
| ([4 * np.pi, np.array([])], np.array([])), | ||
| # UC2: User specified valid tth values between 0-180 degrees (without wavelength) | ||
| ( | ||
| [None, np.array([0, 30, 60, 90, 120, 180])], | ||
| np.array([0, 1, 2, 3, 4, 5]), | ||
| ), | ||
| # UC3: User specified valid tth values between 0-180 degrees (with wavelength) | ||
| ( | ||
| [4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])], | ||
| np.array([np.inf, 24.27636, 12.56637, 8.88577, 7.25520, 6.28319]), | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("inputs, expected", params_tth_to_d) | ||
| def test_tth_to_d(inputs, expected): | ||
| actual = tth_to_d(inputs[1], inputs[0]) | ||
| assert np.allclose(actual, expected) | ||
|
|
||
|
|
||
| params_tth_to_d_bad = [ | ||
| # UC1: user specified an invalid tth value of > 180 degrees (without wavelength) | ||
| ( | ||
| [None, np.array([0, 30, 60, 90, 120, 181])], | ||
| [ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."], | ||
| ), | ||
| # UC2: user specified an invalid tth value of > 180 degrees (with wavelength) | ||
| ( | ||
| [4 * np.pi, np.array([0, 30, 60, 90, 120, 181])], | ||
| [ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."], | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("inputs, expected", params_tth_to_d_bad) | ||
| def test_tth_to_d_bad(inputs, expected): | ||
| with pytest.raises(expected[0], match=expected[1]): | ||
| tth_to_d(inputs[1], inputs[0]) | ||
|
|
||
|
|
||
| params_d_to_tth = [ | ||
| # UC1: Empty d values, no wavelength, return empty arrays | ||
| ([None, np.empty((0))], np.empty((0))), | ||
| # UC2: Empty d values, wavelength specified, return empty arrays | ||
| ([4 * np.pi, np.empty((0))], np.empty(0)), | ||
| # UC3: User specified valid d values, no wavelength, return empty arrays | ||
| ( | ||
| [None, np.array([1, 0.8, 0.6, 0.4, 0.2, 0])], | ||
| np.array([0, 1, 2, 3, 4, 5]), | ||
| ), | ||
| # UC4: User specified valid d values (with wavelength) | ||
| ( | ||
| [4 * np.pi, np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi])], | ||
| np.array([60.0, 90.0, 120.0]), | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("inputs, expected", params_d_to_tth) | ||
| def test_d_to_tth(inputs, expected): | ||
| actual = d_to_tth(inputs[1], inputs[0]) | ||
| assert np.allclose(expected, actual) | ||
|
|
||
|
|
||
| params_d_to_tth_bad = [ | ||
| # UC1: user specified invalid d values that result in tth > 180 degrees | ||
| ( | ||
| [4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2])], | ||
| [ | ||
| ValueError, | ||
| "The supplied input array and wavelength will result in an impossible two-theta. " | ||
| "Please check these values and re-instantiate the DiffractionObject with correct values.", | ||
| ], | ||
| ), | ||
| # UC2: user specified a wrong wavelength that result in tth > 180 degrees | ||
| ( | ||
| [100, np.array([1, 0.8, 0.6, 0.4, 0.2, 0])], | ||
| [ | ||
| ValueError, | ||
| "The supplied input array and wavelength will result in an impossible two-theta. " | ||
| "Please check these values and re-instantiate the DiffractionObject with correct values.", | ||
| ], | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("inputs, expected", params_d_to_tth_bad) | ||
| def test_d_to_tth_bad(inputs, expected): | ||
| with pytest.raises(expected[0], match=expected[1]): | ||
| d_to_tth(inputs[1], inputs[0]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.