Skip to content

Commit 443fe36

Browse files
authored
Merge pull request diffpy#192 from yucongalicechen/xtype-test
test function on_xtype
2 parents 3aef608 + 7eb0c0d commit 443fe36

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

news/xtype.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* functionality to return the 2D array based on the specified xtype
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/diffraction_objects.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
def _xtype_wmsg(xtype):
2323
return (
24-
f"WARNING: I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an "
24+
f"I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an "
2525
f"xtype from {*XQUANTITIES, }"
2626
)
2727

@@ -401,16 +401,17 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None):
401401
return scaled
402402

403403
def on_xtype(self, xtype):
404-
"""
405-
return a 2D np array with x in the first column and y in the second for x of type type
404+
f"""
405+
return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid
406406
407407
Parameters
408408
----------
409-
xtype
409+
xtype str
410+
the type of quantity for the independent variable from {*XQUANTITIES, }
410411
411412
Returns
412413
-------
413-
414+
a list of two 1D np array with x and y data
414415
"""
415416
if xtype.lower() in ANGLEQUANTITIES:
416417
return self.on_tth()
@@ -419,7 +420,7 @@ def on_xtype(self, xtype):
419420
elif xtype.lower() in DQUANTITIES:
420421
return self.on_d()
421422
else:
422-
warnings.warn(_xtype_wmsg(xtype))
423+
raise ValueError(_xtype_wmsg(xtype))
423424

424425
def dump(self, filepath, xtype=None):
425426
if xtype is None:

tests/test_diffraction_objects.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from deepdiff import DeepDiff
77
from freezegun import freeze_time
88

9-
from diffpy.utils.diffraction_objects import DiffractionObject
10-
from diffpy.utils.transforms import wavelength_warning_emsg
9+
from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject
1110

1211

1312
def compare_dicts(dict1, dict2):
@@ -204,13 +203,24 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
204203
assert dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__) == expected
205204

206205

207-
def _test_valid_diffraction_objects(actual_diffraction_object, function, expected_array):
208-
if actual_diffraction_object.wavelength is None:
209-
with pytest.warns(UserWarning) as warn_record:
210-
getattr(actual_diffraction_object, function)()
211-
assert str(warn_record[0].message) == wavelength_warning_emsg
212-
actual_array = getattr(actual_diffraction_object, function)()
213-
return np.allclose(actual_array, expected_array)
206+
def test_on_xtype():
207+
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
208+
assert np.allclose(test.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])])
209+
assert np.allclose(test.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])])
210+
assert np.allclose(test.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])])
211+
assert np.allclose(test.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])])
212+
213+
214+
def test_on_xtype_bad():
215+
test = DiffractionObject()
216+
with pytest.raises(
217+
ValueError,
218+
match=re.escape(
219+
f"I don't know how to handle the xtype, 'invalid'. Please rerun specifying an "
220+
f"xtype from {*XQUANTITIES, }"
221+
),
222+
):
223+
test.on_xtype("invalid")
214224

215225

216226
params_index = [

0 commit comments

Comments
 (0)