Skip to content
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

Allow going back a line while hand-picking points #1573

Merged
merged 4 commits into from
Sep 14, 2023
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
53 changes: 37 additions & 16 deletions hexrd/ui/calibration/calibration_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
from functools import partial
import itertools
from pathlib import Path

import h5py
Expand Down Expand Up @@ -185,6 +186,7 @@ def hand_pick_points(self):
picker.point_picked.connect(self.point_picked)
picker.line_completed.connect(self.line_completed)
picker.last_point_removed.connect(self.last_point_removed)
picker.last_line_restored.connect(self.last_line_restored)
picker.finished.connect(self.calibration_line_picker_finished)
picker.view_picks.connect(self.on_view_picks_clicked)
picker.accepted.connect(self.finish_line)
Expand Down Expand Up @@ -626,8 +628,8 @@ def point_picked(self, x, y):
self.current_data_list[ind] = data
self.increment_overlay_data_index()

# In case a point was over-written, force an update of
# the line artists.
# The line builder doesn't accurately update the lines
# during Laue picking, so we force it here.
self.update_lines_from_picks()

def line_completed(self):
Expand All @@ -642,13 +644,26 @@ def last_point_removed(self):
# Still nothing to do
return
# Remove the last point of data
self.current_data_list.pop(-1)
self.current_data_list.pop()
elif self.active_overlay.is_laue:
self.decrement_overlay_data_index()
_, _, ind = self.current_data_path
if 0 <= ind < len(self.current_data_list):
self.current_data_list[ind] = (np.nan, np.nan)

# The line builder doesn't accurately update the lines
# during Laue picking, so we force it here.
self.update_lines_from_picks()

def last_line_restored(self):
# This should only be called for powder overlays, because
# Laue overlays are single-line
while self.current_data_list:
self.current_data_list.pop()

# Go back one line
self.decrement_overlay_data_index()

def disable_line_picker(self, b=True):
if self.line_picker:
self.line_picker.disabled = b
Expand Down Expand Up @@ -686,23 +701,29 @@ def update_lines_from_picks(self):
if not self.line_picker:
return

# Save the previous index
prev_data_index = self.overlay_data_index

picker = self.line_picker
for i, line in enumerate(picker.lines):
self.overlay_data_index = i
if not self.current_data_path:
break
if self.active_overlay.is_powder:
# Save the previous index
prev_data_index = self.overlay_data_index
for i, line in enumerate(picker.lines):
self.overlay_data_index = i
if not self.current_data_path:
break

if self.current_data_list:
data = list(zip(*self.current_data_list))
else:
data = [(), ()]
if self.current_data_list:
data = list(zip(*self.current_data_list))
else:
data = [(), ()]

line.set_data(data)
line.set_data(data)

self.overlay_data_index = prev_data_index
else:
# For Laue, there should be just one line that contains
# all of the points.
data = list(zip(*itertools.chain(*self.overlay_picks.values())))
picker.lines[0].set_data(data)

self.overlay_data_index = prev_data_index
picker.canvas.draw_idle()

def auto_pick_points(self):
Expand Down
2 changes: 1 addition & 1 deletion hexrd/ui/calibration/structureless/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def polar_lines_to_cart(data, instr):
calibrator_line = {}
panel_found = np.zeros(line.shape[0], dtype=bool)
for det_key, panel in instr.detectors.items():
points = angles_to_cart(line, panel)
points = angles_to_cart(line, panel, tvec_c=instr.tvec)
_, on_panel = panel.clip_to_panel(points)
points[~on_panel] = np.nan
valid_points = ~np.any(np.isnan(points), axis=1)
Expand Down
42 changes: 39 additions & 3 deletions hexrd/ui/line_picker_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class LinePickerDialog(QObject):
# Emitted when the last point was removed
last_point_removed = Signal()

# Emitted when the last line was restored
last_line_restored = Signal()

# Emitted when "Picks Table" was clicked
view_picks = Signal()

Expand All @@ -57,7 +60,9 @@ def __init__(self, canvas, parent, single_line_mode=False,
self.ui.setWindowFlags(flags | Qt.Tool)

self.linebuilder = None
self.previous_linebuilders = []
self.lines = []
self.unused_colors = []

self.two_click_mode = self.ui.two_click_mode.isChecked()

Expand Down Expand Up @@ -105,7 +110,10 @@ def update_enable_states(self):
linebuilder = self.linebuilder
enable_back_button = (
linebuilder is not None and
all(z for z in [linebuilder.xs, linebuilder.ys])
(
all(z for z in [linebuilder.xs, linebuilder.ys]) or
bool(self.previous_linebuilders)
)
)
self.ui.back_button.setEnabled(enable_back_button)

Expand Down Expand Up @@ -134,6 +142,7 @@ def clear(self):
self.lines.pop(0).remove()

self.linebuilder = None
self.previous_linebuilders.clear()

self.zoom_canvas.cleanup()
self.zoom_canvas = None
Expand Down Expand Up @@ -164,13 +173,32 @@ def back_button_pressed(self):
return

if not linebuilder.xs or not linebuilder.ys:
# Nothing to delete
# Go back a line instead
self.restore_last_line()
return

linebuilder.remove_last_point()

self.last_point_removed.emit()

def restore_last_line(self):
if not self.previous_linebuilders:
# There are no previous linebuilders to restore
return

self.unused_colors.append(self.lines[-1]._color)

self.linebuilder = self.previous_linebuilders.pop()
self.lines.pop().remove()

self.canvas.draw_idle()

if self.cycle_cursor_colors and self.lines:
prev_color = self.lines[-1]._color
self.zoom_canvas.cursor_color = prev_color

self.last_line_restored.emit()

def two_click_mode_changed(self, on):
self.two_click_mode = on
self.zoom_frozen = False
Expand All @@ -188,7 +216,15 @@ def start(self):

def add_line(self):
ax = self.canvas.axis
color = next(self.color_cycler)
if self.unused_colors:
# Use the unused colors first
color = self.unused_colors.pop()
else:
color = next(self.color_cycler)

if self.linebuilder:
# Save the old linebuilders as we add new ones
self.previous_linebuilders.append(self.linebuilder)

# empty line
line, = ax.plot([], [], color=color, **self.line_settings)
Expand Down
12 changes: 9 additions & 3 deletions hexrd/ui/utils/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ def cart_to_angles(xys, panel, eta_period=None, tvec_s=None, tvec_c=None,
return ang_crds


def angles_to_cart(angles, panel, apply_distortion=True):
angles = np.radians(angles)
return panel.angles_to_cart(angles, apply_distortion=apply_distortion)
def angles_to_cart(angles, panel, tvec_s=None, tvec_c=None,
apply_distortion=True):
kwargs = {
'tth_eta': np.radians(angles),
'tvec_s': tvec_s,
'tvec_c': tvec_c,
'apply_distortion': apply_distortion,
}
return panel.angles_to_cart(**kwargs)


def angles_to_pixels(angles, panel):
Expand Down