Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/xtype.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* functionality to return the 2D array based on the specified xtype

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
11 changes: 6 additions & 5 deletions src/diffpy/utils/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,25 +376,26 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None):
return scaled

def on_xtype(self, xtype):
"""
f"""
return a 2D np array with x in the first column and y in the second for x of type type
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to return a list of two 1D arrays


Parameters
----------
xtype
xtype str
the type of quantity for the independent variable from {*XQUANTITIES, }

Returns
-------

a 2D np array with x and y data
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

"""
if xtype.lower() in ANGLEQUANTITIES:
return self.on_tth()
elif xtype.lower() in QQUANTITIES:
return self.on_q()
elif xtype.lower() in DQUANTITIES:
return self.on_d
return self.on_d()
else:
raise ValueError(f"Unknown xtype: {xtype}. Allowed xtypes are {*XQUANTITIES, }.")
raise ValueError(_xtype_wmsg(xtype))
Comment thread
yucongalicechen marked this conversation as resolved.

def dump(self, filepath, xtype=None):
if xtype is None:
Expand Down
46 changes: 12 additions & 34 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import pytest
from freezegun import freeze_time

from diffpy.utils.diffraction_objects import (
ANGLEQUANTITIES,
DQUANTITIES,
QQUANTITIES,
XQUANTITIES,
DiffractionObject,
)
from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject


def compare_dicts(dict1, dict2):
Expand Down Expand Up @@ -208,38 +202,22 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
assert dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__) == expected


params_on_xtype = [
(
[
np.array([100, 200, 300, 400, 500, 600]), # intensity array
np.array([0, 30, 60, 90, 120, 180]), # tth array
np.array([1, 2, 3, 4, 5, 6]), # q array
np.array([10, 20, 30, 40, 50, 60]), # d array
],
[
np.array([[0, 30, 60, 90, 120, 180], [100, 200, 300, 400, 500, 600]]), # expected on_tth
np.array([[1, 2, 3, 4, 5, 6], [100, 200, 300, 400, 500, 600]]), # expected on_q
np.array([[10, 20, 30, 40, 50, 60], [100, 200, 300, 400, 500, 600]]), # expected on_d
],
)
]


@pytest.mark.parametrize("inputs, expected", params_on_xtype)
def test_on_xtype(inputs, expected):
test = DiffractionObject()
test.on_tth = np.array([inputs[1], inputs[0]])
test.on_q = np.array([inputs[2], inputs[0]])
test.on_d = np.array([inputs[3], inputs[0]])
for xtype_list, expected_value in zip([ANGLEQUANTITIES, QQUANTITIES, DQUANTITIES], expected):
for xtype in xtype_list:
assert np.allclose(test.on_xtype(xtype), expected_value)
def test_on_xtype():
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
assert np.allclose(test.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])])
Comment thread
sbillinge marked this conversation as resolved.
assert np.allclose(test.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])])
assert np.allclose(test.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])])
assert np.allclose(test.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])])


def test_on_xtype_bad():
test = DiffractionObject()
with pytest.raises(
ValueError, match=re.escape(f"Unknown xtype: invalid. Allowed xtypes are {*XQUANTITIES, }.")
ValueError,
match=re.escape(
f"WARNING: I don't know how to handle the xtype, 'invalid'. Please rerun specifying an "
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think are now raising an exception, right? So this isn't a warning. Just remove WARNING: Python will handle the rest.

f"xtype from {*XQUANTITIES, }"
),
):
test.on_xtype("invalid")

Expand Down