From 9b8c0182b65d0129073de66d4c779f3be3e6ee81 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Thu, 28 Mar 2024 09:26:36 -0500 Subject: [PATCH 1/3] Avoid creating extra computed images if possible This adds a property to the mask manager that we can use to determine whether or not there are border-only masks present. If there are border-only masks present, then the display images and computed images will be different, so we will have to re-create the computed images from scratch. If there are no border-only masks, then the display images and the computed images are the same, and we can skip that extra computation step by re-using the display images as the computed images. Signed-off-by: Patrick Avery --- hexrdgui/image_canvas.py | 15 ++++++++++----- hexrdgui/masking/mask_manager.py | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index ab3c5ced5..99c23f2ca 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -196,11 +196,16 @@ def load_images(self, image_names): img = images_dict[name] self.axes_images[i].set_data(img) - # Create a computed version for the images dict - computed_images_dict = self.scaled_image_dict - if HexrdConfig().stitch_raw_roi_images: - computed_images_dict = self.iviewer.raw_images_to_stitched( - image_names, computed_images_dict) + if MaskManager().contains_border_only_masks: + # Create a computed version for the images dict + computed_images_dict = self.scaled_image_dict + if HexrdConfig().stitch_raw_roi_images: + computed_images_dict = self.iviewer.raw_images_to_stitched( + image_names, computed_images_dict) + else: + # The computed image is the same as the display image. + # Save some computation time for faster rendering. + computed_images_dict = images_dict self.raw_view_images_dict = computed_images_dict for name, axis in self.raw_axes.items(): diff --git a/hexrdgui/masking/mask_manager.py b/hexrdgui/masking/mask_manager.py index c0208b25b..659d0b6f0 100644 --- a/hexrdgui/masking/mask_manager.py +++ b/hexrdgui/masking/mask_manager.py @@ -329,6 +329,14 @@ def update_mask_visibility(self, name, visibility): def update_border_visibility(self, name, visibility): self.masks[name].update_border_visibility(visibility) + @property + def contains_border_only_masks(self): + # If we have any border-only masks, that means the display images + # are different from computed images, and require extra computation. + # If this returns False, we can skip that extra computation and + # set display images and computed images to be the same. + return any(x.show_border and not x.visible for x in self.masks) + def threshold_toggled(self): if self.threshold_mask: self.remove_mask(self.threshold_mask.name) From bcb7e14ec1aec4610d4e8b507aa0ff5e31c5fbf0 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Thu, 28 Mar 2024 09:28:33 -0500 Subject: [PATCH 2/3] Do not update blit manager if no overlays This was a little time consuming to do. Skip it if there are no overlays. Signed-off-by: Patrick Avery --- hexrdgui/image_canvas.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 99c23f2ca..e034cfc65 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -642,7 +642,9 @@ def update_overlays(self): return self.remove_all_overlay_artists() - if not HexrdConfig().show_overlays: + if not HexrdConfig().show_overlays or not HexrdConfig().overlays: + # Avoid proceeding if possible, as updating the blit manager + # can be time consuming. self.remove_all_overlay_artists() self.draw_idle() return From 08505cdddb9282074498053783464f540fa50238 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Thu, 28 Mar 2024 09:30:11 -0500 Subject: [PATCH 3/3] Pass `images_dict` into create_raw_masks_dict() This is to avoid creating the images_dict a second time, which can be time consuming. Now we pass it in as an argument. Signed-off-by: Patrick Avery --- hexrdgui/hexrd_config.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/hexrdgui/hexrd_config.py b/hexrdgui/hexrd_config.py index 9b8051737..a7a2f239f 100644 --- a/hexrdgui/hexrd_config.py +++ b/hexrdgui/hexrd_config.py @@ -888,13 +888,13 @@ def images_dict(self): @property def raw_masks_dict(self): - return self.create_raw_masks_dict(display=False) + return self.create_raw_masks_dict(self.images_dict, display=False) - def create_raw_masks_dict(self, display=False): + def create_raw_masks_dict(self, images_dict, display=False): """Get a masks dict""" from hexrdgui.masking.mask_manager import MaskManager masks_dict = {} - for name, img in self.images_dict.items(): + for name, img in images_dict.items(): final_mask = np.ones(img.shape, dtype=bool) for mask in MaskManager().masks.values(): if display and not mask.visible: @@ -940,22 +940,23 @@ def create_masked_images_dict(self, fill_value=0, display=False): # and no panel buffers. fill_value = 0 - raw_masks_dict = self.create_raw_masks_dict(display=display) + raw_masks_dict = self.create_raw_masks_dict(images_dict, + display=display) for det, mask in raw_masks_dict.items(): + img = images_dict[det] if has_panel_buffers: panel = instr.detectors[det] utils.convert_panel_buffer_to_2d_array(panel) - for name, img in images_dict.items(): - if (np.issubdtype(type(fill_value), np.floating) and - not np.issubdtype(img.dtype, np.floating)): - img = img.astype(float) - images_dict[name] = img - if det == name: - img[~mask] = fill_value - - if has_panel_buffers: - img[~panel.panel_buffer] = fill_value + if (np.issubdtype(type(fill_value), np.floating) and + not np.issubdtype(img.dtype, np.floating)): + img = img.astype(float) + images_dict[det] = img + + img[~mask] = fill_value + + if has_panel_buffers: + img[~panel.panel_buffer] = fill_value return images_dict