Skip to content

Commit

Permalink
Toggle boundaries around masked regions
Browse files Browse the repository at this point in the history
Signed-off-by: Brianna Major <[email protected]>
  • Loading branch information
bnmajor committed Oct 11, 2023
1 parent f077e2d commit 9a33507
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
1 change: 1 addition & 0 deletions hexrd/ui/create_polar_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def create_polar_mask_from_raw(name, value):
for det, data in value:
line_data.extend(convert_raw_to_polar(det, data))
create_polar_mask(name, line_data)
HexrdConfig().polar_bounds[name] = line_data


def rebuild_polar_masks():
Expand Down
2 changes: 2 additions & 0 deletions hexrd/ui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def __init__(self):
self.default_cmap = constants.DEFAULT_CMAP
self._previous_structureless_calibration_picks_data = None
self.image_mode = constants.ViewType.raw
self.visible_mask_boundaries = []
self.polar_bounds = {}

# Make sure that the matplotlib font size matches the application
self.font_size = self.font_size
Expand Down
19 changes: 19 additions & 0 deletions hexrd/ui/image_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from PySide2.QtCore import QThreadPool, QTimer, Signal, Qt
from PySide2.QtWidgets import QFileDialog, QMessageBox
from matplotlib import patches

from matplotlib.backends.backend_qt5agg import FigureCanvas

Expand Down Expand Up @@ -177,6 +178,9 @@ def load_images(self, image_names):
img = images_dict[name]
self.axes_images[i].set_data(img)

for name, axis in self.raw_axes.items():
self.update_mask_boundaries(axis, name)

# This will call self.draw_idle()
self.show_saturation()

Expand Down Expand Up @@ -1074,6 +1078,7 @@ def polar_masks_changed(self):
if skip:
return

self.update_mask_boundaries(self.axis)
self.iviewer.reapply_masks()
self.axes_images[0].set_data(self.scaled_images[0])
self.update_azimuthal_integral_plot()
Expand Down Expand Up @@ -1404,6 +1409,20 @@ def polar_show_snip1d(self):
self._snip_viewer_dialog = SnipViewerDialog(background, extent)
self._snip_viewer_dialog.show()

def update_mask_boundaries(self, axis, det=None):
for p in axis.patches:
p.remove()

for boundary in HexrdConfig().visible_mask_boundaries:
if self.mode == ViewType.raw:
raw = HexrdConfig().raw_mask_coords[boundary]
verts = [v for k, v in raw if k == det]
if self.mode == ViewType.polar or self.mode == ViewType.stereo:
verts = HexrdConfig().polar_bounds[boundary]
for vert in verts:
kwargs = {'fill': False, 'lw': 1, 'linestyle': '--'}
axis.add_patch(patches.Polygon(vert, **kwargs))


class PolarXAxisTickLocator(AutoLocator):
"""Subclass the tick locator so we can modify its behavior
Expand Down
48 changes: 39 additions & 9 deletions hexrd/ui/mask_manager_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def create_masks_list(self):
self.masks['threshold'] = (
'threshold', HexrdConfig().threshold_masks)
HexrdConfig().visible_masks = list(self.masks.keys())
HexrdConfig().visible_mask_boundaries = list(self.masks.keys())

def update_masks_list(self, mask_type):
if mask_type == 'raw' or mask_type == 'polar':
Expand Down Expand Up @@ -124,6 +125,9 @@ def setup_table(self, status=True):

# Add checkbox to toggle boundary visibility
cb = QCheckBox()
status = key in HexrdConfig().visible_mask_boundaries
cb.setChecked(status)
# TODO: Disable for certain mask types
cb.setStyleSheet('margin-left:50%; margin-right:50%;')
self.ui.masks_table.setCellWidget(i, 2, cb)
cb.toggled.connect(
Expand Down Expand Up @@ -169,6 +173,8 @@ def remove_mask(self, row, name):
del self.masks[name]
if name in HexrdConfig().visible_masks:
HexrdConfig().visible_masks.remove(name)
if name in HexrdConfig().visible_mask_boundaries:
HexrdConfig().visible_mask_boundaries.remove(name)
HexrdConfig().raw_mask_coords.pop(name, None)
HexrdConfig().masks.pop(name, None)
if mtype == 'threshold':
Expand Down Expand Up @@ -207,6 +213,8 @@ def update_mask_name(self, row):
if self.old_name in HexrdConfig().visible_masks:
HexrdConfig().visible_masks.append(new_name)
HexrdConfig().visible_masks.remove(self.old_name)
HexrdConfig().visible_mask_boundaries.append(new_name)
HexrdConfig().visible_mask_boundaries.remove(self.old_name)

if self.old_name == self.threshold:
self.threshold = new_name
Expand All @@ -223,9 +231,11 @@ def context_menu_event(self, event):
if action == export:
selection = self.ui.masks_table.item(index.row(), 0).text()
data = HexrdConfig().raw_mask_coords[selection]
d = {'_visible': []}
d = {'_visible': [], '_bounds': []}
if selection in HexrdConfig().visible_masks:
d['_visible'] = [selection]
if selection in HexrdConfig().visible_mask_boundaries:
d['_bounds'] = [selection]
for i, (det, mask) in enumerate(data):
parent = d.setdefault(det, {})
parent.setdefault(selection, {})[str(i)] = mask
Expand All @@ -244,7 +254,10 @@ def load_state(self, h5py_group):
self.load_masks(h5py_group['masks'])

def write_all_masks(self, h5py_group=None):
d = {'_visible': HexrdConfig().visible_masks}
d = {
'_visible': HexrdConfig().visible_masks,
'_bounds': HexrdConfig().visible_mask_boundaries
}
for name, data in HexrdConfig().raw_mask_coords.items():
for i, (det, mask) in enumerate(data):
parent = d.setdefault(det, {})
Expand Down Expand Up @@ -276,6 +289,7 @@ def write_masks_to_group(self, data, h5py_group):
def clear_masks(self):
HexrdConfig().masks.clear()
HexrdConfig().visible_masks.clear()
HexrdConfig().visible_mask_boundaries.clear()
HexrdConfig().raw_mask_coords.clear()
self.masks.clear()
self.setup_table()
Expand All @@ -301,6 +315,9 @@ def load_masks(self, h5py_group):
if key == '_visible':
# Convert strings into actual python strings
HexrdConfig().visible_masks = list(h5py_read_string(data))
elif key == '_bounds':
# Convert strings into actual python strings
HexrdConfig().visible_mask_boundaries = list(h5py_read_string(data))
elif key == 'threshold':
HexrdConfig().threshold_values = data['values'][()].tolist()
threshold_masks = {}
Expand Down Expand Up @@ -399,27 +416,40 @@ def show_masks(self):
fig.canvas.draw_idle()
fig.show()

def update_visibility_checkboxes(self):
def update_checkboxes(self, column):
with block_signals(self.ui.masks_table):
for i, key in enumerate(self.masks.keys()):
cb = self.ui.masks_table.cellWidget(i, 1)
cb = self.ui.masks_table.cellWidget(i, column)
status = key in HexrdConfig().visible_masks
cb.setChecked(status)
self.masks_changed()

def hide_all_masks(self):
HexrdConfig().visible_masks.clear()
self.update_visibility_checkboxes()
self.update_checkboxes(column=1)

def show_all_masks(self):
HexrdConfig().visible_masks = list(self.masks.keys())
self.update_visibility_checkboxes()
self.update_checkboxes(column=1)

def boundaries_changed(self):
if self.image_mode in (ViewType.polar, ViewType.stereo):
HexrdConfig().polar_masks_changed.emit()
elif self.image_mode == ViewType.raw:
HexrdConfig().raw_masks_changed.emit()

def toggle_boundary(self, checked, name):
print(f'toggle_boundary: {checked} {name}')
if checked and name not in HexrdConfig().visible_mask_boundaries:
HexrdConfig().visible_mask_boundaries.append(name)
elif not checked and name in HexrdConfig().visible_mask_boundaries:
HexrdConfig().visible_mask_boundaries.remove(name)

self.masks_changed()

def hide_all_boundaries(self):
print('hide_all_boundaries')
HexrdConfig().visible_mask_boundaries.clear()
self.update_checkboxes(column=2)

def show_all_boundaries(self):
print('show_all_boundaries')
HexrdConfig().visible_mask_boundaries = list(self.masks.keys())
self.update_checkboxes(column=2)
2 changes: 1 addition & 1 deletion hexrd/ui/mask_regions_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def discard_interactive_template(self):
det = self.added_templates.pop()
it = self.interactive_templates[det].pop()
it.disconnect()
it.template.remove()
it.clear()

def undo_selection(self):
if not self.added_templates:
Expand Down

0 comments on commit 9a33507

Please sign in to comment.