From 0e210d4b9059cc93762ac8d3aa00e52f330e4124 Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Wed, 11 May 2022 14:41:32 +0200
Subject: [PATCH] LPD: make bad pixel masking value configurable

---
 src/calng/LpdCorrection.py | 47 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/calng/LpdCorrection.py b/src/calng/LpdCorrection.py
index b906d795..fc602da1 100644
--- a/src/calng/LpdCorrection.py
+++ b/src/calng/LpdCorrection.py
@@ -6,6 +6,7 @@ from karabo.bound import (
     DOUBLE_ELEMENT,
     KARABO_CLASSINFO,
     OVERWRITE_ELEMENT,
+    STRING_ELEMENT,
     VECTOR_STRING_ELEMENT,
 )
 
@@ -287,6 +288,23 @@ class LpdCorrection(BaseCorrection):
             expected, LpdCorrection._managed_keys, LpdCorrection._correction_field_names
         )
 
+        # additional settings for correction steps
+        (
+            STRING_ELEMENT(expected)
+            .key("corrections.badPixels.maskingValue")
+            .displayedName("Bad pixel masking value")
+            .description(
+                "Any pixels masked by the bad pixel mask will have their value "
+                "replaced with this. Note that this parameter is to be interpreted as "
+                "a numpy.float32; use 'nan' to get NaN value."
+            )
+            .assignmentOptional()
+            .defaultValue("nan")
+            .reconfigurable()
+            .commit(),
+        )
+        LpdCorrection._managed_keys.add("corrections.badPixels.maskingValue")
+
         # mandatory: manager needs this in schema
         (
             VECTOR_STRING_ELEMENT(expected)
@@ -305,6 +323,16 @@ class LpdCorrection(BaseCorrection):
             self.unsafe_get("dataFormat.pixelsY"),
         )
 
+    def __init__(self, config):
+        super().__init__(config)
+        try:
+            bad_pixel_mask_value = np.float32(
+                config.get("corrections.badPixels.maskingValue")
+            )
+        except ValueError:
+            bad_pixel_mask_value = np.float32("nan")
+        self._kernel_runner_init_args = {"bad_pixel_mask_value": bad_pixel_mask_value}
+
     def _load_constant_to_runner(self, constant, constant_data):
         self.kernel_runner.load_constant(constant, constant_data)
         correction_step = {
@@ -390,3 +418,22 @@ class LpdCorrection(BaseCorrection):
                 ),
                 metadata,
             )
+
+    def preReconfigure(self, config):
+        # TODO: DRY (taken from AGIPD device)
+        super().preReconfigure(config)
+        if config.has("corrections.badPixels.maskingValue"):
+            # only check if it is valid (let raise exception)
+            # if valid, postReconfigure will use it
+            np.float32(config.get("corrections.badPixels.maskingValue"))
+
+    def postReconfigure(self):
+        super().postReconfigure()
+        if not hasattr(self, "_prereconfigure_update_hash"):
+            return
+
+        update = self._prereconfigure_update_hash
+        if update.has("corrections.badPixels.maskingValue"):
+            masking_value = np.float32(update["corrections.badPixels.maskingValue"])
+            self._kernel_runner_init_args["bad_pixel_mask_value"] = masking_value
+            self.kernel_runner.bad_pixel_mask_value = masking_value
-- 
GitLab