Skip to content

Commit

Permalink
image_2d: detect duplicate coordinates more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
pmldrmota authored and dnadlinger committed Oct 14, 2023
1 parent cabba13 commit ebde709
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ndscan/plots/image_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ def __init__(self, model: ScanModel, get_alternate_plot_names):
self.plot = None
self.crosshair = None

self.found_duplicate_coords = False
self.unique_coords = set[tuple[float, float]]()

def _initialise_series(self, channels):
if self.plot is not None:
self.plot_item.removeItem(self.plot.image_item)
Expand Down Expand Up @@ -334,6 +337,18 @@ def bounds(schema):

def _update_points(self, points, invalidate):
if self.plot:
if invalidate:
self.found_duplicate_coords = False
self.unique_coords.clear()
# If all points were unique so far, check if we have duplicates now.
if not self.found_duplicate_coords:
num_skip = len(self.unique_coords)
for x in zip(points["axis_0"][num_skip:], points["axis_1"][num_skip:]):
if x in self.unique_coords:
self.found_duplicate_coords = True
break
else:
self.unique_coords.add(x)
self.plot.data_changed(points, invalidate_previous=invalidate)

def build_context_menu(self, pane_idx: int, builder):
Expand All @@ -355,7 +370,7 @@ def set_both():
action.triggered.connect(set_both)
builder.ensure_separator()

if any(num > 0 for _, num in self.plot.averages_by_coords.values()):
if self.found_duplicate_coords:
action = builder.append_action("Average points with same coordinates")
action.setCheckable(True)
action.setChecked(self.plot.averaging_enabled)
Expand Down

0 comments on commit ebde709

Please sign in to comment.