Skip to content

Commit

Permalink
Merge pull request #1653 from bnmajor/toggle-mask-boundaries
Browse files Browse the repository at this point in the history
Toggle mask boundaries
  • Loading branch information
psavery authored Mar 1, 2024
2 parents 5cf9089 + f4788cd commit fc8b793
Show file tree
Hide file tree
Showing 13 changed files with 384 additions and 112 deletions.
5 changes: 5 additions & 0 deletions hexrdgui/calibration/cartesian_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self):
self.type = ViewType.cartesian
self.instr = create_hedm_instrument()
self.images_dict = HexrdConfig().images_dict
self.img = None

# Perform some checks before proceeding
self.check_keys_match()
Expand Down Expand Up @@ -283,6 +284,10 @@ def create_warped_image(self, detector_id):
self.warp_dict[detector_id] = res
return res

@property
def display_img(self):
return self.img

def generate_image(self):
img = np.zeros((self.dpanel.rows, self.dpanel.cols))
for key in self.images_dict.keys():
Expand Down
4 changes: 4 additions & 0 deletions hexrdgui/calibration/polar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def snipped_img(self):
def img(self):
return self.pv.img

@property
def display_img(self):
return self.pv.display_img

@property
def snip_background(self):
return self.pv.snip_background
Expand Down
49 changes: 40 additions & 9 deletions hexrdgui/calibration/polarview.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def __init__(self, instrument, distortion_instrument=None):

self.raw_img = None
self.snipped_img = None
self.processed_img = None
self.computation_img = None
self.display_image = None

self.snip_background = None

Expand Down Expand Up @@ -374,11 +375,17 @@ def apply_image_processing(self):

# Apply the masks before the polar_tth_distortion, because the
# masks should also be distorted as well.
img = self.apply_masks(img)

img = self.apply_tth_distortion(img)
# We only apply "visible" masks to the display image
img = self.apply_visible_masks(img)
disp_img = self.apply_tth_distortion(img)
self.display_image = disp_img

self.processed_img = img
# Both "visible" and "boundary" masks are applied to the
# computational image
comp_img = self.apply_boundary_masks(img)
comp_img = self.apply_tth_distortion(comp_img)
self.computation_img = comp_img

def apply_snip(self, img):
# do SNIP if requested
Expand Down Expand Up @@ -410,7 +417,7 @@ def apply_snip(self, img):

return img

def apply_masks(self, img):
def apply_visible_masks(self, img):
# Apply user-specified masks if they are present
from hexrdgui.masking.mask_manager import MaskManager
img = img.copy()
Expand All @@ -430,22 +437,46 @@ def apply_masks(self, img):

return img

def apply_boundary_masks(self, img):
# Apply user-specified masks if they are present
from hexrdgui.masking.mask_manager import MaskManager
img = img.copy()
total_mask = self.raw_mask
for mask in MaskManager().masks.values():
if mask.type == MaskType.threshold or not mask.show_border:
continue
mask_arr = mask.get_masked_arrays(ViewType.polar)
total_mask = np.logical_or(total_mask, ~mask_arr)
img[total_mask] = np.nan

return img

def reapply_masks(self):
# This will only re-run the final steps of the processing...
if self.snipped_img is None:
return

# Apply the masks before the polar_tth_distortion, because the
# masks should also be distorted as well.
img = self.apply_masks(self.snipped_img)

img = self.apply_tth_distortion(img)
# We only apply "visible" masks to the display image
img = self.apply_visible_masks(self.snipped_img)
disp_img = self.apply_tth_distortion(img)
self.display_image = disp_img

self.processed_img = img
# Both "visible" and "boundary" masks are applied to the
# computational image
comp_img = self.apply_boundary_masks(img)
comp_img = self.apply_tth_distortion(comp_img)
self.computation_img = comp_img

@property
def img(self):
return self.processed_img
return self.computation_img

@property
def display_img(self):
return self.display_image

def warp_all_images(self):
self.reset_cached_distortion_fields()
Expand Down
6 changes: 5 additions & 1 deletion hexrdgui/calibration/stereo_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def eta_period(self):
def project_from_polar(self):
return HexrdConfig().stereo_project_from_polar

@property
def display_img(self):
return self.img

def detector_borders(self, det):
panel = self.instr_pv.detectors[det]

Expand Down Expand Up @@ -135,7 +139,7 @@ def draw_stereo_from_polar(self):
# Don't redraw the polar view unless we have to
self.draw_polar()

polar_img = self.pv.img
polar_img = self.pv.display_img

extent = np.degrees(self.pv.extent)
tth_range = extent[:2]
Expand Down
15 changes: 12 additions & 3 deletions hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,13 +888,21 @@ def images_dict(self):

@property
def raw_masks_dict(self):
return self.create_raw_masks_dict(display=False)

def create_raw_masks_dict(self, display=False):
"""Get a masks dict"""
from hexrdgui.masking.mask_manager import MaskManager
masks_dict = {}
for name, img in self.images_dict.items():
final_mask = np.ones(img.shape, dtype=bool)
for mask in MaskManager().masks.values():
if not mask.visible:
if display and not mask.visible:
# Only apply visible masks for display
continue

if not mask.visible and not mask.show_border:
# This mask should not be applied at all.
continue

if mask.type == MaskType.threshold:
Expand All @@ -915,7 +923,7 @@ def raw_masks_dict(self):
def masked_images_dict(self):
return self.create_masked_images_dict()

def create_masked_images_dict(self, fill_value=0):
def create_masked_images_dict(self, fill_value=0, display=False):
"""Get an images dict where masks have been applied"""
from hexrdgui.masking.mask_manager import MaskManager
from hexrdgui.create_hedm_instrument import create_hedm_instrument
Expand All @@ -932,7 +940,8 @@ def create_masked_images_dict(self, fill_value=0):
# and no panel buffers.
fill_value = 0

for det, mask in self.raw_masks_dict.items():
raw_masks_dict = self.create_raw_masks_dict(display=display)
for det, mask in raw_masks_dict.items():
if has_panel_buffers:
panel = instr.detectors[det]
utils.convert_panel_buffer_to_2d_array(panel)
Expand Down
Loading

0 comments on commit fc8b793

Please sign in to comment.