Skip to content

Commit

Permalink
Move masking functions to MaskManager class
Browse files Browse the repository at this point in the history
Signed-off-by: Brianna Major <[email protected]>
  • Loading branch information
bnmajor committed Dec 20, 2023
1 parent 65974c4 commit b15c9d6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 89 deletions.
2 changes: 1 addition & 1 deletion hexrdgui/calibration/auto/powder_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _run(self):
fwhm_estimate = options['initial_fwhm']

# Get an intensity-corrected masked dict of the images
img_dict = HexrdConfig().masked_images_dict
img_dict = MaskManager().masked_images_dict

statuses = self.refinement_flags_without_overlays
self.cf = statuses
Expand Down
6 changes: 3 additions & 3 deletions hexrdgui/calibration/calibration_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def run_calibration(self):
flags = HexrdConfig().get_statuses_instrument_format()
instr.calibration_flags = flags

img_dict = HexrdConfig().masked_images_dict
img_dict = MaskManager().masked_images_dict

instr_calibrator = run_calibration(picks, instr, img_dict, materials)
self.write_instrument_to_hexrd_config(instr)
Expand Down Expand Up @@ -758,7 +758,7 @@ def auto_pick_powder_points(self):
options = HexrdConfig().config['calibration']['powder']
self.instr = create_hedm_instrument()

img_dict = HexrdConfig().masked_images_dict
img_dict = MaskManager().masked_images_dict

statuses = HexrdConfig().get_statuses_instrument_format()
self.instr.calibration_flags = statuses
Expand Down Expand Up @@ -840,7 +840,7 @@ def auto_pick_laue_spots(self):
self.async_runner.run(self.run_auto_laue_pick)

def run_auto_laue_pick(self):
img_dict = HexrdConfig().masked_images_dict
img_dict = MaskManager().masked_images_dict

# These are the options the user chose earlier...
options = HexrdConfig().config['calibration']['laue_auto_picker']
Expand Down
2 changes: 1 addition & 1 deletion hexrdgui/calibration/stereo_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def extent(self):

@property
def raw_img_dict(self):
return HexrdConfig().masked_images_dict
return MaskManager().masked_images_dict

@property
def stereo_size(self):
Expand Down
84 changes: 0 additions & 84 deletions hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,76 +855,6 @@ def images_dict(self):
"""Default to intensity corrected images dict"""
return self.intensity_corrected_images_dict

@property
def raw_masks_dict(self):
"""Get a masks dict"""
# We must ensure that we are using raw masks
from hexrdgui.create_raw_mask import rebuild_raw_masks
prev_masks = copy.deepcopy(self.masks)
try:
rebuild_raw_masks()
masks = copy.deepcopy(self.masks)
finally:
self.masks = prev_masks

masks_dict = {}
images_dict = self.images_dict
for name, img in images_dict.items():
final_mask = np.ones(img.shape, dtype=bool)
for mask_name, data in masks.items():
if mask_name not in self.visible_masks:
continue

for det, mask in data:
if det == name:
final_mask = np.logical_and(final_mask, mask)
if (self.threshold_mask_status and
self.threshold_masks.get(name) is not None):
idx = self.current_imageseries_idx
thresh_mask = self.threshold_masks[name][idx]
final_mask = np.logical_and(final_mask, thresh_mask)
masks_dict[name] = final_mask

return masks_dict

@property
def masked_images_dict(self):
return self.create_masked_images_dict()

def create_masked_images_dict(self, fill_value=0):
"""Get an images dict where masks have been applied"""
from hexrdgui.create_hedm_instrument import create_hedm_instrument

images_dict = self.images_dict
instr = create_hedm_instrument()

has_masks = bool(self.visible_masks)
has_panel_buffers = any(panel.panel_buffer is not None
for panel in instr.detectors.values())

if not has_masks and not has_panel_buffers:
# Force a fill_value of 0 if there are no visible masks
# and no panel buffers.
fill_value = 0

for det, mask in self.raw_masks_dict.items():
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

return images_dict

def save_imageseries(self, ims, name, write_file, selected_format,
**kwargs):
hexrd.imageseries.save.write(ims, write_file, selected_format,
Expand Down Expand Up @@ -2648,20 +2578,6 @@ def recent_images(self, images):
self._recent_images = v
self.recent_images_changed.emit()

def apply_masks_to_panel_buffers(self, instr):
# Apply raw masks to the panel buffers on the passed instrument
for det_key, mask in self.raw_masks_dict.items():
panel = instr.detectors[det_key]

# Make sure it is a 2D array
utils.convert_panel_buffer_to_2d_array(panel)

# Add the mask
# NOTE: the mask here is False when pixels should be masked.
# This is the same as the panel buffer, which is why we are
# doing a `np.logical_and()`.
panel.panel_buffer = np.logical_and(mask, panel.panel_buffer)

def clean_panel_buffers(self):
# Ensure that the panel buffer sizes match the pixel sizes.
# If not, clear the panel buffer and print a warning.
Expand Down

0 comments on commit b15c9d6

Please sign in to comment.