-
Notifications
You must be signed in to change notification settings - Fork 21
1) Capture no wavelength UserWarning
for test_q_to_thh
method 2) discuss passing variables to @pytest.mark.parametrize
#225
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
781e358
Capture no wavelenght warning for test_q_to_tth
bobleesj 7ac21a3
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] 792ce17
Apply precommit
bobleesj 4d0f762
Merge branch 'warnings' of https://github.com/bobleesj/diffpy.utils i…
bobleesj 8c891db
use variables within parametrize for test_diffraction_objects
bobleesj cce5716
Remove INFO and improve how to set wavelenght warning msg
bobleesj 6bb822f
Use variable names within parametrize for transforms
bobleesj d176b6b
Remove another duplicate line for actual_tth = q_to_tth(q, wavelength)
bobleesj 69aa7c7
Improve wavelength warning msg
bobleesj 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
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,81 +1,97 @@ | ||
import re | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
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 | ||
([None, np.empty((0))], np.empty((0))), | ||
(None, np.empty((0)), np.empty((0))), | ||
# UC2: Empty q values, wavelength specified, return empty arrays | ||
([4 * np.pi, np.empty((0))], np.empty(0)), | ||
(4 * np.pi, np.empty((0)), np.empty(0)), | ||
# UC3: User specified valid q values, no wavelength, return empty arrays | ||
( | ||
[None, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])], | ||
None, | ||
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]), | ||
np.array([0, 1, 2, 3, 4, 5]), | ||
), | ||
# UC4: User specified valid q values (with wavelength) | ||
# expected tth values are 2*arcsin(q) in degrees | ||
([4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0])], np.array([0, 90.0, 180.0])), | ||
(4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("inputs, expected", params_q_to_tth) | ||
def test_q_to_tth(inputs, expected): | ||
actual = q_to_tth(inputs[1], inputs[0]) | ||
assert np.allclose(expected, actual) | ||
@pytest.mark.parametrize("wavelength, q, expected_tth", params_q_to_tth) | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_q_to_tth(wavelength, q, expected_tth): | ||
|
||
wavelength_warning_emsg = ( | ||
"No wavelength has been specified. You can continue to use the DiffractionObject, but " | ||
"some of its powerful features will not be available. " | ||
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), " | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"you may set do.wavelength = 1.54 with the unit in angstroms." | ||
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. Please see the small tweak above |
||
) | ||
if wavelength is None: | ||
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_emsg)): | ||
actual_tth = q_to_tth(q, wavelength) | ||
actual_tth = q_to_tth(q, wavelength) | ||
else: | ||
actual_tth = q_to_tth(q, wavelength) | ||
|
||
assert np.allclose(expected_tth, actual_tth) | ||
|
||
|
||
params_q_to_tth_bad = [ | ||
# UC1: user specified invalid q values that result in tth > 180 degrees | ||
( | ||
[4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.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.", | ||
], | ||
4 * np.pi, | ||
np.array([0.2, 0.4, 0.6, 0.8, 1, 1.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([0, 0.2, 0.4, 0.6, 0.8, 1])], | ||
[ | ||
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.", | ||
], | ||
100, | ||
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]), | ||
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_q_to_tth_bad) | ||
def test_q_to_tth_bad(inputs, expected): | ||
with pytest.raises(expected[0], match=expected[1]): | ||
q_to_tth(inputs[1], inputs[0]) | ||
@pytest.mark.parametrize("q, wavelength, expected_error_type, expected_error_msg", params_q_to_tth_bad) | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_q_to_tth_bad(q, wavelength, expected_error_type, expected_error_msg): | ||
with pytest.raises(expected_error_type, match=expected_error_msg): | ||
q_to_tth(wavelength, q) | ||
|
||
|
||
params_tth_to_q = [ | ||
# UC0: User specified empty tth values (without wavelength) | ||
([None, np.array([])], np.array([])), | ||
(None, np.array([]), np.array([])), | ||
# UC1: User specified empty tth values (with wavelength) | ||
([4 * np.pi, np.array([])], np.array([])), | ||
(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])], | ||
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) | ||
# expected q values are sin15, sin30, sin45, sin60, sin90 | ||
( | ||
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])], | ||
4 * np.pi, | ||
np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]), | ||
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]), | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("inputs, expected", params_tth_to_q) | ||
def test_tth_to_q(inputs, expected): | ||
actual = tth_to_q(inputs[1], inputs[0]) | ||
assert np.allclose(actual, expected) | ||
@pytest.mark.parametrize("wavelength, tth, expected_q", params_tth_to_q) | ||
def test_tth_to_q(wavelength, tth, expected_q): | ||
actual_q = tth_to_q(tth, wavelength) | ||
assert np.allclose(actual_q, expected_q) | ||
|
||
|
||
params_tth_to_q_bad = [ | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is clearer to say than 1.54 here, it add "for a wavelength of 1.54 angstroms". Which would someone like Fillipo find clearer?