From 3a71947062798ae381a79ed39b06b167cd7b7e23 Mon Sep 17 00:00:00 2001
From: Mads Jakobsen <mads.jakobsen@xfel.eu>
Date: Fri, 14 Mar 2025 10:13:16 +0100
Subject: [PATCH] moved respective schema update into online image plotter

---
 .../src/onlinemid_karabo/processor.py         | 69 ++++---------------
 .../src/onlinemid/utils/image_plotting.py     | 46 ++++++-------
 2 files changed, 33 insertions(+), 82 deletions(-)

diff --git a/karabo_devices/shared_mem_processor/src/onlinemid_karabo/processor.py b/karabo_devices/shared_mem_processor/src/onlinemid_karabo/processor.py
index 6e4730f..52e4836 100644
--- a/karabo_devices/shared_mem_processor/src/onlinemid_karabo/processor.py
+++ b/karabo_devices/shared_mem_processor/src/onlinemid_karabo/processor.py
@@ -181,77 +181,32 @@ class OnlineMIDProcessor(TrainMatcher.TrainMatcher):
         # initalize member variable for the q bin array 
         # (Will have shape of 16x512x128)
         
-        
+        schema = Schema()
+
+        self.onlineImagePlotter.register("first_bunch", AgipdModulePlotter)
+        schema = self.onlineImagePlotter.add_schema("first_bunch", schema)
+
+        self.onlineImagePlotter.register("gain_stage", GainStagePlotter)
+        schema = self.onlineImagePlotter.add_schema("gain_stage", schema)
 
-        blub = self.onlineImagePlotter.get_schema_update("plots", 'first_bunch', AgipdModulePlotter)
-        blub = self.onlineImagePlotter.get_schema_update("plots", 'gain_stage', GainStagePlotter)
+
+        #self.onlineImagePlotter.get_schema_update("plots", 'first_bunch', AgipdModulePlotter)
+        #blub = self.onlineImagePlotter.get_schema_update("plots", 'gain_stage', GainStagePlotter)
 
         
       #  self.assembled_output = self.signalSlotable.getOutputChannel("plots")
         
       #  self.updateSchema()
         
-        
-        res = Schema()
-
-        (
-            NODE_ELEMENT(res)
-            .key('first_bunch')
-            .displayedName('first_bunch - agipd module plot')
-            .commit(),
-
-            IMAGEDATA_ELEMENT(res)
-            .key('first_bunch.plot')
-            .displayedName('plot')
-            .description('plot')
-            .commit(),
-
-            STRING_ELEMENT(res)
-            .key('first_bunch.update_interval')
-            .displayedName('Update interval')
-            .description('Update interval of the plot')
-            .assignmentOptional()
-            .defaultValue("")
-            .commit(),
-
-        )
-
-        (
-            NODE_ELEMENT(res)
-            .key('gain_stage')
-            .displayedName('gain_stage - agipd module plot')
-            .commit(),
-
-            IMAGEDATA_ELEMENT(res)
-            .key('gain_stage.plot')
-            .displayedName('plot')
-            .description('plot')
-            .commit(),
-
-            STRING_ELEMENT(res)
-            .key('gain_stage.update_interval')
-            .displayedName('Update interval')
-            .description('Update interval of the plot')
-            .assignmentOptional()
-            .defaultValue("")
-            .commit(),
-
-        )
-
-
+    
         schema_update = Schema()
         (
             OUTPUT_CHANNEL(schema_update)
             .key('plots')
-            .dataSchema(res)
+            .dataSchema(schema)
             .commit(),
         )
-
-
-
         self.updateSchema(schema_update)
-        
-        
         self.assembled_output = self.signalSlotable.getOutputChannel("plots")
         
 
diff --git a/python_package/src/onlinemid/utils/image_plotting.py b/python_package/src/onlinemid/utils/image_plotting.py
index f2c93dc..04014b3 100644
--- a/python_package/src/onlinemid/utils/image_plotting.py
+++ b/python_package/src/onlinemid/utils/image_plotting.py
@@ -100,8 +100,23 @@ class DataToPlotToHash():
 
         self.hash_keys_and_plotter = dict()
 
-
-    def get_schema_update(self, output_channel_key, hash_key, Plotter):
+    def is_hash_key_registered(self, hash_key):
+        if hash_key not in self.hash_keys_and_plotter:
+            print(f"key {hash_key} does not exist, call 'register' function in initialization() part of this karabo device!")
+            return hash
+        
+    def register(self, hash_key, Plotter):
+        if hash_key not in self.hash_keys_and_plotter:
+            print(f"key {hash_key} does not exist, init plotter")
+            self.hash_keys_and_plotter[hash_key] = { 
+                "plotter" : Plotter(),
+                "time_to_make_plot" : time.time()}
+        
+    
+    
+    def add_schema(self, hash_key, schema):
+        if not self.is_hash_key_registered(hash_key):
+            return schema
         
         from karabo.bound import (
             NODE_ELEMENT,
@@ -111,47 +126,30 @@ class DataToPlotToHash():
             Schema
             )
 
-        if hash_key not in self.hash_keys_and_plotter:
-            print(f"key {hash_key} does not exist, init plotter")
-            self.hash_keys_and_plotter[hash_key] = { 
-                "plotter" : Plotter(),
-                "time_to_make_plot" : time.time()}
             
         basenode = hash_key
-    
-        res = Schema()
-
         (
-            NODE_ELEMENT(res)
+            NODE_ELEMENT(schema)
             .key(basenode)
             .displayedName(f'{basenode} - agipd module plot')
             .commit(),
 
-            IMAGEDATA_ELEMENT(res)
+            IMAGEDATA_ELEMENT(schema)
             .key(f"{basenode}.plot")
             .displayedName('plot')
             .description('plot')
             .commit(),
 
-            STRING_ELEMENT(res)
+            STRING_ELEMENT(schema)
             .key(f"{basenode}.update_interval")
             .displayedName('Update interval')
             .description('Update interval of the plot')
             .assignmentOptional()
             .defaultValue("")
             .commit(),
-
         )
 
-
-        schema_update = Schema()
-        (
-            OUTPUT_CHANNEL(schema_update)
-            .key(output_channel_key)
-            .dataSchema(res)
-            .commit(),
-        )
-        return schema_update
+        return schema
         
     def add_to_hash(self, hash, hash_key, *args):
         
@@ -165,11 +163,9 @@ class DataToPlotToHash():
         from karabo.bound import ImageData
 
         if current_img is not None:
-            print("adding", f'{hash_key}.plot')
             hash[f'{hash_key}.plot'] = ImageData(current_img)
 
             now = time.time()
-            print("adding", f'{hash_key}.update_interval')
             hash[f'{hash_key}.update_interval'] = f'{(now - self.hash_keys_and_plotter[hash_key]['time_to_make_plot']):.1f}s' 
             self.hash_keys_and_plotter[hash_key]['time_to_make_plot'] = now
         
-- 
GitLab