Skip to content

Commit c48b962

Browse files
committed
Consider tth distortion when saving picks
We previously would not consider the tth distortion when saving picks, so the cartesian coordinates would be incorrect. Now we reverse the tth distortion if needed. Also, the two calibration workflows (Composite and Fast Powder) write the coordinates directly from the overlays, which is more accurate (some precision is lost when converting back and forth between cartesian and polar when the tth distortion is applied, because it is a field correction). Signed-off-by: Patrick Avery <[email protected]>
1 parent 310a19d commit c48b962

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

hexrdgui/calibration/auto/powder_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def on_finished():
242242
def save_picks_to_file(self, selected_file):
243243
# Reuse the same logic from the HKLPicksTreeViewDialog
244244
dialog = self.create_hkl_picks_tree_view_dialog()
245-
dialog.export_picks(selected_file)
245+
dialog.export_picks_from_overlays(selected_file, self.overlays)
246246

247247
def load_picks_from_file(self, selected_file):
248248
# Reuse the same logic from the HKLPicksTreeViewDialog

hexrdgui/calibration/calibration_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,8 @@ def on_finished():
10451045

10461046
def save_picks_to_file(self, selected_file):
10471047
# Reuse the same logic from the HKLPicksTreeViewDialog
1048-
self.edit_picks_dialog.export_picks(selected_file)
1048+
d = self.edit_picks_dialog
1049+
d.export_picks_from_overlays(selected_file, self.overlays)
10491050

10501051
def load_picks_from_file(self, selected_file):
10511052
# Reuse the same logic from the HKLPicksTreeViewDialog

hexrdgui/calibration/hkl_picks_tree_view_dialog.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from hexrdgui.ui_loader import UiLoader
1717
from hexrdgui.utils.conversions import angles_to_cart, cart_to_angles
1818
from hexrdgui.utils.dicts import ndarrays_to_lists
19+
from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed
1920

2021

2122
class HKLPicksTreeViewDialog:
@@ -92,18 +93,28 @@ def export_picks_clicked(self):
9293
return self.export_picks(selected_file)
9394

9495
def export_picks(self, filename):
96+
return self._export_dict_to_file(filename, {
97+
'angular': self.dictionary,
98+
'cartesian': self.dict_with_cart_coords,
99+
})
100+
101+
def export_picks_from_overlays(self, filename, overlays):
102+
# Export picks from overlays using the same export logic as
103+
# the regular dictionary.
104+
return self._export_dict_to_file(filename, {
105+
'angular': overlays_to_tree_format(overlays, polar=True),
106+
'cartesian': overlays_to_tree_format(overlays, polar=False),
107+
})
108+
109+
def _export_dict_to_file(self, filename: str, export_data: dict):
95110
filename = Path(filename)
96111

97112
if filename.exists():
98113
filename.unlink()
99114

100-
# unwrap_dict_to_h5 unfortunately modifies the data
101-
# make a deep copy to avoid the modification.
102-
export_data = {
103-
'angular': copy.deepcopy(self.dictionary),
104-
'cartesian': self.dict_with_cart_coords,
105-
}
106-
115+
# unwrap_dict_to_h5 unfortunately modifies the data.
116+
# Make a deep copy to avoid the modification.
117+
export_data = copy.deepcopy(export_data)
107118
with h5py.File(filename, 'w') as wf:
108119
unwrap_dict_to_h5(wf, export_data)
109120

@@ -178,7 +189,7 @@ def button_box_visible(self, b):
178189
self.ui.button_box.setVisible(b)
179190

180191

181-
def convert_picks(picks, conversion_function, **kwargs):
192+
def convert_picks(picks, conversion_function):
182193
instr = create_hedm_instrument()
183194
ret = copy.deepcopy(picks)
184195
for name, detectors in ret.items():
@@ -191,25 +202,43 @@ def convert_picks(picks, conversion_function, **kwargs):
191202
# Avoid the runtime warning
192203
hkls[hkl] = [np.nan, np.nan]
193204
else:
194-
hkls[hkl] = conversion_function([spot], panel,
195-
**kwargs)[0]
205+
hkls[hkl] = conversion_function([spot], panel)[0]
196206
continue
197207

198208
# Must be powder
199209
for hkl, line in hkls.items():
200210
if len(line) != 0:
201-
hkls[hkl] = conversion_function(line, panel, **kwargs)
211+
hkls[hkl] = conversion_function(line, panel)
202212

203213
return ret
204214

205215

206216
def picks_angles_to_cartesian(picks):
207-
return convert_picks(picks, angles_to_cart)
217+
# Create the conversion function
218+
def func(angs, panel):
219+
# Reverse the tth distortion first
220+
angs = apply_tth_distortion_if_needed(
221+
angs,
222+
in_degrees=True,
223+
reverse=True,
224+
)
225+
# Now convert to cart
226+
return angles_to_cart(angs, panel)
227+
228+
return convert_picks(picks, func)
208229

209230

210231
def picks_cartesian_to_angles(picks):
211-
kwargs = {'eta_period': HexrdConfig().polar_res_eta_period}
212-
return convert_picks(picks, cart_to_angles, **kwargs)
232+
# Create the conversion function
233+
eta_period = HexrdConfig().polar_res_eta_period
234+
235+
def func(xys, panel):
236+
angs = cart_to_angles(xys, panel, eta_period=eta_period)
237+
238+
# Apply tth distortion now as well
239+
return apply_tth_distortion_if_needed(angs, in_degrees=True)
240+
241+
return convert_picks(picks, func)
213242

214243

215244
def generate_picks_results(overlays, polar=True):
@@ -249,8 +278,8 @@ def generate_picks_results(overlays, polar=True):
249278
return pick_results
250279

251280

252-
def overlays_to_tree_format(overlays):
253-
picks = generate_picks_results(overlays)
281+
def overlays_to_tree_format(overlays, polar=True):
282+
picks = generate_picks_results(overlays, polar=polar)
254283
return picks_to_tree_format(picks)
255284

256285

0 commit comments

Comments
 (0)