From 4a22c0a011ae8fc82ae027dd693ee0fea3b3a89f Mon Sep 17 00:00:00 2001 From: Jerome Kieffer Date: Thu, 20 Feb 2025 15:00:41 +0100 Subject: [PATCH 1/3] Use dynamic mask when displaying images --- src/pyFAI/gui/pilx/MainWindow.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/pyFAI/gui/pilx/MainWindow.py b/src/pyFAI/gui/pilx/MainWindow.py index b10d54834..9adaf1d1f 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__ = "27/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,7 +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 class MainWindow(qt.QMainWindow): sigFileChanged = qt.Signal(str) @@ -278,13 +280,30 @@ 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 + 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 + print(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) image_base = ImageBase(data=image, mask=mask_image) self._image_plot_widget.setImageData(image_base.getValueData()) self._image_plot_widget.setGraphTitle(f"{posixpath.basename(dataset_path)} #{image_index}") From 1e4aefcfd4db9674d87e951745764c5285ee96af Mon Sep 17 00:00:00 2001 From: Jerome Kieffer Date: Thu, 20 Feb 2025 15:09:50 +0100 Subject: [PATCH 2/3] Remove debug --- src/pyFAI/gui/pilx/MainWindow.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pyFAI/gui/pilx/MainWindow.py b/src/pyFAI/gui/pilx/MainWindow.py index 0cd5216ba..03a89cf73 100644 --- a/src/pyFAI/gui/pilx/MainWindow.py +++ b/src/pyFAI/gui/pilx/MainWindow.py @@ -290,7 +290,6 @@ def displayImageAtIndices(self, indices: ImageIndices): else: mask_image = None detector = self.worker_config.poni.detector - print(detector) if detector: detector_mask = detector.mask if detector.shape != image.shape: From b36d4daf32ae53880103e50db4fe42d1f733a990 Mon Sep 17 00:00:00 2001 From: Jerome Kieffer Date: Thu, 20 Feb 2025 16:41:11 +0100 Subject: [PATCH 3/3] separate the mask generation into a separated method --- src/pyFAI/gui/pilx/MainWindow.py | 57 +++++++++++++++++++------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/pyFAI/gui/pilx/MainWindow.py b/src/pyFAI/gui/pilx/MainWindow.py index 03a89cf73..65e66a4e4 100644 --- a/src/pyFAI/gui/pilx/MainWindow.py +++ b/src/pyFAI/gui/pilx/MainWindow.py @@ -243,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 @@ -279,29 +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: - 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) - 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}")