Skip to content

Commit

Permalink
MNT #289 append (not replace) reflections when clear=False
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Nov 9, 2023
1 parent bf8e0bb commit 3ed3ddd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
28 changes: 5 additions & 23 deletions hkl/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import datetime
import json
import math
import pathlib
import typing
from dataclasses import asdict
Expand Down Expand Up @@ -215,25 +214,6 @@ def validate(self, dc_obj):
_check_range(value, AX_MIN, AX_MAX, f"real-space axis {axis}")
# do not validate 'flag' (not used in hklpy)

def find(self, sample):
"""Find this reflection in the sample's reflections. Return None if not found."""

def equal(a, b):
return math.isclose(a, b, abs_tol=EQUAL_TOLERANCE)

for sref in sample._sample.reflections_get():
rdict = sample._get_reflection_dict(sref)
# fmt: off
matches = [
equal(rdict["reflection"][axis], self.reflection[axis])
for axis in self.reflection
] + [
equal(rdict["wavelength"], self.wavelength)
]
# fmt: on
if False not in matches:
return sref


@dataclass
class DCSample:
Expand Down Expand Up @@ -295,9 +275,6 @@ def write(self, diffractometer):
# temporarily, change the wavelength
w0 = diffractometer.calc.wavelength
w1 = rdict["wavelength"]
refl = reflection.find(sample)
if refl is not None:
sample.remove_reflection(refl)
try:
diffractometer.calc.wavelength = w1
r = sample.add_reflection(*args)
Expand Down Expand Up @@ -568,6 +545,11 @@ def restore(self, data, clear=True, restore_constraints=True):
If ``True`` (default), remove any previous configuration of the
diffractometer and reset it to default values before restoring the
configuration.
If ``False``, sample reflections will be append with all reflections
included in the configuration data for that sample. Existing
reflections will not be changed. The user may need to edit the
list of reflections after ``restore(clear=False)``.
restore_constraints *bool*:
If ``True`` (default), restore any constraints provided.
Expand Down
13 changes: 7 additions & 6 deletions hkl/tests/test_restore_reflections.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def test_issue289(e4cv):
s_444 = silicon.add_reflection(4, 4, 4, (34, 134, 54, 64))
silicon.compute_UB(s_440, s_444)

assert len(main.reflections) == 2
assert len(kryptonite.reflections) == 2
assert len(silicon.reflections) == 2
assert len(vibranium.reflections) == 2
n_saved_reflections = 2
assert len(main.reflections) == n_saved_reflections
assert len(kryptonite.reflections) == n_saved_reflections
assert len(silicon.reflections) == n_saved_reflections
assert len(vibranium.reflections) == n_saved_reflections
# same test, using diffractometer sample dictionary now.
for sample in e4cv.calc._samples.values():
assert len(sample.reflections) == 2, f"{sample.name=}"
assert len(sample.reflections) == n_saved_reflections, f"{sample.name=}"
assert len(e4cv.calc._samples) == 4

agent = DiffractometerConfiguration(e4cv)
Expand All @@ -57,4 +58,4 @@ def test_issue289(e4cv):

assert len(e4cv.calc._samples) == 4
for sample in e4cv.calc._samples.values():
assert len(sample.reflections) == 2
assert len(sample.reflections) == 2 * n_saved_reflections

0 comments on commit 3ed3ddd

Please sign in to comment.