Skip to content

Commit e4b78fd

Browse files
initial commit
1 parent 49bb353 commit e4b78fd

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/diffpy/utils/scattering_objects/diffraction_objects.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,24 @@ def _set_array_from_range(self, begin, end, step_size=None, n_steps=None):
614614
return array
615615

616616
def get_angle_index(self, angle):
617-
count = 0
617+
"""
618+
returns the index of a given angle in the angles list
619+
620+
Parameters
621+
----------
622+
angle float
623+
the angle to search for
624+
625+
Returns
626+
-------
627+
the index of the angle in the angles list
628+
"""
629+
if not hasattr(self, "angles"):
630+
self.angles = np.array([])
618631
for i, target in enumerate(self.angles):
619632
if angle == target:
620633
return i
621-
else:
622-
count += 1
623-
if count >= len(self.angles):
624-
raise IndexError(f"WARNING: no angle {angle} found in angles list")
634+
raise IndexError(f"WARNING: no angle {angle} found in angles list.")
625635

626636
def insert_scattering_quantity(
627637
self,

tests/diffpy/utils/scattering_objects/test_diffraction_objects.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ def _test_valid_diffraction_objects(actual_diffraction_object, function, expecte
241241
return np.allclose(actual_array, expected_array)
242242

243243

244+
def test_get_angle_index():
245+
test = DiffractionObject()
246+
test.angles = np.array([10, 20, 30, 40, 50, 60])
247+
actual_angle_index = test.get_angle_index(angle=10)
248+
assert actual_angle_index == 0
249+
250+
251+
def test_get_angle_index_bad():
252+
test = DiffractionObject()
253+
# empty angles list
254+
with pytest.raises(IndexError, match="WARNING: no angle 11 found in angles list."):
255+
test.get_angle_index(angle=11)
256+
# pre-defined angles list
257+
test.angles = np.array([10, 20, 30, 40, 50, 60])
258+
with pytest.raises(IndexError, match="WARNING: no angle 11 found in angles list."):
259+
test.get_angle_index(angle=11)
260+
261+
244262
def test_dump(tmp_path, mocker):
245263
x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6)
246264
directory = Path(tmp_path)

0 commit comments

Comments
 (0)