Skip to content

test function on_xtype #192

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 9 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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>
13 changes: 7 additions & 6 deletions src/diffpy/utils/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

def _xtype_wmsg(xtype):
return (
f"WARNING: I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an "
f"I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an "
f"xtype from {*XQUANTITIES, }"
)

Expand Down Expand Up @@ -376,16 +376,17 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None):
return scaled

def on_xtype(self, xtype):
"""
return a 2D np array with x in the first column and y in the second for x of type type
f"""
return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid

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

Returns
-------

a list of two 1D np array with x and y data
"""
if xtype.lower() in ANGLEQUANTITIES:
return self.on_tth()
Expand All @@ -394,7 +395,7 @@ def on_xtype(self, xtype):
elif xtype.lower() in DQUANTITIES:
return self.on_d()
else:
warnings.warn(_xtype_wmsg(xtype))
raise ValueError(_xtype_wmsg(xtype))

def dump(self, filepath, xtype=None):
if xtype is None:
Expand Down
29 changes: 20 additions & 9 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import re
from pathlib import Path

import numpy as np
import pytest
from freezegun import freeze_time

from diffpy.utils.diffraction_objects import DiffractionObject
from diffpy.utils.transforms import wavelength_warning_emsg
from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject


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


def _test_valid_diffraction_objects(actual_diffraction_object, function, expected_array):
if actual_diffraction_object.wavelength is None:
with pytest.warns(UserWarning) as warn_record:
getattr(actual_diffraction_object, function)()
assert str(warn_record[0].message) == wavelength_warning_emsg
actual_array = getattr(actual_diffraction_object, function)()
return np.allclose(actual_array, expected_array)
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])])
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"I don't know how to handle the xtype, 'invalid'. Please rerun specifying an "
f"xtype from {*XQUANTITIES, }"
),
):
test.on_xtype("invalid")


def test_dump(tmp_path, mocker):
Expand Down
Loading