Skip to content

Commit d2985e5

Browse files
committed
Mask simulated texture data
This applies the same masks that were applied to the polar view to the simulated texture image. Fixes part of: #1926 Signed-off-by: Patrick Avery <[email protected]>
1 parent 690751f commit d2985e5

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

hexrdgui/calibration/polarview.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,16 @@ def apply_intensity_corrections(self, img):
502502

503503
return img
504504

505-
def apply_visible_masks(self, img):
506-
# Apply user-specified masks if they are present
507-
img = img.copy()
508-
total_mask = self.warp_mask
505+
@property
506+
def all_masks_pv_array(self) -> np.ndarray:
507+
return np.logical_or(
508+
self.visible_mask_pv_array,
509+
self.boundary_mask_pv_array,
510+
)
511+
512+
@property
513+
def visible_mask_pv_array(self) -> np.ndarray:
514+
total_mask = np.zeros(self.shape, dtype=bool)
509515
for mask in MaskManager().masks.values():
510516
if mask.type == MaskType.threshold or not mask.visible:
511517
continue
@@ -517,21 +523,31 @@ def apply_visible_masks(self, img):
517523
gt_mask = img > gt_val
518524
mask = np.logical_or(lt_mask, gt_mask)
519525
total_mask = np.logical_or(total_mask, mask)
520-
img[total_mask] = np.nan
521526

522-
return img
527+
return total_mask
523528

524-
def apply_boundary_masks(self, img):
525-
# Apply user-specified masks if they are present
526-
img = img.copy()
527-
total_mask = self.warp_mask
529+
@property
530+
def boundary_mask_pv_array(self) -> np.ndarray:
531+
total_mask = np.zeros(self.shape, dtype=bool)
528532
for mask in MaskManager().masks.values():
529533
if mask.type == MaskType.threshold or not mask.show_border:
530534
continue
531535
mask_arr = mask.get_masked_arrays(ViewType.polar, self.instr)
532536
total_mask = np.logical_or(total_mask, ~mask_arr)
537+
return total_mask
538+
539+
def apply_visible_masks(self, img):
540+
# Apply user-specified masks if they are present
541+
total_mask = np.logical_or(self.warp_mask, self.visible_mask_pv_array)
542+
img = img.copy()
533543
img[total_mask] = np.nan
544+
return img
534545

546+
def apply_boundary_masks(self, img):
547+
# Apply user-specified masks if they are present
548+
total_mask = np.logical_or(self.warp_mask, self.boundary_mask_pv_array)
549+
img = img.copy()
550+
img[total_mask] = np.nan
535551
return img
536552

537553
def reapply_masks(self):

hexrdgui/calibration/wppf_options_dialog.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,27 @@ def on_texture_binning_setting_changed(self):
19431943
self.invalidate_texture_data()
19441944
self.update_simulated_polar_dialog()
19451945

1946+
def _compute_2d_pv_bin_mask(self):
1947+
canvas = HexrdConfig().active_canvas
1948+
if canvas.mode != 'polar' or canvas.iviewer is None:
1949+
return
1950+
1951+
settings = self.texture_settings
1952+
1953+
# Make a float image so that any pixels touched by nans
1954+
# in the binning will be nan.
1955+
mask_float = np.zeros(canvas.iviewer.pv.shape, dtype=float)
1956+
pv = canvas.iviewer.pv
1957+
mask = np.logical_or(pv.all_masks_pv_array, pv.warp_mask)
1958+
mask_float[mask] = np.nan
1959+
binned = bin_polar_view(
1960+
canvas.iviewer.pv,
1961+
mask_float,
1962+
settings['azimuthal_interval'],
1963+
settings['integration_range'],
1964+
)
1965+
return np.isnan(binned)
1966+
19461967
def _compute_2d_pv_bin(self):
19471968
canvas = HexrdConfig().active_canvas
19481969
if canvas.mode != 'polar' or canvas.iviewer is None:
@@ -1967,11 +1988,16 @@ def _compute_2d_pv_sim(self):
19671988
# Nothing we can do
19681989
return None
19691990

1991+
# Compute the mask to apply
1992+
mask = self._compute_2d_pv_bin_mask()
1993+
19701994
try:
19711995
# Make sure the `eta_step` is up-to-date
19721996
obj.eta_step = self.texture_settings['azimuthal_interval']
19731997
obj.computespectrum_2D()
1974-
return obj.simulated_2d
1998+
img = obj.simulated_2d.copy()
1999+
img[mask] = np.nan
2000+
return img
19752001
finally:
19762002
if not had_object:
19772003
self.reset_object()

hexrdgui/calibration/wppf_simulated_polar_dialog.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, pv_bin: np.ndarray, pv_sim: np.ndarray | None,
3636
def setup_color_map(self):
3737
self.color_map_editor = ColorMapEditor(self, self.ui)
3838
self.ui.color_map_editor_layout.addWidget(self.color_map_editor.ui)
39-
self.color_map_editor.update_bounds(self.scaled_image_data)
39+
self.color_map_editor.update_bounds(unmasked(self.scaled_image_data))
4040
self.color_map_editor.data = self.data
4141

4242
def setup_canvas(self):
@@ -133,7 +133,7 @@ def on_data_modified(self):
133133
ax_im.set_data(self.get_scaled_image_data(i))
134134
ax_im.set_extent(self.extent)
135135

136-
self.color_map_editor.data = self.data
136+
self.color_map_editor.data = unmasked(self.data)
137137

138138
self.draw_later()
139139

@@ -148,3 +148,13 @@ def show(self):
148148

149149
def draw_later(self):
150150
self.figure.canvas.draw_idle()
151+
152+
153+
def unmasked(
154+
array: np.ndarray | np.ma.MaskedArray,
155+
fill_value=np.nan,
156+
) -> np.ndarray:
157+
if isinstance(array, np.ma.MaskedArray):
158+
return array.filled(fill_value)
159+
160+
return array

0 commit comments

Comments
 (0)