diff --git a/src/pyFAI/gui/pilx/MainWindow.py b/src/pyFAI/gui/pilx/MainWindow.py index 55499c22a..65e66a4e4 100644 --- a/src/pyFAI/gui/pilx/MainWindow.py +++ b/src/pyFAI/gui/pilx/MainWindow.py @@ -33,7 +33,7 @@ __contact__ = "loic.huder@ESRF.eu" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" -__date__ = "31/01/2025" +__date__ = "20/02/2025" __status__ = "development" from typing import Tuple @@ -42,6 +42,7 @@ import logging import os.path import posixpath +import numpy from silx.gui import qt from silx.gui.colors import Colormap from silx.image.marchingsquares import find_contours @@ -62,6 +63,8 @@ from .widgets.MapPlotWidget import MapPlotWidget from .widgets.TitleWidget import TitleWidget from ...io.integration_config import WorkerConfig +from ...utils.mathutil import binning as rebin_fct +from ...detectors import Detector logger = logging.getLogger(__name__) class MainWindow(qt.QMainWindow): @@ -240,6 +243,39 @@ def displayPatternAtIndices(self, self._integrated_plot_widget.setGraphXLabel(point._x_name) self._integrated_plot_widget.setGraphYLabel(point._y_name) + def getMask(self, image, maskfile=None): + """returns a 2D array of boolean with invalid pixels masked, + combination of Detector mask, static & dynamic mask. + Handles detector/mask image binning on the fly + + :param image: 2D array image with data, used for dynamic masking + :param maskfile: filename or URL pointing to a static mask + :return: 2D array + """ + if maskfile: + mask_image = get_data(url=DataUrl(maskfile)) + if mask_image.shape != image.shape: + binning = [m//i for i, m in zip(image.shape, mask_image.shape)] + if min(binning)<1: + mask_image = None + else: + mask_image = rebin_fct(mask_image, binning) + else: + mask_image = None + detector = self.worker_config.poni.detector + if detector: + detector_mask = detector.mask + if detector.shape != image.shape: + detector.guess_binning(image) + detector_mask = rebin_fct(detector_mask, detector.binning) + if mask_image is None: + mask_image = detector_mask + else: + numpy.logical_or(mask_image, detector_mask, out=mask_image) + detector.mask = mask_image + mask_image = detector.dynamic_mask(image) + return mask_image + def displayImageAtIndices(self, indices: ImageIndices): if self._file_name is None: return @@ -276,14 +312,7 @@ def displayImageAtIndices(self, indices: ImageIndices): else: maskfile = None - if maskfile: - mask_image = get_data(url=DataUrl(maskfile)) - if mask_image.shape != image.shape: - mask_image = None - else: - mask_image = None - - image_base = ImageBase(data=image, mask=mask_image) + image_base = ImageBase(data=image, mask=self.getMask(image, maskfile)) self._image_plot_widget.setImageData(image_base.getValueData()) self._image_plot_widget.setGraphTitle(f"{posixpath.basename(dataset_path)} #{image_index}")